Slide 1

Slide 1 text

Adit Lal GDE Android Meta-Programming in Kotlin for Enhanced Android Security Secure Code, Simplified Travv World

Slide 2

Slide 2 text

⾠ MANDATORY LEGAL WARNING ⾠ Information acquired here is solely for educational purposes: - Refrain from testing on unauthorized apps. - Seek security advice from knowledgeable sources within your company. - This presentation is not af fi liated with or endorsed by the OWASP Foundation or my employer.

Slide 3

Slide 3 text

Why is the security of apps so important?

Slide 4

Slide 4 text

"Crafting a reputation is a time-intensive process, yet a mere moment in a cyber incident has the potential to shatter it." Why is the security of apps so important?

Slide 5

Slide 5 text

MSTG The Mobile Security Testing Guide is a comprehensive manual that describes technical processes for verifying the controls listed in the MASVS OWASP The Open Web Application Security Project® (OWASP) is a nonprofit foundation that works to improve the security of software MASVS Mobile App Security Requirements and Verification is a standard that establishes security requirements for testing mobile app security SOP

Slide 6

Slide 6 text

What do developers do for app security?

Slide 7

Slide 7 text

Mobile App Security Requirements and Verification MASVS Architecture, Design and Threat Modeling Cryptography Requirements Data storage and privacy Authentication and session management Network communication requirements Platform interaction Code quality and build settings

Slide 8

Slide 8 text

Testing Guide To begin - Divide into 2 categories Static Analysis Dynamic Analysis

Slide 9

Slide 9 text

Testing Guide Static Analysis ● Understand working of a binary without running it ● Assets analysis ○ Application entry points ○ Hard coded secrets ○ External libraries ● Code analysis (reverse engineering) ○ Retrieve the logic of the application ○ Identify weakness (cryptographic protocols, …) ● Tools : Jadx, IDA pro, Gridra Dynamic Analysis

Slide 10

Slide 10 text

Dynamic Analysis Testing Guide ● Understand the working of a binary by executing it. ● On a (rooted) phone, verify app by checking exploits using ○ Data storage ○ Storage ○ Logcat ● Traffic analysis ○ Man In The Middle attack ● Code instrumentation ○ Methods hooking ● Tools: Debuggers, FRIDA, EdXposed

Slide 11

Slide 11 text

Let’s recap anatomy of APK Anatomy of Android apps

Slide 12

Slide 12 text

DEX compiler source code .java Kotlin compiler source code .kt .kts Java bytecode .class .jar Java compiler source code .c .cpp Dalvik bytecode .dex Machine code .so application.apk Anatomy of Android apps

Slide 13

Slide 13 text

application.zip output → Android Application Package (APK) application.apk Anatomy of Android apps

Slide 14

Slide 14 text

$ unzip -d application application.apk
 $ ls application/
 AndroidManifest.xml
 META-INF/
 classes.dex, classes2.dex, ...
 lib/
 assets/
 ... My input! Dalvik bytecode Entry point [Read it first] Native libraries Read-only data application.apk Anatomy of Android apps

Slide 15

Slide 15 text

$ unzip -d application application.apk
 $ ls application/
 AndroidManifest.xml
 META-INF/
 classes.dex, classes2.dex, ...
 lib/
 assets/
 ... My input! Dalvik bytecode Entry point [Read it first] Native libraries Read-only data application.apk Anatomy of Android apps

Slide 16

Slide 16 text

Static Analysis

Slide 17

Slide 17 text

Static Analysis - Manifest Usually, the first thing that a penetration tester will check on an engagement. We will get additional information on - Permissions - android:protectionLevel attribute - Manifest attributes - Debuggable - CleartextTraffic - Network security configuration - Backup and restore - Exported android components - Intent filters Tools: Jadx Jeb APKTool Static Analysis

Slide 18

Slide 18 text

Static Analysis - Reverse Engineering Design Code Compilation Output Software engineering “square an integer” int square(int num) { return num * num;} mov DWORD PTR [rbp-4], edi mov eax, DWORD PTR [rbp-4] imul eax, eax 89 7d fc 8b 45 fc 0f af c0 Static Analysis

Slide 19

Slide 19 text

Static Analysis - Reverse Engineering Design Code Compilation Output Software engineering Reverse engineering “square an integer” int square(int num) { return num * num;} mov DWORD PTR [rbp-4], edi mov eax, DWORD PTR [rbp-4] imul eax, eax 89 7d fc 8b 45 fc 0f af c0 Static Analysis

Slide 20

