-
Notifications
You must be signed in to change notification settings - Fork 7
/
6_Invoke-DoubleHop.ps1
161 lines (122 loc) · 5.03 KB
/
6_Invoke-DoubleHop.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
<##############################################################################
Ashley McGlone
Microsoft Premier Field Engineer
April 2017
http://aka.ms/GoateePFE
This script is part of a demo of Kerberos Double Hop mitigations in
PowerShell. Presented at the PowerShell and DevOps Global Summit 2017.
http://aka.ms/pskdh
LEGAL DISCLAIMER
This Sample Code is provided for the purpose of illustration only and is not
intended to be used in a production environment. THIS SAMPLE CODE AND ANY
RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a
nonexclusive, royalty-free right to use and modify the Sample Code and to
reproduce and distribute the object code form of the Sample Code, provided
that You agree: (i) to not use Our name, logo, or trademarks to market Your
software product in which the Sample Code is embedded; (ii) to include a valid
copyright notice on Your software product in which the Sample Code is embedded;
and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and
against any claims or lawsuits, including attorneys’ fees, that arise or result
from the use or distribution of the Sample Code.
##############################################################################>
break
# Avoid WinRM limitations of RB KCD by passing creds with $Using to the second hop
$ServerB = 'sb'
$ServerC = 'sc'
$Cred = (Get-Credential proseware\administrator)
# Works without RB KCD, passing creds again with $using:
# NOTE: PSComputerName returns ServerB, not ServerC!
Invoke-Command -ComputerName $ServerB -Credential $Cred -ScriptBlock {
Invoke-Command -ComputerName $using:ServerC -Credential $using:Cred -ScriptBlock {"Invoked on ServerC: $(hostname)"}
$cim = New-CimSession -ComputerName $using:ServerC -Credential $using:Cred
Get-CimInstance -ClassName win32_computersystem -CimSession $cim
Get-ScheduledTask -CimSession $cim | Select-Object -First 1
Get-NetAdapter -CimSession $cim | Select-Object -First 1
Remove-CimSession $cim
}
# Works across domains
$ServerB = 'sb.proseware.com'
$ServerC = 'ms1.alpineskihouse.com'
$DomainBCred = (Get-Credential proseware\administrator)
$DomainCCred = (Get-Credential alpineskihouse\administrator)
# Works without RB KCD, passing creds again with $using:
# NOTE: PSComputerName returns ServerB, not ServerC!
Invoke-Command -ComputerName $ServerB -Credential $DomainBCred -ScriptBlock {
Invoke-Command -ComputerName $using:ServerC -Credential $using:DomainCCred -ScriptBlock {"Invoked on ServerC: $(hostname)"}
$cim = New-CimSession -ComputerName $using:ServerC -Credential $using:DomainCCred
Get-CimInstance -ClassName win32_computersystem -CimSession $cim
Get-ScheduledTask -CimSession $cim | Select-Object -First 1
Get-NetAdapter -CimSession $cim | Select-Object -First 1
Remove-CimSession $cim
}
# Helper function INVOKE-DOUBLEHOP
Import-Module C:\PshSummit\RBKCD.psm1
Get-Command -Module RBKCD
Invoke-DoubleHop -ServerB sb -ServerC dc -DomainBCred $DomainBCred -Scriptblock {
dir \\dc\c$
}
Invoke-DoubleHop -ServerB sb -ServerC dc -DomainBCred $DomainBCred -Scriptblock {
$PSSenderInfo
$PSSenderInfo.UserInfo.Identity
$PSSenderInfo.UserInfo.WindowsIdentity
}
Invoke-DoubleHop -ServerB sb -ServerC dc -DomainBCred $DomainBCred -Scriptblock {
hostname
$env:COMPUTERNAME
Get-WmiObject Win32_ComputerSystem
Get-CimInstance Win32_ComputerSystem
}
Invoke-DoubleHop -ServerB sb -ServerC dc -DomainBCred $DomainBCred -Scriptblock {
Unlock-ADAccount -Identity alice -Verbose
}
Invoke-DoubleHop -ServerB sb -ServerC dc -DomainBCred $DomainBCred -Scriptblock {
Get-WinEvent -LogName Microsoft-Windows-WinRM/Operational -MaxEvents 5
}
# Now cross-domain
$p = @{
ServerB = 'sb'
ServerC = 'dc1.alpineskihouse.com'
DomainBCred = $DomainBCred
DomainCCred = $DomainCCred
}
Invoke-DoubleHop @p -Scriptblock {
dir C:\
}
Invoke-DoubleHop @p -Scriptblock {
$env:COMPUTERNAME
$PSSenderInfo
}
Invoke-DoubleHop @p -Scriptblock {
hostname
$env:COMPUTERNAME
Get-WmiObject Win32_ComputerSystem
Get-CimInstance Win32_ComputerSystem
}
Invoke-DoubleHop @p -Scriptblock {
Unlock-ADAccount -Identity alice -Verbose
}
# Now criss-cross-domain
# proseware -> alpineskihouse -> proseware
$p = @{
ServerB = 'ms1.alpineskihouse.com'
ServerC = 'sb.proseware.com'
DomainBCred = $DomainCCred
DomainCCred = $DomainBCred
}
Invoke-DoubleHop @p -Scriptblock {
dir \\sb\C$
}
Invoke-DoubleHop @p -Scriptblock {
$env:COMPUTERNAME
$PSSenderInfo
$PSSenderInfo.UserInfo.Identity
$PSSenderInfo.UserInfo.WindowsIdentity
}
Invoke-DoubleHop @p -Scriptblock {
hostname
$env:COMPUTERNAME
Get-WmiObject Win32_ComputerSystem
Get-CimInstance Win32_ComputerSystem
}