Pytm 标志

Pytm

组织
AgentSecOps
pytm

使用 pytm 库以 Python 进行威胁建模,支持程序化 STRIDE 分析、数据流图生成和自动化安全威胁识别。适用于:(1)使用 Python 代码以编程方式创建威胁模型;(2)生成数据流图(DFD),并自动识别 STRIDE 威胁;(3)将威胁建模集成到 CI/CD 流水线和左移安全实践中;(4)分析系统架构在信任边界上的安全威胁;(5)生成包含 STRIDE 类别和缓解建议的威胁报告;(6)将威胁模型作为代码维护,以便进行版本控制和自动化处理。

Add to TypingMind

概览

发布者AgentSecOps
代码仓库SecOpsAgentKit
技能名称pytm
星标数
225
复刻数
44
附带文件
仅含指令
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 AgentSecOps 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Pytm AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/AgentSecOps/SecOpsAgentKit/tree/main/skills/threatmodel/pytm
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/AgentSecOps/SecOpsAgentKit.git /tmp/SecOpsAgentKit
mkdir -p .claude/skills
cp -r /tmp/SecOpsAgentKit/skills/threatmodel/pytm .claude/skills/pytm
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Pytm,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Pytm。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Pytm 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Threat Modeling with pytm

Overview

pytm is a Python library for programmatic threat modeling based on the STRIDE methodology. It enables security engineers to define system architecture as code, automatically generate data flow diagrams (DFDs), identify security threats across trust boundaries, and produce comprehensive threat reports. This approach integrates threat modeling into CI/CD pipelines, enabling shift-left security and continuous threat analysis.

Quick Start

Create a basic threat model:

python
#!/usr/bin/env python3
from pytm import TM, Server, Dataflow, Boundary, Actor

# Initialize threat model
tm = TM("Web Application Threat Model")
tm.description = "E-commerce web application"

# Define trust boundaries
internet = Boundary("Internet")
dmz = Boundary("DMZ")
internal = Boundary("Internal Network")

# Define actors and components
user = Actor("Customer")
user.inBoundary = internet

web = Server("Web Server")
web.inBoundary = dmz

db = Server("Database")
db.inBoundary = internal

# Define data flows
user_to_web = Dataflow(user, web, "HTTPS Request")
user_to_web.protocol = "HTTPS"
user_to_web.data = "credentials, payment info"
user_to_web.isEncrypted = True

web_to_db = Dataflow(web, db, "Database Query")
web_to_db.protocol = "SQL/TLS"
web_to_db.data = "user data, transactions"

# Generate threat report and diagram
tm.process()

Install pytm:

bash
pip install pytm
# Also requires graphviz for diagram generation
brew install graphviz  # macOS
# or: apt-get install graphviz  # Linux

Core Workflows

Workflow 1: Create New Threat Model

Progress: [ ] 1. Define system scope and trust boundaries [ ] 2. Identify all actors (users, administrators, external systems) [ ] 3. Map system components (servers, databases, APIs, services) [ ] 4. Define data flows between components with security attributes [ ] 5. Run tm.process() to generate threats and DFD [ ] 6. Review STRIDE threats and add mitigations [ ] 7. Generate threat report with scripts/generate_report.py

Work through each step systematically. Check off completed items.

Workflow 2: STRIDE Threat Analysis

pytm automatically identifies threats based on STRIDE categories:

  • Spoofing: Identity impersonation attacks
  • Tampering: Unauthorized modification of data
  • Repudiation: Denial of actions without traceability
  • Information Disclosure: Unauthorized access to sensitive data
  • Denial of Service: Availability attacks
  • Elevation of Privilege: Unauthorized access escalation

For each identified threat:

  1. Review threat description and affected component
  2. Assess likelihood and impact (use references/risk_matrix.md)
  3. Determine if existing controls mitigate the threat
  4. Add mitigation using threat.mitigation = "description"
  5. Document residual risk and acceptance criteria

Workflow 3: Architecture as Code

Define system architecture programmatically:

python
from pytm import TM, Server, Datastore, Dataflow, Boundary, Actor, Lambda

tm = TM("Microservices Architecture")

# Cloud boundaries
internet = Boundary("Internet")
cloud_vpc = Boundary("Cloud VPC")

