# Android binary libraries

## Mediacode.apk

The application contains DEX code and binary blobs.

One version for each architecture.

* armeabi: ARM 32bits no Floating Point
* mips: MIPS
* x86: intel X86 32bits

Libraries export symbols to be used through JNI.

* `nm -gD lib/x86/librrnad.so | grep JNI`

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2F1Ix5g9NlmDq8VytOeFaF%2FScreenshot%20from%202024-03-04%2015-27-37.png?alt=media&#x26;token=07cf9394-4a36-48df-ac38-825a8863d6a9" alt=""><figcaption></figcaption></figure>

Before the binary libraries can be used, Java must load them.

* `System.loadLibrary`: argument is the library name (without lib, architecture or .so).
* `System.load`: generic object load. The argument is the full path to the object.
* **The JNI\_OnLoad method is called automatically (in the lib).**
  * Allows automatic setup of data structures and generic initialization.
  * May be abused if malware is present.

Without the library, the application will crash when external methods are requested.

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2F3WPIX7bpiOYEEnNLk11m%2FScreenshot%20from%202024-03-04%2015-27-37.png?alt=media&#x26;token=d61b6cea-f7ce-44c4-81a8-e78e620f0bb2" alt=""><figcaption></figcaption></figure>

## JNI Arguments

**Native methods support arguments from Java code.**

* Arguments are pointers to Java structures.
* Must be processed using specific methods, capable of handling the native Java types.

Native methods can also be called Java methods and classes.

* Mainly achieved by the first argument of any JNI method: JNIEnv\*.

JNIEnv\* is a pointer to a structure with a large number of functions.

* JNI Methods use it to invoke Java methods and handle Java types.

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FIWy95a2QMqKyh7yJJjjr%2Fimage.png?alt=media&#x26;token=45721cb5-6564-48b6-8e26-558eef647ef3" alt=""><figcaption></figcaption></figure>

In the Java world native methods are declared:

* With the keyword native.
* Without implementation.

Easy to spot if we have the Java or Smali code.

* Java: `public native String decryptString(String)`.
* Smali: `.method public native decryptString(Ljava/lang/String;)Ljava/lang/String`
