首页 > 编程语言 >如何发现Python依赖库漏洞

如何发现Python依赖库漏洞

时间:2022-11-30 17:03:04浏览次数:39  
标签:audit default 依赖 Python vulnerability 漏洞 version pip --

因为python编程的流行,python的各种库也越来越多,但许多小伙伴可能只注意到了自己编程所要依赖的环境,但是却忽略了库的版本也有可能存在漏洞的风险,如果不及时检查和更新python依赖库,那么很有可能你写的代码本身就存在漏洞,因为你引用了一个包含已知漏洞的库。

如何避免这种风险

今天就给大家带来一个python库环境漏洞检测工具。

pip-audit是一种用于扫描 Python 环境以查找具有已知漏洞的包的工具。它通过PyPI JSON API使用 Python 包装咨询数据库作为漏洞报告的来源。

该项目由Trail of Bits在 Google 的支持下开发。这不是 Google 的官方产品。

如何使用呢

安装

pip-audit需要Python 3.6或更新版本,可以直接通过pip安装:

python -m pip install pip-audit

使用方法

usage: pip-audit [-h] [-V] [-l] [-r REQUIREMENTS] [-f FORMAT] [-s SERVICE]
[-d] [-S] [--desc [{on,off,auto}]] [--cache-dir CACHE_DIR]
[--progress-spinner {on,off}] [--timeout TIMEOUT]

audit the Python environment for dependencies with known vulnerabilities

optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-l, --local show only results for dependencies in the local
environment (default: False)
-r REQUIREMENTS, --requirement REQUIREMENTS
audit the given requirements file; this option can be
used multiple times (default: None)
-f FORMAT, --format FORMAT
the format to emit audit results in (choices: columns,
json, cyclonedx-json, cyclonedx-xml) (default:
columns)
-s SERVICE, --vulnerability-service SERVICE
the vulnerability service to audit dependencies
against (choices: osv, pypi) (default: pypi)
-d, --dry-run collect all dependencies but do not perform the
auditing step (default: False)
-S, --strict fail the entire audit if dependency collection fails
on any dependency (default: False)
--desc [{on,off,auto}]
include a description for each vulnerability; `auto`
defaults to `on` for the `json` format. This flag has
no effect on the `cyclonedx-json` or `cyclonedx-xml`
formats. (default: auto)
--cache-dir CACHE_DIR
the directory to use as an HTTP cache for PyPI; uses
the `pip` HTTP cache by default (default: None)
--progress-spinner {on,off}
display a progress spinner (default: on)
--timeout TIMEOUT set the socket timeout (default: 15)

举例

审计当前Python环境的依赖项:

$ pip-audit
No known vulnerabilities found

审计一个给定需求文件的依赖关系:

$ pip-audit -r ./requirements.txt
No known vulnerabilities found

审计当前Python环境(不包括系统包)的依赖项:

$ pip-audit -r ./requirements.txt -l
No known vulnerabilities found

当存在漏洞时,审计依赖:

$ pip-audit
Found 2 known vulnerabilities in 1 packages
Name Version ID Fix Versions
---- ------- -------------- ------------
Flask 0.5 PYSEC-2019-179 1.0
Flask 0.5 PYSEC-2018-66 0.12.3

审计依赖项,包括以下描述:

$ pip-audit --desc
Found 2 known vulnerabilities in 1 packages
Name Version ID Fix Versions Description
---- ------- -------------- ------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Flask 0.5 PYSEC-2019-179 1.0 The Pallets Project Flask before 1.0 is affected by: unexpected memory usage. The impact is: denial of service. The attack vector is: crafted encoded JSON data. The fixed version is: 1. NOTE: this may overlap CVE-2018-1000656.
Flask 0.5 PYSEC-2018-66 0.12.3 The Pallets Project flask version Before 0.12.3 contains a CWE-20: Improper Input Validation vulnerability in flask that can result in Large amount of memory usage possibly leading to denial of service. This attack appear to be exploitable via Attacker provides JSON data in incorrect encoding. This vulnerability appears to have been fixed in 0.12.3. NOTE: this may overlap CVE-2019-1010083.

