Python结合Bandit 完成代码安全检测

前言
  1. 本章讲解一下,在Python如何使用Bandit进行代码检测。
  2. Bandit 是一款 Python 源码分析框架,可用于 Python 代码的安全性分析。Bandit 使用标准库中的 AST 模块,将 Python 源码解析成 Python 语法节点构成的树。Bandit 允许用户编写自定义的测试。测试完成后,Bandit 会生成针对源码的安全报告。
安装Bandit
pip install bandit
检测是否安装成功, bandit -v (等同于: bandit --version)

(rpa_client) C:\Users\xxxxxx\rpa_client_re\transfer_data>bandit --version
bandit 1.7.4
  python version = 3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)]

(rpa_client) C:\Users\wang984\Desktop\Rpa_Re\rpa_client_re\transfer_data>bandit -v
usage: bandit [-h] [-r] [-a {file,vuln}] [-n CONTEXT_LINES] [-c CONFIG_FILE]
              [-p PROFILE] [-t TESTS] [-s SKIPS]
              [-l | --severity-level {all,low,medium,high}]
              [-i | --confidence-level {all,low,medium,high}]
              [-f {csv,custom,html,json,screen,txt,xml,yaml}]
              [--msg-template MSG_TEMPLATE] [-o [OUTPUT_FILE]] [-v] [-d] [-q]
              [--ignore-nosec] [-x EXCLUDED_PATHS] [-b BASELINE]
              [--ini INI_PATH] [--exit-zero] [--version]
              [targets [targets ...]]
Bandit举例的扫描列表 (B问题编号 问题名称)
-----------------------------------------------
        B101    assert_used
        B102    exec_used
        B103    set_bad_file_permissions
        B104    hardcoded_bind_all_interfaces
        B105    hardcoded_password_string
        B106    hardcoded_password_funcarg
        B107    hardcoded_password_default
        B108    hardcoded_tmp_directory
        B110    try_except_pass
        B112    try_except_continue
        B201    flask_debug_true
        B301    pickle
        B302    marshal
        B303    md5
        B304    ciphers
        B305    cipher_modes
        B306    mktemp_q
        B307    eval
        B308    mark_safe
        B309    httpsconnection
        B310    urllib_urlopen
        B311    random
        B312    telnetlib
        B313    xml_bad_cElementTree
        B314    xml_bad_ElementTree
        B315    xml_bad_expatreader
        B316    xml_bad_expatbuilder
        B317    xml_bad_sax
        B318    xml_bad_minidom
        B319    xml_bad_pulldom
        B320    xml_bad_etree
        B321    ftplib
        B323    unverified_context
        B324    hashlib_new_insecure_functions
        B325    tempnam
        B401    import_telnetlib
        B402    import_ftplib
        B403    import_pickle
        B404    import_subprocess
        B405    import_xml_etree
        B406    import_xml_sax
        B407    import_xml_expat
        B408    import_xml_minidom
        B409    import_xml_pulldom
        B410    import_lxml
        B411    import_xmlrpclib
        B412    import_httpoxy
        B413    import_pycrypto
        B501    request_with_no_cert_validation
        B502    ssl_with_bad_version
        B503    ssl_with_bad_defaults
        B504    ssl_with_no_version
        B505    weak_cryptographic_key
        B506    yaml_load
        B507    ssh_no_host_key_verification
        B601    paramiko_calls
        B602    subprocess_popen_with_shell_equals_true
        B603    subprocess_without_shell_equals_true
        B604    any_other_function_with_shell_equals_true
        B605    start_process_with_a_shell
        B606    start_process_with_no_shell
        B607    start_process_with_partial_path
        B608    hardcoded_sql_expressions
        B609    linux_commands_wildcard_injection
        B610    django_extra_used
        B611    django_rawsql_used
        B701    jinja2_autoescape_false
        B702    use_of_mako_templates
        B703    django_mark_safe
————————————————
  • 问题具体可参考:https://bandit.readthedocs.io/en/1.7.4/plugins/index.html#complete-test-plugin-listing
使用案例
  1. 文件夹扫描 / 文件扫描
1. bandit -r 文件夹地址    (-r 必要参数,标识扫描内容)
2. bandit 文件地址   (eg: bandit test.py)
  1. 扫描结果记录至文件中
# 扫描当前文件夹下的所有py文件,并记录在文本呢文件中

1. bandit *.py -o test_bandit.txt -f txt      # -o 指定文件

3.扫描结果记录至html文件中

# 扫描工程根目录下的所有文件 .点表示所有!

1. bandit -r . -o test_bandit.html -f html
  1. 检测内容是否存在某个或者某些问题
1. bandit -r . -o test_bandit.html -f html -t B107   仅扫描是否存在B107

2. bandit -r . -o test_bandit.html -f html -t B107,B404  多个问题扫描使用逗号分隔
  1. 设置屏蔽列表
# 漏洞函数列表内自定义非 危险函数

方法(1):
    1. bandit -r . -o test_bandit.html -f html -s B602,B404,B107
    2. -s 参数拼接 屏蔽函数ID, 使用,间隔

方法(2):
    1. 对扫描文件,具体函数追加注释   # nosec
    2. from subprocess import Popen, PIPE # nosec

方法(3):
    1. 在扫描文件夹目录中新建.bandit 文件
    2. 编辑文件内容
        [bandit]
        skips: B404
    3.多个问题屏蔽
        [bandit]
        skips: B404,B107,B602
总结
  1. 以上观点供大家参考使用,如有异议,欢迎一起谈论。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值