forked from wh1t3p1g/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
289 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package tabby.core.scanner; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import soot.SootMethod; | ||
import soot.Unit; | ||
import soot.jimple.InvokeExpr; | ||
import soot.jimple.JimpleBody; | ||
import soot.jimple.Stmt; | ||
import tabby.core.soot.switcher.InvokeStmtSwitcher; | ||
import tabby.dal.bean.ref.MethodReference; | ||
import tabby.dal.cache.CacheHelper; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 收集所有调用关系,这部分不做污点分析 | ||
* @author wh1t3P1g | ||
* @since 2020/11/17 | ||
*/ | ||
@Data | ||
@Slf4j | ||
@Component | ||
public class CallGraphScanner implements Scanner<List<MethodReference>>{ | ||
|
||
@Autowired | ||
private CacheHelper cacheHelper; | ||
@Autowired | ||
private InvokeStmtSwitcher invokeStmtSwitcher; | ||
|
||
@Override | ||
public void run(List<MethodReference> targets) { | ||
collect(targets); | ||
build(); | ||
} | ||
|
||
@Override | ||
public void collect(List<MethodReference> targets) { | ||
log.info("start to build call graph!"); | ||
targets.forEach(this::collect); | ||
log.info("build call graph DONE!"); | ||
} | ||
|
||
public void collect(MethodReference methodRef){ | ||
try{ | ||
SootMethod method = methodRef.getCachedMethod(); | ||
JimpleBody body = (JimpleBody) method.getActiveBody(); | ||
invokeStmtSwitcher.setSource(methodRef); | ||
for(Unit unit: body.getUnits()){ | ||
Stmt stmt = (Stmt) unit; | ||
if(stmt.containsInvokeExpr()){ | ||
InvokeExpr invokeExpr = stmt.getInvokeExpr(); | ||
invokeExpr.apply(invokeStmtSwitcher); | ||
} | ||
} | ||
}catch (RuntimeException e){ | ||
// e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void build() { | ||
|
||
} | ||
|
||
@Override | ||
public void save() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tabby.core.scanner; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/11/17 | ||
*/ | ||
public interface Scanner<T> { | ||
|
||
|
||
void run(T targets); | ||
|
||
/** | ||
* 收集类信息 | ||
* @param targets 待收集的类名/函数 | ||
*/ | ||
void collect(T targets); | ||
|
||
/** | ||
* build relationships | ||
*/ | ||
void build(); | ||
|
||
/** | ||
* save to cache file | ||
* then cache file to neo4j | ||
*/ | ||
void save(); | ||
} |
88 changes: 87 additions & 1 deletion
88
src/main/java/tabby/core/soot/switcher/InvokeStmtSwitcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,97 @@ | ||
package tabby.core.soot.switcher; | ||
|
||
import soot.jimple.AbstractJimpleValueSwitch; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import soot.SootMethodRef; | ||
import soot.Value; | ||
import soot.jimple.*; | ||
import soot.jimple.internal.JimpleLocal; | ||
import tabby.dal.bean.edge.Call; | ||
import tabby.dal.bean.ref.MethodReference; | ||
import tabby.dal.bean.ref.handle.ClassRefHandle; | ||
import tabby.dal.cache.CacheHelper; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/10/10 | ||
* | ||
*/ | ||
@Setter | ||
@Getter | ||
@Slf4j | ||
@Component | ||
public class InvokeStmtSwitcher extends AbstractJimpleValueSwitch { | ||
|
||
private MethodReference source; | ||
|
||
@Autowired | ||
private CacheHelper cacheHelper; | ||
|
||
@Override | ||
public void caseStaticInvokeExpr(StaticInvokeExpr v) { | ||
if(isNecessaryEdge("static", v)){ | ||
SootMethodRef sootMethodRef = v.getMethodRef(); | ||
ClassRefHandle classRefHandle = new ClassRefHandle(sootMethodRef.getDeclaringClass().getName()); | ||
|
||
buildCallRelationship(classRefHandle, sootMethodRef); | ||
} | ||
} | ||
|
||
@Override | ||
public void caseVirtualInvokeExpr(VirtualInvokeExpr v) { | ||
SootMethodRef sootMethodRef = v.getMethodRef(); | ||
ClassRefHandle classRefHandle = new ClassRefHandle(v.getBase().getType().toString()); | ||
buildCallRelationship(classRefHandle, sootMethodRef); | ||
} | ||
|
||
@Override | ||
public void caseSpecialInvokeExpr(SpecialInvokeExpr v) {// 初始化 | ||
SootMethodRef sootMethodRef = v.getMethodRef(); | ||
ClassRefHandle classRefHandle = new ClassRefHandle(v.getBase().getType().toString()); | ||
buildCallRelationship(classRefHandle, sootMethodRef); | ||
} | ||
|
||
@Override | ||
public void caseInterfaceInvokeExpr(InterfaceInvokeExpr v) { | ||
SootMethodRef sootMethodRef = v.getMethodRef(); | ||
ClassRefHandle classRefHandle = new ClassRefHandle(v.getBase().getType().toString()); | ||
buildCallRelationship(classRefHandle, sootMethodRef); | ||
} | ||
|
||
@Override | ||
public void defaultCase(Object v) { | ||
super.defaultCase(v); | ||
} | ||
|
||
public void buildCallRelationship(ClassRefHandle classRefHandle, SootMethodRef sootMethodRef){ | ||
MethodReference target = cacheHelper.loadMethodRef(classRefHandle, sootMethodRef.getName(), sootMethodRef.getSignature()); | ||
MethodReference source = cacheHelper.loadMethodRefByHandle(this.source.getHandle()); | ||
if(target != null && source != null){ | ||
Call call = Call.newInstance(source, target); | ||
call.setRealCallType(classRefHandle.getName()); | ||
source.getCallEdge().add(call); | ||
} | ||
} | ||
|
||
public <T> boolean isNecessaryEdge(String type, T v){ | ||
if ("static".equals(type)) { // 对于静态函数调用,只关注 函数参数可控的情况 | ||
StaticInvokeExpr invokeExpr = (StaticInvokeExpr) v; | ||
if (invokeExpr.getArgCount() == 0) { | ||
return false; | ||
} | ||
List<Value> values = invokeExpr.getArgs(); | ||
for (Value value : values) { | ||
if (value instanceof JimpleLocal || | ||
value instanceof StringConstant) { // Class.forName(xxx) 这种情况 | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/tabby/core/soot/toolkit/SimpleCallGraphExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package tabby.core.soot.toolkit; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/11/17 | ||
*/ | ||
public class SimpleCallGraphExtractor { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/tabby/core/soot/transformer/CallGraphTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tabby.core.soot.transformer; | ||
|
||
import soot.Body; | ||
import soot.BodyTransformer; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/11/9 | ||
*/ | ||
public class CallGraphTransformer extends BodyTransformer { | ||
@Override | ||
protected void internalTransform(Body b, String phaseName, Map<String, String> options) { | ||
|
||
} | ||
} |
Oops, something went wrong.