Skip to content

Commit a3a67b6

Browse files
committed
[FIX] Incomplete Shodan-API error message.
1 parent 60c290c commit a3a67b6

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

lib/api/shodan/pack.py

+49-29
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,55 @@
1010
from lib.utils.config import ConfigFileParser
1111

1212

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
2120

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()
2225

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):
3244
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

lib/api/zoomeye/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def auto_login(self):
3939
def manual_login(self):
4040
msg = 'Please input your ZoomEye Email and Password below.'
4141
logger.info(msg)
42-
self.username = raw_input('ZoomEye Username(Email): ')
43-
self.password = getpass.getpass(prompt='ZoomEye Password: ')
42+
self.username = raw_input('ZoomEye Username(Email): ').strip()
43+
self.password = getpass.getpass(prompt='ZoomEye Password: ').strip()
4444
if not self.get_token():
4545
msg = 'Invalid ZoomEye username or password.'
4646
sys.exit(logger.error(msg))

0 commit comments

Comments
 (0)