Skip to content

Instantly share code, notes, and snippets.

@Ddorda
Created March 25, 2018 13:08
Show Gist options
  • Save Ddorda/4fc00de9545bfa080f6ead8a680e432a to your computer and use it in GitHub Desktop.
Save Ddorda/4fc00de9545bfa080f6ead8a680e432a to your computer and use it in GitHub Desktop.
Get Syscalls numbers dynamically from header files
UNISTD_PATH = '/usr/include/x86_64-linux-gnu/asm/unistd_64.h'
def get_syscall_numbers(unistd_path=UNISTD_PATH):
d = {}
with open(unistd_path, 'r') as f:
for line in f.readlines():
if line.startswith('#define __NR_'):
try:
syscall_data = line.strip().split()
d[syscall_data[1].replace('__NR_', '')] = int(syscall_data[2])
except:
pass
return d
SYSCALLS = get_syscall_numbers()
print SYSCALLS['perf_event_open']
# can be used with:
# import ctypes
# libc = ctypes.CDLL(None)
# syscall = libc.syscall
# syscal(SYSCALLS['perf_event_open'], ....)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment