/modules/recon/hosts/geo/http/api/ipinfodb.py

https://bitbucket.org/LaNMaSteR53/recon-ng · Python · 51 lines · 42 code · 6 blank · 3 comment · 8 complexity · e6d143634eb6cbb3202884ce4d7f376d MD5 · raw file

  1. import framework
  2. # unique to module
  3. import json
  4. class Module(framework.module):
  5. def __init__(self, params):
  6. framework.module.__init__(self, params)
  7. self.register_option('source', 'db', 'yes', 'source of addresses for module input (see \'info\' for options)')
  8. self.info = {
  9. 'Name': 'IPInfoDB GeoIP',
  10. 'Author': 'Tim Tomes (@LaNMaSteR53)',
  11. 'Description': 'Leverages the ipinfodb.com API to geolocate the given host(s) by IP address and updates the \'hosts\' table of the database with the results.',
  12. 'Comments': [
  13. 'Source options: [ db | <address> | ./path/to/file | query <sql> ]'
  14. ]
  15. }
  16. def module_run(self):
  17. api_key = self.get_key('ipinfodb_api')
  18. hosts = self.get_source(self.options['source']['value'], 'SELECT DISTINCT ip_address FROM hosts WHERE ip_address IS NOT NULL')
  19. for host in hosts:
  20. # request the scan
  21. url = 'http://api.ipinfodb.com/v3/ip-city/?key=%s&ip=%s&format=json' % (api_key, host)
  22. self.verbose('URL: %s' % url)
  23. resp = self.request(url)
  24. if resp.json: jsonobj = resp.json
  25. else:
  26. self.error('Invalid JSON response for \'%s\'.\n%s' % (host, resp.text))
  27. continue
  28. if jsonobj['statusCode'].lower() == 'error':
  29. self.error(jsonobj['statusMessage'])
  30. continue
  31. if self.options['source']['value'] == 'db':
  32. location = []
  33. for name in ['cityName', 'regionName']:
  34. if jsonobj[name]: location.append(str(jsonobj[name]).title())
  35. data = [', '.join(location)]
  36. data.append(jsonobj['countryName'].title())
  37. data.append(str(jsonobj['latitude']))
  38. data.append(str(jsonobj['longitude']))
  39. data.append(host)
  40. self.query('UPDATE hosts SET region=\'%s\', country=\'%s\', latitude=\'%s\', longitude=\'%s\' WHERE ip_address=\'%s\'' % tuple(data))
  41. tdata = [['Host Info', 'Value']]
  42. for key in jsonobj:
  43. tdata.append([key, jsonobj[key]])
  44. # output the results in table format
  45. self.table(tdata, True)