Skip to main content

Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Analysis and Toolkit Setup. Part 1 of 2.

Illustration of Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Analysis and Toolkit Setup. Part 1 of 2.
Dariusz Tytko

During one of latest pentests I tested mobile application.

To perform analysis of the communication protocol and prepare a toolkit for testing network communication, the Android version of the application was used.

Protocol analysis

The protocol is implemented using C++ language, the implementation is included in the native library lib/arm64-v8a/lib[…].so. Wireshark, Frida (the instrumentation toolkit) and Ghidra (a software reverse engineering framework) were used to analyze the protocol.

The application communicates with the servers using TCP port [REDACTED]. Within the protocol, binary packets are exchanged in the following format:

Binary packet format

The first two packets (in both directions – application to server and server to application) are in plaintext and have the following format:

Plaintext packet format

After exchanging the plaintext packets, the rest of the communication is encrypted:

Encrypted communication

To encrypt the RPC messages, the ChaCha20 algorithm is used. The ChaCha20 algorithm is initialized as follows (Python 3 equivalent):

ChaCha20 initialization

It is important to note that, due to the limited time in the first phase of tests, the algorithm for generating the encryption key was not reverse-engineered, as it was not necessary to prepare a working toolkit – the Frida gadget, which extracts the key from the application’s memory, was used instead. However, reverse engineering of the key generation algorithm will be performed in the next phase, as it is important to verify the security of this mechanism, which is a critical part of the implemented protocol’s security.

Testing toolkit

The prepared toolkit consists of the following modules:

Toolkit modules

The iptables command is executed on the router to redirect the application’s network traffic to the proxy:

Iptables redirection

The proxy forwards the traffic to the stream presenter, which is responsible for decrypting and presenting the RPC messages in a human-readable format, e.g., an RPC message responsible for changing the user’s city (number 6), sent as a serialized binary data:

Binary RPC message

Is decoded to the following format:

Decoded RPC message

The proxy also forwards the traffic to the stream processor, which decrypts the RPC messages, optionally modifies them, encrypts them, and returns them to the proxy, which then forwards them to the server. The traffic sent in the opposite direction (from the server to the application) is processed in a similar way. Additionally, there are two helper modules: the console, which allows configuration of the stream processor (e.g., setting filters on which RPC messages should be intercepted and presented for modification), and the key extractor, which is a Frida gadget that extracts the encryption key from the application’s memory. This is achieved by intercepting one of the functions from the lib/arm64-v8a/lib[…].so library, which is responsible for initializing the encryption keys for the ChaCha20 algorithm. The function was located and reverse-engineered using Ghidra framework:

Ghidra reverse engineering

Intercepting the above function allows the extraction of the keys using the following Frida gadget:

Frida gadget

An example session of using the toolkit is presented below:

Toolkit session example

Next part

As the key generation algorithm has been analyzed, it was possible to perform two attacks on the encryption key exchange protocol: Man-in-the-Middle and brute-force.

How it was found and exploited will be shown in next article.

Other Insights

Illustration of Memory Heist: The Secrets and Risks of Cold Boot Attacks

Memory Heist: The Secrets and Risks of Cold Boot Attacks

Mateusz Lewczak

A Cold Boot Attack is a technique designed to capture data directly from a computer's RAM, where critical and sensitive information is often stored. What kind of data? It could be almost anything: passwords, encryption keys, user login data, or even active sessions, which could provide attackers with extensive access to the system. In short, the attacker is after any information held in RAM during computer operation, and the Cold Boot Attack allows them to retrieve it.

READ article
Illustration of Accessing Internal Network by WiFi Hacking - 2024 Pentest Case

Accessing Internal Network by WiFi Hacking - 2024 Pentest Case

Aleksander Wojdyła

During the last penetration test, I performed an Evil Twin attack, which involves setting up a fake access point with the same name as the legitimate one. Due to improper configuration of endpoint devices (e.g., computers, phones, tablets), users could accept an incorrect (fake, generated by the auditor) certificate identifying the network. This led to a successful capture of the authentication segment of the communication. Subsequently, the auditor subjected the captured data to brute-force attacks, resulting in the retrieval of credentials.

READ article
A professional cybersecurity consultant ready to assist with your inquiry.

Any questions?

Happy to get a call or email
and help!