-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shodan functions to a lib and new habu.shodan.open
- Loading branch information
1 parent
9c5c0ef
commit 9bc4db0
Showing
5 changed files
with
124 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters