The official Python library for Shodan

Overview

shodan: The official Python library and CLI for Shodan

Shodan is a search engine for Internet-connected devices. Google lets you search for websites, Shodan lets you search for devices. This library provides developers easy access to all of the data stored in Shodan in order to automate tasks and integrate into existing tools.

Features

https://cli.shodan.io/img/shodan-cli-preview.png

Quick Start

from shodan import Shodan

api = Shodan('MY API KEY')

# Lookup an IP
ipinfo = api.host('8.8.8.8')
print(ipinfo)

# Search for websites that have been "hacked"
for banner in api.search_cursor('http.title:"hacked by"'):
    print(banner)

# Get the total number of industrial control systems services on the Internet
ics_services = api.count('tag:ics')
print('Industrial Control Systems: {}'.format(ics_services['total']))

Grab your API key from https://account.shodan.io

Installation

To install the Shodan library, simply:

$ pip install shodan

Or if you don't have pip installed (which you should seriously install):

$ easy_install shodan

Documentation

Documentation is available at https://shodan.readthedocs.org/ and https://help.shodan.io

Comments
  • move config dir to ~/.config

    move config dir to ~/.config

    In reference to #135 I hope this fix is ok with you guys. I notice you didn't have any logic code in settings.py, but I feel as though this is the easiest way to implement this. This method allows users who create the ~/.config/shodan directory to utilise it, while still providing support for the users who do not care to move the folder.

    I also ran tox and got

    python: commands succeeded
      congratulations :)
    

    Not sure if that's how to run the tests

    opened by Soneji 11
  • Unable to connect to shodan

    Unable to connect to shodan

    Me and at least one other person are having trouble using this api recently. I use it in a tool here: https://github.com/DanMcInerney/device-pharmer

    I am getting shodan results exactly as the docs for this API suggest to do it, namely:

    api = shodan.Shodan(API_KEY)
    results = api.search(search, page=1)
    

    Yet even when I use my own paid-for API key, I'm getting "Unable to connect to Shodan" errors. Any idea what's going wrong?

    opened by DanMcInerney 8
  • Error: Unable to parse JSON response

    Error: Unable to parse JSON response

    Hi, there. I run the sample code in your python docs pages. However, it returned an error.

    Error: Unable to parse JSON response
    

    Most of time the api will return errors although sometimes it can successfully execute. And I really don't know why. My os is Win10 and I also tried it at Win7. Doesn't work too.

    opened by ghost 7
  • Blank page when using Search option in CLI

    Blank page when using Search option in CLI

    Using the Host option works as expected. However, when I try to use Search to narrow down on a specific set of fields, all I get is an empty (blank) page with no data. At the bottom left corner, the page shows "lines 1-1/1 (END)".

    opened by razermouse 6
  • module has no object Shodan

    module has no object Shodan

    In the implementation of the basic search example, using shodan.Shodan(SHODAN_API_KEY) throws an error. I think it should be shodan.WebAPI(SHODAN_API_KEY)

    opened by wandeg 6
  • Unicode Error

    Unicode Error

    Hey there, i hope that this is the right repo for my issue. I´m using the Shodan CLI and installed it with "pip install shodan". Im using the following command for requesting hosts:

    shodan host <ip address>
    

    However, when there is an umlaut character in the city name (in this case 'Düsseldorf'), the output is broken and the following error message is shown:

    shodan host REDACTED
    REDACTED
    Hostnames:               REDACTED.com
    Traceback (most recent call last):
      File "/usr/local/bin/shodan", line 11, in <module>
        sys.exit(main())
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 722, in __call__
        return self.main(*args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 697, in main
        rv = self.invoke(ctx)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 1066, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
        return callback(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/shodan/__main__.py", line 479, in host
        click.echo('{:25s}{}'.format('City:', host['city']))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)
    
    bug 
    opened by whoot 5
  • API Error only when using Radar

    API Error only when using Radar

    I receive the following success message after performing shodan init {MY_API_KEY}:

    Successfully initialized
    

    And can run all commands I've tested without issue, save for shodan radar, which returns the following error:

    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/site-packages/shodan/cli/worldmap.py", line 205, in fetch_data
        for banner in self.api.stream.banners():
      File "/usr/local/lib/python3.7/site-packages/shodan/stream.py", line 99, in banners
        stream = self._create_stream('/shodan/banners', timeout=timeout)
      File "/usr/local/lib/python3.7/site-packages/shodan/stream.py", line 50, in _create_stream
        raise APIError(data['error'])
    shodan.exception.APIError: Access denied
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/bin/shodan", line 11, in <module>
        load_entry_point('shodan==1.8.1', 'console_scripts', 'shodan')()
      File "/usr/local/lib/python3.7/site-packages/click/core.py", line 722, in __call__
        return self.main(*args, **kwargs)
      File "/usr/local/lib/python3.7/site-packages/click/core.py", line 697, in main
        rv = self.invoke(ctx)
      File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/usr/local/lib/python3.7/site-packages/click/core.py", line 895, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/usr/local/lib/python3.7/site-packages/click/core.py", line 535, in invoke
        return callback(*args, **kwargs)
      File "/usr/local/lib/python3.7/site-packages/shodan/__main__.py", line 1331, in radar
        launch_map(api)
      File "/usr/local/lib/python3.7/site-packages/shodan/cli/worldmap.py", line 242, in launch_map
        return curses.wrapper(app.run)
      File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/curses/__init__.py", line 94, in wrapper
        return func(stdscr, *args, **kwds)
      File "/usr/local/lib/python3.7/site-packages/shodan/cli/worldmap.py", line 222, in run
        refresh = self.fetch_data(now)
      File "/usr/local/lib/python3.7/site-packages/shodan/cli/worldmap.py", line 212, in fetch_data
        except StandardError:
    NameError: name 'StandardError' is not defined
    

    I'm running macOS 10.13.6 and Python 3.7.

    Also tried to install with Python 2.7 on a raspberry pi with a fully updated Raspbian release, and received this error:

    Traceback (most recent call last):
      File "/usr/local/bin/shodan", line 11, in <module>
        load_entry_point('shodan==1.8.1', 'console_scripts', 'shodan')()
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 716, in __call__
        return self.main(*args, **kwargs)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 696, in main
        rv = self.invoke(ctx)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 1060, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 889, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/usr/lib/python2.7/dist-packages/click/core.py", line 534, in invoke
        return callback(*args, **kwargs)
      File "build/bdist.linux-armv7l/egg/shodan/__main__.py", line 1331, in radar
      File "build/bdist.linux-armv7l/egg/shodan/cli/worldmap.py", line 242, in launch_map
      File "/usr/lib/python2.7/curses/wrapper.py", line 43, in wrapper
        return func(stdscr, *args, **kwds)
      File "build/bdist.linux-armv7l/egg/shodan/cli/worldmap.py", line 222, in run
      File "build/bdist.linux-armv7l/egg/shodan/cli/worldmap.py", line 205, in fetch_data
      File "build/bdist.linux-armv7l/egg/shodan/stream.py", line 99, in banners
      File "build/bdist.linux-armv7l/egg/shodan/stream.py", line 50, in _create_stream
    shodan.exception.APIError: Access denied
    

    Regenerating my API key did not help, nor did running the init command with sudo.

    opened by seajaysec 5
  • .json() doesn't work in some cases

    .json() doesn't work in some cases

    The .json() is in some cases not callable, when removing the () it will work..

    See: http://love-python.blogspot.nl/2013/03/dict-object-is-not-callable-on-json.html

    opened by Wouter0100 5
  • ImportError: cannot import name 'Shodan' from 'shodan'

    ImportError: cannot import name 'Shodan' from 'shodan'

    When the following code is executed in visual studio code

    from shodan import Shodan api = Shodan('my-api-key') ipinfo = api.host('8.8.8.8') print(ipinfo)

    it throws the following error ImportError: cannot import name 'Shodan' from 'shodan'

    Please help

    PIP LIST Package Version


    aniso8601 9.0.1 certifi 2021.5.30 charset-normalizer 2.0.4 click 8.0.1 click-plugins 1.1.1 colorama 0.4.4 Flask 2.0.1 Flask-RESTful 0.3.9 idna 3.2 itsdangerous 2.0.1 Jinja2 3.0.1 MarkupSafe 2.0.1 pip 21.2.3 pytz 2021.1 requests 2.26.0 setuptools 41.2.0 shodan 1.25.0 six 1.16.0 urllib3 1.26.6 Werkzeug 2.0.1 XlsxWriter 2.0.0

    opened by omega71044 4
  • Add

    Add "net:ip/mask" filter for shodan parse

    A simple implementation of the net:filter used in web GUI.

    It uses python3's builtin ipaddress lib and only imports those libs if the net:filter is used.

    Example:

    $ shodan download example 'country:fi cyber'
    
    $ shodan parse example.json.gz  | cut -c1-100
    95.216.39.52	53	srv01.cyber-cortex.net	PowerDNS Authoritative Server 4.1.1\nResolver ID: srv01.cyber
    95.216.52.222	2083	static.222.52.216.95.clients.your-server.de	HTTP/1.1 200 OK\r\nDate: Sun, 15 Sep 
    
    $ shodan parse -f net:95.216.39.0/24 example.json.gz  | cut -c1-100
    95.216.39.52	53	srv01.cyber-cortex.net	PowerDNS Authoritative Server 4.1.1\nResolver ID: srv01.cyber
    
    $ shodan parse -f net:95.216.0.0/16 example.json.gz  | cut -c1-100
    95.216.39.52	53	srv01.cyber-cortex.net	PowerDNS Authoritative Server 4.1.1\nResolver ID: srv01.cyber
    95.216.52.222	2083	static.222.52.216.95.clients.your-server.de	HTTP/1.1 200 OK\r\nDate: Sun, 15 Sep 
    
    opened by unfo 4
  • ValueError: invalid literal for int() with base 16: b'' when testing the 'private firehose' demo

    ValueError: invalid literal for int() with base 16: b'' when testing the 'private firehose' demo

    Hi,

    I was testing the Network alerts functionality when I ran across an error.

    I've now had the same error twice so it looks like it is repeatable:

    Traceback (most recent call last): File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 572, in _update_chunk_length self.chunk_left = int(line, 16) ValueError: invalid literal for int() with base 16: b''

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 331, in _error_catcher yield File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 637, in read_chunked self._update_chunk_length() File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 576, in _update_chunk_length raise httplib.IncompleteRead(line) http.client.IncompleteRead: IncompleteRead(0 bytes read)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "/root/shodan/venv/lib/python3.5/site-packages/requests/models.py", line 749, in generate for chunk in self.raw.stream(chunk_size, decode_content=True): File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 461, in stream for line in self.read_chunked(amt, decode_content=decode_content): File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 665, in read_chunked self._original_response.close() File "/usr/lib/python3.5/contextlib.py", line 77, in exit self.gen.throw(type, value, traceback) File "/root/shodan/venv/lib/python3.5/site-packages/urllib3/response.py", line 349, in _error_catcher raise ProtocolError('Connection broken: %r' % e, e) urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "monitornetwork_ports.py", line 17, in for banner in api.stream.alert(): File "/root/shodan/venv/lib/python3.5/site-packages/shodan/stream.py", line 79, in alert for line in self._iter_stream(stream, raw): File "/root/shodan/venv/lib/python3.5/site-packages/shodan/stream.py", line 63, in _iter_stream for line in stream.iter_lines(decode_unicode=True): File "/root/shodan/venv/lib/python3.5/site-packages/requests/models.py", line 793, in iter_lines for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode): File "/root/shodan/venv/lib/python3.5/site-packages/requests/utils.py", line 503, in stream_decode_response_unicode for chunk in iterator: File "/root/shodan/venv/lib/python3.5/site-packages/requests/models.py", line 752, in generate raise ChunkedEncodingError(e) requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

    opened by swedishmike 4
  • Error: Unable to connect to Shodan

    Error: Unable to connect to Shodan

    on trying shodan init <api_key> on my Kali Linux machine, I am getting error Error: Unable to connect to Shodan. -> updated the requests library, still the issue persists. shodan_error

    opened by harshinsecurity 1
  • Allow users to specify certificate bundles or ssl verification = False

    Allow users to specify certificate bundles or ssl verification = False

    In requests, users can specify on requests the "verify" variable as either a path to a certificate bundle, or the boolean value False for actually verify certificates.

    Allowing the user to specify this upon connection would be very useful for those who may use a corporate proxy and need to run a separate cert bundle or are lazy and just set it to False. By instantiating the requests inside the client Class, it hides that option for the user. Maybe even just providing an option kwargs would be sufficient. (incase you want to use any other options of requests).

    I looked at the code, and was considering offering a pull request, but I see requests being used in many of the various python files here. Sometime it's used by instantiating a requests.Session (which you can specify the verify value on so all subsequent requests use that) or you can just pass it to the .request (or the .get, or .post etc) methods for those modules that don't use the requests.Session. Since the usage is different through out the repo, I thought I'd raise an issue and request (no pun intended) someone more knowledgeable of the package itself update it.

    opened by JohnOmernik 0
  • Distribution error on Mac

    Distribution error on Mac

    easy_install shodan

      File "/usr/bin/easy_install", line 13, in <module>
        load_entry_point('setuptools==41.0.1', 'console_scripts', 'easy_install')()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/command/easy_install.py", line 2316, in main
        **kw
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/__init__.py", line 145, in setup
        return distutils.core.setup(**attrs)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 124, in setup
        dist.parse_config_files()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/dist.py", line 698, in parse_config_files
        ignore_option_errors=ignore_option_errors)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 115, in parse_configuration
        options.parse()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 425, in parse
        section_parser_method(section_options)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 398, in parse_section
        self[name] = value
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 183, in __setitem__
        value = parser(value)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 574, in _parse_packages
        'find_namespace: directive is unsupported on Python < 3.3')
    distutils.errors.DistutilsOptionError: find_namespace: directive is unsupported on Python < 3.3```
    
    
    
    
    Dependency distribution error 
    

    easy_install xlsxwriter Traceback (most recent call last): File "/usr/bin/easy_install", line 13, in load_entry_point('setuptools==41.0.1', 'console_scripts', 'easy_install')() File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/command/easy_install.py", line 2316, in main **kw File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/init.py", line 145, in setup return distutils.core.setup(**attrs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 124, in setup dist.parse_config_files() File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/dist.py", line 698, in parse_config_files ignore_option_errors=ignore_option_errors) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 115, in parse_configuration options.parse() File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 425, in parse section_parser_method(section_options) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 398, in parse_section self[name] = value File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 183, in setitem value = parser(value) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/config.py", line 574, in _parse_packages 'find_namespace: directive is unsupported on Python < 3.3') distutils.errors.DistutilsOptionError: find_namespace: directive is unsupported on Python < 3.3

    opened by sherlocksecurity 1
  • JSON output to console

    JSON output to console

    I considered doing this as a PR but I'm not sure whether:

    1. It would be accepted
    2. How best to do this while retaining the colouring // style of other output types

    Essentially for both the search and host command I think it would be useful to be able to just immediately print the JSON to the console so that it could be read // manipulated via jq, e.g.

    shodan host 1.2.3.4 --format json | jq <some jq syntax>
    

    Would something like this be acceptable?

    def host_print_json(host, history=False):
        click.echo(json.dumps(host['data'], sort_keys=True, indent=4))
    
    opened by tlansec 0
  • Keep specific hostname when adding domain-based network alert

    Keep specific hostname when adding domain-based network alert

    If a user runs a command such as:

    shodan alert domain www.shodan.io
    

    Then the CLI will create a domain-based network alert for shodan.io. However, it should create a network alert for just www.shodan.io which is how the Shodan Monitor website operates.

    opened by achillean 0
Releases(1.28.0)
🐍 The official Python client library for Google's discovery based APIs.

Google API Client This is the Python client library for Google's discovery based APIs. To get started, please see the docs folder. These client librar

Google APIs 6.2k Jan 8, 2023
The official Python client library for the Kite Connect trading APIs

The Kite Connect API Python client - v3 The official Python client for communicating with the Kite Connect API. Kite Connect is a set of REST-like API

Zerodha Technology 756 Jan 6, 2023
The Official Twilio SendGrid Led, Community Driven Python API Library

The default branch name for this repository has been changed to main as of 07/27/2020. This library allows you to quickly and easily use the SendGrid

Twilio SendGrid 1.4k Jan 7, 2023
🐍 The official Python client library for Google's discovery based APIs.

Google API Client This is the Python client library for Google's discovery based APIs. To get started, please see the docs folder. These client librar

Google APIs 6.2k Dec 31, 2022
Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Harun Mbaabu Mwenda 46 Sep 1, 2022
The official Python library for the Plutto API

Plutto Ruby SDK This library will help you easily integrate Plutto API to your software, making your developer life a little bit more enjoyable. Insta

Plutto 3 Nov 23, 2021
A reddit.com bot that will return reference links from official python documentation site for the standard library.

Python Docs Bot A reddit.com bot that will return documentation links for the library and language reference sections of the python docs website. The

Trevor Miller 2 Sep 14, 2021
Aio-binance-library - Async library for connecting to the Binance API on Python

aio-binance-library Async library for connecting to the Binance API on Python Th

GRinvest 10 Nov 21, 2022
Official Python client for the MonkeyLearn API. Build and consume machine learning models for language processing from your Python apps.

MonkeyLearn API for Python Official Python client for the MonkeyLearn API. Build and run machine learning models for language processing from your Pyt

MonkeyLearn 157 Nov 22, 2022
DEPRECATED - Official Python Client for the Discogs API

⚠️ DEPRECATED This repository is no longer maintained. You can still use a REST client like Requests or other third-party Python library to access the

Discogs 483 Dec 31, 2022
The Official Dropbox API V2 SDK for Python

The offical Dropbox SDK for Python. Documentation can be found on Read The Docs. Installation Create an app via the Developer Console. Install via pip

Dropbox 828 Jan 5, 2023
Unofficial Python wrapper for official Hacker News API

haxor Unofficial Python wrapper for official Hacker News API. Installation pip install haxor Usage Import and initialization: from hackernews import H

null 147 Sep 18, 2022
Official python API for Phish.AI public and private API to detect zero-day phishing websites

phish-ai-api Summary Official python API for Phish.AI public and private API to detect zero-day phishing websites How it Works (TLDR) Essentially we h

Phish.AI 168 May 17, 2022
The official wrapper for spyse.com API, written in Python, aimed to help developers build their integrations with Spyse.

Python wrapper for Spyse API The official wrapper for spyse.com API, written in Python, aimed to help developers build their integrations with Spyse.

Spyse 15 Nov 22, 2022
Official Python wrapper for the Quantel Finance API

Quantel is a powerful financial data and insights API. It provides easy access to world-class financial information. Quantel goes beyond just financial statements, giving users valuable information like insider transactions, major shareholder transactions, share ownership, peers, and so much more.

Guy 47 Oct 16, 2022
Asynchronous and also synchronous non-official QvaPay client for asyncio and Python language.

Asynchronous and also synchronous non-official QvaPay client for asyncio and Python language. This library is still under development, the interface could be changed.

Leynier Gutiérrez González 8 Sep 18, 2021
The official Discord Python framework for thenewboston blockchain.

Project Setup Follow the steps below to set up the project on your environment. Mac Setup Homebrew requires the Xcode command-line tools from Apple's

Bucky Roberts 18 Jul 15, 2022
A pixeldrain python package using pixeldrain official api

Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License -> https://github.com/FayasNoushad/Pixeldrain/blob/main/LICENSE In

Fayas Noushad 6 Jan 26, 2022
The Python version of the official Discord bot for the Astura Studios Discord community server.

About Astura (Python version) is the official Discord bot for the Astura Studios Discord community server developed and maintained by Ascendus and the

Ascendus 1 Apr 21, 2022