forked from PyroTek3/PowerShell-AD-Recon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discover-PSInterestingServices
267 lines (217 loc) · 15.6 KB
/
Discover-PSInterestingServices
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
function Discover-PSInterestingServices
{
<#
.SYNOPSIS
This script is used to discover network servers with interesting services without port scanning.
Service discovery in the Active Directory Forest is performed by querying an Active Directory Gloabl Catalog via LDAP.
The script can also provide additional computer information such as OS and last bootup time.
PowerSploit Function: Discover-PSInterestingServices
Author: Sean Metcalf, Twitter: @PyroTek3
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
Version: 1.5
.DESCRIPTION
This script is used to discover network servers with interesting services without port scanning.
Service discovery in the Active Directory Forest is performed by querying an Active Directory Gloabl Catalog via ADSI.
REQUIRES: Active Directory user authentication. Standard user access is fine - admin access is not necessary.
Currently, the script performs the following actions:
* Queries a Global Catalog in the Active Directory root domain for all SPNs in the forest
* Identifies interesting services running on computers (if a port is identified in the SPN, it is shown in the report as SPN.port)
A description of SPN Service Types can be found here:
http://adsecurity.org/?page_id=183
.PARAMETER StandardSPNServiceFilter
Array of Strings: Standard list of SPN Services Reported: ("ADAM","AGPM","bo","CESREMOTE","Dfs","DNS","Exchange","FIMService","ftp","http","IMAP","ipp","iSCSITarget","kadmin","ldap","MS","sql","nfs","secshd","sip","SMTP","SoftGrid","TERMSRV","Virtual","vmrc","vnc","vpn","vssrvc","WSMAN","xmpp")
It is best to remove from this list if needed. Use the OptionalSPNServiceFilter parameter for adding SPN Services to the report.
.PARAMETER OptionalSPNServiceFilter
Array of Strings: Provide additonal SPN service types desired in the report.
Multiple values are acceptable.
.EXAMPLE
Discover-PSInterestingServices
Perform discovery on servers running interesting services via ADSI returning results in a custom PowerShell object.
Discovers the following SPNs: ("ADAM","AGPM","bo","CESREMOTE","Dfs","DNS","Exchange","FIMService","ftp","http","IMAP","ipp","iSCSITarget","kadmin","ldap","MS","sql","nfs","secshd","sip","SMTP","SoftGrid","TERMSRV","Virtual","vmrc","vnc","vpn","vssrvc","WSMAN","xmpp")
Discover-PSInterestingServices -GetAllForestSPNs
Perform discovery of ALL SPN typs in Active Directory in order to discover servers running interesting services via ADSI returning results in a custom PowerShell object.
Discover-PSInterestingServices -OptionalSPNServiceFilter ("Microsoft Virtual Console Service","Dfsr")
Perform discovery on servers running interesting services (adding Hyper-V hosts and domain DFS servers) via ADSI returning results in a custom PowerShell object.
.NOTES
This script is used to discover computers with interesting services without port scanning.
.LINK
Blog: http://www.ADSecurity.org
Github repo: https://github.com/PyroTek3/PowerShell-AD-Recon
#>
Param
(
[switch] $GetAllForestSPNs,
[String[]] $StandardSPNServiceFilter = ("ADAM","AGPM","bo","CESREMOTE","Dfs","DNS","Exchange","FIMService","ftp","http","IMAP","ipp","iSCSITarget","kadmin","ldap","MS","sql","nfs","secshd","sip","SMTP","SoftGrid","TERMSRV","Virtual","vmrc","vnc","vpn","vssrvc","WSMAN","xmpp"),
[String[]] $OptionalSPNServiceFilter
)
IF ($OptionalSPNServiceFilter)
{ [array]$SPNServiceFilter = $StandardSPNServiceFilter + $OptionalSPNServiceFilter }
ELSE
{ [array]$SPNServiceFilter = $StandardSPNServiceFilter }
Write-verbose "Build SPN searcher based on Standard and Optional "
[string]$ADSearcherSPNTypes = "(|"
ForEach ($SPNServiceFilterItem in $SPNServiceFilter)
{ [string]$ADSearcherSPNTypes += "(serviceprincipalname=*$SPNServiceFilterItem*)" }
[string]$ADSearcherSPNTypes += " )"
Write-Verbose "Get current Active Directory domain... "
$ADForestInfo = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$ADForestInfoRootDomain = $ADForestInfo.RootDomain
$ADForestInfoRootDomainArray = $ADForestInfoRootDomain -Split("\.")
$ADForestInfoRootDomainDN = "DC=" + $ADForestInfoRootDomain -Replace("\.",',DC=')
$ADDomainInfoLGCDN = 'GC://' + $ADForestInfoRootDomainDN
Write-Verbose "Discovering Interesting Services in the AD Forest $ADForestInfoRootDomainDN "
$root = [ADSI]$ADDomainInfoLGCDN
IF ($GetAllForestSPNs -eq $True)
{ $ADSearcher = new-Object System.DirectoryServices.DirectorySearcher($root,"(serviceprincipalname=*)") }
ELSE
{ $ADSearcher = new-Object System.DirectoryServices.DirectorySearcher($root,"$ADSearcherSPNTypes") }
$ADSearcher.PageSize = 1000
$AllForestSPNs = $ADSearcher.FindAll()
#$AllForestSPNsCount = $AllForestSPNs.Count
$AllInterestingSPNs = $NULL
$AllSPNs = $NULL
$AllSPNTypes = $NULL
$AllInterestingSPNHashTable =@{}
$AllInterestingSPNReverseHashTable =@{}
ForEach ($AllForestSPNsItem in $AllForestSPNs)
{
$AllForestSPNsItemPath = $AllForestSPNsItem.Path
Write-Verbose "Reviewing SPN for $AllForestSPNsItemPath "
$AllForestSPNsItemDomainName = $NULL
[array]$AllForestSPNsItemArray = $AllForestSPNsItem.Path -Split(",DC=")
[int]$DomainNameFECount = 0
ForEach ($AllForestSPNsItemArrayItem in $AllForestSPNsItemArray)
{
IF ($DomainNameFECount -gt 0)
{ [string]$AllForestSPNsItemDomainName += $AllForestSPNsItemArrayItem + "." }
$DomainNameFECount++
}
$AllForestSPNsItemDomainName = $AllForestSPNsItemDomainName.Substring(0,$AllForestSPNsItemDomainName.Length-1)
ForEach ($FSPNItemSPNItem in $AllForestSPNsItem.properties.serviceprincipalname)
{
Write-Verbose "Reviewing SPN Data: $FSPNItemSPNItem "
[string]$FSPNItemSPNItemSPNType = ( $FSPNItemSPNItem -Split("/") )[0]
[array]$AllSPNTypes += ( $FSPNItemSPNItem -Split("/") )[0]
IF ( ($FSPNItemSPNItemSPNType -like "*kadmin*") -AND ($AllForestSPNsItem -like "*krbtgt*") )
{
$AllInterestingSPNHashTable.Set_Item("krbtgt ($AllForestSPNsItemDomainName)",$FSPNItemSPNItem)
$AllInterestingSPNReverseHashTableData = $AllInterestingSPNReverseHashTable.Get_Item($FSPNItemSPNItem)
IF ($AllInterestingSPNReverseHashTableData)
{
$AllInterestingSPNReverseHashTableDataUpdate = $AllInterestingSPNReverseHashTableData + ";" + "krbtgt ($AllForestSPNsItemDomainName)"
$AllInterestingSPNReverseHashTable.Set_Item($FSPNItemSPNItem,$AllInterestingSPNReverseHashTableDataUpdate)
}
IF (!$AllInterestingSPNReverseHashTableData)
{ $AllInterestingSPNReverseHashTable.Set_Item($FSPNItemSPNItem,"krbtgt ($AllForestSPNsItemDomainName)") }
}
ELSE
{
$FSPNItemSPNItemServerFQDN = ( ( ( $FSPNItemSPNItem -Split("/") )[1] )-Split(":") )[0]
IF ($FSPNItemSPNItemServerFQDN -notlike "*$AllForestSPNsItemDomainName*" )
{ $FSPNItemSPNItemServerFQDN = $FSPNItemSPNItemServerFQDN + "." + $AllForestSPNsItemDomainName }
[string]$FSPNItemSPNItemServerPort = ( ( ( $FSPNItemSPNItem -Split("/") )[1] )-Split(":") )[1]
$AllInterestingSPNReverseHashTableData = $AllInterestingSPNReverseHashTable.Get_Item($FSPNItemSPNItemSPNType)
IF ( ($AllInterestingSPNReverseHashTableData) -AND ($AllInterestingSPNReverseHashTableData -notlike "*$FSPNItemSPNItemServerFQDN*") )
{
$AllInterestingSPNReverseHashTableDataUpdate = $AllInterestingSPNReverseHashTableData + ";" + $FSPNItemSPNItemServerFQDN
$AllInterestingSPNReverseHashTable.Set_Item($FSPNItemSPNItemSPNType,$AllInterestingSPNReverseHashTableDataUpdate)
}
IF (!$AllInterestingSPNReverseHashTableData)
{ $AllInterestingSPNReverseHashTable.Set_Item($FSPNItemSPNItemSPNType,$FSPNItemSPNItemServerFQDN) }
IF ( ($FSPNItemSPNItemServerPort) -AND ($FSPNItemSPNItemServerPort -match "^[\d\.]+$") )
{ $FSPNItemSPNItemSPNType = $FSPNItemSPNItemSPNType + "." + $FSPNItemSPNItemServerPort }
ForEach ($SPNServiceFilterItem in $SPNServiceFilter)
{
IF ($FSPNItemSPNItemSPNType -like "*$SPNServiceFilterItem*")
{
Write-Verbose "SPNServiceFilterItem is $SPNServiceFilterItem "
$AllInterestingSPNsData = $AllInterestingSPNHashTable.Get_Item($FSPNItemSPNItemServerFQDN)
IF ( ($AllInterestingSPNsData) -AND ($AllInterestingSPNsData -notlike "*$SPNServiceFilterItem*") )
{
$AllInterestingSPNsDataUpdate = $AllInterestingSPNsData + ";" + $FSPNItemSPNItemSPNType
$AllInterestingSPNHashTable.Set_Item($FSPNItemSPNItemServerFQDN,$AllInterestingSPNsDataUpdate)
Write-Verbose "Updating AllInterestingSPNHashTable with $FSPNItemSPNItemServerFQDN : $AllInterestingSPNsDataUpdate "
}
IF (!$AllInterestingSPNsData)
{
$AllInterestingSPNHashTable.Set_Item($FSPNItemSPNItemServerFQDN,$FSPNItemSPNItemSPNType)
Write-Verbose "Updating AllInterestingSPNHashTable with new data $FSPNItemSPNItemServerFQDN : $FSPNItemSPNItemSPNType "
}
}
}
}
}
}
$ALLIntServerServicesReport = @()
ForEach ($AllInterestingSPNHashTableItem in $AllInterestingSPNHashTable.GetEnumerator() )
{
$AllServerInterstingSPNServiceList = $NULL
$AllInterestingSPNHashTableItemServerDomainName = $NULL
$AllInterestingSPNHashTableItemServerDomainDN = $NULL
$AllInterestingSPNHashTableItemServerFQDN = $AllInterestingSPNHashTableItem.Name
[array]$AllServerInterstingSPNArray = ($AllInterestingSPNHashTableItem.Value) -split(";")
[array]$AllServerInterstingSPNArraySorted = $AllServerInterstingSPNArray | sort-Object
ForEach ($AllServerInterstingSPNArraySortedItem in $AllServerInterstingSPNArraySorted)
{ [string]$AllServerInterstingSPNServiceList += $AllServerInterstingSPNArraySortedItem + ";" }
$AllServerInterstingSPNServiceList = $AllServerInterstingSPNServiceList.Substring(0,$AllServerInterstingSPNServiceList.Length-1)
$AllInterestingSPNHashTableItemServerFQDNArray = $AllInterestingSPNHashTableItemServerFQDN -Split('\.')
[int]$FQDNArrayFECount = 0
ForEach ($AllInterestingSPNHashTableItemServerFQDNArrayItem in $AllInterestingSPNHashTableItemServerFQDNArray)
{
IF ($FQDNArrayFECount -ge 1)
{
[string]$AllInterestingSPNHashTableItemServerDomainName += $AllInterestingSPNHashTableItemServerFQDNArrayItem + "."
[string]$AllInterestingSPNHashTableItemServerDomainDN += "DC=" + $AllInterestingSPNHashTableItemServerFQDNArrayItem + ","
}
$FQDNArrayFECount++
}
$AllInterestingSPNHashTableItemServerDomainName = $AllInterestingSPNHashTableItemServerDomainName.Substring(0,$AllInterestingSPNHashTableItemServerDomainName.Length-1)
$AllInterestingSPNHashTableItemServerDomainDN = $AllInterestingSPNHashTableItemServerDomainDN.Substring(0,$AllInterestingSPNHashTableItemServerDomainDN.Length-1)
$AllInterestingSPNHashTableItemServerDomainLDAPDN = "LDAP://$AllInterestingSPNHashTableItemServerDomainDN"
$AllInterestingSPNHashTableItemServerName = $AllInterestingSPNHashTableItemServerFQDN -Replace(("."+$AllInterestingSPNHashTableItemServerDomainName),"")
IF ($AllInterestingSPNHashTableItemServerFQDN -like "*changepw*")
{ $AllInterestingSPNHashTableItemServerFQDN = $AllInterestingSPNHashTableItemServerDomainName + "\krbgt" }
IF ($AllInterestingSPNHashTableItemServerFQDN -like "*_msdcs*")
{
$AllInterestingSPNHashTableItemServerFQDN = $AllInterestingSPNHashTableItemServerDomainName + "\DNSzone"
$AllInterestingSPNHashTableItemServerName = $NULL
}
TRY
{
$ADComputerSearch = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$ADComputerSearch.SearchRoot = $AllInterestingSPNHashTableItemServerDomainLDAPDN
$ADComputerSearch.PageSize = 500
$ADComputerSearch.Filter = "(&(objectCategory=Computer)(name=$AllInterestingSPNHashTableItemServerName))"
$ComputerADInfo = $ADComputerSearch.FindAll()
[string]$ComputerADInfoLastLogonTimestamp = ($ComputerADInfo[0].properties.lastlogontimestamp)
TRY { [datetime]$ComputerADInfoLLT = [datetime]::FromFileTime($ComputerADInfoLastLogonTimestamp) }
CATCH { $ComputerADInfoLLT = $Null }
}
CATCH
{ Write-Warning "Unable to gather property data for computer $AllInterestingSPNHashTableItemServerName " }
$ComputerADInfoShortOS = $Null
$ComputerADInfoShortOSArray = $ComputerADInfoOperatingSystem -split(" ")
ForEach ($ComputerADInfoShortOSArrayItem in $ComputerADInfoShortOSArray )
{
IF ($ComputerADInfoShortOSArrayItem -eq "Windows")
{ [string] $ComputerADInfoShortOS += "Win" }
IF ($ComputerADInfoShortOSArrayItem -eq "Server")
{ }
IF ($ComputerADInfoShortOSArrayItem -match "\d")
{ [string] $ComputerADInfoShortOS += $ComputerADInfoShortOSArrayItem }
}
$IntServerServicesReport = New-Object -TypeName System.Object
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name Domain -Value $AllInterestingSPNHashTableItemServerDomainName
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name ServerName -Value $AllInterestingSPNHashTableItemServerFQDN
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name SPNServices -Value $AllServerInterstingSPNServiceList
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name OperatingSystem -Value ($ComputerADInfo[0].properties.operatingsystem)
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name OSServicePack -Value ($ComputerADInfo[0].properties.operatingsystemservicepack)
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name LastBootup -Value $ComputerADInfoLLT
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name OSVersion -Value ($ComputerADInfo[0].properties.operatingsystemversion)
$IntServerServicesReport | Add-Member -MemberType NoteProperty -Name Description -Value ($ComputerADInfo[0].properties.description)
[array]$ALLIntServerServicesReport += $IntServerServicesReport
}
return $ALLIntServerServicesReport
}