Hunting Android Malware: A novel runtime technique for identifying malicious applications

Christopher Le Roy (Security Researcher · Heroku)

BruCON 0x0A · Day 2

Overview

In this BruCON talk, Christopher Le Roy, a Security Researcher at Heroku, presented a novel approach to identifying malicious Android applications at runtime. Titled "Hunting Android Malware: A novel runtime technique for identifying malicious applications," the presentation tackled the persistent and evolving challenge of Android malware detection, highlighting the limitations of traditional static analysis and the shortcomings of existing dynamic analysis methods. Le Roy's research introduces an on-device, runtime technique that leverages instrumentation to inspect the heap and hook critical methods, thereby revealing malicious behaviors that are often obscured from conventional detection mechanisms.

Watch on YouTube

Visual summary for Hunting Android Malware: A novel runtime technique for identifying malicious applications by Christopher Le Roy
Visual summary for Hunting Android Malware: A novel runtime technique for identifying malicious applications by Christopher Le Roy

Key moments

  1. 2:09 Developer's question: How to detect malware?
  2. 8:00 Novel idea: Analyzing Android app objects on the heap
  3. 14:00 Demo: Frida detects injected code and remote connections
  4. 16:00 Why static analysis misses runtime code injection
  5. 22:50 Demo: Hooking Runtime.exec to detect hidden shell commands
  6. 26:10 Introducing ATEK: On-device API for app analysis
  7. 31:50 Conclusions: Defense in depth and attack surface concerns

Hunting Android Malware: A novel runtime technique for identifying malicious applications

Speakers: Christopher Le Roy, Security Researcher, Heroku

Conference: BruCON

YouTube: https://www.youtube.com/watch?v=ar-Oh3865po

Overview

In this BruCON talk, Christopher Le Roy, a Security Researcher at Heroku, presented a novel approach to identifying malicious Android applications at runtime. Titled "Hunting Android Malware: A novel runtime technique for identifying malicious applications," the presentation tackled the persistent and evolving challenge of Android malware detection, highlighting the limitations of traditional static analysis and the shortcomings of existing dynamic analysis methods. Le Roy's research introduces an on-device, runtime technique that leverages instrumentation to inspect the heap and hook critical methods, thereby revealing malicious behaviors that are often obscured from conventional detection mechanisms.

The core problem addressed is the difficulty for developers and security professionals to reliably detect if an Android application has been backdoored or is exhibiting malicious activity directly on a running device. This talk proposes a solution centered around the fundamental idea that all application actions ultimately involve objects on the heap. By dynamically "carving" these objects and monitoring their instantiation and method calls, Le Roy demonstrated how to uncover stealthy tactics like dynamic code loading, remote command and control (C2) communications, and shell command execution, all in real-time on the device.

The significance of this research lies in its potential to augment existing Android security frameworks. While not presented as a silver bullet, the technique offers a crucial layer of defense-in-depth by providing a granular view into an application's actual runtime behavior. The development of ate_cake, a custom Android Frida library, further extends this capability by allowing Android applications to perform these sophisticated runtime analyses programmatically, mirroring the model of the SafetyNet Attestation API and opening new avenues for on-device threat intelligence and proactive security monitoring.

Background

▶ Watch: Developer's question: How to detect malware? (2:09)

The landscape of Android security is continuously challenged by the proliferation of mobile malware, a problem that has persisted for many years and continues to generate alarming headlines. Christopher Le Roy's interest in this area was sparked by his earlier offensive research, which involved creating a tool called quad_sir to backdoor Android applications with Meterpreter payloads. While demonstrating quad_sir, a pivotal question emerged from a developer: "How do I detect if my app has been backdoored by this tool or if my app has been infected with some malware?" This question underscored a significant gap in existing Android security capabilities, particularly the lack of reliable on-device runtime detection.