Slide 20 text

Static Analysis - Reverse Engineering Tools: Jadx Jeb APKTool Static Analysis

Slide 21

Slide 21 text

Dynamic Analysis

Slide 22

Slide 22 text

Dynamic Analysis — MITM Dynamic Analysis Tools: Burp Suite mitmproxy HTTP-Toolkit / Charles

Slide 23

Slide 23 text

Tools: Charles Dynamic Analysis — MITM Dynamic Analysis

Slide 24

Slide 24 text

- Storage monitoring - Observing changes on the file system while using the application. - Files created post log-in - Storing of passwords/tokens in Shared Pref file. - Files created/changed on internal storage - /data/data//…. - Files created/changed on external storage - /sdcard/… - Keys created in Keystore Dynamic Analysis

Slide 25

Slide 25 text

- Behaviour analysis - Observing behavioural changes for various interaction with the app Application running on a rooted device may behave differently than on a non-rooted device - Observing error information Gives out small pieces of information (eg) login messages Dynamic Analysis

Slide 26

Slide 26 text

- Code Instrumentation Java.perform(function() { / / - - - - - Location - - - - - / / https: / / developer.android.com/reference/android/location/Location var location = Java.use('android.location.Location'); / / public double getLatitude () var location_getLatitude = location.getLatitude.overload(); location_getLatitude.implementation = function() { var latitude = location_getLatitude.call(this); console.log("[+] Location.getLatitude() : " + latitude); return latitude; / / var modif i ed_latitude = -75.09978; / / return modif i ed_latitude; } / / public double getLongitude () var location_getLongitude = location.getLongitude.overload(); location_getLongitude.implementation = function() { var longitude = location_getLongitude.call(this); console.log("[+] Location.getLongitude() : " + longitude); return longitude; / / var modif i ed_longitude = -123.332196; / / return modif i ed_longitude; } }); Dynamic Analysis

Slide 27

Slide 27 text

Action - Reaction - Prevention Improper Can you spot the mistake? How to secure your android application

Slide 28

Slide 28 text

Catch vulnerabilities at compile time. Improper Can you spot the mistake? Compiler Plugins for Security

Slide 29

Slide 29 text

Improper Can you spot the mistake? Hardcoded API Key Detection

Slide 30

Slide 30 text

Improper Can you spot the mistake? Hardcoded API Key Detection package tech.savvy.kdroid.secure /** * Annotation to fl ag hardcoded secrets or sensitive information in code. * @property message A custom message explaining why the element is fl agged. */ @Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.SOURCE) annotation class HardcodedSecret( val message: String = "Potential hardcoded secret detected" )

Slide 31

Slide 31 text

Improper Can you spot the mistake? Hardcoded API Key Detection class SecurityProcessor( private val codeGenerator: CodeGenerator, private val logger: KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver) : List { val symbols = resolver.getSymbolsWithAnnotation("tech.savvy.kdroid.secure.HardcodedSecret") symbols.forEach { symbol - > if (symbol is KSPropertyDeclaration) { logger.warn("Potential security issue detected: $ {symbol.simpleName.asString()}") } } return emptyList() } }

Slide 32

Slide 32 text

Improper Can you spot the mistake? Hardcoded API Key Detection class SecurityProcessorProvider : SymbolProcessorProvider { override fun create(environment: SymbolProcessorEnvironment) : SymbolProcessor { return SecurityProcessor(environment.codeGenerator, environment.logger) } } plugins { kotlin("jvm") id("com.google.devtools.ksp") } dependencies { implementation("com.google.devtools.ksp:symbol - processing - api : < latest - version>") }

Slide 33

Slide 33 text

Improper Can you spot the mistake? Hardcoded API Key Detection package com.example.app import tech.savvy.kdroid.secure.HardcodedSecret class Example { @HardcodedSecret("This API key should not be hardcoded") val apiKey = "AIza1234567890abcdef" / / Hardcoded API key for demo }

Slide 34

Slide 34 text

Improper Can you spot the mistake? Hardcoded API Key Detection @HardcodedKey(type = "Encryption", description = "Migrate to SecureStorage") val aesKey = "this - is - a - hardcoded - aes - key" / / Processor generates: object SecureConf i g { fun getAesKey() : String { return SecureStorage.get("AES_KEY") } }

Slide 35

Slide 35 text

Improper Can you spot the mistake? Hardcoded API Key Detection SecureStorage.putString("TOKEN", encrypt("my - token")) val token = decrypt(SecureStorage.getString("TOKEN"))

Slide 36

Slide 36 text

Secure Communication Enforce How to secure your android application

Slide 37

Slide 37 text

Enforce secure communication - Show an app chooser. If an implicit intent can launch at least two possible apps on a user's device, explicitly show an app chooser. This interaction strategy allows users to transfer sensitive information to an app that they trust. Intent intent = new Intent(Intent.ACTION_SEND); String title = getResources().getString(R.string.chooser_title); Intent chooser = Intent.createChooser(intent, title); try { startActivity(chooser); } catch (ActivityNotFoundException e) { / / Def i ne what your app should do if no activity can handle the intent. } Secure Communication Enforce

Slide 38

Slide 38 text

Enforce secure communication - Use Intents for IPC To send data to a specific component of an app, you must create a new instance of the Intent class and use its setComponent() method to specify both the package name of the app and the name of the component. You can then add data to it using the putExtra() method. Intent intent = new Intent(); / / Specify the component name intent.setComponent(new ComponentName("my.other.app","my.other.app.MyActivity")); / / Add data intent.putExtra("DATA", "Hello World!"); / / Send the intent to the activity startActivity(intent); Secure Communication Enforce

Slide 39

Slide 39 text

Enforce secure communication - Use Intents for IPC To send data to multiple apps at once, you can send the intent as a broadcast using the sendBroadcast() method → can be intercepted. ● Use a custom permission whose protectionLevel is set to signature ● https://developer.android.com/guide/topics/manifest/permission- element#plevel / / Create an intent Intent intent = new Intent(); / / Add data intent.putExtra("DATA", "Hello World"); / / Specify an action name for / / the receiver's intent - f i lter intent.setAction("my.app.receive"); / / Send as a broadcast using a custom permission sendBroadcast(intent, "my.custom.permission"); Secure Communication Enforce

Slide 40

Slide 40 text

Enforce secure communication - Lint class IntentUsageProcessor( private val logger: KSPLogger ) : SymbolProcessor { override fun process(resolver: Resolver) : List { val symbols = resolver.getAllFiles() / / Analyze all f i les symbols.forEach { f i le - > f i le.declarations.forEach { declaration - > if (declaration is KSFunctionDeclaration) { analyzeFunction(declaration) } } } return emptyList() } … } Secure Communication Enforce

Slide 41

Slide 41 text

Enforce secure communication - Lint private fun analyzeFunction(function: KSFunctionDeclaration) { function.body ? . statements ? . forEach { statement - > if (statement.toString().contains("Intent(")) { / / Detect Intent usage val isImplicit = statement.toString().contains("Intent.ACTION_") & & !statement.toString().contains("component") if (isImplicit) { logger.warn( "Implicit Intent detected in function $ {function.simpleName.asString()} " + "at line ${function.location}", function ) } } } } Secure Communication Enforce

Slide 42

Slide 42 text

