socks v5是一种用于代理的协议,就是说client用这种协议与server沟通,让server帮忙代访问remote后再将结果通过此协议返给client,所以一般是涉及到3个端,分别是client客户端、server代理端、remote请求目的地。通常因为client无法直接访问remote,有可能是由于防火墙限制,所以需要经过一个中间的server代理来访问remote。著名的shawsocks(client)简称ss就是基于这种协议的,工作于传输层之上,传输层可基于TCP或UDP。原理是这样的,ss和浏览器(可以是其他软件)运行在同一PC上,暂合称为client端,浏览器发出的数据全部转发给ss,然后由ss转发给server,server再进行转发,所以表现出来是这样的: browser ==> shawdowsocks ==> server ==> remote,数据返回时按原路返回。需要注意的是,server不会对应用层协议进行拆分,而是只进行转发,所以remote收到的数据中仍包含client端的地址、浏览器信息等等。

本文所列的表格通常长这样的:

┌────────┬────────┬────────┐
│ field1 │ field2 │ field3 │
├────────┼────────┼────────┤
│ 1 │ 2 │ 3 │
└────────┴────────┴────────┘

其中field就是协议头的域。其下面的数字表示该域所占字节数量,而不是其表示内容;如果是"variable",表示可变的,一般在其前面会有个域能指定其长度;如果是0x00这样的十六进制则表示该域内容固定为0x00;。

协议内容 (基于TCP)

  • Step 1:client to server
┌─────┬──────────┬─────────┐
│ VER │ NMETHODS │ METHODS │
├─────┼──────────┼─────────┤
│ 1 │ 1 │ 1~255 │
└─────┴──────────┴─────────┘

用途:确立连接认证方法协商,client告知server其所支持的认证方法,server可从中挑选一个。其中

ver socks协议的版本号,常用的是socks5版本的,故可以为0x05

nmethods 后面的域methods有n个字节

methods 每个字节即为一个认证方法,供server挑选

  • Step 2:server to client
┌──────┬──────────┐
│ VER │ METHOD │
├──────┼──────────┤
│ 1 │ 1 │
└──────┴──────────┘

用途:告知client需要使用哪种认证方法进行认证。

ver 版本,即0x05

method 认证方法,从Step 1中的METHODS中选一个。

  • Step 3:client to server
┌──────┬──────┬────────┬───────┬───────────┬──────────┐
│ VER │ CMD │ RSV │ ATYP │ DST.ADDR │ DST.PROT │
├──────┼──────┼────────┼───────┼───────────┼──────────┤
│ 1 │ 1 │ 0x00 │ 1 │ Variable │ 2 │
└──────┴──────┴────────┴───────┴───────────┴──────────┘

用途:告知server要连接的remote相关信息。

ver 版本,即0x05

cmd 命令,可以是 1)0x01CONNECT 2) 0x02BIND 3) 0x03UDP ASSOCIATE

rsv 保留,就是暂时没用的

atyp 指定紧跟的域的地址类型,可以是 1) 0x01 IPv4 2) 0x03 DOMAINNAME 3) 0x04 IPv6

dst.addr 地址

dst.prot 端口号

注:如果atyp是domainname,那么长度(以字节为单位)得写在dst.addr的首个字节里边,否则无法知道域名长度;如果是ipv4就4字节,如果是ipv6就16字节。 cmd的3个命令在最下面讲。

  • Step 4:server to client
┌──────┬──────┬────────┬───────┬───────────┬──────────┐
│ VER │ REP │ RSV │ ATYP │ BND.ADDR │ BND.PORT │
├──────┼──────┼────────┼───────┼───────────┼──────────┤
│ 1 │ 1 │ 0x00 │ 1 │ Variable │ 2 │
└──────┴──────┴────────┴───────┴───────────┴──────────┘

用途:回复client。

ver 版本,即0x05

rep 回复,可以是下面的一项:

o 0x00 succeeded

o 0x01 general SOCKS server failure

o 0x02 connection not allowed by ruleset

o 0x03 Network unreachable

o 0x04 Host unreachable

o 0x05 Connection refused

