這個參數最複雜,如果一時不理解,在後邊的實例演示中,會看到詳細的使用方法
API使用實例
我們希望演示代碼盡量具有實用性,或者更接近實用性,本次演示HTTP請求的HOOK,以便查看Android應用進行了哪些HTTP請求。但是因為只是演示代碼,不能解決所有問題,具體使用需要根據業務場景確定。
本小節只對兩種HTTP請求API進行HOOK,其他HTTP請求框架的HOOK代碼類似。
本小節對於Xposed配置不再啰嗦,直接使用Java代碼描述。
1. 創建的實現類。2. HOOK java.net.URL#openConnection()
finalString appPkgName = lp.packageName;
/*
* {@linkjava.net.URL#openConnection()}方法沒有參數,所以沒有列出參數類型
*/
XposedHelpers.findAndHookMethod(URL.class,"openConnection", newXC_MethodHook() {
@Override
protected voidbeforeHookedMethod(MethodHookParam param)throwsThrowable {
URL url = (URL) param.thisObject;
Log.i(TAG,param.method+" in Application("+appPkgName+") request: "+ url.toString());
}
});3. HOOK HttpClient
/*
* (1)高版本Android系統默認不包含HttpClient,所以需要try...catch,避免後續代碼不能執行
* (2)參數類型列表為:HttpHost.class, HttpRequest.class, HttpContext.class
* (3)調用{@linkXposedHelpers#findClass(String, ClassLoader)}從類載入器中查找類
* (4)工具方法,反射調用:{@linkXposedHelpers#callMethod(java.lang.Object, java.lang.String, java.lang.Object...)}
*/
try{
ClassLoader classLoader = lp.classLoader;//當前進程的Java類載入器
//類載入器,期望從中查找到類AbstractHttpClient和HttpUriRequest
//如果找不到,HOOK失敗
//如果找得到,HOOK成功
finalClass httpClient = XposedHelpers.findClass(
"org.apache.http.impl.client.AbstractHttpClient",classLoader);
finalClass httpUriRequest = XposedHelpers.findClass(
"org.apache.http.client.methods.HttpUriRequest",classLoader);
XposedHelpers.findAndHookMethod(httpClient,
"execute",//目標方法,AbstractHttpClient類中的方法
"org.apache.http.HttpHost",// execute方法的第個參數類型
"org.apache.http.HttpRequest",// execute方法的第1個參數類型
"org.apache.http.protocol.HttpContext",// execute方法的第2個參數類型
newXC_MethodHook() {//方法回調
@Override
protected voidbeforeHookedMethod(MethodHookParam param)throwsThrowable {
Object request = param.args[1];
if(request ==null)return;
if(!httpUriRequest.isAssignableFrom(request.getClass())) {
return;
}
//參考:org.apache.http.client.methods.HttpUriRequest
Object uri = XposedHelpers.callMethod(request,"getURI");
Log.i(TAG,param.method+" in Application("+appPkgName+") request: "+ uri.toString());
}
});
}catch(XposedHelpers.ClassNotFoundError e) {
Log.e(TAG,"There is NO HttpClient",e);
}4. 指定Android應用執行HTTP請求的HOOK
如果需要對指定的Android應用進行HOOK,可以通過應用的包名進行過濾。
packagefoo.ree.demos.x2nd;
importde.robv.android.xposed.IXposedHookLoadPackage;
importde.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
public classMainHookimplementsIXposedHookLoadPackage {
private static finalStringTAG= MainHook.class.getSimpleName();
@Override
public voidhandleLoadPackage(finalLoadPackageParam lp)throwsThrowable {
finalString appPkgName = lp.packageName;
if(!"目標應用包名".equals(appPkgName))return;// "目標應用包名"需要替換為實際的包名
...... // 其他代碼
}
}
5. 完整代碼如下,通過這些即可看到Android應用執行了哪些HTTP請求。
packagefoo.ree.demos.x2nd;
importandroid.util.Log;
importjava.net.URL;
importde.robv.android.xposed.IXposedHookLoadPackage;
importde.robv.android.xposed.XC_MethodHook;
importde.robv.android.xposed.XposedHelpers;
importde.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
public classMainHookimplementsIXposedHookLoadPackage {
private static finalStringTAG= MainHook.class.getSimpleName();
@Override
public voidhandleLoadPackage(finalLoadPackageParam lp)throwsThrowable {
finalString appPkgName = lp.packageName;
if(!"目標應用包名".equals(appPkgName))return;// "目標應用包名"需要替換為實際的包名
/**
* {@linkjava.net.URL#openConnection()}方法沒有參數,所以沒有列出參數類型
*/
XposedHelpers.findAndHookMethod(URL.class,"openConnection", newXC_MethodHook() {
@Override
protected voidbeforeHookedMethod(MethodHookParam param)throwsThrowable {
URL url = (URL) param.thisObject;
Log.i(TAG,param.method+" in Application("+appPkgName+") request: "+ url.toString());
}
});
/**
* (1)高版本Android系統默認不包含HttpClient,所以需要try...catch,避免後續代碼不能執行
* (2)參數類型列表為:HttpHost.class, HttpRequest.class, HttpContext.class
* (3)調用{@linkXposedHelpers#findClass(String, ClassLoader)}從類載入器中查找類
* (4)工具方法,反射調用:{@linkXposedHelpers#callMethod(java.lang.Object, java.lang.String, java.lang.Object...)}
*/
try{
ClassLoader classLoader = lp.classLoader;//當前進程的Java類載入器
//類載入器,期望從中查找到類AbstractHttpClient和HttpUriRequest
//如果找不到,HOOK失敗
//如果找得到,HOOK成功
finalClass httpClient = XposedHelpers.findClass(
"org.apache.http.impl.client.AbstractHttpClient",classLoader);
finalClass httpUriRequest = XposedHelpers.findClass(
"org.apache.http.client.methods.HttpUriRequest",classLoader);
XposedHelpers.findAndHookMethod(httpClient,
"execute",//目標方法,AbstractHttpClient類中的方法
"org.apache.http.HttpHost",// execute方法的第個參數類型
"org.apache.http.HttpRequest",// execute方法的第1個參數類型
"org.apache.http.protocol.HttpContext",// execute方法的第2個參數類型
newXC_MethodHook() {//方法回調
@Override
protected voidbeforeHookedMethod(MethodHookParam param)throwsThrowable {
Object request = param.args[1];
if(request ==null)return;
if(!httpUriRequest.isAssignableFrom(request.getClass())) {
return;
}
//參考:org.apache.http.client.methods.HttpUriRequest
Object uri = XposedHelpers.callMethod(request,"getURI");
Log.i(TAG,param.method+" in Application("+appPkgName+") request: "+ uri.toString());
}
});
}catch(XposedHelpers.ClassNotFoundError e) {
Log.e(TAG,"There is NO HttpClient",e);
}
}
}
本小節的內容比較啰嗦,如果有哪些描述有誤,請通過留言回復。
完整代碼地址:https://github.com/fooree/fooXposed/tree/master/Foox_2nd
下一節會對一些系統API的HOOK進行演示。