Pytm:一款Python风格的威胁建模框架

枫少@KillBoy
枫少@KillBoy
管理员
220
文章
0
粉丝
资源分享5610,4722字数 1453阅读4分50秒阅读模式
AI智能摘要
你是否还在为繁琐的威胁建模手动绘图、漏掉关键风险而头疼?Pytm来了——一款用Python代码定义系统的自动化威胁建模框架。只需编写简洁的Python脚本,它就能自动生成数据流图、序列图,并识别潜在安全威胁。支持自定义威胁库、灵活扩展元素属性,还能输出专业报告。安全从业者的新利器,让威胁建模从“费力耗时”变为“自动化流水线”。
— AI 生成的文章内容摘要

Pytm:一款Python风格的威胁建模框架-图片1

Pytm是一款Python风格的威胁建模框架,它可以帮助我们以Python语言风格的形式并使用pytm框架中的元素和属性来定义你的系统。根据我们的定义参数,pytm可以针对你的系统生成数据流图(DFD)、序列图以及威胁模型。

工具要求

linux/MacOS
Python 3.x
Graphviz package
Java (OpenJDK 10 or 11)
plantuml.jar

工具下载

广大研究人员可以使用下列命令将该项目源码克隆至本地:

git clone https://github.com/izar/pytm.git

工具使用

tm.py [-h] [--debug] [--dfd] [--report REPORT] [--exclude EXCLUDE] [--seq] [--list] [--describe DESCRIBE]
optional arguments:
-h, --help           show this help message and exit
--debug              print debug messages
--dfd                output DFD (default)
--report REPORT      output report using the named template file (sample template file is under docs/template.md)
--exclude EXCLUDE    specify threat IDs to be ignored
--seq                output sequential diagram
--list               list all available threats
--describe DESCRIBE  describe the properties available for a given element

当前该工具可用的元素包括:TM、服务器、外部实体、数据存储、Actor、进程、进程集、数据边界和Lambda。

除此之外,我们也可以使用命令“–describe”来查看每一个元素的可用属性:

(pytm) ➜  pytm git:(master) ✗ ./tm.py --describe Element
Element
OS
check
definesConnectionTimeout
description
dfd
handlesResources
implementsAuthenticationScheme
implementsNonce
inBoundary
inScope
isAdmin
isHardened
name
onAWS

如果你是安全从业人员的话,你也可以向“threatlib/threats.json”文件中添加新的威胁属性:

{
"SID":"INP01",
"target": ["Lambda","Process"],
"description": "Buffer Overflow via Environment Variables",
"details": "This attack pattern involves causing a buffer overflow through manipulation of environment variables. Once the attacker finds that they can modify an environment variable, they may try to overflow associated buffers. This attack leverages implicit trust often placed in environment variables.",
"Likelihood Of Attack": "High",
"severity": "High",
"condition": "target.usesEnvironmentVariables is True and target.sanitizesInput is False and target.checksInputBounds is False",
"prerequisites": "The application uses environment variables.An environment variable exposed to the user is vulnerable to a buffer overflow.The vulnerable environment variable uses untrusted data.Tainted data used in the environment variables is not properly validated. For instance boundary checking is not done before copying the input data to a buffer.",
"mitigations": "Do not expose environment variable to the user.Do not use untrusted data in your environment variables. Use a language or compiler that performs automatic bounds checking. There are tools such as Sharefuzz [R.10.3] which is an environment variable fuzzer for Unix that support loading a shared library. You can use Sharefuzz to determine if you are exposing an environment variable vulnerable to buffer overflow.",
"example": "Attack Example: Buffer Overflow in $HOME A buffer overflow in sccw allows local users to gain root access via the $HOME environmental variable. Attack Example: Buffer Overflow in TERM A buffer overflow in the rlogin program involves its consumption of the TERM environmental variable.",
"references": "https://capec.mitre.org/data/definitions/10.html, CVE-1999-0906, CVE-1999-0046, http://cwe.mitre.org/data/definitions/120.html, http://cwe.mitre.org/data/definitions/119.html, http://cwe.mitre.org/data/definitions/680.html"
}

注意事项

threats.json”文件中包含的字符串可以通过eval()函数来运行,它可以确保文件拥有正确的权限并确保代码能够正确执行。