Traditional Android malware analysis primarily relies on two approaches: static analysis and dynamic analysis. Static analysis involves tearing apart the APK (Android Package Kit) to examine its components, permissions, code signatures, and other metadata. While useful for initial triage, static analysis in Android is notoriously difficult due to the complexities of Java, Kotlin, and Smali code, as well as variations across different ART (Android Runtime) versions. Crucially, static analysis can only reveal a subset of an application's potential functionality, often failing to detect behaviors that are dynamically loaded or executed at runtime.

Dynamic analysis, on the other hand, involves executing the application in a controlled environment to observe its behavior. This includes services like Google Play Protect (formerly Google Play Bouncer), which scans submitted applications, and third-party antivirus solutions. However, Le Roy expresses a skeptical view of mobile antivirus, considering "antivirus in the word mobile" an oxymoron. More advanced dynamic analysis often involves sandboxing the app in isolated environments like Cuckoo Sandbox, but this typically requires a separate machine, lacks scalability (often needing human analysts for complex samples), and isn't designed for real-time, on-device detection. Other protective measures like SELinux, MDM (Mobile Device Management), and MEM (Mobile Enterprise Management) aim to enforce policies but don't directly address runtime malware detection within an application.

Le Roy highlights several shortcomings of these existing methods: the inherent difficulty and limited scope of static analysis, the scalability issues and offline nature of many dynamic analysis tools, and the impracticality of using traditional forensics tools at runtime on a live device. Specifically, he points out that static analysis cannot reveal runtime injection of code (e.g., remotely loaded JARs or APKs) or reflectively executed commands, as these components are not present in the original APK. This frustration—the absence of a reliable way to detect malware on one's own Android device at runtime—became the driving force behind his research. The core insight was that every action an Android app performs, from network communication to file access, ultimately involves the instantiation and manipulation of objects on the heap, making the heap a rich target for runtime inspection.

Key Findings

▶ Watch: Demo: Frida detects injected code and remote connections (14:00)

The research presented by Christopher Le Roy yielded several key findings that collectively propose a novel and effective method for runtime Android malware detection:

  1. Runtime Object Carving for Behavior Detection: The most significant finding is the ability to programmatically scan the application's heap at runtime for instantiated objects of specific types and to extract their values. This "object carving" technique, facilitated by Frida, provides a dynamic view of an app's actual behavior, which is often completely invisible to static analysis. By looking for objects like java.net.Socket, java.net.URL, or DexClassLoader, security researchers can infer malicious activities.
  1. Detection of Dynamic Code Loading: The presence of instantiated DexClassLoader objects on the heap is a strong indicator of dynamic code injection. Malware frequently uses DexClassLoader to load additional JARs or APKs into the process at runtime, often fetching them remotely. This technique allows malware to hide its full functionality from static analysis, as the malicious code is not part of the initial APK. Le Roy demonstrated that by inspecting DexClassLoader instances, one can identify the paths from which these external code modules are loaded, such as data/data/com.twitter.android/, revealing the hidden functionality.
  1. Identification of Command and Control (C2) Communications: By scanning for java.net.Socket and java.net.URL objects, the technique can identify active network connections established by the application. This directly exposes C2 channels, data exfiltration attempts, or other unauthorized network interactions. The demo explicitly showed how a Meterpreter connection to a specific IP address and port (e.g., 10.2.168.x:4444) could be identified among legitimate network traffic.
  1. Hooking java.lang.Runtime.exec for Shell Command Detection: A particular challenge in runtime analysis is detecting shell command execution via java.lang.Runtime.exec. Since Runtime objects are static and their properties don't easily reveal past command executions, direct object inspection is ineffective. Le Roy's solution involves using Frida to "overload" or hook the six unique method signatures of Runtime.exec. This allows for the interception of every call to exec, logging the exact command being executed (e.g., sh -c ps, sh -c sh). This is crucial for detecting reverse shells or other command injection attacks.
  1. Enabling On-Device Runtime Analysis with ate_cake: The research culminated in the development of ate_cake, a custom Android Frida library. This library allows an Android application to programmatically perform the described runtime analyses directly on the device itself. Inspired by the SafetyNet Attestation API, ate_cake provides an API for querying the state and behavior of other applications, moving the powerful analysis capabilities from a workstation to the mobile device. This is a significant step towards enabling continuous, on-device threat monitoring.

