rahuldottech

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

Command line arguments on Windows

Aug 032017

The following command shows how command line arguments basically work on Windows:

executable.exe arg1 "this is arg2" \"arg3 arg4\"

These are the basics:

  1. Arguments are separated by spaces
  2. Strings inside double quotes are treated as a single argument
  3. To include a literal double quote, escape it using a backslash

Seems easy enough, right? Well, this will cover you for most cases, except...

Try the following out:

Command Arguments passed
executable.exe \\
\\
executable.exe \"
"
executable.exe \\"
\
executable.exe \\\"
\"

The last two cases don't make a lot of sense... So what's going on here? 

Turns out that backslashes can be used to escape backslashes themselves, but only when they are followed by double quotes... Confusing? Let me explain.

I've created a simple executable which tells me the command line arguments using code from here. Now let us try the second-to-last case again:

>executable.exe \\"
There are 2 arguments:
0 executable.exe
1 \

And now let us try the same thing with some extra text at the end:

>executable.exe \\" hi hi" lol
There are 3 arguments:
0 executable.exe
1 \ hi hi
2 lol

What do we see? The first backslash is escaping the second backslash, which is why the second backslash isn't escaping the double quotes, which is why the text between the quotes is getting treated as a single argument!

This also explains the last case. The third backslash is escaping the double quotes!

Decoding Percent-Encoded Data from CMD

Jul 302017

A couple of months ago I needed to decode percent-encoded POST form data in a batch file. I could find no existing scripts or utilities to do so, so I wrote my own.

You can find the Github repository here: https://github.com/rahuldottech/percent-decoder

It is written in pure Windows Batch, and performs surprisingly well. 

Like all my other code, it is licensed under the MIT license.

Usage:

percentdecoder.cmd <encoded-string-goes-here>

(it works even if you rename the file)

Enjoy!

If you find a bug, open an Issue on Github, leave a comment bellow or send me an email at <code@mydomain>.

Using diskpart to reformat corrupt drives

Jun 292017

Original posted on 18th May 2017 at http://rahul2001.com/weblog/2017/05/using-diskpart-to-reformat-corrupt-drives

Okay, so. Over the years I've come across several corrupt drives. And in my experience, if it is at all possible to make them usable again, this method will do it for you:

  1. Open cmd as an administrator.
  2. Run diskpart.
  3. Type in select disk.
  4. Then enter select disk <yourdisk>.
  5. Type in clean.
  6. Then enter select disk <yourdisk> again.
  7. Finally run the command create partition primary.
  8. Open Windows Explorer, right click on your drive, and format it.

That's it, you should be able to use your drive normally again!

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!

Atom

▬▬ι═══════ﺤ -═══════ι▬▬