-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFIR-Script.ps1
366 lines (315 loc) · 14.8 KB
/
DFIR-Script.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
$ASCIIBanner = @"
_______ _______ ______ _______ ______ ___ ___
| ____|| \ | _ \ | ____| / __ \ \ \ / /
| |__ | .--. || |_) | _______ | |__ | | | | \ V /
| __| | | | || / |_______| | |__| | | | | > <
| |____ | '--' || |\ \__ | | | \__/ | / \
|_______||_______/ | _| `.___| |__| \______/ /__/ \__\
'@
Write-Host $ASCIIBanner
Write-Host "`n"
Write-Host "By twitter: @DNcoder, Github: DNcrypter"
Write-Host "===========================================`n"
$IsAdmin = ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($IsAdmin) {
Write-Host "DFIR Session starting as Administrator..."
}
else {
Write-Host "No Administrator session detected. For the best performance run as Administrator. Not all items can be collected..."
Write-Host "DFIR Session starting..."
}
Write-Host "Creating output directory..."
$CurrentPath = $pwd
$ExecutionTime = $(get-date -f yyyy-MM-dd)
$FolderCreation = "$CurrentPath\DFIR-$env:computername-$ExecutionTime"
mkdir -Force $FolderCreation | Out-Null
Write-Host "Output directory created: $FolderCreation..."
$currentUsername = (Get-WmiObject Win32_Process -f 'Name="explorer.exe"').GetOwner().User
$currentUserSid = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match 'S-1-5-21-\d+-\d+\-\d+\-\d+$' -and $_.ProfileImagePath -match "\\$currentUsername$"} | ForEach-Object{$_.PSChildName}
Write-Host "Current user: $currentUsername $currentUserSid"
function Get-IPInfo {
Write-Host "Collecting local ip info..."
$Ipinfoutput = "$FolderCreation\ipinfo.txt"
Get-NetIPAddress | Out-File -Force -FilePath $Ipinfoutput
}
function Get-ShadowCopies {
Write-Host "Collecting Shadow Copies..."
$ShadowCopy = "$FolderCreation\ShadowCopies.txt"
Get-CimInstance Win32_ShadowCopy | Out-File -Force -FilePath $ShadowCopy
}
function Get-OpenConnections {
Write-Host "Collecting Open Connections..."
$ConnectionFolder = "$FolderCreation\Connections"
mkdir -Force $ConnectionFolder | Out-Null
$Ipinfoutput = "$ConnectionFolder\OpenConnections.txt"
Get-NetTCPConnection -State Established | Out-File -Force -FilePath $Ipinfoutput
}
function Get-AutoRunInfo {
Write-Host "Collecting AutoRun info..."
$AutoRunFolder = "$FolderCreation\Persistence"
mkdir -Force $AutoRunFolder | Out-Null
$RegKeyOutput = "$AutoRunFolder\AutoRunInfo.txt"
Get-CimInstance Win32_StartupCommand | Select-Object Name, command, Location, User | Format-List | Out-File -Force -FilePath $RegKeyOutput
}
function Get-InstalledDrivers {
Write-Host "Collecting Installed Drivers..."
$AutoRunFolder = "$FolderCreation\Persistence"
$RegKeyOutput = "$AutoRunFolder\InstalledDrivers.txt"
driverquery | Out-File -Force -FilePath $RegKeyOutput
}
function Get-ActiveUsers {
Write-Host "Collecting Active users..."
$UserFolder = "$FolderCreation\UserInformation"
mkdir -Force $UserFolder | Out-Null
$ActiveUserOutput = "$UserFolder\ActiveUsers.txt"
query user /server:$server | Out-File -Force -FilePath $ActiveUserOutput
}
function Get-LocalUsers {
Write-Host "Collecting Local users..."
$UserFolder = "$FolderCreation\UserInformation"
$ActiveUserOutput = "$UserFolder\LocalUsers.txt"
Get-LocalUser | Format-Table | Out-File -Force -FilePath $ActiveUserOutput
}
function Get-ActiveProcesses {
Write-Host "Collecting Active Processes..."
$ProcessFolder = "$FolderCreation\ProcessInformation"
New-Item -Path $ProcessFolder -ItemType Directory -Force | Out-Null
$UniqueProcessHashOutput = "$ProcessFolder\UniqueProcessHash.csv"
$ProcessListOutput = "$ProcessFolder\ProcessList.csv"
$processes_list = @()
foreach ($process in (Get-WmiObject Win32_Process | Select-Object Name, ExecutablePath, CommandLine, ParentProcessId, ProcessId))
{
$process_obj = New-Object PSCustomObject
if ($null -ne $process.ExecutablePath)
{
$hash = (Get-FileHash -Algorithm SHA256 -Path $process.ExecutablePath).Hash
$process_obj | Add-Member -NotePropertyName Proc_Hash -NotePropertyValue $hash
$process_obj | Add-Member -NotePropertyName Proc_Name -NotePropertyValue $process.Name
$process_obj | Add-Member -NotePropertyName Proc_Path -NotePropertyValue $process.ExecutablePath
$process_obj | Add-Member -NotePropertyName Proc_CommandLine -NotePropertyValue $process.CommandLine
$process_obj | Add-Member -NotePropertyName Proc_ParentProcessId -NotePropertyValue $process.ParentProcessId
$process_obj | Add-Member -NotePropertyName Proc_ProcessId -NotePropertyValue $process.ProcessId
$processes_list += $process_obj
}
}
($processes_list | Select-Object Proc_Path, Proc_Hash -Unique).GetEnumerator() | Export-Csv -NoTypeInformation -Path $UniqueProcessHashOutput
($processes_list | Select-Object Proc_Name, Proc_Path, Proc_CommandLine, Proc_ParentProcessId, Proc_ProcessId, Proc_Hash).GetEnumerator() | Export-Csv -NoTypeInformation -Path $ProcessListOutput
}
function Get-SecurityEventCount {
Write-Host "Collecting stats Security Events last 48 hours..."
$SecurityEvents = "$FolderCreation\SecurityEvents"
mkdir -Force $SecurityEvents | Out-Null
$ProcessOutput = "$SecurityEvents\EventCount.txt"
$SecurityEvents = Get-EventLog -LogName security -After (Get-Date).AddDays(-2)
$SecurityEvents | Group-Object -Property EventID -NoElement | Sort-Object -Property Count -Descending | Out-File -Force -FilePath $ProcessOutput
}
function Get-SecurityEvents {
Write-Host "Collecting Security Events last 48 hours..."
$SecurityEvents = "$FolderCreation\SecurityEvents"
mkdir -Force $SecurityEvents | Out-Null
$ProcessOutput = "$SecurityEvents\SecurityEvents.txt"
get-eventlog security -After (Get-Date).AddDays(-2) | Format-List * | Out-File -Force -FilePath $ProcessOutput
}
function Get-EVTXFiles {
Write-Host "Collecting Important EVTX Files..."
$EventViewer = "$FolderCreation\Event Viewer"
mkdir -Force $EventViewer | Out-Null
$evtxPath = "C:\Windows\System32\winevt\Logs"
$channels = @(
"Application",
"Security",
"System",
"Microsoft-Windows-Sysmon%4Operational",
"Microsoft-Windows-TaskScheduler%4Operational",
"Microsoft-Windows-PowerShell%4Operational"
)
Get-ChildItem "$evtxPath\*.evtx" | Where-Object{$_.BaseName -in $channels} | ForEach-Object{
Copy-Item -Path $_.FullName -Destination "$($EventViewer)\$($_.Name)"
}
}
function Get-OfficeConnections {
param(
[Parameter(Mandatory=$false)][String]$UserSid
)
Write-Host "Collecting connections made from office applications..."
$ConnectionFolder = "$FolderCreation\Connections"
$OfficeConnection = "$ConnectionFolder\ConnectionsMadeByOffice.txt"
if($UserSid) {
Get-ItemProperty -Path "registry::HKEY_USERS\$UserSid\SOFTWARE\Microsoft\Office\16.0\Common\Internet\Server Cache*" -erroraction 'silentlycontinue' | Out-File -Force -FilePath $OfficeConnection
}
else {
Get-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Internet\Server Cache* -erroraction 'silentlycontinue' | Out-File -Force -FilePath $OfficeConnection
}
}
function Get-NetworkShares {
param(
[Parameter(Mandatory=$false)][String]$UserSid
)
Write-Host "Collecting Active Network Shares..."
$ConnectionFolder = "$FolderCreation\Connections"
$ProcessOutput = "$ConnectionFolder\NetworkShares.txt"
if($UserSid) {
Get-ItemProperty -Path "registry::HKEY_USERS\$UserSid\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\" -erroraction 'silentlycontinue' | Format-Table | Out-File -Force -FilePath $ProcessOutput
}
else {
Get-ChildItem -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\ | Format-Table | Out-File -Force -FilePath $ProcessOutput
}
}
function Get-SMBShares {
Write-Host "Collecting SMB Shares..."
$ConnectionFolder = "$FolderCreation\Connections"
$ProcessOutput = "$ConnectionFolder\SMBShares.txt"
Get-SmbShare | Out-File -Force -FilePath $ProcessOutput
}
function Get-RDPSessions {
Write-Host "Collecting RDS Sessions..."
$ConnectionFolder = "$FolderCreation\Connections"
$ProcessOutput = "$ConnectionFolder\RDPSessions.txt"
qwinsta /server:localhost | Out-File -Force -FilePath $ProcessOutput
}
function Get-RemotelyOpenedFiles {
Write-Host "Collecting Remotly Opened Files..."
$ConnectionFolder = "$FolderCreation\Connections"
$ProcessOutput = "$ConnectionFolder\RemotelyOpenedFiles.txt"
openfiles | Out-File -Force -FilePath $ProcessOutput
}
function Get-DNSCache {
Write-Host "Collecting DNS Cache..."
$ConnectionFolder = "$FolderCreation\Connections"
$ProcessOutput = "$ConnectionFolder\DNSCache.txt"
Get-DnsClientCache | Format-List | Out-File -Force -FilePath $ProcessOutput
}
function Get-PowershellHistory {
Write-Host "Collecting Powershell History..."
$PowershellHistoryOutput = "$FolderCreation\PowershellHistory.txt"
history | Out-File -Force -FilePath $PowershellHistoryOutput
}
function Get-RecentlyInstalledSoftwareEventLogs {
Write-Host "Collecting Recently Installed Software EventLogs..."
$ApplicationFolder = "$FolderCreation\Applications"
mkdir -Force $ApplicationFolder | Out-Null
$ProcessOutput = "$ApplicationFolder\RecentlyInstalledSoftwareEventLogs.txt"
Get-WinEvent -ProviderName msiinstaller | where id -eq 1033 | select timecreated,message | FL *| Out-File -Force -FilePath $ProcessOutput
}
function Get-RunningServices {
Write-Host "Collecting Running Services..."
$ApplicationFolder = "$FolderCreation\Applications"
$ProcessOutput = "$ApplicationFolder\RecentlyInstalledSoftwareEventLogs.txt"
Get-Service | Where-Object {$_.Status -eq "Running"} | format-list | Out-File -Force -FilePath $ProcessOutput
}
function Get-ScheduledTasks {
Write-Host "Collecting Scheduled Tasks..."
$ScheduledTaskFolder = "$FolderCreation\ScheduledTask"
mkdir -Force $ScheduledTaskFolder| Out-Null
$ProcessOutput = "$ScheduledTaskFolder\ScheduledTasksList.txt"
Get-ScheduledTask | Where-Object {$_.State -ne "Disabled"} | Format-List | Out-File -Force -FilePath $ProcessOutput
}
function Get-ScheduledTasksRunInfo {
Write-Host "Collecting Scheduled Tasks Run Info..."
$ScheduledTaskFolder = "$FolderCreation\ScheduledTask"
$ProcessOutput = "$ScheduledTaskFolder\ScheduledTasksListRunInfo.txt"
Get-ScheduledTask | Where-Object {$_.State -ne "Disabled"} | Get-ScheduledTaskInfo | Out-File -Force -FilePath $ProcessOutput
}
function Get-ConnectedDevices {
Write-Host "Collecting Information about Connected Devices..."
$DeviceFolder = "$FolderCreation\ConnectedDevices"
New-Item -Path $DeviceFolder -ItemType Directory -Force | Out-Null
$ConnectedDevicesOutput = "$DeviceFolder\ConnectedDevices.csv"
Get-PnpDevice | Export-Csv -NoTypeInformation -Path $ConnectedDevicesOutput
}
function Get-ChromiumFiles {
param(
[Parameter(Mandatory=$true)][String]$Username
)
Write-Host "Collecting raw Chromium history and profile files..."
$HistoryFolder = "$FolderCreation\Browsers\Chromium"
New-Item -Path $HistoryFolder -ItemType Directory -Force | Out-Null
$filesToCopy = @(
'Preferences',
'History'
)
Get-ChildItem "C:\Users\$Username\AppData\Local\*\*\User Data\*\" | Where-Object { `
(Test-Path "$_\History") -and `
[char[]](Get-Content "$($_.FullName)\History" -Encoding byte -TotalCount 'SQLite format'.Length) -join ''
} | Where-Object {
$srcpath = $_.FullName
$destpath = $_.FullName -replace "^C:\\Users\\$Username\\AppData\\Local",$HistoryFolder -replace "User Data\\",""
New-Item -Path $destpath -ItemType Directory -Force | Out-Null
$filesToCopy | ForEach-Object{
$filesToCopy | Where-Object{ Test-Path "$srcpath\$_" } | ForEach-Object{ Copy-Item -Path "$srcpath\$_" -Destination "$destpath\$_" }
}
}
}
function Get-FirefoxFiles {
param(
[Parameter(Mandatory=$true)][String]$Username
)
if(Test-Path "C:\Users\$Username\AppData\Roaming\Mozilla\Firefox\Profiles\") {
Write-Host "Collecting raw Firefox history and profile files..."
$HistoryFolder = "$FolderCreation\Browsers\Firefox"
New-Item -Path $HistoryFolder -ItemType Directory -Force | Out-Null
$filesToCopy = @(
'places.sqlite',
'permissions.sqlite',
'content-prefs.sqlite',
'extensions'
)
Get-ChildItem "C:\Users\$Username\AppData\Roaming\Mozilla\Firefox\Profiles\" | Where-Object { `
(Test-Path "$($_.FullName)\places.sqlite") -and `
[char[]](Get-Content "$($_.FullName)\places.sqlite" -Encoding byte -TotalCount 'SQLite format'.Length) -join ''
} | ForEach-Object {
$srcpath = $_.FullName
$destpath = $_.FullName -replace "^C:\\Users\\$Username\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles",$HistoryFolder
New-Item -Path $destpath -ItemType Directory -Force | Out-Null
$filesToCopy | Where-Object{ Test-Path "$srcpath\$_" } | ForEach-Object{ Copy-Item -Path "$srcpath\$_" -Destination "$destpath\$_" }
}
}
}
function Zip-Results {
Write-Host "Write results to $FolderCreation.zip..."
Compress-Archive -Force -LiteralPath $FolderCreation -DestinationPath "$FolderCreation.zip"
}
#Run all functions that do not require admin priviliges
function Run-WithoutAdminPrivilege {
param(
[Parameter(Mandatory=$false)][String]$UserSid,
[Parameter(Mandatory=$false)][String]$Username
)
Get-IPInfo
Get-OpenConnections
Get-AutoRunInfo
Get-ActiveUsers
Get-LocalUsers
Get-ActiveProcesses
Get-OfficeConnections -UserSid $UserSid
Get-NetworkShares -UserSid $UserSid
Get-SMBShares
Get-RDPSessions
Get-PowershellHistory
Get-DNSCache
Get-InstalledDrivers
Get-RecentlyInstalledSoftwareEventLogs
Get-RunningServices
Get-ScheduledTasks
Get-ScheduledTasksRunInfo
Get-ConnectedDevices
if($Username) {
Get-ChromiumFiles -Username $Username
Get-FirefoxFiles -Username $Username
}
}
#Run all functions that do require admin priviliges
Function Run-WithAdminPrivilges {
Get-SecurityEventCount
Get-SecurityEvents
Get-RemotelyOpenedFiles
Get-ShadowCopies
Get-EVTXFiles
}
Run-WithoutAdminPrivilege -UserSid $currentUserSid -Username $currentUsername
if ($IsAdmin) {
Run-WithAdminPrivilges
}
Zip-Results