APP開發中常須要獲取設備的DeviceId,以應對刷單,目前經常使用的幾個設備識別碼主要有IMEI(國際移動設備身份碼 International Mobile Equipment Identity)或者MEID(Mobile Equipment IDentifier),這二者也是常說的DeviceId,不過Android6.0以後須要權限才能獲取,並且,在Java層這個ID很容易被Hook,可能並不靠譜,另外也能夠經過MAC地址或者藍牙地址,序列號等,暫列以下:android
以上四個是經常使用的Android識別碼,系統也提供了詳情的接口讓開發者獲取,可是因爲都是Java層方法,很容易被Hook,尤爲是有些專門刷單的,在手機Root以後,利用Xposed框架裏的一些插件很容易將獲取的數據給篡改。舉個最簡單的IMEI的獲取,經常使用的獲取方式以下:git
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
return telephonyManager.getDeviceId()複製代碼
假如Root用戶利用Xposed Hook了TelephonyManager類的getDeviceId()方法,以下,在afterHookedMethod方法中,將DeviceId設置爲隨機數,這樣每次獲取的DeviceId都是不一樣的。github
public class XposedModule implements IXposedHookLoadPackage {
try {
findAndHookMethod(TelephonyManager.class.getName(), lpparam.classLoader, "getDeviceId", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
param.setResult("" + System.currentTimeMillis());
}
});
} catch (Exception e1) {
}catch (Error e) {
} }複製代碼
因此爲了獲取相對準確的設備信息咱們須要採起相應的應對措施,好比:bash
首先看一下看一下如何獲取getDeviceId,源碼以下app
public String getDeviceId() {
try {
return getITelephony().getDeviceId();
} catch (RemoteException ex) {
return null;
} catch (NullPointerException ex) {
return null;
}
}
private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
}複製代碼
若是getDeviceId被Hook可是 getITelephony沒被Hook,咱們就能夠直接經過反射獲取TelephonyManager的getITelephony方法,進一步經過ITelephony的getDeviceId獲取DeviceId,不過這個方法跟ROM版本有關係,比較早的版本壓根沒有getITelephony方法,早期可能經過IPhoneSubInfo的getDeviceId來獲取,不過以上兩種方式都很容被Hook,既然能夠Hook getDeviceId方法,同理也能夠Hook getITelephony方法,這個層次的反Hook並無多大意義。所以,能夠稍微深刻一下。ITelephony.Stub.asInterface,這是一個很明顯的Binder通訊的方式,那麼不如,咱們本身獲取Binder代理,進而利用Binder通訊的方式向Phone服務發送請求,獲取設備DeviceId,Phone服務是利用aidl文件生成的Proxy與Stub,能夠基於這個來實現咱們的代碼,Binder通訊比較重要的幾點:InterfaceDescriptor+TransactionId+參數,獲取DeviceId的幾乎不須要什麼參數(低版本可能須要)。具體作法是:框架
具體實現以下,這種作法能夠應對代理方的Hook。ide
public static int getTransactionId(Object proxy,
String name) throws RemoteException, NoSuchFieldException, IllegalAccessException {
int transactionId = 0;
Class outclass = proxy.getClass().getEnclosingClass();
Field idField = outclass.getDeclaredField(name);
idField.setAccessible(true);
transactionId = (int) idField.get(proxy);
return transactionId;
}
//根據方法名,反射得到方法transactionId
public static String getInterfaceDescriptor(Object proxy) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method getInterfaceDescriptor = proxy.getClass().getDeclaredMethod("getInterfaceDescriptor");
return (String) getInterfaceDescriptor.invoke(proxy);
}
static String getDeviceIdLevel2(Context context) {
String deviceId = "";
try {
Class ServiceManager = Class.forName("android.os.ServiceManager");
Method getService = ServiceManager.getDeclaredMethod("getService", String.class);
getService.setAccessible(true);
IBinder binder = (IBinder) getService.invoke(null, Context.TELEPHONY_SERVICE);
Class Stub = Class.forName("com.android.internal.telephony.ITelephony$Stub");
Method asInterface = Stub.getDeclaredMethod("asInterface", IBinder.class);
asInterface.setAccessible(true);
Object binderProxy = asInterface.invoke(null, binder);
try {
Method getDeviceId = binderProxy.getClass().getDeclaredMethod("getDeviceId", String.class);
if (getDeviceId != null) {
deviceId = binderGetHardwareInfo(context.getPackageName(),
binder, getInterfaceDescriptor(binderProxy),
getTransactionId(binderProxy, "TRANSACTION_getDeviceId"));
}
} catch (Exception e) {
}
Method getDeviceId = binderProxy.getClass().getDeclaredMethod("getDeviceId");
if (getDeviceId != null) {
deviceId = binderGetHardwareInfo("",
binder, BinderUtil.getInterfaceDescriptor(binderProxy),
BinderUtil.getTransactionId(binderProxy, "TRANSACTION_getDeviceId"));
}
} catch (Exception e) {
}
return deviceId;
}
private static String binderGetHardwareInfo(String callingPackage,
IBinder remote,
String DESCRIPTOR,
int tid) throws RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
if (!TextUtils.isEmpty(callingPackage)) {
_data.writeString(callingPackage);
}
remote.transact(tid, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}複製代碼
有不少系統參數咱們是經過Build來獲取的,好比序列號、手機硬件信息等,例如獲取序列號,在Java層直接利用Build的feild獲取便可ui
public static final String SERIAL = getString("ro.serialno");
private static String getString(String property) {
return SystemProperties.get(property, UNKNOWN);
}複製代碼
不過SystemProperties的get方法很容被Hook,被Hook以後序列號就能夠隨便更改,不過好在SystemProperties類是經過native方法來獲取硬件信息的,咱們能夠本身編寫native代碼來獲取硬件參數,這樣就避免被Java Hook,spa
public static String get(String key) {
if (key.length() > PROP_NAME_MAX) {
throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
}
return native_get(key);
}複製代碼
來看一下native源碼插件
static jstring SystemProperties_getSS(JNIEnv *env, jobject clazz,
jstring keyJ, jstring defJ)
{
int len;
const char* key;
char buf[PROPERTY_VALUE_MAX];
jstring rvJ = NULL;
if (keyJ == NULL) {
jniThrowNullPointerException(env, "key must not be null.");
goto error;
}
key = env->GetStringUTFChars(keyJ, NULL);
len = property_get(key, buf, "");
if ((len <= 0) && (defJ != NULL)) {
rvJ = defJ;
} else if (len >= 0) {
rvJ = env->NewStringUTF(buf);
} else {
rvJ = env->NewStringUTF("");
}
env->ReleaseStringUTFChars(keyJ, key);
error:
return rvJ;
}複製代碼
參考這部分源碼,本身實現.so庫便可,這樣既能夠避免被Java層Hook。
做者:看書的小蝸牛
原文連接獲取Android設備DeviceId與反Xposed Hook