Skip to content

Commit 44d48e0

Browse files
committed
chore: update examples to optionally use environment variables to read STIX bundles
1 parent 47e6374 commit 44d48e0

75 files changed

Lines changed: 389 additions & 73 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/.env.example

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# These environment variables can be helpful when running scripts in the examples directory
2+
#
3+
# In order to use them, copy this file to .env and modify the values as needed
4+
# Two optional tools worth considering to automatically use the .env file are:
5+
# 1. `python-dotenv` python library: https://github.com/theskumar/python-dotenv
6+
# 2. `direnv` tool: https://direnv.net
7+
# Setting up these tools is out of scope for this example.
8+
#
9+
# if you have mitreattack-python installed, you can use the following command to download all STIX bundles:
10+
#
11+
# download_attack_stix --all
12+
#
13+
# the default download directory from the above command is "attack-releases"
14+
15+
STIX_BASE_DIR=attack-releases/stix-2.0/v17.1
16+
STIX_BUNDLE=attack-releases/stix-2.0/v17.1/enterprise-attack.json

examples/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Examples Directory
2+
3+
This directory contains example scripts demonstrating how to use the [`mitreattack-python`](https://github.com/mitre-attack/mitreattack-python)
4+
library to extract, analyze, and report on MITRE ATT&CK data.
5+
These scripts cover a variety of use cases, including querying STIX bundles, generating reports, and automating ATT&CK data analysis.
6+
7+
## Full Example Listing & Documentation
8+
9+
A complete, categorized list of example scripts, usage details, and direct links is maintained in the built documentation:
10+
11+
- [mitreattack-python Examples Documentation](https://mitreattack-python.readthedocs.io/en/latest/mitre_attack_data/examples.html)
12+
13+
## Setup
14+
15+
Many example scripts allow optional configuration via environment variables for paths to STIX bundles.
16+
If you want to set this up you can follow these instructions.
17+
18+
- Copy the provided [`examples/.env.example`](examples/.env.example:1) file to `.env`:
19+
20+
```sh
21+
cp .env.example .env
22+
```
23+
24+
- Edit `.env` to set the correct paths and variables for your environment.
25+
26+
Creating a .env file is not enough however. You will need to use a tool such as the following to help manage the environment variables:
27+
28+
- [`python-dotenv`](https://pypi.org/project/python-dotenv/) (automatically loads `.env` in Python scripts)
29+
- [`direnv`](https://direnv.net/) (manages environment variables per directory)
30+
31+
Setting up these tools is out of scope for this README.
32+
33+
### Dependencies
34+
35+
- [`mitreattack-python`](https://github.com/mitre-attack/mitreattack-python)
36+
- Python 3.x
37+
- ATT&CK STIX bundles
38+
39+
### Downloading ATT&CK STIX Bundles
40+
41+
Many example scripts require ATT&CK STIX bundles, which must be downloaded and placed in the directory specified in your `.env` file (e.g., `attack-releases/stix-2.0/v17.1`).
42+
You can download these bundles using the provided CLI command if you have mitreattack-python installed:
43+
44+
```sh
45+
download_attack_stix --all
46+
```
47+
48+
This will download all available ATT&CK releases in STIX format to the default directory (`attack-releases`).
49+
You can customize the download location and versions using additional options. For example:
50+
51+
- Download the latest release (default):
52+
53+
```sh
54+
download_attack_stix
55+
```
56+
57+
- Download specific versions:
58+
59+
```sh
60+
download_attack_stix -v 16.1 -v 17.1
61+
```
62+
63+
- Download all releases in both STIX formats:
64+
65+
```sh
66+
download_attack_stix --all --stix21
67+
```
68+
69+
## How to Run Scripts
70+
71+
- Run individual scripts with Python:
72+
73+
```sh
74+
python get_all_techniques.py
75+
```
76+
77+
## Contribution & Customization
78+
79+
Feel free to adapt these scripts for your own use cases. Contributions and improvements are welcome!

examples/get_all_assets.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("ics-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "ics-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
assets = mitre_attack_data.get_assets(remove_revoked_deprecated=True)
811

examples/get_all_assets_targeted_by_all_techniques.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("ics-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "ics-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
# get all assets targeted by techniques
811
assets_targeted_by_techniques = mitre_attack_data.get_all_assets_targeted_by_all_techniques()

examples/get_all_campaigns.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
campaigns = mitre_attack_data.get_campaigns(remove_revoked_deprecated=True)
811

examples/get_all_campaigns_attributed_to_all_groups.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
# get all campaigns related to groups
811
campaigns_attributed = mitre_attack_data.get_all_campaigns_attributed_to_all_groups()

examples/get_all_campaigns_using_all_software.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
# get all campaigns related to software
811
campaigns_using_software = mitre_attack_data.get_all_campaigns_using_all_software()

examples/get_all_campaigns_using_all_techniques.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
# get all campaigns related to techniques
811
campaigns_using_techniques = mitre_attack_data.get_all_campaigns_using_all_techniques()

examples/get_all_datacomponents.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
9+
610

711
datacomponents = mitre_attack_data.get_datacomponents(remove_revoked_deprecated=True)
812

examples/get_all_datacomponents_detecting_all_techniques.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
from mitreattack.stix20 import MitreAttackData
24

35

46
def main():
5-
mitre_attack_data = MitreAttackData("enterprise-attack.json")
7+
stix_filepath = os.environ.get("STIX_BUNDLE", "enterprise-attack.json")
8+
mitre_attack_data = MitreAttackData(stix_filepath=stix_filepath)
69

710
# get all data components related to techniques
811
datacomponents_detecting = mitre_attack_data.get_all_datacomponents_detecting_all_techniques()

0 commit comments

Comments
 (0)