C語(yǔ)言實(shí)現(xiàn)的socket5代理_第1頁(yè)
C語(yǔ)言實(shí)現(xiàn)的socket5代理_第2頁(yè)
C語(yǔ)言實(shí)現(xiàn)的socket5代理_第3頁(yè)
C語(yǔ)言實(shí)現(xiàn)的socket5代理_第4頁(yè)
C語(yǔ)言實(shí)現(xiàn)的socket5代理_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、C語(yǔ)言寫(xiě)的Linux平臺(tái)socks5代理程序信息來(lái)源:邪惡八進(jìn)制信息安全團(tuán)隊(duì)(www. eviloctal. com)前兒天MSN老上不去,我還以為是公司做了防火墻限制。J:是把去年這個(gè)時(shí)候?qū)?得一個(gè)代理程序改了改,拿出來(lái)用。結(jié)果發(fā)現(xiàn)MSN是因?yàn)槲④浀膯?wèn)題,鄙視啊 因?yàn)閷?xiě)得比較急,這個(gè)只支持TCP代理,UDP的我沒(méi)寫(xiě),因?yàn)镸SN用不上。這個(gè) 代碼可以隨意修改分發(fā),不過(guò)最好能給我一份。這是頭文件:/ / Socks5代理頭文件,定義協(xié)議相關(guān)數(shù)據(jù)包結(jié)構(gòu)/版本0.1,作者云舒/ 2007年1月9日凌晨1點(diǎn)15分,GF回家己經(jīng)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 / 協(xié)商方法服務(wù)器響應(yīng) 'char versi

3、on; /服務(wù)器支持的Socks版本,0x04或者0x05char select_method;/服務(wù)器選擇的方法,0x00為匿名,0x02為密碼認(rèn)證 METHOD SELECT RESPONSE: typedef struct _method_select_request / 協(xié)商方法服務(wù)端諳求 char version; /客戶端支持的版本,0x04或者0x05char number_methods; /客戶端支持的方法的數(shù)量char methods2551; /客戶端支持的方法類型,最多255個(gè),0x00為匿名,0x02為密碼認(rèn)證 METHOD_SELECT_REQUEST;typede

4、f struct _AUTH_RESPONSE /用戶密碼認(rèn)證服務(wù)端響應(yīng)char version;/版本,此處恒定為0x01char result;/服務(wù)端認(rèn)證結(jié)果,0x00為成功,其他均為失敗 AUTH_RESPONSE;typedef struct _AUTH_REQUEST 用戶密碼認(rèn)證客戶端諳求char version; /版本,此處恒定為0x01char name_len; /第三個(gè)字段用戶名的長(zhǎng)度,一個(gè)字節(jié),最長(zhǎng)為Oxffchar name255 ; / 用戶名char pwd_len;/第四個(gè)字段密碼的長(zhǎng)度,一個(gè)字節(jié),最長(zhǎng)為Oxff char pwd 255; / 密碼 AUTH

5、_REQUEST;typedef struct _S0CKS5_RESP0NSE /連接真實(shí)主機(jī),Socks代理服務(wù)器響應(yīng)char version; /服務(wù)器支持的Socks版本,0x04或者0x05char reply; /代理服務(wù)器連接真實(shí)主機(jī)的結(jié)果,0x00成功char reserved; / 保留位,恒定位 0x00char address_type; / Socks代理服務(wù)器綁定的地址類型,IP V4為0x01, IP V6為0x04,域名為0x03char address_port 1 : /如果address_type為域名,此處第字節(jié)為域名長(zhǎng) 度,其后為域名本身,無(wú)0字符結(jié)尾,

6、域無(wú)后為Socks代理服務(wù)器綁定端口 S0CKS5_RESP0NSE;typedef struct _SOCKS5_REQUEST /客戶端請(qǐng)求連接真實(shí)主機(jī)char version; /客戶端支持的Socks版本,0x04或者0x05char cmd; / 客戸端命令,CONNECT 為 0x01, BIND 為 0x02, UDP 為 0x03, 一般 為 0x01char reserved; / 保留位,恒定位 0x00char address_type; /客戶端請(qǐng)求的真實(shí)主機(jī)的地址類型,IP V4為0x00, IP V6 為 0x04,域名為 0x03 char address_por

7、tl; / 如果 address_type 為域 名,此處第一字節(jié)為域名長(zhǎng)度,其后為域箱本身,無(wú)0字符結(jié)尾,域矗后為真實(shí) 主機(jī)綁定端口S0CKS5_REQUEST;ttendif主程序來(lái)了:/ Socks5程序,只支持TCP代理/版本0.1,作者云舒/ 2007年1刀9 口凌晨1點(diǎn)15分,GF回家已經(jīng)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 測(cè)試)/ /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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論