-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
253 lines (209 loc) · 5.83 KB
/
main.go
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
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"syscall"
)
const fov = 120
// Set to 0 to find game automatically
var pid = 0
func runInstruction(instr []byte, rcx uint64) {
// Get old registers
var oldRegs syscall.PtraceRegs
syscall.PtraceGetRegs(pid, &oldRegs)
// Get old (partial) instruction at the address
oldInstr := make([]byte, len(instr))
syscall.PtracePeekData(pid, uintptr(oldRegs.Rip), oldInstr)
// Replace with new instruction & registers
syscall.PtracePokeData(pid, uintptr(oldRegs.Rip), instr)
var newRegs syscall.PtraceRegs
syscall.PtraceGetRegs(pid, &newRegs)
newRegs.Rcx = rcx
syscall.PtraceSetRegs(pid, &newRegs)
// Step
syscall.PtraceSingleStep(pid)
var status syscall.WaitStatus
syscall.Wait4(pid, &status, 0, nil)
if !status.Stopped() || status.StopSignal() != 5 {
panic("woops")
}
// Replace with old instruction & registers
syscall.PtracePokeData(pid, uintptr(oldRegs.Rip), oldInstr)
syscall.PtraceSetRegs(pid, &oldRegs)
}
func trapAndListen(funcStart uintptr) {
// Attach to process
syscall.PtraceAttach(pid)
var status syscall.WaitStatus
syscall.Wait4(pid, &status, 0, nil)
if status.Stopped() && status.StopSignal() == 19 {
fmt.Println("ptrace has attached")
} else {
panic("woops")
}
// Get old (partial) instructions at the addresses
oldInstr1 := make([]byte, 1)
syscall.PtracePeekData(pid, funcStart, oldInstr1)
oldInstr2 := make([]byte, 1)
syscall.PtracePeekData(pid, funcStart+0x73, oldInstr2)
// Replace instructions with breakpoints
syscall.PtracePokeData(pid, funcStart, []byte{0xcc})
syscall.PtracePokeData(pid, funcStart+0x73, []byte{0xcc})
// Continue running
syscall.PtraceCont(pid, 0)
for {
// Wait for a trap
syscall.Wait4(pid, &status, 0, nil)
if status.StopSignal() != 5 {
if status.Stopped() {
syscall.PtraceCont(pid, int(status.StopSignal()))
}
continue
}
fmt.Println("trapped fov call")
// Get current regs
var regs syscall.PtraceRegs
syscall.PtraceGetRegs(pid, ®s)
// Save rcx
passedRcx := regs.Rcx
fmt.Printf("captured rcx: %x\n", passedRcx)
// Add the old instruction back & rewind the ip
fmt.Println("rewinding instruction pointer")
syscall.PtracePokeData(pid, funcStart, oldInstr1)
regs.Rip = uint64(funcStart)
syscall.PtraceSetRegs(pid, ®s)
// Step the old instruction
syscall.PtraceSingleStep(pid)
var status syscall.WaitStatus
syscall.Wait4(pid, &status, 0, nil)
if !status.Stopped() || status.StopSignal() != 5 {
panic("woops")
}
// Put the breakpoint back
syscall.PtracePokeData(pid, funcStart, []byte{0xcc})
// Continue execution to the breakpoint at the end of the function
fmt.Println("continuing to end of function")
syscall.PtraceCont(pid, 0)
syscall.Wait4(pid, &status, 0, nil)
if !status.Stopped() || status.StopSignal() != 5 {
panic("woops")
}
// Run instruction to update fov
// mov dword ptr [rcx+0x40], 0xfov
instr := bytes.NewBuffer([]byte{0xc7, 0x41, 0x40})
binary.Write(instr, binary.LittleEndian, float32(fov)/float32(78))
fmt.Println("running instr:", hex.EncodeToString(instr.Bytes()))
runInstruction(instr.Bytes(), passedRcx)
// Add the old instruction back & rewind the ip
fmt.Println("rewinding instruction pointer again")
syscall.PtracePokeData(pid, funcStart+0x73, oldInstr2)
syscall.PtraceGetRegs(pid, ®s)
regs.Rip = uint64(funcStart + 0x73)
syscall.PtraceSetRegs(pid, ®s)
// Step the old instruction
syscall.PtraceSingleStep(pid)
syscall.Wait4(pid, &status, 0, nil)
if !status.Stopped() || status.StopSignal() != 5 {
panic("woops")
}
// Put the breakpoint back
syscall.PtracePokeData(pid, funcStart+0x73, []byte{0xcc})
fmt.Println("done")
// Stop the process so gdb can be attached
syscall.PtraceCont(pid, 0)
}
}
func findProcess() int {
procs, err := os.ReadDir("/proc")
if err != nil {
panic(err)
}
for _, v := range procs {
if !v.IsDir() {
continue
}
f, err := os.Open("/proc/" + v.Name() + "/cmdline")
if errors.Is(err, os.ErrNotExist) {
continue
} else if err != nil {
panic(err)
}
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
f.Close()
if strings.Contains(string(b), "SoTGame.exe") && !strings.Contains(string(b), "steam.exe") && !strings.Contains(string(b), "waitforexitandrun") {
pid, err := strconv.Atoi(string(v.Name()))
if err != nil {
panic(err)
}
return pid
}
}
panic("couldn't find pid for game. try setting it manually.")
}
func main() {
if pid == 0 {
pid = findProcess()
fmt.Println("found pid:", pid)
}
// Open memory
fmt.Println("scanning memory")
mem, err := os.Open(fmt.Sprintf("/proc/%v/mem", pid))
if err != nil {
panic(err)
}
defer mem.Close()
// Open map
procMap, err := os.Open(fmt.Sprintf("/proc/%v/maps", pid))
if err != nil {
panic(err)
}
defer procMap.Close()
// Search through each block in map
scanner := bufio.NewScanner(procMap)
for scanner.Scan() {
// Split on spaces to find addr range
split := strings.Split(scanner.Text(), " ")
if split[0] == "ffffffffff600000-ffffffffff601000" {
continue
} else if !strings.Contains(split[1], "r") {
continue
}
// Split range to find start and end
split = strings.Split(split[0], "-")
start, err := strconv.ParseInt(split[0], 16, 64)
if err != nil {
panic(err)
}
end, err := strconv.ParseInt(split[1], 16, 64)
if err != nil {
panic(err)
}
// Read range from memory
b := make([]byte, int(end-start))
_, err = mem.Seek(start, io.SeekStart)
if err != nil {
panic(err)
}
_, err = mem.Read(b)
if err != nil {
fmt.Println(err)
}
// Search for pattern
i := bytes.Index(b, []byte{0x40, 0x57, 0x48, 0x83, 0xEC, 0x30, 0x80, 0x79, 0x44, 0x00})
if i != -1 {
fmt.Printf("found func at: %x\n", int(start)+i)
trapAndListen(uintptr(int(start) + i))
}
}
}