docs/metasploit-framework.wiki/Metasploit-Web-Service.md
The Metasploit web service allows interaction with Metasploit's various data models through a REST API.
To use the web service you will need a PostgreSQL database to serve as the backend data store. The msfdb tool allows you to manage both the Metasploit Framework database and web service. If you are going to configure the database manually you can find more information on the Managing the Database page.
Execute msfdb init and respond to prompts during the interactive initialization. The script first creates and configures the database, then it configures the web service, and finally configures the local msfconsole with the new data service connection.
The msfdb tool allows you to manage both the Metasploit Framework database and web service components together or independently. If the --component option is not provided then the specified command will be executed for the database followed by the web service. This default mode of operation is useful when first setting up the database and web service. The component may be specified if you wish to make changes to a given component independent of the other.
Usage: msfdb [options] <command>
msfdb --help for the complete usage informationmsfdb start - Start the database and web servicemsfdb --component webservice stop - Stop the web servicemsfdb --component webservice --address 0.0.0.0 start - Start the web service, listening on any host addressSSL is enabled by default and msfdb will generate a fake "snakeoil" SSL certificate during initialization using Rex::Socket::Ssl.ssl_generate_certificate if one is not provided. The generated SSL certificate uses a random common name (CN) which will not match your hostname, therefore, you will need to make appropriate accommodations when operating the web service with such a certificate. Please generate your own SSL certificate and key instead and supply those to msfdb using the --ssl-cert-file and --ssl-key-file options, and enable SSL verification by passing the option --no-ssl-disable-verify.
A simple verification that web service is up and running can be performed using cURL: curl --insecure -H "Accept: application/json" -H "Authorization: Bearer <token>" https://localhost:5443/api/v1/msf/version
The API account can be accessed with your preferred web browser by visiting https://<address>:<port>/api/v1/auth/account. If you want to change the API token for your account you can log in to the API account page and generate a new API token. You can find more
information on the data models and various API endpoints by visiting the API Documentation at: https://<address>:<port>/api/v1/api-docs
You can use the db_connect command to connect to the desired data service. When you successfully connect to a data service that connection will be saved in the Metasploit config file. You can provide a name with the -n option, otherwise one will be randomly generated. You can then use that name to reconnect to the data service at a later time.
Please note that you can only be connected to one data service at a time. The db_disconnect command will need to be used before switching to a new data service. You can use db_status to see information about the currently connected data service.
Usage: db_connect <options> <url>
-l,--list-services - List the available data services that have been previously saved.-y,--yaml - Connect to the data service specified in the provided database.yml file.-n,--name - Name used to store the connection. Providing an existing name will overwrite the settings for that connection.-c,--cert - Certificate file matching the remote data server's certificate. Needed when using self-signed SSL cert.-t,--token - The API token used to authenticate to the remote data service.--skip-verify - Skip validating authenticity of server's certificate (NOT RECOMMENDED).db_connect http://localhost:5443 - Connect to the Metasploit REST API instance at localhost running on port 5443db_connect -c ~/.msf4/msf-ws-cert.pem -t 72ce00fd9ab1a96970137e5a12faa12f38dcc4a9e42158bdd3ce7043c65f5ca37b862f3faf3630d2 https://localhost:5443 - Connect to the server running at localhost on port 5443 that has SSL and authentication enabled.db_connect -l - List the data services that have been saved.db_connect -n LA_server http://localhost:5443 - Connect to the data service running on localhost port 5443 and assign the name "LA_server" to the saved entry.http://<host>:<port>https://<host>:<port><user>:<password>@<host>:<port>/<database name>The db_save command can be used to save the currently connected data service as the default. Every time msfconsole starts up it will attempt to connect to that data service. You can always switch between data services if you have a default set, this will just determine which data service you are connected to when msfconsole is started.
Usage: db_save
db_connect http://localhost:5443 then db_save - Connect to the data service running on localhost port 5443 then set it as the default connection.Saved data services can be removed using the db_remove command. This can be useful if the data service no longer exists at that location, or if you no longer want to keep a record of it around for fast connection.
Usage: db_remove <name>
db_remove LA_server - Remove the saved data service entry called "LA_server"There are a few pieces of information to keep in mind when using data services with Metasploit Framework.
database.yml will still be honored for the foreseeable future, but a saved default data service will take priority when it is present.~/.msf4/config by default.