-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbpatch.go
More file actions
181 lines (148 loc) · 3.58 KB
/
Copy pathbpatch.go
File metadata and controls
181 lines (148 loc) · 3.58 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
package main
import (
"encoding/binary"
"flag"
"fmt"
"os"
)
var version = "v1.0.1"
const REPEATED = 20 // Number of repetitions of the pattern
// checkPatched checks if the binary is already patched
func checkPatched(data []byte, patch []byte) bool {
return indexOf(data, patch) != -1
}
// indexOf finds a byte slice inside another byte slice
func indexOf(data, pattern []byte) int {
dataLen := len(data)
patLen := len(pattern)
if patLen == 0 || patLen > dataLen {
return -1
}
for i := 0; i <= dataLen-patLen; i++ {
match := true
for j := 0; j < patLen; j++ {
if data[i+j] != pattern[j] {
match = false
break
}
}
if match {
return i
}
}
return -1
}
// check checks if a repeated pattern exists
func check(data []byte, what byte, a int, s int, count int) int {
for i := 0; i < count; i++ {
index := a + s*i
if index >= len(data) || data[index] != what {
return 0
}
}
return s * count
}
// find searches for a repeated pattern
func find(data []byte, what byte, count int) (int, int) {
fs := len(data)
maxStep := fs / count
for i := 0; i < fs; i++ {
for step := 2; step <= maxStep; step += 2 {
j := check(data, what, i, step, count)
if j > 0 {
return i, j
}
}
}
return 0, 0
}
func defaultPatch() []byte {
return []byte{
0x00, 0xA0, 0x00, 0x47, 0x7A, 0xFF, 0x17, 0xEE,
0xFD, 0xFF, 0xFF, 0x1A, 0x00, 0x00, 0xA0, 0xE3,
0x9A, 0x0F, 0x07, 0xEE, 0x00, 0x00, 0xA0, 0xE3,
0x15, 0x0F, 0x07, 0xEE, 0x01, 0x00, 0x8F, 0xE2,
0x10, 0xFF, 0x2F, 0xE1, 0x28, 0x00, 0x10, 0x30,
0x31, 0x00, 0x10, 0x31, 0x88, 0x47, 0xC0, 0x46,
}
}
func main() {
showVersion := flag.Bool("version", false, "Show version")
flag.BoolVar(showVersion, "v", false, "Show version (shorthand)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: bpatch [options] <jabxxx.b> <payload.bin>\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
if *showVersion {
fmt.Println("bpatch version", version)
return
}
args := flag.Args()
if len(args) < 1 {
flag.Usage()
os.Exit(1)
}
filePath := args[0]
var payloadPath string
if len(args) >= 2 {
payloadPath = args[1]
}
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s not found\n", filePath)
os.Exit(1)
}
var patch []byte
if payloadPath != "" {
patch, err = os.ReadFile(payloadPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: %s not found, using default payload\n", payloadPath)
}
}
if len(patch) == 0 {
patch = defaultPatch()
}
// Already patched?
if checkPatched(data, patch) {
fmt.Println("The file is already patched.")
return
}
// Find repeated pattern
newCodeStart, newCodeLen := find(data, 0x55, REPEATED)
if newCodeLen == 0 {
fmt.Println("Movefrom not found.")
return
}
newCodeEnd := newCodeStart + newCodeLen
fmt.Printf("Pattern found from %s to %s\n",
fmt.Sprintf("0x%X", newCodeStart),
fmt.Sprintf("0x%X", newCodeEnd),
)
// Read return instruction
if newCodeEnd+2 > len(data) {
fmt.Println("Unexpected end of file.")
return
}
ret := binary.LittleEndian.Uint16(data[newCodeEnd : newCodeEnd+2])
if ret != 0x2000 {
fmt.Println("Can't find 'return' instruction at the expected position.")
return
}
// Apply patch
copy(data[newCodeStart:], patch)
// Fill remaining space with NOPs (0x46C0)
for i := newCodeStart + len(patch); i <= newCodeEnd; i += 2 {
if i+1 < len(data) {
data[i] = 0xC0
data[i+1] = 0x46
}
}
err = os.WriteFile(filePath, data, 0644)
if err != nil {
fmt.Println("Failed to write patched file.")
return
}
fmt.Println("Patch applied successfully.")
}