# API Gateway
api_gateway = Server("API Gateway")
api_gateway.inBoundary = cloud_vpc
api_gateway.implementsAuthentication = True
api_gateway.implementsAuthorization = True

# Microservices
auth_service = Lambda("Auth Service")
auth_service.inBoundary = cloud_vpc

order_service = Lambda("Order Service")
order_service.inBoundary = cloud_vpc

# Data stores
user_db = Datastore("User Database")
user_db.inBoundary = cloud_vpc
user_db.isEncryptedAtRest = True

# Data flows with security properties
client_to_api = Dataflow(Actor("Client"), api_gateway, "API Request")
client_to_api.protocol = "HTTPS"
client_to_api.isEncrypted = True
client_to_api.data = "user credentials, orders"

api_to_auth = Dataflow(api_gateway, auth_service, "Auth Check")
api_to_auth.protocol = "gRPC/TLS"

auth_to_db = Dataflow(auth_service, user_db, "User Lookup")
auth_to_db.protocol = "TLS"

tm.process()

Workflow 4: CI/CD Integration

Automate threat modeling in continuous integration:

yaml
# .github/workflows/threat-model.yml
name: Threat Model Analysis
on: [push, pull_request]

jobs:
  threat-model:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          pip install pytm
          sudo apt-get install -y graphviz

      - name: Generate threat model
        run: python threat_model.py

      - name: Upload DFD diagram
        uses: actions/upload-artifact@v3
        with:
          name: threat-model-dfd
          path: '*.png'

      - name: Check for unmitigated threats
        run: python scripts/check_mitigations.py threat_model.py

Workflow 5: Threat Report Generation

Generate comprehensive threat documentation:

bash
# Run threat model with report generation
python threat_model.py

# Generate markdown report
./scripts/generate_report.py --model threat_model.py --output threat_report.md

# Generate JSON for tool integration
./scripts/generate_report.py --model threat_model.py --format json --output threats.json

Report includes:

  • System architecture overview
  • Trust boundary analysis
  • Complete STRIDE threat enumeration
  • Existing and recommended mitigations
  • Risk prioritization matrix

Security Considerations

Sensitive Data Handling

  • Threat models as code: Store in version control but review for sensitive architecture details
  • Credentials and secrets: Never hardcode in threat models - use placeholders
  • Data classification: Clearly label data flows with sensitivity levels (PII, PCI, PHI)
  • Report distribution: Control access to threat reports revealing security architecture

Access Control

  • Threat model repository: Restrict write access to security team and architects
  • CI/CD integration: Protect threat modeling pipeline from tampering
  • Diagram artifacts: Control distribution of DFDs showing system architecture
  • Mitigation tracking: Integrate with secure issue tracking systems

Audit Logging

Log the following for security governance:

  • Threat model creation and modification history
  • Identified threats and severity assessments
  • Mitigation implementation and validation
  • Risk acceptance decisions with approval
  • Threat model review and update cycles

Compliance Requirements

  • NIST 800-30: Risk assessment methodology alignment
  • ISO 27001: A.14.1.2 - Securing application services on public networks
  • OWASP SAMM: Threat Assessment practice maturity
  • PCI-DSS 6.3.1: Security threat identification in development
  • SOC2 CC9.1: Risk assessment process for system changes

Bundled Resources

Scripts (scripts/)

  • generate_report.py - Generate markdown/JSON threat reports with STRIDE categorization
  • check_mitigations.py - Validate all identified threats have documented mitigations
  • threat_classifier.py - Classify threats by severity using DREAD or custom risk matrix
  • template_generator.py - Generate threat model templates for common architectures

References (references/)

  • stride_methodology.md - Complete STRIDE methodology guide with threat examples
  • risk_matrix.md - Risk assessment framework with likelihood and impact scoring
  • component_library.md - Reusable pytm components for common patterns (APIs, databases, cloud services)
  • mitigation_strategies.md - Common mitigation patterns mapped to STRIDE categories and OWASP controls

Assets (assets/)

  • templates/web_application.py - Web application threat model template
  • templates/microservices.py - Microservices architecture template
  • templates/mobile_app.py - Mobile application threat model template
  • templates/iot_system.py - IoT system threat model template
  • dfd_styles.json - Custom graphviz styling for professional diagrams

Common Patterns

Pattern 1: Web Application Three-Tier Architecture

python
from pytm import TM, Server, Datastore, Dataflow, Boundary, Actor

