C语言实现的socket5代理_第1页
C语言实现的socket5代理_第2页
C语言实现的socket5代理_第3页
C语言实现的socket5代理_第4页
C语言实现的socket5代理_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C语言写的Linux平台socks5代理程序信息來源:邪恶八进制信息安全团队(www. eviloctal. com)前儿天MSN老上不去,我还以为是公司做了防火墙限制。J:是把去年这个时候写 得一个代理程序改了改,拿出來用。结果发现MSN是因为微软的问题,鄙视啊 因为写得比较急,这个只支持TCP代理,UDP的我没写,因为MSN用不上。这个 代码可以随意修改分发,不过最好能给我一份。这是头文件:/ / Socks5代理头文件,定义协议相关数据包结构/版本0.1,作者云舒/ 2007年1月9日凌晨1点15分,GF回家己经11天了。/ 2008年1月25 H修改,今年GF_直在我身边,哈哈/参考:

2、/ httD: /www.:rfc-editor, ore/rfc/rfc 1928. txt/ http:/ www. :rfc-editor. oi?g/rfc/rfcl929. txt/ /tiifndef SOCKS5_Httdefine S0CKS5_H ttdefine VERSION 0x05 ttdefine CONNECT 0x01 ttdefine IPV4 0x01 ttdefine DOMAIN 0x03 define IPV6 0x04 typedef struct _method_select_response / 协商方法服务器响应 'char versi

3、on; /服务器支持的Socks版本,0x04或者0x05char select_method;/服务器选择的方法,0x00为匿名,0x02为密码认证 METHOD SELECT RESPONSE: typedef struct _method_select_request / 协商方法服务端谙求 char version; /客户端支持的版本,0x04或者0x05char number_methods; /客户端支持的方法的数量char methods2551; /客户端支持的方法类型,最多255个,0x00为匿名,0x02为密码认证 METHOD_SELECT_REQUEST;typede

4、f struct _AUTH_RESPONSE /用户密码认证服务端响应char version;/版本,此处恒定为0x01char result;/服务端认证结果,0x00为成功,其他均为失败 AUTH_RESPONSE;typedef struct _AUTH_REQUEST 用户密码认证客户端谙求char version; /版本,此处恒定为0x01char name_len; /第三个字段用户名的长度,一个字节,最长为Oxffchar name255 ; / 用户名char pwd_len;/第四个字段密码的长度,一个字节,最长为Oxff char pwd 255; / 密码 AUTH

5、_REQUEST;typedef struct _S0CKS5_RESP0NSE /连接真实主机,Socks代理服务器响应char version; /服务器支持的Socks版本,0x04或者0x05char reply; /代理服务器连接真实主机的结果,0x00成功char reserved; / 保留位,恒定位 0x00char address_type; / Socks代理服务器绑定的地址类型,IP V4为0x01, IP V6为0x04,域名为0x03char address_port 1 : /如果address_type为域名,此处第字节为域名长 度,其后为域名本身,无0字符结尾,

6、域无后为Socks代理服务器绑定端口 S0CKS5_RESP0NSE;typedef struct _SOCKS5_REQUEST /客户端请求连接真实主机char version; /客户端支持的Socks版本,0x04或者0x05char cmd; / 客戸端命令,CONNECT 为 0x01, BIND 为 0x02, UDP 为 0x03, 一般 为 0x01char reserved; / 保留位,恒定位 0x00char address_type; /客户端请求的真实主机的地址类型,IP V4为0x00, IP V6 为 0x04,域名为 0x03 char address_por

7、tl; / 如果 address_type 为域 名,此处第一字节为域名长度,其后为域箱本身,无0字符结尾,域矗后为真实 主机绑定端口S0CKS5_REQUEST;ttendif主程序來了:/ Socks5程序,只支持TCP代理/版本0.1,作者云舒/ 2007年1刀9 口凌晨1点15分,GF回家已经11天了。/ 2008年1月25 11修改,今年GF 一氏在我身边,哈哈/参考:/ httD: /www rfc-editor org/rfc/rfcl928 txt/ httD:/www:rfc一editor orx, rfc.ifcl929 txt/编译:/ gcc -o socksS -02

8、 Socks5. c -lpthread( RedHat AS5 测试)/ /include <stdio. h> include <netinet/in.h> include <netdb h> ttinclude <sys/time.h> include <sys/types h> include <unistd h> ttinclude <stdlib. h> ttinclude <signa 1. h> ttinclude <pthread. h> ttinclude <e

