-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
189 lines (168 loc) · 4.46 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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"strconv"
"strings"
"github.com/aktsk/apk-medit/cmd"
prompt "github.com/c-bata/go-prompt"
)
var appPID string
var addrCache []cmd.Found
var withoutPtrace bool
var specificPID string
func executor(in string) {
if in == "ps" {
if specificPID == "" {
if pid, err := cmd.Plist(); err != nil {
log.Fatal(err)
} else if pid != "" {
appPID = pid
}
} else {
fmt.Printf("The Target PID is already set to %s.\n", specificPID)
}
} else if strings.HasPrefix(in, "attach") {
slice := strings.Split(in, " ")
var pid string
if len(slice) > 1 {
pid = slice[1]
} else if appPID != "" {
pid = appPID
} else {
fmt.Println("PID cannot be specified.")
}
cmd.Attach(pid)
} else if strings.HasPrefix(in, "find") {
inputSlice := strings.Split(in, " ")
dataType := "all"
targetVal := inputSlice[1]
if len(inputSlice) < 1 {
fmt.Println("Target value cannot be specified.")
return
}
if len(inputSlice) == 3 {
targetVal = inputSlice[2]
dataType = inputSlice[1]
}
foundAddr, _ := cmd.Find(appPID, targetVal, dataType)
addrCache = foundAddr
} else if strings.HasPrefix(in, "filter") {
if len(addrCache) == 0 {
fmt.Println("No previous results. ")
return
}
slice := strings.Split(in, " ")
if len(slice) == 1 {
fmt.Println("Target value cannot be specified.")
return
}
foundAddr, err := cmd.Filter(appPID, slice[1], addrCache)
if err != nil {
fmt.Println(err)
}
addrCache = foundAddr
} else if strings.HasPrefix(in, "patch") {
slice := strings.Split(in, " ")
if len(slice) == 1 {
fmt.Println("Target value cannot be specified.")
return
}
if withoutPtrace {
err := cmd.PatchWithoutPtrace(appPID, slice[1], addrCache)
if err != nil {
fmt.Println(err)
}
} else {
err := cmd.PatchWithPtrace(appPID, slice[1], addrCache)
if err != nil {
fmt.Println(err)
}
}
} else if in == "detach" {
if err := cmd.Detach(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else if strings.HasPrefix(in, "dump") {
inputSlice := strings.Split(in, " ")
beginAddr, err := parseAddr(inputSlice[1])
if err != nil {
fmt.Println(err)
return
}
endAddr, err := parseAddr(inputSlice[2])
if err != nil {
fmt.Println(err)
return
}
if err := cmd.Dump(appPID, beginAddr, endAddr); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else if in == "exit" {
fmt.Println("Bye!")
os.Exit(0)
} else if in == "" {
} else {
fmt.Println("Command not found.")
}
return
}
func parseAddr(arg string) (int, error) {
arg = strings.Replace(arg, "0x", "", 1)
address, err := strconv.ParseInt(arg, 16, 64)
if err == nil {
return int(address), nil
}
address, err = strconv.ParseInt(arg, 10, 64)
if err == nil {
return int(address), nil
}
return 0, err
}
func completer(t prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "find <int>", Description: "Search the specified integer."},
{Text: "find <datatype> <int>", Description: "Types can be specified are string, word, dword, qword."},
{Text: "filter <int>", Description: "Filter previous search results that match the current search results."},
{Text: "patch <int>", Description: "Write the specified value on the address found by search."},
{Text: "attach", Description: "Attach to the target process by ptrace."},
{Text: "detach", Description: "Detach from the attached process."},
{Text: "ps", Description: "Find the target process and if there is only one, specify it as the target."},
{Text: "dump <begin addr> <end addr>", Description: "Display memory dump like hexdump"},
{Text: "exit"},
}
}
func main() {
flag.BoolVar(&withoutPtrace, "without-ptrace", false, "Memory modification without ptrace, which is not available in Android 10 and later")
flag.StringVar(&specificPID, "pid", "", "Attach to a process with this pid")
flag.Parse()
// for ptrace attach
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if specificPID == "" {
if pid, err := cmd.Plist(); err != nil {
log.Fatal(err)
} else if pid != "" {
appPID = pid
}
} else {
appPID = specificPID
}
addrCache = []cmd.Found{}
p := prompt.New(
executor,
completer,
prompt.OptionTitle("medit: MEmory eDIT tool"),
prompt.OptionPrefix("> "),
prompt.OptionInputTextColor(prompt.Cyan),
prompt.OptionPrefixTextColor(prompt.DarkBlue),
prompt.OptionPreviewSuggestionTextColor(prompt.Green),
prompt.OptionDescriptionTextColor(prompt.DarkGray),
)
p.Run()
}