审计JSON格式的依赖:

$ pip-audit -f json | jq
Found 2 known vulnerabilities in 1 packages
[
{
"name": "flask",
"version": "0.5",
"vulns": [
{
"id": "PYSEC-2019-179",
"fix_versions": [
"1.0"
],
"description": "The Pallets Project Flask before 1.0 is affected by: unexpected memory usage. The impact is: denial of service. The attack vector is: crafted encoded JSON data. The fixed version is: 1. NOTE: this may overlap CVE-2018-1000656."
},
{
"id": "PYSEC-2018-66",
"fix_versions": [
"0.12.3"
],
"description": "The Pallets Project flask version Before 0.12.3 contains a CWE-20: Improper Input Validation vulnerability in flask that can result in Large amount of memory usage possibly leading to denial of service. This attack appear to be exploitable via Attacker provides JSON data in incorrect encoding. This vulnerability appears to have been fixed in 0.12.3. NOTE: this may overlap CVE-2019-1010083."
}
]
},
{
"name": "jinja2",
"version": "3.0.2",
"vulns": []
},
{
"name": "pip",
"version": "21.3.1",
"vulns": []
},
{
"name": "setuptools",
"version": "57.4.0",
"vulns": []
},
{
"name": "werkzeug",
"version": "2.0.2",
"vulns": []
},
{
"name": "markupsafe",
"version": "2.0.1",
"vulns": []
}
]

标签:audit,default,依赖,Python,vulnerability,漏洞,version,pip,--
From: https://blog.51cto.com/u_14573321/5899968

相关文章

  • Python高级-多任务协程-笔记
    为了更好的理解协程,可以翻阅上一篇​​ Python高级-多任务进程-笔记​​1.迭代器迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合......
  • 实现Trie树-Python
    #实现Trie树:字典套字典classTrie():def__init__(self):self.child={}definsert(self,word):current_node=self.child......
  • Python中5大模块的使用教程(collections模块、time时间模块、random模块、os模块、sys
    1.模块的简单认识定义:模块就是我们把装有特定功能的代码进行归类的结果.从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块(函数,类)<模......
  • python-面向对象-类的封装-私有方法私有属性
    1.封装,就是把客观事物封装成抽象的类,并规定类中的数据和方法只让可信的类或对象操作。封装可分为两个层面:(1)第一层面的封装,创建类和对象时,分别创建两者的名称,只能通过类名或......
  • Python 日期(字符串格式)增加n天并返回(字符串格式)
    fromdatetimeimportdatetimefromdatetimeimporttimedeltadefdate_add(date_str,days_count=1):date_list=time.strptime(date_str,"%Y-%m-%d")y,......
  • Python3 notes
    Python3基础标识符第一个字符必须是字母表中字母或下划线_。标识符的其他的部分由字母、数字和下划线组成。标识符对大小写敏感。在Python3中,可以用中文作为变......
  • Python学习(二):字符串常用函数有哪些?
    1.检验字符串长度:len(str);a="hellopython"len(a)12a="hellopython"len(a[::2])##从头取到尾,隔一个取值的长度6 2.切割字符串:obj.split(str);a="hell......
  • redis 漏洞复现
    漏洞产生的前提条件1、redis绑定在0.0.0.0:6379,且没有进行添加防火墙规则避免其他非信任来源ip访问等相关安全策略,直接暴露在公网;2、没有设置密码认证(一般为空),可......
  • python-解力扣提【两数相加】
    1.题目  2.无任何参考下自己的解题代码 解题思路:i和j在列表索引中循环,不相等且两数相加等于target则返回[i,j] 3.参考大神代码解题思路:1).enumerate多用于在f......
  • Python-pyreqs库,python项目环境迁移(检阅所使用库)
    前言在python项目部署或是迁移时,需要对项目所使用环境也进行迁移,操作方法很多也很复杂,python中提供了pyreqs库,可对项目所使用的python库进行检索并导出为文件,可方便进行环......