Skip to main content

Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Hacking - Man-in-the-Middle and Brute-force in Action. Part 2 of 2.

Illustration of Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Hacking - Man-in-the-Middle and Brute-force in Action. Part 2 of 2.
Dariusz Tytko

In first part of this article, I described how do we analyze protocols during mobile applications testing.

During this analysis, I noticed that the Diffie–Hellman protocol is used to exchange encryption keys. The protocol implementation was audited, and I discovered that it is prone to two attacks: Man-in-the-Middle and brute-force. Each of these attacks compromise the security of the protocol, allowing attackers to view and modify the data sent between the mobile applications and the servers.

The attacker must have access to the application’s network traffic (e.g., to the router through which the phone connects to the Internet).

Introduction

The application uses the Diffie–Hellman protocol to exchange encryption keys between the server and the mobile application. At the beginning of the connection, plaintext RPC messages are exchanged for this purpose. An example of an RPC message sent from the server to the application is shown below:

Server to application RPC

The RPC message sent from the application to the server:

Application to server RPC

For simplicity, only the process of exchanging an encryption key to encrypt data in the application-to-server direction will be described. The exchange of the encryption key for encrypting data in the server-to-application direction is implemented in a similar way.

Diffie–Hellman protocol

The protocol is implemented in the following way:

  1. The server generates a prime number p (yellow) and a base g (green).
  2. The server generates a secret value a.
  3. The server calculates A (blue) = pow(g, a) mod p.
  4. The server sends the values p, g and A to the mobile application:

Server sends values

  1. The mobile application generates a secret value b.
  2. The mobile application calculates encryption key = pow(A, b) mod p.
  3. The mobile application calculates B (pink) = pow(g, b) mod p.
  4. The mobile application sends the value B to the server:

Application sends B

  1. The server calculates encryption key = pow(B, a) mod p.
  2. At this point, the server and the application have exchanged the encryption key, which is used to encrypt rest of the communication in the application-to-server direction.
  3. The exchange an encryption key for the server-to-application direction is performed in a similar way.

Man-in-the-Middle attack

The Diffie-Hellman protocol does not provide authentication, making it prone to Man-in-the-Middle attacks. An attacker can act as a network traffic forwarder between the server and the application. During the key exchange phase, the attacker exchanges separate keys for both connections: one between the server and the attacker, and another between the attacker and the application. After that, the attacker can decrypt the data using the first key, display and/or modify it, re-encrypt it using the second key, and then forward it.

It is worth noting that there is also a simpler variant of this attack that allows the attacker to force the server and the application to use an encryption key chosen by the attacker. During the key exchange phase, the attacker changes the g and A values to zero:

Modified values

In this scenario, the application calculates encryption key as pow(0, b) mod p. Regardless of the secret value b, the encryption keys will always be zero. Then, the application calculates B value as pow(0, b) mod p, which will also always be zero regardless of the secret value b. The B value is then sent to the server:

Zero B value

The server calculates the encryption key = pow(0, a) mod p. Regardless of the secret value a, the encryption key will always be zero. As a result, the attacker can force the server and the application to use an encryption key consisting entirely of zeros.

I was able to perform this attack during the tests. An example usage of the exploit, displaying decrypted RPC messages for both directions – application to server (in green) and server to application (in blue):

Attack demonstration

Brute-force attack

During the analysis of the Diffie–Hellman protocol implementation, it was noticed that it is prone to a brute-force attack. The mobile application generates a secret value b (in red) that is 4 bytes long (it should be at least 16 bytes long). This secret value is used to calculate B = pow(g, b) mod p. Since the attacker knows B, g and p, it is possible to iterate through all possible values of the b (only pow(2, 32) = 4 294 967 296 possible values) and check if any of them produce the expected B value. If so, the secret value b is found. The attacker can then use it to calculate the encryption key = pow(A, b) mod p.

Update version 1.2:

It is important to add that a secure source of random values (such as /dev/urandom) should be used to generate the entire key, not just a seed value to initialize the PRNG, as is currently done:

Random generation

It is also important to note that if the server generates a weak secret value, the protocol also becomes vulnerable to brute-force attacks.

Summary

It is important to remember to not implement a custom, proprietary encryption protocol, as it is a difficult task prone to mistakes that can compromise the security of the solution. Instead, it is advised to use widely adopted, proven secure solutions, such as WebSocket Secure (WSS), which can be used to transport the current RPC messages.

Other Insights

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

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.

READ article
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
A professional cybersecurity consultant ready to assist with your inquiry.

Any questions?

Happy to get a call or email
and help!