Technical Deep Dive

▶ Watch: Why static analysis misses runtime code injection (16:00)

Le Roy's journey into runtime Android malware detection began with exploring various memory analysis techniques, each presenting its own set of challenges. Initial considerations included inspecting /proc/PID/maps for heap regions, but this was quickly dismissed due to its complexity and the difficulty of parsing raw memory maps. Using HPROF (Heap PROFile) files for heap snapshots also proved problematic, as it involves fighting the Garbage Collector (GC) and dealing with time-sensitive snapshots that are difficult to work with efficiently. Traditional memory forensics tools like Volatility were also considered but deemed unsuitable for real-time, on-device analysis, as they typically require a rooted device or a customized kernel to obtain full memory snapshots. Even debugging tools like GDB presented hurdles, especially with different memory allocators like DL_malloc and ART, making it difficult to reliably extract object information.

These frustrations led Le Roy to Frida, a dynamic instrumentation toolkit. Frida was chosen for its superior capabilities in accessing heap objects, tracing calls, and monitoring behavior in an efficient and user-friendly manner, especially its "object carving functionality." Frida operates by injecting its FridaGadget into a target process, allowing JavaScript snippets to interact with the application's runtime. This enables powerful hooks and memory manipulation.

The core technical approach involves using Frida's JavaScript API to:

  1. Enumerate Objects on the Heap: Frida can scan the entire heap of a target process for instances of specified Java classes. For example, to detect dynamic code loading, the script searches for Java.use("dalvik.system.DexClassLoader").$instances. Each found instance reveals the DexClassLoader object, including the path to the loaded JAR or APK. This directly exposes malicious code injection, as DexClassLoader is commonly used by malware to load additional functionality remotely or from hidden files.
  2. Monitor Network Connections: To identify C2 communications, the script looks for instances of Java.use("java.net.Socket").$instances and Java.use("java.net.URL").$instances. Inspecting these objects reveals details like local and remote IP addresses, ports, and connection states, allowing for the identification of suspicious outbound connections. The talk demonstrated detecting a Meterpreter C2 channel by observing a java.net.Socket instance connecting to a specific internal IP and port (e.g., 10.2.168.x:4444).
  3. Hooking java.lang.Runtime.exec: Detecting command execution is a critical aspect of malware analysis. The java.lang.Runtime.exec() method is used by Android applications to execute shell commands. However, the Runtime object is a static singleton, and inspecting its state directly does not reveal which commands have been executed. To overcome this, Frida's hooking capabilities are employed. The Runtime.exec() method has six overloaded signatures. Le Roy's technique involves hooking all these signatures using Java.use("java.lang.Runtime").exec.overload(...). When any of these exec methods are called, the hook intercepts the call, logs the arguments (i.e., the command being executed), and then passes control back to the original method. This provides a real-time log of all shell commands executed by the process, a powerful mechanism for detecting reverse shells or other command injection attacks that would otherwise be invisible.

To bring this analysis capability directly onto an Android device, Le Roy developed ate_cake. This custom Android Frida library aims to provide an API similar to the SafetyNet Attestation API, allowing an Android application to query the security state of other apps. ate_cake essentially implements D-Bus over TCP on Android to communicate with a running Frida daemon. Acknowledging the complexity and "ugliness" of implementing D-Bus directly, Le Roy's approach involved a clever hack: he sniffed existing Frida client-server D-Bus sessions (from a workstation to an Android device), performed replay attacks, and reverse-engineered the necessary byte sequences. By identifying and parameterizing key fields within these byte arrays (such as the PID of the target process), ate_cake can send specific commands to the Frida daemon and receive results, all from within another Android application. This allows ate_cake to execute the aforementioned heap scans and method hooks on any target process, making runtime malware detection an on-device capability. The prerequisite for ate_cake is a running Frida daemon on the Android device, which can be achieved by bundling Frida into a custom Android ROM or manually setting it up on a rooted device.

