threagile

repository·master·Indexed 20 days ago

https://github.com/threagile/threagile

An open-source, agile threat modeling toolkit that allows users to model architectures and assets using YAML files. It automatically checks models against standard and custom risk rules to identify security threats. Threagile can be executed via a CLI, as a REST API server, or through a Docker container. It supports generating data-flow diagrams, risk reports in PDF and JSON formats, and Asciidoctor reports for custom PDF generation.

Tokens
21.6K
Snippets
42
Records
65
Agent score
70%

What's inside threagile

  1. Split large models using includes

    master

    To prevent a single YAML model file from becoming unmanageably large, you can split your model into multiple files using the includes keyword. This allows you to organize components like common definitions, data assets, technical assets, boundaries, and risk tracking into separate files.

    includes:
      - common.yaml
      - data-assets.yaml
      - technical-assets.yaml
      - boundaries.yaml
      - risk-tracking.yaml
  2. Configure the `risk:` section logic

    master

    The risk: section is the core of the script and contains four subsections:

    • id:: Defines the unique synthetic risk ID. Use {$risk.id} to reference the category ID and {parameter_name.field} to reference asset properties.
    • match:: A filter condition. The engine iterates over all technical assets; if the do: block returns true, a risk is generated for that asset.
    • data:: A template for the generated risk object. It defines fields like title, severity, and exploitation_likelihood using the provided parameter.
    • utils:: Reusable helper methods that can be called within match:, data:, or other utils: using the syntax method_name(arg1, arg2).
  3. Use built-in Threagile model macros

    master

    Threagile provides built-in macros that act as mini-programs to modify your model file. These macros can automate common tasks such as adding infrastructure components, cleaning up tags, or seeding data.

    Note: Macros currently have limited support and have not been tested when used in conjunction with includes.

    | Macros | Description |
    |-----------------------|------------------------|
    | `add-build-pipeline`  | Add Build Pipeline |
    | `add-vault`           | Add Vault |
    | `pretty-print`        | Pretty Print |
    | `remove-unused-tags`  | Remove Unused Tags |
    | `seed-risk-tracking`  | Seed Risk Tracking |
    | `seed-tags`           | Seed Tags |
  4. Understand built-in risk rules in Threagile

    master

    Threagile uses a set of built-in risk rules to analyze your threat model and identify potential security risks. These rules automatically scan your model for patterns that indicate vulnerabilities such as missing authentication, unencrypted communication, or injection risks.

    Commonly identified risks include:

    • Access & Boundary Risks: DoS-risky Access Across Trust-Boundary, Unguarded Access From Internet, Wrong Trust Boundary Content.
    • Injection & Web Risks: Search-Query Injection, SQL/NoSQL-Injection, Cross-Site Scripting (XSS), Cross-Site Request Forgery (CSRF), XML External Entity (XXE), LDAP-Injection, Path-Traversal, Server-Side Request Forgery (SSRF).
    • Infrastructure & Deployment Risks: Missing Build Infrastructure, Missing Cloud Hardening, Missing Network Segmentation, Push instead of Pull Deployment, Container Platform Escape.
    • Data & Secret Risks: Accidental Secret Leak, Missing Vault (Secret Storage), Unencrypted Communication, Unnecessary Data Transfer.
    • Identity & Auth Risks: Missing Authentication, Missing Identity Propagation, Missing Two-Factor Authentication (2FA), Missing Identity Store.

    You can also define your own logic by creating custom risk rules.

  5. Use the Edit feature in Server mode

    master

    In server mode, you can use the UI to edit your model and run analyses.

    Critical Warning: The model is stored entirely in the browser's memory. To persist your changes, you must export the model to your hard drive. If you refresh or close the browser without exporting, your changes will be lost.

    The UI is built based on the schema.json definition.

  6. Document risk analysis using risk_tracking

    master
    After running Threagile in analyze mode, the tool identifies various risks based on risk rule algorithms. Because not all identified risks require immediate mitigation (some may be 'accepted risks'), you should use the risk_tracking field in your YAML model. This field allows you to document your analysis and track the status of identified risks directly within the model.
  7. Use Explain and Defer for Audit Trails

    master

    To provide transparency in why a risk was generated or how severity was calculated, use explain and defer statements within your utils: methods.

    • explain: Records a fact at the exact point of execution.
    • defer: Ensures an explanation runs when the method exits. This is useful because defer blocks have access to the final values of variables assigned during the method's execution.

    Example:

    get_highest:
      parameters:
        - tech_asset
        - "type"
      do:
        - defer:
            - explain: "the highest {type} value of the technical asset is '{value}'"
        - assign:
            - value: "{tech_asset.{type}}"
        - explain: "{type} value of the technical asset is '{value}'"
        # ... loop logic ...
        - return: "{value}"
  8. Access model data via {$model.path}

    master

    In Threagile Risk Scripts, you can access the underlying threat model data using the {$model.path} syntax. This allows you to query properties of technical and data assets to define custom risk logic.

    Commonly used paths include:

    • $model.technical_assets: Accesses the map of all technical assets.
    • $model.data_assets: Accesses the map of all data assets.
    • $model.data_assets.{id}.confidentiality: Accesses the confidentiality rating of a specific data asset by its ID.
    • $model.data_assets.{id}.integrity: Accesses the integrity rating of a specific data asset by its ID.
    • $model.data_assets.{id}.availability: Accesses the availability rating of a specific data asset by its ID.
  9. Define Match Conditions for Risk Rules

    master

    The match: section determines which technical assets trigger a risk. The engine iterates over all technical assets and evaluates the logic provided in the do: block.

    Best Practices:

    • Respect Scoping: Always include - false: "{tech_asset.out_of_scope}" in your and conditions to ensure out-of-scope assets are not flagged.
    • Filter by Tag: Use the contains operator on {tech_asset.tags}.
    • Filter by Technology: Use the any operator on {tech_asset.technologies} to check for specific attributes (e.g., {.attributes.web-application}).
    • Logical Operators: Use and, or, and not to combine conditions.
    # Filter by tag
    match:
      parameter: tech_asset
      do:
        - if:
            and:
              - false: "{tech_asset.out_of_scope}"
              - contains:
                  item: my-tag
                  in: "{tech_asset.tags}"
            then:
              return: true
    
    # Filter by technology attribute
    match:
      parameter: tech_asset
      do:
        - if:
            and:
              - false: "{tech_asset.out_of_scope}"
              - any:
                  in: "{tech_asset.technologies}"
                  true: "{.attributes.web-application}"
            then:
              return: true
  10. Create Utility Methods in Risk Scripts

    master

    For complex logic, use the utils: section to define reusable methods. Methods are case-insensitive.

    Capabilities:

    • Parameters: Define inputs for the method.
    • Logic: Use assign, if, loop, return, and equal-or-greater.
    • Model Access: Access the global model using the {$model.path.to.data} syntax.
    • Iteration: Use loop to iterate over collections like tech_asset.data_assets_processed.
    • Comparison: When comparing enum values (like confidentiality or criticality), use the as keyword in comparison expressions to perform semantic comparison rather than plain string comparison.

    Example: Dynamic Impact Calculation

    utils:
      get_impact:
        parameters:
          - tech_asset
        do:
          - assign:
              - impact: low
              - highest_confidentiality: "get_highest({tech_asset}, confidentiality)"
          - if:
              or:
                - equal-or-greater:
                    as: confidentiality
                    first: "{highest_confidentiality}"
                    second: confidential
              then:
                - assign:
                    impact: medium
          - return: "{impact}"
  11. Define a Threagile model using YAML

    master

    A Threagile model is defined in a yaml file and must comply with the project's JSON schema. The model is built around two primary types of assets:

    1. technical_assets: The core components of your system. This is the starting point for any model.
    2. data_assets: Definitions of the data that is stored, processed, or transmitted by technical assets.

    To build a complete model, you must establish relationships between these assets using the following fields within your technical assets:

    • communication_links: Describes how technical assets connect to one another. Within these links, you should specify data_assets_sent to indicate what data is moving between assets.
    • data_assets_processed: Identifies which data assets a technical asset operates on.
    • data_assets_stored: Identifies which data assets a technical asset holds.

    Additionally, you can group assets using:

    • trust_boundaries: To define security zones.
    • shared_runtime: To group assets that share the same execution environment.
  12. Include other YAML files in your Threagile model

    master

    You can modularize your threat model by using the includes keyword in your main YAML model file. This allows you to pull in fields and definitions from external YAML files, which Threagile will then merge into your primary model. This is useful for managing common assets, boundaries, or risk tracking across multiple models.

    includes:
      - common.yaml
      - data-assets.yaml
      - technical-assets.yaml
      - boundaries.yaml
      - risk-tracking.yaml