-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCorelanVMInstall.ps1
More file actions
executable file
·401 lines (335 loc) · 13.1 KB
/
CorelanVMInstall.ps1
File metadata and controls
executable file
·401 lines (335 loc) · 13.1 KB
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
# Requires -RunAsAdministrator
#
# (c) Corelan Consulting bv - 2020
# www.corelan-consulting.com
# www.corelan-training.com
# www.corelan-certified.com
# www.corelan.be
#
#
# PowerShell script to prepare a Windows 11/10 machine for use in the
# Corelan Stack & Heap Exploit Development classes
#
#
$env:tempfolder = "c:\corelantemp"
$env:pythonfile = "python-2.7.18.msi"
$env:windbgfile = "winsdksetup.exe"
$env:vscommunityfile = "vs_WDExpress.exe"
$env:monafile = "mona.py"
$env:windbglibfile = "windbglib.py"
$env:pykdfile = "pykd.zip"
$env:immunityprogramfolder = "C:\Program Files (x86)\Immunity Inc\Immunity Debugger"
$env:immunitypycommandsfolder = Join-Path $env:immunityprogramfolder "PyCommands"
$cmdPath = "$env:WINDIR\System32\cmd.exe"
$desktopPath = [Environment]::GetFolderPath("Desktop")
$shortcutPath = Join-Path $desktopPath "Corelan CMD Prompt.lnk"
$cmdArguments = '/K "cd /d ""C:\Program Files (x86)\Windows Kits\10\Debuggers\x86"""'
function Confirm-Continue
{
param(
[string]$Message = "Ready to continue?"
)
while ($true)
{
$response = Read-Host "$Message (Y/N)"
switch ($response.ToLower())
{
"y" { return }
"yes" { return }
"n"
{
Write-Output "Aborted by user."
exit 1
}
"no"
{
Write-Output "Aborted by user."
exit 1
}
default
{
Write-Output "Please enter Y or N."
}
}
}
}
function Ensure-Folder($path)
{
if (-not (Test-Path $path -PathType Container))
{
New-Item -Path $path -ItemType Directory -Force *>$null
}
}
function Ensure-Admin
{
Write-Output "[+] Testing if we have admin privileges"
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
Write-Output "**********************************************"
Write-Output "!! This script must be run as Administrator !!"
Write-Output "**********************************************"
exit 1
}
else
{
Write-Output " OK: Administrator privileges detected."
}
}
function Download-File
{
param(
[string]$Uri,
[string]$OutFile,
[string]$Label
)
Write-Output " $Label"
if (Test-Path $OutFile)
{
Remove-Item $OutFile -Force
}
# Try curl first
$curl = "$env:SystemRoot\System32\curl.exe"
if (Test-Path $curl)
{
& $curl -L --fail -o $OutFile $Uri
if ($LASTEXITCODE -eq 0)
{
return
}
}
# Fallback to BITS
try
{
Start-BitsTransfer -Source $Uri -Destination $OutFile
return
}
catch
{
Write-Output "*** Download failed with curl and BITS"
exit 1
}
}
function Ensure-Winget
{
param(
[string]$TempFolder
)
Write-Output "[+] Checking for winget"
if (Get-Command winget -ErrorAction SilentlyContinue)
{
Write-Output " winget found"
return
}
Write-Output "*** winget was not found on this system."
Write-Output "***"
Write-Output "*** This script can continue without installing it,"
Write-Output "*** but features that rely on winget will not work."
Write-Output ""
while ($true)
{
$response = Read-Host "Do you want to install winget now? (Y/N)"
switch ($response.ToLower())
{
"y" { break }
"yes" { break }
"n"
{
Write-Output "Aborted by user."
exit 1
}
"no"
{
Write-Output "Aborted by user."
exit 1
}
default
{
Write-Output "Please enter Y or N."
}
}
}
Ensure-Folder $TempFolder
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$appInstallerFile = Join-Path $TempFolder "Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$appInstallerUrl = "https://aka.ms/getwinget"
try
{
Write-Output " Downloading App Installer package"
if (Test-Path $appInstallerFile)
{
Remove-Item $appInstallerFile -Force
}
Invoke-WebRequest -Uri $appInstallerUrl -OutFile $appInstallerFile -UseBasicParsing *>$null
}
catch
{
Write-Output "*** Failed to download App Installer package"
Write-Output "*** Please install winget manually and run this script again."
exit 1
}
try
{
Write-Output " Installing App Installer / winget"
Add-AppxPackage -Path $appInstallerFile
}
catch
{
Write-Output "*** Failed to install App Installer package"
Write-Output "*** Please install winget manually and run this script again."
exit 1
}
Start-Sleep -Seconds 5
$machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$env:Path = "$machinePath;$userPath"
if (-not (Get-Command winget -ErrorAction SilentlyContinue))
{
Write-Output "*** winget still not available after installation"
Write-Output "*** A reboot or sign-out/sign-in may be required."
exit 1
}
Write-Output " winget installed successfully"
}
function Test-InternetConnectivity
{
Write-Output "[+] Checking internet connectivity"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$testUris = @(
"https://www.python.org/",
"https://pypi.org/",
"https://github.com/",
"https://aka.ms/"
)
foreach ($uri in $testUris)
{
try
{
Invoke-WebRequest -Uri $uri -Method Head -UseBasicParsing -TimeoutSec 20 *>$null
Write-Output " OK $uri"
}
catch
{
Write-Output "*** Unable to reach $uri"
Write-Output "*** Please verify internet connectivity and try again."
exit 1
}
}
}
### MAIN ROUTINE ###
Ensure-Admin
Write-Host "*** -->> Make sure you have an active internet connection before proceeding! <<-- ***"
Confirm-Continue
Test-InternetConnectivity
Write-Output "[+] Creating temp folder $env:tempfolder"
New-Item -Path "c:\" -Name "corelantemp" -ItemType "directory" *>$null
Ensure-Winget -TempFolder $env:tempfolder
Write-Output "[+] Creating shortcut to cmd.exe on desktop (set to 'Run As Administrator')."
# Create shortcut
$WshShell = New-Object -ComObject WScript.Shell
$shortcut = $WshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $cmdPath
$shortcut.Arguments = $cmdArguments
$shortcut.WorkingDirectory = "$env:WINDIR\System32"
$shortcut.WindowStyle = 1
$shortcut.IconLocation = "$cmdPath,0"
$shortcut.Save()
# --- Enable "Run as Administrator" flag ---
$bytes = [System.IO.File]::ReadAllBytes($shortcutPath)
$bytes[0x15] = $bytes[0x15] -bor 0x20
[System.IO.File]::WriteAllBytes($shortcutPath, $bytes)
# Since all the URLs used for downloading packages uses TLS 1.2 so we need to make sure TLS 1.2 is being used while files been requested otherwise we get SSL error
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (Test-Path $env:tempfolder -PathType Container)
{
Write-Output "[+] Downloading packages to temp folder"
Write-Output " 1. Python 2.7.18"
Invoke-WebRequest -Uri "https://www.python.org/ftp/python/2.7.18/python-2.7.18.msi" -OutFile "$env:tempfolder\$env:pythonfile" *>$null
Write-Output " 2. Classic WinDBG"
Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/p/?linkid=2083338&clcid=0x409" -OutFile "$env:tempfolder\$env:windbgfile" *>$null
Write-Output " 3. PyKD"
Invoke-WebRequest -Uri "https://github.com/corelan/windbglib/raw/master/pykd/pykd.zip" -OutFile "$env:tempfolder\$env:pykdfile" *>$null
Expand-Archive -Path "$env:tempfolder\$env:pykdfile" -DestinationPath "$env:tempfolder\" -Force
Write-Output " 4. mona.py"
Invoke-WebRequest -Uri "https://github.com/corelan/mona/raw/master/mona.py" -OutFile "$env:tempfolder\$env:monafile" *>$null
Write-Output " 5. windbglib.py"
Invoke-WebRequest -Uri "https://github.com/corelan/windbglib/raw/master/windbglib.py" -OutFile "$env:tempfolder\$env:windbglibfile" *>$null
Write-Output " 6. Visual Studio 2017 Desktop Express"
Invoke-WebRequest -Uri "https://aka.ms/vs/15/release/vs_WDExpress.exe" -OutFile "$env:tempfolder\$env:vscommunityfile" *>$null
Write-Output "[+] Creating System Environment variable _NT_SYMBOL_PATH"
[Environment]::SetEnvironmentVariable("_NT_SYMBOL_PATH", "srv*c:\symbols*http://msdl.microsoft.com/download/symbols", "Machine")
Write-Output "[+] Adding c:\Python27 to PATH"
$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
if ($oldpath -like '*c:\Python27*')
{
Write-Output " PATH already contains entry for Python 2.7"
}
else
{
$newPath = "$oldpath;c:\Python27"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
}
Write-Output "[+] Installing software"
Write-Output " 1. Python 2.7"
Start-Process "$env:tempfolder\$env:pythonfile" -Wait -ArgumentList '/quiet /passive'
Write-Output " 2. VC++ Redistributable"
Start-Process "$env:tempfolder\vcredist_x86.exe" -Wait -ArgumentList "/q"
if (Test-Path "C:\Program Files (x86)\Common Files\microsoft shared\VC\msdia90.dll" -PathType Leaf)
{
Write-Output " 3. Register msdia90.dll"
Start-Process regsvr32 -Wait -ArgumentList '"C:\Program Files (x86)\Common Files\microsoft shared\VC\msdia90.dll" /s'
}
else
{
Write-Output " 3. *** Error registering msdia90.dll, file does not exist ***"
}
Write-Output " 4. WinDBG"
Write-Output " Hold on, this may take a while..."
Start-Process "$env:tempfolder\$env:windbgfile" -Wait -ArgumentList '/features OptionId.WindowsDesktopDebuggers /ceip off /q'
Write-Output " 5. WinDBGX"
winget install --id Microsoft.WinDbg -e --source winget --silent --accept-package-agreements --accept-source-agreements
Write-Output " 6. Visual Studio Code"
winget install --id Microsoft.VisualStudioCode -e --source winget --silent --accept-package-agreements --accept-source-agreements
Write-Output " 7. PyKD, windbglib and mona"
Write-Output " a. Installing mona.py in WinDBG"
Copy-Item -Path "$env:tempfolder\$env:monafile" -Destination "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\"
Copy-Item -Path "$env:tempfolder\$env:windbglibfile" -Destination "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\"
Copy-Item -Path "$env:tempfolder\pykd.pyd" -Destination "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\winext\"
# if Immunity Debugger was already installed, copy mona.py into the PyCommands folder
if (Test-Path $env:immunitypycommandsfolder -PathType Container)
{
Write-Output " b. Installing mona.py in Immunity Debugger"
Copy-Item -Path "$env:tempfolder\$env:monafile" -Destination $env:immunitypycommandsfolder -Force
# Check for mona.ini
$monaIniPath = Join-Path $env:immunityprogramfolder "mona.ini"
if (-not (Test-Path $monaIniPath -PathType Leaf))
{
Write-Output " c. Creating mona.ini"
"workingfolder=c:\logs\%p" | Out-File -FilePath $monaIniPath -Encoding ASCII
}
else
{
Write-Output " c. mona.ini already exists"
}
}
Write-Output " 8. 7Zip"
winget install --id 7zip.7zip -e --source winget --silent --accept-package-agreements --accept-source-agreements
Write-Output " 9. Visual Studio 2017 Desktop Express - manual install"
Start-Process "$env:tempfolder\$env:vscommunityfile" -Wait
Write-Output "[+] Launching WinDBG to check if everything is ok"
Write-Output " ==> Please check the WinDBG log window and confirm that:"
Write-Output " - the !peb command didn't produce an error message"
Write-Output " - the !py mona command resulted in producing a list of available mona commands"
Start-Process "C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\windbg" -ArgumentList '-c ".load pykd.pyd; !py mona config -set workingfolder c:\logs\%p; !peb; !py mona" -o "c:\windows\system32\calc.exe"'
Write-Output "[+] Removing temporary folder again"
Remove-Item -Path "$env:tempfolder" -recurse -force
Write-Output ""
Write-Output "[+] All set"
Write-Output ""
Write-Output "[+] Reboot your VM, and wait for updates to be installed if needed"
}
else
{
Write-Output "*** Oops, folder '$env:tempfolder' does not exist"
}