Repository navigation
Expand file tree
/
Copy pathstix2csv.ps1
More file actions
162 lines (149 loc) · 6.69 KB
/
Copy pathstix2csv.ps1
File metadata and controls
162 lines (149 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
param (
[Parameter(Mandatory = $true)]
[string]$InputFolder,
[Parameter(Mandatory = $true)]
[string]$OutputFile
)
# Set expiration timestamp (48 hours from now)
$expiration = (Get-Date).AddHours(48).ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")
$allRecords = @()
# Regex for STIX pattern parsing
$patternRegex = "\[(\w+):value\s*=\s*'([^']+)'\]"
# Get all JSON files in the input folder
$InputFiles = Get-ChildItem -Path $InputFolder -Filter *.json -File | Select-Object -ExpandProperty FullName
Write-Host "Found $($InputFiles.Count) JSON file(s) in folder: $InputFolder"
foreach ($InputFile in $InputFiles) {
Write-Host "Processing: $InputFile"
try {
$json = Get-Content $InputFile -Raw | ConvertFrom-Json
} catch {
Write-Warning "Failed to parse ${InputFile}: $_"
continue
}
if (-not $json.objects) {
Write-Warning "No 'objects' array found in: ${InputFile}"
continue
}
foreach ($obj in $json.objects) {
switch ($obj.type) {
"ipv4-addr" {
$allRecords += [PSCustomObject]@{
IndicatorType = "IpAddress"
IndicatorValue = $obj.value
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX IP"
Description = "Imported from ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
"url" {
$allRecords += [PSCustomObject]@{
IndicatorType = "Url"
IndicatorValue = $obj.value
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX URL"
Description = "Imported from ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
"domain-name" {
$allRecords += [PSCustomObject]@{
IndicatorType = "DomainName"
IndicatorValue = $obj.value
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX Domain"
Description = "Imported from ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
"file" {
if ($obj.hashes.'SHA-1') {
$allRecords += [PSCustomObject]@{
IndicatorType = "FileSha1"
IndicatorValue = $obj.hashes.'SHA-1'
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX File SHA1"
Description = "Imported from ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
if ($obj.hashes.'SHA-256') {
$allRecords += [PSCustomObject]@{
IndicatorType = "FileSha256"
IndicatorValue = $obj.hashes.'SHA-256'
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX File SHA256"
Description = "Imported from ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
}
"indicator" {
if ($obj.pattern -match $patternRegex) {
$stixType = $matches[1]
$indicatorValue = $matches[2]
$mappedType = switch ($stixType) {
"ipv4-addr" { "IpAddress" }
"url" { "Url" }
"domain-name" { "DomainName" }
"file" { "FileSha1" } # Default for file pattern
default { $null }
}
if ($mappedType) {
$allRecords += [PSCustomObject]@{
IndicatorType = $mappedType
IndicatorValue = $indicatorValue
ExpirationTime = $expiration
Action = "Block"
Severity = "High"
Title = "Imported STIX Indicator"
Description = "Extracted from pattern in ${InputFile}"
RecommendedActions = ""
RbacGroups = ""
Category = ""
MitreTechniques = ""
GenerateAlert = "TRUE"
}
}
}
}
}
}
}
# Export to CSV
if ($allRecords.Count -eq 0) {
Write-Warning "No indicators found in any file."
exit 1
}
$allRecords | Export-Csv -Path $OutputFile -NoTypeInformation
Write-Host "Exported $($allRecords.Count) indicators to: $OutputFile"