Connect with our team of offensive security, AI security and pen testing experts at Black Hat Europe 2023. Learn More

Connect with our team of offensive security, AI security and pen testing experts at Black Hat Europe 2023. Learn More

Services
Capture
Managed Detection & Response

Eradicate cyberthreats with world-class intel and expertise

twi-cloud-lock-color-svg
Managed Security Services

Expand your team’s capabilities and strengthen your security posture

twi-briefcase-color-svg
Consulting & Professional Services

Tap into our global team of tenured cybersecurity specialists

twi-dashboard-color-svg
Penetration Testing

Subscription- or project-based testing, delivered by global experts

twi-database-color-svg
Database Security

Get ahead of database risk, protect data and exceed compliance requirements

twi-email-color-svg
Email Security & Management

Catch email threats others miss with layered security & maximum control

twi-managed-portal-color
Co-Managed SOC (SIEM)

Eliminate alert fatigue, focus your SecOps team, stop threats fast, and reduce cyber risk

Solutions
BY TOPIC
Microsoft Exchange Server Attacks
Stay protected against emerging threats
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
The Trustwave Approach
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Platform
SpiderLabs Fusion Center
Security Operations Centers
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats
SpiderLabs Blog

Custom Native Library Loader for Android

If you read my co-worker Neal Hindocha's recent post "Debugging Android Libraries using IDA" you notice he mentioned using a "custom library loader". We had used this on a recent mobile penetration test to have complete control over some home grown custom native libraries the target application was using.

The biggest problem we were facing with the test was that the library in question was being used for anit-rooting and anti-debug functionality to protect the app, and part of our job was to bypass this and patch it out. Of course, attaching directly to the running Android app to get at the code in this library was problematic, since most of the protections were likely loaded before we could attach.

What we needed was something like dllloader for Olly or Immunity that we could use to load the target .so file independently of the process, so we could have total control and access.

Here is what we came up with. While obviously not as feature rich as dllloader, this quick and dirty loader did the job:

 

#include <stdio.h>#include <stdlib.h>#include <dlfcn.h>int main(){      printf("Loading libs\n");     int (*pt2Function)(void) = NULL;                        //pointer to a void function - change this to match method sig     void* sdl_library = dlopen("/system/libtarget.so", RTLD_LAZY);     if (sdl_library == NULL) {     // report error ...     printf("Unable to load library\n");     char *errstr;  errstr = dlerror();  if (errstr != NULL)  printf ("A dynamic linking error occurred: (%s)\n", errstr); } else {  printf("Lib loaded, getting dlysm\n");     void* initializer = dlsym(sdl_library,"JNI_OnLoad");  if (initializer == NULL) {     // report error ...   printf("Unable to get address of JNI_OnLoad\n");   char *errstr;   errstr = dlerror();   if (errstr != NULL)   printf ("A dynamic linking error occurred: (%s)\n", errstr);  } else {      // cast initializer to its proper type and use      printf("calling get process\n");      //asm("BKPT #0");      pt2Function = initializer;      printf("got get process, setting up\n");            printf("Ok, lets Calling the function");      int result=pt2Function();      printf("Result of call is %d", result);        //asm("BKPT #0");  } }     return 0;}

As you can see, our target was the 'JNI_Onload' function. In our case, we targeted this because it was in this function that the anti-debug and system monitoring functionality was set up. We could have set up the function pointer to point to ANY of the .so's exported functions and called them.

This code could easily have been compiled on run on any standard linux system. We used dlopen() to dynamically load the library and dlsym() to obtain a pointer to the exported function we wanted to test. We could then call the target function directly and pass in any parameters (if any) the function took in order to follow code execution or even to fuzz, looking for exploits. The only ARM specific code we had (which it turns out we really didn't need) are the commented out ARM breakpoints.

To compile this, you simply use the Android NDK and create an ndk-specific Android.mk file:

 

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) # give module nameLOCAL_MODULE    := loader  # list your C files to compileLOCAL_SRC_FILES := test.c# Build executables instead of a library for android.include $(BUILD_EXECUTABLE)

You place the files in the normal Android NDK file structure - code and the Android.mk file, including the C code for the lib goes under the jni directory and your executable will appear under libs/armabi/ directory. To build, you open a terminal in the root of your project directory (same level as jni and libs) and execute the following command:

Mike-Park:android-loader mpark$ /path-to/android-ndk-r8e/ndk-build

In my case, I created a directory called 'android-loader' that contained jni/ and libs/ and ran from there.

After a few warnings, you should get a perfectly useable ARM executable in the libs/armabi/ directory called loader. You can then use adb push to place this in the /system directory (after a remount of course) along with your target .so and run it from there. It works great in the emulator and on the devices we tried.

Again, this is a quick and dirty bit of code that works. We used it for debugging with IDA, but it could easily be adapted for fuzzing and to take the library to load from the command line. I'd like to make it more robust, so any feedback or suggestions are welcome.

In the meantime, enjoy hacking android native code.

 

 

Latest SpiderLabs Blogs

The 2023 Retail Services Sector Threat Landscape: A Trustwave Threat Intelligence Briefing

The annual holiday shopping season is poised for a surge in spending, a fact well-known to retailers, consumers, and cybercriminals alike. The latter group, however, is poised to exploit any...

Read More

Pwning Electroencephalogram (EEG) Medical Devices by Default

Overall Analysis of Vulnerability Identification – Default Credentials Leading to Remote Code Execution During internal network testing, a document was discovered titled the “XL Security Site...

Read More

Hidden Data Exfiltration Using Time, Literally

I was looking at my watch last week and my attention was moved towards the seconds over at the right of the watch face, incrementing nicely along as you’d expect. Now, I don’t know if I’d just spent...

Read More