Demo / Proof of Concept

▶ Watch: Introducing ATEK: On-device API for app analysis (26:10)

The core of Le Roy's presentation involved a compelling series of demonstrations showcasing the effectiveness of his runtime analysis techniques. The primary malware sample used was a standard Meterpreter payload backdoored into a legitimate Twitter application. This allowed for the execution of various malicious activities in the context of a seemingly benign app.

The first part of the demo focused on detecting dynamic code loading and Command and Control (C2) communications. Le Roy ran a Frida JavaScript client from a workstation, targeting the infected Twitter application. The script was configured to scan the heap for instances of java.net.Socket and DexClassLoader.

  1. Detection of Meterpreter C2: As the Meterpreter session established a connection, the Frida script successfully identified multiple java.net.Socket instances. Among these, the script specifically highlighted a suspicious connection to an internal IP address (e.g., 10.2.168.x) on port 4444, which is the default listening port for a Meterpreter multi-handler. This demonstrated the ability to pinpoint active C2 channels by inspecting network-related objects on the heap.
  2. Detection of Dynamic Code Loading: Crucially, the script also identified two instantiated objects of DexClassLoader. This confirmed that the Meterpreter payload was dynamically loading JAR files into the Twitter application's process at runtime. The script was able to display the paths from which these JARs were loaded, such as data/data/com.twitter.android/, illustrating how code injected remotely by the multi-handler became visible on the heap. This directly contrasted with static analysis, which would not reveal the contents or even the presence of these dynamically loaded malicious JARs.

Next, Le Roy demonstrated the technique for detecting shell command execution, a particularly stealthy activity. He used a separate, vanilla Android application that was designed to load a JAR file via DexClassLoader at runtime. This loaded JAR contained code to execute shell commands, specifically bin/sh -c ps and an HTTP GET request.

  1. Hooking java.lang.Runtime.exec: By running a Frida script designed to hook all six signatures of java.lang.Runtime.exec, Le Roy showed that when the dynamically loaded code executed bin/sh -c ps, the hook successfully intercepted the call. The script output a message indicating "exec got called" and displayed the exact command that was passed to the exec method. This proved the effectiveness of the hooking mechanism in revealing hidden command execution.
  2. Meterpreter Shell Command Detection: The exec hook was then applied to the infected Twitter application. When an interactive native shell was dropped via Meterpreter (which typically uses sh -c sh to establish the shell), the Frida script immediately detected the exec call, revealing sh -c sh as the executed command. This demonstrated that even sophisticated frameworks like Meterpreter, when interacting with the underlying OS shell, could be caught by this runtime hook. Le Roy noted that subsequent commands within the established Meterpreter shell might not always trigger new exec calls as they often pipe through an existing instance, but the initial shell establishment was reliably detected.

While a live demonstration of ate_cake running directly from an Android application was unfortunately missed during the talk due to time constraints, Le Roy confirmed that the proof-of-concept application is available online. He described that this application uses the ate_cake library to specify a target process (e.g., the infected Twitter app) and then programmatically queries it for indicators like DexClassLoader instances or remote java.net.Socket connections, reporting whether the app appears malicious. This verbal confirmation underscored the practical applicability of bringing these advanced runtime analysis capabilities directly to the device.

Defensive Implications

▶ Watch: Conclusions: Defense in depth and attack surface concerns (31:50)

The research presented by Christopher Le Roy offers significant defensive implications for the Android ecosystem, moving beyond traditional, often insufficient, security paradigms. While not a standalone panacea, this novel runtime analysis technique provides a powerful addition to a defense-in-depth strategy.

