react-native-device-defense 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +236 -0
  3. package/android/build.gradle +90 -0
  4. package/android/proguard-rules.pro +28 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/cpp/CMakeLists.txt +45 -0
  7. package/android/src/main/cpp/device-security.cpp +314 -0
  8. package/android/src/main/java/vn/osp/security/DebugDetection.kt +131 -0
  9. package/android/src/main/java/vn/osp/security/DeviceSecurityModule.kt +277 -0
  10. package/android/src/main/java/vn/osp/security/DeviceSecurityPackage.kt +58 -0
  11. package/android/src/main/java/vn/osp/security/EmulatorDetection.kt +204 -0
  12. package/android/src/main/java/vn/osp/security/HookDetection.kt +270 -0
  13. package/android/src/main/java/vn/osp/security/NativeSecurityCheck.kt +66 -0
  14. package/android/src/main/java/vn/osp/security/RootDetection.kt +349 -0
  15. package/lib/commonjs/NativeDeviceSecurity.js +9 -0
  16. package/lib/commonjs/NativeDeviceSecurity.js.map +1 -0
  17. package/lib/commonjs/api.js +213 -0
  18. package/lib/commonjs/api.js.map +1 -0
  19. package/lib/commonjs/components/SecurityBlockedScreen.js +177 -0
  20. package/lib/commonjs/components/SecurityBlockedScreen.js.map +1 -0
  21. package/lib/commonjs/components/index.js +13 -0
  22. package/lib/commonjs/components/index.js.map +1 -0
  23. package/lib/commonjs/hooks/index.js +13 -0
  24. package/lib/commonjs/hooks/index.js.map +1 -0
  25. package/lib/commonjs/hooks/useDeviceSecurity.js +81 -0
  26. package/lib/commonjs/hooks/useDeviceSecurity.js.map +1 -0
  27. package/lib/commonjs/index.js +48 -0
  28. package/lib/commonjs/index.js.map +1 -0
  29. package/lib/commonjs/types.js +2 -0
  30. package/lib/commonjs/types.js.map +1 -0
  31. package/lib/module/NativeDeviceSecurity.js +3 -0
  32. package/lib/module/NativeDeviceSecurity.js.map +1 -0
  33. package/lib/module/api.js +206 -0
  34. package/lib/module/api.js.map +1 -0
  35. package/lib/module/components/SecurityBlockedScreen.js +169 -0
  36. package/lib/module/components/SecurityBlockedScreen.js.map +1 -0
  37. package/lib/module/components/index.js +2 -0
  38. package/lib/module/components/index.js.map +1 -0
  39. package/lib/module/hooks/index.js +2 -0
  40. package/lib/module/hooks/index.js.map +1 -0
  41. package/lib/module/hooks/useDeviceSecurity.js +73 -0
  42. package/lib/module/hooks/useDeviceSecurity.js.map +1 -0
  43. package/lib/module/index.js +21 -0
  44. package/lib/module/index.js.map +1 -0
  45. package/lib/module/types.js +2 -0
  46. package/lib/module/types.js.map +1 -0
  47. package/lib/typescript/NativeDeviceSecurity.d.ts +16 -0
  48. package/lib/typescript/NativeDeviceSecurity.d.ts.map +1 -0
  49. package/lib/typescript/api.d.ts +55 -0
  50. package/lib/typescript/api.d.ts.map +1 -0
  51. package/lib/typescript/components/SecurityBlockedScreen.d.ts +23 -0
  52. package/lib/typescript/components/SecurityBlockedScreen.d.ts.map +1 -0
  53. package/lib/typescript/components/index.d.ts +2 -0
  54. package/lib/typescript/components/index.d.ts.map +1 -0
  55. package/lib/typescript/hooks/index.d.ts +3 -0
  56. package/lib/typescript/hooks/index.d.ts.map +1 -0
  57. package/lib/typescript/hooks/useDeviceSecurity.d.ts +7 -0
  58. package/lib/typescript/hooks/useDeviceSecurity.d.ts.map +1 -0
  59. package/lib/typescript/index.d.ts +12 -0
  60. package/lib/typescript/index.d.ts.map +1 -0
  61. package/lib/typescript/types.d.ts +81 -0
  62. package/lib/typescript/types.d.ts.map +1 -0
  63. package/package.json +72 -0
  64. package/react-native-device-security.podspec +18 -0
  65. package/src/NativeDeviceSecurity.ts +33 -0
  66. package/src/api.ts +225 -0
  67. package/src/components/SecurityBlockedScreen.tsx +204 -0
  68. package/src/components/index.ts +1 -0
  69. package/src/hooks/index.ts +5 -0
  70. package/src/hooks/useDeviceSecurity.ts +91 -0
  71. package/src/index.ts +27 -0
  72. package/src/types.ts +95 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 OSP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,236 @@
