-
Notifications
You must be signed in to change notification settings - Fork 0
/
poke.go
259 lines (232 loc) · 5.26 KB
/
poke.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
254
255
256
257
258
259
package main
import (
"bufio"
"bytes"
"encoding/binary"
"flag"
"fmt"
"io"
"os"
"regexp"
"runtime"
"strconv"
"syscall"
)
func init() {
runtime.LockOSThread()
}
func main() {
flag.Parse()
pid := getPid()
searchVal := getSearchVal()
attachToProcess(pid)
matchingAddresses := searchRegions(searchVal, pid)
for len(matchingAddresses) > 1 {
fmt.Println("num matches:", len(matchingAddresses))
resumeProcess(pid)
searchVal = getSearchVal()
stopProcess(pid)
matchingAddresses = searchOldMatches(searchVal, matchingAddresses, pid)
}
if len(matchingAddresses) == 1 {
fmt.Println("found a single match!")
replaceVal := getReplacementValue()
pokeData(pid, replaceVal, matchingAddresses[0])
} else {
fmt.Println("no matches found")
}
detach(pid)
}
func getPid() int {
if flag.NArg() > 0 {
pid, err := strconv.Atoi(flag.Arg(0))
if err != nil {
panic(err)
}
return pid
} else {
panic("target process id required")
}
}
func getSearchVal() []byte {
var val int32
getIntFromUser("value to find: ", &val)
return intToBytes(val)
}
func getReplacementValue() []byte {
var val int32
getIntFromUser("replacement value: ", &val)
return intToBytes(val)
}
func getIntFromUser(prompt string, i interface{}) {
for true {
fmt.Print(prompt)
_, err := fmt.Scanf("%d", i)
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
func attachToProcess(pid int) {
err := syscall.PtraceAttach(pid)
if err != nil {
panic(err)
}
waitForStop(pid)
fmt.Println("successfully attached to", pid)
}
func waitForStop(pid int) {
var status syscall.WaitStatus
_, err := syscall.Wait4(pid, &status, 0, nil)
if err != nil || !status.Stopped() {
fmt.Println("target didn't stop")
}
}
func detach(pid int) {
fmt.Println("detaching from", pid)
err := syscall.PtraceDetach(pid)
if err != nil {
panic(err)
}
fmt.Println("detached from", pid)
}
func pokeData(pid int, dataBytes []byte, addr int64) {
fmt.Println("replacing with value:", bytesToInt(dataBytes))
_, err := syscall.PtracePokeData(pid, uintptr(addr), dataBytes)
if err != nil {
fmt.Println("unable to write data")
}
}
func resumeProcess(pid int) {
err := syscall.PtraceCont(pid, 0)
if err != nil {
panic(err)
}
}
func stopProcess(pid int) {
err := syscall.Kill(pid, syscall.SIGSTOP)
if err != nil {
panic(err)
}
waitForStop(pid)
}
func searchOldMatches(val []byte, oldMatches []int64, pid int) []int64 {
var matches []int64
mem := openMemFile(pid)
defer mem.Close()
matches = appendMatches(val, matches, oldMatches, mem)
return matches
}
func appendMatches(val []byte, matches, oldMatches []int64, mem *os.File) []int64 {
buf := make([]byte, len(val))
for _, addr := range oldMatches {
mem.Seek(addr, 0)
current := fill(buf, mem)
if bytes.Equal(val, current) {
matches = append(matches, addr)
}
}
return matches
}
type Region struct {
start, end int64
}
func searchRegions(val []byte, pid int) []int64 {
var matches []int64
regions := getWritableRegions(pid)
mem := openMemFile(pid)
defer mem.Close()
for _, region := range regions {
matches = appendRegionMatches(val, matches, region, mem)
}
return matches
}
func openMemFile(pid int) *os.File {
file, err := os.Open("/proc/" + strconv.Itoa(pid) + "/mem")
if err != nil {
panic(err)
}
return file
}
func appendRegionMatches(val []byte, matches []int64, region Region, mem *os.File) []int64 {
var bufSize int64 = 4096
buf := make([]byte, bufSize)
mem.Seek(region.start, 0)
for offset := region.start; offset < region.end; offset += bufSize {
segmentLen := min(bufSize, region.end-offset)
matches = appendSegmentMatches(val, matches, offset, fill(buf[:segmentLen], mem))
}
return matches
}
func appendSegmentMatches(val []byte, matches []int64, position int64, segment []byte) []int64 {
size := len(val)
for offset := 0; offset < len(segment); offset += size {
if bytes.Equal(val, segment[offset:offset+size]) {
matches = append(matches, position+int64(offset))
}
}
return matches
}
func fill(buf []byte, mem *os.File) []byte {
_, err := io.ReadFull(mem, buf)
if err != nil {
panic(err)
}
return buf
}
func intToBytes(data int32) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, data)
if err != nil {
fmt.Println(err)
}
return buf.Bytes()
}
func bytesToInt(data []byte) int32 {
var result int32
err := binary.Read(bytes.NewBuffer(data), binary.LittleEndian, &result)
if err != nil {
fmt.Println(err)
}
return result
}
func getWritableRegions(pid int) []Region {
var regions []Region
file, err := os.Open("/proc/" + strconv.Itoa(pid) + "/maps")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
region := getRegionIfMatch(line)
if region != nil {
regions = append(regions, *region)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
return regions
}
var memSegRE = regexp.MustCompile(`([\da-f]+)-([\da-f]+) +.w.. +.*`)
func getRegionIfMatch(line string) *Region {
matches := memSegRE.FindStringSubmatch(line)
if matches != nil {
var region Region
region.start, _ = strconv.ParseInt(matches[1], 16, 64)
region.end, _ = strconv.ParseInt(matches[2], 16, 64)
return ®ion
}
return nil
}
func min(a, b int64) int64 {
if a < b {
return a
} else {
return b
}
}