Last active
April 27, 2024 13:12
-
-
Save umutbasal/330dced7ed70008ce30fa52b876d0771 to your computer and use it in GitHub Desktop.
ebpf syscall count by user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from bcc import BPF | |
from time import sleep | |
from bcc.syscall import syscall_name, syscalls | |
program = r""" | |
RAW_TRACEPOINT_PROBE(sys_enter) { | |
u64 uid; | |
u64 counter = 0; | |
u64 *p; | |
uid = bpf_get_current_uid_gid() & 0xFFFFFFFF; | |
int opcode = ctx->args[1]; | |
bpf_trace_printk("%d, %d", uid, opcode); | |
return 0; | |
} | |
""" | |
counter = {} | |
b = BPF(text=program) | |
import time | |
import pwd | |
from tabulate import tabulate | |
import os | |
#b.trace_print() | |
# | userid | syscall | count | | |
# |--------|---------|-------| | |
# | root | execv | 10 | | |
# | | | | | |
# | | | | | |
head = ["userid", "syscall", "count"] | |
start_time = time.time() | |
while 1: | |
try: | |
(task, pid, cpu, flags, ts, msg) = b.trace_fields() | |
(uid, opcode) = msg.split(b", ") | |
except ValueError: | |
continue | |
if uid not in counter: | |
counter[uid] = {} | |
if opcode not in counter[uid]: | |
counter[uid][opcode] = 0 | |
counter[uid][opcode] += 1 | |
current_time = time.time() | |
if current_time - start_time > 1: | |
data = [] | |
for uid in counter: | |
user = pwd.getpwuid(int(uid)).pw_name | |
for opcode in counter[uid]: | |
data.append([user, syscall_name(int(opcode)), counter[uid][opcode]]) | |
os.system('clear') | |
print(tabulate(data, headers=head)) | |
print("\n") | |
start_time = current_time |
Author
umutbasal
commented
Apr 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment