-
Notifications
You must be signed in to change notification settings - Fork 4
/
Invoke-BadShares.ps1
473 lines (419 loc) · 19.9 KB
/
Invoke-BadShares.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
function New-BadSharesRootDirectory {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$Root = "C:\",
[ValidateNotNullOrEmpty()]
[string]$Name = "BadShares"
)
begin {
$BadSharesRoot = $Root
$BadSharesSharesDirectoryName = "BadShares"
$BadSharesPath = "$BadSharesRoot$BadSharesSharesDirectoryName"
}
process {
Write-Host "If you continue, this script create several new folders and files." -ForegroundColor Yellow
Write-Host "Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[i] Beginning the BadShares setup process..."
} else {
Write-Warning "You need to select y to use BadShares."
break;
}
if (Test-Path -Path $BadSharesPath) {
Write-Warning "The directory '$BadSharesPath' already exists!"
Write-Host "[i] If you continue, this script will overwrite: " -ForegroundColor Yellow -NoNewline
Write-Host "$BadSharesPath" -ForegroundColor Cyan
Write-Host "[i] Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[+] Creating a shares directory at: $BadSharesPath"
try {
New-Item -Path $BadSharesPath -ItemType Directory -Force
} catch {
Write-Error "Could not create the directory '$BadSharesPath'. Please check the path and run again."
}
} else {
Write-Warning "You chose to NOT create a share directory.`nCreate the shares directory manually or run again to create the shares directory."
break;
}
} else {
Write-Host "[+] Creating a shares directory at: $BadSharesPath"
try {
New-Item -Path $BadSharesPath -ItemType Directory
} catch {
Write-Error "Could not create the directory '$BadSharesPath'. Please check the path and run again."
break;
}
}
if (Test-Path -Path $BadSharesPath) {
Write-Host "[i] The shares directory '$BadSharesPath' has been created."
} else {
Write-Error "Could not create the directory '$BadSharesPath'. Please check the path and run again."
break;
}
}
}
function New-SMBSharedFolder {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$Root = "C:\BadShares",
[ValidateNotNullOrEmpty()]
[string[]]$BadShareNames
)
Write-Host "`n[i] Now we will create the SMB Shares"
Write-Host "[i] If you continue, this script will create SMB shares for each BadShare" -ForegroundColor Yellow
Write-Host "[i] Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[+] Creating new SMB shares"
try {
foreach ($Name in $BadShareNames) {
$BadSharePath = "$Root\$Name"
Write-Verbose "Creating directory: $BadSharePath"
try {
New-Item -Path $BadSharePath -ItemType Directory
} catch {
Write-Error "Could not create BadShare: BadSharePath"
}
try {
New-SmbShare -Name $Name -Path $BadSharePath
} catch {
Write-Error "Could not create the SMB Share for $BadSharePath"
}
}
} catch {
Write-Error "Could not create SMB shares."
}
} else {
Write-Warning "You chose to NOT create a the SMB shares.`nCreate the shares manually or run again to create the shares."
break;
}
Write-Host "[i] Finished creating BadShares"
}
function Clear-BadShares {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$Root = "C:\BadShares"
)
if (Test-Path $Root){
try {
$SharedFolders = Get-ChildItem -Path $Root
foreach ($Share in $SharedFolders) {
Remove-SmbShare -Name $Share.Name
}
Remove-Item -Path $Root -Recurse #-Confirm
} catch {
Write-Error "There was a problem clearing existing BadShares"
}
} else {
# BadShares doesn't exist yet
}
}
function New-RandomLastWriteTime {
[CmdletBinding()]
param (
[string]$filePath
)
if (-not (Test-Path $filePath)) {
Write-Error "File '$filePath' does not exist."
return
}
$randomDays = Get-Random -Minimum 0 -Maximum 7300
$timeSpan = New-TimeSpan -Days $randomDays
$currentLastWriteTime = (Get-Item $filePath).LastWriteTime
if ($currentLastWriteTime.Add($timeSpan) -gt (Get-Date)){
Write-Verbose "Trying to set the time to: $($currentLastWriteTime.Add($timeSpan)). Time traveler huh?"
$newLastWriteTime = $currentLastWriteTime.Add(-$timeSpan)
Set-ItemProperty -Path $filePath -Name LastWriteTime -Value $newLastWriteTime
} elseif ($currentLastWriteTime.Add($timeSpan) -lt ((Get-Date).AddYears(-20))) {
Write-Verbose "Trying to set the time to: $($currentLastWriteTime.Add($timeSpan)). How old are you?"
$newLastWriteTime = $currentLastWriteTime.AddYears(20)
Set-ItemProperty -Path $filePath -Name LastWriteTime -Value $newLastWriteTime
} else {
$newLastWriteTime = $currentLastWriteTime.Add($timeSpan)
Set-ItemProperty -Path $filePath -Name LastWriteTime -Value $newLastWriteTime
}
Write-Verbose "Last write time of '$filePath' has been randomly changed to: $newLastWriteTime"
}
function New-RandomFile {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$Path
)
$FileNames = @("Annual Report","Quarterly Review","Marketing Plan","Sales Presentation",
"Financial Statement","Budget Forecast","Invoice 2024","Contract Agreement",
"Employment Contract","Resume John Doe","Cover Letter","Meeting Minutes 2024 04 01",
"Policy Guidelines","Procedure Manual","Project Plan Phase1","Product Catalog",
"Client Proposal","Employee Handbook","Expense Report","Training Materials",
"Feedback Survey","Database Backup","Error Log","Marketing Campaign","Training Schedule",
"Performance Review","Customer List","Service Level Agreement","Vendor Contract",
"Meeting Agenda 2024 04 05","Product Specifications","Purchase Order","Sales Forecast",
"Project Status Report","Expense Budget 2024","Marketing Strategy","Customer Satisfaction Survey",
"Training Manual","Feedback Form","Vendor List","Security Policy","Employee Handbook Updates",
"Performance Appraisal Form","IT Support Request","Risk Assessment","Change Request Form",
"Weekly Timesheet","Customer Service Policy","Product Demo Video")
$RandomFileName = $FileNames | Get-Random
$FileExtensions = @(".txt",".doc",".docx",".pdf",".xlsx",".xls",".pptx",".ppt",".jpg",".jpeg",
".png",".gif",".bmp",".zip",".7z",".rar",".csv",".xml",".html",".css",
".json",".mp3",".mp4",".avi",".mov",".wav",".tiff",".psd",".svg")
$RandomFileExtension = $FileExtensions | Get-Random
$RandomFullFileName = "$Path\$RandomFileName$RandomFileExtension"
$RandomFileSize = Get-Random -Minimum 1 -Maximum 25000
fsutil file createnew $RandomFullFileName $RandomFileSize | Out-Null
New-RandomLastWriteTime -filePath $RandomFullFileName
New-RandomLastWriteTime -filePath $Path
}
function Add-DummyFiles {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[int]$NumberOfDummyFiles = 10,
[ValidateNotNullOrEmpty()]
[string]$Root = "C:\BadShares"
)
$BadSharesRoot = $Root
$BadSharesFolders = Get-ChildItem -Path $BadSharesRoot
Write-Host "`n[i] Now we will create dummy files"
Write-Host "[i] If you continue, this script will create $NumberOfDummyFiles files in each share" -ForegroundColor Yellow
Write-Host "[i] Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[+] Creating dummy files"
try {
foreach ($Share in $BadSharesFolders) {
1..$NumberOfDummyFiles | ForEach-Object {
New-RandomFile -Path $Share.FullName
}
}
} catch {
Write-Error "Could not create dummy files"
}
} else {
Write-Warning "You chose to NOT create the dummy files.`nThe shares will not have dummy files."
break;
}
}
function Test-IsElevated {
<#
.SYNOPSIS
Tests if PowerShell is running with elevated privileges (run as Administrator).
.DESCRIPTION
This function returns True if the script is being run as an administrator or False if not.
.EXAMPLE
Test-IsElevated
.EXAMPLE
if (!(Test-IsElevated)) { Write-Host "You are not running with elevated privileges and will not be able to make any changes." -ForeGroundColor Yellow }
.EXAMPLE
# Prompt to launch elevated if not already running as administrator:
if (!(Test-IsElevated)) {
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
#>
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal $identity
$principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function Set-MiconfiguredBadShares {
[CmdletBinding()]
param (
[object]$ShareList
)
$Groups = @("Everyone", "$($env:USERDOMAIN)\Domain Users", "NT AUTHORITY\Authenticated Users", "NT AUTHORITY\ANONYMOUS LOGON")
$UnsafePermissions = @("FullControl","Write","Modify")
Write-Host "`n[i] Now we will randomly misconfigure the share permissions"
Write-Host "[i] If you continue, this script will modify the permissions on random shares" -ForegroundColor Yellow
Write-Host "[i] Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[+] Modifying share permissions"
try {
1..10 | ForEach-Object {
$RandomSharedFolder = $ShareList | Get-Random
$RandomGroup = $Groups | Get-Random
$RandomUnsafePermission = $UnsafePermissions | Get-Random
$RandomShare = Get-Item $RandomSharedFolder.FullName
$RandomFileACL = (Get-Item $RandomShare).GetAccessControl()
$RandomFileArgs = $RandomGroup, $RandomUnsafePermission, "Allow"
$RandomFileAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $RandomFileArgs
$RandomFileACL.SetAccessRule($RandomFileAccessRule)
$RandomShare.SetAccessControl($RandomFileACL)
}
} catch {
Write-Error "Could not create modify share permissions"
}
} else {
Write-Warning "You chose to NOT modify the share permissions.`nThe shares will not have misconfigured permissions."
break;
}
}
function Set-UnsecuredCredentials {
[CmdletBinding()]
param (
[object]$ShareList
)
$FileNames = @("password.txt","pwd.txt","login.txt","unattend.xml","web.cofig",
"install.ini","passwords.doc","passwords.docx","passwords.xls",
"passwords.xlsx","logins.doc","logins.docx","logins.xls","logins.xlsx",
"install.ps1","ProdBackup.psm1", "ProdBackup.psd1", "adminsetup.vbs",
"admin.bat", "setup.cmd")
$UnsecredCredentials = @("Username: johndoe|Password: Password123","Username: alice_smith|Password: qwerty456",
"Username: admin_user|Password: P@ssw0rd!","Username: user123|Password: SecretPass789",
"Username: test_account|Password: LetMeIn2024","Username: jane_doe|Password: Welcome123",
"Username: developer_user|Password: DevPass@2024","Username: support_user|Password: SupportPass#2024",
"Username: marketing_user|Password: Market123!","Username: operations_user|Password: OpsPass567"
"P@ssw0rd123!", "SecurePass456$", "RandomPass789*", "Str0ngPassword!", "Pa$$w0rd!123", "Secur3P@ss",
"P@ssw0rd2024", "StrongP@ssword!", "Pa$$w0rd!456", "RandomP@ss789","password", "123456", "qwerty",
"abc123", "letmein", "password1", "12345678", "welcome", "admin", "iloveyou", "1234567", "football",
"123123", "monkey", "1234567890", "1234", "123456789", "dragon", "baseball", "sunshine")
Write-Host "`n[i] Now we will create random files with unsecured credentials"
Write-Host "[i] If you continue, this will create random files that contain plaintext passwords" -ForegroundColor Yellow
Write-Host "[i] Do you want to continue [Y] Yes " -ForegroundColor Yellow -NoNewline
Write-Host "[N] " -ForegroundColor Yellow -NoNewline
Write-Host "No: " -ForegroundColor Yellow -NoNewline
$WarningError = ''
$WarningError = Read-Host
if ($WarningError -like 'y') {
Write-Host "`n[+] Creating unsecured credential files"
try {
foreach ($Share in $ShareList.FullName) {
$RandomFileName = $FileNames | Get-Random
New-Item -Path $Share -Name $RandomFileName -Value $($UnsecredCredentials | Get-Random)
}
} catch {
Write-Error "Could not create unsecured credential files"
}
} else {
Write-Warning "You chose to NOT create unsecured credential files.`nThe shares will not have files with passwords in them."
break;
}
}
function Find-BadShares {
[CmdletBinding()]
param (
[String[]]$ShareList
)
foreach ($Share in $ShareList) {
#Write-Host "Checking permissions on $Share"
#$SharedFolder = Get-Item $Share
$Access = (Get-Acl $Share).Access | Where-Object {$_.IdentityReference -match "Domain Users|Authenticated Users|Everyone" -and $_.FileSystemRights -Match "FullControl|Write|Modify" }
foreach ($Ace in $Access) {
$ShareObject = [pscustomobject]@{
SharePath = $Share
Identity = $Ace.IdentityReference
Permissions = $Ace.FileSystemRights
Type = $Ace.AccessControlType
}
}
$ShareObject
}
}
function Find-UnsecuredCredentials {
[CmdletBinding()]
param (
[String[]]$ShareList
)
$Shares = Get-ChildItem $ShareList -Recurse -File
foreach ($Share in $Shares) {
if ($Share.BaseName -Match "password|pwd|login|admin|install|web|unattend|backup|setup"){
$ShareObject = [pscustomobject]@{
SharePath = $Share.FullName
Keyword = $Matches[0]
}
}
$ShareObject
}
}
function Invoke-BadShares {
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string]$Root = "C:\",
[ValidateNotNullOrEmpty()]
[string]$Name = "BadShares",
[ValidateNotNullOrEmpty()]
[string[]]$BadShareList = @("Human Resources","Finance","Marketing","Sales","Information Technology",
"Customer Service","Research and Development","Operations","Legal","Administration","Public Relations",
"Quality Assurance","Supply Chain Management","Product Management","Training and Development","Accounting",
"Business Development","Engineering","Design","Logistics","Purchasing","Risk Management","Compliance",
"Facilities Management","Health and Safety","Internal Audit","Corporate Communications"),
[ValidateNotNullOrEmpty()]
[boolean]$Log = $True,
[ValidateNotNullOrEmpty()]
[string]$LogFile = "$PSScriptRoot\BadSharesLog_$(Get-Date -Format "MMddyyyy-HHmm").log"
)
if (Test-IsElevated) {
# Continue
} else {
Write-Warning -Message "Creating SMB Shares requirs Administrator rights. Please launch an elevated PowerShell session."
break;
}
function Get-Art {
Write-Host " ( " -ForegroundColor Red
Write-Host " ( ( )\ ) ) " -ForegroundColor Red
Write-Host " ( )\ ) )\ ) (()/( ( /( ) ( ( " -ForegroundColor Red
Write-Host " )((_) ( /( (()/( /(_)))\()) ( /( )( ))\ ( " -ForegroundColor Yellow
Write-Host "((_)_ )(_)) ((_))(_)) ((_)\ )(_))(()\ /((_))\ " -ForegroundColor DarkYellow
Write-Host " | _ )((_)_ _| | / __|| |(_)((_)_ ((_)(_)) ((_) " -ForegroundColor Cyan
Write-Host " | _ \/ _`` |/ _`` | \__ \| ' \ / _`` || '_|/ -_)(_-< " -ForegroundColor Cyan
Write-Host " |___/\__,_|\__,_| |___/|_||_|\__,_||_| \___|/__/ " -ForegroundColor Cyan
Write-Host "`n By: Spencer Alessi v0.1 "
}
if ($Log) {
Start-Transcript -Path $LogFile
}
Get-Art
Write-Host "`nWelcome to BadShares!"
Write-Host "BadShares creates file shares with random names and randomly misconfigured permissions."
Write-Host "It also creates unsecured credential files and scatters them randomly throughout the shares.`n"
Write-Warning "DO NOT RUN IN PRODUCTION!"
Write-Host "`nRun " -NoNewline
Write-Host "Clear-BadShares" -ForegroundColor Cyan -NoNewLine
Write-Host " to remove all BadShares.`n"
Write-Host "[i] Clearing any existing BadShares so we can start fresh..."
try { Clear-BadShares -Root $Root$Name} catch {}
# create a BadShare folders, where all our bad shares will live
$BadShareRoot = New-BadSharesRootDirectory
# create SMB shares from our BadShares
$BadShareFolders = New-SMBSharedFolder -Root $BadShareRoot.FullName -BadShareNames $BadShareList
# fill our BadShares with dummy data
Add-DummyFiles -NumberOfDummyFiles 5 -Root $BadShareRoot.FullName
# intentionally misconfigure some of our BadShares
$BadShares = Get-ChildItem $BadShareRoot -Directory
Set-MiconfiguredBadShares -ShareList $BadShares
# fill our BadShares with credential material such as:
# password files: .txt, .doc(x), .xls(x), unattend.xml
# config files: web.config, .config, .ini, .xml
# certificates: .pfx
# scripts: .ps1, .psm1, .psd1, .vbs, .bat, .cmd
$RandomBadShares = $BadShares | Get-Random -Count 10
$UnsecuredCredentials = Set-UnsecuredCredentials -ShareList $RandomBadShares
Write-Host "`n[+] BadShares has finished! Displaying results..." -ForegroundColor Cyan
Find-BadShares -ShareList $BadShares.FullName | ft -AutoSize
Find-UnsecuredCredentials -ShareList $BadShares.FullName | Sort-Object -Unique -Property SharePath
if ($Log) {
""
Stop-Transcript
}
}