o 0x06 TTL expired

o 0x07 Command not supported

o 0x08 Address type not supported

o 0x09 to 0xFF unassigned

rsv 保留。

atyp 跟上面讲过的一样,包括接下来的两个域。

协议内容 (基于UDP)

  • client to server
┌──────┬───────┬───────┬───────────┬───────────┬──────────┐
│ RSV │ FRAG │ ATYP │ DST.ADDR │ DST.PORT │ DATA │
├──────┼───────┼───────┼───────────┼───────────┼──────────┤
│ 2 │ 1 │ 1 │ Variable │ 2 │ Variable │
└──────┴───────┴───────┴───────────┴───────────┴──────────┘

rsv 保留。

frag 当前数据包的fragment号,这是传输层的分片,要自己实现的,而不是网络层的分片。但可以选择不实现分片,直接把frag非零的数据报丢掉。

ATYP 跟上面讲过的一样,包括接下来的两个域。

data 数据。

细节详解

关于Step 1中的METHODS字段有如下可选:

o 0x00 NO AUTHENTICATION REQUIRED(常用)

o 0x01 GSSAPI

o 0x02 USERNAME/PASSWORD(常用)

o 0x03 to 0x7F IANA ASSIGNED

o 0x80 to 0xFE RESERVED FOR PRIVATE METHODS

o 0xFF NO ACCEPTABLE METHODS (常用)

注:0x01必须支持,0x02应该要支持。注意了,认证方法是其他独立的协议,比如USERNAME/PASSWORD,这与本协议无关。


关于cmd中的3个选项:

  • CONNECT

    In the reply to a CONNECT, BND.PORT contains the port number that the server assigned to connect to the target host, while BND.ADDR contains the associated IP address. The supplied BND.ADDR is often different from the IP address that the client uses to reach the SOCKS server, since such servers are often multi-homed. It is expected that the SOCKS server will use DST.ADDR and DST.PORT, and the client-side source address and port in evaluating the CONNECT request.
  • BIND

    The BIND request is used in protocols which require the client to accept connections from the server. FTP is a well-known example, which uses the primary client-to-server connection for commands and status reports, but may use a server-to-client connection for transferring data on demand (e.g. LS, GET, PUT).

    It is expected that the client side of an application protocol will use the BIND request only to establish secondary connections after a primary connection is established using CONNECT. In is expected that a SOCKS server will use DST.ADDR and DST.PORT in evaluating the BIND request.

    Two replies are sent from the SOCKS server to the client during a BIND operation. The first is sent after the server creates and binds a new socket. The BND.PORT field contains the port number that the SOCKS server assigned to listen for an incoming connection. The BND.ADDR field contains the associated IP address. The client will typically use these pieces of information to notify (via the rimary or control connection) the application server of the rendezvous address. The second reply occurs only after the anticipated incoming connection succeeds or fails.
  • UDP ASSOCIATE

    The UDP ASSOCIATE request is used to establish an association within the UDP relay process to handle UDP datagrams. The DST.ADDR and DST.PORT fields contain the address and port that the client expects to use to send UDP datagrams on for the association. The server MAY use this information to limit access to the association. If the client is not in possesion of the information at the time of the UDP ASSOCIATE, the client MUST use a port number and address of all zeros.

    A UDP association terminates when the TCP connection that the UDP ASSOCIATE request arrived on terminates.

    In the reply to a UDP ASSOCIATE request, the BND.PORT and BND.ADDR fields indicate the port number/address where the client MUST send UDP request messages to be relayed.

其他

