Skip to content

Instantly share code, notes, and snippets.

@KiloNiner
Created May 2, 2024 15:25
Show Gist options
  • Save KiloNiner/0e77c29bffe1d9ed4839237142c74171 to your computer and use it in GitHub Desktop.
Save KiloNiner/0e77c29bffe1d9ed4839237142c74171 to your computer and use it in GitHub Desktop.
Convert BloodHound graph files into CSV files
function Convert-BHGraph
{
<#
.SYNOPSIS
Convert BloodHound graph files into CSV files.
.DESCRIPTION
The PowerShell script Convert-BHGraph is designed to convert JSON files, specifically formatted as BloodHound graph exports, into CSV files. It accepts optional parameters for the source JSON file path and the destination CSV file path, defaulting to .\bh-graph.json and .\bh-graph.csv respectively. The script reads the JSON file, extracts nodes and edges data, and constructs a hashtable for nodes to facilitate quick lookups. It then processes the edges to create custom objects representing relationships, which include labels and kinds for both source and target nodes, as well as the type of edge. Finally, these relationship objects are exported to a CSV file at the specified destination path, providing a structured representation of the graph's relationships.
.EXAMPLE
Convert-BHGraph
Converts the file bh-graph.json from the current directory into a CSV file named bh-graph.csv.
.EXAMPLE
Convert-BHGraph -Path my-bloodhound-graph.json -Destination my-bloodhound-relationships.csv
Converts the file my-bloodhound-graph.json into a CSV file named my-bloodhound-relationships.csv.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $false, Position = 0)]
[String]
$Path = '.\bh-graph.json',
[Parameter(Mandatory = $false, Position = 1)]
[String]
$Destination = '.\bh-graph.csv'
)
# Ingest the bloodhound export
$bhe = Get-Content -Raw -LiteralPath $Path| ConvertFrom-Json
# Convert nodes from the export into a hashtable for quick lookups.
$nodes = @{}
$bhe.data.nodes.psobject.members|ForEach-Object -Process {
$nodes.Add($_.name,$_.value)
}
# Put edges into an array
$edges = @($bhe.data.edges)
# Create custom objects with relationships
$relationships = $edges | ForEach-Object -Process {
[PSCustomObject] @{
SourceLabel = $nodes.($_.source).label
SourceKind = $nodes.($_.source).kind
EdgeKind = $_.kind
TargetLabel = $nodes.($_.target).label
TargetKind = $nodes.($_.target).kind
}
}
# Export relationships to csv
$relationships|Export-Csv -NoTypeInformation -LiteralPath $Destination
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment