/
EmulatorDetector.java
498 lines (425 loc) · 16.6 KB
/
EmulatorDetector.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package com.framgia.android.emulator;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Copyright 2016 Framgia, Inc.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
* <p>
* Created by Pham Quy Hai on 5/16/16.
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public final class EmulatorDetector {
public interface OnEmulatorDetectorListener {
void onResult(boolean isEmulator);
}
private static final String[] PHONE_NUMBERS = {
"15555215554", "15555215556", "15555215558", "15555215560", "15555215562", "15555215564",
"15555215566", "15555215568", "15555215570", "15555215572", "15555215574", "15555215576",
"15555215578", "15555215580", "15555215582", "15555215584"
};
private static final String[] DEVICE_IDS = {
"000000000000000",
"e21833235b6eef10",
"012345678912345"
};
private static final String[] IMSI_IDS = {
"310260000000000"
};
private static final String[] GENY_FILES = {
"/dev/socket/genyd",
"/dev/socket/baseband_genyd"
};
private static final String[] QEMU_DRIVERS = {"goldfish"};
private static final String[] PIPES = {
"/dev/socket/qemud",
"/dev/qemu_pipe"
};
private static final String[] X86_FILES = {
"ueventd.android_x86.rc",
"x86.prop",
"ueventd.ttVM_x86.rc",
"init.ttVM_x86.rc",
"fstab.ttVM_x86",
"fstab.vbox86",
"init.vbox86.rc",
"ueventd.vbox86.rc"
};
private static final String[] ANDY_FILES = {
"fstab.andy",
"ueventd.andy.rc"
};
private static final String[] NOX_FILES = {
"fstab.nox",
"init.nox.rc",
"ueventd.nox.rc"
};
private static final Property[] PROPERTIES = {
new Property("init.svc.qemud", null),
new Property("init.svc.qemu-props", null),
new Property("qemu.hw.mainkeys", null),
new Property("qemu.sf.fake_camera", null),
new Property("qemu.sf.lcd_density", null),
new Property("ro.bootloader", "unknown"),
new Property("ro.bootmode", "unknown"),
new Property("ro.hardware", "goldfish"),
new Property("ro.kernel.android.qemud", null),
new Property("ro.kernel.qemu.gles", null),
new Property("ro.kernel.qemu", "1"),
new Property("ro.product.device", "generic"),
new Property("ro.product.model", "sdk"),
new Property("ro.product.name", "sdk"),
new Property("ro.serialno", null)
};
private static final String IP = "10.0.2.15";
private static final int MIN_PROPERTIES_THRESHOLD = 0x5;
private final Context mContext;
private boolean isDebug = false;
private boolean isTelephony = false;
private boolean isCheckPackage = true;
private List<String> mListPackageName = new ArrayList<>();
@SuppressLint("StaticFieldLeak") //Since we use application context now this won't leak memory anymore. This is only to please Lint
private static EmulatorDetector mEmulatorDetector;
public static EmulatorDetector with(Context pContext) {
if (pContext == null) {
throw new IllegalArgumentException("Context must not be null.");
}
if (mEmulatorDetector == null)
mEmulatorDetector = new EmulatorDetector(pContext.getApplicationContext());
return mEmulatorDetector;
}
private EmulatorDetector(Context pContext) {
mContext = pContext;
mListPackageName.add("com.google.android.launcher.layouts.genymotion");
mListPackageName.add("com.bluestacks");
mListPackageName.add("com.bignox.app");
}
public EmulatorDetector setDebug(boolean isDebug) {
this.isDebug = isDebug;
return this;
}
public boolean isDebug() {
return isDebug;
}
public boolean isCheckTelephony() {
return isTelephony;
}
public boolean isCheckPackage() {
return isCheckPackage;
}
public EmulatorDetector setCheckTelephony(boolean telephony) {
this.isTelephony = telephony;
return this;
}
public EmulatorDetector setCheckPackage(boolean chkPackage) {
this.isCheckPackage = chkPackage;
return this;
}
public EmulatorDetector addPackageName(String pPackageName) {
this.mListPackageName.add(pPackageName);
return this;
}
public EmulatorDetector addPackageName(List<String> pListPackageName) {
this.mListPackageName.addAll(pListPackageName);
return this;
}
public List<String> getPackageNameList() {
return this.mListPackageName;
}
public void detect(final OnEmulatorDetectorListener pOnEmulatorDetectorListener) {
new Thread(new Runnable() {
@Override
public void run() {
boolean isEmulator = detect();
log("This System is Emulator: " + isEmulator);
if (pOnEmulatorDetectorListener != null) {
pOnEmulatorDetectorListener.onResult(isEmulator);
}
}
}).start();
}
private boolean detect() {
boolean result = false;
log(getDeviceInfo());
// Check Basic
if (!result) {
result = checkBasic();
log("Check basic " + result);
}
// Check Advanced
if (!result) {
result = checkAdvanced();
log("Check Advanced " + result);
}
// Check Package Name
if (!result) {
result = checkPackageName();
log("Check Package Name " + result);
}
return result;
}
private boolean checkBasic() {
boolean result = Build.FINGERPRINT.startsWith("generic")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.toLowerCase().contains("droid4x")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.HARDWARE.equals("goldfish")
|| Build.HARDWARE.equals("vbox86")
|| Build.PRODUCT.equals("sdk")
|| Build.PRODUCT.equals("google_sdk")
|| Build.PRODUCT.equals("sdk_x86")
|| Build.PRODUCT.equals("vbox86p")
|| Build.BOARD.toLowerCase().contains("nox")
|| Build.BOOTLOADER.toLowerCase().contains("nox")
|| Build.HARDWARE.toLowerCase().contains("nox")
|| Build.PRODUCT.toLowerCase().contains("nox")
|| Build.SERIAL.toLowerCase().contains("nox");
if (result) return true;
result |= Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic");
if (result) return true;
result |= "google_sdk".equals(Build.PRODUCT);
return result;
}
private boolean checkAdvanced() {
boolean result = checkTelephony()
|| checkFiles(GENY_FILES, "Geny")
|| checkFiles(ANDY_FILES, "Andy")
|| checkFiles(NOX_FILES, "Nox")
|| checkQEmuDrivers()
|| checkFiles(PIPES, "Pipes")
|| checkIp()
|| (checkQEmuProps() && checkFiles(X86_FILES, "X86"));
return result;
}
private boolean checkPackageName() {
if (!isCheckPackage || mListPackageName.isEmpty()) {
return false;
}
final PackageManager packageManager = mContext.getPackageManager();
for (final String pkgName : mListPackageName) {
final Intent tryIntent = packageManager.getLaunchIntentForPackage(pkgName);
if (tryIntent != null) {
final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(tryIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!resolveInfos.isEmpty()) {
return true;
}
}
}
return false;
}
private boolean checkTelephony() {
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
== PackageManager.PERMISSION_GRANTED && this.isTelephony && isSupportTelePhony()) {
return checkPhoneNumber()
|| checkDeviceId()
|| checkImsi()
|| checkOperatorNameAndroid();
}
return false;
}
private boolean checkPhoneNumber() {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
try {
@SuppressLint({"HardwareIds", "MissingPermission"})
String phoneNumber = telephonyManager.getLine1Number();
for (String number : PHONE_NUMBERS) {
if (number.equalsIgnoreCase(phoneNumber)) {
log(" check phone number is detected");
return true;
}
}
} catch (Exception e) {
log("No permission to detect access of Line1Number");
}
return false;
}
private boolean checkDeviceId() {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
try {
@SuppressLint({"HardwareIds", "MissingPermission"})
String deviceId = telephonyManager.getDeviceId();
for (String known_deviceId : DEVICE_IDS) {
if (known_deviceId.equalsIgnoreCase(deviceId)) {
log("Check device id is detected");
return true;
}
}
} catch (Exception e) {
log("No permission to detect access of DeviceId");
}
return false;
}
private boolean checkImsi() {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
try {
@SuppressLint({"HardwareIds", "MissingPermission"})
String imsi = telephonyManager.getSubscriberId();
for (String known_imsi : IMSI_IDS) {
if (known_imsi.equalsIgnoreCase(imsi)) {
log("Check imsi is detected");
return true;
}
}
} catch (Exception e) {
log("No permission to detect access of SubscriberId");
}
return false;
}
private boolean checkOperatorNameAndroid() {
String operatorName = ((TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkOperatorName();
if (operatorName.equalsIgnoreCase("android")) {
log("Check operator name android is detected");
return true;
}
return false;
}
private boolean checkQEmuDrivers() {
for (File drivers_file : new File[]{new File("/proc/tty/drivers"), new File("/proc/cpuinfo")}) {
if (drivers_file.exists() && drivers_file.canRead()) {
byte[] data = new byte[1024];
try {
InputStream is = new FileInputStream(drivers_file);
is.read(data);
is.close();
} catch (Exception exception) {
exception.printStackTrace();
}
String driver_data = new String(data);
for (String known_qemu_driver : QEMU_DRIVERS) {
if (driver_data.contains(known_qemu_driver)) {
log("Check QEmuDrivers is detected");
return true;
}
}
}
}
return false;
}
private boolean checkFiles(String[] targets, String type) {
for (String pipe : targets) {
File qemu_file = new File(pipe);
if (qemu_file.exists()) {
log("Check " + type + " is detected");
return true;
}
}
return false;
}
private boolean checkQEmuProps() {
int found_props = 0;
for (Property property : PROPERTIES) {
String property_value = getProp(mContext, property.name);
if ((property.seek_value == null) && (property_value != null)) {
found_props++;
}
if ((property.seek_value != null)
&& (property_value.contains(property.seek_value))) {
found_props++;
}
}
if (found_props >= MIN_PROPERTIES_THRESHOLD) {
log("Check QEmuProps is detected");
return true;
}
return false;
}
private boolean checkIp() {
boolean ipDetected = false;
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.INTERNET)
== PackageManager.PERMISSION_GRANTED) {
String[] args = {"/system/bin/netcfg"};
StringBuilder stringBuilder = new StringBuilder();
try {
ProcessBuilder builder = new ProcessBuilder(args);
builder.directory(new File("/system/bin/"));
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
stringBuilder.append(new String(re));
}
in.close();
} catch (Exception ex) {
// empty catch
}
String netData = stringBuilder.toString();
log("netcfg data -> " + netData);
if (!TextUtils.isEmpty(netData)) {
String[] array = netData.split("\n");
for (String lan :
array) {
if ((lan.contains("wlan0") || lan.contains("tunl0") || lan.contains("eth0"))
&& lan.contains(IP)) {
ipDetected = true;
log("Check IP is detected");
break;
}
}
}
}
return ipDetected;
}
private String getProp(Context context, String property) {
try {
ClassLoader classLoader = context.getClassLoader();
Class<?> systemProperties = classLoader.loadClass("android.os.SystemProperties");
Method get = systemProperties.getMethod("get", String.class);
Object[] params = new Object[1];
params[0] = property;
return (String) get.invoke(systemProperties, params);
} catch (Exception exception) {
// empty catch
}
return null;
}
private boolean isSupportTelePhony() {
PackageManager packageManager = mContext.getPackageManager();
boolean isSupport = packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
log("Supported TelePhony: " + isSupport);
return isSupport;
}
private void log(String str) {
if (this.isDebug) {
Log.d(getClass().getName(), str);
}
}
public static String getDeviceInfo() {
return "Build.PRODUCT: " + Build.PRODUCT + "\n" +
"Build.MANUFACTURER: " + Build.MANUFACTURER + "\n" +
"Build.BRAND: " + Build.BRAND + "\n" +
"Build.DEVICE: " + Build.DEVICE + "\n" +
"Build.MODEL: " + Build.MODEL + "\n" +
"Build.HARDWARE: " + Build.HARDWARE + "\n" +
"Build.FINGERPRINT: " + Build.FINGERPRINT;
}
}