下面的样本是tm.py文件,它描述了一个简单的应用程序,其中一名用户“User”登录进了应用程序,然后在App上发布了评论。App服务器将这些评论存储进了数据库,服务器中有一个AWS Lambda会定期清理数据库。

#!/usr/bin/env python3
from pytm.pytm import TM, Server, Datastore, Dataflow, Boundary, Actor, Lambda
tm = TM("my test tm")
tm.description = "another test tm"
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.isHardened = True
db = Datastore("SQL Database (*)")
db.OS = "CentOS"
db.isHardened = False
db.inBoundary = Web_DB
db.isSql = True
db.inScope = False
my_lambda = Lambda("cleanDBevery6hours")
my_lambda.hasAccessControl = True
my_lambda.inBoundary = Web_DB
my_lambda_to_db = Dataflow(my_lambda, db, "(λ)Periodically cleans DB")
my_lambda_to_db.protocol = "SQL"
my_lambda_to_db.dstPort = 3306
user_to_web = Dataflow(user, web, "User enters comments (*)")
user_to_web.protocol = "HTTP"
user_to_web.dstPort = 80
user_to_web.data = 'Comments in HTML or Markdown'
user_to_web.order = 1
web_to_user = Dataflow(web, user, "Comments saved (*)")
web_to_user.protocol = "HTTP"
web_to_user.data = 'Ack of saving or error message, in JSON'
web_to_user.order = 2
web_to_db = Dataflow(web, db, "Insert query with comments")
web_to_db.protocol = "MySQL"
web_to_db.dstPort = 3306
web_to_db.data = 'MySQL insert statement, all literals'
web_to_db.order = 3
db_to_web = Dataflow(db, web, "Comments contents")
db_to_web.protocol = "MySQL"
db_to_web.data = 'Results of insert op'
db_to_web.order = 4
tm.process()

图表将以Dot或PlantUML的形式输出。

如果在运行tm.py文件时使用了“--dfd”参数,那么它将会向stdout生成输出文件:

tm.py --dfd | dot -Tpng -o sample.png

生成的图表如下:

Pytm:一款Python风格的威胁建模框架-图片2

下列命令可以生成一份序列图:

tm.py --seq | java -Djava.awt.headless=true -jar plantuml.jar -tpng -pipe > seq.png

Pytm:一款Python风格的威胁建模框架-图片3

生成的图表和数据可以引入到模板文件中来创建最终的报告:

tm.py --report docs/template.md | pandoc -f markdown -t html > report.html

用于生成报告的模板格式如下:

# Threat Model Sample
***
## System Description
{tm.description}
## Dataflow Diagram
![Level 0 DFD](dfd.png)
## Dataflows
Name|From|To |Data|Protocol|Port
----|----|---|----|--------|----
{dataflows:repeat:{{item.name}}|{{item.source.name}}|{{item.sink.name}}|{{item.data}}|{{item.protocol}}|{{item.dstPort}}
}
## Findings
{findings:repeat:* {{item.description}} on element "{{item.target}}"
}

当前支持的威胁如下:

INP01 - Buffer Overflow via Environment Variables
INP02 - Overflow Buffers
INP03 - Server Side Include (SSI) Injection
CR01 - Session Sidejacking
INP04 - HTTP Request Splitting
CR02 - Cross Site Tracing
INP05 - Command Line Execution through SQL Injection
INP06 - SQL Injection through SOAP Parameter Tampering
SC01 - JSON Hijacking (aka JavaScript Hijacking)
LB01 - API Manipulation
AA01 - Authentication Abuse/ByPass
DS01 - Excavation
DE01 - Interception
DE02 - Double Encoding
API01 - Exploit Test APIs
AC01 - Privilege Abuse
INP07 - Buffer Manipulation
AC02 - Shared Data Manipulation
DO01 - Flooding
HA01 - Path Traversal
AC03 - Subverting Environment Variable Values
DO02 - Excessive Allocation
DS02 - Try All Common Switches
INP08 - Format String Injection
INP09 - LDAP Injection
INP10 - Parameter Injection
INP11 - Relative Path Traversal
INP12 - Client-side Injection-induced Buffer Overflow
AC04 - XML Schema Poisoning
DO03 - XML Ping of the Death
AC05 - Content Spoofing
INP13 - Command Delimiters
INP14 - Input Data Manipulation
DE03 - Sniffing Attacks
CR03 - Dictionary-based Password Attack
API02 - Exploit Script-Based APIs
HA02 - White Box Reverse Engineering
DS03 - Footprinting
AC06 - Using Malicious Files
HA03 - Web Application Fingerprinting
SC02 - XSS Targeting Non-Script Elements
AC07 - Exploiting Incorrectly Configured Access Control Security Levels
INP15 - IMAP/SMTP Command Injection
HA04 - Reverse Engineering
SC03 - Embedding Scripts within Scripts
INP16 - PHP Remote File Inclusion
AA02 - Principal Spoof
CR04 - Session Credential Falsification through Forging
DO04 - XML Entity Expansion
DS04 - XSS Targeting Error Pages
SC04 - XSS Using Alternate Syntax
CR05 - Encryption Brute Forcing
AC08 - Manipulate Registry Information
DS05 - Lifting Sensitive Data Embedded in Cache

