Metasploit Penetration Testing Cookbook
上QQ阅读APP看书,第一时间看更新

Setting up the database in Metasploit

An important feature of Metasploit is the presence of databases which you can use to store your penetration testing results. Any penetration test consists of lots of information and can run for several days so it becomes essential to store the intermediate results and findings. So a good penetration testing tool should have proper database integration to store the results quickly and efficiently.

Getting ready

Metasploit comes with PostgreSQL as the default database. For the BackTrack machine, we have one more option—MySQL. You can use either of the two databases. Let us first check out the default settings of the PostgreSQL database. We will have to navigate to database.yml located under opt/framework3/config. To do this, run the following command:

root@bt:~# cd /opt/framework3/config root@bt:/opt/framework3/config# cat database.yml production: adapter: postgresql database: msf3 username: msf3 password: 8b826ac0 host: 127.0.0.1 port: 7175 pool: 75 timeout: 5

Notice the default username, password, and default database that has been created. Note down these values as they will be required further. You can also change these values according to your choice as well.

How to do it...

Now our job is to connect the database and start using it. Let us launch the msfconsole and see how we can set up the databases and store our results.

Let us first check the available database drivers.

msf > db_driver [*]Active Driver: postgresql [*]Available: postgresql, mysql

PostgreSQL is set as the default database. If you want to change the database driver then you can execute the following command:

Msf> db_driver mysql [*]Active Driver: Mysql

This will change the active driver to MySQL. In this book, we will primarily be using PostgreSQL for demonstrations.

Note

Rapid7 has dropped the support for MySQL database in the recent versions of Metasploit so the db_driver command may not work. The only default driver supported with the framework in that case will be PostgreSQL.

How it works...

To connect the driver to msfconsle we will be using the db_connect command. This command will be executed using the following syntax:

db_connect username:password@hostIP:port number/database_name 

Here we will use the same default values of username, password, database name, and port number which we just noted down from the database.yml file:

msf > db_connect msf3:8b826ac0@127.0.0.1:7175/msf3

On successful execution of the command, our database is fully configured.

There's more...

Let us discuss some more important facts related to setting up the database.

Getting an error while connecting the database

There are chances of an error while trying to establish the connection. There are two things to keep in mind if any error arises:

  • Check the db_driver and db_connect commands and make sure that you are using the correct combination of the database.
  • Use start/etc/init.d to start the database service and then try connecting it.

If the error still prevails then we can re-install the database and associated libraries using the following commands:

msf> gem install postgres msf> apt-get install libpq-dev

Deleting the database

At any time, you can drop the database created and start again to store fresh results. The following command can be executed for deleting the database:

msf> db_destroy msf3:8b826ac0@127.0.0.1:7175/msf3 Database "msf3" dropped. msf>