Skip to content

Commit 20e8740

Browse files
committed
🎨增加示例公共方法-命令行注入符号检查
1 parent 6ef73f3 commit 20e8740

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 开发指引
44
1. 修改`src/main.py`,按需实现工具逻辑。
5-
2. `tool.json`文件里声明了2个字段,`check_cmd``run_cmd`,对应`src/main.py`中需要实现的2个执行命令。
5+
2. 如果工具逻辑中需要执行命令行,请检查命令行字符串是否存在注入符号,防止命令行注入漏洞(参考`src/main.py`中的`__detect_injection_symbols`方法)
6+
3. `tool.json`文件里声明了2个字段,`check_cmd``run_cmd`,对应`src/main.py`中需要实现的2个执行命令。
67
- `check_cmd`
78
- 功能:判断当前执行环境是否满足工具要求。
89
>比如某些工具只能在linux下执行,需要判断当前是否为linux环境。

src/main.py

+27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import os
15+
import re
1516
import json
1617
import fnmatch
1718
import argparse
@@ -135,6 +136,30 @@ def __get_path_filters(self, task_params):
135136
"re_exclusion": regex_exlucde_paths
136137
}
137138

139+
def __detect_injection_symbols(self, cmd_str, symbols=True, raise_exception=True):
140+
"""检测可注入的符号,防止命令行注入
141+
:param cmd_str: <str> 命令行字符串
142+
:param symbols: <bool|regexp> 检查命令注入符号,默认为True,检查全部;指定regexp(正则表达式)时,检查传入的指定符号。
143+
全部注入符号正则表达式为 "\n|;|&+|\|+|`|\$\("
144+
:param raise_exception: <bool> 发现注入符号时,是否抛异常,默认抛异常
145+
"""
146+
if isinstance(symbols, bool) and symbols is True:
147+
symbols = "|".join([
148+
"\n",
149+
";",
150+
"&+",
151+
"\|+",
152+
"`",
153+
"\$\(",
154+
])
155+
if isinstance(symbols, str):
156+
match_chars = re.findall(symbols, cmd_str)
157+
if match_chars:
158+
if raise_exception:
159+
raise Exception(f"Find Injection Symbols({match_chars}) in command: {cmd_str}")
160+
else:
161+
print(f"Find Injection Symbols({match_chars}) in command: {cmd_str}")
162+
138163
def __scan(self):
139164
"""
140165
扫码代码
@@ -190,6 +215,8 @@ def __scan(self):
190215
print("[debug] get diff files: %s" % diff_files)
191216

192217
# todo: 此处需要自行实现工具逻辑,输出结果,存放到result列表中
218+
# todo: 如果需要执行命令行(比如subprocess调用),请调用 __detect_injection_symbols 方法检查命令行字符串是否存在注入符号,防止命令行注入漏洞
219+
193220
# todo: 这里是demo结果,仅供展示,需要替换为实际结果
194221
demo_path = os.path.join(source_dir, "run.py")
195222
result = [

0 commit comments

Comments
 (0)