Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using netcat (nc) as an HTTP proxy server and monitor

Is it possible to use the Unix netcat (nc) program to create a TCP proxy server and monitor? I would like all TCP traffic to be passed across the pipe, as well as sent to stdout for monitoring. Note this will be used for monitoring HTTP traffic between a hardware device and an HTTP server.

like image 916
Landon Kuhn Avatar asked Oct 29 '10 16:10

Landon Kuhn


2 Answers

Just had the need yesterday. You can find the answer here (french) : http://www.linux-france.org/~mdecore/linux/doc/memo2/node168.html

mknod backpipe p
nc -l -p 80 < backpipe | tee -a in | nc localhost 8080 | tee -a out.html > backpipe

This listens on port 80 and redirect on port 8080. Incoming traffic will be present in the in file, outgoing traffic in the out.html file.The named pipe is needed for the connection to be bi-directional.

like image 84
Antoine Martin Avatar answered Nov 10 '22 10:11

Antoine Martin


Not netcat on its own, since it would have to interpret the HTTP request and pass it on. For example, an HTTP request through a proxy starts with:

GET http://www.example.org/ HTTP/1.1

which your proxy then has to go, 'okay, I gotta connect to example.org and GET /'.

Now this could maybe be done by piping the nc output into a script which parses the HTTP req and then calls 'wget' to get the page, then slurp that back through netcat... oh heck, why?

Apache, or squid can probably do the job.

like image 42
Spacedman Avatar answered Nov 10 '22 11:11

Spacedman