# Metasploit/UsingMetasploit

* Metasploit FAQs

# Using Metasploit

This chapter covers various aspects of using The Metasploit Framework.
For the time being, its a collection of assorted topics. Later these can be organized to make more sense.

## Using Databases with MSF

MSF allows storing scan/exploitation results into databases for persistent storage. The framework supports using quite a few database back-ends. These include:
* Postgres
* MySQL
* SQLite (version 2 & 3)
Note: Except for SQLite (which stores the database as a file), you need to start the database server for Postgres or MySQL before starting to use the databases with the Framework.
Note: Using the Framework with postgres requires edits to /etc/postgresql/x.x/main/pg_hba.conf. Replace the default "ident" method with "trust" as shown:
Note: before using postgres, you must run $ gem install postgres. This requires header files that come with postgresql-dev
Note: For best results use msfconsole to interact with a database from the Framework.
Database storage comes in handy for MSF in quite a few ways. The most interesting/hot topic of automated exploitation (someone please write a good article on that based on H.D's blog) works with database integration. MSF can scan hosts using nmap and store their states in the DB and then automatically tries to exploit vulnerabilities that have exploits present for them in MSF. Auxiliary modules such as scanners can also utilize databases to store different state information. You can even write your own quick scanner and have it store information in the underlying database.

### The Database Schema

MSF creates a database for its usage. This database contains following tables
* hosts
* creds
* refs
* services
* vulns
* vulns_refs
* clients
* events
* loots
* report_templates
* reports
* tasks
* users
* workspaces
You can easily view the structure of this database in your RDBMS.
Note: The folder framework_base_folder/data/sql/ contains SQL files that are used to create the database tables for a given RDBMS.

### Loading A Database Module

Before starting to use databases with MSF, appropriate database module should be loaded. For msfconsole, this can be loaded by using the load db_* commands. Here is a sample session of using MySQL with MSF.

### Creating a Database

The first time you want to use a database with MSF, you need to create it. Once a database is created, you can use it by just connecting to it in future sessions. The db_create command allows you to create a new database. Let's create a database named metasploit
Once a database is created, it is automatically usable for that session. For further sessions, you can connect to the db.

### Using an Existing Database

If you have created a database previously, then for future sessions you can just use it with the db_connect command.

### Disconnecting a Database

If in a session you feel that you no longer have the need to use the database then you can disconnect from the database by simply issuing the db_disconnect command at the 'MSF >' prompt.

### Dropping a Database

When you just want to delete the database with all data in it (perhaps, you have taken a backup and are now looking to free up disk space), then you can do so by using the db_destroy command.

### Database Backend Commands

When MSF is connected to a database, another set of commands called Database Backend Commands are available. These commands allow you to perform port scans on hosts, check for live hosts, what services they are running and the vulnerabilities that these services have.
The important one and most often used is db_nmap which will run nmap with specified commands and record the findings within the database.
To list the host(s) found in the scan...
To list possible vulnerabilities found in the scan of the host(s)...

### db_autopwn

You can use another database backend command db_autopwn to execute exploits against the host(s) from the database. H.D. Moore wrote about this functionality when added to the framework -- for more information.