Skip to content

Commit

Permalink
Encrypt so files
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyesiqiu committed Aug 3, 2024
1 parent c2d7664 commit 06d40c1
Show file tree
Hide file tree
Showing 17 changed files with 925 additions and 17 deletions.
2 changes: 2 additions & 0 deletions dpt/src/main/java/com/luoye/dpt/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public class Const {
public static final String PROXY_APPLICATION_NAME = "com.luoyesiqiu.shell.ProxyApplication";
public static final String PROXY_COMPONENT_FACTORY = "com.luoyesiqiu.shell.ProxyComponentFactory";
public static final short MULTI_DEX_CODE_VERSION = 1;

public static final String DEFAULT_RC4_KEY = "ncWK&S5wbqU%IX6j";
}
51 changes: 50 additions & 1 deletion dpt/src/main/java/com/luoye/dpt/builder/Apk.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.android.apksigner.ApkSignerTool;
import com.iyxan23.zipalignjava.ZipAlign;
import com.luoye.dpt.Const;
import com.luoye.dpt.elf.ReadElf;
import com.luoye.dpt.model.Instruction;
import com.luoye.dpt.model.MultiDexCode;
import com.luoye.dpt.task.ThreadPool;
Expand All @@ -12,6 +13,7 @@
import com.luoye.dpt.util.LogUtils;
import com.luoye.dpt.util.ManifestUtils;
import com.luoye.dpt.util.MultiDexCodeUtils;
import com.luoye.dpt.util.RC4Utils;
import com.luoye.dpt.util.ZipUtils;
import com.wind.meditor.core.FileProcesser;
import com.wind.meditor.property.AttributeItem;
Expand All @@ -22,6 +24,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -135,6 +138,7 @@ private static void process(Apk apk){

apk.addProxyDex(apkMainProcessPath);
apk.copyNativeLibs(apkMainProcessPath);
apk.encryptSoFiles(apkMainProcessPath);

apk.buildApk(apkFile.getAbsolutePath(),apkMainProcessPath, FileUtils.getExecutablePath());

Expand Down Expand Up @@ -246,14 +250,59 @@ protected void addJunkCodeDex(String apkDir) {
}

private void compressDexFiles(String apkDir){
ZipUtils.compress(getDexFiles(apkDir),getOutAssetsDir(apkDir).getAbsolutePath()+File.separator + "i11111i111.zip");
ZipUtils.compress(getDexFiles(apkDir),getOutAssetsDir(apkDir).getAbsolutePath() + File.separator + "i11111i111.zip");
}

private void copyNativeLibs(String apkDir){
File file = new File(FileUtils.getExecutablePath(), "shell-files/libs");
FileUtils.copy(file.getAbsolutePath(),getOutAssetsDir(apkDir).getAbsolutePath() + File.separator + "vwwwwwvwww");
}

private void encryptSoFiles(String apkDir){

File obfDir = new File(getOutAssetsDir(apkDir).getAbsolutePath() + File.separator, "vwwwwwvwww");
File[] soAbiDirs = obfDir.listFiles();
if(soAbiDirs != null) {
for (File soAbiDir : soAbiDirs) {
File[] soFiles = soAbiDir.listFiles();
if(soFiles != null) {
for (File soFile : soFiles) {
if(!soFile.getAbsolutePath().endsWith(".so")) {
continue;
}
try {
ReadElf readElf = new ReadElf(soFile);
List<ReadElf.SectionHeader> sectionHeaders = readElf.getSectionHeaders();
readElf.close();
for (ReadElf.SectionHeader sectionHeader : sectionHeaders) {

if(".bitcode".equals(sectionHeader.getName())) {

LogUtils.info("start encrypt %s section: %s,offset: %s,size: %s",
soFile.getAbsolutePath(),
sectionHeader.getName(),
Long.toHexString(sectionHeader.getOffset()),
Long.toHexString(sectionHeader.getSize())
);

byte[] bitcode = IoUtils.readFile(soFile.getAbsolutePath(),sectionHeader.getOffset(),(int)sectionHeader.getSize());

byte[] enc = RC4Utils.crypt(Const.DEFAULT_RC4_KEY.getBytes(StandardCharsets.UTF_8),bitcode);

IoUtils.writeFile(soFile.getAbsolutePath(),enc,sectionHeader.getOffset());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

}

private void deleteAllDexFiles(String dir){
List<File> dexFiles = getDexFiles(dir);
for (File dexFile : dexFiles) {
Expand Down
Loading

0 comments on commit 06d40c1

Please sign in to comment.