-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTalsec.cs
More file actions
196 lines (178 loc) · 7.7 KB
/
Talsec.cs
File metadata and controls
196 lines (178 loc) · 7.7 KB
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
/// <summary>
/// Unity plugin for Talsec freeRASP security SDK, providing runtime application self-protection
/// against various threats on Android and iOS platforms.
/// </summary>
public class TalsecPlugin : MonoBehaviour
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void _initTalsec(string[] appBundleIds, int appBundleIdsCount, string appTeamId, string watcherMailAddress, bool isProd);
#endif
private readonly string ControllerName = "com.unity.free.rasp.Controller";
// Singleton instance
private static TalsecPlugin _instance;
private ThreatDetectedCallback threatDetectedCallback;
private AndroidJavaObject javaControllerObject;
// Public accessor for the instance
public static TalsecPlugin Instance
{
get
{
if (_instance == null)
{
GameObject go = new GameObject("TalsecPlugin");
_instance = go.AddComponent<TalsecPlugin>();
DontDestroyOnLoad(go);
}
return _instance;
}
}
// Initialize the plugin
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else if (_instance != this)
{
Destroy(gameObject);
}
}
/// <summary>
/// Single point of entry for initializing Talsec SDK using TalsecConfig
/// </summary>
/// <param name="config">Complete Talsec configuration containing all platform-specific and common settings</param>
public void initTalsec(TalsecConfig config)
{
if (config == null)
{
throw new System.ArgumentNullException("config", "TalsecConfig cannot be null");
}
RuntimePlatform currentPlatform = Application.platform;
if (currentPlatform == RuntimePlatform.Android)
{
// this will throw an exception if the configuration is not valid
initAndroidTalsec(config.androidConfig, config.watcherMailAddress, config.isProd);
return;
}
else if (currentPlatform == RuntimePlatform.IPhonePlayer)
{
// this will throw an exception if the configuration is not valid
initiOSTalsec(config.iosConfig, config.watcherMailAddress, config.isProd);
return;
}
Debug.LogWarning($"Talsec initialization skipped: Platform {currentPlatform} is not supported. Only Android and iOS are supported.");
}
public void stopTalsec() {
RuntimePlatform currentPlatform = Application.platform;
if (currentPlatform == RuntimePlatform.Android)
{
if (javaControllerObject != null)
{
javaControllerObject.Call("stopTalsec");
}
}
}
private void initAndroidTalsec(AndroidConfig androidConfig, string watcherMailAddress, bool isProd)
{
// Get the current activity
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
javaControllerObject = new AndroidJavaObject(ControllerName);
// Register this GameObject to receive callbacks
// We pass the name of this GameObject and the callback method names
javaControllerObject.Call("setUnityGameObjectCallback", gameObject.name);
// Call a method with parameters
javaControllerObject.Call("initializeTalsec", currentActivity, androidConfig.packageName, androidConfig.signingCertificateHashBase64, androidConfig.supportedAlternativeStores, watcherMailAddress, isProd);
}
}
private void initiOSTalsec(IOSConfig iosConfig, string watcherMailAddress, bool isProd)
{
#if UNITY_IOS && !UNITY_EDITOR // without this there will be compilation error
_initTalsec(iosConfig.appBundleIds, iosConfig.appBundleIds.Length, iosConfig.appTeamId, watcherMailAddress, isProd);
#endif
}
public void setThreatDetectedCallback(ThreatDetectedCallback callback) {
this.threatDetectedCallback = callback;
}
// This method will be called from the native side of the code
// both iOS & Android will use this method
// hence all the threat types for both platforms are handled here
private void scanResult(string talsecScanResultCallback)
{
if(this.threatDetectedCallback != null) {
switch(talsecScanResultCallback) {
case "onAppIntegrity":
this.threatDetectedCallback.onAppIntegrity();
break;
case "onPrivilegedAccess":
this.threatDetectedCallback.onPrivilegedAccess();
break;
case "onDebug":
this.threatDetectedCallback.onDebug();
break;
case "onObfuscationIssues":
this.threatDetectedCallback.onObfuscationIssues();
break;
case "onRuntimeManipulation":
this.threatDetectedCallback.onHooks();
break;
case "onPasscode":
this.threatDetectedCallback.onPasscode();
break;
case "onPasscodeChange":
this.threatDetectedCallback.onPasscodeChange();
break;
case "onSimulator":
this.threatDetectedCallback.onSimulator();
break;
case "onSecureHardwareNotAvailable":
this.threatDetectedCallback.onSecureHardwareNotAvailable();
break;
case "onDeviceBinding":
this.threatDetectedCallback.onDeviceBinding();
break;
case "onDeviceID":
this.threatDetectedCallback.onDeviceID();
break;
case "onUnofficialStore":
this.threatDetectedCallback.onUnofficialStore();
break;
case "onSystemVPN":
this.threatDetectedCallback.onSystemVPN();
break;
case "onScreenshot":
this.threatDetectedCallback.onScreenshot();
break;
case "onScreenRecording":
this.threatDetectedCallback.onScreenRecording();
break;
case "onDevMode":
this.threatDetectedCallback.onDevMode();
break;
case "onADBEnabled":
this.threatDetectedCallback.onADBEnabled();
break;
case "onMultiInstance":
this.threatDetectedCallback.onMultiInstance();
break;
case "onUnsecureWiFi":
this.threatDetectedCallback.onUnsecureWiFi();
break;
case "onTimeSpoofing":
this.threatDetectedCallback.onTimeSpoofing();
break;
case "onLocationSpoofing":
this.threatDetectedCallback.onLocationSpoofing();
break;
}
}
}
}