-
Notifications
You must be signed in to change notification settings - Fork 57
/
hook.js
226 lines (204 loc) · 7.76 KB
/
hook.js
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
/**
* hook write
* search dex magic word
* dump dex
*/
function hook_write(){
var read_buf_len = 7;
var file_name_partial = "/sdcard/hook_write_dex_";
var dex_file_magic = "dex\n035";
Interceptor.attach(Module.findExportByName("libc.so" , "write"), {
//write(FILE *fd, buf, len(buf))
onEnter: function(args) {
send("write("+args[0]+", "+Memory.readCString(args[1], read_buf_len)+", "+args[2]+")");
if(Memory.readCString(args[1], read_buf_len) == dex_file_magic){
var filename = get_dex_file_name(file_name_partial);
var outFile = new File(filename, "w");
var buf = Memory.readByteArray(args[1], parseInt(args[2]));
outFile.write(buf);
outFile.flush();
outFile.close();
send("write finished!!!");
}},
onLeave:function(retval){
}
});
}
/**
* hook memcmp
* search dex magic word
* dump dex
*/
function hook_memcmp() {
var read_buf_len = 7;
var file_name_partial = "/sdcard/hook_memcmp_dex_";
var dex_file_magic = "dex\n035";
Interceptor.attach(Module.findExportByName("libc.so" , "memcmp"), {
onEnter: function(args) {
send("show args1 is: "+Memory.readCString(args[0], parseInt(args[2]))+" and args2 is: "+Memory.readCString(args[1], parseInt(args[2]))+" and length is: "+args[2]);
if(Memory.readCString(args[0],read_buf_len) == dex_file_magic){
var filename = get_dex_file_name(file_name_partial);
var outFile = new File(filename, "w");
var buf = Memory.readByteArray(args[0], parseInt(args[2]));
outFile.write(buf);
outFile.flush();
outFile.close();
send("memcmp hooked ");
}
if(Memory.readCString(args[1],read_buf_len) == dex_file_magic){
var filename = get_dex_file_name(file_name_partial);
var outFile = new File(filename, "w");
var buf = Memory.readByteArray(args[1], parseInt(args[2]));
outFile.write(buf);
outFile.flush();
outFile.close();
send("memcmp hooked ");
}
},
onLeave:function(retval){
}
});
}
/**
* 360
*/
function hook_memcpy() {
var read_buf_len = 7;
var file_name_partial = "/sdcard/hook_memcpy_dex_";
var dex_file_magic = "dex\n035";
//void *memcpy(void *dest, const void *src, size_t n);
Interceptor.attach(Module.findExportByName("libc.so" , "memcpy"), {
onEnter: function (args) {
this.dest = args[0];
this.src = args[1];
this.size = args[2].toInt32();
},
onLeave: function (retval) {
if(Memory.readCString(this.dest, read_buf_len) == dex_file_magic){
send("hook memcpy("+Memory.readCString(this.dest, read_buf_len)+", "+Memory.readCString(this.src, read_buf_len)+", "+this.size+")");
var filename = get_dex_file_name(file_name_partial);
var buf = Memory.readByteArray(this.dest, this.size);
var outFile = new File(filename, "wb+");
outFile.write(buf);
outFile.flush();
outFile.close();
send("hook memcpy finished!!!");
}
}
});
}
function hook_open() {
Interceptor.attach(Module.findExportByName("libc.so" , "open"), {
onEnter: function (args) {
send("hook open("+Memory.readCString(args[0])+", "+args[1].toInt32()+")");
},
onLeave: function (retval) {
}
});
}
function hook_fopen() {
Interceptor.attach(Module.findExportByName("libc.so" , "fopen"), {
onEnter: function (args) {
send("hook fopen("+Memory.readCString(args[0])+", "+Memory.readCString(args[1])+")");
},
onLeave: function (retval) {
}
});
}
function hook_mmap() {
Interceptor.attach(Module.findExportByName("libc.so" , "mmap"), {
onEnter: function (args) {
send("hook mmap("+args[0]+", "+args[1]+", "+args[2]+", "+args[3]+", "+args[4]+", "+args[5]+")");
},
onLeave: function (retval) {
send("mmap result is: "+Memory.readCString(retval, 5));
}
});
}
function hook_munmap() {
Interceptor.attach(Module.findExportByName("libc.so" , "mmap"), {
onEnter: function (args) {
send("hook munmap("+args[0]+", "+args[1]+")");
},
onLeave: function (retval) {
}
});
}
function hook_snprintf() {
Interceptor.attach(Module.findExportByName("libc.so" , "snprintf"), {
onEnter: function (args) {
this.str = args[0];
},
onLeave: function (retval) {
send("hook snprintf str is: "+Memory.readCString(this.str));
}
});
}
function hook_openMemory() {
var read_buf_len = 7;
var file_name_partial = "/data/data/com.me.hookme/hook_openMemory_dex_";
var dex_file_magic = "dex\n035";
// const DexFile* DexFile::OpenMemory(const byte* base, size_t size, const std::string& location, uint32_t location_checksum, MemMap* mem_map, std::string* error_msg)
Interceptor.attach(Module.findExportByName("libart.so", "_ZN3art7DexFile10OpenMemoryEPKhjRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEjPNS_6MemMapES2_PS9_"), {
onEnter: function(args) {
if(Memory.readCString(args[0], read_buf_len) == dex_file_magic){
var filename = get_dex_file_name(file_name_partial);
var outFile = new File(filename, "w");
var buf = Memory.readByteArray(args[0], args[1].toInt32());
outFile.write(buf);
outFile.flush();
outFile.close();
send("hook openMemory finished!!!");
}
},
onLeave: function(retval){
}
});
}
function hook_defineClass() {
//_ZN3art11ClassLinker11DefineClassEPKcNS_6HandleINS_6mirror11ClassLoaderEEERKNS_7DexFileERKNS7_8ClassDefE
//mirror::Class* ClassLinker::DefineClass(const char* descriptor, Handle<mirror::ClassLoader> class_loader, const DexFile& dex_file, const DexFile::ClassDef& dex_class_def)
//DefineClass(descriptor.c_str(), class_loader, *dex_file,*dex_class_def);
Interceptor.attach(Module.findExportByName("libart.so" , "_ZN3art11ClassLinker11DefineClassEPKcNS_6HandleINS_6mirror11ClassLoaderEEERKNS_7DexFileERKNS7_8ClassDefE"), {
onEnter: function (args) {
send("args[2] is: "+Memory.readCString(args[2], 7));
},
onLeave: function (retval) {
}
});
}
function hook_dlopen() {
Interceptor.attach(Module.findExportByName("libc.so" , "dlopen"), {
onEnter: function (args) {
send("hook dlopen("+Memory.readCString(args[0]) +", "+ args[1]+")");
},
onLeave: function (retval) {
send("hook dlopen result: "+retval);
}
});
}
function hook_dlsym() {
Interceptor.attach(Module.findExportByName("libc.so" , "dlsym"), {
onEnter: function (args) {
send("hook dlsym("+args[0] +", "+ Memory.readCString(args[1])+")");
},
onLeave: function (retval){
send("hook dlsym result: "+retval);
}
});
}
function hook_registerNativeMethods() {
Interceptor.attach(Module.findExportByName("libart.so" , "_ZN3art21RegisterNativeMethodsEP7_JNIEnvPKcPK15JNINativeMethodi"), {
onEnter: function (args){
},
onLeave: function (retval){
}
});
}
function get_dex_file_name(file_name_partial) {
var date = new Date();
var time = date.getTime();
var file_name = file_name_partial + time + ".dex";
return file_name;
}
setTimeout(hook_openMemory(),0);