public static void main(String[] args) {
try
{
/
/
目录自定义
File
payloadSrcFile
=
new
File
(
"F:\AndroidUnidbg\untitled\src\orgin.apk"
);
/
/
原apk目录
File
unShellDexFile
=
new
File
(
"F:\AndroidUnidbg\untitled\src\shell.dex"
);
/
/
具有加固功能的dex文件
byte[] payloadArray
=
encrpt(readFileBytes(payloadSrcFile),
0x66
);
/
/
对apk进行加密,这里可以自定义,我采用了异或加密
byte[] unShellDexArray
=
readFileBytes(unShellDexFile);
/
/
readFileBytes函数:将文件内容转换为bytes类型
int
payloadLen
=
payloadArray.length;
int
unShellDexLen
=
unShellDexArray.length;
int
totalLen
=
payloadLen
+
unShellDexLen
+
4
;
/
/
可以看到最终的长度是增加了
4
,最后
4
个字节用于填充源app文件的大小
byte[] newdex
=
new byte[totalLen];
/
/
添加解壳代码
System.arraycopy(unShellDexArray,
0
, newdex,
0
, unShellDexLen);
/
/
添加加密后的解壳数据
System.arraycopy(payloadArray,
0
, newdex, unShellDexLen, payloadLen);
/
/
添加解壳数据长度
System.arraycopy(intToByte(payloadLen),
0
, newdex, totalLen
-
4
,
4
);
/
/
修改DEX
file
size文件头
fixFileSizeHeader(newdex);
/
/
修改DEX SHA1 文件头
fixSHA1Header(newdex);
/
/
修改DEX CheckSum文件头
fixCheckSumHeader(newdex);
String
str
=
"F:\AndroidUnidbg\untitled\src\classes.dex"
;
File
file
=
new
File
(
str
);
if
(!
file
.exists()) {
file
.createNewFile();
}
FileOutputStream localFileOutputStream
=
new FileOutputStream(
str
);
/
/
保存加密之后的dex
localFileOutputStream.write(newdex);
localFileOutputStream.flush();
localFileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}