Update snet extension

This commit is contained in:
topjohnwu
2018-08-22 11:50:21 +08:00
parent e42b608444
commit d2c196896d
15 changed files with 150 additions and 257 deletions

View File

@@ -1,148 +0,0 @@
package com.topjohnwu.snet;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.internal.zzlu;
/* Decompiled and modified from GooglePlayServiceUtil.class */
public class ModdedGPSUtil {
private static final String TAG = "GooglePlayServicesUtil";
static String dexPath;
static Dialog getErrorDialog(int errCode, final Activity activity, final int requestCode) {
SwapResContext ctx = new SwapResContext(activity, dexPath);
Resources res = ctx.getResources();
if (zzlu.zzQ(ctx) && errCode == 2) {
errCode = 42;
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(GooglePlayServicesUtil.zze(ctx, errCode));
String btnMsg = GooglePlayServicesUtil.zzf(ctx, errCode);
if (btnMsg != null) {
final Intent intent = GooglePlayServicesUtil.zzan(errCode);
builder.setPositiveButton(btnMsg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
PackageManager pm = activity.getPackageManager();
if (intent != null && intent.resolveActivity(pm) != null)
activity.startActivityForResult(intent, requestCode);
dialog.dismiss();
}
});
}
switch(errCode) {
case 0:
return null;
case 1:
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_install_title)).create();
case 2:
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_update_title)).create();
case 3:
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_enable_title)).create();
case 4:
case 6:
return builder.create();
case 5:
Log.e(TAG, "An invalid account was specified when connecting. Please provide a valid account.");
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_invalid_account_title)).create();
case 7:
Log.e(TAG, "Network error occurred. Please retry request later.");
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_network_error_title)).create();
case 8:
Log.e(TAG, "Internal error occurred. Please see logs for detailed information");
return builder.create();
case 9:
Log.e(TAG, "Google Play services is invalid. Cannot recover.");
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_unsupported_title)).create();
case 10:
Log.e(TAG, "Developer error occurred. Please see logs for detailed information");
return builder.create();
case 11:
Log.e(TAG, "The application is not licensed to the user.");
return builder.create();
case 12:
case 13:
case 14:
case 15:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
default:
Log.e(TAG, "Unexpected error code " + errCode);
return builder.create();
case 16:
Log.e(TAG, "One of the API components you attempted to connect to is not available.");
return builder.create();
case 17:
Log.e(TAG, "The specified account could not be signed in.");
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_google_play_services_sign_in_failed_title)).create();
case 42:
return builder.setTitle(res.getString(com.google.android.gms.R.string.common_android_wear_update_title)).create();
}
}
public static class SwapResContext extends ContextWrapper {
private AssetManager asset;
private Resources resources;
public SwapResContext(Context base, String apk) {
super(base);
try {
asset = AssetManager.class.newInstance();
AssetManager.class.getMethod("addAssetPath", String.class).invoke(asset, apk);
} catch (Exception e) {
e.printStackTrace();
}
Resources res = base.getResources();
resources = new Resources(asset, res.getDisplayMetrics(), res.getConfiguration());
}
@Override
public Resources getResources() {
return resources;
}
@Override
public AssetManager getAssets() {
return asset;
}
}
}

View File

@@ -1,15 +1,24 @@
package com.topjohnwu.snet;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Base64;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.internal.ConnectionErrorMessages;
import com.google.android.gms.common.internal.DialogRedirect;
import com.google.android.gms.safetynet.SafetyNet;
import com.google.android.gms.safetynet.SafetyNetApi;
import com.google.android.gms.safetynet.SafetyNetClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import org.json.JSONException;
import org.json.JSONObject;
@@ -18,54 +27,29 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.security.SecureRandom;
public class SafetyNetHelper implements InvocationHandler, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, ResultCallback<SafetyNetApi.AttestationResult> {
public class SafetyNetHelper implements InvocationHandler,
OnSuccessListener<SafetyNetApi.AttestationResponse>, OnFailureListener {
public static final int CAUSE_SERVICE_DISCONNECTED = 0x01;
public static final int CAUSE_NETWORK_LOST = 0x02;
public static final int RESPONSE_ERR = 0x04;
public static final int CONNECTION_FAIL = 0x08;
private static final int RESPONSE_ERR = 0x01;
private static final int CONNECTION_FAIL = 0x02;
private static final int BASIC_PASS = 0x10;
private static final int CTS_PASS = 0x20;
public static final int BASIC_PASS = 0x10;
public static final int CTS_PASS = 0x20;
private static final GoogleApiAvailability API_AVAIL = GoogleApiAvailability.getInstance();
private static final SecureRandom RANDOM = new SecureRandom();
private static final String TAG = "SNET";
public static final int SNET_EXT_VER = 10;
/* Insert the magic API key here :) */
private static final String API_KEY = "";
private GoogleApiClient mGoogleApiClient;
private Activity mActivity;
private Object callback;
private final Activity mActivity;
private final Object callback;
SafetyNetHelper(Activity activity, Object cb) {
mActivity = activity;
callback = cb;
}
/* Override ISafetyNetHelper.getVersion */
private int getVersion() {
return SNET_EXT_VER;
}
/* Override ISafetyNetHelper.attest */
private void attest() {
// Connect Google Service
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addApi(SafetyNet.API)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.build();
mGoogleApiClient.connect();
}
@Override
public Object invoke(Object o, Method method, Object[] args) {
if (method.getName().equals("attest")) {
attest();
} else if (method.getName().equals("getVersion")) {
return getVersion();
}
return null;
}
private void invokeCallback(int code) {
Class<?> clazz = callback.getClass();
try {
@@ -73,34 +57,50 @@ public class SafetyNetHelper implements InvocationHandler, GoogleApiClient.Conne
} catch (Exception ignored) {}
}
@Override
public void onConnectionSuspended(int i) {
invokeCallback(i);
/* Override ISafetyNetHelper.getVersion */
private int getVersion() {
return BuildConfig.VERSION_CODE;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (GooglePlayServicesUtil.isUserRecoverableError(result.getErrorCode()))
ModdedGPSUtil.getErrorDialog(result.getErrorCode(), mActivity, 0).show();
invokeCallback(CONNECTION_FAIL);
}
@Override
public void onConnected(Bundle bundle) {
/* Override ISafetyNetHelper.attest */
private void attest() {
int code = API_AVAIL.isGooglePlayServicesAvailable(mActivity);
if (code != ConnectionResult.SUCCESS) {
if (API_AVAIL.isUserResolvableError(code))
getErrorDialog(code, 0).show();
invokeCallback(CONNECTION_FAIL);
return;
}
// Create nonce
byte[] nonce = new byte[24];
new SecureRandom().nextBytes(nonce);
RANDOM.nextBytes(nonce);
// Call SafetyNet
SafetyNet.SafetyNetApi.attest(mGoogleApiClient, nonce).setResultCallback(this);
SafetyNetClient client = SafetyNet.getClient(mActivity.getBaseContext());
client.attest(nonce, API_KEY).addOnSuccessListener(this).addOnFailureListener(this);
}
private Dialog getErrorDialog(int errorCode, int requestCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Context swapCtx = new SwapResContext(mActivity, Snet.dexPath);
Intent intent = API_AVAIL.getErrorResolutionIntent(swapCtx, errorCode, "d");
builder.setMessage(ConnectionErrorMessages.getErrorMessage(swapCtx, errorCode));
builder.setPositiveButton(
ConnectionErrorMessages.getErrorDialogButtonMessage(swapCtx, errorCode),
DialogRedirect.getInstance(mActivity, intent, requestCode));
String title;
if ((title = ConnectionErrorMessages.getErrorTitle(swapCtx, errorCode)) != null) {
builder.setTitle(title);
}
return builder.create();
}
@Override
public void onResult(SafetyNetApi.AttestationResult result) {
public void onSuccess(SafetyNetApi.AttestationResponse result) {
int code = 0;
try {
if (!result.getStatus().isSuccess())
throw new JSONException("");
String jsonStr = new String(Base64.decode(
result.getJwsResult().split("\\.")[1], Base64.DEFAULT));
JSONObject json = new JSONObject(jsonStr);
@@ -110,10 +110,34 @@ public class SafetyNetHelper implements InvocationHandler, GoogleApiClient.Conne
code = RESPONSE_ERR;
}
// Disconnect
mGoogleApiClient.disconnect();
// Return results
invokeCallback(code);
}
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
int errCode = ((ApiException) e).getStatusCode();
if (API_AVAIL.isUserResolvableError(errCode))
getErrorDialog(errCode, 0).show();
else
Log.e(TAG, "Cannot resolve: " + e.getMessage());
} else {
Log.e(TAG, "Unknown: " + e.getMessage());
}
invokeCallback(CONNECTION_FAIL);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
switch (method.getName()) {
case "attest":
attest();
return null;
case "getVersion":
return getVersion();
default:
return null;
}
}
}

View File

@@ -5,9 +5,11 @@ import android.app.Activity;
import java.lang.reflect.Proxy;
public class Snet {
public static Object newHelper(Class<?> clazz, String dexPath, Activity activity, Object cb) {
ModdedGPSUtil.dexPath = dexPath;
static String dexPath;
public static Object newHelper(Class<?> interfaceClass, String dexPath, Activity activity, Object cb) {
Snet.dexPath = dexPath;
return Proxy.newProxyInstance(SafetyNetHelper.class.getClassLoader(),
new Class[] { clazz }, new SafetyNetHelper(activity, cb));
new Class[] { interfaceClass }, new SafetyNetHelper(activity, cb));
}
}

View File

@@ -0,0 +1,34 @@
package com.topjohnwu.snet;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.AssetManager;
import android.content.res.Resources;
public class SwapResContext extends ContextWrapper {
private AssetManager asset;
private Resources resources;
public SwapResContext(Context base, String apk) {
super(base);
try {
asset = AssetManager.class.newInstance();
AssetManager.class.getMethod("addAssetPath", String.class).invoke(asset, apk);
} catch (Exception e) {
e.printStackTrace();
}
Resources res = base.getResources();
resources = new Resources(asset, res.getDisplayMetrics(), res.getConfiguration());
}
@Override
public Resources getResources() {
return resources;
}
@Override
public AssetManager getAssets() {
return asset;
}
}