Rest Api PDF
Rest Api PDF
Rest Api PDF
REST API
The RESTAPI provides an interface that enables you to easily consume the resources that are available
in Metasploit Pro, such as hosts, vulnerabilities, and campaign data, from any application that can make
HTTP requests. You can use the RESTAPI to extract data from Metasploit Pro to manage in other tools,
to automate tasks, and to integrate with other applications.
Authenticating Requests
You must have a valid Metasploit Pro license key in order to use the RESTAPI. To generate an API key,
you need to log in to the web interface (https://localhost:3790) and select Administration > Global
Settings. When the Global Settings page appears, click on the API Keys tab and click the Create
anAPIkey button, as shown below:
The form requires that you provide a key name for the API token. After you provide a name, click the
Create button to generate the token.
To view the API key, click on the obfuscated token located in the API Keys table, as shown below:
A popup window appears and displays the full key. You need to copy this key to use in your calls.
REST API 1
Now that you have an API key, you can use it to create a request to the server. All requests must include
an APIkey that is defined in a custom HTTPheader called 'token', which is specified using the -H option,
as shown below:
$ curl -H "token:6bde125560088033c618c2app234"
https://localhost:3790/rest_api/v2/base
The example above is a base route that performs an API check and uses the following options:
l -H: Adds a custom HTTP header to pass to the server. Use this option to pass the API key in your
requests.
The request returns the following response with a valid API key:
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 07 Apr 2015 21:26:13 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-UA-Compatible: IE=Edge,chrome=1
ETag: "7215ee9c7d9dc229d2921a40e899ec5f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 2359d1d5c55e06242220a2465358cac9
X-Runtime: 0.004253
The 200 OK response code indicates that the request was successful. If you receive a 403 response, you
need to verify that the API key is valid.
! The examples in this guide use cURL to create, format, and send requests for resources.
l You must have an active Metasploit Pro license key to use the RESTAPI.
l Each request must have a valid API token in the HTTP 'token' header.
Authenticating Requests 2
Requesting Data
Each resource is associated with a URI and is named as a noun, such as 'hosts', 'sessions', and
'campaigns'. You create a request for a resource using a defined URI.
https://<Metasploit server>:3790/rest_api/v2/workspaces/:workspace_
id/hosts/:host_id
Any item preprended with a colon, such as 'workspace_id', indicates that it is a variable and needs to be
replaced with the appropriate value. To learn how to find the ID for a workspace or host, read this section.
For example, if you want to request a list of hosts for the default workspace, the request would be similar
to this:
$ curl -H "token:6bde125560088033c618c2app234"
https://localhost:3790/rest_api/v2/workspaces/1/hosts
This request returns a JSONobject that contains all the hosts in the default workspace as shown below:
{
"id":2,
"created_at":"2015-04-07T13:02:26-07:00",
"address":"10.20.33.22",
"mac":"",
"comm":null,
"name":"host2",
"state":"alive",
"os_name":"",
"os_flavor":"",
"os_sp":"", "os_lang":null,
"arch":null, "workspace_id":1,
"updated_at":"2015-04-07T13:02:26-07:00",
"purpose":"", "info":null,
"comments":null,
"scope":null,
"virtual_host":null,
"note_count":0,
"vuln_count":0,
"service_count":0,
"host_detail_count":0,
"exploit_attempt_count":0,
"cred_count":0,
"nexpose_data_asset_id":null,
"history_count":0,
Requesting Data 3
"detected_arch":null
}
l Endpoints respond with an array of objects for an index request and a single object for a show request.
Finding IDs
To craft a request, you may need to know the ID for the resource from which you want to pull data. Most
commonly, you need to know the workspace and host ID.
To view the workspace ID, run the following to view all workspaces on a particular Metasploit server:
$ curl -H "token:6bde125560088033c618c2app234"
https://localhost:3790/rest_api/v2/workspaces
{
"id":1,
"name":"default",
"boundary":null,
"description":null,
"owner_id":null,
"limit_to_network":false,
"created_at":"2015-04-07T11:51:23-07:00",
"updated_at":"2015-04-07T11:51:23-07:00"
}
Find the 'id' field to identify the workspace ID. Then, to view a host ID, run the following:
$ curl -H "token:6bde125560088033c618c2app234"
https://localhost:3790/rest_api/v2/workspaces/1/hosts
Finding IDs 4
This requests the host index from workspace 1, which is the default workspace. You can replace the
workspace ID with any workspace you want. The request returns all hosts in a particular workspace and
their details, as shown below:
{
"id":1,
"created_at":"2015-04-07T13:02:26-07:00",
"address":"10.20.33.22",
"mac":"",
"comm":null,
"name":"host2",
"state":"alive",
"os_name":"",
"os_flavor":"",
"os_sp":"", "os_lang":null,
"arch":null, "workspace_id":1,
"updated_at":"2015-04-07T13:02:26-07:00",
"purpose":"", "info":null,
"comments":null,
"scope":null,
"virtual_host":null,
"note_count":0,
"vuln_count":0,
"service_count":0,
"host_detail_count":0,
"exploit_attempt_count":0,
"cred_count":0,
"nexpose_data_asset_id":null,
"history_count":0,
"detected_arch":null
}
The 'id' field in the example above represents the host ID.
API Endpoints
The RESTAPI provides access to the resources, such as hosts and sessions, available. Currently, you
can request a list of resources (index) or the details for a single resource (show), which is identified by an
ID.
Workspaces
API Endpoints 5
URI
/rest_api/v2/workspaces
Example
https://localhost:3790/rest_api/v2/workspaces
Parameters
l None
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1
Parameters
l id
API Endpoints 6
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l name
l boundary
l description
l owner_id
l limit_to_network
l created_at
l updated_at
Hosts
URI
/rest_api/v2/workspaces/:workspace_id/hosts
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts
Parameters
l workspace_id
API Endpoints 7
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1
Parameters
l workspace_id
l host_id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l workspace_id
l address
l mac
l comm
l name
l state
l os_name
l os_flavor
API Endpoints 8
l os_sp
l os_lang
l arch
l purpose
l info
l comments
l scope
l virtual_host
l note_count
l vuln_count
l service_count
l host_detail_count
l exploit_attempt_count
l cred_count
l nexpose_data_asset_id
l history_count
l detected_arch
l created_at
l updated_at
Notes
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/notes
API Endpoints 9
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/notes
Parameters
l workspace_id
l host_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/notes/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/notes/1
Parameters
l workspace_id
l host_id
l id
API Endpoints 10
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l ntype
l workspace_id
l vuln_id
l service_id
l host_id
l critical
l seen
l data
l created_at
l updated_at
Sessions
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/sessions
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/sessions
Parameters
API Endpoints 11
l workspace_id
l host_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/sessions/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/sessions/1
Parameters
l workspace_id
l host_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l host_id
l stype
l via_exploit
l via_payload
API Endpoints 12
l desc
l port
l platform
l datastore
l close_reason
l local_id
l module_run_id
l last_seen
l campaign_id
l opened_at
l closed_at
Services
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services
Parameters
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
API Endpoints 13
Service Show Request
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1
Parameters
l workspace_id
l host_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l host_id
l port
l state
l name
l info
l created_at
l updated_at
Vulns
API Endpoints 14
Vulnerabilities Index Request
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/vulns
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/vulns
Parameters
l workspace_id
l host_id
l service_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/vulns/:id
API Endpoints 15
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/vulns/1
Parameters
l workspace_id
l host_id
l service_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id Integer
l service_id
l host_id
l name
l info
l exploited_at
l vuln_detail_count
l vuln_attempt_count
l nexpose_data_vuln_def_id
l created_at
l updated_at
WebSites
API Endpoints 16
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites
Parameters
l workspace_id
l host_id
l service_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1
API Endpoints 17
Parameters
l workspace_id
l host_id
l service_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l service_id
l vhost
l comments
l options
l created_at
l updated_at
WebForms
You can create a web forms index request or a web form show request.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_forms
API Endpoints 18
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_forms
Parameters
l workspace_id
l host_id
l service_id
l web_site_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_forms/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_forms/1
Parameters
API Endpoints 19
l workspace_id
l host_id
l service_id
l web_site_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l web_site_id
l path
l method
l params
l query
l created_at
l updated_at
WebPages
You can create a web pages index request or a web page show request.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_pages
API Endpoints 20
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_pages
Parameters
l workspace_id
l host_id
l service_id
l web_site_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_pages/:id
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_pages/1
Parameters
API Endpoints 21
l workspace_id
l host_id
l service_id
l web_site_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l web_site_id
l path
l query
l code
l cookie
l auth
l ctype
l mtime
l location
l headers Array
l body
l request
l created_at
l updated_at
WebVulns
You can create a web vulnerabilities index request or a web vulnerability show request.
API Endpoints 22
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_vulns
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_pages/web_vulns
Parameters
l workspace_id
l host_id
l service_id
l web_site_id
Response
A successful call to this endpoint returns a 200 OK response and a JSON object.
URI
/rest_api/v2/workspaces/:workspace_id/hosts/:host_id/services/:service_
id/web_sites/:web_site_id/web_vulns/:id
API Endpoints 23
Example
https://localhost:3790/rest_api/v2/workspaces/1/hosts/1/services/1/web_
sites/1/web_pages/web_vulns/1
Parameters
l workspace_id
l host_id
l service_id
l web_site_id
l id
Response
A successful call to this endpoint returns a 200 OK response and the following fields:
l id
l web_site_id
l path
l method
l params
l pname
l risk
l name
l query
l legacy_category
l confidence
l description
l blame
l request
l owner
API Endpoints 24
l payload
l request_id
l category_id
l created_at
l updated_at
RESTAPIExample
#
# Trivial example of using the REST-based API
#
#
begin
require 'json' # provides serialization of Ruby data structures to
and from JSON format
require 'rest-client' # super-friendly HTTP access
rescue LoadError
puts "please install deps:\n"
puts "gem install json"
puts "gem install rest-client"
end
class MetasploitRestClient
attr_reader :token
def initialize(opts)
@token = opts.fetch(:token)
end
token = ARGV[0]
client = MetasploitRestClient.new(token:token)
RESTAPIExample 25