修改openssh源代码,添加流量统计并发送到远程功能

来源:互联网 发布:电视软件哪个资源多 编辑:程序博客网 时间:2024/05/21 14:05
修改openssh源代码,添加流量统计并发送到远程功能


版本号: openssh-6.1p1


1:打开channels.h,在channel结构中增加2个字段,如下图:







2: 打开auth.h,找到Authctxt结构,在里面增加2个字段,如下图:




3:打开channels.c,找到channel_handle_rfd函数,如下图:




4:打开channels.c,找到channel_handle_wfd函数,如下图:




5:打开serverloop.c ,找到server_loop2函数,如下图:




第一部分实现:流量超过1KB时发送给远程
第二部分实现:当用户结束连接时把流量纪录发送给远程


6:打开serverloop.c找到server_request_session函数,如下图:



7:打开serverloopc,加入把流量发送给远程的函数,如下:

[cpp] view plaincopy
  1. /* zazaar */  
  2. static void   
  3. submit_traffic_record(char * user,int * in,int * out){  
  4.     
  5.     struct addrinfo *ailist,*aip;  
  6.     struct addrinfo hint;  
  7.     struct sockaddr_in addr_in;  
  8.     int    sockfd,ret = -1;  
  9.     char   buff;  
  10.     int traffic_in  = *(in);  
  11.     int traffic_out = *(out);  
  12.   
  13.     if ((traffic_in == 0) && (traffic_out == 0)){  
  14.         return;  
  15.     }  
  16.   
  17.     *(in)  -= traffic_in;  
  18.     *(out) -= traffic_out;  
  19.   
  20.     char *getbuff;  
  21.     asprintf (&getbuff,"GET /traffic_record.php?user=%s&in=%i&out=%i HTTP/1.1\x0D\x0AHost: www.zazaar.com\x0D\x0A\x0D\x0A\x00",user,traffic_in,traffic_out);  
  22.   
  23.     hint.ai_flags = 0;  
  24.     hint.ai_family =0;  
  25.     hint.ai_socktype = SOCK_STREAM;  
  26.     hint.ai_protocol = 0;  
  27.     hint.ai_addrlen = 0;  
  28.     hint.ai_canonname = NULL;  
  29.     hint.ai_addr = NULL;  
  30.     hint.ai_next = NULL;  
  31.   
  32.     if (getaddrinfo("www.zazaar.com","http",&hint,&ailist) != 0){  
  33.         debug("getaddrinfo error");  
  34.         return;  
  35.     }  
  36.     aip = ailist;  
  37.     if ((sockfd = socket(aip->ai_family,SOCK_STREAM,0)) < 0){  
  38.         debug("socket fail.");  
  39.         return;  
  40.     }  
  41.   
  42.     if (connect(sockfd,aip->ai_addr,aip->ai_addrlen) == 0){  
  43.         if (send(sockfd,getbuff,strlen(getbuff),0)){  
  44.              ret = recv(sockfd,&buff,1,0);  
  45.         }  
  46.     }  
  47.     close(sockfd);  
  48.   
  49.     free (getbuff);  
  50.   
  51.     return;  
  52. }  


最后编译完成, 你会发现用户登录SSH后,流量(包括tunnel)被纪录,在用户退出或流量超过1KB时,自动发送纪录到远程,发送纪录以GET提交,例如: GET user=root&in=123&out=321

用户名为root,输入流量123byte,输出流量321byte


http://blog.csdn.net/hannibal_why/article/details/8363269

0 0