mitreattack-python

repository·main·Indexed 20 days ago

https://github.com/mitre-attack/mitreattack-python

A Python library of tools and utilities for working with MITRE ATT&CK data, primarily handling STIX 2.0 content. It includes the MitreAttackData library, tools for converting STIX data to Excel spreadsheets or Pandas DataFrames via the attackToExcel module, and utilities for managing ATT&CK collections. Additionally, it provides the navlayers core for programmatically creating and configuring MITRE ATT&CK Navigator layers, including management of techniques, gradients, legends, and layout settings.

Tokens
28.4K
Snippets
72
Records
101
Agent score
71%

What's inside mitreattack-python

  1. Overview of ATT&CK Collection scripts

    main

    The mitreattack.collections module provides tools for managing ATT&CK collections, which are sets of STIX objects grouped for convenience.

    Available scripts:

    • index_to_markdown.py: Converts a collection index to Markdown.
    • collection_to_index.py: Summarizes collections into an index file.
    • stix_to_collection.py: Converts raw STIX bundles into bundles containing a collection object.
  2. Overview of navlayers modules

    main

    The navlayers package provides modules and scripts for working with ATT&CK Navigator layers. These layers are annotations overlaid on the ATT&CK Matrix. The package supports the ATT&CK Navigator Layer file format version 4.3 and can automatically upgrade legacy version 3.0 and 4.X layers to version 4.3.

    Key functional areas include:

    • Core Modules: Interfaces for loading, validating, manipulating, and saving layers (e.g., layer, technique, metadata).
    • Manipulator Scripts: Tools like layerops to combine multiple layers.
    • Exporter Scripts: Tools to export layers to Excel (to_excel) or SVG (to_svg) formats.
    • Generator Scripts: Tools to generate summary layers based on ATT&CK objects (overview_generator, usage_generator, sum_generator).
    • Utility Modules: Helpers for matrix generation and template management.
    • Command Line Tools: CLI utilities for exporting (layerExporter_cli.py) and generating (layerGenerator_cli.py) layers.
  3. Overview of the mitreattack-python library

    main

    The mitreattack-python library provides a collection of Python tools and utilities specifically designed for working with MITRE ATT&CK content.

    The library is organized into several key areas:

    1. MitreAttackData Library: The core component of the package, containing primary data handling utilities.
    2. Additional Modules: Specialized tools including:
      • navlayers: Navigation layer utilities.
      • attackToExcel: Tools for exporting ATT&CK data to Excel formats.
      • collections: Utilities for managing collections of ATT&CK objects.
      • diffStix: Tools for performing diffs on STIX data.
  4. Use the navlayers module for ATT&CK Navigator layers

    main

    The navlayers module provides a collection of utilities for working with ATT&CK Navigator layers. It allows you to import, export, and manipulate layers programmatically.

    Key capabilities include:

    • Importing: Read layers from the filesystem or directly from Python dictionaries.
    • Manipulation: Combine multiple layers and edit their contents.
    • Exporting: Export processed layers to Excel files or SVG images.

    For detailed implementation details and specific API references, refer to the source documentation in the mitreattack/navlayers directory.

  5. Use the collections utilities for ATT&CK data

    main

    The collections module provides a set of utilities designed for working with ATT&CK Collections and Collection Indexes.

    Key capabilities include:

    • Converting and summarizing data within collections and collection indexes.
    • Generating a collection object from a raw STIX bundle input.

    For detailed implementation details and specific API references, refer to the mitreattack.collections package documentation.

  6. Understanding the Layer object structure in Layers Core

    main

    The Layers Core subcomponent manages Layer objects. A Layer acts as the main handle and container for a _LayerObj instance. The _LayerObj is the raw layer object that contains several sub-objects representing the different components of an ATT&CK Navigator layer.

    Note: This implementation assumes familiarity with the ATT&CK Navigator layer format.

    demo (Layer instance) <------------------------------------------------> The container for a layer object
      |---> demo.layer (_LayerObj instance)--------------------------------> The raw layer object itself
              |---> demo.layer.version (Versions instance)-----------------> A versions object
              |---> demo.layer.filters (Filter instance)-------------------> A filter object
              |---> demo.layer.layout (Layout instance)--------------------> A layout object
              |---> demo.layer.techniques (List of Technique instances)----> A collection of technique objects
              |---> demo.layer.gradient (Gradient instance)----------------> A gradient object
              |---> demo.layer.legendItems (List of LegendItem instances)----> A collection of legend item objects
              |---> demo.layer.metadata (List of Metadata instances)-------> A collection of metadata objects
  7. Use the MitreAttackData library to work with ATT&CK STIX data

    main
    The MitreAttackData library is the primary component of this package. It is designed to read and manipulate MITRE ATT&CK STIX 2.0 content. It allows you to query the dataset for specific objects and retrieve their related objects.
  8. How the StixObjectFactory determines return types

    main

    The MitreAttackData methods use the StixObjectFactory to determine the return type of retrieved objects. Depending on the object type, the factory will either:

    1. Convert STIX 2.0 content into a stix2 Custom Object (for ATT&CK-specific types like Tactics or Matrices).
    2. Return a standard STIX 2.0 Domain Object (for standard types like Groups or Intrusion Sets).

    This allows you to interact with both standard and custom MITRE ATT&CK objects using a consistent API, accessing attributes directly from the returned object.

  9. Combine multiple layers using LayerOps

    main

    The LayerOps class allows you to automate the combination of multiple ATT&CK Navigator layers using user-defined lambda functions. This is useful for merging scores, comments, or metadata across different layers.

    Workflow

    1. Initialize: Create a LayerOps instance by passing lambda functions for the fields you want to combine (e.g., score, comment, name, colors, metadata, desc).
    2. Process: Call the .process() method, passing in a list or a dictionary of Layer objects. The method applies your lambdas to the provided data to produce a new, combined Layer object.

    Initialization Parameters

    • score, comment, enabled, colors, metadata, name, desc: Each accepts a lambda function that defines how to combine the values from the input layers.
    • default_values (optional): A dictionary of default values to use if a technique is missing a field in the combined layers.

    The .process() Method

    x.process(data, default_values=None)

    • data: Must be a list of Layer objects or a dict of {key: Layer} pairs.
    • default_values (optional): Overrides the default values provided during initialization for this specific operation.
    from mitreattack.navlayers.manipulators.layerops import LayerOps
    from mitreattack.navlayers.core.layer import Layer
    
    # Setup layers
    demo = Layer()
    demo.from_file("layer1.json")
    demo2 = Layer()
    demo2.from_file("layer2.json")
    
    # Example 1: Average scores across a list of layers
    lo = LayerOps(score=lambda x: sum(x) / len(x),
                  name=lambda x: x[1],
                  desc=lambda x: "This is a list example")
    
    out_layer = lo.process([demo, demo2])
    out_layer.to_file("averaged_layer.json")
    
    # Example 2: Combine scores using a dictionary of layers
    lo2 = LayerOps(score=lambda x: sum([x[y] for y in x]) / len([x[y] for y in x]),
                   colors=lambda x: x['b'],
                   desc=lambda x: "This is a dict example")
    
    out_layer3 = lo2.process({'a': demo, 'b': demo2})
  10. Understand the ATT&CK Excel spreadsheet format

    main

    The generated Excel files follow a specific structure:

    • Master Spreadsheets: Contain all object types in one place.
    • Individual Type Spreadsheets: Contain one spreadsheet per object type (e.g., Techniques, Software).
    • Relationship Handling: In individual type spreadsheets, relationships (like procedure examples) are broken out into separate sheets by relationship type. In the master spreadsheet, all relationship types are combined into a single sheet.
    • Citations: A dedicated citations sheet is provided to look up in-text citations.
    • Matrices: For domains with multiple matrices (e.g., Mobile ATT&CK), each matrix is assigned its own named sheet.
    • Filtering: Objects that have been revoked or deprecated in STIX are not included in the Excel spreadsheets.
  11. Retrieve individual ATT&CK objects

    main

    The MitreAttackData library provides methods to fetch specific ATT&CK objects using various identifiers such as STIX IDs, ATT&CK IDs, names, or aliases.

    Common retrieval patterns include:

    • By STIX ID
    • By ATT&CK ID
    • By Name
    • By Alias (for Groups, Software, or Campaigns)
    • By STIX Type
    # Refer to the following example scripts for specific implementations:
    # get_object_by_stix_id.py
    # get_object_by_attack_id.py
    # get_objects_by_name.py
    # get_groups_by_alias.py
    # get_software_by_alias.py
    # get_campaigns_by_alias.py