-
Notifications
You must be signed in to change notification settings - Fork 355
/
DeepBlueHash-collector.ps1
69 lines (69 loc) · 2.82 KB
/
DeepBlueHash-collector.ps1
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
$hashdirectory=".\hashes\"
$events = get-winevent @{logname="Microsoft-Windows-Sysmon/Operational";id=1,6,7,29}
ForEach ($event in $events) {
if ($event.id -eq 1){ # Process creation
if ($event.Properties.Count -le 16){
$path=$event.Properties[3].Value # Full path of the file
$hash=$event.Properties[11].Value # Hashes
}
ElseIf ($event.Properties.Count -le 17){
$path=$event.Properties[4].Value # Full path of the file
$hash=$event.Properties[16].Value # Hashes
}
Else {
$path=$event.Properties[4].Value # Full path of the file
$hash=$event.Properties[17].Value # Hashes
}
}
ElseIf ($event.id -eq 29){ # FileExecutableDetected
$path=$event.Properties[6].Value # Full path of the file
$hash=$event.Properties[7].Value # Hashes
}
Else{
# Hash and path are part of the message field in Sysmon events 6 and 7. Need to parse the XML
$eventXML = [xml]$event.ToXml()
If ($event.id -eq 6){ # Driver (.sys) load
if ($event.Properties.Count -le 6){
$path=$eventXML.Event.EventData.Data[1]."#text" # Full path of the file
$hash=$eventXML.Event.EventData.Data[2]."#text" # Hashes
$hash
}
Else{
$path=$eventXML.Event.EventData.Data[2]."#text" # Full path of the file
$hash=$eventXML.Event.EventData.Data[3]."#text" # Hashes
}
}
ElseIf ($event.id -eq 7){ # Image (.dll) load
if ($event.Properties.Count -lt 14){
$path=$eventXML.Event.EventData.Data[4]."#text" # Full path of the file
$hash=$eventXML.Event.EventData.Data[5]."#text" # Hashes
}
Elseif ($event.Properties.Count -lt 15){
$path=$eventXML.Event.EventData.Data[5]."#text" # Full path of the file
$hash=$eventXML.Event.EventData.Data[10]."#text" # Hashes
}
Else{
$path=$eventXML.Event.EventData.Data[5]."#text" # Full path of the file
$hash=$eventXML.Event.EventData.Data[11]."#text" # Hashes
}
}
Else{
Out-Host "Logic error 1, should not reach here..."
Exit 1
}
}
# Multiple hashes may be logged, we want SHA256. Remove everything through "SHA256="
$SHA256= $hash -Replace "^.*SHA256=",""
# Split the string on commas, grab field 0
$SHA256=$SHA256.Split(",")[0]
if ($SHA256 -Match '^[0-9A-F]{64}$'){ # SHA256 hashes are 64 character hex strings
$hashfile="$hashdirectory\$SHA256"
if (-not (Test-Path "$hashfile*")){
# Hash file doesn't exist (or any variants with extensions), create it
$path | Set-Content $hashfile
}
}
Else{
Out-Host "No SHA256 hash found. Ensure Sysmon is creating SHA256 hashes"
}
}