-
Notifications
You must be signed in to change notification settings - Fork 1
/
ad-enum.ps1
111 lines (91 loc) · 3.29 KB
/
ad-enum.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
Write-Host "
/^\
| |
|-|
/^\ | |
/^\ / [_] \+-+
|---||-------| |
_/^\_ _/^\_| [_] |_/^\_ _/^\_
|___| |___||_______||___| |___|
| |======| |===========| |=====| |
| | | | /^\ | | | |
| | | | | | | | | |
|_|______|_|__ | |___|_|_____|_|
"
function findAll {
param($filter,$Searcher)
$domainObj = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$PDC = ($domainObj.PdcRoleOwner).Name
$SearchString = "LDAP://"
$SearchString += $PDC + "/"
$DistinguishedName = "DC=$($domainObj.Name.Replace('.', ',DC='))"
$SearchString += $DistinguishedName
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$SearchString)
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$Searcher.SearchRoot = $objDomain
$Searcher.filter=$filter
$Result = $Searcher.FindAll()
return $Result
}
function enum-group ($Result, $match){
$indent = ""
foreach($group in $Result.properties) {
if( !($group.member -eq $null)){
foreach($member in $group.member) {
$groupname = $member.split(",")[0].replace("CN=",'')
if($groupname -eq $match) {
Write-Host "Parent:" $group.name " --> " "Child:" $match
}
}
}
}
}
Write-Host "--------------------------------"
Write-Host " LIST AD Group RELATIONSHIPS"
Write-Host "--------------------------------"
$Result = findall "(objectClass=Group)" $Searcher
foreach($group in $Result.properties) {
$match=$group.name
enum-group -Result $Result -match $match
}
Write-Host "---------------------------"
Write-Host " LIST DOMAIN ADMINS"
Write-Host "---------------------------"
$Result = findall "name=Domain Admins" $Searcher
Foreach($obj in $Result){
Write-host "Usernames:"
foreach($member in $obj.Properties.member) {
$name = $member.split(",")[0].replace("CN=",'')
Write-Host "`t" $name
}
}
Write-Host "---------------------------"
Write-Host " LIST AD SERVICE ACCOUNTS"
Write-Host "---------------------------"
$Result = findall "serviceprincipalname=*" $Searcher
$dtable = New-Object System.Data.DataTable
$dtable.Columns.Add("Hostname", "System.String") | Out-Null
$dtable.Columns.Add("SPN", "System.String") | Out-Null
$dtable.Columns.Add("Service", "System.String") | Out-Null
$dtable.Columns.Add("DomainName", "System.String") | Out-Null
$dtable.Columns.Add("IPAddress", "System.String") | Out-Null
foreach ($Object in $Result){
$hostname = $Object.Properties.serviceprincipalname.split("/")[1].split(":")[0]
$service = $Object.Properties.serviceprincipalname.split("/")[0]
$spn = $Object.Properties.serviceprincipalname[0]
$resolv = resolve-dnsname $hostname 2> $null
$nRow = $dtable.NewRow()
$nRow.Hostname = $hostname
$nRow.SPN = $spn
$nRow.Service = $service
if ($resolv -eq $null) {
$nRow.DomainName = ""
$nRow.IPAddress = ""
} else {
$nRow.DomainName = $resolv[0].Name
$nRow.IPAddress = $resolv[0].IPaddress
}
$dtable.Rows.Add($nRow)
}
$dtable | Format-List
Write-Host "------------------------------------------------"