9、rrno. h> ttinclude <string h> ttinclude <sys/socket h> ttinclude <arpa/ineth>include "SocksS. h" ftdefine MAX_USER 10 define BUFF_SIZE 1024 ttdefine AUTH.CODE 0x02 ttdefine TIME_0UT 6000000 ttdefine USER_NAME "yunshu" ftdefine PASS_W0RD "ph4nt0nT / Selec

10、t auth method, return 0 if success, -1 if failed int SelectMethod( int sock )char recv_bufferBUFF_SIZE = 0 ;char reply_buffer2 = 0 ;METHOD_SELECT_REQUEST *method_request; METHOD_SELECT_RESPONSE *method_response;/ recv METHOD_SELECT_REQUESTint ret = recv ( sock, recv_buffer, BUFF_SIZE, 0 ); 辻(ret <

11、;= 0 )perror( recv error );close( sock );return 一1;/printf( "SelectMethod: recv %d bytes'n", ret );/ if client request a wrong version or a wrong number_method method_request = (METHOD_SELECT_REQUEST *)recv_buffer; method_response = (METHOD_SELECT_RESPONSE *)reply_buffer;method_respons

12、e->version = VERSION;/ if not socksSif( (int)method_:request-version != VERSION ) _method_response->select_method = Oxff:send( sock, method_response, sizeof(METHOD_SELECT_RESPONSE), 0 ); close( sock );return T;method_response->select_method = AUTH_CODE:i.f( -1 二二 send( sock, method_response

13、, sizeof (METHOD_SELECT_RESPONSE), 0 ) " "close( sock );return T;return 0;/ test password, return 0 for success, int AuthPassword( int sock ) char recv_bufferBUFF_SIZE = 0 ; char reply_bufferBUFF_SIZE = 0 ;AUTH_REQUEST *auth_request; ALTH_RESPONSE *auth_response;/ auth username and passwor

14、dint ret = recv( sock, recv_buffer, BUFF_SIZE, 0 );辻(ret <= 0 )perror ( z,recv username and password error");close( sock );return 一1;/printf( "AuthPass: recv %d bytes'n", ret );auth_request = (AUTH_REQUEST *)recv_buffer;memset( reply_buffer, 0, BUFF_SIZE ); auth_response = (Al&

15、#39;TH_RESPONSE *)reply_buffer; auth_response->version = 0x01;char recv_name256 = 0 ;char recv_pass256 = 0 ;/ auth_request->name_len is a char, max number is Oxffchar pwd_str2 = 0 ;strncpy( pwd_str, auth_request->name + auth_request->name_len, 1 ); int pwd_len = (int)pwd_str0;strncpy( re

16、cv_name, auth_request->name, auth_request->name_len ); strncpy( recv_pass, auth_request->name + auth_request->name_len + sizeof(auth_request->pwd_len), pwd_len );/printf( "username: %snpassword: %sn". recv_name, recv_pass ); / check username and password辻(strncmp( recv_name,

17、 USER_NAME, strlen (USER_NAME) ) = 0) && (strncmp( recv_pass, PASS_W0RD, strlen(PASS_W0RD) ) = 0) " " auth_response->resuIt 二 0x00; 辻(T = send( sock, auth_response, sizeof (AUTH_RESPONSE), 0 ) - -close( sock );return T;elsereturn 0;elseauth_response->resuIt 二 0x01;send( sock,

18、 auth_response, sizeof(AUTH_RESPONSE), 0 );close( sock ); return 一1;/ parse command, and try to connect real server. / return socket for success, -1 for failed int ParseCommand( int sock )char recv_bufferBUFF_SIZE = 0 ;char reply_bufferBUFF_SIZE = 0 ;SOCKS5_REQUEST *socks5_request;S0CKS5_RESP0NSE *s

19、ocks5_response;/ recv commandint ret = recv( sock, recv_buffer, BUFF_SIZE, 0 ); if( ret <= 0 ) ” ”perror( "recv connect command error");close( sock ); return T; socks5_request = (SOCKS5_REQUEST *)recv_buffer;if( (socks5_request->version != VERSION) | (socks5_request->cmd != CONNEC

20、T) |(socks5_request->address_type = IPV6)/printf( "connect command errorn");close( sock );return T;/ begain process connect requeststruct sockaddr_in sin;memset( (void *)&sin, 0, sizeof(struct sockaddr_in); sin. sin_family = AF_INET: / get real server&#39;s ip addressif ( socks5

21、_request->address_type = IPV4 ) "memcpy( &sin. sin_addrs_addr, &socks5_request->address_type + sizeof(socks5_request->address_type) , 4 );memcpy( &sin. sin_port, &socks5_request->address_type +sizeof(socks5_request->address_type) +4、 2 );/printf( "Real Server

22、: %s %dn, inet_ntoa( sin. sin_addr ), ntohs( sin. sin_port );else if( socks5_request->address_type = DOMAIN ) ' 'char domain_length = *(&socks5_request->address_type +sizeof(socks5_request->address_type);char target_domain 256 = 0 ;strncpy( target_domain, &socks5_request->

23、;address_type + 2, (unsigned int)domain_length );/printf( "target: %sn", target_domain );struct hostent *phost = gethostbyname( target_domain ); if( phost = NULL )/printf ( "Resolve %s error !nz,, target_domain );close( sock );return -1;memcpy( &sin. sin_addr , phost->h_addr_li

24、st0 , phost->h_length ); memcpy( &sin. sin_port» &socks5_request->address_type + sizeof(socks5_request->address_type) +sizeof(domain_length) + domain_length, 2 ); " " / try to connect to real serverint real_server_sock = socket( AF_INET, SOCK_STREAM, 0 ); if ( real_se

25、rver_sock < 0 ) : _ ”perror( "Socket creation failed'n");close( sock );return T;memset( reply_buffer, 0, sizeof(BUFF_SIZE);socks5_response = (S0CKS5_RESP0NSE *)reply_buffer;socks5_response->version = VERSION; socks5_response->reserved 二 0x00: socks5_response->address_type =

26、 0x01; memset( socks5_response + 4, 0 , 6 );ret = connect( real_server_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in);if ( ret = 0 )socks5_response->reply = 0x00;if ( -1 = send( sock, socks5_response, 10, 0 ) close( sock );return -1;elseperror( "Connect to real server error );

27、socks5_response->reply = 0x01;send( sock, socks5_response, 10, 0 );close( sock ); return T;return real_server_sock: " int ForwardData( int sock, int real_server_sock ) 一 一char recv_bufferBUFF_SIZE = 0 ;fd set fd read; struct timeval time_out;time out. tv sec = 0; time_out. tv_usec = TIME_0UT

28、: int ret = 0;while( 1 )FD_ZERO( &fd_read );FD_SET( sock, &fd_read );FD_SET ( real_server_sock, &fd_read );ret = select( (sock > real_server_sock ? sock : real_server_sock) + 1 &fd_read, NULL, NULL, &time_out );if ( -1 = ret ) ”perror( z select socket error );break;else if( 0

29、= ret )/perror( "select time out");continue;/printf( "DEBUG testing readable!n"); 辻(FD_ISSET(sock, &fd_read) ” ”/printf( "client can read!;memset( recv_buffer, 0, BUFF_SIZE );ret = recv( sock, recv_buffer, BUFF_SIZE, 0 ); if ( ret > 0 )/printf( "%s", recv_bu

30、ffer );/printf ( "recv %d bytes from die nt. n", ret );ret = send( real_server_sock, recv_buffer, ret, 0 );if( ret = -1 )perror ( "send data to real server errorz,);break;/printf( "send %d bytes to client!n*, ret );else if( ret = 0 ) ” ”/printf( "client close socket. n"

31、);break;else/perror( "recv from client error");break;else if( FD_ISSET(real_server_sock, &fd_read) "/printf( ":real server can read!n* );memset( recv_buffer, 0, BUFF_SIZE );ret = recv( real_server_sock, recv_buffer, BUFF_SIZE, 0 ); 迁(ret > 0 )/printf( “%s", recv_buffe

32、r );/printf( "recv %d bytes from real server. n ret );ret = send( sock, recv_buffer, ret, 0 );if ( ret = -1 ) ” ”perror( "send data to client error");break;else if( ret = 0 )/printf( "real server close socket. nf );break;elseperror( "recv from real server error"); break

33、;return 0;int Socks5( void *client_sock ) "int sock = *(int *)client_sock;if ( SelectMethod( sock ) = T ) ” ”/printf ( "socks version errornz,);return -1;if( AuthPassword( sock )二二 -1 )/printf ( "auth password errorn,z );return 一1;int real_server_sock = ParseCommand( sock ); if ( real

34、_server_sock = -1 ) ”/printf ( "parse command error. n,z );return T;ForwardData ( sock, real_server_sock );close( sock );close( real_server_sock );return 0;int main( int argc, char *argv) if ( argc != 2 ) printf ( z,Socks5 proxy for test, code by YunShu'n");printf( "Usage: %s <proxy_portn", argv0);printf( ''Options:n* );printf( " proxy_port> which port of

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论