When I run this command cat index.html | nc -lnvp 2222 and then open the local address of the server in the browser with this header:
GET / HTTP/1.1
Host: 192.168.146.131:2222
User-Agent: "my user agent here"
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
and this as the index.html: hi whats up (thats it)
I get the http request in the terminal where netcat is running and my browser on my other machine is waiting. Only when I CTRL-C the connection on the terminal I get the response in the browser.
My uname -a prints out: Linux kali 4.0.0-kali1-amd64 #1 SMP Debian 4.0.4-1+kali2 (2015-06-03) x86_64 GNU/Linux
When I try to use cat index.html | nc -l 2222 it doesnt work at all. My Kali machine doesnt even get a http request. When I try the same on an Ubuntu machine it works like I want to it to work: It simply sends the index.html to the browser without waiting for me to CTRL-C netcat.
Anyone an idea why netcat behaves so weird?
There are two different netcats, and you're using one of each:
Traditional netcat will avoid closing the connection by default, because the client might send more data.
OpenBSD netcat closes the connection by default when there's no more data to send.
Old versions of HTTP (like you're falling back on) expects the connection to be closed, so it works by default with the OpenBSD netcat.
You can have traditional netcat close the connection on eof as well, using -q 0:
stuff | nc.traditional -l -p 2222 -q 0
man nc writes:
"netcat stays running until the network side closes" so your browser will wait forever for the data to finish. Use -q option to quit, see the man page again: "after EOF on stdin, wait the specified number of seconds and then quit".
cat index.html | nc -lnvp 2222 -q 0
nc -l 2222 does not make sense, you need to use -p to specify the port.