<<
path:
root/public/blog.git/html/dev/syscalls.py
blob: 709ad90b0c3a3a1274b9ac49386af79fd1e25c0b
[raw]
[clear marker]
4from sys import exit, argv
5from pathlib import Path
9HELP = """Usage: syscalls.py [OPTIONS] [FP]
12 -r <program path> Records syscalls
13 -p <strace output file> Prints the used syscalls from your 'strace.txt'.
14 If the path is omitted, it looks after 'strace.txt'
15 in your current directory.
19STRACE_OUTPUT_FP = Path("strace.txt")
21SCMP_BOUNDARY_MARKER = "---- SECCOMP BOUNDARY ----"
26 if len(args) == 1 or len(args) > 3:
30 if "h" in args[1] or "help" in args[1]:
36 print("Need filepath to program")
42 fp = STRACE_OUTPUT_FP if len(args) == 2 else args[2]
43 gather_unique_syscalls(fp)
45 print("Unknown argument")
49def strace_run(program):
60def gather_unique_syscalls(fp):
62 print("File does not exist:", fp)
65 content = open_file_or_exit(fp)
67 (i for i, s in enumerate(content) if SCMP_BOUNDARY_MARKER in s), None
70 if boundary_idx == None:
71 print("Cannot find seccomp boundary marker")
76 after_boundary = content[boundary_idx:]
79 for i, line in enumerate(after_boundary):
83 syscall = f"[ERR: at line {i + boundary_idx}]"
90 print("/** Copy pasta from dev/syscalls.py */")
91 print("SECCOMP_ALLOWED_SYSCALLS :: string.[")
94 code = f' "{item.upper()}",'
103 subprocess.run(cmd, text=True, check=True)
104 except subprocess.CalledProcessError as e:
105 print(f"Command failed {e.returncode}: {e.stderr}")
109def open_file_or_exit(fp):
111 with open(fp, 'r', encoding="utf8") as f:
113 except Exception as e:
118if __name__ == "__main__":
123 except KeyboardInterrupt:
124 print("Terminated by user")