In this article by Nipun Jaswal, the author of the book Metasploit Bootcamp, we will be covering the following topics:
(For more resources related to this topic, see here.)
Penetration testing is an art of performing a deliberate attack on a network, web application, server or any device that require a thorough check up from the security perspective. The idea of a penetration test is to uncover flaws while simulating real world threats. A penetration test is performed to figure out vulnerabilities and weaknesses in the systems so that vulnerable systems can stay immune to threats and malicious activities.
Achieving success in a penetration test largely depends on using the right set of tools and techniques. A penetration tester must choose the right set of tools and methodologies in order to complete a test. While talking about the best tools for penetration testing, the first one that comes to mind is Metasploit. It is considered as one of the most practical tools to carry out penetration testing today. Metasploit offers a wide variety of exploits, a great exploit development environment, information gathering and web testing capabilities, and much more.
Now that we have completed the setup of Kali Linux let us talk about the big picture: Metasploit. Metasploit is a security project that provides exploits and tons of reconnaissance features to aid a penetration tester. Metasploit was created by H.D Moore back in 2003, and since then, its rapid development has led it to be recognized as one of the most popular penetration testing tools. Metasploit is entirely a Ruby-driven project and offers a great deal of exploits, payloads, encoding techniques, and loads of post-exploitation features.
Metasploit comes in various editions, as follows:
You can download Metasploit from the following link: https://www.rapid7.com/products/metasploit/download/editions/
We will be using the Metasploit community and framework version.Metasploit also offers various types of user interfaces, as follows:
Before we put our hands onto the Metasploit framework, let us understand basic terminologies used in Metasploit. However, the following modules are not just terminologies but modules that are heart and soul of the Metasploit project:
Metasploit comprises of various components such as extensive libraries, modules, plugins, and tools. A diagrammatic view of the structure of Metasploit is as follows:
Let's see what these components are and how they work. It is best to start with the libraries that act as the heart of Metasploit.
Let's understand the use of various libraries as explained in the following table:
Library name | Uses |
REX | Handles almost all core functions such as setting up sockets, connections, formatting, and all other raw functions |
MSF CORE | Provides the underlying API and the actual core that describes the framework |
MSF BASE | Provides friendly API support to modules |
We have many types of modules in Metasploit, and they differ regarding their functionality. We have payload modules for creating access channels to exploited systems. We have auxiliary modules to carry out operations such as information gathering, fingerprinting, fuzzing an application, and logging into various services. Let's examine the basic functionality of these modules, as shown in the following table:
Module type | Working |
Payloads | Payloads are used to carry out operations such as connecting to or from the target system after exploitation or performing a particular task such as installing a service and so on.
Payload execution is the next step after the system is exploited successfully. |
Auxiliary |
Auxiliary modules are a special kind of module that performs specific tasks such as information gathering, database fingerprinting, scanning the network to find a particular service and enumeration, and so on. |
Encoders | Encoders are used to encode payloads and the attack vectors to (or intending to) evade detection by antivirus solutions or firewalls. |
NOPs | NOP generators are used for alignment which results in making exploits stable. |
Exploits | The actual code that triggers a vulnerability |
Gathering knowledge of the architecture of Metasploit, let us now run Metasploit to get a hands-on knowledge about the commands and different modules. To start Metasploit, we first need to establish database connection so that everything we do can be logged into the database. However, usage of databases also speeds up Metasploit's load time by making use of cache and indexes for all modules. Therefore, let us start the postgresql service by typing in the following command at the terminal:
root@beast:~# service postgresql start
Now, to initialize Metasploit's database let us initialize msfdb as shown in the following screenshot:
It is clearly visible in the preceding screenshot that we have successfully created the initial database schema for Metasploit. Let us now start the Metasploit's database using the following command:
root@beast:~# msfdb start
We are now ready to launch Metasploit. Let us issue msfconsole in the terminal to startMetasploit as shown in the following screenshot:
Welcome to the Metasploit console, let us run the help command to see what other commands are available to us:
The commands in the preceding screenshot are core Metasploit commands which are used to set/get variables, load plugins, route traffic, unset variables, printing version, finding the history of commands issued, and much more. These commands are pretty general. Let's see module based commands as follows:
Everything related to a particular module in Metasploit comes under module controls section of the Help menu. Using the preceding commands, we can select a particular module, load modules from a particular path, get information about a module, show core, and advanced options related to a module and even can edit a module inline. Let us learn some basic commands in Metasploit and familiarize ourselves to the syntax and semantics of these commands:
Command | Usage | Example |
use [auxiliary/exploit/payload/encoder] | To select a particular |
|
show[exploits/payloads/encoder/auxiliary/options] | To see the list of available modules of a particular type |
|
set [options/payload] | To set a value to a particular object |
|
setg [options/payload] | To assign a value to a particular object globally, so the values do not change when a module is switched on |
|
run | To launch an auxiliary module after all the required options are set |
|
exploit | To launch an exploit |
|
back | To unselect a module and move back |
|
Info | To list the information related to a particular exploit/module/auxiliary |
|
Search | To find a particular module |
|
check | To check whether a particular target is vulnerable to the exploit or not |
|
Sessions | To list the available sessions |
|
Meterpreter commands | Usage | Example |
sysinfo | To list system information of the compromised host |
|
ifconfig | To list the network interfaces on the compromised host |
|
Arp | List of IP and MAC addresses of hosts connected to the target |
|
background | To send an active session to background |
|
shell | To drop a cmd shell on the target |
|
getuid | To get the current user details |
|
getsystem | To escalate privileges and gain system access |
|
getpid | To gain the process id of the meterpreter access |
|
ps | To list all the processes running at the target |
|
If you are using Metasploit for the very first time, refer to http://www.offensive-security.com/metasploit-unleashed/Msfconsole_Commandsfor more information on basic commands
Metasploit is an excellent choice when compared to traditional manual techniques because of certain factors which are listed as follows:
Throughout this article, we learned the basics of Metasploit. We learned about various syntax and semantics of Metasploit commands. We also learned the benefits of using Metasploit.
Further resources on this subject: