Last active 9 months ago

addaudithook 可用來監聽 Python 內部的安全性相關事件,例如模組導入、檔案存取或執行系統命令,可用於安全監控、日誌記錄或限制不安全的操作。

timmy revised this gist 9 months ago. Go to revision

1 file changed, 12 insertions

audit_hook_example.py(file created)

@@ -0,0 +1,12 @@
1 + import sys
2 +
3 + def audit_hook(event, args):
4 + print(f"Audit event: {event}, Arguments: {args}")
5 +
6 + # 註冊審計鉤子
7 + sys.addaudithook(audit_hook)
8 +
9 + # 觸發一些事件
10 + open("test.txt", "w").write("Hello") # 檔案寫入事件
11 + import os # 模組導入事件
12 + os.system("echo Security Check") # 執行命令事件
Newer Older