一些实例
1. 遍历当前光标处函数所有的Element
Java代码:
import java.io.*;
import java.util.List;
import jeb.api.IScript;
import jeb.api.JebInstance;
import jeb.api.ui.*;
import jeb.api.ast.*;
public class Hello implements IScript {
protected JebInstance mJebInstance;
private static File logFile;
private static BufferedWriter writer;
@Override
public void run(JebInstance jebInstance) {
init();
mJebInstance = jebInstance;
// 获取光标所在函数的签名 (Java视图)
String cursorSig = ((CodeView) jebInstance.getUI().getView(View.Type.JAVA)).getCodePosition().getSignature();
// 获取语法树
Method curMethod = jebInstance.getDecompiledMethodTree(cursorSig);
logWrite("current method:" + curMethod.getSignature() + "
");
// 获取方法体
Block body = curMethod.getBody();
logWrite(body.toString() + "
");
for (int i = 0;i < body.size();i++) {
ShowElement(body.get(i),1);
}
close();
}
public void ShowElement(IElement element,int depth) {
printDepth(depth);
logWrite(element.toString() + "
");
List<IElement> list_element = element.getSubElements();
for (IElement e:list_element) {
ShowElement(e,depth + 1);
}
}
public void printDepth(int depth) {
for (int i = 0;i < depth;i++) {
logWrite(" ");
}
}
public void logWrite(String log) {
try {
writer.write(log);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void init() {
logFile = new File("D:\log.txt");
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile), "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我JEB里面打印乱了,只好输出到文件了
执行结果:
2. 遍历所有的方法
@Override
public void run(JebInstance jebInstance) {
Dex dex = jebInstance.getDex();
int methodCount = dex.getMethodCount();
String methodSignature;
for (int i = 0; i < methodCount; i++) {
DexMethod dexMethod = dex.getMethod(i);
methodSignature = dexMethod.getSignature(true);
jebInstance.print(methodSignature);
}
} 3.遍历所有的class
// 遍历所有的类 找到指定的类
List<String> listClassSignatures = dex.getClassSignatures(false);
int index = 0;
boolean isFind = false;
for (String classSignatures:listClassSignatures) {
if (classSignatures.equals(decodeClassSignature)) {
isFind = true;
jebInstance.print("find");
jebInstance.print(classSignatures);
break;
}
index++;
}
if (!isFind) {
close();
return;
} 4. 遍历某个class下所有的直接方法
DexClass dexClass = dex.getClass(index);
DexClassData dexClassData = dexClass.getData();
DexMethodData[] dexMethods = dexClassData.getDirectMethods();
for (int i = 0;i < dexMethods.length;i++) {
int methodIndex = dexMethods[i].getMethodIndex();
DexMethod dexMethod = dex.getMethod(methodIndex);
String methodSignature = dexMethod.getSignature(true);
jebInstance.print(methodSignature);
}