
Bandit是一个用于分析Python代码审计安全漏洞的工具,旨在识别常见的安全问题,如硬编码密码、不安全的函数调用、SQL注入等。它通过构建抽象语法树(AST)并对节点运行适当的插件来检测潜在的安全问题。Bandit最初由OpenStack安全项目开发,后来迁移到PyCQA(Python代码质量权威)维护。
Bandit的核心架构包括以下几个主要组件:
ast模块解析代码并生成AST,然后遍历AST节点,调用相应的插件进行检测。@test_properties装饰器注册,并定义检测逻辑。AST解析与插件调用:
from bandit.core import manager
b_mgr = manager.BanditManager(config, agg_type)
b_mgr.run_tests()
这段代码展示了Bandit如何通过
BanditManager类管理AST解析和插件调用。插件定义:
@test_properties.checks('Call')
@test_properties.test_id('B102')
def exec_used(context):
if context.call_function_name_qual == 'exec':
return bandit.Issue(
severity=bandit.MEDIUM,
confidence=bandit.HIGH,
cwe=issue.Cwe.OS_COMMAND_INJECTION,
text='Use of exec detected.',
)
这段代码定义了一个检测
exec函数使用的插件,当检测到
exec函数调用时,会返回一个安全问题。配置文件示例:
include:
- '*.py'
profiles:
test:
include:
- start_process_with_a_shell
shell_injection:
subprocess: []
shell:
- os.system
这个配置文件定义了检测规则,指定了需要检测的文件类型和插件。Bandit适用于以下场景:
应用测试
测试环境 ubuntu 22.04
打开terminal
git clone https://github.com/PyCQA/bandit.git //下载bandit项目
cd bandit
sudo python setup.py install //安装bandit
如果安装过程有缺失其他模块,则先安装缺失的模块即可
下载一个带有python安全漏洞分险的项目 这里我用的Vulnerable-Flask-App项目
执行 bandit /home/chendong/bandit/Vulnerable-Flask-App/app/app.py 审计指定的.py源代码文件2.测试结果如下显示了几个安全问题
Total issues (by severity):
Undefined: 0
Low: 6
Medium: 2
High: 1
Total issues (by confidence):
Undefined: 0
Low: 1
Medium: 4
High: 4具体安全问题描述如下
Test results:
>> Issue: [B105:hardcoded_password_string] Possible hardcoded password: 'secret'
Severity: Low Confidence: Medium
CWE: CWE-259 (https://cwe.mitre.org/data/definitions/259.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b105_hardcoded_password_string.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:26:11
25 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
26 app.config['SECRET_KEY_HMAC'] = 'secret'
27 app.config['SECRET_KEY_HMAC_2'] = 'am0r3C0mpl3xK3y'
--------------------------------------------------
>> Issue: [B105:hardcoded_password_string] Possible hardcoded password: 'am0r3C0mpl3xK3y'
Severity: Low Confidence: Medium
CWE: CWE-259 (https://cwe.mitre.org/data/definitions/259.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b105_hardcoded_password_string.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:27:11
26 app.config['SECRET_KEY_HMAC'] = 'secret'
27 app.config['SECRET_KEY_HMAC_2'] = 'am0r3C0mpl3xK3y'
28 app.secret_key = 'F12Zr47j\3yX R~X@H!jmM]Lwf/,?KT'
--------------------------------------------------
>> Issue: [B105:hardcoded_password_string] Possible hardcoded password: 'F12Zr47jyX R~X@H!jmM]Lwf/,?KT'
Severity: Low Confidence: Medium
CWE: CWE-259 (https://cwe.mitre.org/data/definitions/259.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b105_hardcoded_password_string.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:28:17
27 app.config['SECRET_KEY_HMAC_2'] = 'am0r3C0mpl3xK3y'
28 app.secret_key = 'F12Zr47j\3yX R~X@H!jmM]Lwf/,?KT'
29 app.config['STATIC_FOLDER'] = None
--------------------------------------------------
>> Issue: [B105:hardcoded_password_string] Possible hardcoded password: 'admin123'
Severity: Low Confidence: Medium
CWE: CWE-259 (https://cwe.mitre.org/data/definitions/259.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b105_hardcoded_password_string.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:63:24
62 user.username = 'admin'
63 user.password = 'admin123'
64 db.session.add(user)
--------------------------------------------------
>> Issue: [B324:hashlib] Use of weak MD5 hash for security. Consider usedforsecurity=False
Severity: High Confidence: High
CWE: CWE-327 (https://cwe.mitre.org/data/definitions/327.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b324_hashlib.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:141:24
140 password = content['password']
141 hash_pass = hashlib.md5(password).hexdigest()
142 new_user = User(username, hash_pass)
--------------------------------------------------
>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
Severity: Medium Confidence: Low
CWE: CWE-89 (https://cwe.mitre.org/data/definitions/89.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b608_hardcoded_sql_expressions.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:261:32
260 print(search_term)
261 str_query = "SELECT first_name, last_name, username FROM customer WHERE username = '%s';" % search_term
262 # mycust = Customer.query.filter_by(username = search_term).first()
--------------------------------------------------
>> Issue: [B311:blacklist] Standard pseudo-random generators are not suitable for security/cryptographic purposes.
Severity: Low Confidence: High
CWE: CWE-330 (https://cwe.mitre.org/data/definitions/330.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/blacklists/blacklist_calls.html#b311-random
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:295:15
294 f = request.files['file']
295 rand = random.randint(1, 100)
296 fname = secure_filename(f.filename)
--------------------------------------------------
>> Issue: [B311:blacklist] Standard pseudo-random generators are not suitable for security/cryptographic purposes.
Severity: Low Confidence: High
CWE: CWE-330 (https://cwe.mitre.org/data/definitions/330.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/blacklists/blacklist_calls.html#b311-random
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:319:15
318 f = request.files['file']
319 rand = random.randint(1, 100)
320 fname = secure_filename(f.filename)
--------------------------------------------------
>> Issue: [B506:yaml_load] Use of unsafe yaml load. Allows instantiation of arbitrary objects. Consider yaml.safe_load().
Severity: Medium Confidence: High
CWE: CWE-20 (https://cwe.mitre.org/data/definitions/20.html)
More Info: https://bandit.readthedocs.io/en/1.8.4.dev9/plugins/b506_yaml_load.html
Location: /home/chendong/bandit/Vulnerable-Flask-App/app/app.py:329:16
328
329 ydata = yaml.load(y)
330
Bandit是一个功能强大的Python代码安全分析工具,通过AST解析和插件机制,能够有效识别代码中的常见安全问题。其灵活的配置和多种输出格式使其易于集成到现有的开发流程中,帮助开发者和安全团队提高代码的安全性。
github链接地址:https://github.com/PyCQA/bandit.git