Loading core/java/android/app/Instrumentation.java +4 −0 Original line number Diff line number Diff line Loading @@ -63,6 +63,8 @@ import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; import com.android.internal.safetynet.SafetyNetHooks; /** * Base class for implementing application instrumentation code. When running * with instrumentation turned on, this class will be instantiated for you Loading Loading @@ -1157,6 +1159,7 @@ public class Instrumentation { Application app = getFactory(context.getPackageName()) .instantiateApplication(cl, className); app.attach(context); SafetyNetHooks.init(app); return app; } Loading @@ -1174,6 +1177,7 @@ public class Instrumentation { ClassNotFoundException { Application app = (Application)clazz.newInstance(); app.attach(context); SafetyNetHooks.init(app); return app; } Loading core/java/com/android/internal/safetynet/SafetyNetHooks.java 0 → 100644 +60 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.internal.safetynet; import java.lang.reflect.Field; import java.util.Arrays; import android.app.Application; import android.os.Build; import android.os.Build.VERSION; import android.util.Log; public final class SafetyNetHooks { private static final String TAG = "SafetyNetHooks"; private static final String GMS_PACKAGE_NAME = "com.google.android.gms"; private static void setBuildField(String key, String value) { try { Field field = Build.class.getDeclaredField(key); field.setAccessible(true); field.set(null, value); field.setAccessible(false); } catch (NoSuchFieldException | IllegalAccessException e) { Log.e(TAG, "Failed to fake Build." + key, e); } } private static void setSecurityPatch(String value) { try { Field field = VERSION.class.getDeclaredField("SECURITY_PATCH"); field.setAccessible(true); field.set(null, value); field.setAccessible(false); } catch (NoSuchFieldException | IllegalAccessException e) { Log.e(TAG, "Failed to fake Build.VERSION.SECURITY_PATCH", e); } } public static void init(Application app) { if (GMS_PACKAGE_NAME.equals(app.getPackageName())) { setBuildField("MODEL", Build.MODEL + " "); setSecurityPatch("2021-10-05"); } } } Loading
core/java/android/app/Instrumentation.java +4 −0 Original line number Diff line number Diff line Loading @@ -63,6 +63,8 @@ import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; import com.android.internal.safetynet.SafetyNetHooks; /** * Base class for implementing application instrumentation code. When running * with instrumentation turned on, this class will be instantiated for you Loading Loading @@ -1157,6 +1159,7 @@ public class Instrumentation { Application app = getFactory(context.getPackageName()) .instantiateApplication(cl, className); app.attach(context); SafetyNetHooks.init(app); return app; } Loading @@ -1174,6 +1177,7 @@ public class Instrumentation { ClassNotFoundException { Application app = (Application)clazz.newInstance(); app.attach(context); SafetyNetHooks.init(app); return app; } Loading
core/java/com/android/internal/safetynet/SafetyNetHooks.java 0 → 100644 +60 −0 Original line number Diff line number Diff line /* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.internal.safetynet; import java.lang.reflect.Field; import java.util.Arrays; import android.app.Application; import android.os.Build; import android.os.Build.VERSION; import android.util.Log; public final class SafetyNetHooks { private static final String TAG = "SafetyNetHooks"; private static final String GMS_PACKAGE_NAME = "com.google.android.gms"; private static void setBuildField(String key, String value) { try { Field field = Build.class.getDeclaredField(key); field.setAccessible(true); field.set(null, value); field.setAccessible(false); } catch (NoSuchFieldException | IllegalAccessException e) { Log.e(TAG, "Failed to fake Build." + key, e); } } private static void setSecurityPatch(String value) { try { Field field = VERSION.class.getDeclaredField("SECURITY_PATCH"); field.setAccessible(true); field.set(null, value); field.setAccessible(false); } catch (NoSuchFieldException | IllegalAccessException e) { Log.e(TAG, "Failed to fake Build.VERSION.SECURITY_PATCH", e); } } public static void init(Application app) { if (GMS_PACKAGE_NAME.equals(app.getPackageName())) { setBuildField("MODEL", Build.MODEL + " "); setSecurityPatch("2021-10-05"); } } }