|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +import harvester.core as core |
| 8 | + |
| 9 | +# Default input file with queries |
| 10 | +core.QUERIES_JSON_FILE = "shodan_queries.json" |
| 11 | + |
| 12 | +# Default confidence level |
| 13 | +core.DEFAULT_CONFIDENCE = "certain" |
| 14 | + |
| 15 | +# Default quantity of results |
| 16 | +core.MAX_COUNTRIES = 10 |
| 17 | +core.MAX_VENDORS = 10 |
| 18 | +core.MAX_VULNERS = 10 |
| 19 | + |
| 20 | +# Default paths and directories |
| 21 | +core.NMAP_SCRIPTS_PATH = "nse-scripts" |
| 22 | +core.RESULTS_DIR = "results" |
| 23 | + |
| 24 | + |
| 25 | +def get_key_from_env(): |
| 26 | + """ |
| 27 | + Get Shodan API Key from environment variable |
| 28 | +
|
| 29 | + :return: Shodan API key from env variable (str) |
| 30 | + """ |
| 31 | + try: |
| 32 | + shodan_api_key = os.environ['SHODAN_API_KEY'] |
| 33 | + return shodan_api_key |
| 34 | + except KeyError: |
| 35 | + print( |
| 36 | + 'Please set the environment variable SHODAN_API_KEY or use -sk key') |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | + |
| 40 | +def main(): |
| 41 | + """ |
| 42 | + Main interface for harvester core |
| 43 | +
|
| 44 | + :return: None |
| 45 | + """ |
| 46 | + if sys.version_info < (3, 6): |
| 47 | + print('Required python version is 3.6 or greater') |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + if len(sys.argv) == 1: |
| 51 | + print( |
| 52 | + "Usage: '{script_name} -h' for help".format( |
| 53 | + script_name=sys.argv[0])) |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + parser = argparse.ArgumentParser(description=".") |
| 57 | + parser.add_argument("-sk", "--shodan-key", action="store", |
| 58 | + default=None, help="Shodan API key") |
| 59 | + parser.add_argument("-n", "--new", action="store_true", |
| 60 | + help="New scan in shodan") |
| 61 | + parser.add_argument("-q", "--queries", action="store", |
| 62 | + default=core.QUERIES_JSON_FILE, |
| 63 | + help="File with queries") |
| 64 | + parser.add_argument("-d", "--destination", action="store", |
| 65 | + default=core.RESULTS_DIR, help="Destination directory") |
| 66 | + parser.add_argument("-c", "--confidence", default=core.DEFAULT_CONFIDENCE, |
| 67 | + action="store", help="""Confidence level. Available |
| 68 | + levels: certain, firm, |
| 69 | + tentative""") |
| 70 | + parser.add_argument("-v", "--vulners", action="store", nargs='*', |
| 71 | + help="""List of vendors for vulners scan, e.g., |
| 72 | + '--vulners silver peak, arista, talari'. |
| 73 | + Use '--vulners all' to include all vendors |
| 74 | + in statistics.""") |
| 75 | + parser.add_argument("-mv", "--max-vendors", default=core.MAX_VENDORS, type=int, |
| 76 | + action="store", |
| 77 | + help="Max number of vendors in statistics") |
| 78 | + parser.add_argument("-mc", "--max-countries", default=core.MAX_COUNTRIES, |
| 79 | + type=int, |
| 80 | + action="store", |
| 81 | + help="Max number of countries in statistics") |
| 82 | + parser.add_argument("-maxv", "--max-vulners", default=core.MAX_VULNERS, |
| 83 | + type=int, action="store", |
| 84 | + help="Max number of vulners in statistics") |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + # Try to get key from environment if it was not passed with CLI |
| 88 | + if not args.shodan_key and args.new is True: |
| 89 | + args.shodan_key = get_key_from_env() |
| 90 | + |
| 91 | + # Check confidence level |
| 92 | + if args.confidence.lower() not in ['certain', 'firm', 'tentative']: |
| 93 | + print('Wrong confidence level. Use -h key for help.') |
| 94 | + sys.exit(1) |
| 95 | + |
| 96 | + # Run harvester |
| 97 | + core.run(args) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments