Skip to content
KitploitKITPLOIT
工具漏洞利用博客
Log in
提交
工具漏洞利用博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

订阅源联系隐私© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
pytm — 一个Pythonic的威胁建模框架 | Kitploit
工具/GitHubGitHub/owasp/pytm
漏洞分析代码分析DevSecOps学习与教育
GitHubowasp/pytm

pytm

一个Pythonic的威胁建模框架

查看仓库
1.2k2241514天前Kitploit 审核通过

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享

build+test OpenSSF Best Practices

pytm:一个面向威胁建模的 Pythonic 框架

pytm logo

简介

传统的威胁建模往往介入得太晚,有时甚至完全缺席。此外,手动创建数据流和报告可能极其耗时。pytm 的目标是将威胁建模左移,使威胁建模更加自动化并以开发者为中心。

特性

基于你的输入和架构设计的定义,pytm 可以自动生成以下内容:

  • 数据流图(DFD)
  • 时序图
  • 与你的系统相关的威胁

要求

  • Linux/MacOS
  • Python 3.11+
  • Graphviz 包
  • Java(OpenJDK 10 或 11)
  • plantuml.jar

快速开始

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

入门 - Devbox 变体

为了简化 pytm 的使用,可以使用 Devbox 将主机依赖完全隔离。这通常是比 OCI 容器方案开销更低、更方便的替代方案。

  • 在 Linux/MacOS 上安装 Devbox:curl -fsSL https://get.jetify.com/devbox | bash
  • 在 Windows/WSL 上安装 Devbox
  • 更新到最新版本的 devbox:devbox version update
  • 在 ~/.config/nix/nix.conf 文件中设置你的 GitHub 访问令牌:access-tokens = github.com=YOUR_TOKEN_HERE
  • 创建一个新的隔离 shell 环境,其中包含项目 devbox.json 文件中指定的所有工具和包:devbox shell
  • 使用 which python 命令显示当你在终端中直接输入 python 时将使用的 Python 可执行文件的完整路径。输出应为以下路径:.devbox/nix/profile/default/bin/python
  • 通过运行以下命令进行测试,该命令应生成一个名为 sample.png 的 PNG 格式 DFD 文件:./tm.py --dfd | dot -Tpng -o sample.png
  • 退出 Devbox shell 环境:exit

入门 - AI 变体

“但我以为我们已经不再搞‘写代码’这回事了!”——这一部分是为那些太喜欢和 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 变体

  • devbox shell
  • 照常使用 pytm
  • exit

创建威胁模型

以下是一个示例 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"]
下载工具