opensnitch 修改process.path规则匹配命令行参数及配置组合规则
Opensnitch 规则定制问题解答
1 能否实现仅放行指定Python脚本的出站连接?
能。
无需全局放行/usr/bin/python解释器,只需将解释器路径与对应脚本的命令行参数两个条件做AND合取,只有完全符合条件的进程连接才会触发对应动作。
2 AND合取逻辑的语法规则及示例
Gustavo Iniguez Goya维护的Opensnitch fork版本中,单条件规则默认使用simple类型的operator,多条件AND合取需要将operator类型改为list:
list类型operator的rules数组内的所有子规则,默认遵循*逻辑与(AND)*关系,即所有条件同时满足才会触发规则- 如需修改为逻辑或(OR),可手动添加
"logical_op": "or"字段覆盖默认值
基础合取规则示例(仅放行指定Python脚本)
规则文件可直接保存为JSON格式,放到/etc/opensnitchd/rules/路径下,无需通过GUI手动配置:
{ "name": "allow-python-monitor-script-only", "enabled": true, "action": "allow", "duration": "always", "operator": { "type": "list", "rules": [ { "type": "simple", "operand": "process.path", "data": "/usr/bin/python" }, { "type": "regex", "operand": "process.command", "data": "^/usr/bin/python\\s+/home/arielf/scripts/monitor\\.py$" } ] } }
该规则仅当进程路径为
/usr/bin/python、且完整命令行匹配指定monitor.py脚本时才放行,其他Python脚本的出站连接不受该规则影响。
3 正则匹配及多条件组合配置方法
Opensnitch的operator支持regex类型,可对命令行参数、远程IP等对象做正则匹配,以下为常用场景示例:
3.1 正则匹配命令行参数
如需匹配/home/arielf/scripts/目录下所有Python脚本的出站连接,可使用通配正则:
{ "name": "allow-all-arielf-python-scripts", "enabled": true, "action": "allow", "duration": "always", "operator": { "type": "list", "rules": [ { "type": "simple", "operand": "process.path", "data": "/usr/bin/python" }, { "type": "regex", "operand": "process.command", "data": "^/usr/bin/python\\s+/home/arielf/scripts/.*\\.py$" } ] } }
3.2 正则匹配远程IP地址
如需仅拦截/usr/bin/curl访问192.168.1.0/24网段的连接,可通过dst.ip操作数匹配:
{ "name": "block-curl-to-local-subnet", "enabled": true, "action": "deny", "duration": "always", "operator": { "type": "list", "rules": [ { "type": "simple", "operand": "process.path", "data": "/usr/bin/curl" }, { "type": "regex", "operand": "dst.ip", "data": "^192\\.168\\.1\\.[0-9]{1,3}$" } ] } }
3.3 同时匹配命令行参数与远程IP
如需仅放行指定Python脚本连接203.0.113.0/24网段(如指定外部API服务器),可叠加两个条件做AND合取:
{ "name": "allow-python-script-to-api-only", "enabled": true, "action": "allow", "duration": "always", "operator": { "type": "list", "rules": [ { "type": "regex", "operand": "process.command", "data": "^/usr/bin/python\\s+/home/arielf/scripts/fetch_data\\.py$" }, { "type": "regex", "operand": "dst.ip", "data": "^203\\.0\\.113\\.[0-9]{1,3}$" } ] } }
所有规则修改后无需手动重启opensnitchd服务,守护进程会自动扫描
/etc/opensnitchd/rules/路径下的新增JSON文件并加载。
内容的提问来源于stack exchange,提问作者arielf
相关产品推荐
相关产品推荐

