-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppLockerInventory.ps1
259 lines (197 loc) · 5.89 KB
/
AppLockerInventory.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
#requires -version 2
<#
.SYNOPSIS
AppLocker Inventory
.DESCRIPTION
List files on computer/folder and execute cmdlet "Get-AppLockerFileInformation" on listed files. By defaut a full scan is launched.
.PARAMETER FilesList
Files list from a previous scan.
.PARAMETER ScanName
Tag on the ouput files name.
.PARAMETER FolderToScan
Folder to scan. Can be used for scanning a specific application.
.OUTPUTS
Files: List of file on computer/folder.
Inventory: List of AppLocker file information collected.
.NOTES
Version: 0.1
Author: Erforschr
License: MIT License
Creation Date: 01/08/2019
.EXAMPLE
.\AppLockerInventory.ps1
.EXAMPLE
.\AppLockerInventory.ps1 -Fileslist .\Files_<Computer>_FullScan_2019-XX-XX_XX-XX.txt
.EXAMPLE
.\AppLockerInventory.ps1 -ScanName Oracle -FolderToScan 'C:\Program Files\Oracle'
.EXAMPLE
.\AppLockerInventory.ps1 -ScanName Oracle -FilesList .\Files_<Computer>_Oracle_2019-XX-XX_XX-XX.txt
#>
#---[Parameters]-----------------------------------------------------------------------------------
Param (
[ValidateScript({
$FileObj = Get-Item $_
If(-Not ($FileObj.FullName | Test-Path -PathType 'Container'))
{
throw "Parameter 'FolderToScan' is not a valid path"
}
return $true
})]
[System.IO.FileInfo]
$FolderToScan
,
[String]
$ScanName = 'Scan'
,
[ValidateScript({
$FileObj = Get-Item $_
If(-Not ($FileObj.FullName | Test-Path -PathType 'Leaf'))
{
throw "File not found"
}
If (-Not ($FileObj.FullName -Like '*.txt'))
{
throw "File is not a .txt file"
}
return $true
})]
[System.IO.FileInfo]
$FilesList
)
#---[Imports and preferences]----------------------------------------------------------------------
Import-Module AppLocker
$ErrorActionPreference = 'Stop' # Continue SilentlyContinue Stop
#---[Variables]------------------------------------------------------------------------------------
$Date = $(Get-Date).ToString("yyyy-MM-dd_HH-mm")
$ScriptPath = $(Split-Path -parent $MyInvocation.MyCommand.Definition)
$ComputerName = $env:computername
#---[Inventory files]------------------------------------------------------------------
# Get list of folders to scan
$Folders = New-Object System.Collections.ArrayList
If ($FolderToScan)
{
$t = $Folders.Add($FolderToScan)
}
Else
{
$Drives = Get-PSDrive | Select-Object -ExpandProperty 'Name' | Select-String -Pattern '^[a-z]$'
ForEach ($Drive in $Drives)
{
$Folder = [System.String]::Format("{0}:\",$Drive)
$t = $Folders.Add($Folder)
}
}
# Scan for files
If ($FilesList)
{
Write-Host "Skipping folders scan"
}
Else
{
Write-Host "Scanning folders"
If ($FolderToScan)
{
$Files = [System.String]::Format("{0}\{1}_{2}_{3}_{4}.txt", $ScriptPath, 'Files', $ComputerName, $ScanName, $Date)
}
Else
{
$Files = [System.String]::Format("{0}\{1}_{2}_{3}_{4}.txt", $ScriptPath, 'Files', $ComputerName, 'FullScan', $Date)
}
ForEach ($Folder in $Folders)
{
If ($Folder)
{
Write-Host "Scanning $Folder"
$Command = [System.String]::Format("cmd.exe /c 'dir ""{0}"" /s /b /a >> {1}'",$Folder, $Files)
Invoke-Expression -Command $Command
}
}
}
# Count listed files
$FileCounter = 0
If ($FilesList)
{
$FilesListObj = Get-Item $FilesList
$SRFiles = New-Object System.IO.StreamReader ($FilesListObj)
}
Else
{
$SRFiles = New-Object System.IO.StreamReader ($Files)
}
While ($RLine = $SRFiles.ReadLine())
{
$FileCounter += 1
}
Write-Host "Files listed: $FileCounter"
Write-Host ""
#---[Collect AppLocker information]----------------------------------------------------------------
Write-Host "Collecting AppLocker information"
$Inventory = [System.String]::Format("{0}\{1}_{2}_{3}_{4}.csv", $ScriptPath, 'Inventory', $ComputerName, $ScanName, $Date)
If ($FolderToScan)
{
$Inventory = [System.String]::Format("{0}\{1}_{2}_{3}_{4}.csv", $ScriptPath, 'Inventory', $ComputerName, $ScanName, $Date)
}
Else
{
$Inventory = [System.String]::Format("{0}\{1}_{2}_{3}_{4}.csv", $ScriptPath, 'Inventory', $ComputerName, 'FullScan', $Date)
}
$SWInventory = New-Object System.IO.StreamWriter ($Inventory)
$SWInventory.AutoFlush = $True
$Line = [System.String]::Format("""{0}"";""{1}"";""{2}"";""{3}"";""{4}"";""{5}"";""{6}"";""{7}""",
"FullName",
"Extension",
"Length",
"Path",
"Publisher",
"Hash",
"SourceFileName",
"AppX")
If ($FilesList)
{
$FilesListObj = Get-Item $FilesList
$SRFiles = New-Object System.IO.StreamReader ($FilesListObj)
}
Else
{
$SRFiles = New-Object System.IO.StreamReader ($Files)
}
$FileCounter = 0
$ErrorCounter = 0
While ($RLine = $SRFiles.ReadLine())
{
Try
{
$File = Get-Item $RLine
If ($File.Extension)
{
$AppLockerInfo = Get-AppLockerFileInformation -Path $File.FullName
$Line = [System.String]::Format("""{0}"";""{1}"";""{2}"";""{3}"";""{4}"";""{5}"";""{6}"";""{7}""",
$File.FullName,
$File.Extension,
$File.Length,
$AppLockerInfo.Path,
$AppLockerInfo.Publisher,
$AppLockerInfo.Hash.HashDataString,
$AppLockerInfo.Hash.SourceFileName,
$AppLockerInfo.AppX
)
$SWInventory.WriteLine($Line)
}
$FileCounter += 1
}
Catch
{
$ErrorCounter += 1
}
If ($FileCounter % 500 -eq 0) {
Write-Host -NoNewLine "`rFile info collected: $FileCounter"
}
}
Write-Host -NoNewLine "`rFiles info collected: $FileCounter"
Write-Host "`r`nErrors: $ErrorCounter"
Write-Host ""
$SRFiles.Close()
$SWInventory.Close()
Write-Host "Files list done: $Files"
Write-Host "Inventory done: $Inventory"
Write-Host ""