tm = TM("Three-Tier Web Application")

# Boundaries
internet = Boundary("Internet")
dmz = Boundary("DMZ")
internal = Boundary("Internal Network")

# Components
user = Actor("End User")
user.inBoundary = internet

lb = Server("Load Balancer")
lb.inBoundary = dmz
lb.implementsNonce = True

web = Server("Web Server")
web.inBoundary = dmz
web.implementsAuthentication = True
web.implementsAuthenticationOut = False

app = Server("Application Server")
app.inBoundary = internal
app.implementsAuthorization = True

db = Datastore("Database")
db.inBoundary = internal
db.isSQL = True
db.isEncryptedAtRest = True

# Data flows
Dataflow(user, lb, "HTTPS").isEncrypted = True
Dataflow(lb, web, "HTTPS").isEncrypted = True
Dataflow(web, app, "HTTP").data = "session token, requests"
Dataflow(app, db, "SQL/TLS").data = "user data, transactions"

tm.process()

Pattern 2: Cloud Native Microservices

python
from pytm import TM, Lambda, Datastore, Dataflow, Boundary, Actor

tm = TM("Cloud Microservices")

cloud = Boundary("Cloud Provider VPC")
user = Actor("Mobile App")

# Serverless functions
api_gateway = Lambda("API Gateway")
api_gateway.inBoundary = cloud
api_gateway.implementsAPI = True

auth_fn = Lambda("Auth Function")
auth_fn.inBoundary = cloud

# Managed services
cache = Datastore("Redis Cache")
cache.inBoundary = cloud
cache.isEncrypted = True

db = Datastore("DynamoDB")
db.inBoundary = cloud
db.isEncryptedAtRest = True

# Data flows
Dataflow(user, api_gateway, "API Call").protocol = "HTTPS"
Dataflow(api_gateway, auth_fn, "Auth").protocol = "internal"
Dataflow(auth_fn, cache, "Session").isEncrypted = True
Dataflow(api_gateway, db, "Query").isEncrypted = True

tm.process()

Pattern 3: Adding Custom Threats

Define organization-specific threats:

python
from pytm import TM, Threat

tm = TM("Custom Threat Model")

# Add custom threat to component
web_server = Server("Web Server")

custom_threat = Threat(
    target=web_server,
    id="CUSTOM-001",
    description="API rate limiting bypass using distributed requests",
    condition="web_server.implementsRateLimiting is False",
    mitigation="Implement distributed rate limiting with Redis",
    references="OWASP API Security Top 10 - API4 Unrestricted Resource Consumption"
)

web_server.threats.append(custom_threat)

Pattern 4: Trust Boundary Analysis

Focus on cross-boundary threats:

python
# Identify all trust boundary crossings
for flow in tm.dataflows:
    if flow.source.inBoundary != flow.sink.inBoundary:
        print(f"Cross-boundary flow: {flow.name}")
        print(f"  From: {flow.source.inBoundary.name}")
        print(f"  To: {flow.sink.inBoundary.name}")
        print(f"  Encrypted: {flow.isEncrypted}")
        print(f"  Authentication: {flow.implementsAuthentication}")

Trust boundary crossings require extra scrutiny:

  • Authentication and authorization mechanisms
  • Encryption in transit
  • Input validation and sanitization
  • Logging and monitoring

Integration Points

SDLC Integration

  • Design Phase: Create initial threat model during architecture review
  • Development: Reference threat model for security requirements
  • Code Review: Validate mitigations are implemented correctly
  • Testing: Generate security test cases from identified threats
  • Deployment: Validate security controls match threat model assumptions
  • Operations: Update threat model for infrastructure changes

Security Tools Ecosystem

  • Issue Tracking: Export threats as Jira/GitHub issues for mitigation tracking
  • Documentation: Generate threat models for security documentation
  • SIEM: Map threats to detection rules and monitoring alerts
  • Pentesting: Provide threat model to pentesters for targeted assessment
  • Code Analysis: Link SAST/DAST findings to threat model threats

Cloud and DevOps

  • Infrastructure as Code: Threat model Terraform/CloudFormation templates
  • Container Security: Model container orchestration and service mesh
  • API Design: Threat model API gateway and microservices communication
  • Secrets Management: Model key management and secrets distribution

Troubleshooting

Issue: Missing Threats in Output

