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
|
void init_getString(){
int successFlag = 1;
unsigned int mySectionSize;
unsigned int mySectionOffset;
unsigned int sectionHeaderOffset;
unsigned int sectionHeaderSize;
unsigned int stringSectionHeaderOffset;
unsigned int addNewSectionOffset;
unsigned int nsize;
unsigned long base;
unsigned long text_addr;
unsigned int i;
Elf32_Ehdr *ehdr;
//获取so的起始地址
base = getLibAddr();
ehdr = (Elf32_Ehdr *)base;
//获取新加section的偏移值
addNewSectionOffset = ehdr->e_entry;
//从内存读取加密section的offset和size
mySectionOffset = *(unsigned int *)(base + addNewSectionOffset);
text_addr = mySectionOffset + base;
mySectionSize = *(unsigned int *) (base + addNewSectionOffset + 4);
nsize = mySectionSize/4096 + (mySectionSize%4096 == 0 ? 0 : 1);
__android_log_print(ANDROID_LOG_INFO, "JNITag", "addNewSectionOffset = 0x%x, addNewSectionVirtualOffset = 0x%x", addNewSectionOffset, addNewSectionOffset + base);
__android_log_print(ANDROID_LOG_INFO, "JNITag", "mySectionOffset = 0x%x, mySectionSize = 0x%x, nsize:%d", mySectionOffset, mySectionSize, nsize);
__android_log_print(ANDROID_LOG_INFO, "JNITag", "mySectionVirtualOffset = 0x%x", text_addr);
printf("mySectionSize = %d\n", mySectionSize);
//修改内存的操作权限
if(mprotect((void *) (text_addr / PAGE_SIZE * PAGE_SIZE), 4096 * nsize, PROT_READ | PROT_EXEC | PROT_WRITE) != 0){
puts("mem privilege change failed");
__android_log_print(ANDROID_LOG_INFO, "JNITag", "mem privilege change failed");
successFlag = 0;
}
//解密
for(i = 0;i < mySectionSize; i++){
char *addr = (char*)(text_addr + i);
*addr = ~(*addr);
}
if(mprotect((void *) (text_addr / PAGE_SIZE * PAGE_SIZE), 4096 * nsize, PROT_READ | PROT_EXEC) != 0){
puts("mem privilege change failed");
__android_log_print(ANDROID_LOG_INFO, "JNITag", "The second mem privilege change failed");
successFlag = 0;
}
if (successFlag) {
puts("Decrypt success");
__android_log_print(ANDROID_LOG_INFO, "JNITag", "Decrypt success");
} else {
puts("Decrypt fail");
__android_log_print(ANDROID_LOG_INFO, "JNITag", "Decrypt fail");
}
}
|