Skip to content

Commit

Permalink
主要更新控制中心的代码,使其可以对一个apk进行加固。把jni的存放目录移动了一个位置。
Browse files Browse the repository at this point in the history
  • Loading branch information
zylc369 committed Apr 3, 2015
1 parent 402b662 commit c9dea21
Show file tree
Hide file tree
Showing 45 changed files with 547 additions and 124 deletions.
27 changes: 26 additions & 1 deletion AdvmpTest/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
include $(call all-subdir-makefiles)
ADVMPC_PATH := ../../template/jni/advmpc

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := advmpc

LOCAL_C_INCLUDES := $(ADVMPC_PATH)

LOCAL_SRC_FILES := $(ADVMPC_PATH)/ioapi.c \
$(ADVMPC_PATH)/unzip.c \
$(ADVMPC_PATH)/Globals.cpp \
$(ADVMPC_PATH)/avmp.cpp \
$(ADVMPC_PATH)/BitConvert.cpp \
$(ADVMPC_PATH)/InterpC.cpp \
$(ADVMPC_PATH)/io.cpp \
$(ADVMPC_PATH)/Utils.cpp \
$(ADVMPC_PATH)/YcFile.cpp

LOCAL_SRC_FILES += $(ADVMPC_PATH)/DexOpcodes.cpp \
$(ADVMPC_PATH)/Exception.cpp

LOCAL_LDLIBS := -llog -lz

include $(BUILD_SHARED_LIBRARY)
12 changes: 0 additions & 12 deletions AdvmpTest/jni/advmpc/Common.h

This file was deleted.

78 changes: 78 additions & 0 deletions base/src/main/java/buwai/android/dexlib2/helper/MethodHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
public class MethodHelper {

public static final String[] paramNames = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

/**
* 在起始位置插入指令。
* @param method 方法对象。
Expand Down Expand Up @@ -128,4 +131,79 @@ public static String genParamsShortDesc(MethodReference mr) {
return sb.toString();
}

/**
* 生成在native中的类型。
* @param mr
* @return
*/
public static String genTypeInNative (MethodReference mr) {
String type = mr.getReturnType();
return genTypeInNative(type);
}

/**
* 生成在native中的类型。
* @param type
* @return
*/
public static String genTypeInNative (CharSequence type) {
char cType = type.charAt(0);
switch (cType) {
case 'V':
return "void";
case 'Z':
return "jboolean";
case 'B':
return "jbyte";
case 'S':
return "jshort";
case 'C':
return "jchar";
case 'I':
return "jint";
case 'J':
return "jlong";
case 'F':
return "jfloat";
case 'D':
return "jdouble";
case 'L':
return "jobject";
case '[':
return "jobject";
default:
return null;
}
}

/**
* 生成JNI方法参数列表。
*
* @param mr
* @return 返回方法参数列表。
*/
public static String genParamTypeListInNative(MethodReference mr) {
List<? extends CharSequence> params = mr.getParameterTypes();
StringBuilder paramsList = new StringBuilder();
paramsList.append("JNIEnv *env, jobject thiz");
int length = params.size();

for (int i = 0; i < length; i++) {
paramsList.append(", ");
paramsList.append(genTypeInNative(mr));
paramsList.append(" ");
paramsList.append(paramNames[i]);
}
return paramsList.toString();
}

/**
* 生成JNINativeMethod结构的数据。
* @param method
* @return
*/
public static String genJNINativeMethod(Method method) {
return String.format("{ \"%s\", \"%s\", (void*)%s }, ", method.getName(), genMethodSig(method), method.getName());
}

}
45 changes: 45 additions & 0 deletions base/src/main/java/buwai/android/shell/base/Utils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
package buwai.android.shell.base;

import java.io.*;

