Skip to content

Commit

Permalink
Added rudimentary download logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
palant committed Feb 24, 2021
1 parent 7da4290 commit 292a413
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 82 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ Each component has a `filter` option allowing to restrict its functionality. It

## MethodLogger component

This component will add a `System.out.println()` call to the start of each method. It will print the method name and parameter values to the log.
This component will add logging to the start of each method. In addition to the method signature, the parameter values will be logged.

Configuration options:

* `MethodLogger.enabled`: add to enable this component
* `MethodLogger.filter`: (optional) restricts functionality to a set of classes or methods (see Filters section above)
* `MethodLogger.tag`: (optional) log tag to be used (default is `APKInstrumentation`)
* `MethodLogger.tag`: (optional) log tag to be used (default is `MethodLogger`)

## DownloadLogger component

This component will log calls to `URL.openConnection()`. It will log the calling method and the URL target.

Configuration options:

* `DownloadLogger.enabled`: add to enable this component
* `DownloadLogger.filter`: (optional) restricts functionality to a set of classes or methods (see Filters section above)
* `DownloadLogger.tag`: (optional) log tag to be used (default is `DownloadLogger`)

## AssignmentRemover component

Expand Down
86 changes: 86 additions & 0 deletions src/info/palant/apkInstrumentation/DownloadLogger.java
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();
}
}
}
}
7 changes: 6 additions & 1 deletion src/info/palant/apkInstrumentation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ private static void addTransformers(Properties config)
PackManager.v().getPack("jtp").add(new Transform("jtp.AssignmentRemover", new AssignmentRemover(config)));
hasTransformers = true;
}
if (config.getProperty("DownloadLogger.enabled") != null)
{
PackManager.v().getPack("jtp").add(new Transform("jtp.DownloadLogger", new DownloadLogger(config)));
hasTransformers = true;
}

if (!hasTransformers)
{
Expand Down Expand Up @@ -195,7 +200,7 @@ private static void runCommand(String[] command) throws IOException
{
ProcessBuilder builder = new ProcessBuilder(command);
builder.inheritIO();
Process process = builder.start();//Runtime.getRuntime().exec(command);
Process process = builder.start();
try
{
int result = process.waitFor();
Expand Down
46 changes: 7 additions & 39 deletions src/info/palant/apkInstrumentation/MethodLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class MethodLogger extends BodyTransformer
private final Filter filter;
private String tag;

private final SootMethod method_Log_i;
private final SootMethod method_Object_toString;
private final SootMethod method_StringBuilder_init;
private final SootMethod method_StringBuilder_append;

Expand All @@ -43,11 +41,8 @@ public MethodLogger(Properties config)
Scene.v().addBasicClass("java.lang.Object", SootClass.SIGNATURES);
Scene.v().addBasicClass("java.lang.String", SootClass.SIGNATURES);
Scene.v().addBasicClass("java.lang.StringBuilder", SootClass.SIGNATURES);
Scene.v().addBasicClass("java.lang.System", SootClass.SIGNATURES);
Scene.v().loadNecessaryClasses();

this.method_Log_i = Scene.v().getMethod("<android.util.Log: int i(java.lang.String,java.lang.String)>");
this.method_Object_toString = Scene.v().getMethod("<java.lang.Object: java.lang.String toString()>");
this.method_StringBuilder_init = Scene.v().getMethod("<java.lang.StringBuilder: void <init>(java.lang.String)>");
this.method_StringBuilder_append = Scene.v().getMethod("<java.lang.StringBuilder: java.lang.StringBuilder append(java.lang.String)>");

Expand All @@ -59,7 +54,7 @@ public MethodLogger(Properties config)

this.tag = config.getProperty("MethodLogger.tag");
if (tag == null)
tag = "APKInstrumentation";
tag = "MethodLogger";
}

@Override
Expand All @@ -69,13 +64,12 @@ protected void internalTransform(Body b, String phaseName, Map<String, String> o
if (this.filter != null && !this.filter.matches(body))
return;

List<Unit> units = new ArrayList<>();
UnitSequence units = new UnitSequence(body);

Local messageStringified = Utils.generateNewLocal(body, RefType.v("java.lang.String"));
List<Local> parameters = body.getParameterLocals();
if (parameters.size() > 0)
{
Local message = Utils.generateNewLocal(body, RefType.v("java.lang.StringBuilder"));
Local message = units.newLocal(RefType.v("java.lang.StringBuilder"));
units.add(
Jimple.v().newAssignStmt(
message,
Expand Down Expand Up @@ -111,48 +105,22 @@ protected void internalTransform(Body b, String phaseName, Map<String, String> o
);
}

Local stringified = Utils.generateNewLocal(body, RefType.v("java.lang.String"));
units.add(Utils.stringify(parameter, stringified));
parameter = units.stringify(parameter);
units.add(
Jimple.v().newInvokeStmt(
Jimple.v().newVirtualInvokeExpr(
message,
this.method_StringBuilder_append.makeRef(),
stringified
parameter
)
)
);
}

units.add(
Jimple.v().newAssignStmt(
messageStringified,
Jimple.v().newVirtualInvokeExpr(
message,
this.method_Object_toString.makeRef()
)
)
);
units.log(this.tag, units.stringify(message));
}
else
{
units.add(
Jimple.v().newAssignStmt(
messageStringified,
StringConstant.v("Entered method " + body.getMethod().getSignature())
)
);
}

units.add(
Jimple.v().newInvokeStmt(
Jimple.v().newStaticInvokeExpr(
this.method_Log_i.makeRef(),
StringConstant.v(this.tag),
messageStringified
)
)
);
units.log(this.tag, StringConstant.v("Entered method " + body.getMethod().getSignature()));

body.getUnits().insertBefore(units, body.getFirstNonIdentityStmt());
body.validate();
Expand Down
155 changes: 155 additions & 0 deletions src/info/palant/apkInstrumentation/UnitSequence.java
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;
}
}
Loading

0 comments on commit 292a413

Please sign in to comment.