Monday, September 19, 2016

Makeshift Webserver with NCat

Sometimes I'll be working on a project, and I'll find myself in need to share a single file over http. For instance, I may need to test that a program or script is correctly grabbing files over http with Invoke-WebRequest, or curl. I may need to quickly have a file upload available, and the only reliable way is via an http server (polycom certificates for example)

I don't want to setup IIS, WAMP, or anything permanent, I just need a quick webserver on a single port temporarily.

For this, I will use ncat.

To continue on with the use cases below, head over to https://nmap.org/ncat/ and download nmap. For this example, place the ncat.exe in the Directory you are going to be running the script from, as well as any files you are wishing to serve up.

Image File
$mime =  "image/jpeg"
.\ncat -lk -p 8080 --sh-exec "echo HTTP/1.1 200 OK\r\nContent-Type: $mime\r\n& echo(&type jpeg-home.jpg"
Assumptions
jpeg-home.jpg is in current working directory.
ncat.exe is in the current working directory
that this is a jpeg file. If it is a gif or png, you will need to adjust the mime type

HTML File
I've found that this can be done without the mime type. Perhaps this is implied if not specified.
.\ncat -lk -p 8080 --sh-exec "echo HTTP/1.1 200 OK\r\n& echo(&type index.html"

SSL Certificate
$mime = "application/pkix-cert" .\ncat -lk -p 8080 --sh-exec "echo HTTP/1.1 200 OK\r\nContent-Type: $mime\r\n& echo(&type lyncpool.crt"


Other File Types

You will need to change $mime to match your file type. The list I have used to find the appropriate mime type is available here: https://www.sitepoint.com/web-foundations/mime-types-complete-list/

No comments:

Post a Comment