传统的威胁建模往往介入得太晚,有时甚至完全缺席。此外,手动创建数据流和报告可能极其耗时。pytm 的目标是将威胁建模左移,使威胁建模更加自动化并以开发者为中心。
基于你的输入和架构设计的定义,pytm 可以自动生成以下内容:
tm.py 是一个示例模型。你可以运行它来生成它所引用的报告和图表图像文件:
mkdir -p tm
./tm.py --report docs/basic_template.md | pandoc -f markdown -t html > tm/report.html
./tm.py --dfd | dot -Tpng -o tm/dfd.png
./tm.py --seq | java -Djava.awt.headless=true -jar $PLANTUML_PATH -tpng -pipe > tm/seq.png
还有一个示例 Makefile,它将所有这些封装成可轻松共享给多个模型的目标。如果你安装了 GNU make(Linux 发行版默认可用,但 OSX 上不可用),只需运行:
make MODEL=the_name_of_your_model_minus_.py
你应该将 plantuml.jar 放在与模型相同的目录中,或者设置 PLANTUML_PATH。
为了避免安装所有依赖项,例如 pandoc 或 Java,该脚本可以在容器内运行:
# do this only once
export USE_DOCKER=true
make image
# call this after every change in your model
make
为了简化 pytm 的使用,可以使用 Devbox 将主机依赖完全隔离。这通常是比 OCI 容器方案开销更低、更方便的替代方案。
curl -fsSL https://get.jetify.com/devbox | bashdevbox version update~/.config/nix/nix.conf 文件中设置你的 GitHub 访问令牌:access-tokens = github.com=YOUR_TOKEN_HEREdevbox.json 文件中指定的所有工具和包:devbox shellpython 时将使用的 Python 可执行文件的完整路径。输出应为以下路径:.devbox/nix/profile/default/bin/pythonsample.png 的 PNG 格式 DFD 文件:./tm.py --dfd | dot -Tpng -o sample.pngexit“但我以为我们已经不再搞‘写代码’这回事了!”——这一部分是为那些太喜欢和 AI 助手聊天的人准备的:查看 https://github.com/izar/tm_skills 了解一些将 OWASP pytm 添加到你的编码助手的方法(你仍然需要有一个可用的 pytm 安装!)。
所有可用参数:
usage: tm.py [-h] [--debug] [--dfd] [--report REPORT] [--exclude EXCLUDE]
[--seq] [--list] [--colormap] [--describe DESCRIBE]
[--list-elements] [--json JSON] [--levels LEVELS [LEVELS ...]]
[--stale_days STALE_DAYS]
options:
-h, --help show this help message and exit
--debug print debug messages
--dfd output DFD
--report REPORT output report using the named template file (sample
template file is under docs/basic_template.md)
--exclude EXCLUDE specify threat IDs to be ignored
--seq output sequential diagram
--list list all available threats
--colormap color the risk in the diagram
--describe DESCRIBE describe the properties available for a given element
--list-elements list all elements which can be part of a threat model
--json JSON output a JSON file
--levels LEVELS [LEVELS ...]
Select levels to be drawn in the threat model (int
separated by comma).
--stale_days STALE_DAYS
checks if the delta between the TM script and the code
described by it is bigger than the specified value in
days
stale_days 参数试图确定模型脚本(即你正在编写的脚本)与实现所建模系统的代码之间相隔多少天。理想情况下,在积极开发的系统中,大多数情况下两者应该相当接近。你可以定期运行此检查,以衡量项目的脉搏以及威胁模型的“新鲜度”。
当前可用的元素包括:TM、Element、Server、ExternalEntity、Datastore、Actor、Process、Dataflow、Boundary、Lambda、LLM 和 Agent。
可以通过使用 --describe 后跟元素名称来列出元素的可用属性:
$ ./tm.py --describe Server
Server class attributes:
OS Operating system
default: ''
assumptions Assumptions about the element. These optionally allow to exclude threats with the given SIDs
default factory: list
controls Security controls for this element
default factory: Controls
data pytm.Data object(s) in incoming data flows
default factory: DataSet
description Description of the element
default: ''
findings Threats that apply to this element
default factory: list
handlesResources Does this asset handle resources?
default: False
inBoundary Trust boundary this element exists in
default: None
inScope Is the element in scope of the threat model
default: True
inputs incoming Dataflows
default factory: list
is_drawn default: False
levels List of levels (0, 1, 2, ...) to be drawn in the model
default factory: <lambda>
maxClassification Maximum data classification this element can handle
default: <Classification.UNKNOWN: 0>
minTLSVersion Minimum TLS version required
default: <TLSVersion.NONE: 0>
name Name of the element
required
onAWS Is this asset on AWS?
default: False
outputs outgoing Dataflows
default factory: list
overrides Overrides to findings, allowing to set a custom response, CVSS score or override other attributes
default factory: list
port Default TCP port for incoming data flows
default: -1
protocol Default network protocol for incoming data flows
default: ''
severity Severity level of threats affecting this element
default: 0
sourceFiles Location of the source code that describes this element relative to the directory of the model script
default factory: list
usesCache Does this server use cache?
default: False
usesEnvironmentVariables Does this asset use environment variables?
default: False
usesSessionTokens Does this server use session tokens?
default: False
usesVPN Does this server use VPN?
default: False
usesXMLParser Does this server use XML parser?
default: False
uuid default factory: <lambda>
colormap 参数与 dfd 一起使用时,会输出一个颜色编码的 DFD,其中元素根据其风险等级(由运行规则识别)被涂成红色、黄色或绿色。
devbox shellpytmexit以下是一个示例 tm.py 文件,描述了一个简单的应用程序:用户登录应用程序并在应用上发布评论。应用服务器将这些评论存储到数据库中。有一个 AWS Lambda 定期清理数据库。
#!/usr/bin/env python3
from pytm import TM, Server, Datastore, Dataflow, Boundary, Actor, Lambda, LLM, Data, Classification, DatastoreType
tm = TM("my test tm")
tm.description = "another test tm"
tm.isOrdered = True
User_Web = Boundary("User/Web")
Web_DB = Boundary("Web/DB")
user = Actor("User")
user.inBoundary = User_Web
web = Server("Web Server")
web.OS = "CloudOS"
web.controls.isHardened = True
web.sourceFiles = ["server/web.cc"]
db = Datastore("SQL Database (*)")
db.OS = "CentOS"
db.controls.isHardened = False
db.inBoundary = Web_DB
db.type = DatastoreType.SQL
db.inScope = False
db.sourceFiles = ["model/schema.sql"]