The database in Metasploit serves as a central repository for managing scan results, vulnerabilities, credentials, and other information collected during penetration tests. Leveraging the database streamlines workflows, enhances organization, and integrates seamlessly with scanning tools like Nmap.
Setting Up PostgreSQL
Metasploit uses PostgreSQL as its backend database. Below are the steps to configure and connect Metasploit with PostgreSQL.
Check PostgreSQL Status:
Verify that PostgreSQL is installed and running:
sudoservicepostgresqlstatus
Output should display Active: active (exited).
Start PostgreSQL:
If PostgreSQL is not running, start it:
sudosystemctlstartpostgresql
Initialize the Metasploit Database:
Configure the database schema:
sudomsfdbinit
This creates the database user msf and prepares the schema.
Troubleshooting:
If initialization fails, ensure all dependencies are up to date:
sudoaptupdate&&sudomsfdbinit
Verify Database Connection:
Open Metasploit and check the database status:
msfconsole-qdb_status
Output should display:
[*] Connected to msf. Connection type: PostgreSQL.
Using the Database
The Metasploit database allows testers to store, query, and manage data efficiently. Here’s how to leverage it for penetration testing.
Key Commands:
Command
Description
db_status
Check the database connection status.
db_connect
Manually connect to a database.
db_disconnect
Disconnect from the database.
db_export <file>
Export the database contents to a file.
db_import <file>
Import scan results or data from a file.
hosts
View a list of discovered hosts.
services
List services running on discovered hosts.
creds
View stored credentials.
loot
Manage and view gathered files or data.
Workspaces
Workspaces help organize data by project, IP range, or domain.
List Workspaces:
Create a New Workspace:
Switch Between Workspaces:
Delete a Workspace:
Help Menu for Workspaces:
Importing Scan Results
Metasploit integrates with tools like Nmap to directly import scan results into the database.
Importing Nmap Results:
Use Nmap Directly from Metasploit:
Viewing Imported Data:
List discovered hosts:
List services running on hosts:
Exporting and Backing Up Data
Export Database Contents:
Export all stored data to a file:
Import Previously Exported Data:
Restore data from an XML file:
Backing Up Workspaces:
Backup specific workspaces for later reuse.
Common Use Cases
Storing Scanned Hosts and Services:
Use db_nmap to scan a network and populate the database:
View the results:
Credential Management:
Automatically store discovered credentials:
Collaborative Penetration Testing:
Use the database to synchronize data across team members by sharing exports.