Symptoms: Expected STRIDE threats not appearing in generated report

Solution:

  1. Verify component properties are set correctly (e.g., isSQL=True for databases)
  2. Check data flow security attributes (isEncrypted, protocol)
  3. Ensure components are assigned to boundaries
  4. Review trust boundary crossings
  5. Consult references/stride_methodology.md for threat conditions

Issue: Diagram Generation Failure

Symptoms: DFD not generated or graphviz errors

Solution:

bash
# Verify graphviz installation
dot -V

# Install graphviz if missing
brew install graphviz  # macOS
sudo apt-get install graphviz  # Linux

# Test pytm with simple model
python -c "from pytm import TM; tm = TM('test'); tm.process()"

Issue: Too Many False Positive Threats

Symptoms: Identified threats don't apply to your architecture

Solution:

  1. Set accurate component properties to suppress irrelevant threats
  2. Use threat conditions to filter: threat.condition = "..."
  3. Document why threats don't apply: threat.mitigation = "N/A - using managed service"
  4. Create custom threat library with references/component_library.md

Issue: Threat Model Maintenance Drift

Symptoms: Threat model doesn't reflect current architecture

Solution:

  1. Store threat models in version control alongside architecture diagrams
  2. Trigger threat model regeneration in CI on architecture changes
  3. Schedule quarterly threat model reviews
  4. Link threat model updates to architecture review board approvals
  5. Use scripts/check_mitigations.py to validate completeness

Advanced Configuration

Custom Threat Definitions

Create organization-specific threat library:

python
# custom_threats.py
from pytm import Threat

def add_custom_threats(tm):
    """Add organization-specific threats to threat model"""

    # Cloud-specific threats
    cloud_misconfiguration = Threat(
        id="CLOUD-001",
        description="Misconfigured cloud storage bucket exposes sensitive data",
        condition="datastore.inBoundary.name == 'Cloud' and not datastore.isEncrypted",
        mitigation="Enable encryption at rest and bucket policies"
    )

    # API-specific threats
    api_abuse = Threat(
        id="API-001",
        description="API endpoint abuse through lack of rate limiting",
        condition="server.implementsAPI and not server.implementsRateLimiting",
        mitigation="Implement rate limiting and API key rotation"
    )

    return [cloud_misconfiguration, api_abuse]

Risk Scoring Integration

Add DREAD scoring to threats:

python
class ScoredThreat(Threat):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.damage = 0        # 0-10
        self.reproducibility = 0
        self.exploitability = 0
        self.affected_users = 0
        self.discoverability = 0

    def dread_score(self):
        return (self.damage + self.reproducibility + self.exploitability +
                self.affected_users + self.discoverability) / 5

# Usage
threat = ScoredThreat(
    target=component,
    description="SQL Injection",
    damage=9,
    reproducibility=8,
    exploitability=7,
    affected_users=10,
    discoverability=6
)
print(f"DREAD Score: {threat.dread_score()}/10")

Diagram Customization

Customize DFD output with graphviz attributes:

python
# Set custom colors for trust boundaries
internet.color = "red"
dmz.color = "orange"
internal.color = "green"

# Customize diagram output
tm.graph_options = {
    "rankdir": "LR",  # Left to right layout
    "bgcolor": "white",
    "fontname": "Arial",
    "fontsize": "12"
}

References

常见问题

Pytm AI 技能有什么作用?

使用 pytm 库以 Python 进行威胁建模,支持程序化 STRIDE 分析、数据流图生成和自动化安全威胁识别。适用于:(1)使用 Python 代码以编程方式创建威胁模型;(2)生成数据流图(DFD),并自动识别 STRIDE 威胁;(3)将威胁建模集成到 CI/CD 流水线和左移安全实践中;(4)分析系统架构在信任边界上的安全威胁;(5)生成包含 STRIDE 类别和缓解建议的威胁报告;(6)将威胁模型作为代码维护,以便进行版本控制和自动化处理。

为什么在 TypingMind 中使用 Pytm?

因为您只需安装一次,就能搭配任何模型使用。Pytm 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Pytm?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/AgentSecOps/SecOpsAgentKit/tree/main/skills/threatmodel/pytm。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Pytm?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Pytm 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Pytm 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Pytm AI 技能免费吗?

AgentSecOps 已在 GitHub 上发布了该技能。请查看代码仓库了解许可条款。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