Skip to Content
Network Services

Query Internet Exposure with Shodan and cnspec

Use cnspec with Shodan to discover internet-facing services and open ports.

Discover internet-facing services, open ports, and potential exposure on your hosts with cnspec and Shodan. Scan with the Mondoo Shodan Security policy, or use the Shodan provider inside your own policies to alert on unexpected open ports or banner strings against assets you control.

Shodan is one of several ways cnspec scans networks and hosts. For the full list, see the networks and hosts overview. New to cnspec? Start with the quickstart.

Prerequisites

To query Shodan with cnspec, you must have:

Authenticate

Set your Shodan API key as an environment variable:

export SHODAN_TOKEN="your-api-key"

To retrieve your API key, go to your Shodan Account Overview.

Scan a host with Shodan

cnspec scan shodan host HOST

For HOST, substitute the domain name or IP address. For example:

cnspec scan shodan host example.com

To assess a domain and the hosts Shodan associates with it, use the domain sub-command instead:

cnspec scan shodan domain example.com

To scan each host Shodan knows about in one or more IP ranges as its own asset, pass the ranges with --networks and add --discover hosts:

cnspec scan shodan --networks 203.0.113.0/24 --discover hosts

Connection options

OptionDescription
--tokenShodan API token
--networksComma-separated list of IP ranges to query, for example 203.0.113.0/24,198.51.100.0/24. cnspec skips any range larger than a /16 (65,536 addresses).

Scan with the Mondoo Shodan Security policy

Mondoo maintains an out-of-the-box Mondoo Shodan Security policy that assesses what Shodan reports about your assets:

  • Hosts (shodan host, and each host found with --discover hosts): no open ports and no known vulnerabilities (CVEs). These checks run only when the host is an IPv4 address, so pass an IPv4 address to shodan host rather than a hostname.
  • Domains (shodan domain): none of the hosts the domain and its subdomains resolve to has known vulnerabilities, and the domain has at least two nameservers
  • The Shodan account (cnspec scan shodan with no sub-command): the account is a Shodan member

Mondoo Platform users: Enable the policy in your space. In the Mondoo App, go to Findings > Policies, search for "Shodan", and add the policy. To learn more, read Manage policies in Mondoo Platform.

Open source users: Pass the policy bundle URL directly to cnspec:

cnspec scan shodan host 203.0.113.10 \
  --policy-bundle https://raw.githubusercontent.com/mondoohq/cnspec/refs/heads/main/content/mondoo-shodan.mql.yaml

Explore and test checks interactively

Open a cnspec shell to query Shodan data for a host:

cnspec shell shodan host example.com

List open ports on a host

cnspec> shodan.host.ports
shodan.host.ports: [80, 443, 8080]

Retrieve the organization associated with a host

cnspec> shodan.host.org
shodan.host.org: "Example Corp"

To exit the cnspec shell, press Ctrl + D or type exit.

Continuously scan with Mondoo Platform

Go beyond one-off scans. Use Mondoo Platform to continuously monitor hosts with Shodan. To learn more, read Secure a Host with Shodan.

Learn more

Last updated on

On this page