-
Notifications
You must be signed in to change notification settings - Fork 53
/
Lnk-Sweeper.ps1
547 lines (442 loc) · 20 KB
/
Lnk-Sweeper.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<#
.SYNOPSIS
Manage windows shortcut (.LNK) artifacts.
Author: @r00t-3xp10it
Tested Under: Windows 10 (19043) x64 bits
Required Dependencies: none
Optional Dependencies: lnk_parser_cmd.exe {auto-download}
PS cmdlet Dev version: v3.4.18
.DESCRIPTION
Auxiliary module of cleantracks cmdlet to manage link (.LNK) files from sellected directory recursive.
This cmdlet allow users to search for .LNK files \ display .LNK metadata \ Delete .LNK files recursive.
.NOTES
This cmdlet stores the current Date\Time values to be compared againts .lnk files found timestamp,
and delete them if the current date match the .lnk file CreationTime. By default it scans %Recent%
directory recursive unless user inputs another directory using -Directory <'absoluct-path'> argument.
If invoked -Action 'query' argument then cmdlet will only search (not deleting any artifacts found).
If invoked -Forensic 'True' argument then cmdlet parses all .LNK artifacts metadata onscreen (console).
If invoked -TimeStamp 'Year' then it searches for .LNK files with the sellected 'Year' TimeStamp attrib.
WARNING: Depending of the -Directory <'path'> sellection, this function migth broken explorer shortcuts if
invoked -TimeStamp based on <'Year'> only. Example: If invoked -Directory "$Env:USERPROFILE" -TimeStamp "2021"
the cmdlet will delete all shortcuts files recursive of %USERPROFILE% because -Directory function works recursive.
.Parameter Action
Accepts arguments: Clean, Query (default: query)
.Parameter TimeStamp
Accepts arguments: Today, Year (default: today)
.Parameter Forensic
Accepts arguments: True, False (default: false)
.Parameter PSBanner
Accepts arguments: True, False (default: true)
.Parameter Directory
Directory to scan recursive (default: $Env:APPDATA\Microsoft\Windows\Recent)
.Parameter Paranoid
Scan pre-defined directorys insted of the default one? (default: false)
.Parameter OutLog
Export cmdlet data\metadata found to logfile? (default: false)
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Action query
Search recursive for .LNK files of default Directory
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Action clean
Delete recursive all .LNK files of default Directory
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Action query -OutLog true
Search recursive for .LNK and export data to logfile
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Action clean -Directory "$Env:WINDIR\WinSxS"
Delete recursive all .LNK files of -Directory <'path'> with 'today' timestamp.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -action query -Directory "$Env:USERPROFILE" -TimeStamp "1999"
Search recursive for .LNK files of -Directory <'path'> with '1999' year timestamp.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -action clean -Directory "$Env:USERPROFILE\Desktop" -TimeStamp Year
Delete recursive all .LNK files of -Directory <'path'> with current 'Year' timestamp.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -action query -Directory "$Env:WINDIR\WinSxS" -TimeStamp "17/12/2021" -Forensic true
Search recursive for .LNK files of -Directory <'path'> with 'sellected' timestamp and parse the .LNK metadata.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Action query -Directory "$Env:TMP" -TimeStamp "today" -Forensic true -OutLog true
Search recursive for .LNK files of -Directory <'path'> with 'sellected' timestamp and export metadata to logfile
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -action query -Paranoid True -TimeStamp "17/12/2021" -Forensic true
Search recursive for .LNK files on 'pre-defined dirs' with 'sellected' timestamp and parse .LNK metadata.
.INPUTS
None. You cannot pipe objects into Lnk-Sweeper.ps1
.OUTPUTS
[-] Cleaning: windows shortcut (.LNK) artifacts.
------------------------------------------------------------------
Directory: C:\Users\pedro\AppData\Roaming\Microsoft\Windows\Recent
TimeStamp: 17/12/2021
Forensic : false
------------------------------------------------------------------
deleted : C:\Users\pedro\AppData\Roaming\Microsoft\Windows\Recent\Artifact.lnk
deleted : C:\Users\pedro\AppData\Roaming\Microsoft\Windows\Recent\SKYNET-20211214-2204.lnk
deleted : C:\Users\pedro\AppData\Roaming\Microsoft\Windows\Recent\ms-settingswindowsupdate.lnk
.LINK
https://shorturl.at/eqJK6
https://www.briancarr.org/post/lnk-files
https://belkasoft.com/forensic-analysis-of-lnk-files
https://blog.nviso.eu/2017/04/04/tracking-threat-actors-through-lnk-files
#>
[CmdletBinding(PositionalBinding=$false)] param(
[string]$Directory="$Env:APPDATA\Microsoft\Windows\Recent",
[string]$timeStamp="today",
[string]$Paranoid="false",
[string]$Forensic="false",
[string]$PSBanner="true",
[string]$Bypass="false",
[string]$OutLog="false",
[string]$Action="query"
)
$Count = 0
#Current date values
$MyDate = (Date).Day
$MyYear = (Date).Year
$MyMonth = (Date).Month
$MyStorage = ($pwd).Path
#LocationsToScanList
$LocationsToScanList = @(
"$Env:USERPROFILE",
"$Env:WINDIR\System32",
"$Env:APPDATA\Microsoft\Windows\Recent"
)
$ErrorActionPreference = "SilentlyContinue"
## Disable Powershell Command Logging for current session.
Set-PSReadlineOption –HistorySaveStyle SaveNothing|Out-Null
$host.UI.RawUI.WindowTitle = "@Lnk-Sweeper v3.4.18 {SSA@RedTeam}"
$Banner = @"
__ __ __ __ __ ______ __ __ ______ ______ ______ ______ ______
/\ \ /\ "-.\ \ /\ \/ / /\ ___\ /\ \ _ \ \ /\ ___\ /\ ___\ /\ == \/\ ___\ /\ == \
\ \ \____\ \ \-. \\ \ _"-. \ \___ \\ \ \/ ".\ \\ \ __\ \ \ __\ \ \ _-/\ \ __\ \ \ __<
\ \_____\\ \_\\"\_\\ \_\ \_\ \/\_____\\ \__/".~\_\\ \_____\\ \_____\\ \_\ \ \_____\\ \_\ \_\
\/_____/ \/_/ \/_/ \/_/\/_/ \/_____/ \/_/ \/_/ \/_____/ \/_____/ \/_/ \/_____/ \/_/ /_/
"@;
If($PSBanner -ieq "True")
{
Clear-Host
Write-Host "$Banner" -ForegroundColor Blue
}
If($Action -ieq "clean")
{
$CharItem = " deleted :"
Write-Host "[-] Cleaning: Windows shortcut (.LNK) artifacts." -ForegroundColor Blue
}
Else
{
$CharItem = " artifact:"
Write-Host "[-] QueryFor: Windows shortcut (.LNK) artifacts." -ForegroundColor Blue
}
If($OutLog -ieq "true" -and $Forensic -ieq "false")
{
#Build report logfile in current directory
echo "[Lnk-Sweeper] artifacts list" > Lnk-Sweeper.txt
echo "----------------------------" >> Lnk-Sweeper.txt
}
If($TimeStamp -ieq "Today" -or $TimeStamp -Match '^(\d+|\d+\d+)/(\d+|\d+\d+)/(\d+\d+\d+\d+)$')
{
<#
.SYNOPSIS
Author: @r00t-3xp10it
Helper - Search\Delete .LNK files with current 'Day/Month/Year' set in TimeStamp.
.NOTES
-TimeStamp parameter accepts string 'today' or date in 'dd/mm/yyyy' format.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -TimeStamp "today"
Use 'todays' date to search for .LNK files.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -TimeStamp "17/12/2021"
Use 'sellected' date to search for .LNK files.
#>
If($TimeStamp -Match '^(\d+|\d+\d+)/(\d+|\d+\d+)/(\d+\d+\d+\d+)$')
{
#parsing timestamp value data!
$MyDate = $TimeStamp.Split('/')[0]
$MyYear = $TimeStamp.Split('/')[2]
$MyMonth = $TimeStamp.Split('/')[1]
}
If($Paranoid -ieq "true")
{
<#
.SYNOPSIS
Author: @r00t-3xp10it
Helper - CleanTracks - Paranoid function!
.NOTES
This function its trigger by setting -Paranoid parameter to 'true'.
It will make the cmdlet to scan a list of pre-defined directorys plus
the -Directory input by user insted of just scanning the default one.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Paranoid True
Scan pre-defined directorys insted of the default one.
#>
If($Directory -iNotMatch "^(C:\\Users\\$Env:USERNAME|C:\\Windows\\System32|C:\\Users\\$Env:USERNAME\\AppData\\Roaming\\Microsoft\\Windows\\Recent)$")
{
#Add -Directory user input entry to ScanList.
$LocationsToScanList += "$Directory"
}
}
ElseIf($Paranoid -ieq "false")
{
#Add only one entry to ScanList.
$LocationsToScanList = "$Directory"
}
ForEach($Token in $LocationsToScanList)
{
#Display cmdlet settings onscreen.
Write-Host " ------------------------------------------------------------------"
Write-Host " Directory: $Token"
Write-Host " TimeStamp: ${MyDate}/${MyMonth}/${MyYear}"
Write-Host " Forensic : $Forensic"
If($OutLog -ieq "true")
{
Write-Host " LogFile : $MyStorage\Lnk-Sweeper.txt" -ForegroundColor Yellow
}
Write-Host " ------------------------------------------------------------------"
Start-Sleep -Milliseconds 400
#-and $_.FullName -iNotMatch "($ForbiddenLocation)"
#Search fo all Link artifacts recursive starting on sellected directory!
Get-ChildItem -Path "$Token" -Include *.lnk -Recurse | Select-Object * | Where-Object {
$_.CreationTime.Day -Match "$MyDate" -and $_.CreationTime.Month -Match "$MyMonth" -and
$_.CreationTime.Year -Match "$MyYear" -and $_.FullName -iNotMatch "$Env:USERNAME\\Links" } | ForEach-Object {
If($Forensic -ieq "True")
{
<#
.SYNOPSIS
Author: https://code.google.com/archive/p/lnk-parser
Helper - Parse .LNK file metadata onscreen (terminal)
.OUTPUTS
[Distributed Link Tracker Properties]
Version: 0
NetBIOS name: desktop-unlaahq
Droid volume identifier: fbadb152-faa6-4cd4-a8e7-56df01695110
Droid file identifier: a4fef014-7ac4-11eb-b11c-409f383199e6
Birth droid volume identifier: fbadb152-faa6-4cd4-a8e7-56df01695110
Birth droid file identifier: a4fef014-7ac4-11eb-b11c-409f383199e6
MAC address: 40:9f:38:31:99:e6
UUID timestamp: 03/01/2021 (19:30:56.528) [UTC]
UUID sequence number: 12572
#>
If(-not(Test-Path -Path "lnk_parser_cmd.exe" -ErrorAction SilentlyContinue))
{
#Download lnk_parser from code.google.com
iwr -Uri "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/lnk-parser/lnk_parser_cmd.exe" -OutFile "lnk_parser_cmd.exe"|Out-Null
Unblock-File -Path lnk_parser_cmd.exe #Unblock the binary file downloaded!
}
$Count++
Write-Host ""
#Run lnk_parser locally
If($OutLog -ieq "true")
{
.\lnk_parser_cmd.exe -r $_.FullName
#Export lnk files found metadata to logfile
.\lnk_parser_cmd.exe -r $_.FullName >> Lnk-Sweeper.txt
}
Else
{
#Display onscreen the .LNK metadata.
.\lnk_parser_cmd.exe -r $_.FullName
}
Start-Sleep -Milliseconds 400
}
Else
{
$Count++
If($OutLog -ieq "true")
{
#Display onscreen the .LNK files found.
Write-Host "$CharItem"$_.FullName
#Export results to logfile
$_.FullName >> Lnk-Sweeper.txt
}
Else
{
#Display onscreen the .LNK files found.
Write-Host "$CharItem"$_.FullName
}
Start-Sleep -Milliseconds 400
}
If($Action -ieq "clean")
{
#Delete the .LNK artifact found.
Remove-Item -Path $_.FullName -Force
}
}
}#EndOfForEach()
}
ElseIf($TimeStamp -ieq "Year" -or $TimeStamp -Match '^(\d+\d+\d+\d+)$')
{
<#
.SYNOPSIS
Author: @r00t-3xp10it
Helper - Search\Delete .LNK files with current 'Year' in TimeStamp.
.NOTES
Warning: Depending of the -Directory tree sellection, this function
migth broken explorer shortcuts if invoked -TimeStamp based on 'Year'
Example: If invoked -Directory "$Env:USERPROFILE" -TimeStamp "2021"
Then cmdlet will delete all shortcut .LNK files set by username acc
because -Directory parameter works recursive on the input directory.
.NOTES
-TimeStamp parameter accepts string 'year' or '2021' date formats.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -TimeStamp "Year"
Use 'current year' date to search for .LNK files.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -TimeStamp "2021"
Use 'sellected year' date to search for .LNK files.
#>
If($TimeStamp -Match '^(\d+\d+\d+\d+)$')
{
$MyYear = "$TimeStamp"
}
If($Paranoid -ieq "true")
{
<#
.SYNOPSIS
Author: @r00t-3xp10it
Helper - CleanTracks - Paranoid function!
.NOTES
This function its trigger by setting -Paranoid parameter to 'true'.
It will make the cmdlet to scan a list of pre-defined directorys plus
the -Directory input by user insted of just scanning the default one.
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Paranoid True
Scan pre-defined directorys insted of the default one.
#>
If($Directory -iNotMatch "^(C:\\Users\\$Env:USERNAME|C:\\Windows\\System32|C:\\Users\\$Env:USERNAME\\AppData\\Roaming\\Microsoft\\Windows\\Recent)$")
{
#Add -Directory user input entry to ScanList.
$LocationsToScanList += "$Directory"
}
}
ElseIf($Paranoid -ieq "false")
{
#Add only one entry to ScanList.
$LocationsToScanList = "$Directory"
}
ForEach($Token in $LocationsToScanList)
{
If($Bypass -ieq "false")
{
<#
.SYNOPSIS
Author: @r00t-3xp10it
Helper - Bypass 'Year' timestamp deletion restriction!
.EXAMPLE
PS C:\> .\Lnk-Sweeper.ps1 -Bypass "true"
Bypass 'Year' timestamp deletion restriction!
#>
If($Action -ieq "clean" -and $LocationsToScanList -iMatch "^(C:\\Users\\$Env:USERNAME|C:\\Users\\$Env:USERNAME\\Links|C:\\Windows)$")
{
Write-Host "[x] Forbiden: Recurse delete lnk files from '$Token' only with 'YEAR' in timestamp!" -ForegroundColor Red -BackgroundColor Black
Start-Sleep -Seconds 1;Write-Host " Help : Invoke cmdlet again with -bypass 'true' argument to bypass restriction .." -ForegroundColor Yellow
exit #Exit @Lnk-Sweeper
}
}
#Display cmdlet settings onscreen.
Write-Host " ------------------------------------------------------------------"
Write-Host " Directory: $Token"
Write-Host " TimeStamp: ${MyYear}"
Write-Host " Forensic : $Forensic"
If($OutLog -ieq "true")
{
Write-Host " LogFile : $MyStorage\Lnk-Sweeper.txt" -ForegroundColor Yellow
}
Write-Host " ------------------------------------------------------------------"
Start-Sleep -Milliseconds 400
#Search fo all Link artifacts recursive starting on sellected directory!
Get-ChildItem -Path "$Token" -Include *.lnk -Recurse | Select-Object * | Where-Object {
$_.CreationTime.Year -Match "$MyYear" -and $_.FullName -iNotMatch "$Env:USERNAME\\Links" } | ForEach-Object {
If($Forensic -ieq "True")
{
<#
.SYNOPSIS
Author: https://code.google.com/archive/p/lnk-parser
Helper - Parse .LNK file metadata onscreen (terminal)
.OUTPUTS
[Distributed Link Tracker Properties]
Version: 0
NetBIOS name: desktop-unlaahq
Droid volume identifier: fbadb152-faa6-4cd4-a8e7-56df01695110
Droid file identifier: a4fef014-7ac4-11eb-b11c-409f383199e6
Birth droid volume identifier: fbadb152-faa6-4cd4-a8e7-56df01695110
Birth droid file identifier: a4fef014-7ac4-11eb-b11c-409f383199e6
MAC address: 40:9f:38:31:99:e6
UUID timestamp: 03/01/2021 (19:30:56.528) [UTC]
UUID sequence number: 12572
#>
If(-not(Test-Path -Path "lnk_parser_cmd.exe" -EA SilentlyContinue))
{
#Download lnk_parser from code.google.com
iwr -Uri "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/lnk-parser/lnk_parser_cmd.exe" -OutFile "lnk_parser_cmd.exe"|Out-Null
Unblock-File -Path lnk_parser_cmd.exe #Unblock the binary file downloaded!
}
$Count++
Write-Host ""
#Run lnk_parser locally
If($OutLog -ieq "true")
{
.\lnk_parser_cmd.exe -r $_.FullName
#Export lnk files found metadata to logfile
.\lnk_parser_cmd.exe -r $_.FullName >> Lnk-Sweeper.txt
}
Else
{
#Display onscreen the .LNK metadata.
.\lnk_parser_cmd.exe -r $_.FullName
}
Start-Sleep -Milliseconds 400
}
Else
{
$Count++
If($OutLog -ieq "true")
{
#Display onscreen the .LNK files found.
Write-Host "$CharItem"$_.FullName
#Export results to logfile
$_.FullName >> Lnk-Sweeper.txt
}
Else
{
#Display onscreen the .LNK files found.
Write-Host "$CharItem"$_.FullName
}
Start-Sleep -Milliseconds 400
}
If($Action -ieq "clean")
{
#Delete the .LNK artifacts found.
Remove-Item -Path $_.FullName -Force
}
}
}#EndOfForEach()
}
Else
{
#Wrong TimeStamp format
Write-Host "[x] Error : Wrong TimeStamp format selection detected .." -ForegroundColor Red -BackgroundColor Black
Start-Sleep -Seconds 1
Write-Host "----------------------------------------------------------"
Write-Host " Syntax : .\Lnk-Sweeper.ps1 -TimeStamp `"year`""
Write-Host " Syntax : .\Lnk-Sweeper.ps1 -TimeStamp `"${MyYear}`""
Write-Host " Syntax : .\Lnk-Sweeper.ps1 -TimeStamp `"today`""
Write-Host " Syntax : .\Lnk-Sweeper.ps1 -TimeStamp `"${MyDate}/${MyMonth}/${MyYear}`""
Write-Host "----------------------------------------------------------"
exit #Exit @Lnk-Sweeper
}
If($Count -eq 0)
{
#None artifacts found
Write-Host "[x] Error : None artifacts found that match the searh criteria .." -ForegroundColor Red -BackgroundColor Black
}
Start-Sleep -Seconds 1
#Clean cmdlet artifacts left behind and unload cmdlet!
If(Test-Path -Path "lnk_parser_cmd.exe" -EA SilentlyContinue)
{
#Remove binary used to parse .LNK metadata.
Remove-Item -Path "lnk_parser_cmd.exe" -Force
}
If($PSBanner -ieq "True")
{
Write-Host ""
}
exit