-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsupermonkey.go
95 lines (79 loc) · 2.25 KB
/
supermonkey.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
package supermonkey
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"unsafe"
"github.com/cch123/supermonkey/internal/bouk"
"github.com/cch123/supermonkey/internal/nm"
)
type PatchGuard = bouk.PatchGuard
var (
symbolTable = map[string]uintptr{}
)
// Patch replaces a function with another
func Patch(target, replacement interface{}) *PatchGuard {
return bouk.Patch(target, replacement)
}
func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *PatchGuard {
return bouk.PatchInstanceMethod(target, methodName, replacement)
}
// PatchByFullSymbolName needs user to provide the full symbol path
func PatchByFullSymbolName(symbolName string, patchFunc interface{}) *PatchGuard {
addr := symbolTable[symbolName]
if addr == 0 {
prompt := fmt.Sprintf(`The symbol is %v, and the patch target addr is 0, there may be 2 possible reasons:
1. the function is inlined, please add //go:noinline to function comment or add -l to gcflags
2. your input for symbolName or pkg/obj/method is wrong, check by using go tool nm {your_bin_file} %s`, symbolName, "\n")
colorPrint(prompt, green)
similarSymbols(symbolName)
os.Exit(1)
}
return bouk.PatchSymbol(unsafe.Pointer(addr), patchFunc)
}
// UnpatchAll unpatches all functions
func UnpatchAll() {
bouk.UnpatchAll()
}
func init() {
path, _ := os.Executable()
content, _ := nm.Parse(path)
lines := strings.Split(content, "\n")
for _, line := range lines {
line := strings.TrimSpace(line)
arr := strings.Split(line, " ")
if len(arr) < 3 {
continue
}
funcSymbol, addr := arr[2], arr[0]
addrUint, _ := strconv.ParseUint(addr, 16, 64)
symbolTable[funcSymbol] = uintptr(addrUint)
}
}
// Finds similar symbols
func similarSymbols(symbolName string) {
similarList := make([]string, 0)
for s, _ := range symbolTable {
if strings.Contains(s, symbolName) {
similarList = append(similarList, " - "+s)
}
}
if len(similarList) == 0 {
return
}
if len(similarList) == 1 {
colorPrint("The most similar symbol is", red)
} else if len(similarList) > 1 {
colorPrint("The most similar symbols are", red)
}
colorPrint(strings.Join(similarList, "\n"), red)
}
const (
red = 31
green = 32
)
func colorPrint(s string, f int) {
fmt.Printf("\033[0;%dm%s\033[0m\n", f, s)
}