Skip to content

Commit

Permalink
shodan functions to a lib and new habu.shodan.open
Browse files Browse the repository at this point in the history
  • Loading branch information
fportantier committed Dec 13, 2018
1 parent 9c5c0ef commit 9bc4db0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 21 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,29 @@ Options:
```


## habu.shodan.open


``` {.sourceCode .bash}
Usage: habu.shodan.open [OPTIONS] IP
Output the open ports for an IP against shodan (nmap format).
Example:
$ habu.shodan.open 8.8.8.8
T:53,U:53
Options:
-c Disable cache
-j Output in JSON format
-x Output an nmap command to scan open ports
-v Verbose output
-o FILENAME Output file (default: stdout)
--help Show this message and exit.
```


## habu.shodan


Expand Down
22 changes: 2 additions & 20 deletions habu/cli/cmd_shodan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import sys

import click
import requests
import requests_cache

from habu.lib.loadcfg import loadcfg

from habu.lib.shodan import shodan_get_result

@click.command()
@click.argument('ip')
Expand Down Expand Up @@ -62,23 +60,7 @@ def cmd_shodan(ip, no_cache, verbose, output):
if verbose:
logging.basicConfig(level=logging.INFO, format='%(message)s')

if not no_cache:
homedir = pwd.getpwuid(os.getuid()).pw_dir
requests_cache.install_cache(homedir + '/.habu_requests_cache')

url = 'https://api.shodan.io/shodan/host/{}?key={}'.format(ip, habucfg['SHODAN_APIKEY'])

r = requests.get(url)

if r.status_code not in [200, 404]:
print('ERROR', r)
return False

if r.status_code == 404:
print("Not Found")
return False

data = r.json()
data = shodan_get_result(ip, habucfg['SHODAN_APIKEY'], no_cache, verbose)

output.write(json.dumps(data, indent=4))
output.write('\n')
Expand Down
63 changes: 63 additions & 0 deletions habu/cli/cmd_shodan_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

import json
import logging
import os
import os.path
import pwd
import sys

import click

from habu.lib.loadcfg import loadcfg
from habu.lib.shodan import shodan_get_result

@click.command()
@click.argument('ip')
@click.option('-c', 'no_cache', is_flag=True, default=False, help='Disable cache')
@click.option('-j', 'json_output', is_flag=True, default=False, help='Output in JSON format')
@click.option('-x', 'nmap_command', is_flag=True, default=False, help='Output an nmap command to scan open ports')
@click.option('-v', 'verbose', is_flag=True, default=False, help='Verbose output')
@click.option('-o', 'output', type=click.File('w'), default='-', help='Output file (default: stdout)')
def cmd_shodan_open(ip, no_cache, json_output, nmap_command, verbose, output):
"""Output the open ports for an IP against shodan (nmap format).
Example:
\b
$ habu.shodan.open 8.8.8.8
T:53,U:53
"""

habucfg = loadcfg()

if 'SHODAN_APIKEY' not in habucfg:
print('You must provide a shodan apikey. Use the ~/.habu.json file (variable SHODAN_APIKEY), or export the variable HABU_SHODAN_APIKEY')
print('Get your API key from https://www.shodan.io/')
sys.exit(1)

if verbose:
logging.basicConfig(level=logging.INFO, format='%(message)s')

data = shodan_get_result(ip, habucfg['SHODAN_APIKEY'], no_cache, verbose)
ports = []

if 'data' in data:
for service in data['data']:
ports.append('{}:{}'.format(
service['transport'][0].upper(),
service['port']
))

if nmap_command:
if ports:
output.write('nmap -A -v -p {} {}'.format(','.join(ports), ip))
else:
if json_output:
output.write(json.dumps(ports, indent=4))
output.write('\n')
else:
output.write(','.join(ports))

if __name__ == '__main__':
cmd_shodan_open()
34 changes: 34 additions & 0 deletions habu/lib/shodan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

import logging
import os
import os.path
import pwd

import requests
import requests_cache


def shodan_get_result(ip, api_key, no_cache=False, verbose=False):

if verbose:
logging.basicConfig(level=logging.INFO, format='%(message)s')

if not no_cache:
homedir = pwd.getpwuid(os.getuid()).pw_dir
requests_cache.install_cache(homedir + '/.habu_requests_cache')

url = 'https://api.shodan.io/shodan/host/{}?key={}'.format(ip, api_key)

r = requests.get(url)

if r.status_code not in [200, 404]:
logging.error(str(r))
return {}

if r.status_code == 404:
return {}

data = r.json()

return data
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='habu',
version='0.0.91',
version='0.0.92',
description='Python Network Hacking Toolkit',
long_description=readme,
long_description_content_type='text/markdown',
Expand Down Expand Up @@ -74,6 +74,7 @@
habu.protoscan=habu.cli.cmd_protoscan:cmd_protoscan
habu.server.ftp=habu.cli.cmd_server_ftp:cmd_server_ftp
habu.shodan=habu.cli.cmd_shodan:cmd_shodan
habu.shodan.open=habu.cli.cmd_shodan_open:cmd_shodan_open
habu.synflood=habu.cli.cmd_synflood:cmd_synflood
habu.tcpflags=habu.cli.cmd_tcpflags:cmd_tcpflags
habu.tcpscan=habu.cli.cmd_tcpscan:cmd_tcpscan
Expand Down

0 comments on commit 9bc4db0

Please sign in to comment.