-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps_patcher.ps1
274 lines (274 loc) · 9.48 KB
/
ps_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
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
# https://github.com/0xDEADFED5/ps_patcher
# Copyright (c) 2023 0xDEADFED5, MIT license
# PowerShell 5+ required (i think)
# FYI, this will read entire files into memory before patching them
class PatchInfo {
# array of filenames to patch
[string[]] $files
# if true, filenames will be assumed to be relative and will be expanded to full path (default = true)
[bool] $relative = $true
# search and replace patterns, will be applied to every file in this entry
# patterns looks like this: FFFF ?F -> 909090
# spaces and case are ignored, ? matches any half-byte(nibble), ?? matches any whole byte
# no wildcards (?) in the replace pattern (second half)
[string[]] $patterns
# if true, only patch the first match found, otherwise search the whole file for multiple matches (default = true)
[bool] $first_only = $true
# if true, save original file to *.bak before patching, prompt before overwrite (default = true)
[bool] $backup = $true
# if true and not all search patterns are found, confirm before saving (default = false)
[bool] $warn_not_found = $false
}
#################################################################################################
#### Define patches here
#################################################################################################
$patches = @(
# example showing wildcard matches
[PatchInfo]@{
files = @("example\test.bin")
patterns = @("00 0? -> FFFF", "?C?C CC?? -> DEADFED5")
}
# example of patching multiple matches, and warning on not found
[PatchInfo]@{
files = @("example\test2.bin")
patterns = @("FFFF -> CC CC", "88 -> 99")
first_only = $false
warn_not_found = $true
}
)
#################################################################################################
##### Code starts here
#################################################################################################
$low_nibbles = @{
[char]'0' = [byte]0x00
[char]'1' = [byte]0x01
[char]'2' = [byte]0x02
[char]'3' = [byte]0x03
[char]'4' = [byte]0x04
[char]'5' = [byte]0x05
[char]'6' = [byte]0x06
[char]'7' = [byte]0x07
[char]'8' = [byte]0x08
[char]'9' = [byte]0x09
[char]'A' = [byte]0x0A
[char]'B' = [byte]0x0B
[char]'C' = [byte]0x0C
[char]'D' = [byte]0x0D
[char]'E' = [byte]0x0E
[char]'F' = [byte]0x0F
}
$high_nibbles = @{
[char]'0' = [byte]0x00
[char]'1' = [byte]0x10
[char]'2' = [byte]0x20
[char]'3' = [byte]0x30
[char]'4' = [byte]0x40
[char]'5' = [byte]0x50
[char]'6' = [byte]0x60
[char]'7' = [byte]0x70
[char]'8' = [byte]0x80
[char]'9' = [byte]0x90
[char]'A' = [byte]0xA0
[char]'B' = [byte]0xB0
[char]'C' = [byte]0xC0
[char]'D' = [byte]0xD0
[char]'E' = [byte]0xE0
[char]'F' = [byte]0xF0
}
function do_exit() {
Write-Host 'Finished. Press Enter to continue...'
$null = $Host.UI.ReadLine()
Exit
}
function abort([string]$msg) {
Write-Host "Fatal error: $msg" -ForegroundColor red
do_exit
}
function pattern_to_nibbles([string]$pattern) {
$pattern = $pattern.Replace(' ', '').ToUpper()
if ($pattern.Length % 2 -ne 0) {
abort("Search pattern must have even length: '${pattern}'")
}
$search = [System.Array]::CreateInstance([byte], $pattern.Length)
$high = $true
$chars = $pattern.ToCharArray()
for ($x = 0; $x -lt $chars.Count; $x++) {
$c = $chars[$x]
if ($high) {
if ($chars[$x] -eq '?') {
$search[$x] = 0xFF
}
else {
if ($high_nibbles.ContainsKey($c)) {
$search[$x] = $high_nibbles[$c]
}
else {
abort("Bad search pattern: '${pattern}' !")
}
}
$high = $false
}
else {
if ($chars[$x] -eq '?') {
$search[$x] = 0xFF
}
else {
if ($low_nibbles.ContainsKey($c)) {
$search[$x] = $low_nibbles[$c]
}
else {
abort("Bad search pattern: '${pattern}' !")
}
}
$high = $true
}
}
$search
}
function pattern_to_bytes([string]$pattern) {
$pattern = $pattern.Replace(' ', '').ToUpper()
if ($pattern.Length % 2 -ne 0) {
abort("Replace pattern must have even length: '${pattern}'")
}
[byte[]] -split ($pattern -replace '..', '0x$& ')
}
function search_bytes([byte[]][ref]$buffer, [byte[]][ref]$pattern, [bool]$first_only) {
$indexes = New-Object Collections.Generic.List[Int32]
$p_len = $pattern.Count / 2
if ($p_len -gt $buffer.Count) {
abort("Search pattern is longer than file!")
}
for ($x = 0; $x -le $buffer.Count - $p_len; $x++) {
$found = $true
$y = 0
$x_temp = 0
$hi = $true
while ($found -and $y -ne $pattern.Count) {
if ($pattern[$y] -eq 0xFF) {
$hi = !$hi
$y++
continue
}
if ($hi) {
$hi = $false
if (($buffer[$x + $x_temp] -band 0xF0) -eq $pattern[$y]) {
$y++
continue
}
else {
$found = $false
break
}
}
else {
$hi = $true
if (($buffer[$x + $x_temp] -band 0x0F) -eq $pattern[$y]) {
$y++
$x_temp++
continue
}
else {
$found = $false
break
}
}
}
if ($found) {
$indexes.Add($x)
if ($first_only) {
break
}
# if input is '90 90 90 90 90' and pattern is '90 90'
# i want to get 0,2 as result... not 0,1,2,3
$x += ($p_len - 1)
}
}
$indexes
}
function patch_buffer([byte[]][ref]$buffer, $index, [byte[]][ref]$patch) {
if (($index + $patch.Count) -gt $buffer.Count) {
abort("Attempting to patch past end of file!")
}
for ($x = 0; $x -lt $patch.Count; $x++) {
$buffer[$index + $x] = $patch[$x]
}
}
foreach ($p in $patches) {
foreach ($f in $p.files) {
$unique = 0
$total = 0
if ($p.relative) {
$f = Join-Path $PSScriptRoot $f
}
if (!(Test-Path $f)) {
Write-Host "File '${f}' not found, skipping ..." -ForegroundColor yellow
continue
}
Write-Host "Loading file '${f}' ..."
$buffer = [System.IO.File]::ReadAllBytes($f)
Write-Host "File loaded." -ForegroundColor green
foreach ($x in $p.patterns) {
$s = $x -split '->'
if ($s.Length -eq 2) {
$search_p = $s[0].Trim()
$search = pattern_to_nibbles($search_p)
Write-Host "Searching '${f}' for pattern '${search_p}' ..."
$i = search_bytes ([ref]$buffer) ([ref]$search) $p.first_only
if ($i.Count -eq 0) {
Write-Host "Search pattern '${search_p}' not found in file '${f}', continuing ..." -ForegroundColor yellow
continue
}
else {
$unique++
$c = $i.Count
$total += $c
Write-Host "Search pattern '${search_p}' found ${c} times in file '${f}', continuing ..." -ForegroundColor green
$replace = pattern_to_bytes($s[1])
foreach ($z in $i) {
patch_buffer ([ref]$buffer) $z ([ref]$replace)
}
}
}
else {
abort("Must be single '->' in pattern!")
}
}
if ($unique -ne 0) {
if ($p.backup) {
$backup = $f + '.bak'
Write-Host "Backing up '${f}' to '${backup}'"
if (Test-Path($backup)) {
$choice = Read-Host "File '${backup}' exists, overwrite? (y/N) "
if ($choice.ToLower() -eq 'y') {
Remove-Item -Path $backup
Rename-Item -Path $f -NewName $backup
}
}
else {
Rename-Item -Path $f -NewName $backup
}
}
$p_total = $p.patterns.Count
if ($p.warn_not_found -and ($unique -ne $p_total)) {
$choice = Read-Host "Found (${unique} of ${p_total}) patches. ${total} total matches were patched, save file? (y/N)"
if (!($choice.ToLower() -eq 'y')) {
if (!(Test-Path $f)) {
# shit, already renamed it
Write-Host "Restoring '${backup}' to '${f}'"
Rename-Item -Path $backup -NewName $f
}
continue
}
}
else {
Write-Host "Found (${unique} of ${p_total}) patches. ${total} total matches were patched."
}
[System.IO.File]::WriteAllBytes($f, $buffer)
Write-Host "Changes saved to '${f}'." -ForegroundColor green
}
else {
Write-Host "No search patterns matched in file '${f}' ..." -ForegroundColor yellow
}
}
}
do_exit