-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-WDACTrustDB.psm1
410 lines (372 loc) · 16.8 KB
/
New-WDACTrustDB.psm1
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
$ThisIsASignedModule = $false
if ((Split-Path (Get-Item $PSScriptRoot) -Leaf) -eq "SignedModules") {
$PSModuleRoot = Join-Path $PSScriptRoot -ChildPath "..\"
$ThisIsASignedModule = $true
} else {
$PSModuleRoot = $PSScriptRoot
}
if (Test-Path (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\JSON-LocalStorageTools.psm1")) {
Import-Module (Join-Path $PSModuleRoot -ChildPath "SignedModules\Resources\JSON-LocalStorageTools.psm1")
} else {
Import-Module (Join-Path $PSModuleRoot -ChildPath "Resources\JSON-LocalStorageTools.psm1")
}
function New-WDACTrustDB {
<#
.SYNOPSIS
Creates a new WDAC trust database (storing audit events, trusted files, and groups / devices)
.DESCRIPTION
Creates a SQlite database in the specified destination and creates the relevant tables to store code integrity events / trusted files, certificates, and information about groups and devices within those groups.
Author: Nathan Jepson
License: MIT License
.PARAMETER DBName
Specifies the name of the database. Default name is "trust.db"
.PARAMETER Destination
Destination directory to save the new database.
.PARAMETER SqliteAssembly
Specify the filepath of the Sqlite .dll, in order to set / reset the SqliteAssembly local variable in the PowerShell module resources folder.
.EXAMPLE
Add-WDACTrustDB -Destination "C:\Users\JohnSmith\Documents\WDAC_Folder\" -SqliteAssembly "C:\Sqlite\sqlite-netFx46-binary-x64-2015-1.0.118.0\System.Data.SQLite.dll"
.EXAMPLE
Add-WDACTrustDB -Destination ".\" -SqliteAssembly "C:\Sqlite\sqlite-netFx46-binary-x64-2015-1.0.118.0\System.Data.SQLite.dll" -DBName "WDAC_Trust.db"
.EXAMPLE
Add-WDACTrustDB -Destination "C:\Users\JohnSmith\Documents\WDAC_Folder\"
#>
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$DBName = "trust.db",
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Destination,
[ValidateNotNullOrEmpty()]
[string]$SqliteAssembly
)
if ($ThisIsASignedModule) {
Write-Verbose "The current file is in the SignedModules folder."
}
if (-not $SqliteAssembly) {
try {
$SqliteAssembly = (Get-LocalStorageJSON)."SqliteAssembly"
if (-not $SqliteAssembly) {
throw "No valid value for Sqlite binary provided from local storage."
}
} catch {
Write-Error $_
Write-Warning "Unable to read or process the file path for the Sqlite binary."
return
}
}
$MakeTables = @'
CREATE TABLE apps (
SHA256FlatHash TEXT PRIMARY KEY,
FileName TEXT NOT NULL,
TimeDetected TEXT NOT NULL,
FirstDetectedPath TEXT NOT NULL,
FirstDetectedUser TEXT,
FirstDetectedProcessID Integer,
FirstDetectedProcessName TEXT NOT NULL,
SHA256AuthenticodeHash TEXT NOT NULL,
SHA1AuthenticodeHash TEXT,
SHA256PageHash TEXT,
SHA1PageHash TEXT,
SHA256SipHash Text,
OriginDevice TEXT NOT NULL,
EventType Text,
SigningScenario Text,
OriginalFileName Text,
FileVersion Text,
InternalName Text,
FileDescription Text,
ProductName Text,
PackageFamilyName Text,
UserWriteable Integer DEFAULT 0 NOT NULL,
FailedWHQL Integer,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Skipped Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
BlockingPolicyID Text COLLATE NOCASE,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
AppIndex Integer UNIQUE NOT NULL,
RequestedSigningLevel Text,
ValidatedSigningLevel Text,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT
);
CREATE TABLE msi_or_script (
SHA256FlatHash TEXT PRIMARY KEY,
SHA1FlatHash TEXT,
TimeDetected TEXT NOT NULL,
FirstDetectedPath TEXT NOT NULL,
FirstDetectedUser TEXT,
FirstDetectedProcessID Integer,
SHA256AuthenticodeHash TEXT,
SHA256SipHash Text,
UserWriteable Integer DEFAULT 0 NOT NULL,
Signed Text,
OriginDevice TEXT NOT NULL,
EventType Text,
AppIndex Integer UNIQUE NOT NULL,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Skipped Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
BlockingPolicyID Text COLLATE NOCASE,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(BlockingPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT
);
CREATE TABLE signers (
AppIndex INTEGER NOT NULL,
SignatureIndex INTEGER NOT NULL,
CertificateTBSHash TEXT NOT NULL,
SignatureType TEXT,
PageHash Integer,
Flags Integer,
PolicyBits Integer,
ValidatedSigningLevel Text,
VerificationError Text,
FOREIGN KEY(AppIndex) REFERENCES apps(AppIndex) ON DELETE CASCADE,
FOREIGN KEY(CertificateTBSHash) REFERENCES certificates(TBSHash) ON DELETE CASCADE,
PRIMARY KEY(AppIndex,SignatureIndex)
);
CREATE TABLE signers_msi_or_script (
AppIndex INTEGER NOT NULL,
SignatureIndex INTEGER NOT NULL,
CertificateTBSHash TEXT NOT NULL,
FOREIGN KEY(AppIndex) REFERENCES msi_or_script(AppIndex) ON DELETE CASCADE,
FOREIGN KEY(CertificateTBSHash) REFERENCES certificates(TBSHash) ON DELETE CASCADE,
PRIMARY KEY(AppIndex,SignatureIndex)
);
CREATE TABLE certificates (
TBSHash Text PRIMARY KEY,
CommonName Text NOT NULL,
ParentCertTBSHash Text,
NotValidBefore Text,
NotValidAfter Text,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
BlockingPolicyID Text COLLATE NOCASE,
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(BlockingPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(ParentCertTBSHash) REFERENCES certificates(TBSHash) ON DELETE SET NULL
);
CREATE TABLE publishers (
LeafCertCN Text NOT NULL,
PcaCertTBSHash Text NOT NULL,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
BlockingPolicyID Text COLLATE NOCASE,
PublisherIndex Integer UNIQUE NOT NULL,
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(BlockingPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(PcaCertTBSHash) REFERENCES certificates(TBSHash) ON DELETE CASCADE,
PRIMARY KEY(PcaCertTBSHash,LeafCertCN)
);
CREATE TABLE file_publishers (
PublisherIndex Integer NOT NULL,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
BlockingPolicyID Text COLLATE NOCASE,
MinimumAllowedVersion Text NOT NULL,
MaximumAllowedVersion Text,
FileName Text NOT NULL,
SpecificFileNameLevel Text NOT NULL,
PRIMARY KEY(PublisherIndex,FileName,MinimumAllowedVersion,SpecificFileNameLevel),
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(BlockingPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(PublisherIndex) REFERENCES publishers(PublisherIndex) ON DELETE CASCADE
);
CREATE TABLE file_names (
FileName Text NOT NULL,
SpecificFileNameLevel Text NOT NULL,
Untrusted Integer DEFAULT 0 NOT NULL,
TrustedDriver Integer DEFAULT 0 NOT NULL,
TrustedUserMode Integer DEFAULT 0 NOT NULL,
Staged Integer DEFAULT 0 NOT NULL,
Revoked Integer DEFAULT 0 NOT NULL,
Deferred Integer DEFAULT 0 NOT NULL,
Blocked Integer DEFAULT 0 NOT NULL,
AllowedPolicyID Text COLLATE NOCASE,
DeferredPolicyIndex Integer,
Comment Text,
BlockingPolicyID Text COLLATE NOCASE,
PRIMARY KEY(FileName,SpecificFileNameLevel),
FOREIGN KEY(AllowedPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(BlockingPolicyID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT
);
CREATE TABLE file_publisher_options (
FileName Text NOT NULL,
PublisherIndex Integer NOT NULL,
VersioningType Integer NOT NULL,
MinimumAllowedVersionPivot Text,
MinimumTolerableMinimum Text,
SpecificFileNameLevel Text NOT NULL,
FOREIGN KEY(PublisherIndex) REFERENCES publishers(PublisherIndex) ON DELETE CASCADE,
PRIMARY KEY(PublisherIndex,FileName,SpecificFileNameLevel)
);
CREATE TABLE policy_file_publisher_options (
FileName Text NOT NULL,
PublisherIndex Integer NOT NULL,
PolicyGUID Text NOT NULL COLLATE NOCASE,
MinimumAllowedVersionPivot Text,
MinimumTolerableMinimum Text,
SpecificFileNameLevel Text NOT NULL,
FOREIGN KEY(PolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT,
FOREIGN KEY(PublisherIndex) REFERENCES publishers(PublisherIndex) ON DELETE CASCADE,
PRIMARY KEY (FileName,PublisherIndex,PolicyGUID,SpecificFileNameLevel)
);
CREATE TABLE policy_versioning_options (
PolicyGUID Text COLLATE NOCASE PRIMARY KEY,
VersioningType Integer NOT NULL,
FOREIGN KEY(PolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE CASCADE
);
CREATE TABLE groups (
GroupName Text PRIMARY KEY
);
CREATE TABLE group_mirrors (
GroupName Text NOT NULL,
MirroredGroupName Text NOT NULL,
PRIMARY KEY(GroupName,MirroredGroupName),
FOREIGN KEY(MirroredGroupName) REFERENCES groups(GroupName) ON DELETE CASCADE,
FOREIGN KEY(GroupName) REFERENCES groups(GroupName) ON DELETE CASCADE
);
CREATE TABLE policies (
PolicyGUID Text NOT NULL COLLATE NOCASE PRIMARY KEY,
PolicyID Text,
PolicyHash Text,
PolicyName Text UNIQUE NOT NULL,
PolicyVersion Text NOT NULL,
ParentPolicyGUID Text COLLATE NOCASE,
BaseOrSupplemental INTEGER DEFAULT 0 NOT NULL,
IsSigned Integer DEFAULT 0 NOT NULL,
AuditMode Integer DEFAULT 1 NOT NULL,
IsPillar Integer DEFAULT 0 NOT NULL,
OriginLocation Text NOT NULL,
OriginLocationType Text NOT NULL,
LastDeployedPolicyVersion Text,
LastSignedVersion Text,
LastUnsignedVersion Text,
DeployedSigned Integer DEFAULT 0 NOT NULL,
FOREIGN KEY (ParentPolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT
);
CREATE TABLE policy_assignments (
GroupName Text NOT NULL,
PolicyGUID Text NOT NULL COLLATE NOCASE,
PRIMARY KEY(GroupName,PolicyGUID),
FOREIGN KEY(GroupName) REFERENCES groups(GroupName) ON DELETE RESTRICT,
FOREIGN KEY(PolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT
);
CREATE TABLE ad_hoc_policy_assignments (
PolicyGUID Text NOT NULL COLLATE NOCASE,
DeviceName Text NOT NULL COLLATE NOCASE,
PRIMARY KEY(PolicyGUID,DeviceName),
FOREIGN KEY(DeviceName) REFERENCES devices(DeviceName) ON DELETE CASCADE,
FOREIGN KEY(PolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE RESTRICT
);
CREATE TABLE devices (
DeviceName Text COLLATE NOCASE PRIMARY KEY,
AllowedGroup Text,
UpdateDeferring Integer DEFAULT 0,
processor_architecture Text,
FOREIGN KEY(AllowedGroup) REFERENCES groups(GroupName) ON DELETE RESTRICT
);
CREATE TABLE deferred_policies (
DeferredPolicyIndex Integer PRIMARY KEY,
DeferredDevicePolicyGUID Text NOT NULL COLLATE NOCASE,
PolicyVersion Text,
IsSigned Integer DEFAULT 0 NOT NULL
);
CREATE TABLE deferred_policies_assignments (
DeferredPolicyIndex Integer NOT NULL,
DeviceName Text NOT NULL COLLATE NOCASE,
comment Text,
FOREIGN KEY(DeferredPolicyIndex) REFERENCES deferred_policies(DeferredPolicyIndex) ON DELETE RESTRICT,
FOREIGN KEY(DeviceName) REFERENCES devices(DeviceName) ON DELETE RESTRICT,
PRIMARY KEY(DeferredPolicyIndex,DeviceName)
);
CREATE TABLE first_signed_policy_deployments (
PolicyGUID Text NOT NULL COLLATE NOCASE,
DeviceName Text NOT NULL COLLATE NOCASE,
PRIMARY KEY(PolicyGUID,DeviceName),
FOREIGN KEY(DeviceName) REFERENCES devices(DeviceName) ON DELETE CASCADE,
FOREIGN KEY(PolicyGUID) REFERENCES policies(PolicyGUID) ON DELETE CASCADE
);
'@
$Database = Join-Path -Path $Destination -ChildPath $DBName
if (Test-Path $Database) {
Write-Warning "Database already exists.";
return;
}
try {
#This could throw off some EDR or anti-virus solutions
[Reflection.Assembly]::LoadFile($SqliteAssembly)
} catch [NotSupportedException] {
Write-Verbose $_
Write-Error "This Sqlite binary is not supported in this version of PowerShell.";
return;
} catch {
Write-Verbose $_
Write-Error "Could not load the Sqlite binary. Failure to create database.";
return;
}
try {
New-Item -Path $Destination -Name $DBName -ErrorAction Stop | Out-Null
$sDatabaseConnectionString=[string]::Format("data source={0};Foreign Key Constraints=On",$Database)
$oSQLiteDBConnection = New-Object System.Data.SQLite.SQLiteConnection
$oSQLiteDBConnection.ConnectionString = $sDatabaseConnectionString
$oSQLiteDBConnection.open()
$oSQLiteDBCommand=$oSQLiteDBConnection.CreateCommand()
$oSQLiteDBCommand.Commandtext=$MakeTables
$oSQLiteDBCommand.CommandType = [System.Data.CommandType]::Text
$oSQLiteDBCommand.ExecuteNonQuery()
$oSQLiteDBConnection.close()
Remove-Variable oSQLiteDBConnection -ErrorAction SilentlyContinue
Write-Host "New trust database created successfully." -ForegroundColor Green
} catch {
Write-Verbose ($_ | Format-List -Property * | Out-String)
throw $_
}
}
Export-ModuleMember -Function New-WDACTrustDB