-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReportGenerator.ps1
293 lines (255 loc) · 8.98 KB
/
ReportGenerator.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
#requires -Version 2 -Modules EnhancedHTML2
Import-Module -Name PSSQLite
$SQLLiteDB = 'C:\temp\UserDB.db'
$ReportPath = 'D:\usertest.html'
$QueryUsersWithSamePassword = @"
SELECT DisplayName,NTHash,ClearTxtPWD,DistinguishedName FROM USERS
LEFT JOIN PWDTranslation
On USERS.NTHash=PWDTranslation.Hash
WHERE NTHash IN
(SELECT NTHash FROM Users
GROUP BY NTHash HAVING COUNT (NTHash) >1)
ORDER BY NTHash
"@
$UsersWithTheSamePassword = Invoke-SqliteQuery -Query $QueryUsersWithSamePassword -DataSource $SQLLiteDB
#Get Currently Most Used Password/Hash
$QueryCurrentlyMostUsedHash = @"
SELECT Count(*) HashCount, NTHash,ClearTxtPWD
FROM USERS
LEFT JOIN PWDTranslation
On USERS.NTHash=PWDTranslation.Hash
GROUP BY USERS.NTHash
HAVING HashCount > 1
Order By HashCount Desc
"@
$CurrentlyMostUsedHash = Invoke-SqliteQuery -Query $QueryCurrentlyMostUsedHash -DataSource $SQLLiteDB
#Most used password historically
$QueryMostUsedHashHistorically = @"
SELECT Count(*) HashCount, HashHistory .HASH,ClearTxtPWD
FROM HashHistory
LEFT JOIN PWDTranslation
On HashHistory.Hash=PWDTranslation.Hash
GROUP BY HashHistory.Hash
HAVING HashCount > 1
Order By HashCount Desc
"@
$HistoricallyMostUsedHash = Invoke-SqliteQuery -Query $QueryMostUsedHashHistorically -DataSource $SQLLiteDB
#Users with Weak Password
$QueryUsersWithWeakPassword = @"
SELECT DisplayName,
UserPrincipalName,
COALESCE (GUID ,0) AS Guid,
ClearTxtPWD
FROM PWDTranslation
LEFT JOIN USERS
On Users.NTHash = PWDTranslation.Hash
Where Guid <> 0
"@
$UsersWithWeakPassword = Invoke-SqliteQuery -Query $QueryUsersWithWeakPassword -DataSource $SQLLiteDB
$queryMostUsedHashes = @"
SELECT hh.hash,u.DisplayName, u.DistinguishedName, pt.ClearTxtPWD
FROM Users u INNER JOIN HashHistory hh
ON u.GUID = hh.GUID JOIN PWDTranslation pt
ON hh.Hash = pt.hash
"@
$MostUsedHashes = Invoke-SqliteQuery -Query $queryMostUsedHashes -DataSource $SQLLiteDB
$QueryUsersWhoHaveReusedTheirPasswords = @"
CREATE TEMPORARY TABLE ParentChild AS
SELECT * FROM USERS u INNER JOIN HashHistory hh ON u.GUID = hh.GUID ;
Select DisplayName, pc.Hash, ClearTxtPWD, Count(*) as HashCount
FROM ParentChild pc LEFT OUTER JOIN PWDTranslation pwd
ON pc.Hash = pwd.hash
Group By DisplayName, pc.Hash, ClearTxtPWD
HAVING HashCount > 1
Order By hashCount DESC;
"@
$UsersWhoHaveReusedTheirPasswords = Invoke-SqliteQuery -Query $QueryUsersWhoHaveReusedTheirPasswords -DataSource $SQLLiteDB
$QueryUsersWithBlankPassword = @"
SELECT *
FROM USERS
WHERE NTHash = '31d6cfe0d16ae931b73c59d7e0c089c0';
"@
$UsersWithBlankPassword = Invoke-SqliteQuery -Query $QueryUsersWithBlankPassword -DataSource $SQLLiteDB
$QueryUsersWhoHaveHadBlankPassword = @"
SELECT DisplayName, SamAccountName, Enabled, LastLogon, NTHash
FROM USERS u INNER JOIN HashHistory hh ON u.GUID = hh.GUID
WHERE Hash == '31d6cfe0d16ae931b73c59d7e0c089c0'
"@
$UsersWhoHaveHadBlankPassword = Invoke-SqliteQuery -Query $QueryUsersWhoHaveHadBlankPassword -DataSource $SQLLiteDB
$QueryUsersWhoHaveNeverChangedPassword = @"
Select DisplayName, SamAccountName, Enabled, LastLogon, NTHash, PWDTranslation.ClearTxtPWD
FROM USERS LEFT JOIN PWDTranslation
ON USERS.NTHash = PWDTranslation.Hash
WHERE NTHashHistoryCount == 1
"@
$UsersWhoHaveNeverChangedPassword = Invoke-SqliteQuery -Query $QueryUsersWhoHaveNeverChangedPassword -DataSource $SQLLiteDB
$style = @"
<style>
body {
color:#333333;
font-family:Calibri,Tahoma;
font-size: 10pt;
}
h1 {
text-align:center;
}
h2 {
border-radius: 25px;
border: 2px solid #73AD21
padding: 20px;
border: 3px solid gray;
margin: 5px;
width: 650px;
height: 30px;
text-align: center;
background-color: #66ccff;
}
th {
font-weight:bold;
color:#eeeeee;
background-color:#333333;
cursor:pointer;
}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
.paginate_enabled_next, .paginate_enabled_previous {
cursor:pointer;
border:1px solid #222222;
background-color:#dddddd;
padding:2px;
margin:4px;
border-radius:2px;
}
.paginate_disabled_previous, .paginate_disabled_next {
color:#666666;
cursor:pointer;
background-color:#dddddd;
padding:2px;
margin:4px;
border-radius:2px;
}
.dataTables_info { margin-bottom:4px; }
.sectionheader { cursor:pointer; }
.sectionheader:hover { color:red; }
.grid { width:100% }
.red {
color:red;
font-weight:bold;
}
</style>
"@
Function Get-UsersWithTheSamePassword
{
$UsersWithTheSamePassword |
Group-Object -Property nthash |
Sort-Object -Property count -Descending |
ForEach-Object -Process {
$Props = @{
count = $_.Count
Hash = $_.Name
'UserName(s)' = $_.Group |
ForEach-Object -Process {
"$($_.DisplayName)<br>"
}
Password = $_.Group.ClearTxtPWD |
Select-Object -First 1
'DistinguishedName(s)' = $_.Group |
ForEach-Object -Process {
"$($_.DistinguishedName)<br>"
}
}
New-Object -TypeName PSObject -Property $Props
}
}
$UsersWithTheSamePassword = Get-UsersWithTheSamePassword
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Current users with the same Password ($(@($UsersWithTheSamePassword).Count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'Count', 'Hash', 'Password', 'UserName(s)', 'DistinguishedName(s)'
}
$html_UserWithTheSamePassword = $UsersWithTheSamePassword |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Historically most used Hashes/Passwords ($(@($HistoricallyMostUsedHash).count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'HashCount', 'Hash', 'ClearTxtPWD'
}
$html_HistoricallyMostUsedHashes = $HistoricallyMostUsedHash |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Users who have reused their Password(s) ($(@($UsersWhoHaveReusedTheirPasswords).Count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'HashCount', 'Hash', 'ClearTxtPWD', 'DisplayName'
}
$html_UsersWhoHaveReusedTheirPasswords = $UsersWhoHaveReusedTheirPasswords |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Current users with `"Weak/Known`" Passwords ($(@($UsersWithWeakPassword).count)) </h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'DisplayName', 'UserPrincipalName', 'ClearTxtPWD'
}
$html_UsersWithWeakPasswords = $UsersWithWeakPassword |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Users who have never changed Password ($(@($UsersWhoHaveNeverChangedPassword).count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'DisplayName', 'samAccountName', 'Enabled', 'NTHash', 'ClearTxtPWD'
}
$html_UsersWhoHaveNeverChangedPassword = $UsersWhoHaveNeverChangedPassword |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Users who have had a blank Password ($(@($UsersWhoHaveHadBlankPassword).count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'DisplayName', 'samAccountName', 'Enabled', 'NTHash', 'ClearTxtPWD'
}
$html_UsersWhoHaveHadBlankPassword = $UsersWhoHaveHadBlankPassword |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table'
'PreContent' = "<h2> Users with a blank Password ($(@($UsersWithBlankPassword).count))</h2>"
'MakeTableDynamic' = $true
'MakeHiddenSection' = $false
'TableCssClass' = 'grid'
'Properties' = 'DisplayName', 'samAccountName', 'Enabled', 'NTHash', 'ClearTxtPWD'
}
$html_UsersWithBlankPassword = $UsersWithBlankPassword |
ConvertTo-EnhancedHTMLFragment @params
$params = @{
'CssStyleSheet' = $style
'Title' = 'User Report'
'PreContent' = '<h1>AD User Analysis</h1>'
'HTMLFragments' = @($html_UsersWithWeakPasswords, $html_UserWithTheSamePassword,
$html_UsersWhoHaveReusedTheirPasswords, $html_HistoricallyMostUsedHashes, $html_UsersWhoHaveNeverChangedPassword,
$html_UsersWhoHaveHadBlankPassword, $html_UsersWithBlankPassword )
'jQueryDataTableUri' = 'http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.3/jquery.dataTables.min.js'
'jQueryUri' = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'
}
ConvertTo-EnhancedHTML @params |
Out-File -FilePath $ReportPath
. D:\usertest.html
<#
$params = @{'CssStyleSheet'=$style;
'Title'="System Report for $computer";
'PreContent'="<h1>System Report for $computer</h1>";
'HTMLFragments'=@($html_os,$html_cs,$html_dr,$html_pr,$html_sv,$html_na)}
ConvertTo-EnhancedHTML @params |
Out-File -FilePath $filepath
#>