forked from sk1080/nvidia-kvm-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.ps1
156 lines (129 loc) · 4.08 KB
/
patcher.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
# Nvidia KVM Patcher
# SK1080
# License: Do whatever you want with this code
# Pardon my poor powershell skills
# Note: Only Tested on Versions 361.91, 368.39, and 372.54
# Note: Targeted at Windows 10 X64, Fix Tool Paths for Testsigning and /os: option in Inf2Cat to support other OS
# Note: Testsigning Requires Windows 10 WDK
param (
[Parameter(Position=1)][string]$directory = ""
)
if($directory -eq "")
{
Write-Host "Usage: patcher.ps1 [/path/to/Display.Driver]"
exit
}
$sys_packed = Join-Path $directory "nvlddmkm.sy_"
$sys_packed_backup = Join-Path $directory "nvlddmkm.sy_.bak"
if(Test-Path $sys_packed)
{
Move-Item $sys_packed $sys_packed_backup
}
$sys_packed = $sys_packed_backup
$sys_unpacked = Join-Path $directory "nvlddmkm.sys"
if(-Not(Test-Path $sys_packed))
{
Write-Host "[!] Failure: Unable to find nvlddmkm.sy_ in $directory"
exit
}
Write-Host '[+] Unpacking nvlddmkm.sys'
iex "expand $sys_packed $sys_unpacked" | Out-Null
if(-Not(Test-Path $sys_unpacked))
{
Write-Host "[!] Failure: Unable to unpack nvlddmkm.sys"
exit
}
Write-Host '[+] Patching CPUID Check'
#Stupidity Incomming
$stream = New-Object IO.FileStream -ArgumentList $sys_unpacked, 'Open', 'Read'
$enc = [Text.Encoding]::GetEncoding(28591)
$sr = New-Object IO.StreamReader -ArgumentList $stream, $enc
$text = $sr.ReadToEnd()
$sr.close()
$stream.close()
$pattern = '\xBB\x00\x00\x00\x40\x48\x8D\x44\x24\x50' # + 0x30
$patch = [Byte[]]@(0x31, 0xC0, 0x90, 0x90, 0x90, 0x90, 0x90, 0x85, 0xC0)
$regex = [Regex]$pattern
$matches = $regex.Matches($text)
$count = $matches.Count
if($count -ne 1)
{
Write-Host "[!] Failure: $count occurrences of patch pattern found"
exit
}
$offset = $matches[0].Index + 0x30
$stream = New-Object IO.FileStream -ArgumentList $sys_unpacked, 'Open', 'Write'
[void] $stream.Seek($offset, 'Begin')
$stream.Write($patch, 0, $patch.Length)
$stream.Close()
Write-Host '[+] Modifying Installer Config to Use Extracted Driver'
$installer_config = Join-Path $directory "DisplayDriver.nvi"
if(-Not(Test-Path $installer_config))
{
Write-Host "[!] Failure: Unable to find $installer_config"
exit
}
(Get-Content $installer_config) -replace 'nvlddmkm.sy_', 'nvlddmkm.sys' | Set-Content $installer_config
Write-Host '[+] Attempting to Test Sign Driver'
$inf2cat = ''
$signtool = ''
$inf2cat_paths = 'C:/Program Files (x86)/Windows Kits/10/bin/x86/Inf2Cat.exe', 'C:/WinDDK/7600.16385.1/bin/selfsign/Inf2Cat.exe'
$signtool_paths = 'C:/Program Files (x86)/Windows Kits/10/Tools/bin/i386/signtool.exe', 'C:/WinDDK/7600.16385.1/bin/amd64/signtool.exe'
foreach($path in $inf2cat_paths)
{
if(Test-Path $path)
{
$inf2cat = $path
break
}
}
foreach($path in $signtool_paths)
{
if(Test-Path $path)
{
$signtool = $path
break
}
}
if(-Not(Test-Path $inf2cat))
{
Write-Host "[!] Failure: Unable to locate inf2cat, see/edit script for expected paths"
exit
}
if(-Not(Test-Path $signtool))
{
Write-Host "[!] Failure: Unable to locate signtool, see/edit script for expected paths"
exit
}
$ostype = ''
$winver = [System.Environment]::OSVersion.Version
if($winver.Major -eq 6)
{
if($winver.Minor -eq 1)
{
$ostype = '7_X64'
}
elseif($winver.Minor -eq 2 -Or $winver.Minor -eq 3)
{
$ostype = '8_X64'
}
}
if($winver.Major -eq 10)
{
$ostype = '10_X64'
}
if($ostype -eq '')
{
Write-Host "[!] Failure: Unable to determine OS type, see script"
exit
}
Write-Host " [+] Detected OS Type: $ostype"
Write-Host ' [+] Generating Catalog File (this may take a while)'
& $inf2cat /driver:`"$directory`" /os:$ostype | Out-Null
Write-Host ' [+] Generating and Installing Certificate'
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
& (Join-Path $ScriptPath "gencert.ps1") -directory $ScriptPath | Out-Null
Write-Host ' [+] Signing Catalog File'
$catalog_path = $directory + './nv_disp.cat'
& $signtool sign /v /n SKSoftware /t http://timestamp.verisign.com/scripts/timstamp.dll $catalog_path
& $signtool sign /v /n SKSOftware /t http://timestamp.verisign.com/scripts/timstamp.dll $sys_unpacked