First, let's review the structure of Metasploit.
Metasploit is designed with a modular concept to improve code reuse efficiency. The framework is developed in Ruby and includes components written in Perl, C, Assembly, and Python. It is primarily designed for Linux operating systems, so its command structure is very similar to a Linux command shell. However, it now supports all major operating systems, such as Windows, Solaris, and Mac.
+---------------+------------------+----------------------------------------------+
| English Name | Module Name | Description |
+---------------+------------------+----------------------------------------------+
| `Aux` | Auxiliary Module | Provides a wealth of auxiliary modules for |
| | | information gathering during penetration, |
| | | including scanning and fingerprinting |
| | | various network services, building fake |
| | | services to collect login credentials, |
| | | password guessing, etc. |
+---------------+------------------+----------------------------------------------+
| `Exploits` | Exploit Module | Code components that exploit discovered |
| | | security vulnerabilities or configuration |
| | | weaknesses to attack remote target systems, |
| | | plant and run payloads, and gain access |
| | | control over the target systems. |
+---------------+------------------+----------------------------------------------+
| `Post` | Post-Exploit | Supports various post-exploitation actions |
| | Module | on the controlled system after gaining |
| | | remote access control through exploitation, |
| | | such as obtaining sensitive information, |
| | | further pivoting, and launching pivot |
| | | attacks. |
+---------------+------------------+----------------------------------------------+
| `Payloads` | Payload Module | Payloads are code segments that run on the |
| | | target system after a successful |
| | | exploitation, typically to open a control |
| | | session connection for the attacker. |
+---------------+------------------+----------------------------------------------+
| `Encoders` | Encoder Module | After assembling the payload and NOP |
| | | instructions into an instruction sequence, |
| | | Metasploit needs to perform an important |
| | | encoding step before the exploit module |
| | | injects the malicious data buffer into the |
| | | target system for execution. |
+---------------+------------------+----------------------------------------------+
| `Nops` | NOP Module | NOP (No Operation) instructions are |
| | | operations or instructions that have no |
| | | substantial effect on the program's |
| | | execution state. |
+---------------+------------------+----------------------------------------------+
Metasploit also integrates several vulnerability scanning components, such as:
-
Nmap
scanner: Suitable for Windows, Linux, and Mac OS. Used for host discovery, port scanning or enumeration, service discovery, and detecting operating systems, hardware addresses, software versions, and vulnerabilities.
-
NeXpose
scanner: Scans the network to find running devices, identify their operating systems and application vulnerabilities, analyze the scanned data, and generate vulnerability scan reports.
-
Nessus
scanner: One of the most widely used vulnerability scanning tools. It adopts a client/server model, with the server performing security checks and the client configuring and managing the server. The server also uses a plugin system, allowing users to add plugins for specific functions and more complex security checks.
Now, we will create a new Metasploit scanner module called simple_tcp.rb
.
- Open an xfce terminal on the LabEx host machine and start the Metasploitable2 target by running the following command:
sudo virsh start Metasploitable2
- Test the connectivity to the target machine by pinging it:
ping 192.168.122.102
Press Ctrl+C
to stop the ping.
- Launch the Kali Linux container and enter the bash environment by running:
docker run -ti --network host b5b709a49cd5 bash
- Inside the Kali container, test the network connection to the target machine:
ping 192.168.122.102
Press Ctrl+C
to stop the ping.
- Then, navigate to the
scanner
module directory:
cd /usr/share/metasploit-framework/modules/auxiliary/scanner
- Create a new file named
simple_tcp.rb
:
sudo vi simple_tcp.rb
- Copy and paste the following code into the file:
require 'msf/core'
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Scanner
def initialize
super(
'Name' => 'Mr_Zhou Scanner',
'Version' => '$Revision$',
'Description' => 'Shiyanlou TCP Scanner',
'Author' => 'lucat',
'License' => MSF_LICENSE
)
register_options(
[
Opt::RPORT(12345)
], self.class)
end
def run_host(ip)
connect()
sock.puts('HELLO SERVER')
data = sock.recv(1024)
print_status("Received: #{data} from #{ip}")
disconnect()
end
end
- This code defines a new Metasploit module called "Mr_Zhou Scanner" that scans TCP ports and displays any received data from the server. Let's go through the code:
def initialize
super(
'Name' => 'Mr_Zhou Scanner',
'Version' => '$Revision$',
'Description' => 'Shiyanlou TCP Scanner',
'Author' => 'lucat',
'License' => MSF_LICENSE
)
This section sets the metadata for the module, such as its name, description, author, and license.
register_options(
[
Opt::RPORT(12345)
], self.class)
This line registers the option to scan port 12345.
def run_host(ip)
connect()
sock.puts('HELLO SERVER')
data = sock.recv(1024)
print_status("Received: #{data} from #{ip}")
disconnect()
end
This method is executed for each target IP address. It connects to the specified port, sends the string "HELLO SERVER", receives and prints any data from the server, and then disconnects.
Save the file and exit the editor.