Rahul's Website

Tech, coding, security and other random stuff...

Creating a simple HTTP server from CMD using Netcat on Windows

Jun 292017

Original posted on 16th April 2017 at http://rahul2001.com/weblog/2017/04/creating-a-simple-http-from-cmd-using-netcat-on-windows

Code is under the MIT License as always.

You can download netcat for Windows here.

Basics

The following command creates a simple HTTP server from CMD:

@echo off
:main
(type index.html) | nc -w 1 -l -p 80 
goto :main

Here index.html is the file that is displayed to whoever accesses the webpage. It can be viewed by typing localhost in the browser on the same computer or your computer's IP address on any other computer on the network.

The first two lines of index.html must be the following to ensure proper working:

HTTP/1.0 200 OK
Content-Type: text/html

This is the file which I use for testing:

HTTP/1.0 200 OK
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
        <title>Hi!</title>
    </head>
<body>
    <h1>Hello World!</h1>
    <p>Congrats! Your server is working!</body>
</html>

Advanced

You can also get information via POST requests!

HTML Body:

<body>
    <h1>hello world</h1>
    <form method="post">
    send something: <input type="text" name="postText" />
    <input type="submit" value="Submit" />
    </form>
</body>

And on the backend:

(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText"

However, this just displays the output. In order to fetch the input in a variable so that you can carry out further operations, try this:

(type files\index1.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
        echo User input: %postText%

There is a bug in this code though. Often browsers will send the information multiple times, and will also sometimes send empty requests. To get around this, we can use the following code:

set postPrev=rahul2001BLEH
(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
if NOT %postText% == "" (
    if NOT %postText% == %postPrev% (
        echo User input: %postText%
        set postPrev=%postText%
    )
)

This will ignore empty requests and those identical to the previous ones.

Complete Code

server.cmd:

@echo off
set postPrev=rahul2001BLEH
:main
(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
if NOT %postText% == "" (
    if NOT %postText% == %postPrev% (
        echo User input: %postText%
        set postPrev=%postText%
    )
)
goto :main

index.html:

HTTP/1.0 200 OK
Content-Type: text/html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
        <title>hello</title>
    </head>
<body>
    <h1>hello world</h1>
    <form method="post">
    send something: <input type="text" name="postText" />
    <input type="submit" value="Submit" />
    </form>
</body>
</html>

Enjoy!

Using CURL to upload files to a server

Jun 292017

Original posted on 29th March 2017 at http://rahul2001.com/weblog/2017/03/using-curl-to-upload-files-to-server

I have a script which runs remotely on a school PC, which is part of a renderfarm I build for my friend. I needed it to be able to upload the rendered file to my server. Here's how I managed to do this:

On my server I have a page with this simple form to upload the file:

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="rfile" />
<input type="submit"/>
</form>

This following command send a file via a POST request:

curl --form rfile=@renderedfile.jpg --form press=submit http://example.com/upload.php

This script is platform-independent. Enjoy!

My Portable C/C++ Coding Setup with Notepad++

Jun 292017

(or... How I got MinGW to work with Notepad++)

Original posted on 18th May 2016 at http://rahul2001.com/weblog/2016/05/my-portable-cc-coding-set-up-with-notepad

A few days ago, I decided to finally start learning C++. Until now I've only used a rather restricted version of C for robotics. However, I faced a problem: I never do all my work on a single PC. Sometimes I work in school, sometimes at home on my desktop, and sometimes at my grandparents' house on my laptop.
I usually use a portable version of Notepad++ for coding, but then again, most of my code is either web development or batch scripting - stuff that doesn't need a compiler.

Getting g++ to work with proper Notepad++ installations is easy, but this method doesn't work with portable versions since you cannot rely on environment variables. So I spent some time thinking about this issue, and came up with the following solution to integrate a portable version of Notepad++, my favorite editor, with MinGW:

  • Download Notepad++ Portable
  • Add the NppExec plugin from the Plugin Manager.
  • Download MinGW for Windows.
  • Extract the contents of the archive you downloaded.
  • Copy both to a portable drive.
  • Download this small utility I made: r-launch. This basically allows you to run MinGW commands from the Notepad++ directory.
  • Save r-launch.exe to the Notepad++Portable\App\Notepad++ folder of your portable installation.
  • Create a text file called r-launch.txt in the same directory and store the path of the MinGW installation in it. My MinGW installation was in G:\Coding\MinGW and my Notepad++ executable was in G:\Software\Notepad++Portable\App\Notepad++, so my text file read:
    ..\..\..\..\Coding\MinGW
  • Open the NppExec windows in Notepad++ and type your MinGW commands like you normally would, only with r-launch.exe in front of them.
  • You're Done!

Here's the code I use in the NppExec window:

Compile:
Saves and compiles in the same directory with the same name

npp_save
$(NPP_DIRECTORY)\r-launch.exe g++ "$(FULL_CURRENT_PATH)" -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe

Run in NppExec Console:
Runs the compiled version of the program.

    $(CURRENT_DIRECTORY)\$(NAME_PART).exe

Run in cmd:
Runs the compiled version of the program.

npp_run CURRENT_DIRECTORY)\$(NAME_PART).exe

Compile and Run in NppExec Console:

npp_save
$(NPP_DIRECTORY)\r-launch.exe g++ "$(FULL_CURRENT_PATH)" -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe
$(CURRENT_DIRECTORY)\$(NAME_PART).exe

Compile and Run in cmd:

npp_save
$(NPP_DIRECTORY)\r-launch.exe g++ "$(FULL_CURRENT_PATH)" -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe
npp_run $(CURRENT_DIRECTORY)\$(NAME_PART).exe

These commands use g++, but you can use any of the utilities that comes with MinGW.

Enjoy!


The source code of r-launch (licensed under the MIT License):

@echo off
Title r-launch: Rahul's MinGW Launcher
if [%1]==[] goto :help
goto :launch

:help
echo r-launch: Rahul's MinGW Launcher for Notepad++
echo http://rahul2001.com/
echo ----------------------------------------------
echo This file lets you add the MinGW utilities to
echo Notepad++.
echo:
echo Instructions:
echo 1. Save this executable to the same folder as
echo    the Notepad++.
echo 2. Save the path of the MinGW folder in a text
echo    file named 'r-launch.txt' in the same 
echo    folder as this executable.
echo 3. Run your commands with 'r-launch' in front
echo    of them from the NppExec plugin for
echo    Notepad++.
if %0 == "%~0" pause
goto :eof

:launch
(
set /p path=
)<r-launch.txt

if not exist r-launch.txt goto error1
if not exist notepad++.exe goto error2
if not exist %path% goto error3
if not exist %path%\set_distro_paths.bat goto error4

call %path%\set_distro_paths.bat
%*
goto :eof

:error1
echo ERROR: 'r-launch.txt' doesn't exist in the same directory as this executable.
goto :eof
:error2
echo ERROR: 'notepad++.exe' doesn't exist in the same directory as this executable.
goto :eof
:error3
echo ERROR: The directory specified in r-launch.txt doesn't exist.
goto :eof
:error4
echo ERROR: 'set_distro_paths.bat' doesn't exist at in the directory specified in r-launch.txt.
goto :eof

A simple PHP form

Jun 212017

NOTE (a year later): I usually use this with automated scripts, and when I cannot make use of databases. It is not the best designed system, and I'm only leaving it here for historical reasons.

Originally published on 16th May 2016 at http://rahul2001.com/weblog/2016/05/a-simple-php-form

Hi! This is a simple databaseless script to save form data to a file. The code is split into four parts:
- The HTML form
- The PHP code
- The data file
- The .htaccess file



The HTML:
Your document should contain a similar form:

<form novalidate="" method="post" action="submit.php" id="SchoolDetails">
<label for="School">School School:</label>
<input id="School" name="School" type="text" placeholder="School" value="" spellcheck="false" class="">
<br />
<label for="TeamSchool">Team Name:</label>
<input id="Team" name="Team" type="text" placeholder="Team" value="" spellcheck="false" class="">
</form>
<input id="submit" name="submit" type="submit" value="Submit!">

Every Input must have the 'name' attribute. Remember to change submit.php according to the name of your PHP file


The PHP:
This is the PHP code that does the actual work:

<?php
    header("Location: thanks.html"); //This is the page to which the user will be redirected...
    $handle = fopen("userdata.txt", "a"); //this is the name of the text file in which all the data will get stored...
    $post = $_POST;
    $post['----------=User Information'] = '----------';
    $post['User IP'] = $_SERVER['REMOTE_ADDR']; //Saves the IP Address
    $post['Browser/UserAgent'] = $_SERVER['HTTP_USER_AGENT']; //Saves the User Agent
    $post['Referrer'] = $_SERVER['HTTP_REFERER']; //Saves the URL of the page which contains the form
    $post['HTTPS'] = $_SERVER['HTTPS']; //Is the script queried through a secure HTTP protocol?   
    $post['Date&Time'] = date("l jS \of F Y h:i:s A"); //Saves the date and time at which the form was submitted
    //add more global variables here
    $post['========================END'] = '=======================';
    //Save all the form data as Input_Name=Input_Value
    foreach($post as $variable => $value) 
    {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, PHP_EOL);
    }
    fwrite($handle, PHP_EOL);
    fclose($handle);
    exit;
?>

You can add many more PHP global variables. Change thanks.html to point to the webpage to which the users are redirected once they submit the form. userdata.txt is the file to which everything is stored. You can name it anything, but be sure to change the file name everywhere in the code.


The data file:
Just create an empty file called userdata.txt or whatever you want to call it and everything will be saved to it.
Note: It doesn't have to be a .txt file, it can be any format, just make sure it isn't .php because that can be a big security vulnerability.


The .htaccess file:
Now, you have to make a decision. Do you want to be able to see the result on the go without logging in every time? If so, rename your data file to something unguessable like MyUserData5566789334.txt so that only you know the URL. After this, just visit the file in your web browser to see the entries. However, if you want something more secure, edit the .htaccess file in the directory in which your file is stored to contain the following:

<Files "userdata.txt">
Order Allow,Deny
Deny from all
</Files>

The downside of this is that you will have to use FTP or your server's web-based administration system to view the file every time.

That's it! Enjoy your new submission form!

Check Java Installation Using a Batch File

Jun 212017

Original posted on 8th May 2016 at http://rahul2001.com/weblog/2016/05/batch-file-to-check-if-java-is-installed

Here's a handy script to check the installation of Java:

REM get javaw.exe from the registry entry
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i

REM if reg entry is not found, java is not installed
if "%JAVAW_PATH%"=="" ( goto java_error ) else ( goto java_installed )

If java doesn't exist, java_error is called. If it exists, java_installed is called