-
Notifications
You must be signed in to change notification settings - Fork 3
/
syscalls.c
executable file
·408 lines (339 loc) · 8.18 KB
/
syscalls.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xenctrl.h>
#include <arpa/inet.h>
#include <xen/hvm/ether.h>
#include "hashtable.h"
#include "syscalls.h"
#include "ether_main.h"
#include "parameters.h"
#include "ntapi.tab.h"
#include "ether.h"
extern FILE *yyin;
extern void yyparse(void);
extern FILE *ppin;
extern void ppparse(void);
struct nt_syscall_info* nt_syscalls[NT_SYSCALL_COUNT];
struct hashtable* nt_syscall_table;
/* hash function taken from description in:
* http://www.cse.yorku.ca/~oz/hash.html
*
* according to the site the code is in the public domain *
*/
unsigned int hash_fn(void *value)
{
const char* s_value = (const char*)value;
unsigned int hash = 0;
int c;
while ((c = *s_value++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
int string_equality_fn(void *s1, void *s2)
{
return ( 0 == strcmp((const char*)s1, (const char *)s2));
}
/* populate the windows nt syscal table. Use this to map
* from syscall# to syscall name
* and to figure out the number of arguments
* later this table can include more information about
* the call arguments and derive their textual representation
*/
int nt_init_syscall_table(void)
{
nt_syscall_table =
create_hashtable(NT_SYSCALL_COUNT, hash_fn, string_equality_fn);
sp_table = sp_init();
if(nt_syscall_table == NULL)
{
return -1;
}
if(parameter_handling_init() < 0)
{
nt_free_syscalls();
return -1;
}
if(sp_table == NULL)
{
nt_free_syscalls();
return -2;
}
return 0;
}
/* parse the parameters to windodws nt system call arguments */
void nt_parse_parameters(void)
{
FILE *syscall_defines = fopen("winternl.h", "r");
ppin = syscall_defines;
ppparse();
fclose(syscall_defines);
}
/* populate the nt system call map and table from
* winternl.h and nativeapi.h, respectively
*/
int nt_populate_syscalls(void)
{
FILE *nt_table = fopen("nativeapi.h", "r");
int i;
if(nt_init_syscall_table() < 0)
{
perror("Could not initialize system call tables ");
return -2;
}
for( i = 0; i < NT_SYSCALL_COUNT; i++)
{
nt_syscalls[i] = NULL;
}
if(nt_table == NULL)
{
perror("Could not open nt syscall list");
return -1;
}
yyin = nt_table;
yyparse();
fclose(nt_table);
for( i = 0; i < NT_SYSCALL_COUNT; i++)
{
if(strncmp(nt_syscalls[i]->name, "NtDeviceIoControlFile", 21) == 0)
{
nt_syscalls[i]->handler = process_NtDeviceIoControlFile;
}
else if(strncmp(nt_syscalls[i]->name, "NtRequestWaitReplyPort", 22) == 0)
{
nt_syscalls[i]->handler = process_NtRequestWaitReplyPort;
}
}
return 0;
}
void nt_free_syscalls(void)
{
int i, j;
for(i = 0; i < NT_SYSCALL_COUNT; i++)
{
if(nt_syscalls[i] != NULL)
{
for(j = 0; j < MAX_PARAM_COUNT; j++)
{
if(nt_syscalls[i]->params[j].type != NULL)
{
free(nt_syscalls[i]->params[j].type);
}
}
}
}
parameter_handler_cleanup();
sp_cleanup();
}
/* read 32 bits from the guest hvm domain. I had
* to write this function since neither the default
* xen xc_translate_foreign_address or xenaccess
* worked with 64bit hvm domains. I don't know why
* the libxc calls didn't work, but this method
* should work on any hvm domain. All this does is
* call a function in the hypervisor to read the memory
* using the current pagetable of the requested domain
* as the base for translation of va->mfn
*/
int domain_read_32bits( int xc_iface, int domid, unsigned long va, uint32_t *dest)
{
if(ether_readguest(xc_iface,
domid,
va,
(unsigned char*)dest,
sizeof(uint32_t)) != 0)
{
return -1;
}
else
{
return 1;
}
}
int domain_read_current(unsigned long va, void *dest, int length)
{
if(ether_readguest(current_domain.xc_iface,
current_domain.domid,
va,
(unsigned char*)dest,
length) != 0)
{
return -1;
}
else
{
return 1;
}
}
void process_generic_syscall(struct syscall_info *call, uint32_t *parameter_values)
{
if(call->notification_type == SYSCALL_CALL)
{
printf("CALL ");
}
else
{
printf("RET 0x%lx = ", call->return_value);
}
printf("name: [%s], cr3: [0x%lx] pid: [%d], %s(",
call->process_name,
call->cr3,
call->pid,
nt_syscalls[call->number]->name);
if( parameter_values != NULL)
{
int i = 0;
for(i = 0; i < nt_syscalls[call->number]->parameter_count; i++)
{
uint32_t param = parameter_values[i];
if(i != 0)
{
printf(", ");
}
printf("0x%x", param);
/* parameter_values = memory address of parameter */
parameter_handler_do(&(nt_syscalls[call->number]->params[i]), param);
}
}
printf(")\n");
}
void process_NtDeviceIoControlFile(struct syscall_info *call, uint32_t *parameter_values)
{
if(call->notification_type == SYSCALL_CALL && parameter_values != NULL)
{
if(parameter_values[5] == 0x12007)
{
uint16_t port;
uint32_t ip;
printf("CALL name: [%s], cr3: [0x%lx] pid: [%d], %s(",
call->process_name,
call->cr3,
call->pid,
"CONNECT");
domain_read_current(parameter_values[6] + 0x14,
&port, sizeof(uint16_t));
domain_read_current(parameter_values[6] + 0x16,
&ip, sizeof(uint32_t));
ip = ntohl(ip);
port = ntohs(port);
printf("ip: %hu.%hu.%hu.%hu, port: %hu)\n",
(short)((ip >> 24) & 0xFF),
(short)((ip >> 16) & 0xFF),
(short)((ip >> 8) & 0xFF),
(short)(ip & 0xFF),
port);
}
else if (parameter_values[5] == 0x1201f)
{
printf("CALL name: [%s], cr3: [0x%lx] pid: [%d], %s(",
call->process_name,
call->cr3,
call->pid,
"SEND");
printf(")\n");
}
else if (parameter_values[5] == 0x12017)
{
printf("CALL name: [%s], cr3: [0x%lx] pid: [%d], %s(",
call->process_name,
call->cr3,
call->pid,
"RECV");
printf(")\n");
}
}
process_generic_syscall(call, parameter_values);
}
void process_NtRequestWaitReplyPort(struct syscall_info *call, uint32_t *parameter_values)
{
struct port_message {
uint16_t data_length;
uint16_t length;
uint16_t message_type;
uint16_t virtual_range_offset;
uint32_t client_id;
uint32_t message_id;
uint32_t callback_id;
char data[328];
} __attribute__ ((packed));
struct port_message message;
unsigned long final_address = parameter_values[1];
domain_read_current(final_address, &message, sizeof(struct port_message));
/* 0x90241 = magic DNS client id */
uint16_t length = 0;
uint32_t magic = 0;
/* read magic dword */
domain_read_current(final_address+20+8, &magic, sizeof(uint32_t));
if(magic == 0x90241)
{
domain_read_current(final_address+20+0x34, &length, sizeof(uint16_t));
char* dns_name =
convert_unicode_string(length, final_address+20+0x38);
if(dns_name != NULL)
{
printf("CALL name: [%s], cr3: [0x%lx] pid: [%d], %s(",
call->process_name,
call->cr3,
call->pid,
"DNS_LOOKUP");
base16_print(dns_name);
printf(")\n");
free(dns_name);
}
}
process_generic_syscall(call, parameter_values);
}
/* prints a windows NT syscall name and arguments given
* a libxc handle and a syscall_info structure
* for the system call
*/
void nt_print_syscall(int xc_iface, int domid, struct syscall_info* call)
{
/*uint32_t stack_ptr = 0;*/
/*uint32_t eprocess_ptr = 0;*/
/*uint32_t ethread_ptr = 0;*/
uint32_t *params = NULL;
int got_params = 0;
if(call->notification_type == SYSCALL_CALL)
{
if(nt_syscalls[call->number]->parameter_count != 0)
{
params = (uint32_t*)malloc(sizeof(uint32_t)*nt_syscalls[call->number]->parameter_count);
if(params == NULL)
{
printf("COULD NOT ALLOCATE MEMORY\n");
return;
}
got_params = domain_read_current(call->registers.edx + 8, params,
nt_syscalls[call->number]->parameter_count * 4);
sp_save_parameters(params, call);
}
}
else
{
struct saved_parameters *sp = NULL;
sp = sp_find_and_remove_parameters(call->pid, call->tid, call->number);
if(sp != NULL)
{
params = sp->parameters;
free(sp);
}
}
/* per-syscall processing */
if(nt_syscalls[call->number]->handler == NULL)
{
process_generic_syscall(call, params);
}
else
{
syscall_handler_func call_handler = nt_syscalls[call->number]->handler;
(*call_handler)(call, params);
}
if(call->notification_type == SYSCALL_RET)
{
/* only free parameter list
* on syscall return
*/
free(params);
}
}