Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Last active June 5, 2021 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankSpierings/6a0de50dfc99a80b12427739d605338e to your computer and use it in GitHub Desktop.
Save FrankSpierings/6a0de50dfc99a80b12427739d605338e to your computer and use it in GitHub Desktop.
Windows Reverse Port Forwarding using C# / Powershell

Socat

  • On the lhost listening side you can use socat to create two server sockets.
socat -dd TCP-LISTEN:4444,reuseaddr,fork TCP-LISTEN:1234,reuseaddr
  • Once WPF connected to port 4444, you can talk to 127.0.0.1:1234 as if it where the remote host.
/*
Reverse port forwarder; connects reverse (left) to forward (right).
Find csc.exe:
cd /d %SYSTEMROOT% && dir csc.exe /B /S
Compile example:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe wpf.cs
*/
using System.IO;
using System.Net.Sockets;
using System;
using System.Net;
using System.Threading;
public class WPF
{
private static bool Connected(Socket s) {
if (!s.Poll(0, SelectMode.SelectRead) && s.Available == 0) {
return true;
}
else {
return false;
}
}
public static int Main(string[] args)
{
bool keepRunning = true;
if (args.Length < 4) {
Console.WriteLine("Parameters: <lhost> <lport> <rhost> <rport>");
return -1;
}
string lhost = args[0];
int lport = Int32.Parse(args[1]);
string rhost = args[2];
int rport = Int32.Parse(args[3]);;
Console.WriteLine(String.Format("Connecting {0}:{1} <==> {2}:{3}", lhost, lport, rhost, rport));
TcpClient lclient = new TcpClient(lhost, lport);
NetworkStream lstream = lclient.GetStream();
Socket lsock = lclient.Client;
TcpClient rclient;
NetworkStream rstream;
Socket rsock = null;
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
keepRunning = false;
Console.WriteLine("CTRL+C received");
};
while (keepRunning) {
if (!WPF.Connected(lsock)) {
Console.WriteLine(String.Format("Lost connection to: {0}:{1}", lhost, lport));
keepRunning = false;
}
if ((rsock == null) || (!WPF.Connected(rsock))) {
rclient = new TcpClient(rhost, rport);
rstream = rclient.GetStream();
rsock = rclient.Client;
lstream.CopyToAsync(rstream);
rstream.CopyToAsync(lstream);
}
Thread.Sleep(1000);
}
Console.WriteLine("Shutdown...");
return 0;
}
}
$cs = @"
using System.IO;
using System.Net.Sockets;
using System;
using System.Net;
using System.Threading;
public class WPF
{
private static bool Connected(Socket s) {
if (!s.Poll(0, SelectMode.SelectRead) && s.Available == 0) {
return true;
}
else {
return false;
}
}
public static int Main(string[] args)
{
bool keepRunning = true;
if (args.Length < 4) {
Console.WriteLine("Parameters: <lhost> <lport> <rhost> <rport>");
return -1;
}
string lhost = args[0];
int lport = Int32.Parse(args[1]);
string rhost = args[2];
int rport = Int32.Parse(args[3]);;
Console.WriteLine(String.Format("Connecting {0}:{1} <==> {2}:{3}", lhost, lport, rhost, rport));
TcpClient lclient = new TcpClient(lhost, lport);
NetworkStream lstream = lclient.GetStream();
Socket lsock = lclient.Client;
TcpClient rclient;
NetworkStream rstream;
Socket rsock = null;
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
keepRunning = false;
Console.WriteLine("CTRL+C received");
};
while (keepRunning) {
if (!WPF.Connected(lsock)) {
Console.WriteLine(String.Format("Lost connection to: {0}:{1}", lhost, lport));
keepRunning = false;
}
if ((rsock == null) || (!WPF.Connected(rsock))) {
rclient = new TcpClient(rhost, rport);
rstream = rclient.GetStream();
rsock = rclient.Client;
lstream.CopyToAsync(rstream);
rstream.CopyToAsync(lstream);
}
Thread.Sleep(1000);
}
Console.WriteLine("Shutdown...");
return 0;
}
}
"@;
Add-Type -Language CSharp $cs
[WPF]::Main(("10.10.14.1", "4444", "127.0.0.1", "8080"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment