File tree 2 files changed +29
-1
lines changed
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## 开发指引
4
4
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个执行命令。
6
7
- ` check_cmd ` :
7
8
- 功能:判断当前执行环境是否满足工具要求。
8
9
> 比如某些工具只能在linux下执行,需要判断当前是否为linux环境。
Original file line number Diff line number Diff line change 12
12
"""
13
13
14
14
import os
15
+ import re
15
16
import json
16
17
import fnmatch
17
18
import argparse
@@ -135,6 +136,30 @@ def __get_path_filters(self, task_params):
135
136
"re_exclusion" : regex_exlucde_paths
136
137
}
137
138
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
+
138
163
def __scan (self ):
139
164
"""
140
165
扫码代码
@@ -190,6 +215,8 @@ def __scan(self):
190
215
print ("[debug] get diff files: %s" % diff_files )
191
216
192
217
# todo: 此处需要自行实现工具逻辑,输出结果,存放到result列表中
218
+ # todo: 如果需要执行命令行(比如subprocess调用),请调用 __detect_injection_symbols 方法检查命令行字符串是否存在注入符号,防止命令行注入漏洞
219
+
193
220
# todo: 这里是demo结果,仅供展示,需要替换为实际结果
194
221
demo_path = os .path .join (source_dir , "run.py" )
195
222
result = [
You can’t perform that action at this time.
0 commit comments