-
Notifications
You must be signed in to change notification settings - Fork 86
/
PropImitationHooks.java
280 lines (248 loc) · 11.6 KB
/
PropImitationHooks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (C) 2022 Paranoid Android
* (C) 2023 ArrowOS
* (C) 2023 The LibreMobileOS Foundation
*
* 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.util;
import android.app.ActivityTaskManager;
import android.app.Application;
import android.app.TaskStackListener;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Binder;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.R;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
public class PropImitationHooks {
private static final String TAG = "PropImitationHooks";
private static final boolean DEBUG = false;
private static final String[] sCertifiedProps =
Resources.getSystem().getStringArray(R.array.config_certifiedBuildProperties);
private static final String sStockFp =
Resources.getSystem().getString(R.string.config_stockFingerprint);
private static final String sNetflixModel =
Resources.getSystem().getString(R.string.config_netflixSpoofModel);
private static final boolean sSpoofGapps =
Resources.getSystem().getBoolean(R.bool.config_spoofGoogleApps);
private static final String PACKAGE_ARCORE = "com.google.ar.core";
private static final String PACKAGE_ASI = "com.google.android.as";
private static final String PACKAGE_FINSKY = "com.android.vending";
private static final String PACKAGE_GMS = "com.google.android.gms";
private static final String PACKAGE_GPHOTOS = "com.google.android.apps.photos";
private static final String PACKAGE_NETFLIX = "com.netflix.mediaclient";
private static final String PACKAGE_SNAPCHAT = "com.snapchat.android";
private static final String PACKAGE_SMS_ORGANIZER = "com.microsoft.android.smsorganizer";
private static final String PACKAGE_VELVET = "com.google.android.googlequicksearchbox";
private static final String PACKAGE_LIVE_WALLPAPER = "com.google.pixel.livewallpaper";
private static final String PACKAGE_WALLPAPER_EMOJI = "com.google.android.apps.emojiwallpaper";
private static final String PACKAGE_PIXEL_LAUNCHER = "com.google.android.apps.nexuslauncher";
private static final String PROCESS_GMS_PERSISTENT = PACKAGE_GMS + ".persistent";
private static final String PROCESS_GMS_UNSTABLE = PACKAGE_GMS + ".unstable";
private static final Map<String, Object> sPixel7Props = Map.of(
"BRAND", "google",
"MANUFACTURER", "Google",
"DEVICE", "cheetah",
"PRODUCT", "cheetah",
"MODEL", "Pixel 7 Pro",
"FINGERPRINT", "google/cheetah/cheetah:13/TQ3A.230805.001/10316531:user/release-keys"
);
private static final Map<String, Object> sPixelProps = Map.of(
"BRAND", "google",
"MANUFACTURER", "Google",
"DEVICE", "redfin",
"PRODUCT", "redfin",
"MODEL", "Pixel 5",
"FINGERPRINT", "google/redfin/redfin:13/TQ3A.230805.001/10316531:user/release-keys"
);
private static final ComponentName GMS_ADD_ACCOUNT_ACTIVITY = ComponentName.unflattenFromString(
"com.google.android.gms/.auth.uiflows.minutemaid.MinuteMaidActivity");
private static final Map<String, Object> sPixelXLProps = Map.of(
"BRAND", "google",
"MANUFACTURER", "Google",
"DEVICE", "marlin",
"PRODUCT", "marlin",
"MODEL", "Pixel XL",
"FINGERPRINT", "google/marlin/marlin:10/QP1A.191005.007.A3/5972272:user/release-keys"
);
private static final Set<String> sFeatureBlacklist = Set.of(
"PIXEL_2017_PRELOAD",
"PIXEL_2018_PRELOAD",
"PIXEL_2019_MIDYEAR_PRELOAD",
"PIXEL_2019_PRELOAD",
"PIXEL_2020_EXPERIENCE",
"PIXEL_2020_MIDYEAR_EXPERIENCE",
"PIXEL_2021_EXPERIENCE",
"PIXEL_2021_MIDYEAR_EXPERIENCE"
);
private static final Set<String> sTensorFeatureBlacklist = Set.of(
"PIXEL_2022_EXPERIENCE",
"PIXEL_2022_MIDYEAR_EXPERIENCE",
"PIXEL_2021_EXPERIENCE",
"PIXEL_2021_MIDYEAR_EXPERIENCE"
);
private static volatile String sProcessName;
private static volatile boolean sIsGms, sIsFinsky, sIsPhotos, sIsPixelLauncher, sIsASI;
public static void setProps(Context context) {
final String packageName = context.getPackageName();
final String processName = Application.getProcessName();
setPropValue("TYPE", "user");
setPropValue("TAGS", "release-keys");
if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(processName)) {
Log.e(TAG, "Null package or process name");
return;
}
sProcessName = processName;
sIsGms = packageName.equals(PACKAGE_GMS) && processName.equals(PROCESS_GMS_UNSTABLE);
sIsFinsky = packageName.equals(PACKAGE_FINSKY);
sIsPhotos = sSpoofGapps && packageName.equals(PACKAGE_GPHOTOS);
sIsPixelLauncher = sSpoofGapps && packageName.equals(PACKAGE_PIXEL_LAUNCHER);
sIsASI = sSpoofGapps && packageName.equals(PACKAGE_ASI);
/* Set certified properties for GMSCore
* Set stock fingerprint for ARCore
* Set Pixel 5 for Snapchat, SMS Organizer, Google, ASI and GMS device configurator
* Set Pixel 7 Pro for Live wallpaper and WallpaperEmoji
* Set Pixel XL for Google Photos
* Set custom model for Netflix
*/
if (sIsGms) {
setCertifiedPropsForGms();
} else if (!sStockFp.isEmpty() && packageName.equals(PACKAGE_ARCORE)) {
dlog("Setting stock fingerprint for: " + packageName);
setPropValue("FINGERPRINT", sStockFp);
} else if (sSpoofGapps && (packageName.equals(PACKAGE_VELVET)
|| packageName.equals(PACKAGE_ASI)
|| packageName.equals(PACKAGE_SNAPCHAT)
|| packageName.equals(PACKAGE_SMS_ORGANIZER)
|| (packageName.equals(PACKAGE_GMS)
&& processName.equals(PROCESS_GMS_PERSISTENT)))) {
dlog("Spoofing Pixel 5 for: " + packageName + " process: " + processName);
sPixelProps.forEach(PropImitationHooks::setPropValue);
} else if (sSpoofGapps && (packageName.equals(PACKAGE_WALLPAPER_EMOJI)
|| packageName.equals(PACKAGE_LIVE_WALLPAPER))) {
dlog("Spoofing Pixel 7 Pro for: " + packageName + " process: " + processName);
sPixel7Props.forEach(PropImitationHooks::setPropValue);
} else if (sIsPhotos) {
dlog("Spoofing Pixel XL for Google Photos");
sPixelXLProps.forEach(PropImitationHooks::setPropValue);
} else if (!sNetflixModel.isEmpty() && packageName.equals(PACKAGE_NETFLIX)) {
dlog("Setting model to " + sNetflixModel + " for Netflix");
setPropValue("MODEL", sNetflixModel);
}
}
private static void setPropValue(String key, Object value) {
try {
dlog("Setting prop " + key + " to " + value.toString());
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 set prop " + key, e);
}
}
private static void setCertifiedPropsForGms() {
if (sCertifiedProps.length != 4) {
Log.e(TAG, "Insufficient array size for certified props: "
+ sCertifiedProps.length + ", required 4");
return;
}
final boolean was = isGmsAddAccountActivityOnTop();
final TaskStackListener taskStackListener = new TaskStackListener() {
@Override
public void onTaskStackChanged() {
final boolean is = isGmsAddAccountActivityOnTop();
if (is ^ was) {
dlog("GmsAddAccountActivityOnTop is:" + is + " was:" + was +
", killing myself!"); // process will restart automatically later
Process.killProcess(Process.myPid());
}
}
};
if (!was) {
dlog("Spoofing build for GMS");
setPropValue("DEVICE", sCertifiedProps[0]);
setPropValue("PRODUCT", sCertifiedProps[1]);
setPropValue("MODEL", sCertifiedProps[2]);
setPropValue("FINGERPRINT", sCertifiedProps[3]);
} else {
dlog("Skip spoofing build for GMS, because GmsAddAccountActivityOnTop");
}
try {
ActivityTaskManager.getService().registerTaskStackListener(taskStackListener);
} catch (Exception e) {
Log.e(TAG, "Failed to register task stack listener!", e);
}
}
private static boolean isGmsAddAccountActivityOnTop() {
try {
final ActivityTaskManager.RootTaskInfo focusedTask =
ActivityTaskManager.getService().getFocusedRootTaskInfo();
return focusedTask != null && focusedTask.topActivity != null
&& focusedTask.topActivity.equals(GMS_ADD_ACCOUNT_ACTIVITY);
} catch (Exception e) {
Log.e(TAG, "Unable to get top activity!", e);
}
return false;
}
public static boolean shouldBypassTaskPermission(Context context) {
// GMS doesn't have MANAGE_ACTIVITY_TASKS permission
final int callingUid = Binder.getCallingUid();
final int gmsUid;
try {
gmsUid = context.getPackageManager().getApplicationInfo(PACKAGE_GMS, 0).uid;
dlog("shouldBypassTaskPermission: gmsUid:" + gmsUid + " callingUid:" + callingUid);
} catch (Exception e) {
Log.e(TAG, "shouldBypassTaskPermission: unable to get gms uid", e);
return false;
}
return gmsUid == callingUid;
}
private static boolean isCallerSafetyNet() {
return sIsGms && Arrays.stream(Thread.currentThread().getStackTrace())
.anyMatch(elem -> elem.getClassName().contains("DroidGuard"));
}
public static void onEngineGetCertificateChain() {
// Check stack for SafetyNet or Play Integrity
if (isCallerSafetyNet() || sIsFinsky) {
dlog("Blocked key attestation sIsGms=" + sIsGms + " sIsFinsky=" + sIsFinsky);
throw new UnsupportedOperationException();
}
}
public static boolean hasSystemFeature(String name, boolean def) {
if (sIsPhotos && def && sFeatureBlacklist.stream().anyMatch(name::contains)) {
dlog("Blocked system feature " + name + " for Google Photos");
return false;
}
if (sIsASI && def && sTensorFeatureBlacklist.stream().anyMatch(name::contains)) {
dlog("Blocked system feature " + name + " for ASI");
return false;
}
if (sIsPixelLauncher && def && sTensorFeatureBlacklist.stream().anyMatch(name::contains)) {
dlog("Blocked system feature " + name + " for Pixel Launcher");
return false;
}
return def;
}
private static void dlog(String msg) {
if (DEBUG) Log.d(TAG, "[" + sProcessName + "] " + msg);
}
}