socks v5 协议解析的更多相关文章

  1. SOCKS5 协议解析

    代理 根据 HTTP 1.1 的定义,proxy 是: An intermediary program which acts as both a server and a client for the ...

  2. 调试备忘录-SWD协议解析

    目录--点击可快速直达 目录 写在前面 1  SWD协议简介 2  SWD物理层协议解析 2.1  SWD通信时序分析 2.2  SWD 寄存器简介 2.2.1  DP寄存器 2.2.2  AP寄存器 ...

  3. ts 协议解析

    pes : http://wenku.baidu.com/link?url=KjcA0qXqZ1bWVQTa8i1YOmygofldSQL7Pjj-zGRw1e_6_LFmVLo5DIWF0SNwVn ...

  4. [转]netty对http协议解析原理

    本文主要介绍netty对http协议解析原理,着重讲解keep-alive,gzip,truncked等机制,详细描述了netty如何实现对http解析的高性能. 1 http协议 1.1 描述 标示 ...

  5. twemproxyRedis协议解析探索——剖析twemproxy代码正编

    这篇文章会对twemproxyRedis协议解析代码部分进行一番简单的分析,同时给出twemproxy目前支持的所有Redis命令.在这篇文章开始前,我想大家去简单地理解一下有限状态机,当然不理解也是 ...

  6. B/S 架构中,网络模型的分解与协议解析

    前言 如果是C/S专业毕业的或者是学过计算机网络课程的童鞋们,相信大家都知道网络模型的划分,本文首先来聊一聊目前对于B/S结构中,网络模型分解的两种方式. 没错,相信大家看到这个图片的时候就已经明白了 ...

  7. 详解BLE 空中包格式—兼BLE Link layer协议解析

    BLE有几种空中包格式?常见的PDU命令有哪些?PDU和MTU的区别是什么?DLE又是什么?BLE怎么实现重传的?BLE ACK机制原理是什么?希望这篇文章能帮你回答以上问题. 虽然BLE空中包(pa ...

  8. netty对http协议解析原理解析

    本文主要介绍netty对http协议解析原理,着重讲解keep-alive,gzip,truncked等机制,详细描述了netty如何实现对http解析的高性能. 1 http协议 1.1 描述 标示 ...

  9. MODBUS协议解析中常用的转换帮助类(C#)

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

随机推荐

  1. apache 压缩 gzip

    配置 编辑httpd.conf文件 去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释# 去掉 #LoadModule deflate ...

  2. Ubuntu server 修改系统时区

    执行命令: sudo dpkg-reconfigure tzdata

  3. Luogu P2073 送花 set

    这题...一眼set...但是打了一会儿.. 记录一下每个价格对应的美丽度,顺便充当vis数组,如果美丽度不为0,说明set里已经有了... 删除好说,删*s.begin()和*--s.end()就好 ...

  4. P2896 [USACO08FEB]一起吃饭Eating Together

    传送门 可以考虑DP 设 f [ i ] [ 1/2/3 ] [ 0/1 ] 表示当前考虑到第 i 头牛,打算让当前位置的编号变成 1/2/3,并且打算让整段序列上升/下降 0/1 然后就对每种情况慢 ...

  5. Photoshop入门教程(六):通道

    学习心得:当大部分人听到通道.心里可能会有一种很害怕的感觉,因为“通道”并不像“图层”这样易于理解,望而生畏.”通道“的本质其实是存储图片的信息,把一张图片比作一个为网站,那么通道就是网站的后台,存储 ...

  6. SQL 十分位

    -- 十分位,这个算法不是很准确 select family_agreement_cnt -- 字段 ,dt -- 分区 ,rn -- 排序 ,cnt -- 总行数 ,percent2 -- 分位值 ...

  7. linux下lua运行环境安装

    1.下载安装包: [root@H0f ~]# wget  http://www.lua.org/ftp/lua-5.2.4.tar.gz    http://www.lua.org/ftp/lua-5 ...

  8. linux面试题:删除一个目录下的所有文件,但保留一个指定文件

    面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件 [root@oldboy xx]# touch ...

  9. 1.4 Go语言-switch语句(转)

    与串联的if语句类似,switch语句提供了一个多分支条件执行的方法.不过在这里用一个专有名词来代表分支——case.每一个case可以携带一个表达式或一个类型说明符.前者又可被简称为case表达式. ...

  10. SpringBoot中通过实现WebMvcConfigurer完成参数校验

    在Spring5.0和SpringBoot2.0中废弃了WebMvcConfigurerAdapter类. 现有两种解决方案 1 直接实现WebMvcConfigurer (官方推荐)2 直接继承We ...