Firstly, this approach significantly enhances dynamic analysis capabilities by enabling on-device, real-time inspection. Unlike offline sandboxes that require samples to be moved to a separate environment, Le Roy's method allows security teams or even end-users (through a trusted security application) to scrutinize an app's behavior as it runs on the device. This is crucial for detecting malware that employs sophisticated evasion techniques against static analysis, such as dynamic code loading, reflection, or remotely fetched payloads. By identifying DexClassLoader instances and their loaded JARs/APKs, defenders can uncover hidden functionalities that are not present in the original APK.

Secondly, the ability to carve java.net.Socket and java.net.URL objects at runtime provides an effective mechanism for detecting Command and Control (C2) communications and data exfiltration. This allows for the immediate identification of suspicious network connections, which are often the hallmark of active malware. Rather than relying on network-level monitoring that might be encrypted or proxied, this technique inspects the connections from within the process itself.

Thirdly, the specific solution for hooking java.lang.Runtime.exec fills a critical gap in detection. Malware frequently uses shell commands to achieve persistence, escalate privileges, or interact with the underlying operating system. By intercepting these calls, defenders gain visibility into low-level system interactions that are otherwise difficult to track, especially when invoked via reflective calls or dynamically loaded code. This is invaluable for identifying reverse shells and other command injection vulnerabilities or attacks.

Finally, the development of the ate_cake library points towards a future where the Android operating system itself could provide more robust on-device APIs for security monitoring. Le Roy suggests that, similar to the SafetyNet Attestation API which provides information about the device's integrity, there could be an API to query the runtime state and behavior of individual applications. This would empower legitimate security applications to perform deep, granular analysis without requiring a workstation, fostering a more self-aware and self-defending Android environment.

However, Le Roy also acknowledged important defensive caveats. Implementing such a system would inevitably increase the attack surface of the Android device, as the underlying Frida daemon and ate_cake library typically require root privileges to operate. Any service running with elevated permissions becomes a prime target for attackers. This highlights the inherent trade-off between increased security visibility and potential new vectors for exploitation. Therefore, any real-world implementation would require meticulous security engineering and robust hardening to mitigate these risks. Despite these challenges, the research provides a powerful blueprint for augmenting Android's native defenses against increasingly sophisticated mobile threats.

Key Takeaways

  • Static analysis alone is insufficient: Modern Android malware frequently employs dynamic code loading, reflection, and remote execution, rendering static analysis inadequate for comprehensive detection.
  • Runtime object inspection is powerful: By using instrumentation tools like Frida to scan the application's heap, security researchers can identify instantiated objects (e.g., DexClassLoader, java.net.Socket) and their values, revealing real-time malicious behaviors like dynamic code injection and C2 communications.
  • Hooking java.lang.Runtime.exec is crucial: Detecting shell command execution, a common malware tactic, is challenging with direct object inspection. Overloading java.lang.Runtime.exec methods allows for the interception and logging of all shell commands executed by a process.
  • On-device analysis is the future: The ate_cake library demonstrates the feasibility of performing sophisticated runtime analysis directly from an Android application, akin to the SafetyNet Attestation API, enabling continuous, real-time threat detection on the device itself.
  • Defense-in-depth is key: This technique is not a silver bullet but a vital layer in a multi-faceted security strategy, augmenting existing static and dynamic analysis methods.
  • Increased attack surface is a concern: Implementing such privileged runtime monitoring introduces new security risks, as the underlying tools often require root access and could become targets for exploitation if not rigorously secured.

About the Speaker(s)

Christopher Le Roy is a Security Researcher and Engineer at Heroku. He describes himself as someone who enjoys both "breaking stuff and building stuff." With a background in security research, Le Roy is known for his work in uncovering vulnerabilities and developing tools to understand and counter threats. He is active on Twitter under the handle @Bronpony. His previous research includes the development of quad_sir, a tool for backdooring Android applications, which directly inspired the defensive research presented in this talk.

Reviews

→ Top-rated talks at BruCON 0x0A

All talks from BruCON 0x0A