-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdsysno.py
More file actions
executable file
·67 lines (55 loc) · 2.16 KB
/
rdsysno.py
File metadata and controls
executable file
·67 lines (55 loc) · 2.16 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
#!/bin/python3
import argparse
import random
import time
def randomize_sysno(sysnos):
rdsysnos = sysnos[:]
random.seed(time.time())
random.shuffle(rdsysnos)
# Some syscalls are not allowed to be randomized
# because they are also used by the kernel
def put_back(syscall):
i = syscalls.index(syscall)
j = rdsysnos.index(sysnos[i])
rdsysnos[i], rdsysnos[j] = rdsysnos[j], rdsysnos[i]
if "x86_64" in args.input:
put_back("arch_prctl")
put_back("restart_syscall")
put_back("rt_sigreturn")
if "aarch64" in args.input:
put_back("restart_syscall")
put_back("rt_sigreturn")
return rdsysnos
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Randomize syscall numbers")
parser.add_argument("--input", "-i", required=True, help="Input file (syscall.h.in)")
parser.add_argument("--output", "-o", required=True, help="Output file (syscall.h)")
parser.add_argument("--table", "-t", required=False, help="Output syscall table (.sysno.tbl)")
parser.add_argument("--disable", "-d", action="store_true", help="Disable randomization")
args = parser.parse_args()
if not ("x86_64" in args.input or "aarch64" in args.input):
print("Only x86_64 and aarch64 are supported")
exit(1)
syscalls, sysnos = [], []
with open(args.input, "r") as f:
lines = f.readlines()
for line in lines:
arr = line.split()
if len(arr) < 3:
continue
syscalls.append(arr[1].replace("__NR_", ""))
sysnos.append(int(arr[2]))
if not args.disable:
rdsysnos = randomize_sysno(sysnos)
else:
rdsysnos = sysnos
with open(args.output, "w") as f:
for syscall, sysno in zip(syscalls, rdsysnos):
f.write(f"#define __NR_{syscall:<35} {sysno}\n")
f.write("\n")
for syscall, sysno in zip(syscalls, rdsysnos):
f.write(f"#define SYS_{syscall:<36} {sysno}\n")
if args.table is not None:
with open(args.table, "w") as f:
for sysno, rd in zip(sysnos, rdsysnos):
f.write(f"{sysno}\n{rd}\n")