Skip to content

Commit

Permalink
update assetsToDf
Browse files Browse the repository at this point in the history
  • Loading branch information
clemiller committed Sep 29, 2023
1 parent cf645e8 commit 6348631
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions mitreattack/attackToExcel/stixToDf.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,48 +530,53 @@ def assetsToDf(src):
"""
assets = src.query([Filter("type", "=", "x-mitre-asset")])
assets = remove_revoked_deprecated(assets)
asset_rows = []
for asset in tqdm(assets, desc="parsing assets"):
row = parseBaseStix(asset)
# add asset-specific fields
if "x_mitre_platforms" in asset:
row["platforms"] = ", ".join(sorted(asset["x_mitre_platforms"]))
if "x_mitre_sectors" in asset:
row["sectors"] = ", ".join(sorted(asset["x_mitre_sectors"]))
if "x_mitre_related_assets" in asset:
related_assets = []
related_assets_sectors = []
related_assets_descriptions = []

for related_asset in asset["x_mitre_related_assets"]:
related_assets.append(related_asset["name"])
related_assets_sectors.append(", ".join(related_asset["related_asset_sectors"]))
related_assets_descriptions.append(related_asset["description"])

row["related assets"] = "; ".join(related_assets)
row["related assets sectors"] = "; ".join(related_assets_sectors)
row["related assets description"] = "; ".join(related_assets_descriptions)

asset_rows.append(row)

citations = get_citations(assets)
dataframes = {
"assets": pd.DataFrame(asset_rows).sort_values("name"),
}
# add relationships
codex = relationshipsToDf(src, relatedType="asset")
dataframes.update(codex)
# add relationship references
dataframes["assets"]["relationship citations"] = _get_relationship_citations(dataframes["assets"], codex)
# add/merge citations
if not citations.empty:
# append to existing citations from references
if "citations" in dataframes:
dataframes["citations"] = pd.concat([dataframes["citations"], citations])
else: # add citations
dataframes["citations"] = citations

dataframes["citations"].sort_values("reference")
dataframes = {}
if assets:
asset_rows = []
for asset in tqdm(assets, desc="parsing assets"):
row = parseBaseStix(asset)
# add asset-specific fields
if "x_mitre_platforms" in asset:
row["platforms"] = ", ".join(sorted(asset["x_mitre_platforms"]))
if "x_mitre_sectors" in asset:
row["sectors"] = ", ".join(sorted(asset["x_mitre_sectors"]))
if "x_mitre_related_assets" in asset:
related_assets = []
related_assets_sectors = []
related_assets_descriptions = []

for related_asset in asset["x_mitre_related_assets"]:
related_assets.append(related_asset["name"])
related_assets_sectors.append(", ".join(related_asset["related_asset_sectors"]))
related_assets_descriptions.append(related_asset["description"])

row["related assets"] = "; ".join(related_assets)
row["related assets sectors"] = "; ".join(related_assets_sectors)
row["related assets description"] = "; ".join(related_assets_descriptions)

asset_rows.append(row)

citations = get_citations(assets)
dataframes = {
"assets": pd.DataFrame(asset_rows).sort_values("name"),
}
# add relationships
codex = relationshipsToDf(src, relatedType="asset")
dataframes.update(codex)
# add relationship references
dataframes["assets"]["relationship citations"] = _get_relationship_citations(dataframes["assets"], codex)
# add/merge citations
if not citations.empty:
# append to existing citations from references
if "citations" in dataframes:
dataframes["citations"] = pd.concat([dataframes["citations"], citations])
else: # add citations
dataframes["citations"] = citations

dataframes["citations"].sort_values("reference")
else:
logger.warning("No assets found - nothing to parse")

return dataframes

Expand Down

0 comments on commit 6348631

Please sign in to comment.