Skip to content

Commit

Permalink
'init'
Browse files Browse the repository at this point in the history
  • Loading branch information
guanchao committed Jan 5, 2016
1 parent ffeee4d commit 7b7863d
Show file tree
Hide file tree
Showing 23 changed files with 345 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="lib" path="libs/substrate-api.jar"/>
<classpathentry kind="lib" path="libs/substrate-bless.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
lt application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log
33 changes: 33 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>AndroidAPIHooker</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
18 changes: 18 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shuwoom.apihooker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />

<uses-permission android:name="cydia.permission.SUBSTRATE" />

<application>
<meta-data
android:name="com.saurik.substrate.main"
android:value=".SubstrateMain" />
</application>

</manifest>
Binary file added ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added libs/android-support-v4.jar
Binary file not shown.
Binary file added libs/substrate-api.jar
Binary file not shown.
Binary file added libs/substrate-bless.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
14 changes: 14 additions & 0 deletions project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-22
Binary file added res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions res/values-v11/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>

</resources>
12 changes: 12 additions & 0 deletions res/values-v14/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<resources>

<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>

</resources>
5 changes: 5 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>

<string name="app_name">AndroidAPIHooker</string>

</resources>
20 changes: 20 additions & 0 deletions res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<resources>

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

</resources>
11 changes: 11 additions & 0 deletions src/com/shuwoom/apihooker/SubstrateMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.shuwoom.apihooker;

import com.shuwoom.apihooker.hookers.NetworkHooker;

public class SubstrateMain {

public static void initialize(){
new NetworkHooker().hook();
}

}
5 changes: 5 additions & 0 deletions src/com/shuwoom/apihooker/common/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.shuwoom.apihooker.common;

public class Config {
public static final String DEBUG_TAG = "api_hooker";
}
75 changes: 75 additions & 0 deletions src/com/shuwoom/apihooker/hookers/Hooker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.shuwoom.apihooker.hookers;

import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.util.Log;

import com.saurik.substrate.MS;
import com.saurik.substrate.MS.MethodPointer;
import com.shuwoom.apihooker.common.Config;
import com.shuwoom.apihooker.hookers.interfaces.HookerListener;

public abstract class Hooker {

public abstract void hook();

protected void hookMethods(final HookerListener listener, final String className, final Map<String, Integer> methodsToHook){
MS.hookClassLoad(className, new MS.ClassLoadHook() {

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void classLoaded(Class<?> classToHook) {
Map<GenericDeclaration, String> allMethods = new HashMap<GenericDeclaration, String>();

try {
Class clz = Class.forName(className);

for(Method method : clz.getMethods()){
allMethods.put(method, method.getName());
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}

Iterator iterator = allMethods.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
final Method method = (Method) entry.getKey();
final String methodName = (String)entry.getValue();

if(!methodsToHook.containsKey(methodName))
continue;

Log.v(Config.DEBUG_TAG, "start to hook " + className + "." + methodName);

final MS.MethodPointer<Object, Object> old = new MethodPointer<Object, Object>();
MS.hookMethod(classToHook, method, new MS.MethodHook() {

@Override
public Object invoked(Object resources, Object... args)
throws Throwable {

if(listener != null){
listener.before(className, method, resources, args);
}

Object result = old.invoke(resources, args);

if(listener != null){
listener.after(className, method, resources, args);
}
return result;
}

}, old);
}
}
});
}
}
73 changes: 73 additions & 0 deletions src/com/shuwoom/apihooker/hookers/NetworkHooker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.shuwoom.apihooker.hookers;

import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.util.Log;

import com.saurik.substrate.MS;
import com.saurik.substrate.MS.MethodPointer;
import com.shuwoom.apihooker.common.Config;
import com.shuwoom.apihooker.hookers.interfaces.HookerListener;

public class NetworkHooker extends Hooker{


@Override
public void hook() {
hookIOBridgeClass();
}

/**
* IoBridge.java
* https://android.googlesource.com/platform/libcore/+/android-4.1.1_r1/luni/src/main/java/libcore/io/IoBridge.java
*/
private void hookIOBridgeClass(){
final String className = "libcore.io.IoBridge";

final Map<String, Integer> methodsToHook = new HashMap<String, Integer>();
// methodsToHook.put("open", 1); This is in common with filesystem operation.
methodsToHook.put("recvfrom", 1);
// methodsToHook.put("read", 1);

// methodsToHook.put("write", 2);
methodsToHook.put("sendto", 2);

methodsToHook.put("getSocketLocalAddress", 1);
methodsToHook.put("getSocketLocalPort", 1);

methodsToHook.put("closeSocket", 1);
methodsToHook.put("connectErrno", 1);
methodsToHook.put("connect", 2);
methodsToHook.put("bind", 1);

hookMethods(new HookerListener(){

@Override
public void before(String className, GenericDeclaration method,
Object resources, Object... args) {
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v(Config.DEBUG_TAG, "before:{'time':'"+df.format(now)+"', 'method':'"+className + "." + ((Method)method).getName()+"'}");


}

@Override
public void after(String className, GenericDeclaration method,
Object resources, Object... args) {
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v(Config.DEBUG_TAG, "after:{'time':'"+df.format(now)+"', 'method':'"+className + "." + ((Method)method).getName()+"'}");
}


}, className, methodsToHook);
}

}
11 changes: 11 additions & 0 deletions src/com/shuwoom/apihooker/hookers/interfaces/HookerListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.shuwoom.apihooker.hookers.interfaces;

import java.lang.reflect.GenericDeclaration;

public interface HookerListener {

public void before(String className, GenericDeclaration method, Object resources, Object...args);


public void after(String className, GenericDeclaration method, Object resources, Object...args);
}

0 comments on commit 7b7863d

Please sign in to comment.