|
10 | 10 | from lib.utils.config import ConfigFileParser
|
11 | 11 |
|
12 | 12 |
|
13 |
| -def _readKey(): |
14 |
| - msg = 'Trying to auth with credentials in config file: %s.' % paths.CONFIG_PATH |
15 |
| - logger.info(msg) |
16 |
| - try: |
17 |
| - key = ConfigFileParser().ShodanApikey() |
18 |
| - except: |
19 |
| - key = '' |
20 |
| - return key |
| 13 | +class ShodanBase: |
| 14 | + def __init__(self, query, limit, offset): |
| 15 | + self.query = query |
| 16 | + self.limit = limit |
| 17 | + self.offset = offset |
| 18 | + self.api_key = None |
| 19 | + self.result = None |
21 | 20 |
|
| 21 | + def login(self): |
| 22 | + msg = 'Trying to login with credentials in config file: %s.' % paths.CONFIG_PATH |
| 23 | + logger.info(msg) |
| 24 | + self.api_key = ConfigFileParser().ShodanApikey() |
22 | 25 |
|
23 |
| -def ShodanSearch(query, limit, offset=0): |
24 |
| - key = _readKey() |
25 |
| - try: |
26 |
| - api = shodan.Shodan(key) |
27 |
| - result = api.search(query=query, offset=offset, limit=limit) |
28 |
| - except APIError: |
29 |
| - msg = 'Automatic authorization failed.' |
30 |
| - logger.warning(msg) |
31 |
| - key = raw_input('Input API-KEY >') |
| 26 | + if not self.api_key: |
| 27 | + msg = 'Automatic authorization failed.' |
| 28 | + logger.warning(msg) |
| 29 | + msg = 'Please input your Shodan API Key (https://account.shodan.io/).' |
| 30 | + logger.info(msg) |
| 31 | + self.api_key = raw_input('API KEY > ').strip() |
| 32 | + |
| 33 | + def account_info(self): |
| 34 | + try: |
| 35 | + api = shodan.Shodan(self.api_key) |
| 36 | + account_info = api.info() |
| 37 | + msg = "Available Shodan query credits: %d" % account_info['query_credits'] |
| 38 | + logger.info(msg) |
| 39 | + except APIError, e: |
| 40 | + sys.exit(logger.error(e)) |
| 41 | + return True |
| 42 | + |
| 43 | + def api_query(self): |
32 | 44 | try:
|
33 |
| - api = shodan.Shodan(key) |
34 |
| - result = api.search(query=query, offset=offset, limit=limit) |
35 |
| - except APIError: |
36 |
| - msg = 'Invalid Shodan API key.' |
37 |
| - sys.exit(logger.error(msg)) |
38 |
| - if 'matches' in result: |
39 |
| - anslist = [] |
40 |
| - for match in result['matches']: |
41 |
| - anslist.append(match['ip_str'] + ':' + str(match['port'])) |
42 |
| - return anslist |
43 |
| - else: |
44 |
| - return [] |
| 45 | + api = shodan.Shodan(self.api_key) |
| 46 | + result = api.search(query=self.query, offset=self.offset, limit=self.limit) |
| 47 | + except APIError, e: |
| 48 | + sys.exit(logger.error(e)) |
| 49 | + |
| 50 | + if 'matches' in result: |
| 51 | + anslist = [] |
| 52 | + for match in result['matches']: |
| 53 | + anslist.append(match['ip_str'] + ':' + str(match['port'])) |
| 54 | + self.result = anslist |
| 55 | + else: |
| 56 | + self.result = [] |
| 57 | + |
| 58 | + |
| 59 | +def ShodanSearch(query, limit, offset=0): |
| 60 | + s = ShodanBase(query, limit, offset) |
| 61 | + s.login() |
| 62 | + s.account_info() |
| 63 | + s.api_query() |
| 64 | + return s.result |
0 commit comments