如何使用pip-audit扫描Python包中的安全漏洞

关于pip-audit

pip-audit是一款功能强大的安全漏洞扫描工具,该工具主要针对Python环境,可以帮助广大研究人员扫描和测试Python包中的已知安全漏洞。pip-audit使用了PythonPackagingAdvisory数据库和PyPIJSONAPI作为漏洞报告源。

功能介绍

  • 支持对本地环境和依赖组件(requirements风格文件)进行安全审计;
  • 支持多种漏洞服务(PyPI、OSV);
  • 支持以CycloneDX XML或JSON格式发送SBOM;
  • 提供人类和机器均可读的输出格式(columnar、JSON);
  • 无缝接入 / 重用本地pip缓存;

工具安装

pip-audit基于Python开发,且要求本地环境为Python 3.7或更新版本。安装并配置好Python环境之后,就可以使用下列命令并通过pip来安装pip-audit了:

python-mpipinstallpip-audit

第三方包

pip-audit的正常运行需要使用到多个第三方包,具体组件包名称和版本如下图所示:

除此之外,我们还可以通过conda来安装pip-audit:

condainstall-cconda-forgepip-audit

工具使用

我们可以直接将pip-audit以独立程序运行,或通过”python -m”运行:

pip-audit--help

python-mpip_audit--help
usage: pip-audit [-h] [-V] [-l] [-rREQUIREMENTS] [-fFORMAT] [-sSERVICE]

[-d] [-S] [--desc [{on,off,auto}]] [--cache-dirCACHE_DIR]

[--progress-spinner {on,off}] [--timeoutTIMEOUT]

[--pathPATHS] [-v] [--fix] [--require-hashes]



auditthePythonenvironmentfordependencieswithknownvulnerabilities



optionalarguments:

-h, --helpshowthishelpmessageandexit

-V, --versionshowprogram's version number and exit

-l, --localshowonlyresultsfordependenciesinthelocal

environment (default: False)

-rREQUIREMENTS, --requirementREQUIREMENTS

auditthegivenrequirementsfile; thisoptioncanbe

usedmultipletimes (default: None)

-fFORMAT, --formatFORMAT

theformattoemitauditresultsin (choices: columns,

json, cyclonedx-json, cyclonedx-xml) (default:

columns)

-sSERVICE, --vulnerability-serviceSERVICE

thevulnerabilityservicetoauditdependencies

against (choices: osv, pypi) (default: pypi)

-d, --dry-runwithout`--fix`: collectalldependenciesbutdonot

performtheauditingstep; with`--fix`: performthe

auditingstepbutdonotperformanyfixes (default:

False)

-S, --strictfailtheentireauditifdependencycollectionfails

onanydependency (default: False)

--desc [{on,off,auto}]

includeadescriptionforeachvulnerability; `auto`

defaultsto`on`forthe`json`format. Thisflaghas

noeffectonthe`cyclonedx-json`or`cyclonedx-xml`

formats. (default: auto)

--cache-dirCACHE_DIR

thedirectorytouseasanHTTPcacheforPyPI; uses

the`pip`HTTPcachebydefault (default: None)

--progress-spinner {on,off}

displayaprogressspinner (default: on)

--timeoutTIMEOUTsetthesockettimeout (default: 15)

--pathPATHSrestricttothespecifiedinstallationpathfor

auditingpackages; thisoptioncanbeusedmultiple

times (default: [])

-v, --verbosegivemoreoutput; thissettingoverridesthe

`PIP_AUDIT_LOGLEVEL`variableandisequivalentto

settingitto`debug` (default: False)

--fixautomaticallyupgradedependencieswithknown

vulnerabilities (default: False)

--require-hashesrequireahashtocheckeachrequirementagainst, for

repeatableaudits; thisoptionisimpliedwhenany

packageinarequirementsfilehasa`--hash`option.

(default: False)

退出代码

任务完成后, pip-audit将会退出运行,并返回一个代码以显示其状态,其中:

  • 0:未检测到已知漏洞;
  • 1:检测到了一个或多个已知漏洞;

工具使用样例

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

$pip-audit

Noknownvulnerabilitiesfound

$ pip-audit No known vulnerabilities found审计给定requirements文件的依赖:

$pip-audit-r ./requirements.txt

Noknownvulnerabilitiesfound

审计一个requirements文件,并排除系统包:

$pip-audit-r ./requirements.txt-l

Noknownvulnerabilitiesfound

审计依赖中发现的安全漏洞:

$pip-audit

Found2knownvulnerabilitiesin1package

NameVersionIDFixVersions

-------------------------------------

Flask0.5PYSEC-2019-1791.0

Flask0.5PYSEC-2018-660.12.3

审计依赖(包含描述):

$pip-audit--desc

Found2knownvulnerabilitiesin1package

NameVersionIDFixVersionsDescription

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Flask0.5PYSEC-2019-1791.0ThePalletsProjectFlaskbefore1.0isaffectedby: unexpectedmemoryusage. Theimpactis: denialofservice. Theattackvectoris: craftedencodedJSONdata. Thefixedversionis: 1.NOTE: thismayoverlapCVE-2018-1000656.

Flask0.5PYSEC-2018-660.12.3ThePalletsProjectflaskversionBefore0.12.3containsaCWE-20: ImproperInputValidationvulnerabilityinflaskthatcanresultinLargeamountofmemoryusagepossiblyleadingtodenialofservice. ThisattackappeartobeexploitableviaAttackerprovidesJSONdatainincorrectencoding. Thisvulnerabilityappearstohavebeenfixedin0.12.3. NOTE: thismayoverlapCVE-2019-1010083.

审计JSON格式依赖:

$pip-audit-fjson|jq

Found2knownvulnerabilitiesin1package

[

{

"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": []

}

]

审计并尝试自动审计存在漏洞的依赖:

$pip-audit--fix

Found2knownvulnerabilitiesin1packageandfixed2vulnerabilitiesin1package

NameVersionIDFixVersionsAppliedFix

------------------------------------------------------------------------------

flask0.5PYSEC-2019-1791.0Successfullyupgradedflask (0.5=>1.0)

flask0.5PYSEC-2018-660.12.3Successfullyupgradedflask (0.5=>1.0)

许可证协议

本项目的开发与发布遵循 Apache 2.0开源许可证协议。

项目地址

点审核:【​​GitHub传送门​​】

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/130982.html<

(0)
管理的头像管理
上一篇2025-02-26 08:12
下一篇 2025-02-26 08:14

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注