-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCorelanPyKDInstall.ps1
More file actions
653 lines (524 loc) · 18.5 KB
/
CorelanPyKDInstall.ps1
File metadata and controls
653 lines (524 loc) · 18.5 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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# Requires -RunAsAdministrator
#
# (c) Corelan Consulting bv - 2026
# www.corelan-consulting.com
# www.corelan-training.com
# www.corelan-certified.com
# www.corelan.be
#
# PowerShell script to install Python3.9 and a PyKD version that is compatible with Python3
# for WinDBG Classic and WinDBGX on Windows 10/11
$ErrorActionPreference = "Stop"
$env:tempfolder = "C:\corelantemp"
$env:python32installer = "python-3.9.13.exe"
$env:python64installer = "python-3.9.13-amd64.exe"
$python32Url = "https://www.python.org/ftp/python/3.9.13/python-3.9.13.exe"
$python64Url = "https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe"
$pykdExtX86Url = "https://github.com/corelan/CorelanTraining/raw/refs/heads/master/pykd-ext/2.0.0.24/x86.zip"
$pykdExtX64Url = "https://github.com/corelan/CorelanTraining/raw/refs/heads/master/pykd-ext/2.0.0.24/x64.zip"
$classicDbgBase = "C:\Program Files (x86)\Windows Kits\10\Debuggers"
$engineExt64 = Join-Path $env:LOCALAPPDATA "DBG\EngineExtensions"
$engineExt32 = Join-Path $env:LOCALAPPDATA "DBG\EngineExtensions32"
$userExtensions = Join-Path $env:USERPROFILE "AppData\dbg\UserExtensions"
$python32Root = Join-Path $env:LOCALAPPDATA "Programs\Python\Python39-32"
$python64Root = Join-Path $env:LOCALAPPDATA "Programs\Python\Python39"
$python32SitePackages = Join-Path $python32Root "Lib\site-packages"
$python64SitePackages = Join-Path $python64Root "Lib\site-packages"
$python32Dll = Join-Path $python32Root "python39.dll"
$python64Dll = Join-Path $python64Root "python39.dll"
$vcShared32 = "C:\Program Files (x86)\Common Files\Microsoft Shared\VC"
$msdia140Target = Join-Path $vcShared32 "msdia140.dll"
$msdia100_64 = "C:\Program Files\Common Files\microsoft shared\VC\msdia100.dll"
$msdia120_32 = "C:\Program Files (x86)\Windows Kits\10\App Certification Kit\msdia120.dll"
$regsvr32_64 = "$env:WINDIR\System32\regsvr32.exe"
$regsvr32_32 = "$env:WINDIR\SysWOW64\regsvr32.exe"
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
}
}
}
function Remove-FileIfExists($path)
{
if (Test-Path $path -PathType Leaf)
{
Remove-Item -Path $path -Force -ErrorAction SilentlyContinue
}
}
function Remove-ExistingPyKD
{
Write-Output "[+] Removing existing PyKD files"
$paths = @(
(Join-Path $classicDbgBase "x86\winext\pykd.pyd"),
(Join-Path $classicDbgBase "x64\winext\pykd.pyd"),
(Join-Path $engineExt64 "pykd.pyd"),
(Join-Path $engineExt32 "pykd.pyd"),
(Join-Path $engineExt64 "pykd.dll"),
(Join-Path $engineExt32 "pykd.dll"),
(Join-Path $userExtensions "pykd.pyd"),
(Join-Path $userExtensions "pykd.dll")
)
foreach ($path in $paths)
{
Remove-FileIfExists $path
}
}
function Get-WingetPythonLines
{
$wingetOutput = winget list python 2>&1
if ($LASTEXITCODE -ne 0)
{
Write-Output "*** Failed to run 'winget list python'"
exit 1
}
return @($wingetOutput | Where-Object { $_ -match '^\s*Python' })
}
function Validate-WingetPythonSources
{
param(
[string]$StageDescription
)
Write-Output "[+] Checking Python packages via winget ($StageDescription)"
$pythonLines = Get-WingetPythonLines
if ($pythonLines.Count -eq 0)
{
Write-Output " No Python packages reported by 'winget list python'"
return
}
$invalidLines = @()
foreach ($line in $pythonLines)
{
if ($line -notmatch 'winget\s*$')
{
$invalidLines += $line
}
}
if ($invalidLines.Count -gt 0)
{
Write-Output "*** One or more Python packages do not have source 'winget':"
foreach ($line in $invalidLines)
{
Write-Output " $line"
}
Write-Output ""
Write-Output "*** Please remove the Python packages above that were not installed from winget,"
Write-Output "*** then run this script again."
exit 1
}
else
{
Write-Output " All Python entries reported by winget have source 'winget'"
}
}
function Run-ProcessChecked
{
param(
[string]$FilePath,
[string]$Arguments,
[string]$Description
)
Write-Output " $Description"
$proc = Start-Process -FilePath $FilePath -ArgumentList $Arguments -Wait -PassThru
if ($proc.ExitCode -ne 0)
{
Write-Output "*** $Description failed with exit code $($proc.ExitCode)"
exit 1
}
}
function Find-PyKDPyd
{
param(
[string]$SitePackagesPath
)
$candidates = @(
(Join-Path $SitePackagesPath "pykd.pyd"),
(Join-Path $SitePackagesPath "pykd\pykd.pyd")
)
foreach ($candidate in $candidates)
{
if (Test-Path $candidate -PathType Leaf)
{
return $candidate
}
}
$found = Get-ChildItem -Path $SitePackagesPath -Recurse -Filter "pykd.pyd" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($found)
{
return $found.FullName
}
return $null
}
function Find-Msdia140
{
param(
[string]$SearchRoot
)
$found = Get-ChildItem -Path $SearchRoot -Recurse -Filter "msdia140.dll" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($found)
{
return $found.FullName
}
return $null
}
function Register-DllSilent
{
param(
[string]$DllPath,
[ValidateSet("x86","x64")]
[string]$Bitness,
[switch]$ContinueOnMissing
)
if (-not (Test-Path $DllPath -PathType Leaf))
{
if ($ContinueOnMissing)
{
Write-Output " File not found, continuing: $DllPath"
return
}
else
{
Write-Output "*** File not found: $DllPath"
exit 1
}
}
$regsvr = if ($Bitness -eq "x86") { $regsvr32_32 } else { $regsvr32_64 }
$proc = Start-Process -FilePath $regsvr -ArgumentList "`"$DllPath`" /s" -Wait -PassThru
if ($proc.ExitCode -ne 0)
{
Write-Output "*** regsvr32 failed for $DllPath (exit code $($proc.ExitCode))"
exit 1
}
}
function Install-Python39
{
Write-Output "[+] Installing Python 3.9.13 (32-bit and 64-bit)"
$python32Args = '/quiet InstallAllUsers=0 PrependPath=1 Include_pip=1 Include_test=0 Include_launcher=1 SimpleInstall=1 TargetDir="' + $python32Root + '"'
$python64Args = '/quiet InstallAllUsers=0 PrependPath=1 Include_pip=1 Include_test=0 Include_launcher=1 SimpleInstall=1 TargetDir="' + $python64Root + '"'
Run-ProcessChecked -FilePath (Join-Path $env:tempfolder $env:python32installer) -Arguments $python32Args -Description "Installing Python 3.9.13 32-bit"
Run-ProcessChecked -FilePath (Join-Path $env:tempfolder $env:python64installer) -Arguments $python64Args -Description "Installing Python 3.9.13 64-bit"
}
function Upgrade-Pip
{
Write-Output "[+] Updating pip"
Run-ProcessChecked -FilePath "py" -Arguments "-3.9-32 -m pip install --upgrade pip" -Description "Updating pip for Python 3.9 32-bit"
Run-ProcessChecked -FilePath "py" -Arguments "-3.9-64 -m pip install --upgrade pip" -Description "Updating pip for Python 3.9 64-bit"
}
function Install-PyKD32
{
Write-Output "[+] Installing PyKD 32-bit"
Run-ProcessChecked -FilePath "py" -Arguments "-3.9-32 -m pip install pykd" -Description "Installing PyKD with pip for Python 3.9 32-bit"
Ensure-Folder $engineExt32
Ensure-Folder $vcShared32
$msdia140Source = Find-Msdia140 -SearchRoot $python32Root
if (-not $msdia140Source)
{
Write-Output "*** Unable to locate msdia140.dll for Python 3.9 32-bit"
exit 1
}
Write-Output " Copying msdia140.dll to $vcShared32"
Copy-Item -Path $msdia140Source -Destination $msdia140Target -Force
Write-Output " Registering msdia140.dll"
Register-DllSilent -DllPath $msdia140Target -Bitness x86
}
function Install-VCRuntime2010
{
Write-Host "[*] Installing VC++ 2010 SP1 x86..."
try {
$proc = Start-Process -FilePath "$env:tempfolder\vcredist_x86.exe" -ArgumentList "/quiet /norestart" -Wait -PassThru
if ($proc.ExitCode -ne 0) {
Write-Warning "VC++ 2010 x86 installer returned exit code $($proc.ExitCode). Continuing anyway..."
}
} catch {
Write-Warning "VC++ 2010 x86 installation failed. Continuing anyway..."
}
Write-Host "[*] Installing VC++ 2010 SP1 x64..."
try {
$proc = Start-Process -FilePath "$env:tempfolder\vcredist_x64.exe" -ArgumentList "/quiet /norestart" -Wait -PassThru
if ($proc.ExitCode -ne 0) {
Write-Warning "VC++ 2010 x64 installer returned exit code $($proc.ExitCode). Continuing anyway..."
}
} catch {
Write-Warning "VC++ 2010 x64 installation failed. Continuing anyway..."
}
}
function Install-PyKD64
{
Write-Output "[+] Installing PyKD 64-bit"
Run-ProcessChecked -FilePath "py" -Arguments "-3.9-64 -m pip install pykd" -Description "Installing PyKD with pip for Python 3.9 64-bit"
Ensure-Folder $engineExt64
Write-Output " Registering msdia100.dll (continue if missing)"
Register-DllSilent -DllPath $msdia100_64 -Bitness x64 -ContinueOnMissing
Write-Output " Registering msdia120.dll"
Register-DllSilent -DllPath $msdia120_32 -Bitness x86
}
function Install-Python27PyKD
{
$python27Root = "C:\Python27"
$python27Scripts = Join-Path $python27Root "Scripts"
$pipExe = Join-Path $python27Scripts "pip.exe"
Write-Output "[+] Checking for Python 2.7.18 at $python27Root"
if (-not (Test-Path $python27Root -PathType Container))
{
Write-Output " Python 2.7.18 not found at $python27Root, skipping"
return
}
if (-not (Test-Path $pipExe -PathType Leaf))
{
Write-Output "*** Python 2.7 was found at $python27Root, but $pipExe does not exist"
exit 1
}
Run-ProcessChecked -FilePath $pipExe -Arguments "install pykd" -Description "Installing PyKD with pip for Python 2.7"
}
function Install-PyKDExtensions
{
Write-Output "[+] Installing PyKD-Ext into WinDBG EngineExtensions folders"
$pykdExtX86Zip = Join-Path $env:tempfolder "pykd-ext-x86.zip"
$pykdExtX64Zip = Join-Path $env:tempfolder "pykd-ext-x64.zip"
$pykdExtX86Extract = Join-Path $env:tempfolder "pykd-ext-x86"
$pykdExtX64Extract = Join-Path $env:tempfolder "pykd-ext-x64"
Download-File -Uri $pykdExtX86Url -OutFile $pykdExtX86Zip -Label "3. PyKD-Ext x86"
Download-File -Uri $pykdExtX64Url -OutFile $pykdExtX64Zip -Label "4. PyKD-Ext x64"
Remove-Item -Path $pykdExtX86Extract -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $pykdExtX64Extract -Recurse -Force -ErrorAction SilentlyContinue
Ensure-Folder $pykdExtX86Extract
Ensure-Folder $pykdExtX64Extract
Write-Output " Extracting PyKD-Ext x86"
Expand-Archive -Path $pykdExtX86Zip -DestinationPath $pykdExtX86Extract -Force
Write-Output " Extracting PyKD-Ext x64"
Expand-Archive -Path $pykdExtX64Zip -DestinationPath $pykdExtX64Extract -Force
$pykdDllX86 = Join-Path $pykdExtX86Extract "Release\pykd.dll"
$pykdDllX64 = Join-Path $pykdExtX64Extract "Release\pykd.dll"
if (-not (Test-Path $pykdDllX86 -PathType Leaf))
{
Write-Output "*** Unable to locate x86 Release\pykd.dll in PyKD-Ext archive"
exit 1
}
if (-not (Test-Path $pykdDllX64 -PathType Leaf))
{
Write-Output "*** Unable to locate x64 Release\pykd.dll in PyKD-Ext archive"
exit 1
}
Ensure-Folder $engineExt32
Ensure-Folder $engineExt64
Write-Output " Copying x86 pykd.dll to $engineExt32"
Copy-Item -Path $pykdDllX86 -Destination (Join-Path $engineExt32 "pykd.dll") -Force
Write-Output " Copying x64 pykd.dll to $engineExt64"
Copy-Item -Path $pykdDllX64 -Destination (Join-Path $engineExt64 "pykd.dll") -Force
}
# main stuff
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"
Ensure-Folder $env:tempfolder
Ensure-Folder $engineExt64
Ensure-Folder $engineExt32
Ensure-Folder $userExtensions
Ensure-Winget -TempFolder $env:tempfolder
Remove-ExistingPyKD
Validate-WingetPythonSources -StageDescription "before Python install"
Write-Output "[+] Downloading installers"
Download-File -Uri $python32Url -OutFile (Join-Path $env:tempfolder $env:python32installer) -Label "1. Python 3.9.13 32-bit"
Download-File -Uri $python64Url -OutFile (Join-Path $env:tempfolder $env:python64installer) -Label "2. Python 3.9.13 64-bit"
Install-Python39
Install-VCRuntime2010
Validate-WingetPythonSources -StageDescription "after Python install"
Upgrade-Pip
Install-PyKD32
Install-PyKD64
Install-Python27PyKD
Install-PyKDExtensions
Write-Output "[+] Removing temporary folder again"
Remove-Item -Path $env:tempfolder -Recurse -Force -ErrorAction SilentlyContinue
Write-Output ""
Write-Output "[+] All set"
Write-Output ""
Write-Output "[+] You may want to restart WinDBG / WinDBGX if they were already open"