forked from GuoQiang1993/Frida-Apk-Unpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dexDump.js
executable file
·227 lines (203 loc) · 8.3 KB
/
dexDump.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
227
'use strict';
/**
* Author: guoqiangck
* Create: 2019/6/11
* Dump dex file for packaged apk
* Hook art/runtime/dex_file.cc OpenMemory or OpenCommon
* Support Version: Android 4.4 and later versions
*/
function LogPrint(log) {
var theDate = new Date();
var hour = theDate.getHours();
var minute = theDate.getMinutes();
var second = theDate.getSeconds();
var mSecond = theDate.getMilliseconds()
hour < 10 ? hour = "0" + hour : hour;
minute < 10 ? minute = "0" + minute : minute;
second < 10 ? second = "0" + second : second;
mSecond < 10 ? mSecond = "00" + mSecond : mSecond < 100 ? mSecond = "0" + mSecond : mSecond;
var time = hour + ":" + minute + ":" + second + ":" + mSecond;
console.log("[" + time + "] " + log);
}
function getAndroidVersion(){
var version = 0;
if(Java.available){
var versionStr = Java.androidVersion;
version = versionStr.slice(0,1);
}else{
LogPrint("Error: cannot get android version");
}
LogPrint("Android Version: " + version);
return version;
}
function getFunctionName(){
var i = 0;
var functionName = "";
// Android 4: hook dvmDexFileOpenPartial
// Android 5: hook OpenMemory
// after Android 5: hook OpenCommon
if(getAndroidVersion() > 4){ // android 5 and later version
var artExports = Module.enumerateExportsSync("libart.so");
for(i = 0; i< artExports.length; i++){
if(artExports[i].name.indexOf("OpenMemory") !== -1){
functionName = artExports[i].name;
LogPrint("index " + i + " function name: "+ functionName);
break;
}else if(artExports[i].name.indexOf("OpenCommon") !== -1){
functionName = artExports[i].name;
LogPrint("index " + i + " function name: "+ functionName);
break;
}
}
}else{ //android 4
var dvmExports = Module.enumerateExportsSync("libdvm.so");
if(dvmExports.length !== 0){ // check libdvm.so first
for(i = 0; i< dvmExports.length; i++){
if(dvmExports[i].name.indexOf("dexFileParse") !== -1){
functionName = dvmExports[i].name;
LogPrint("index " + i + " function name: "+ functionName);
break;
}
}
}else{ // if not load libdvm.so, check libart.so
dvmExports = Module.enumerateExportsSync("libart.so");
for(i = 0; i< dvmExports.length; i++){
if(dvmExports[i].name.indexOf("OpenMemory") !== -1){
functionName = dvmExports[i].name;
LogPrint("index " + i + " function name: "+ functionName);
break;
}
}
}
}
return functionName;
}
function getProcessName(){
var processName = "";
var fopenPtr = Module.findExportByName("libc.so", "fopen");
var fopenFunc = new NativeFunction(fopenPtr, 'pointer', ['pointer', 'pointer']);
var fgetsPtr = Module.findExportByName("libc.so", "fgets");
var fgetsFunc = new NativeFunction(fgetsPtr, 'int', ['pointer', 'int', 'pointer']);
var fclosePtr = Module.findExportByName("libc.so", "fclose");
var fcloseFunc = new NativeFunction(fclosePtr, 'int', ['pointer']);
var pathPtr = Memory.allocUtf8String("/proc/self/cmdline");
var openFlagsPtr = Memory.allocUtf8String("r");
var fp = fopenFunc(pathPtr, openFlagsPtr);
if(fp.isNull() === false){
var buffData = Memory.alloc(128);
var ret = fgetsFunc(buffData, 128, fp);
if(ret !== 0){
processName = Memory.readCString(buffData);
LogPrint("processName " + processName);
}
fcloseFunc(fp);
}
return processName;
}
function arraybuffer2hexstr(buffer)
{
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
);
return hexArr.join(' ');
}
function checkDexMagic(dataAddr){
var magicMatch = true;
var magicFlagHex = [0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00];
for(var i = 0; i < 8; i++){
if(Memory.readU8(ptr(dataAddr).add(i)) !== magicFlagHex[i]){
magicMatch = false;
break;
}
}
return magicMatch;
}
function checkOdexMagic(dataAddr){
var magicMatch = true;
var magicFlagHex = [0x64, 0x65, 0x79, 0x0a, 0x30, 0x33, 0x36, 0x00];
for(var i = 0; i < 8; i++){
if(Memory.readU8(ptr(dataAddr).add(i)) !== magicFlagHex[i]){
magicMatch = false;
break;
}
}
return magicMatch;
}
function dumpDex(moduleFuncName, processName){
if(moduleFuncName !== ""){
var hookFunction;
if(getAndroidVersion() > 4){ // android 5 and later version
hookFunction = Module.findExportByName("libart.so", moduleFuncName);
}else{ // android 4
hookFunction = Module.findExportByName("libdvm.so", moduleFuncName); // check libdvm.so first
if(hookFunction == null) {
hookFunction = Module.findExportByName("libart.so", moduleFuncName); //// if not load libdvm.so, check libart.so
}
}
Interceptor.attach(hookFunction,{
onEnter: function(args){
var begin = 0;
var dexMagicMatch = false;
var odexMagicMatch = false;
dexMagicMatch = checkDexMagic(args[0]);
if(dexMagicMatch === true){
begin = args[0];
}else{
odexMagicMatch = checkOdexMagic(args[0]);
if(odexMagicMatch === true){
begin = args[0];
}
}
if(begin === 0){
dexMagicMatch = checkDexMagic(args[1]);
if(dexMagicMatch === true){
begin = args[1];
}else{
odexMagicMatch = checkOdexMagic(args[1]);
if(odexMagicMatch === true){
begin = args[1];
}
}
}
if(dexMagicMatch === true){
LogPrint("magic : " + Memory.readUtf8String(begin));
//console.log(hexdump(begin, { offset: 0, header: false, length: 64, ansi: false }));
var address = parseInt(begin,16) + 0x20;
var dex_size = Memory.readInt(ptr(address));
LogPrint("dex_size :" + dex_size);
var dex_path = "/data/data/" + processName + "/" + dex_size + ".dex";
var dex_file = new File(dex_path, "wb");
dex_file.write(Memory.readByteArray(begin, dex_size));
dex_file.flush();
dex_file.close();
LogPrint("dump dex success, saved path: " + dex_path + "\n");
}else if(odexMagicMatch === true){
LogPrint("magic : " + Memory.readUtf8String(begin));
//console.log(hexdump(begin, { offset: 0, header: false, length: 64, ansi: false }));
var address = parseInt(begin,16) + 0x0C;
var odex_size = Memory.readInt(ptr(address));
LogPrint("odex_size :" + odex_size);
var odex_path = "/data/data/" + processName + "/" + odex_size + ".odex";
var odex_file = new File(odex_path, "wb");
odex_file.write(Memory.readByteArray(begin, odex_size));
odex_file.flush();
odex_file.close();
LogPrint("dump odex success, saved path: " + odex_path + "\n");
}
},
onLeave: function(retval){
}
});
}else{
LogPrint("Error: cannot find correct module function.");
}
}
//start dump dex file
var moduleFucntionName = getFunctionName();
var processName = getProcessName();
if(moduleFucntionName !== "" && processName !== ""){
dumpDex(moduleFucntionName, processName);
}