资源描述
http tunnel 原理 及 穿透防火墙方法-1
tunnel 很多文章解释为 "隧道", "通道".
这里所谓的 tunnel 是指一种绕过防火墙端口屏蔽的通讯方式, 可以有效地解决防火墙对端口的屏蔽作用.
原理:
防火墙两端的数据包封装在防火墙所允许通过的数据包类型或是端口上,然后穿过防火墙与对端通讯,当封装的数据包到达目的地时,再将数据包还原,并将还原后的数据包交送到相应的服务进程上。
举例如下:
A 主机系统在防火墙之后,受防火墙保护。防火墙配置的访问控制原则是只允许80端口的数据进出,屏蔽了其他的所有端口. B主机系统在防火墙之外,是开放的。 现在假设需要从 A 系统Telnet到B系统上去,怎么办?
使用正常的Telnet肯定是不可能了,但我们知道可用的只有80端口,那么这个时候使用http Tunnel,就是一个好的办法。思路如下: 在A 机器上运行一个 Tunnel 的Client端,让它侦听本机的一个不被使用的任意指定端口(Port>1024 and port < 65535),如1234. 同时将来自1234端口上的数据导向到远端(B机)的80端口上(注意,是80端口,防火墙允许通过),然后在B机上运行一个tunnel Server,同样在80端口上监听,然后把 80 端口上接收到的数据 (数据由 tunnel client传来)转到本机的 Telnet 服务端口23,这样就ok了。
根据刚才的设置, 数据流程大概是:
[telnet.exe:任意端口] ---> [tunnel client.exe:1234] ---->[Firewall]---->[tunnel server.exe:80]---->[telnet Server.exe:23]
说明:
telnet.exe 和 tunnel client.exe 是在同一个机器上的.
tunnel server.exe 和 telnet Server.exe 是在同一台机器上的.
按照流程图:
telnet.exe: 把发送的数据转向到 tunnel client.exe 的 1234 端口
tunnel client.exe 把数据发送给 tunnuel server.exe:80 ( 这次连接外网机器的80端口了, 防火墙应该是没有异议吧 )
Friewall 只允许 80 端口的数据进出.
tunnel server.exe 负责把接收到的数据转发给 telnet 的服务进程, 并可以接收 telnet 服务进程的数据
telnet Server.exe 把要发送的数据转给 tunnel server.exe,由它把数据经 80 端口发给 tunnel client.exe.
HTTP Tunnel原理 及 穿透防火墙方法-2
这篇文章提供一个通过代理服务器建立 TCP 连接来绕过防火墙的方法.
通常情况下防火墙限制了很多端口的连接,但是 HTTP 的连接还是允许的(否则谁也上不了网)。
协议指定了一个 CONNECT 请求方法. Client 可以使用这个方法通知 Proxy Server 连接指定的服务器IP和端口号. Proxy Server
在接收到这个请求后与指定的服务器IP和端口号建立连接,如果连接失败会通知 Client 并关闭连接,成功则给 Client 发送
"Coonection Established" 并保持连接. 在与 Client 和 Real Server 建立连接后,Proxy Server 就不关心数据的内容了,此时就表现为 tunnel 了.
[Client]----[FireWall]------>[Proxy Server:80]------->[Real Server]
| | | |
Src 只允许80端口数据通过 数据转发 Dest
相关协议:
在 Client一端, 我们只对 CONNECT 方法感兴趣. 在 Client 和 ProxyServer 建立连接后, Client 必须发送 CONNECT 请求.
格式如下:
CONNECT <destination_address>:<destination_port> <http_version><CR><LF>
<header_line><CR><LF>
<header_line><CR><LF>
...
<header_line><CR><LF>
<CR><LF>
Proxy Server 处理来自 Client 的请求, 然后和 destionation_address:destination_port 建立连接.
proxy server 给 Client 返回 HTTP 回应.
格式如下:
<http_version> <code> <message><CR><LF>
<header_line><CR><LF>
<header_line><CR><LF>
...
<header_line><CR><LF>
<CR><LF>
如果 Proxy Server 分析 Client 请求格式有误或者与 destionation_address:destination_port 连接失败,都会关闭连接.
如果成功则可以转发数据, 作为 tunnel 使用.
HTTP隧道(HTTP代理Socket客户)
来源: 2007-2-14 06:40:20 字体:[大 中 小] ~我要投稿! 收藏
HTTP 隧道 (HTTP代理Socket客户)
--------------------------------------------------------------------------------
这篇文章贡献自Akash kava, 翻译: bugfree/CSDN
环境: VC6
mr ans
※HTTP 隧道※
--------
HTTP是基于文本的通过浏览器检索网页的协议。 大多数情况下你躲在代理服务器的后面,通过LAN接入互联网。 在IE的Connection Option中, 你给出你的LAN的设置。 这个代理服务器运行着基于文本的协议, 你从它那里可以得到外界的网络HTTP相关的数据。是的, 用HTTP通过它上面的小的望孔可以连接到外部世界, 并用二进制协议得到你想要的数据, 或者甚至是你的协议。 它通过HTTP。
字串8
※HTTPS 解释※
---------
在HTTPS中, 数据以一种安全的方式从浏览器到服务器和从服务器到浏览器。 它是二进制的协议; 当他穿过代理时, 代理不知道是什么。 代理仅仅允许二进制流打开, 让服务器和客户两者之间交换数据。 代理服务器认为我们在进行某个安全的会话。
字串9
对于HTTPS, 你的浏览器连接到代理服务器,并送出一个命令 字串9
CONNECT :443 HTTP/1.0 <CR><LF>
HOST :443<CR><LF>
【...如果需要,HTTP头部的其它行以<CR><LF>结束 】
<CR><LF> // 最后的空行 mr ans
接下来, 代理服务器把它作为某个HTTP安全会话, 打开一个到需求服务器和端口的二进制流。 如果连接确立, 代理服务器返回如下回应: m r a n s
HTTP/1.0 200 Connection Established<CR><LF>
【...忽略所有HTTP头部的其它行以<CR><LF>结束,】
<CR><LF> // 最后的空行
字串7
现在, 浏览器连接到了终端服务器, 可以用二进制和安全的方式交换数据了。 字串8
※怎样做这个※
-------------
现在是你的程序任务去愚弄代理服务器, 行为如IE一样进行 Secure HTTP。 mr ans
1. Connect to Proxy Server first.
2. Issue CONNECT Host:Port HTTP/1.1<CR><LF>.
3. Issue <CR><LF>.
4. Wait for a line of response. If it contains HTTP/1.X 200 , the connection is successful.
5. Read further lines of response until you receive an empty line.
6. Now, you are connected to outside world through a proxy. Do any data exchange you want.
mrans
示例源代码
// You need to connect to on port 25
// Through a proxy on 192.0.1.1, on HTTP Proxy 4480
// CSocketClient is Socket wrapping class
// When you apply operator << on CString, it writes CString
// To Socket ending with CRLF
// When you apply operator >> on CString, it receives
// a Line of response from socket until CRLF mrans
try
{
CString Request,Response;
CSocketClient Client;
字串8
Client.ConnectTo("192.0.1.1",4480);
字串8
// Issue CONNECT Command
Request = "CONNECT :25 HTTP/1.0";
Client<<Request;
字串6
// Issue empty line
Request = "";
Client<<Request; mr ans
// Receive Response From Server
Client>>Response;
m r a n s
// Ignore HTTP Version
字串7
int n = Response.Find(' ');
Response = Response.Mid(n+1); mr ans
// Http Response Must be 200 only
if(Response.Left(3)!="200")
{
// Connection refused from HTTP Proxy Server
AfxMessageBox(Response);
}
字串7
// Read Response Lines until you receive an empty line.
do
{
Client>>Response;
if (Response.IsEmpty())
break;
}while (true);
字串6
// Coooooooool.... Now connected to :25
// Do further SMTP Protocol here..
}
catch (CSocketException * pE)
{
pE->ReportError();
}
字串6
※库源码※
-------------
文件Dns.h包含所有所有DNS相关的源代码。 它利用了其它的库, 如SocketEx.h, SocketClient.h, 和 NeuroBuffer.h 字串6
※CSocketEx※
-------------
字串6
作为一个Socket功能的包裹(wapper)类。(如果你不是确切知道CSocket怎样工作的, 它是非常笨重和不可信的) 所有的函数根CSocket同名。 你可以直接应用这个类
字串8
※CSocketClient※
-----------------
派生自CSocketEx, 并且根据详细的Winsock错误抛出适当地例外(exceptions). 为了方便的发送和接收,它定义了两个操作符, >> 和 <<; 如果需要它也交换网络序为主机序和主机序为网络序。 字串8
※CHttpProxySocketClient※
-----------------
派生自CSocketClient, 你可以调用SetProxySettings(ProxyServer, Port) 方法和做代理设置。 接下来, 你可以连接到你想要的主机和端口。ConnnectTo 方法被覆盖, 它自动的实现了HTTP代理协议并无争论的给你了一个连接。
字串6
字串8
※怎样利用CHttpProxySocketClient※
---------------------------------
// e.g. You need to connect to on port 25
// Through a proxy on 192.0.1.1, on HTTP Proxy 4480
// CSocketClient is Socket wrapping class
// When you apply operator << on CString, it writes CString
// To Socket ending with CRLF
// When you apply operator >> on CString, it receives
// Line of response from socket until CRLF
try
{
CHttpProxySocketClient Client;
字串9
Client.SetProxySettings("192.0.1.1",1979);
m r a n s
// Connect to server on port 25
Client.ConnectTo("",25);
m r a n s
// You now have access to on port 25
// If you do not call SetProxySettings, then
// you are connected to directly if
// you have direct access, so always use
// CHttpProxySocketClient and no need to do any
// extra coding. 字串8
}
catch(CSocketException * pE) {
pE->ReportError();
} 字串7
展开阅读全文