/**
* Created by buwai on 25/4/1.
*/
public class Utils {

/**
* 复制整个文件夹内容
*
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static void copyFolder(String oldPath, String newPath) throws IOException {

(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;

for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}

if (temp.isFile()) {
try (FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString())) {

byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}


}

}
24 changes: 9 additions & 15 deletions base/src/main/java/buwai/android/shell/base/ZipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,31 @@ public class ZipHelper {
* 解压缩zip文件中的单个文件。
*
* @param file file zip文件。
* @param outDir 被解压缩文件的输出目录
* @param outPath 输出路径
* @param filename 要解压缩的文件名。
*/
public static void unZipSingle(File file, File outDir, String filename) {
public static void unZipSingle(File file, File outPath, String filename) throws IOException {
try (ZipFile zipFile = new ZipFile(file)) {
ZipEntry entry = zipFile.getEntry(filename);//所解压的文件名
try (InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outDir)) {
try (InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outPath)) {
int temp = 0;
while ((temp = input.read()) != -1) {
output.write(temp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 压缩单个文件
*/
public static void doZipSingle(File zipFile, File file) {
try {
try (InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));) {
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
public static void doZipSingle(File zipFile, File file) throws IOException {
try (InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));) {
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ private static boolean isMatchPackage(String packageNameInList, String packageNa
* @return true: 包含。false: 不包含。
*/
public static boolean isMatchMethodInWhiteList(List<TypeDescription> list, TypeDescription typeDescription) {
if (0 == list.size()) {
return true;
}
for (TypeDescription td : list) {
if (null == td.methodName) {
return true;
Expand All @@ -217,6 +220,9 @@ public static boolean isMatchMethodInWhiteList(List<TypeDescription> list, TypeD
}

public static boolean isMatchMethodInBlackList(List<TypeDescription> list, TypeDescription typeDescription) {
if (0 == list.size()) {
return false;
}
for (TypeDescription td : list) {
if (null == td.methodName) {
return false;
Expand Down
6 changes: 6 additions & 0 deletions control-centre/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
<artifactId>base</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>buwai.android.shell</groupId>
<artifactId>separator</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<finalName>buwaishell</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,101 @@
package buwai.android.shell.controlcentre;

import buwai.android.shell.base.TypeDescription;
import buwai.android.shell.base.Utils;
import buwai.android.shell.base.ZipHelper;
import buwai.android.shell.base.helper.AndroidManifestHelper;
import buwai.android.shell.separator.InstructionInsert01;
import buwai.android.shell.separator.Separator;
import buwai.android.shell.separator.SeparatorOption;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
* Created by Neptunian on 2015/4/1.
*/
public class ControlCentre {

private File mApkFile;
private File mOutDir;
private File mWorkspace;
private ControlCentreOption mOpt;
private File mApkUnpackDir;

public ControlCentre(File src, File outDir) throws IOException {
mApkFile = src;
mOutDir = outDir;
public ControlCentre(ControlCentreOption opt) throws IOException {
mOpt = opt;
prepare();
}

/**
* 做一些准备工作。
*/
private void prepare() throws IOException {
// 创建工作目录。
mWorkspace = Files.createTempDirectory(mOutDir.toPath(), "advmp").toFile();
mApkUnpackDir = new File(mOpt.workspace, "apk");
mApkUnpackDir.mkdir();

// 解压缩apk中的classes.dex文件。
ZipHelper.unZipSingle(mApkFile, mWorkspace, "classes.dex");
ZipHelper.unZipSingle(mOpt.apkFile, new File(mApkUnpackDir, "classes.dex"), "classes.dex");

// 解压缩apk中的AndroidManifest.xml文件。
ZipHelper.unZipSingle(mOpt.apkFile, new File(mApkUnpackDir, "AndroidManifest.xml"), "AndroidManifest.xml");
}

/**
* 加壳。
* @return
*/
// public boolean shell() {
//
// }
public boolean shell() {
boolean bRet = false;
try {
// 插入指令。
TypeDescription classDesc = AndroidManifestHelper.findFirstClass(new File(mApkUnpackDir, "AndroidManifest.xml"));
InstructionInsert01 instructionInsert01 = new InstructionInsert01(new File(mApkUnpackDir, "classes.dex"), classDesc);
instructionInsert01.insert();

runSeparator();

copyJniFiles();



bRet = true;
} catch (IOException e) {
e.printStackTrace();
}
return bRet;
}

/**
* 运行抽取器。
* @return
* @throws IOException
*/
private boolean runSeparator() throws IOException {
SeparatorOption opt = new SeparatorOption();
opt.dexFile = new File(mApkUnpackDir, "classes.dex");
File outDir = new File(mOpt.workspace, "separator");
opt.outDexFile = new File(outDir, "classes.dex");
opt.outYcFile = new File(outDir, "classes.yc");
opt.outCPFile = new File(outDir, "advmp_separator.cpp");

Separator separator = new Separator(opt);
return separator.run();
}

/**
* 将template中的jni目录拷贝到工作目录。
* @throws IOException
*/
private void copyJniFiles() throws IOException {
// TODO 这里写死了。
File jniTemplateDir = new File("E:\\MyProjects\\ADVMP\\template\\jni");
mOpt.jniDir = new File(mOpt.workspace, "jni");
Utils.copyFolder(jniTemplateDir.getAbsolutePath(), mOpt.jniDir.getAbsolutePath());
}

/**
* 更新jni目录中的文件。
*/
private void updateJniFiles() {
File file = new File(mOpt.jniDir.getAbsolutePath() + File.separator + )
}

}
Loading

0 comments on commit c9dea21

Please sign in to comment.