
TCP Port Forwarding Utility on C
A high-performance TCP port forwarder for Windows with optional IP whitelisting and verbose logging capabilities.
cl PortForwarder.c /Fe:PortForwarder.exe ws2_32.lib
gcc PortForwarder.c -o PortForwarder.exe -lws2_32
PortForwarder.exe <local_port> <remote_host> <remote_port> [allowed_ip] [-v]
<local_port> - Local port to listen on (1-65535)<remote_host> - Remote hostname or IP address to forward traffic to<remote_port> - Remote port to forward traffic to (1-65535)[allowed_ip] - Optional - Only accept connections from this IP address[-v] - Optional - Enable verbose mode (show rejected connections)Forward local port 8080 to remote server 192.168.1.100 port 80:
PortForwarder.exe 8080 192.168.1.100 80
Only accept connections from 192.168.1.50:
PortForwarder.exe 8080 192.168.1.100 80 192.168.1.50
Show rejected connection attempts:
PortForwarder.exe 8080 192.168.1.100 80 192.168.1.50 -v
PortForwarder.exe 3306 database.example.com 3306
PortForwarder.exe 2222 10.0.0.50 22 192.168.1.100
Listen on SMB port 445 and forward to a remote SMB endpoint. When 445 is the
local port the tool first frees the port from Windows' built-in SMB server:
PortForwarder.exe 445 10.0.0.50 445 192.168.1.100
When the local port is 445, before binding the tool will automatically:
System, PID 4, via the srvnet kernel driver).LanmanServer and the srv2 /
srvnet drivers (recursively stopping any dependent services), printing each
step for visibility.Example output:
[445] === Port 445 / SMB takeover ===
[445] Privilege check: running elevated (administrator). OK
[445] Port 445 currently held by PID 4 (System (kernel / srvnet))
[445] Disabling SMB server and releasing port 445...
[445] Stopping 'LanmanServer'...
[445] service 'LanmanServer' stopped
[445] Stopping 'srv2'...
[445] service 'srv2' stopped
[445] Stopping 'srvnet'...
[445] service 'srvnet' stopped
[445] Port 445 is now free. Proceeding to bind.
[445] (Restore SMB later with: net start LanmanServer)
⚠️ Stopping these services disables file/printer sharing on the host for the duration of the engagement. Restore it afterwards with
net start LanmanServer(the drivers restart on demand or at next boot). Use only on systems you are authorized to test.
The forwarder provides detailed logging:
[INFO] Configuration:
Local port: 8080
Remote host: 192.168.1.100
Remote port: 80
Allowed IP: 192.168.1.50 (filtered mode)
Verbose: OFF
[INFO] Listening on port 8080...
[INFO] Press Ctrl+C to stop
[INFO] New connection from 192.168.1.50:54321 ACCEPTED
[INFO] Connected to remote 192.168.1.100:80
[INFO] Connection established, forwarding traffic...
[INFO] Closing connection (Sent: 1024 bytes, Received: 2048 bytes, Total: 3072 bytes)
When -v flag is enabled, rejected connections are also logged:
[INFO] Connection from 192.168.1.99:54322 REJECTED (IP not allowed)
Each connection spawns a dedicated forwarding thread that:
The forwarder handles common network errors gracefully:
WSAECONNRESET - Connection reset by peerWSAECONNABORTED - Connection abortedWSAENETRESET - Network caused disconnectWSAETIMEDOUT - Connection timed outrunning flag for clean shutdown⚠️ Important Security Notes:
[allowed_ip] parameter in production to restrict access[ERROR] bind() failed: 10048
Solution: Another application is using the port. Choose a different local port or stop the conflicting service.
[ERROR] connect() to remote failed: 10061
Solution: Verify the remote host is reachable and the service is running on the specified port.
[ERROR] bind() failed: 10013
Solution: Ports below 1024 may require administrator privileges. Run as Administrator or use a higher port number.
[INFO] Connection from X.X.X.X:XXXXX REJECTED (IP not allowed)
Solution: Enable verbose mode (-v) to see rejected IPs, or remove IP filtering.
Press Ctrl+C to gracefully stop the forwarder. It will:
Potential features for future versions:
This software is provided as-is for educational and legitimate network administration purposes. Use responsibly and in compliance with applicable laws and regulations.
Contributions are welcome! Please ensure:
For issues, questions, or feature requests, please open an issue on the project repository.
You can also support my work on Patreon