关于用vb的winsock穿透代理的讨论归讨论,一直没有什么源代码放出,现在我就放源出来,省的某些人拿这所谓的"技术"去骗钱.
由于缺乏测试环境,本程序只在我自己编写的代理模拟器上测试过,其结果和腾讯qq,msn,网易泡泡穿越该模拟器时得出的结果基本一致.因此,代码可能有错误的地方,请各位有条件的用户自行改正,请见谅!
首先,是基础知识,也就是rfc文档.这个是必看内容.我的程序就是基于这些文档写出.下面是各rfc的连接,为了准确,我先提供英文版的连接,在下面的介绍中再把关键部分翻译成中文.另外,要纠正一个错误.国内很多文章说socks5代理的用户名/密码校验标准在 rfc 1928里有说明,其实这是一个完全错误的说法(我很怀疑写那文章的人有没有看过rfc),socks5用户名/密码校验标准其实是在 rfc 1929 里面说明的.
rfc 1928 - socks5 代理标准
rfc 1929 - socks5 代理用户名/密码校验标准
rfc ???? - socks4 代理标准
rfc 2616 - http1.1 标准
我们现在直入正题:先说socks5的tcp穿透(有了这个例子大家自己照这可以写udp穿透)
首先和代理服务器连接-直接用winsock去connect指定的地址端口(通常是1080)即可.然后进入细节商议阶段.
细节商议--无用户名/密码校验
rfc 1928 中对于细节商议的第一步是这样描述的:
the client connects to the server, and sends a version
identifier/method selection message:
+----+----------+----------+
|ver | nmethods | methods |
+----+----------+----------+
| 1 | 1 | 1 to 255 |
+----+----------+----------+
the ver field is set to x'05' for this version of the protocol. the
nmethods field contains the number of method identifier octets that
appear in the methods field.
the server selects from one of the methods given in methods, and
sends a method selection message:
+----+--------+
|ver | method |
+----+--------+
| 1 | 1 |
+----+--------+
if the selected method is x'ff', none of the methods listed by the
client are acceptable, and the client must close the connection.
the values currently defined for method are:
o x'00' no authentication required ---------无用户密码 00
o x'01' gssapi ---------??? gssapi ?
o x'02' username/password ---------有用户密码 02
o x'03' to x'7f' iana assigned
o x'80' to x'fe' reserved for private methods
o x'ff' no acceptable methods ---------失败 255
the client and server then enter a method-specific sub-negotiation.
换言之,就是向服务器发送三个字节的byte数组,由于是无须用户/密码校验,展开来写是 05 01 00
然后服务器返回两个字节的信息,第一个字节固定,第而个字节是说明,如果是16进制的ff(即十进制255)表示连接失败(o x'ff' no acceptable methods)根据上面的列表,我们连接成功应该第二字节为 00.
然后我们进入第二步,请看以下rfc说明:
once the method-dependent subnegotiation has completed, the client
sends the request details. if the negotiated method includes
encapsulation for purposes of integrity checking and/or
confidentiality, these requests must be encapsulated in the method-
dependent encapsulation.
the socks request is formed as follows:
+----+-----+-------+------+----------+----------+
|ver | cmd | rsv | atyp | dst.addr | dst.port |
+----+-----+-------+------+----------+----------+
| 1 | 1 | x'00' | 1 | variable | 2 |
+----+-----+-------+------+----------+----------+
where:
o ver protocol version: x'05' ------------- 固定 05
o cmd
o connect x'01' ------------- tcp方式 01
o bind x'02'
o udp associate x'03' ------------- udp方式 03
o rsv reserved ------------- 固定 00
o atyp address type of following address
o ip v4 address: x'01' ------------- ipv4 01
o domainname: x'03'
o ip v6 address: x'04'
o dst.addr desired destination address
o dst.port desired destination port in network octet
order
the socks server will typically evaluate the request based on source
and destination addresses, and return one or more reply messages, as
appropriate for the request type.
发送 05 01 00 01 + 目的地址(4字节) + 目的端口(2字节),目的地址和端口都是16进制码(不是字符串)。
例202.103.190.27 - 7201
则发送的信息为:05 01 00 01 ca 67 be 1b 1c 21
(ca=202 67=103 be=190 1b=27 1c21=7201)
关于我是怎么把16进制码换成10进制的,请自己看程序
最后,接受服务器返回数据,看rfc:
+----+-----+-------+------+----------+----------+
|ver | rep | rsv | atyp | bnd.addr | bnd.port |
+----+-----+-------+------+----------+----------+
| 1 | 1 | x'00' | 1 | variable | 2 |
+----+-----+-------+------+----------+----------+
where:
o ver protocol version: x'05' ------------ 固定 05
o rep reply field:
o x'00' succeeded ------------ 若为 00 成功 其余可以都看成失败
o x'01' general socks server failure
o x'02' connection not allowed by ruleset
o x'03' network unreachable
o x'04' host unreachable
o x'05' connection refused
o x'06' ttl expired
o x'07' command not supported
o x'08' address type not supported
o x'09' to x'ff' unassigned
o rsv reserved
o atyp address type of following address
o ip v4 address: x'01'
o domainname: x'03'
o ip v6 address: x'04'
o bnd.addr server bound address
o bnd.port server bound port in network octet order
fields marked reserved (rsv) must be set to x'00'.
可见,对于返回信息,只须判断第二字节是否为00.若为 00 连接成功,剩下的操作和直连一样,winsock可直接用senddata 和 getdata 发送/接受数据.
下面介绍需要验证用户名/密码的socks5穿透
第一步还是发送三个字节,只是内容有变,展开来写为: 05 01 02
服务器返回信息也有所不同,正确的返回为 05 02
成功后发送用户/密码信息,请看rfc 说明:
once the socks v5 server has started, and the client has selected the
username/password authentication protocol, the username/password
subnegotiation begins. this begins with the client producing a
username/password request:
+----+------+----------+------+----------+
|ver | ulen | uname | plen | passwd |
+----+------+----------+------+----------+
| 1 | 1 | 1 to 255 | 1 | 1 to 255 |
+----+------+----------+------+----------+
the ver field contains the current version of the subnegotiation,
which is x'01'. the ulen field contains the length of the uname field
that follows. the uname field contains the username as known to the
source operating system. the plen field contains the length of the
passwd field that follows. the passwd field contains the password
association with the given uname.
the server verifies the supplied uname and passwd, and sends the
following response:
+----+--------+
|ver | status |
+----+--------+
| 1 | 1 |
+----+--------+
a status field of x'00' indicates success. if the server returns a
`failure' (status value other than x'00') status, it must close the
connection.
即 发送 01 + 用户名长度(一字节) + 转换成16进制码的用户名 + 密码长度(一字节) + 转换成16进制码的密码,关于如何把用户名和密码转换为10进制byte数组,请自己看程序.
然后服务器返回两个字节的信息,只须判断第二字节,00 为成功,其余为失败.
剩下的步骤和无用户名密码校验是一样的,即
发送 05 01 00 01 + 目的地址(4字节) + 目的端口(2字节),目的地址和端口都是16进制码(不是字符串)。
例202.103.190.27 - 7201
则发送的信息为:05 01 00 01 ca 67 be 1b 1c 21
(ca=202 67=103 be=190 1b=27 1c21=7201)
关于我是怎么把16进制码换成10进制的,请自己看程序
最后接受服务器返回信息.对于返回信息,只须判断第二字节是否为00.若为 00 连接成功,剩下的操作和直连一样,winsock可直接用senddata 和 getdata 发送/接受数据.
socks4的tcp穿透(事实上,socks4只支持tcp穿透)
无用户名/密码验证
请看 rfc 说明
1) connect
the client connects to the socks server and sends a connect request when
it wants to establish a connection to an application server. the client
includes in the request packet the ip address and the port number of the
destination host, and userid, in the following format.
+----+----+----+----+----+----+----+----+----+----+....+----+
| vn | cd | dstport | dstip | userid |null|
+----+----+----+----+----+----+----+----+----+----+....+----+
1 1 2 4 variable 1
vn is the socks protocol version number and should be 4. cd is the
socks command code and should be 1 for connect request. null is a byte
of all zero bits.
我们首先还是连接服务器,然后根据rfc的格式发送数据给服务器.由于是无用户密码验证,我们需要发送9个字节的数据,展开写为 04 01 + 目标端口(2字节) + 目标ip(4字节) + 00,奇怪的是,表中的userid部分似乎是没有用的,我参照过大量的c++代码,代码中都没有体现该部分.
至于如何转换目标端口和ip为相应的byte数组,请自己看示例程序.消息发出后,服务器会返回信息,格式如下:
+----+----+----+----+----+----+----+----+
| vn | cd | dstport | dstip |
+----+----+----+----+----+----+----+----+
1 1 2 4
vn is the version of the reply code and should be 0. cd is the result
code with one of the following values:
90: request granted -------------- 成功
91: request rejected or failed -------------- 失败
92: request rejected becasue socks server cannot connect to
identd on the client
93: request rejected because the client program and identd
report different user-ids
the remaining fields are ignored.
根据rfc的说法,代理服务器返回8字节的数据,我们只要判断第二字节是否为90即可,若是90连接成功,否则失败.剩下的操作和直连一样,winsock可直接用senddata 和 getdata 发送/接受数据.
http1.1 代理的穿透
由于rfc 2616过于冗长,加上http代理穿透的步骤比socks简单,这里就不详细说明了,我只给出连接的步骤和发送数据格式.
第一步仍然是用winsock去连接代理服务器.第二步为发送请求字符,其格式为:
无用户名/密码校验 格式:
"connect" + 空格 + 目标连接地址 + ":" + 目标连接端口 + 空格 + "http/1.1" + chr(13) + chr(10) + "host:" + 空格 + 目标连接地址 + ":" + 目标连接端口 + chr(13) + chr(10) + chr(13) + chr(10)
用户名/密码验证格式:
"connect" + 空格 + 目标连接地址 + ":" + 目标连接端口 + 空格 + "http/1.1" + chr(13) + chr(10) + "host:" + 空格 + 目标连接地址 + ":" + 目标连接端口 + chr(13) + chr(10) + "authorization: basic" + 空格 + 经base64加密过后的[用户名:密码] + chr(13) + chr(10) + chr(13) + chr(10) + "proxy-authorization: basic" + 空格 + 经base64加密过后的[用户名:密码] + chr(13) + chr(10) + chr(13) + chr(10)
发送请求完毕后,将收到代理的回应,根据rfc说明(注意 status-line 和 status-code):
6 response
after receiving and interpreting a request message, a server responds
with an http response message.
response = status-line ; section 6.1
*(( general-header ; section 4.5
| response-header ; section 6.2
| entity-header ) crlf) ; section 7.1
crlf
[ message-body ] ; section 7.2
6.1 status-line
the first line of a response message is the status-line, consisting
of the protocol version followed by a numeric status code and its
associated textual phrase, with each element separated by sp
characters. no cr or lf is allowed except in the final crlf sequence.
status-line = http-version sp status-code sp reason-phrase crlf
6.1.1 status code and reason phrase
the status-code element is a 3-digit integer result code of the
attempt to understand and satisfy the request. these codes are fully
defined in section 10. the reason-phrase is intended to give a short
textual description of the status-code. the status-code is intended
for use by automata and the reason-phrase is intended for the human
user. the client is not required to examine or display the reason-
phrase.
the first digit of the status-code defines the class of response. the
last two digits do not have any categorization role. there are 5
values for the first digit:
- 1xx: informational - request received, continuing process
- 2xx: success - the action was successfully received,
understood, and accepted
- 3xx: redirection - further action must be taken in order to
complete the request
- 4xx: client error - the request contains bad syntax or cannot
be fulfilled
- 5xx: server error - the server failed to fulfill an apparently
valid request
the individual values of the numeric status codes defined for
http/1.1, and an example set of corresponding reason-phrase's, are
presented below. the reason phrases listed here are only
recommendations -- they may be replaced by local equivalents without
affecting the protocol.
status-code =
"100" ; section 10.1.1: continue
| "101" ; section 10.1.2: switching protocols
| "200" ; section 10.2.1: ok
| "201" ; section 10.2.2: created
| "202" ; section 10.2.3: accepted
| "203" ; section 10.2.4: non-authoritative information
| "204" ; section 10.2.5: no content
| "205" ; section 10.2.6: reset content
| "206" ; section 10.2.7: partial content
| "300" ; section 10.3.1: multiple choices
| "301" ; section 10.3.2: moved permanently
| "302" ; section 10.3.3: found
| "303" ; section 10.3.4: see other
| "304" ; section 10.3.5: not modified
| "305" ; section 10.3.6: use proxy
| "307" ; section 10.3.8: temporary redirect
| "400" ; section 10.4.1: bad request
| "401" ; section 10.4.2: unauthorized
| "402" ; section 10.4.3: payment required
| "403" ; section 10.4.4: forbidden
| "404" ; section 10.4.5: not found
| "405" ; section 10.4.6: method not allowed
| "406" ; section 10.4.7: not acceptable
| "407" ; section 10.4.8: proxy authentication required
| "408" ; section 10.4.9: request time-out
| "409" ; section 10.4.10: conflict
| "410" ; section 10.4.11: gone
| "411" ; section 10.4.12: length required
| "412" ; section 10.4.13: precondition failed
| "413" ; section 10.4.14: request entity too large
| "414" ; section 10.4.15: request-uri too large
| "415" ; section 10.4.16: unsupported media type
| "416" ; section 10.4.17: requested range not satisfiable
| "417" ; section 10.4.18: expectation failed
| "500" ; section 10.5.1: internal server error
| "501" ; section 10.5.2: not implemented
| "502" ; section 10.5.3: bad gateway
| "503" ; section 10.5.4: service unavailable
| "504" ; section 10.5.5: gateway time-out
| "505" ; section 10.5.6: http version not supported
| extension-code
可知,如果连接成功,服务器返回的信息是 "http/" + 代理版本 + "200" + 描述("connection established")
所以我们只要判断返回的信息是否以"http"开头,是否存在" 200 "字眼即可.
以下是关键函数的源代码:
public function proxystep(proxytype as integer, pstep as integer)
dim sendbyte() as byte
if proxytype = 0 then '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sock4代理
if pstep = 1 then
redim sendbyte(0 to 8) as byte
sendbyte(0) = 4 ' 04
sendbyte(1) = 1 ' 01
sendbyte(2) = int(destport / 256)
sendbyte(3) = destport mod 256
sendbyte(4) = getipbyte(1, destip)
sendbyte(5) = getipbyte(2, destip)
sendbyte(6) = getipbyte(3, destip)
sendbyte(7) = getipbyte(4, destip)
sendbyte(8) = 0 '最后要以 0 结束
form1.winsock1.senddata sendbyte()
connstep = pstep + 1
exit function
end if
if pstep = 2 then '代理回复,第二字节为 90 为成功,其余值为失败
if asc(mid(revbuffer, 2, 1)) <> 90 then
debug.print asc(mid(revbuffer, 2, 1))
msgbox "连接sock4代理失败!", 48, "错误"
form1.winsock1.close
connstep = 0
exit function
else
form1.label8.caption = "连接目标服务器成功!"
connstep = -1
form2.show
exit function
end if
end if
end if
'*******************下面的例子有大量重复代码,是为了让大家更清楚地了解sock5穿透过程,大家可以拿回去自己优化 **********************************
if proxytype = 1 then '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sock5代理
select case pstep
case 1
redim sendbyte(0 to 2) as byte '第一步 无验证发送 05 01 00, 有验证发送 05 02 02
sendbyte(0) = 5 ' 05
sendbyte(1) = 1 '01 '在有用户密码验证时此字节是 1 还是 2 有诸多争论,现以腾讯qq穿越代理模拟器时发送的数据为准,如有错误,请自己修改!
sendbyte(2) = iif(form1.check1.value = 0, 0, 2) '00 或 02
form1.winsock1.senddata sendbyte()
connstep = pstep + 1
exit function
case 2 '代理回复
if asc(mid(revbuffer, 2, 1)) = 255 then 'ff (255) 为失败
msgbox "连接代理失败!", 64
form1.winsock1.close
connstep = 0
exit function
end if
if asc(mid(revbuffer, 2, 1)) = 0 and asc(mid(revbuffer, 1, 1)) = 5 then '若代理回复 05 00 为无验证连接成功
form1.label8.caption = "连接成功!无验证"
redim sendbyte(0 to 9) as byte '第二步 无验证 发送连接请求
sendbyte(0) = 5
sendbyte(1) = 1
sendbyte(2) = 0
sendbyte(3) = 1
sendbyte(4) = getipbyte(1, destip)
sendbyte(5) = getipbyte(2, destip)
sendbyte(6) = getipbyte(3, destip)
sendbyte(7) = getipbyte(4, destip)
sendbyte(8) = int(destport / 256) '把10进制端口分成两个字节
sendbyte(9) = destport mod 256 '把10进制端口分成两个字节
form1.winsock1.senddata sendbyte()
connstep = connstep + 1
exit function
end if
if asc(mid(revbuffer, 2, 1)) = 2 and asc(mid(revbuffer, 1, 1)) = 5 then '第二步 有用户名密码验证 成功为 05 02
form1.label8.caption = "连接成功!有验证"
redim sendbyte(0 to 2 + len(username) + len(userpassword)) as byte
sendbyte(0) = 1
sendbyte(1) = len(username)
memcopy sendbyte(2), byval username, len(username) '将用户名转换
sendbyte(2 + len(username)) = len(userpassword)
memcopy sendbyte(3 + len(username)), byval userpassword, len(userpassword) '将密码转换
form1.winsock1.senddata sendbyte()
connstep = connstep + 1
exit function
end if
case 3
if asc(mid(revbuffer, 2, 1)) <> 0 and form1.check1.value = 1 then '有验证,验证失败 代理回复第二字节为 00 验证成功,其余值为失败
msgbox "sock5代理校验用户名、密码失败!", 48, "错误"
form1.winsock1.close
connstep = 0
exit function
end if
if asc(mid(revbuffer, 2, 1)) = 0 and form1.check1.value = 1 then '有验证,验证成功,回复值第二字节为 00 ,其余值为失败
form1.label8.caption = "连接成功!有验证!"
redim sendbyte(0 to 9) as byte '发送连接请求
sendbyte(0) = 5
sendbyte(1) = 1
sendbyte(2) = 0
sendbyte(3) = 1
sendbyte(4) = getipbyte(1, destip)
sendbyte(5) = getipbyte(2, destip)
sendbyte(6) = getipbyte(3, destip)
sendbyte(7) = getipbyte(4, destip)
sendbyte(8) = int(destport / 256) '把10进制端口分成两个字节
sendbyte(9) = destport mod 256 '把10进制端口分成两个字节
form1.winsock1.senddata sendbyte()
connstep = connstep + 1
exit function
end if
if asc(mid(revbuffer, 2, 1)) = 0 and form1.check1.value = 0 then
form1.label8.caption = "连接目标服务器成功!" '无验证的最后一步,代理回复第二字节为 00 成功,其余值为失败
connstep = -1
form2.show
exit function
end if
if asc(mid(revbuffer, 2, 1)) <> 0 and form1.check1.value = 0 then
msgbox "连接目标服务器失败!", 48, "错误" '无验证的最后一步,代理回复第二字节为 00 成功,其余值为失败
connstep = 0
form1.winsock1.close
exit function
end if
case 4 '只有有验证才会用到这一步
if asc(mid(revbuffer, 2, 1)) <> 0 then
msgbox "sock5代理连接目标服务器失败!", 48, "错误"
connstep = 0
form1.winsock1.close
exit function
else
form1.label8.caption = "连接目标服务器成功!"
connstep = -1
form2.show
exit function
end if
end select
end if
if proxytype = 2 then '@@@@@@@@@@@@@@@@@@@@@@@@http1.1代理
if pstep = 1 then '无用户名密码验证
if form1.check1.value = 0 then
httpheader = "connect " & form1.text5.text & ":" & form1.text6.text & _
" http/1.1" & chr(13) & chr(10) & "host: " & form1.text5.text & ":" & form1.text6.text & chr(13) & chr(10) & chr(13) & chr(10)
connstep = pstep + 1
form1.winsock1.senddata httpheader
exit function
end if
if form1.check1.value = 1 then ' 有用户名密码验证
httpheader = "connect " & form1.text5.text & ":" & form1.text6.text & _
" http/1.1" & chr(13) & chr(10) & "host: " & form1.text5.text & ":" & _
form1.text6.text & chr(13) & chr(10) & "authorization: basic " & strtobase64(form1.text3.text & _
":" & form1.text4.text) & chr(13) & chr(10) & chr(13) & chr(10) & "proxy-authorization: basic " & _
strtobase64(form1.text3.text & ":" & form1.text4.text) & chr(13) & chr(10) & chr(13) & chr(10)
' chr(13) & chr(10) 能否直接用vbcrlf ? 我不知道
debug.print httpheader
connstep = pstep + 1
form1.winsock1.senddata httpheader
exit function
end if
end if
if pstep = 2 then '代理服务器回复,格式:http/[代理版本] [状态代码] [状态说明]
if lcase(left(revbuffer, 4)) = "http" and mid(" 200 ", 1) <> 0 then '状态代码为 200 为成功
form1.label8.caption = "连接目标服务器成功!"
form2.show
connstep = -1
else
msgbox "http1.1代理连接目标服务器失败!", 48, "错误"
connstep = 0
form1.winsock1.close
exit function
end if
end if
end if
end function
新闻热点
疑难解答