-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.c
328 lines (292 loc) · 7.01 KB
/
lib.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
#define MODULE
#define __KERNEL__
#define __KERNEL_SYSCALLS__
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sys.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/console.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include "disasm/disasm.h"
#include "include/kdbg.h"
extern volatile int status;
extern struct pt_regs *regs;
extern unsigned int esp0;
extern unsigned int ss0;
extern int sym_find_by_val(unsigned int, char *);
static int print(char *fmt, ...);
static void dump(char *addr, int len);
static int disasm(unsigned int addr, char *buf);
static unsigned int str2int(char *buf);
static int is_sym(char c);
static int file_write(struct file *file, char *buf, unsigned int len,
unsigned int pos); // append if pos == -1
static char *_strcpy(char *dest, const char *src);
static int _schedule_task(void (*routine)(void *), void *data);
static char *path2name(char *path);
static struct file *log_file = 0;
static struct tq_struct task[32];
static volatile int ntask = 0;
static int _schedule_task(void (*routine)(void *), void *data)
{
if (ntask == 31)
return -1;
ntask++;
task[ntask].sync = 0;
task[ntask].routine = routine;
task[ntask].data = data;
schedule_task(&task[ntask]);
return 0;
}
static unsigned int *get_pesp()
{
if (regs->eip >= __PAGE_OFFSET)
return &esp0;
else
return (unsigned int *)®s->esp;
}
static unsigned int *get_pss()
{
if (regs->eip >= __PAGE_OFFSET)
return &ss0;
else
return (unsigned int *)®s->xss;
}
static int print(char *fmt, ...)
{
char *buf = (char *)kmalloc(0x1000, GFP_KERNEL | GFP_ATOMIC);
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
if (log_file)
file_write(log_file, buf, strlen(buf), -1);
printk(buf);
kfree(buf);
}
static int file_write(struct file *file, char *buf, unsigned int len,
unsigned int pos)
{
mm_segment_t old_fs;
if (!(file->f_op && file->f_op->write))
return -1;
file->f_pos = 0;
if (pos != -1)
file->f_pos = pos;
old_fs = get_fs();
set_fs(get_ds());
file->f_op->write(file, buf, len, &file->f_pos);
set_fs(old_fs);
return 0;
}
static void dump(char *addr, int len)
{
char *tmp;
int i, j;
if (!mem_can_read_rgn(addr, len))
return;
for (tmp = addr; tmp < addr + len;) {
print("%08lX | ", (long) tmp);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (tmp + 4*i+j < addr + len)
print("%02lX ",
(unsigned char) tmp[i * 4 + j]);
else
print(" ");
}
print(" ");
}
print("\b| ");
for (j = 0; j < 16; j++) {
if (tmp <= addr + len)
if (*tmp >= ' ' && *tmp <= '~')
print("%c", *tmp);
else
print(".");
tmp++;
}
print("\n");
}
}
static char *dec2int(char *buf, unsigned int *n)
{
int i;
for (i = 0;i <= 8;i++) {
if (*buf == ' ' || *buf == 0)
return buf;
if (*buf >= '0' && *buf <= '9')
*n = *n * 10 + (*buf - '0');
else
break;
buf++;
}
return buf;
}
static char *hex2int(char *buf, unsigned int *n)
{
unsigned int i, j;
char *hex = "0123456789abcdef!";
char *HEX = "0123456789ABCDEF!";
i = j = *n = 0;
for (i = 0;i <= 8;i++) {
if (*buf == ' ' || *buf == 0)
return buf;
for (j=0;j<0x10;j++)
if ((*buf == hex[j]) || (*buf == HEX[j]))
break;
if (j != 0x10)
*n = *n * 16 + j;
else
break;
buf++;
}
return buf;
}
static unsigned int str2int(char *buf)
{
unsigned int n = 0;
int i = 0;
int j;
int from_mem = 0;
char _buf[0x100] = {0};
while (*buf == ' ')
buf++;
if (*buf == '[') {
from_mem = 1;
buf++;
}
for (j=0;j < strlen(buf) && j < 0x100;j++)
if (buf[j] && buf[j] != ' ' && buf[j] != '+' &&
buf[j] != '-' && buf[j] != '*' && buf[j] != '/')
_buf[j] = buf[j];
else
break;
if (j >= 3 && (n = sym_find_by_name(_buf)))
buf += j;
else if (!strncmp(buf, "eax", 3)) { n = regs->eax; buf += 3; }
else if (!strncmp(buf, "ecx", 3)) { n = regs->ecx; buf += 3; }
else if (!strncmp(buf, "edx", 3)) { n = regs->edx; buf += 3; }
else if (!strncmp(buf, "ebx", 3)) { n = regs->ebx; buf += 3; }
else if (!strncmp(buf, "ebp", 3)) { n = regs->ebp; buf += 3; }
else if (!strncmp(buf, "esp", 3)) { n = *get_pesp();buf += 3; }
else if (!strncmp(buf, "esi", 3)) { n = regs->esi; buf += 3; }
else if (!strncmp(buf, "edi", 3)) { n = regs->edi; buf += 3; }
else if (!strncmp(buf, "eip", 3)) { n = regs->eip; buf += 3; }
else {
if (!strncmp(buf, "0x", 2))
buf += 2;
buf = hex2int(buf, &n);
}
switch (*buf) {
case '+': n += str2int(buf + 1); break;
case '-': n -= str2int(buf + 1); break;
case '*': n *= str2int(buf + 1); break;
case '&': n &= str2int(buf + 1); break;
case '|': n |= str2int(buf + 1); break;
case '/':{
unsigned int m = str2int(buf + 1);
if (m)
n /= m;
break;
}
}
if (from_mem) {
if (mem_can_read(n))
n = *(unsigned int *)n;
else
n = 0;
}
return n;
}
static char *_strcpy(char *dest, const char *src)
{
while (*dest++ = *src++);
return dest;
}
static int is_sym(char c)
{
int i;
char *syms = " +-*/&|?><[]_#@$~.!";
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
return 1;
for (i = 0; i < strlen(syms); i++)
if (c == syms[i])
return 1;
return 0;
}
static char *disasm_buf __asm__("disasm_buf");
static char *disasm_cur_buf __asm__("disasm_cur_buf");
static unsigned int fprintf_ret __asm__("fprintf_ret");
static int disasm_read_memory(bfd_vma memaddr, bfd_byte * myaddr,
int length, struct disassemble_info *info)
{
if (mem_can_read(memaddr))
memcpy(myaddr, (void *)memaddr, length);
return 0;
}
static void disasm_print_address(bfd_vma addr, struct disassemble_info *info)
{
char buf[128] = {0};
disasm_cur_buf +=
sprintf((char *) disasm_cur_buf, "0x%08lX", (long) addr);
if (sym_find_by_val(addr, buf)) {
int max_len = disasm_buf + 80 - disasm_cur_buf - 2;
if (max_len > strlen(buf))
max_len = strlen(buf);
*disasm_cur_buf++ = '<';
*disasm_cur_buf=0;
strncat(disasm_cur_buf, buf, max_len);
disasm_cur_buf += max_len;
*disasm_cur_buf++ = '>';
}
}
static disasm_fprintf()
{
__asm__("popl (fprintf_ret)\n"
"popl %eax\n"
"pushl disasm_cur_buf\n"
"call sprintf\n"
"addl %eax, disasm_cur_buf\n"
"jmp *fprintf_ret\n");
}
static int disasm(unsigned int addr, char *buf)
{
struct disassemble_info di = {0};
di.mach = bfd_mach_i386_i386;
di.fprintf_func = (fprintf_ftype)&disasm_fprintf;
di.print_address_func = disasm_print_address;
di.read_memory_func = disasm_read_memory;
disasm_buf = disasm_cur_buf = buf;
return print_insn_i386(addr, &di);
}
static int is_call_inst(unsigned int eip)
{
unsigned char c1, c2;
c1 = *(unsigned char *)eip;
c2 = (*((unsigned char *)eip + 1) & 0x38) >> 3;
if (c1 == 0xE8 || c1 == 0x9A ||
(c1 == 0xFF && (c2 == 0x2 || c2 == 0x3)))
return 1;
return 0;
}
static int is_ret_inst(unsigned int eip)
{
unsigned char c1 = *(unsigned char *)eip;
if (c1==0xc2 || c1==0xc3 || c1==0xcf)
return 1;
return 0;
}
static char *path2name(char *path)
{
char *t = path + strlen(path);
while (t>=path && *t!='/')
t--;
return t + 1;
}