Enforce secure communication - Lint class CustomIntentDetector : Detector(), SourceCodeScanner { / / Specify which methods to analyze override fun getApplicableMethodNames() = listOf("Intent") / / Analyze the method calls override fun visitMethodCall(context, node, method) { if (method.name = = "Intent" & & !node.arguments.contains("component")) { / / Report issue for implicit intents context.report( ISSUE, node, "Implicit Intent detected: Specify a target component." ) } } companion object { / / Def i ne the lint issue val ISSUE = Issue.create( Secure Communication Enforce

Slide 43

Slide 43 text

if (method.name = = "Intent" & & !node.arguments.contains("component")) { / / Report issue for implicit intents context.report( ISSUE, node, "Implicit Intent detected: Specify a target component." ) } } companion object { / / Def i ne the lint issue val ISSUE = Issue.create( id = "ImplicitIntent", description = "Implicit Intent Detected", explanation = "Implicit intents can cause security risks. " + "Always use explicit intents.", category = SECURITY, priority = 6, severity = WARNING ) } } Enforce secure communication - Lint Secure Communication Enforce

Slide 44

Slide 44 text

Enforce secure communication - Network Security ● No cleartext communication by default starting Android 7.1 android:usesCleartextTraff i c=false / / by default! ● Pin certificates (res/xml/network_security_config.xml) example.com < / domain> 7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y= < / pin> < / pin - set> < / domain - conf i g> < / network - security - conf i g> Secure Communication Enforce

Slide 45

Slide 45 text

Improper Platform Usage Can you spot the mistake? How to secure your android application https://github.com/ajinabraham/Mobile-Security-Framework-MobSF

Slide 46

Slide 46 text

< / intent - f i lter> < / activity> < / application> Can you spot the mistake?

Slide 47

Slide 47 text

< / intent - f i lter> < / activity> < / application> How is this exploited?

Slide 48

Slide 48 text

Use a tool like ‘drozer’ to scan app for vulnerable activities, broadcast receivers and content providers How is this exploited? github.com/FSecureLABS/drozer •Run ADB to exploit

Slide 49

Slide 49 text

< / intent - f i lter> < / activity> < / application> Fixing the exploit

Slide 50

Slide 50 text

•‘Tap-jacking’ vulnerability •Apps can draw over other apps and monitor their contents •They can also pass spoofed touch events android:f i lterTouchesWhenObscured=“true" •Combined, this can be used maliciously to trick users into entering passwords, accepting permissions, etc •Permission required for these apps, but only recently Other exploits?

Slide 51

Slide 51 text

Permissions

Slide 52

Slide 52 text

Permissions Best practices: Control | Transparency | Data minimization ● Request a minimal number of permissions ● Associate runtime permissions with specific actions ● Consider your app's dependencies ● Be transparent ● Make system accesses explicit Android Permissions Samples Repository ● https://github.com/android/permissions-samples

Slide 53

Slide 53 text

Action - Reaction - Prevention Storing User Data

Slide 54

Slide 54 text

Storing User Data - Android application sandbox ● Applications are isolated into their own space (user-based protection) ○ Prefer using internal storage, it prevents other apps from accessing it. ● Backup (true by default) ○ https://developer.android.com/guide/topics/data/autobackup ○ Cloud backups / Device-to-device (D2D) transfers backup_rules.xml 👉 < / full - backup - content> Storing User Data

Slide 55

Slide 55 text

Storing User Data - File Encryption ● Storing data on the external storage should be encrypted https://developer.android.com/guide/topics/security/ cryptography ● Recommended algorithms ‣Cipher - AES in either CBC or GCM mode with 256-bit keys ‣MessageDigest - SHA-2 family ‣Mac - SHA-2 family HMAC ‣Signature - SHA-2 family with ECDSA

Slide 56

Slide 56 text

Storing User Data - Encrypted Key Value Storage SharedPreferences is a simple key-value storage ● In the application private directory ● Not encrypted EncryptedSharedPreferences ● Store data encrypted in your SharedPreference ● Key Store handled everything in the background val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) val encryptedSharedPreferences = EncryptedSharedPreferences.create( "secret_shared_prefs", masterKeyAlias, context, PrefKeyEncryptionScheme.AES256_SIV, PrefValueEncryptionScheme.AES256_GCM) / / use the shared preferences and editor as you normally would SharedPreferences.Editor editor = sharedPreferences.edit();

Slide 57

Slide 57 text

Common App security risks

Slide 58

Slide 58 text

Common App security risks - Third party frameworks 3rd party SDKs or frameworks can be a huge security risk for your applications. Since they get compiled with your app and run in the same sandbox they have the same rights as your app. Be aware that your application is not secure if any of external libraries is not secure. Common App security risks

Slide 59

Slide 59 text

Common App security risks - Analytics Less is more: especially in combination with third party analytics SDKs You should be careful which information you want to collect. Analytics data can often already be enough to identify users or to be able to access their data. Common App security risks

Slide 60

Slide 60 text

Common App security risks - Loggings The problem arises if the logged stuff contains username, password or other sensitive information. Remove log statements in release builds! proguard - rules.pro - assumenosideeffects class android.util.Log { public static boolean isLoggable(java.lang.String, int); public static int v( . . . ); public static int d( . . . ); public static int i( . . . ); } Common App security risks

Slide 61

Slide 61 text

Android Security Security by Design on Play Academy ● Learn about best practices for encryption, integrity, and the overall app security lifecycle. Shortlink → goo.gle/androidsecuritycourse Scan Me!

Slide 62

Slide 62 text

Android Security https://github.com/jeremylong/DependencyCheck https://github.com/tanprathan/MobileApp-Pentest-Cheatsheet https://github.com/ashishb/android-security-awesome https://github.com/talsec/Free-RASP-Android https://github.com/tjunxiang92/Android-Vulnerabilities https://github.com/LegacyCodeHQ/eureka https://github.com/novacrypto/SecureString https://developer.android.com/topic/security/best-practices

Slide 63

Slide 63 text

Thats all folks! https:/ /linktr.ee/aldefy Travv World https://www.travv.world/ Slides : https://bit.ly/4h4KnTT