项目地址

Pytm:【GitHub传送门

https://www.freebuf.com/sectool/226951.html

 
枫少@KillBoy
评论  56  访客  56
    • 米米糖
      米米糖 1

      这玩意儿能跑在M1芯片上吗?

        • 憨憨哒
          憨憨哒 0

          @ 米米糖 M1上装OpenJDK 11简直折磨,Rosetta转译勉强跑起来。

        • 胡歌
          胡歌 1

          感觉还行,就是环境配置有点麻烦。

            • 毒舌
              毒舌 0

              @ 胡歌 配环境配到头大,Graphviz又报错。

              • 未来城市设计师
                未来城市设计师 0

                @ 胡歌 环境配置确实头疼,Graphviz版本坑了我半小时。

              • 夜行列车
                夜行列车 0

                之前搞过类似的东西,折腾了好久才跑起来 😂

                • 秋千荡高高
                  秋千荡高高 1

                  –describe 参数真方便,查属性省事多了。

                    • 迷茫的指南针
                      迷茫的指南针 0

                      @ 秋千荡高高 –describe 真香,省得翻源码了。

                    • CyberForge
                      CyberForge 0

                      生成的DFD图能导出svg格式不?

                      • 幻想的翼
                        幻想的翼 0

                        Java依赖有点烦,能不能整成纯Python的?

                          • 雪域狼
                            雪域狼 1

                            @ 幻想的翼 感觉这玩意挺适合做安全审计的。

                          • 夜痕
                            夜痕 1

                            那个lambda清理数据库的例子挺实用。

                            • 兔子跳
                              兔子跳 0

                              threats.json里还能自定义威胁?学废了。

                                • 影渡虚空
                                  影渡虚空 0

                                  @ 兔子跳 –exclude参数是忽略特定威胁吧?

                                  • 小蛇灵
                                    小蛇灵 0

                                    @ 兔子跳 自定义威胁是能加,但eval执行真不怕被投毒?

                                  • 孤独树洞
                                    孤独树洞 0

                                    我去,JSON里用eval也太危险了吧…

                                      • 糖人陶
                                        糖人陶 0

                                        @ 孤独树洞 eval跑JSON?心都跳出来了😅

                                      • 二虎吧唧
                                        二虎吧唧 0

                                        要是能把PlantUML换成Mermaid就更好了 👍

                                          • 夜梦未央
                                            夜梦未央 0

                                            @ 二虎吧唧 Mermaid轻量多了,PlantUML还得装Java头疼。

                                          • QuantumFlux
                                            QuantumFlux 0

                                            M1上跑不了Java 11有点抓瞎。

                                            • 霜华鬓
                                              霜华鬓 1

                                              这框架能导出svg吗,png太糊了。

                                                • 写作小能手
                                                  写作小能手 1

                                                  @ 霜华鬓 环境变量那个威胁例子挺典型。

                                                • 墨鸦
                                                  墨鸦 0

                                                  lambda那个例子能不能换成gRPC?

                                                    • 虚空之瞳
                                                      虚空之瞳 0

                                                      @ 墨鸦 gRPC那块得自己改Dataflow定义吧,没现成例子。

                                                    • 石匠王庚
                                                      石匠王庚 0

                                                      threats.json自定义威胁还挺灵活。

                                                      • WokeBae
                                                        WokeBae 0

                                                        PlantUML要是换成Mermaid多好。

                                                        • 沉默的电子
                                                          沉默的电子 0

                                                          感觉还行,就是依赖太重了。

                                                          • 风月笺
                                                            风月笺 0

                                                            JSON里exec代码是真敢写啊…

                                                            • 飞鸟翔翔
                                                              飞鸟翔翔 0

                                                              新手问下,这个框架支持python哪个版本?

                                                              • 算法舞者
                                                                算法舞者 0

                                                                看起来挺适合做课程项目。

                                                                • FlexGod
                                                                  FlexGod 0

                                                                  搞了半天终于跑通demo了。

                                                                    • LitOrNah
                                                                      LitOrNah 0

                                                                      @ FlexGod 恭喜啊,我第一次跑demo直接卡在plantuml路径上了😂

                                                                    • 归心
                                                                      归心 0

                                                                      用起来感觉还行,就是文档有点少。

                                                                      • 樱坂葵
                                                                        樱坂葵 1

                                                                        不懂就问,啥叫数据边界?

                                                                        • 团团熊猫
                                                                          团团熊猫 1

                                                                          这工具能画时序图?

                                                                          • 紫眸阴差
                                                                            紫眸阴差 1

                                                                            这玩意儿画图依赖太多,光配环境就劝退。

                                                                            • 嘚啵的梨子
                                                                              嘚啵的梨子 1

                                                                              数据边界是不是指trust boundary啊?有点懵。

                                                                              • 小鸭子摇摇
                                                                                小鸭子摇摇 1

                                                                                M1用户表示Java 11装得想砸电脑 😭

                                                                                • Voidwhisper
                                                                                  Voidwhisper 1

                                                                                  威胁列表挺全的,覆盖了OWASP TOP10不少项。

                                                                                    • Tinker Tess
                                                                                      Tinker Tess 1

                                                                                      @ Voidwhisper OWASP那几项是挺关键的

                                                                                    • 淘气猴哥
                                                                                      淘气猴哥 0

                                                                                      导出svg有办法吗?png放大糊成马赛克。

                                                                                      • 星河传说
                                                                                        星河传说 0

                                                                                        之前踩过坑,Graphviz版本不对直接报错。

                                                                                        • 饕餮吞星
                                                                                          饕餮吞星 0

                                                                                          threats.json里condition写错一个字母debug半小时。

                                                                                          • 风过竹林
                                                                                            风过竹林 0

                                                                                            Lambda清理DB的例子能不能加个权限校验?

                                                                                            • 蚩尤戈
                                                                                              蚩尤戈 1

                                                                                              文档要是再详细点就好了,现在全靠猜。

                                                                                              • 宇宙星图
                                                                                                宇宙星图 0

                                                                                                这框架画图是挺好看,就是依赖太杂了。

                                                                                                • 轻功绝顶
                                                                                                  轻功绝顶 1

                                                                                                  threats.json里写condition太容易拼错了,搞崩两次了。

                                                                                                  • 引力浪人
                                                                                                    引力浪人 0

                                                                                                    数据边界应该就是trust boundary吧?求确认。

                                                                                                    • 旅梦人
                                                                                                      旅梦人 0

                                                                                                      Lambda例子加个权限校验会更贴近实际场景。

                                                                                                      • 星河引路者
                                                                                                        星河引路者 1

                                                                                                        导出只能png真的很不方便,缩放全是锯齿。

                                                                                                        • 晨曦物联
                                                                                                          晨曦物联 1

                                                                                                          用eval跑JSON谁敢在生产用啊,权限都不敢开。

                                                                                                          • 蜂腰桥客
                                                                                                            蜂腰桥客 1

                                                                                                            新手求助,python 3.8能用这个框架吗?

                                                                                                            • 火焰代码
                                                                                                              火焰代码 0

                                                                                                              PlantUML换成Mermaid确实更轻量,支持一波。

                                                                                                              • 金辉煌
                                                                                                                金辉煌 0

                                                                                                                之前搞过threat modeling,这工具算省事的了。

                                                                                                                • 夜行踪
                                                                                                                  夜行踪 0

                                                                                                                  DFD图能不能加个颜色区分不同层级啊?

                                                                                                                  • 墨梅香
                                                                                                                    墨梅香 1

                                                                                                                    threats.json里的威胁库还挺全。

                                                                                                                  匿名

                                                                                                                  发表评论

                                                                                                                  匿名网友

                                                                                                                  拖动滑块以完成验证