华为开发者空间
安卓 dex 通用脱壳技术研究(三)
安卓 dex 通用脱壳技术研究(三)
http://my.oschina.net/cve2015/blog/508918摘要 本文剖析安卓Dalvik 解释器 Portable 的原理与实现,并通过修改 Dalvik 虚拟机实现 dex 文件的通用脱壳方案;本方法同样适应于 ART 虚拟机;?1234567
http://my.oschina.net/cve2015/blog/508918
摘要
本文剖析安卓 Dalvik 解释器 Portable 的原理与实现,并通过修改 Dalvik 虚拟机实现 dex 文件的通用脱壳方案;本方法同样适应于 ART 虚拟机;
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
此为DexHunter实现的主要功能,进行内存dump,将class_def_items中dump出classdef和extra部分
*/
void
* DumpClass(
void
*parament)
{
while
(timer_flag) {
sleep(5);
}
DvmDex* pDvmDex=((
struct
arg*)parament)->pDvmDex;
//pDvmDex代表一个dex文件
Object *loader=((
struct
arg*)parament)->loader;
DexFile* pDexFile=pDvmDex->pDexFile;
MemMapping * mem=&pDvmDex->memMap;
u4
time
=dvmGetRelativeTimeMsec();
ALOGI(
"GOT IT begin: %d ms"
,
time
);
char
*path =
new
char
[100];
strcpy
(path,dumppath);
strcat
(path,
"classdef"
);
//构造classdef文件路径
FILE
*fp =
fopen
(path,
"wb+"
);
strcpy
(path,dumppath);
strcat
(path,
"extra"
);
//构造extra文件路径
FILE
*fp1 =
fopen
(path,
"wb+"
);
uint32_t mask=0x3ffff;
char
padding=0;
const
char
* header=
"Landroid"
;
unsigned
int
num_class_defs=pDexFile->pHeader->classDefsSize;
//dex文件的Header域,class_defs_size,标示了class_def域有几个class_def_item
uint32_t total_pointer = mem->length-uint32_t(pDexFile->baseAddr-(
const
u1*)mem->addr);
uint32_t rec=total_pointer;
while
(total_pointer&3) {
total_pointer++;
}
int
inc=total_pointer-rec;
uint32_t start = pDexFile->pHeader->classDefsOff+
sizeof
(DexClassDef)*num_class_defs;
uint32_t end = (uint32_t)((
const
u1*)mem->addr+mem->length-pDexFile->baseAddr);
for
(
size_t
i=0;i<num_class_defs;i++)
//遍历所有class_def_item
{
bool
need_extra=
false
;
ClassObject * clazz=NULL;
const
u1* data=NULL;
DexClassData* pData = NULL;
bool
pass=
false
;
const
DexClassDef *pClassDef = dexGetClassDef(pDvmDex->pDexFile, i);
const
char
*descriptor = dexGetClassDescriptor(pDvmDex->pDexFile,pClassDef);
if
(!
strncmp
(header,descriptor,8)||!pClassDef->classDataOff)
{
pass=
true
;
goto
classdef;
}
clazz = dvmDefineClass(pDvmDex, descriptor, loader);
//First Step:class加载
if
(!clazz) {
continue
;
}
ALOGI(
"GOT IT class: %s"
,descriptor);
if
(!dvmIsClassInitialized(clazz)) {
//Second Step:class初始化,遍历所有类进行初始化
if
(dvmInitClass(clazz)){
//与dvmIsClassInitialized()函数共同完成初始化
ALOGI(
"GOT IT init: %s"
,descriptor);
}
}
if
(pClassDef->classDataOff<start || pClassDef->classDataOff>end)
{
need_extra=
true
;
//是不是需要extra这一块,当存在class_data_off不在dex范围内时,就需要
}
data=dexGetClassData(pDexFile, pClassDef);
pData = ReadClassData(&data);
if
(!pData) {
continue
;
}
if
(pData->directMethods) {
for
(uint32_t i=0; i<pData->header.directMethodsSize; i++) {
Method *method = &(clazz->directMethods[i]);
uint32_t ac = (method->accessFlags) & mask;
ALOGI(
"GOT IT direct method name %s.%s"
,descriptor,method->name);
if
(!method->insns||ac&ACC_NATIVE) {
if
(pData->directMethods[i].codeOff) {
need_extra =
true
;
pData->directMethods[i].accessFlags=ac;
pData->directMethods[i].codeOff=0;
}
continue
;
}
u4 codeitem_off = u4((
const
u1*)method->insns-16-pDexFile->baseAddr);
if
(ac != pData->directMethods[i].accessFlags)
{
ALOGI(
"GOT IT method ac"
);
need_extra=
true
;
pData->directMethods[i].accessFlags=ac;
}
if
(codeitem_off!=pData->directMethods[i].codeOff&&((codeitem_off>=start&&codeitem_off<=end)||codeitem_off==0)) {
ALOGI(
"GOT IT method code"
);
need_extra=
true
;
pData->directMethods[i].codeOff=codeitem_off;
}
if
((codeitem_off<start || codeitem_off>end) && codeitem_off!=0) {
//如果code_item_off不在dex文件范围内,则写入extra; fp1为extra的句柄
//这里使用的是slider.pptx,中的第二种方案,见p42
need_extra=
true
;
pData->directMethods[i].codeOff = total_pointer;
DexCode *code = (DexCode*)((
const
u1*)method->insns-16);
uint8_t *item=(uint8_t *) code;
int
code_item_len = 0;
if
(code->triesSize) {
const
u1 * handler_data = dexGetCatchHandlerData(code);
const
u1** phandler=(
const
u1**)&handler_data;
uint8_t * tail=codeitem_end(phandler);
code_item_len = (
int
)(tail-item);
}
else
{
code_item_len = 16+code->insnsSize*2;
}
ALOGI(
"GOT IT method code changed"
);
fwrite
(item,1,code_item_len,fp1);
fflush
(fp1);
total_pointer+=code_item_len;
while
(total_pointer&3) {
fwrite
(&padding,1,1,fp1);
fflush
(fp1);
total_pointer++;
}
}
}
}
if
(pData->virtualMethods) {
for
(uint32_t i=0; i<pData->header.virtualMethodsSize; i++) {
Method *method = &(clazz->virtualMethods[i]);
uint32_t ac = (method->accessFlags) & mask;
ALOGI(
"GOT IT virtual method name %s.%s"
,descriptor,method->name);
if
(!method->insns||ac&ACC_NATIVE) {
if
(pData->virtualMethods[i].codeOff) {
need_extra =
true
;
pData->virtualMethods[i].accessFlags=ac;
pData->virtualMethods[i].codeOff=0;
}
continue
;
}
u4 codeitem_off = u4((
const
u1 *)method->insns - 16 - pDexFile->baseAddr);
if
(ac != pData->virtualMethods[i].accessFlags)
{
ALOGI(
"GOT IT method ac"
);
need_extra=
true
;
pData->virtualMethods[i].accessFlags=ac;
}
if
(codeitem_off!=pData->virtualMethods[i].codeOff&&((codeitem_off>=start&&codeitem_off<=end)||codeitem_off==0)) {
ALOGI(
"GOT IT method code"
);
need_extra=
true
;
pData->virtualMethods[i].codeOff=codeitem_off;
}
if
((codeitem_off<start || codeitem_off>end)&&codeitem_off!=0) {
need_extra=
true
;
pData->virtualMethods[i].codeOff = total_pointer;
DexCode *code = (DexCode*)((
const
u1*)method->insns-16);
uint8_t *item=(uint8_t *) code;
int
code_item_len = 0;
if
(code->triesSize) {
const
u1 *handler_data = dexGetCatchHandlerData(code);
const
u1** phandler=(
const
u1**)&handler_data;
uint8_t * tail=codeitem_end(phandler);
code_item_len = (
int
)(tail-item);
}
else
{
code_item_len = 16+code->insnsSize*2;
}
ALOGI(
"GOT IT method code changed"
);
fwrite
(item,1,code_item_len,fp1);
fflush
(fp1);
total_pointer+=code_item_len;
while
(total_pointer&3) {
fwrite
(&padding,1,1,fp1);
fflush
(fp1);
total_pointer++;
}
}
}
}
classdef:
DexClassDef temp=*pClassDef;
uint8_t *p = (uint8_t *)&temp;
if
(need_extra) {
ALOGI(
"GOT IT classdata before"
);
int
class_data_len = 0;
uint8_t *out = EncodeClassData(pData,class_data_len);
if
(!out) {
continue
;
}
temp.classDataOff = total_pointer;
fwrite
(out,1,class_data_len,fp1);
fflush
(fp1);
total_pointer+=class_data_len;
while
(total_pointer&3) {
fwrite
(&padding,1,1,fp1);
fflush
(fp1);
total_pointer++;
}
free
(out);
ALOGI(
"GOT IT classdata written"
);
}
else
{
if
(pData) {
free
(pData);
}
}
if
(pass) {
temp.classDataOff=0;
temp.annotationsOff=0;
}
ALOGI(
"GOT IT classdef"
);
fwrite
(p,
sizeof
(DexClassDef), 1, fp);
fflush
(fp);
}
fclose
(fp1);
fclose
(fp);
//目前已经准备好了part1,classdef,data和extra 4部分文件
strcpy
(path,dumppath);
strcat
(path,
"whole.dex"
);
//组合4部分,并写入whole.dex
fp =
fopen
(path,
"wb+"
);
rewind
(fp);
int
fd=-1;
int
r=-1;
int
len=0;
char
*addr=NULL;
struct
stat st;
strcpy
(path,dumppath);
strcat
(path,
"part1"
);
fd=open(path,O_RDONLY,0666);
if
(fd==-1) {
return
NULL;
}
r=fstat(fd,&st);
if
(r==-1){
close(fd);
return
NULL;
}
len=st.st_size;
addr=(
char
*)mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
fwrite
(addr,1,len,fp);
fflush
(fp);
munmap(addr,len);
close(fd);
strcpy
(path,dumppath);
strcat
(path,
"classdef"
);
fd=open(path,O_RDONLY,0666);
if
(fd==-1) {
return
NULL;
}
r=fstat(fd,&st);
if
(r==-1){
close(fd);
return
NULL;
}
len=st.st_size;
addr=(
char
*)mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
fwrite
(addr,1,len,fp);
fflush
(fp);
munmap(addr,len);
close(fd);
strcpy
(path,dumppath);
strcat
(path,
"data"
);
fd=open(path,O_RDONLY,0666);
if
(fd==-1) {
return
NULL;
}
r=fstat(fd,&st);
if
(r==-1){
close(fd);
return
NULL;
}
len=st.st_size;
addr=(
char
*)mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
fwrite
(addr,1,len,fp);
fflush
(fp);
munmap(addr,len);
close(fd);
while
(inc>0) {
fwrite
(&padding,1,1,fp);
fflush
(fp);
inc--;
}
strcpy
(path,dumppath);
strcat
(path,
"extra"
);
fd=open(path,O_RDONLY,0666);
if
(fd==-1) {
return
NULL;
}
r=fstat(fd,&st);
if
(r==-1){
close(fd);
return
NULL;
}
len=st.st_size;
addr=(
char
*)mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
fwrite
(addr,1,len,fp);
fflush
(fp);
munmap(addr,len);
close(fd);
fclose
(fp);
delete
path;
time
=dvmGetRelativeTimeMsec();
ALOGI(
"GOT IT end: %d ms"
,
time
);
return
NULL;
}
//------------------------added end----------------------//
|
- 9587
- 0
- 0
- 0
扫一扫分享内容
- 分享
回到
顶部
顶部
所有评论(0)