-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rudimentary download logging functionality
- Loading branch information
Showing
6 changed files
with
266 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This Source Code is subject to the terms of the Mozilla Public License | ||
* version 2.0 (the "License"). You can obtain a copy of the License at | ||
* http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package info.palant.apkInstrumentation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import soot.Body; | ||
import soot.Local; | ||
import soot.Scene; | ||
import soot.SootClass; | ||
import soot.SootMethod; | ||
import soot.Unit; | ||
import soot.BodyTransformer; | ||
import soot.jimple.AssignStmt; | ||
import soot.jimple.InstanceInvokeExpr; | ||
import soot.jimple.Jimple; | ||
import soot.jimple.JimpleBody; | ||
import soot.jimple.StringConstant; | ||
|
||
public class DownloadLogger extends BodyTransformer | ||
{ | ||
private final Filter filter; | ||
private String tag; | ||
|
||
public DownloadLogger(Properties config) | ||
{ | ||
Scene.v().addBasicClass("android.util.Log", SootClass.SIGNATURES); | ||
Scene.v().addBasicClass("java.lang.Object", SootClass.SIGNATURES); | ||
Scene.v().addBasicClass("java.lang.String", SootClass.SIGNATURES); | ||
Scene.v().addBasicClass("java.lang.System", SootClass.SIGNATURES); | ||
Scene.v().loadNecessaryClasses(); | ||
|
||
String filterSpec = config.getProperty("DownloadLogger.filter"); | ||
if (filterSpec != null) | ||
this.filter = new Filter(filterSpec); | ||
else | ||
this.filter = null; | ||
|
||
this.tag = config.getProperty("DownloadLogger.tag"); | ||
if (tag == null) | ||
tag = "DownloadLogger"; | ||
} | ||
|
||
@Override | ||
protected void internalTransform(Body b, String phaseName, Map<String, String> options) | ||
{ | ||
JimpleBody body = (JimpleBody)b; | ||
if (this.filter != null && !this.filter.matches(body)) | ||
return; | ||
|
||
for (Unit unit: body.getUnits().toArray(new Unit[0])) | ||
{ | ||
if (!(unit instanceof AssignStmt)) | ||
continue; | ||
|
||
AssignStmt assignment = (AssignStmt)unit; | ||
if (!(assignment.getRightOp() instanceof InstanceInvokeExpr)) | ||
continue; | ||
|
||
InstanceInvokeExpr invocation = (InstanceInvokeExpr)assignment.getRightOp(); | ||
SootMethod method = invocation.getMethod(); | ||
if (method.getDeclaringClass().getName().equals("java.net.URL") && method.getName().equals("openConnection")) | ||
{ | ||
UnitSequence units = new UnitSequence(body); | ||
|
||
Local message = units.format( | ||
StringConstant.v("Method %s opened URLConnection %x to URL %s"), | ||
StringConstant.v(body.getMethod().getSignature()), | ||
units.getIdentity(assignment.getLeftOp()), | ||
invocation.getBase() | ||
); | ||
units.log(this.tag, message); | ||
|
||
body.getUnits().insertAfter(units, unit); | ||
body.validate(); | ||
} | ||
} | ||
} | ||
} |
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,155 @@ | ||
/* | ||
* This Source Code is subject to the terms of the Mozilla Public License | ||
* version 2.0 (the "License"). You can obtain a copy of the License at | ||
* http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package info.palant.apkInstrumentation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
import soot.ArrayType; | ||
import soot.Body; | ||
import soot.IntType; | ||
import soot.Local; | ||
import soot.PrimType; | ||
import soot.RefType; | ||
import soot.Scene; | ||
import soot.Type; | ||
import soot.Unit; | ||
import soot.Value; | ||
import soot.javaToJimple.LocalGenerator; | ||
import soot.jimple.IntConstant; | ||
import soot.jimple.Jimple; | ||
import soot.jimple.StringConstant; | ||
|
||
public class UnitSequence extends ArrayList<Unit> | ||
{ | ||
private LocalGenerator generator; | ||
|
||
public UnitSequence(Body body) | ||
{ | ||
super(); | ||
|
||
this.generator = new LocalGenerator(body); | ||
} | ||
|
||
public Local newLocal(Type type) | ||
{ | ||
return this.generator.generateLocal(type); | ||
} | ||
|
||
public void log(String tag, Value message) | ||
{ | ||
this.add( | ||
Jimple.v().newInvokeStmt( | ||
Jimple.v().newStaticInvokeExpr( | ||
Scene.v().getMethod("<android.util.Log: int i(java.lang.String,java.lang.String)>").makeRef(), | ||
StringConstant.v(tag), | ||
message | ||
) | ||
) | ||
); | ||
} | ||
|
||
public Local stringify(Value value) | ||
{ | ||
Type type = value.getType(); | ||
String typeSignature = (type instanceof PrimType ? type.toString() : "java.lang.Object"); | ||
if (typeSignature == "byte" || typeSignature == "short") | ||
typeSignature = "int"; | ||
|
||
Local result = this.newLocal(RefType.v("java.lang.String")); | ||
this.add( | ||
Jimple.v().newAssignStmt( | ||
result, | ||
Jimple.v().newStaticInvokeExpr( | ||
Scene.v().getMethod("<java.lang.String: java.lang.String valueOf(" + typeSignature + ")>").makeRef(), | ||
value | ||
) | ||
) | ||
); | ||
return result; | ||
} | ||
|
||
public Local boxPrimitive(Value value) | ||
{ | ||
PrimType origType = (PrimType)value.getType(); | ||
RefType boxedType = origType.boxedType(); | ||
Local result = this.newLocal(boxedType); | ||
|
||
this.add( | ||
Jimple.v().newAssignStmt( | ||
result, | ||
Jimple.v().newStaticInvokeExpr( | ||
boxedType.getSootClass().getMethod("valueOf", Collections.singletonList(origType)).makeRef(), | ||
value | ||
) | ||
) | ||
); | ||
|
||
return result; | ||
} | ||
|
||
public Local arrayLiteral(Type elementType, Value... elements) | ||
{ | ||
Type arrayType = ArrayType.v(elementType, 1); | ||
Local array = this.newLocal(arrayType); | ||
this.add( | ||
Jimple.v().newAssignStmt( | ||
array, | ||
Jimple.v().newNewArrayExpr(elementType, IntConstant.v(elements.length)) | ||
) | ||
); | ||
|
||
int index = 0; | ||
for (Value element: elements) | ||
{ | ||
this.add( | ||
Jimple.v().newAssignStmt( | ||
Jimple.v().newArrayRef(array, IntConstant.v(index++)), | ||
element | ||
) | ||
); | ||
} | ||
|
||
return array; | ||
} | ||
|
||
public Local format(Value formatStr, Value... args) | ||
{ | ||
for (int i = 0; i < args.length; i++) | ||
if (args[i].getType() instanceof PrimType) | ||
args[i] = this.boxPrimitive(args[i]); | ||
Local argsArray = this.arrayLiteral(RefType.v("java.lang.Object"), args); | ||
|
||
Local result = this.newLocal(RefType.v("java.lang.String")); | ||
this.add( | ||
Jimple.v().newAssignStmt( | ||
result, | ||
Jimple.v().newStaticInvokeExpr( | ||
Scene.v().getMethod("<java.lang.String: java.lang.String format(java.lang.String,java.lang.Object[])>").makeRef(), | ||
formatStr, | ||
argsArray | ||
) | ||
) | ||
); | ||
return result; | ||
} | ||
|
||
public Local getIdentity(Value obj) | ||
{ | ||
Local result = this.newLocal(IntType.v()); | ||
this.add( | ||
Jimple.v().newAssignStmt( | ||
result, | ||
Jimple.v().newStaticInvokeExpr( | ||
Scene.v().getMethod("<java.lang.System: int identityHashCode(java.lang.Object)>").makeRef(), | ||
obj | ||
) | ||
) | ||
); | ||
return result; | ||
} | ||
} |
Oops, something went wrong.