给某个SootClass插入字段,关键API为new SootField(String,Type,int);
//向sootclass添加静态Intent intent_for_ipc字段 SootField intent_for_ipc = new SootField("intent_for_ipc", intentSootClass.getType(),Modifier.STATIC); sootclass.addField(intent_for_ipc);注意:一般是在wjtp下给某个SootClass插入字段
SootMethod sm = new SootMethod("getIntent", Arrays.asList(new Type[]{}), RefType.v("java.lang.String"), Modifier.PUBLIC);
SootClass intentSootClass = Scene.v().getSootClass("android.content.Intent"); SootMethod sm = new SootMethod("getIntent", Arrays.asList(new Type[]{}), intentSootClass.getType(),Modifier.PUBLIC);
method=new SootMethod("main",Arrays.asList(newType[]{ArrayType.v(RefType.v("java.lang.String"), 1)}),VoidType.v(),Modifier.PUBLIC|Modifier.STATIC);
SootMethod smmm = new SootMethod("mulPara",Arrays.asList(new Type[]{RefType.v("java.lang.String"),intentSootClass.getType()}),VoidType.v(), Modifier.PUBLIC);
//保存参数 Local arg = Jimple.v().newLocal("l0", intentSootClass.getType()); helperBody.getLocals().add(arg); IdentityStmt para = Jimple.v().newIdentityStmt(arg, Jimple.v().newParameterRef(intentSootClass.getType(), 0)); helperBody.getUnits().add(para); //将arg赋值给字段 helperBody.getUnits().add(Jimple.v().newAssignStmt(Jimple.v().newStaticFieldRef(intent_for_ipc.makeRef()), arg)); helperBody.getUnits().add(Jimple.v().newReturnVoidStmt());
while(iter.hasNext()){ final Unit unit = (Unit)iter.next(); unit.apply(new AbstractStmtSwitch(){ public void caseAssignStmt(AssignStmt stmt){ ... } public void caseInvokeStmt(InvokeStmt stmt){ ... } } }
PatchingChain<Unit> units = body.getUnits(); for(Iterator iter = units.snapshotIterator();iter.hasNext();){ Unit u = (Unit)iter.next(); if(u instanceof InvokeStmt){ String methodName = ((InvokeStmt) u).getInvokeExpr().getMethod().getName(); System.out.println(methodName); if(methodName.equals("add")){ Local local = Jimple.v().newLocal("lizhengqiao",RefType.v("java.lang.String")); body.getLocals().add(local); ... } } //if(u instance IdentityStmt) //... }另:在Java中,可以采用另外一种插桩的方法:直接写一个需要插桩的Java代码,然后插桩调用该Java类中的方法即可。