1
+ # react-native-device-security
2
+
3
+ > Multi-layer device security detection for React Native - Root detection, Anti-hook, Anti-debug, Emulator detection
4
+
5
+ ## Features
6
+
7
+ - ✅ **Multi-layer Root Detection** - Detect rooted devices using multiple techniques
8
+ - 🔒 **Native C++ Detection** - Harder to bypass with JavaScript hooks
9
+ - 🎣 **Frida/Xposed Detection** - Detect common hooking frameworks
10
+ - 🐛 **Anti-Debug** - Detect debugger attachment
11
+ - 📱 **Emulator Detection** - Detect Android emulators
12
+ - 🛡️ **App Integrity Check** - Verify app signature and tampering
13
+ - 🔐 **Block on Security Threat** - Automatically block app when security issues detected
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install react-native-device-security
19
+ # or
20
+ yarn add react-native-device-security
21
+ ```
22
+
23
+ ## Android Setup
24
+
25
+ 1. Add to `android/settings.gradle`:
26
+
27
+ ```gradle
28
+ include ':react-native-device-security'
29
+ project(':react-native-device-security').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-security/android')
30
+ ```
31
+
32
+ 2. Add to `android/app/build.gradle`:
33
+
34
+ ```gradle
35
+ dependencies {
36
+ implementation project(':react-native-device-security')
37
+ }
38
+ ```
39
+
40
+ 3. Add to `MainApplication.java`:
41
+
42
+ ```java
43
+ import vn.osp.security.DeviceSecurityPackage;
44
+
45
+ @Override
46
+ protected List<ReactPackage> getPackages() {
47
+ return Arrays.<ReactPackage>asList(
48
+ // ... other packages
49
+ new DeviceSecurityPackage()
50
+ );
51
+ }
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ ### Basic Usage
57
+
58
+ ```typescript
59
+ import DeviceSecurity from 'react-native-device-security';
60
+
61
+ // Check if device is secure
62
+ const isSecure = await DeviceSecurity.isDeviceSecure();
63
+
64
+ if (!isSecure) {
65
+ // Device is rooted, has hooks, or other security issues
66
+ Alert.alert(
67
+ 'Security Warning',
68
+ 'This device is not secure. The app cannot run on rooted or modified devices.',
69
+ [{ text: 'OK', onPress: () => BackHandler.exitApp() }]
70
+ );
71
+ }
72
+ ```
73
+
74
+ ### Advanced Usage with Hook
75
+
76
+ ```typescript
77
+ import { useDeviceSecurity } from 'react-native-device-security';
78
+
79
+ function App() {
80
+ const { isSecure, securityStatus, isLoading } = useDeviceSecurity({
81
+ onSecurityThreat: (threat) => {
82
+ console.log('Security threat detected:', threat);
83
+ // Handle security threat - block app, show alert, etc.
84
+ },
85
+ blockOnThreat: true, // Block app when security threat detected
86
+ });
87
+
88
+ if (isLoading) {
89
+ return <LoadingScreen />;
90
+ }
91
+
92
+ if (!isSecure) {
93
+ return <SecurityBlockedScreen threats={securityStatus.threats} />;
94
+ }
95
+
96
+ return <MainApp />;
97
+ }
98
+ ```
99
+
100
+ ### Security Status Details
101
+
102
+ ```typescript
103
+ const status = await DeviceSecurity.getSecurityStatus();
104
+
105
+ console.log({
106
+ isSecure: status.isSecure,
107
+ isRooted: status.isRooted,
108
+ hasRootBeerDetected: status.hasRootBeerDetected,
109
+ hasNativeRootDetected: status.hasNativeRootDetected,
110
+ hasDangerousBins: status.hasDangerousBins,
111
+ hasRootApps: status.hasRootApps,
112
+ hasSystemPropsModified: status.hasSystemPropsModified,
113
+ hasFrida: status.hasFrida,
114
+ hasXposed: status.hasXposed,
115
+ hasMagisk: status.hasMagisk,
116
+ isDebuggable: status.isDebuggable,
117
+ isEmulator: status.isEmulator,
118
+ });
119
+ ```
120
+
121
+ ### Block on Security Threat (Recommended for Production)
122
+
123
+ ```typescript
124
+ import DeviceSecurity from 'react-native-device-security';
125
+
126
+ // In your app entry point
127
+ DeviceSecurity.blockOnSecurityThreat({
128
+ showAlert: true,
129
+ alertTitle: 'Cảnh báo bảo mật',
130
+ alertMessage: 'Thiết bị của bạn không an toàn. Ứng dụng không thể chạy trên thiết bị đã root hoặc có sửa đổi.',
131
+ onBlocked: () => {
132
+ // Optional callback when app is blocked
133
+ console.log('App blocked due to security threat');
134
+ }
135
+ });
136
+ ```
137
+
138
+ ## API Reference
139
+
140
+ ### Methods
141
+
142
+ | Method | Returns | Description |
143
+ |--------|---------|-------------|
144
+ | `isDeviceSecure()` | `Promise<boolean>` | Check if device is secure (no threats) |
145
+ | `getSecurityStatus()` | `Promise<SecurityStatus>` | Get detailed security status |
146
+ | `blockOnSecurityThreat(options)` | `void` | Block app when security threat detected |
147
+ | `isRooted()` | `boolean` | Check if device is rooted (synchronous) |
148
+ | `hasFrida()` | `boolean` | Check if Frida is present |
149
+ | `hasXposed()` | `boolean` | Check if Xposed framework is present |
150
+ | `hasMagisk()` | `boolean` | Check if Magisk is present |
151
+ | `isDebuggable()` | `boolean` | Check if app is debuggable |
152
+ | `isEmulator()` | `boolean` | Check if running on emulator |
153
+
154
+ ### Types
155
+
156
+ ```typescript
157
+ interface SecurityStatus {
158
+ isSecure: boolean;
159
+ threats: SecurityThreat[];
160
+ isRooted: boolean;
161
+ hasRootBeerDetected: boolean;
162
+ hasNativeRootDetected: boolean;
163
+ hasDangerousBins: boolean;
164
+ hasRootApps: boolean;
165
+ hasSystemPropsModified: boolean;
166
+ hasFrida: boolean;
167
+ hasXposed: boolean;
168
+ hasMagisk: boolean;
169
+ isDebuggable: boolean;
170
+ isEmulator: boolean;
171
+ }
172
+
173
+ type SecurityThreat =
174
+ | 'root_detected'
175
+ | 'frida_detected'
176
+ | 'xposed_detected'
177
+ | 'magisk_detected'
178
+ | 'debugger_detected'
179
+ | 'emulator_detected'
180
+ | 'system_props_modified';
181
+ ```
182
+
183
+ ## Configuration
184
+
185
+ ### ProGuard/R8 Rules
186
+
187
+ Add to `android/app/proguard-rules.pro`:
188
+
189
+ ```proguard
190
+ # Device Security Library
191
+ -keep class vn.osp.security.** { *; }
192
+ -keepclassmembers class vn.osp.security.** { *; }
193
+ -dontwarn vn.osp.security.**
194
+ ```
195
+
196
+ ## Security Techniques
197
+
198
+ ### Root Detection
199
+ - RootBeer library checks
200
+ - Native C++ checks (JNI)
201
+ - System properties (`ro.debuggable`, `ro.secure`)
202
+ - Dangerous binaries (`su`, `busybox`, `magisk`)
203
+ - Root management apps detection
204
+ - Mount point checks (`/system`, `/vendor` RW)
205
+
206
+ ### Hook Detection
207
+ - Frida port scanning (27042, 27043)
208
+ - Frida library detection
209
+ - Xposed framework detection
210
+ - Magisk module detection
211
+
212
+ ### Anti-Debug
213
+ - `Debug.isDebuggerConnected()` check
214
+ - Tracer PID in `/proc/self/status`
215
+ - Timing analysis
216
+
217
+ ### Emulator Detection
218
+ - Known emulator properties
219
+ - Generic device features
220
+ - Genymotion, Nox, BlueStacks detection
221
+
222
+ ## License
223
+
224
+ MIT
225
+
226
+ ## Author
227
+
228
+ OSP <dev@osp.vn>
229
+
230
+ ## Contributing
231
+
232
+ Pull requests are welcome!
233
+
234
+ ## Support
235
+
236
+ For issues and questions, please open a GitHub issue.
@@ -0,0 +1,90 @@
1
+ def safelyConfigureGetPackages() {
2
+ def packagesConfigured = false
3
+ gradle.afterProject { project ->
4
+ if (!packagesConfigured && project.hasProperty('android')) {
5
+ packagesConfigured = true
6
+ project.android.packagingOptions.jniLibs.useLegacyPackaging = false
7
+ }
8
+ }
9
+ }
10
+
11
+ safelyConfigureGetPackages()
12
+
13
+ buildscript {
14
+ ext.safeExtGet = { prop, fallback ->
15
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
16
+ }
17
+ repositories {
18
+ google()
19
+ mavenCentral()
20
+ }
21
+ dependencies {
22
+ classpath('com.android.tools.build:gradle:7.4.2')
23
+ }
24
+ }
25
+
26
+ apply plugin: 'com.android.library'
27
+ apply plugin: 'com.facebook.react'
28
+
29
+ android {
30
+ ndkVersion rootProject.ext.ndkVersion
31
+ compileSdkVersion safeExtGet('compileSdkVersion', 34)
32
+
33
+ namespace "vn.osp.security"
34
+
35
+ defaultConfig {
36
+ minSdkVersion safeExtGet('minSdkVersion', 21)
37
+ targetSdkVersion safeExtGet('targetSdkVersion', 34)
38
+ versionCode 1
39
+ versionName "1.0.0"
40
+
41
+ externalNativeBuild {
42
+ cmake {
43
+ cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
44
+ arguments "-DANDROID_STL=c++_shared",
45
+ "-DANDROID_ARM_NEON=TRUE"
46
+ }
47
+ }
48
+
49
+ ndk {
50
+ abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
51
+ }
52
+ }
53
+
54
+ buildFeatures {
55
+ buildConfig true
56
+ }
57
+
58
+ buildTypes {
59
+ release {
60
+ minifyEnabled false
61
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
62
+ }
63
+ debug {
64
+ minifyEnabled false
65
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
66
+ }
67
+ }
68
+
69
+ externalNativeBuild {
70
+ cmake {
71
+ path "src/main/cpp/CMakeLists.txt"
72
+ version "3.22.1"
73
+ }
74
+ }
75
+
76
+ compileOptions {
77
+ sourceCompatibility JavaVersion.VERSION_1_8
78
+ targetCompatibility JavaVersion.VERSION_1_8
79
+ }
80
+ }
81
+
82
+ dependencies {
83
+ implementation 'com.facebook.react:react-native:+'
84
+
85
+ // RootBeer for root detection
86
+ implementation 'com.scottyab:rootbeer:0.1.0'
87
+
88
+ testImplementation 'junit:junit:4.13.2'
89
+ testImplementation 'org.mockito:mockito-core:5.3.1'
90
+ }
@@ -0,0 +1,28 @@
1
+ # React Native Device Security - ProGuard/R8 Rules
2
+
3
+ # Keep all native methods
4
+ -keepclassmembers class vn.osp.security.** {
5
+ native <methods>;
6
+ }
7
+
8
+ # Keep the entire module
9
+ -keep class vn.osp.security.** { *; }
10
+ -keep interface vn.osp.security.** { *; }
11
+
12
+ # Keep enum classes
13
+ -keepclassmembers enum vn.osp.security.** {
14
+ *[];
15
+ }
16
+
17
+ # Don't warn about missing classes
18
+ -dontwarn vn.osp.security.**
19
+
20
+ # Obfuscate but keep critical method names for native calls
21
+ -keep class vn.osp.security.NativeSecurityCheck {
22
+ public static boolean isRooted();
23
+ public static boolean hasDangerousBinaries();
24
+ public static boolean hasSuspiciousSystemProperties();
25
+ public static boolean hasHookFramework();
26
+ public static boolean isDebuggerAttached();
27
+ public static java.lang.String getSecurityStatus();
28
+ }
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest package="vn.osp.security">
3
+ <!-- No permissions needed for security checks -->
4
+ </manifest>
@@ -0,0 +1,45 @@
1
+ cmake_minimum_required(VERSION 3.22.1)
2
+ project("device-security")
3
+
4
+ # Set C++ standard
5
+ set(CMAKE_CXX_STANDARD 17)
6
+ set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
+
8
+ # Define source files
9
+ set(SOURCE_FILES
10
+ device-security.cpp
11
+ )
12
+
13
+ # Build shared library
14
+ add_library(device-security SHARED ${SOURCE_FILES})
15
+
16
+ # Find required libraries
17
+ find_library(log-lib log)
18
+
19
+ # Link libraries
20
+ target_link_libraries(device-security
21
+ ${log-lib}
22
+ )
23
+
24
+ # Compiler flags for security and obfuscation
25
+ target_compile_options(device-security PRIVATE
26
+ -fvisibility=hidden
27
+ -fdata-sections
28
+ -ffunction-sections
29
+ -Wall
30
+ -Wextra
31
+ -O2
32
+ )
33
+
34
+ # Linker flags
35
+ target_link_options(device-security PRIVATE
36
+ -Wl,--gc-sections
37
+ -Wl,-s
38
+ )
39
+
40
+ # Strip symbols in release
41
+ if(CMAKE_BUILD_TYPE MATCHES Release)
42
+ add_custom_command(TARGET device-security POST_BUILD
43
+ COMMAND ${CMAKE_STRIP} --strip-unneeded $<TARGET_FILE:device-security>
44
+ )
45
+ endif()