APPLICATION SECURITY
(Threats and Malpractices)
Speaker: Dimitrios Valsamaras | @Ch0pin
Common Malpractices
Webviews
Usage: WebView objects allow the developers to deliver web-based content to the user as a part of an activity’s layout.
Webviews - Basic Usage
Webviews - Enabling Javascript
Webviews - Enabling File Access
? ?
Webviews - Bridging Java with Javascript
function showAndroidToast(toast) {
AndroidBridge.showToast(“Hello from Java Script”);
}
Javascript:
Webviews - Malpractices
Intentional
Webviews - Malpractices
When misused WebViews impose a great risk for the user !!
Common Malpractices:
WebViews - Javascript Injection
Using the onPageStarted, onPageFinished callbacks or the onProgessChanged (less common) we may track the loading process of a web page and append arbitrary code.
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
INJECTION POINT
}
});
WebView - Javascript Injection - Cookies
WebViews - Javascript Injection - Auto Clicks
WebView - Javascript Injection - Scrapping
WebView - Javascript Scraping
WebView - Backdooring
WebView - Silent Loading
By changing the visibility or the size of the webview all the operations can take place without being perceived by the user:
myWebView.setVisibility(myWebView.GONE)
Webviews - Malpractices
UnIntentional
WebView - Hijacking
am start -n com.training.webviews/.MainActivity --es url https://www.example.com
am start -n com.training.webviews/.MainActivity --es url "javascript:AndroidBridge.execCmd('ls -al')"
Get the code https://github.com/Ch0pin/WebViews
Common Malpractices
Free Floating Windows
Definition: A free floating window is a category of windows that can appear freely above any other applications while its existence doesn’t depend on its parent. Additionally, its behaviour and appearance is fully customisable and controllable by the developer.
Not to be confused with Picture in Picture (e.g. Google Maps, youtube e.t.c.)
Free Floating Windows
Some Features
FFW Implementation
FFW Implementation (creating a floating button)
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Button floatingButton = new Button(getApplicationContext());
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
FFW Implementation (creating a floating button - continued )
windowManager.addView(floatingButton, params);
FFW
FFW Implementation (creating a floating button - continued )
<activity android:name=".MainActivity" android:autoRemoveFromRecents="true" android:noHistory="true">
FFW What is wrong with that ?
Background Restrictions (Android 8.0):
More restrictions ...
FFW What is wrong with that ?
Exceptions to the restriction
Apps running on Android 10 or higher can start activities only when one or more of the following conditions are met:
Can it be invisible ?
btn.setAlpha(0);
FFW Abuse
From Free Floating Windows to Free Popping Windows
SPAM
Ransomware
FFW Abuse, TapJacking
Creating more than a simple View...
LayoutInflater layoutInflater = getLayoutInflater();
View mLayout = layoutInflater.inflate(R.layout.tapjacking_dialog,null);
windowManager.addView(mLayout,params);
FFW Abuse, TapJacking
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(1200,1200,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
Click through:
Send touches to the window behind
Picture In Picture
Definition: PiP is a special type of multi-window mode mostly used for video playback. It lets the user watch a video in a small window pinned to a corner of the screen while navigating between apps or browsing content on the main screen.
Popular Applications that use PiP:
Picture In Picture - Implementation
No special permissions !!
<activity android:name="VideoActivity"
android:supportsPictureInPicture="true"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation"
public void onActionClicked(Action action) {
if (action.getId() == R.id.lb_control_picture_in_picture) {
getActivity().enterPictureInPictureMode();
return;
}
...
}
Add the specific entry in the AndroidManifest
Call the enterPictureInPictureMode
Picture In Picture
<activity android:name=".MainActivity"
android:supportsPictureInPicture="true"
android:theme="@style/Theme.AppCompat">
<layout android:defaultHeight="1dp"
android:defaultWidth="1dp"
android:gravity="top|end"
android:minHeight="1dp"
android:minWidth="1dp" />
Common Malpractices
Accessibility Service
The accessibility service provides user interface enhancements to assist users with disabilities, or who may temporarily be unable to fully interact with a device. For example, users who are driving, taking care of a young child might need additional or alternative interface feedback.
? ?
A powerful set of API calls, used by many popular apps including Google Assistant, Google maps, password managers, app lockers
but also from ….
Trojans, backdoors, bots, phishing apps e.t.c.
Accessibility Service from a security perspective
An application for which the accessibility service has been granted can run in the background and…
… Pretty much, it can act in behalf of the user
Accessibility Service - How to enable (Android 10)
Settings → Accessibility →
Click on the app →
Use Service→ Allow
Accessibility Service - Implementation
Implementation Class
Intent filter
Permission
Configuration
AccessibilityService_accessibilityEventTypes: The event types this service would like to receive as specified in AccessibilityEvent. This setting can be changed at runtime by calling
Accessibility Service - Java Code Implementation
Override Required
Class name declared in the Manifest
Accessibility Service - Accessibility Event
Accessibility Service - View Hierarchy Example
Accessibility Service - Event Lifecycle
UI changed
Match ?
Ignore
No
Yes
Trigger Callback
Accessibility Service - Abuse
Accessibility Service - Abuse
Accessibility Service - Abuse, Overlays
Monitoring the API calls performed by the accessibility service implementation.
Accessibility Service - Abuse
Accessibility Service - Abuse
When correctly coordinated it can perform chain of actions to automate more complex tasks (e.g. screen recording)
Common Malpractices
Device Admin
Definition: The Device Administration API provides device administration features at the system level. These APIs allow you to create security-aware apps that are useful in enterprise settings, in which IT professionals require rich control over employee devices.
Device Admin
Device Admin - Implementation
DeviceAdminReceiver subclass
Permission
Filter
Device Admin - Callbacks
Permission
Common Malpractices
Java Reflection
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
Figure from: https://techvidvan.com/tutorials/reflection-in-java/
Java Reflection
Java Reflection
The Test class users reflection to get the ReflectionDemo class characteristics and invoke its defined methods.
Java Reflection - Misuse
Example: “java.lang.Runtime” , “1”
Can be used to “hide” suspicious API calls
[PGP.]P_V.cD_EX\T
Return
Class cls = Class.forName(decrypt(“[PGP.]P_V.cD_EX\T”));
Common Malpractices
Dynamic Code Loading - DCL
DCL(Dynamic code loading) allows an application to load code that is not part of its static, initial codebase. The additional code can be retrieved from a remote location and executed at runtime.
Dynamic Code Loading - Implementation
DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent)
dexPath | String: the list of jar/apk files containing classes and resources, delimited by File.pathSeparator, which defaults to ":" on Android |
optimizedDirectory | String: this parameter is deprecated and has no effect since API level 26. |
librarySearchPath | String: the list of directories containing native libraries, delimited by File.pathSeparator; may be null |
parent | ClassLoader: the parent class loader |
Dynamic Code Loading - Implementation
Fetch the dex, jar, apk e.t.c
String dexPath = context.getFilesDir().getAbsolutePath() + “/” +"dexPath.dex";
Final DexClassLoader nClazz = new DexClassLoader(dexPath,mContext.getCodeCacheDir().getAbsolutePath(), null,getClass().getClassLoader()).loadClass(clazz);
DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent)
Dynamic Code Loading - what is wrong with this ?
Dynamic Code Loading - what is wrong with this ?
References
https://ink.library.smu.edu.sg/cgi/viewcontent.cgi?article=4724&context=sis_research
https://valsamaras.medium.com/tapjacking-attacks-a-thorough-guide-2cd6486d0fc9
https://www.tutorialspoint.com/android/android_user_interface_layouts.htm
https://developer.android.com/reference/android/webkit/WebView
https://developer.android.com/guide/topics/ui/accessibility/service
https://www.javatpoint.com/java-reflection
https://kalpeshchandora12.medium.com/dynamic-code-loading-in-android-dea83ba3bc85