-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenerate-bsd-syscalls.py
executable file
·350 lines (284 loc) · 10.3 KB
/
generate-bsd-syscalls.py
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
#!/usr/bin/env python
import os
import sys
import shutil
import string
import datetime
import json
# Make a temporary file of BSD syscalls without any additional shit around
def make_temp_bsd():
print "[+] Making temporary system call list in " + PATH_BSD_TEMP_FILE
fdin = open(PATH_BSD_SYSCALLS, "r+b")
fdout = open(PATH_BSD_TEMP_FILE, "w+b")
data = fdin.read()
data = data[data.find("0\t"):] # hacky way to set data ptr to proper field
# since first syscall is prepended with '0\t'
fdout.write(data)
fdout.close()
fdin.close()
print "\tdone"
# Generating BSD system call list as a data structure
# [number, return type, name, number of args, arg...n, source]
def bsd_list_generate():
print "[+] Generating system call matrix data structure"
fdin = open(PATH_BSD_TEMP_FILE, "r+b")
data = fdin.read()
fdin.close()
# Split data by lines
data = string.split(data, '\n')
# Remove all empty lines
for i, line in enumerate(data):
if not line:
data.pop(i)
# Remove all asm comments ';'
tmp = []
for i, line in enumerate(data):
if i == 0 or not line.startswith(';'):
tmp.append(line)
data = tmp
# Crafting syscall table (as 2 dimensional matrix)
syscall_matrix = []
for line in data:
entry = []
if line.startswith("#"):
# Apple shitheads are lousy.
if line.find("#endif") != -1:
entry.append("#endif")
syscall_matrix.append(entry)
continue
entry.append(line)
syscall_matrix.append(entry)
continue
line = line.replace(" (", "(")
elems = line.split()
# Syscall ID
entry.append(elems[0])
# Syscall return type
entry.append(elems[4])
# Syscall name
entry.append(elems[5][0:elems[5].find('(')])
# Enumerating arguments of the syscall
# This shit is kinda tricky. For couple of reasons: 1) There _are_
# corner cases (e.g. no args), 2) Unknown # of args per syscall, 3)
# argument can be more than a tuple e.g. 'struct name arg'
#
i = 5
argnum = 0 # we will use it to count number of args
arg = "" # we're using it to create each arg
tmp = [] # temporary list of args for each syscall
while True:
# Corner case where syscall takes 0 args (denoted "void")
if(elems[6] == '}' or elems[6] == "NO_SYSCALL_STUB;"):
entry.append(str(argnum))
break
# If argnum > 0 then it's our first iteration always
elif(i == 5):
arg = elems[i][elems[i].find('(')+1:]
i += 1
continue
elif(elems[i] != '}' and elems[i] != "NO_SYSCALL_STUB;"):
if(elems[i].find(')') != -1):
arg += ' '+elems[i][:elems[i].find(')')]
tmp.append(arg)
argnum += 1
entry.append(str(argnum))
break
if(elems[i].find(',') == -1):
arg += ' '+elems[i]
i += 1
continue
else:
arg += ' '+elems[i][:elems[i].find(',')]
tmp.append(arg)
argnum += 1
arg = ""
i += 1
else:
break
# We're adding args from our temporary list
for el in tmp:
entry.append(el)
# Strip prepended spaces from syscall's args
for i, elem in enumerate(entry):
entry[i] = elem.strip()
syscall_matrix.append(entry)
'''
for entry in syscall_matrix:
print entry
sys.exit()
'''
# Clean-up
cmd = "rm " + PATH_BSD_TEMP_FILE
os.system(cmd)
print "\tdone"
return syscall_matrix
# This comes in handy when generating HTML output
def determine_highest_num_args():
highest_num = 0
for el in bsd_syscall_list:
if el[0].startswith('#'):
continue
if(el[3] > highest_num):
highest_num = el[3]
print "[+] Highest # of args is " + highest_num
return highest_num
def make_syscall_file_xrefs():
print "[+] Generating tags file for " + PATH_XNU_SOURCE
# Stage 0: Generate tags for XNU source
cmd = PATH_EXUBERANT_CTAGS + " -R " + PATH_XNU_SOURCE
os.system(cmd)
print "\tdone"
# Stage 1: Mine for impl files!
print "[+] Mining for implementation files in tags file"
for i, syscall in enumerate(bsd_syscall_list):
if syscall[0].startswith('#'):
continue
# Technically, we could skip them. They ain't do notin'. So we do.
# However, since there are quite static throughout releases we can still
# add them into final output when generating HTML
#elif syscall[2] == "nosys":
# continue
#elif syscall[2] == "enosys":
# continue
else:
fd = open("tags", "r+b")
for line in fd.readlines():
line = line.strip()
#if line.find("struct "+syscall[2]+"_args") != -1:
cols = line.split()
if syscall[2] == cols[0]:
if line.find(".h") != -1:
continue
# LOL, it's pointless since cols are already splitted
# hence we can print cols[1] and have the same result
# look at solution for MACH traps ;)
data = string.split(line, '\t')
#print syscall[2] + ": " + data[0] + " " + data[1]
bsd_syscall_list[i].append(data[1].replace(PATH_XNU_SOURCE, URL_XNU_SOURCE))
'''
for elem in bsd_syscall_list:
print elem
'''
print "\tdone"
def generate_json():
print "[+] Generating JSON file..."
with open(OUTPUT_JSON, 'w') as fd:
json.dump(bsd_syscall_list, fd, indent=4)
print "\tdone"
def generate_html():
print "[+] Generating HTML output..."
html = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="macOS (xnu) BSD system calls reference table.">
<meta name="keywords" content="Apple, macOS, OS X, OSX, XNU, BSD, system calls, OS internals, reference, low level, programming, c, c++, assembler, kernel, reverse engineering, malware analysis">
<meta name="author" content="Andrzej Dyjak, dyjakan">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>macOS BSD System Calls</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
'''
html += "<div class=\"container-fluid\">\n"
html += BANNER
html += "\t<table class=\"table table-sm table-hover\">\n"
html += "\t<thead class=\"thead-inverse\">\n"
html += "\t\t<tr>\n"
html += "\t\t\t<th>" + "#" + "</th>\n"
html += "\t\t\t<th>" + "Name" + "</th>\n"
html += "\t\t\t<th>" + "RDI" + "</th>\n"
html += "\t\t\t<th>" + "RSI" + "</th>\n"
html += "\t\t\t<th>" + "RDX" + "</th>\n"
html += "\t\t\t<th>" + "RCX" + "</th>\n"
html += "\t\t\t<th>" + "R8" + "</th>\n"
html += "\t\t\t<th>" + "R9" + "</th>\n"
html += "\t\t\t<th>" + "Stack" + "</th>\n"
html += "\t\t\t<th>" + "Stack" + "</th>\n"
html += "\t\t\t<th>" + "Implementation" + "</th>\n"
html += "\t\t</tr>\n"
html += "\t</thead>\n"
for elem in bsd_syscall_list:
# Corner case where we have #define
# The loop takes care of blank cells in the table
if elem[0].startswith('#'):
html += "\t<tr class=\"table-info\">\n"
html += "\t\t<td>" + elem[0] + "</td>\n"
# Result of this loop is connected with try/except later; remember!
for i in range(highest_num_args+2):
html += "\t\t<td></td>\n"
html += "\t</tr>\n"
continue
html += "\t<tr>\n"
html += "\t\t<td>" + elem[0] + "</td>\n"
html += "\t\t<td>" + elem[1] + " <b>" + elem[2] + "</b></td>\n"
delta = highest_num_args - int(elem[3])
# Filling up, dynamically, arguments of a syscall
if(int(elem[3]) == 0):
for i in range(delta):
html += "\t\t<td>-</td>\n"
else:
for i in range(int(elem[3])):
html += "\t\t<td>" + elem[4+i] + "</td>\n"
for i in range(delta):
html += "\t\t<td>-</td>\n"
# Heuristics for pulling out implementation file, lawl
try:
link = elem[3+int(elem[3])+1]
html += "\t\t<td><a href=\"" + link + "\">" + link.replace(URL_XNU_SOURCE, '') + "</a></td>\n"
except:
html += "\t\t<td></td>\n"
html += "\t</tr>\n"
continue
html += "\t</tr>\n"
html += "\t</table>\n"
html += "</div>"
html += '''
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>
</html>
'''
with open(OUTPUT_HTML, 'w') as fd:
fd.write(html)
print "\tdone"
def main():
# Kung-foo for BSD syscalls
# bsd_syscall_list contains not only syscalls but also #defines!
make_temp_bsd()
global bsd_syscall_list
bsd_syscall_list = bsd_list_generate()
# Find what is the highest number of arguments
# Lawl, but it's actually _pretty_ handy!
global highest_num_args
highest_num_args = determine_highest_num_args();
highest_num_args = int(highest_num_args)
# Kung-foo for implementation files xrefs
make_syscall_file_xrefs()
# Dump bsd_syscall_list as JSON
generate_json()
# yayks!
generate_html()
print "[+] Great success!"
if __name__ == "__main__":
if(len(sys.argv) < 2):
print sys.argv[0] + " <XNU source path/>"
sys.exit(1)
PATH_XNU_SOURCE = sys.argv[1]
URL_XNU_SOURCE = "https://opensource.apple.com/source/xnu/xnu-4570.41.2/"
PATH_EXUBERANT_CTAGS = "/usr/local/Cellar/ctags/5.8_1/bin/ctags"
PATH_BSD_SYSCALLS = PATH_XNU_SOURCE + "bsd/kern/syscalls.master"
PATH_BSD_TEMP_FILE = "/tmp/bsd-syscall-tmp"
OUTPUT_JSON = "osx-bsd-syscalls.json"
OUTPUT_HTML = "osx-bsd-syscalls.html"
# mention: timestamp, original list from XNU sources, name/handle/email
BANNER = "<h1>macOS BSD System Calls</h1>\n"
v1 = PATH_XNU_SOURCE[PATH_XNU_SOURCE.find("xnu-"):]
ts = datetime.datetime.now().strftime("%A, %d %B %Y")
twitter = "<a href=\"https://twitter.com/andrzejdyjak\">@andrzejdyjak</a>"
BANNER += "<p>Generated from <i><a href=\"" + URL_XNU_SOURCE + "\">" + v1.upper() + "</a></i> on <i>" + ts + "</i> by " + twitter + ".</p>\n"
BANNER += "<p>Description for <a href=\"osx-bsd-syscalls.json\">JSON dump</a> elements (apart from <i>conditionals</i>):</p>"
BANNER += "<pre>[\n\tsyscall number,\n\treturn type,\n\tsyscall name,\n\tnumber of args,\n\targ 1, ..., arg n,\n\tsource\n]</pre>"
BANNER += "<p>You can find github repository <a href=\"https://github.com/dyjakan/osx-syscalls-list\">here</a>. Feedback, ideas, bugs, <i>et cetera</i> – <a href=\"https://sigsegv.pl\">give me a shout</a>.</p>\n"
main()