__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. class Censys_API:
  5. def __init__(self, uid, secret):
  6. self.url = 'https://censys.io/api/v1'
  7. self.uid = uid
  8. self.secret = secret
  9. self.login()
  10. self.ipv4 = []
  11. def login(self):
  12. r = requests.get(self.url + "/data", auth=(self.uid, self.secret))
  13. if r.status_code != 200:
  14. print("Wrong creds for Censys")
  15. sys.exit(1)
  16. return True
  17. def build_query_ipv4(self, targets):
  18. query = ""
  19. for t in targets:
  20. query += "ip:[" + t['start'] + " TO " + t['end'] + "]"
  21. query += " OR "
  22. return query[:-4]
  23. def search_ipv4(self, query):
  24. r = requests.post(self.url + "/search/ipv4", json={'query': query}, auth=(self.uid, self.secret))
  25. data = r.json()
  26. self.parse_ipv4(data)
  27. if data['status'] == 'ok':
  28. count = data['metadata']['count']
  29. pages = data['metadata']['pages']
  30. for page in range(2, pages + 1):
  31. r = requests.post(self.url + "/search/ipv4", json={'query': query, 'page' : page}, auth=(self.uid, self.secret))
  32. data = r.json()
  33. self.parse_ipv4(data)
  34. return self.ipv4
  35. def parse_ipv4(self, data):
  36. for host in data['results']:
  37. r = requests.get(self.url + "/view/ipv4/" + host['ip'], auth=(self.uid, self.secret))
  38. data = r.json()
  39. try:
  40. vhosts = data['443']['https']['tls']['certificate']['parsed']['names']
  41. except:
  42. vhosts = []
  43. self.ipv4.append({'ip': host['ip'], 'protocols': host['protocols'], 'vhosts': vhosts})
  44. return True
  45. class Censys_WEB:
  46. def __init__(self, username, password):
  47. self.url = 'https://censys.io/'
  48. self.username = username
  49. self.password = password
  50. self.session = self.login()
  51. self.ipv4 = []
  52. def login(self):
  53. s = requests.session()
  54. r = s.get(self.url + "/login")
  55. html = BeautifulSoup(r.text, "lxml")
  56. csrf = html.find('input', {'name': 'csrf_token'})['value']
  57. r = s.post(self.url + "/login", data={'login': self.username, 'password': self.password, 'csrf_token': csrf, 'came_from': '/'}, allow_redirects=False)
  58. if r.status_code != 302:
  59. print("Wrong creds for Censys")
  60. return s
  61. def build_query_ipv4(self, targets):
  62. query = ""
  63. for t in targets:
  64. query += "ip:[" + t['start'] + " TO " + t['end'] + "]"
  65. query += " OR "
  66. return query[:-4]
  67. def search_ipv4(self, query):
  68. r = self.session.get(self.url + "ipv4/_search?q=", params={"q": query, "page": 1})
  69. data = r.text
  70. '''Per usare etree bisogna fixare l'html rotto
  71. data = "<root>" + data + "</root>"
  72. data = re.sub("\<a\ href=\/.*\>.*\<\/a\>", "", data)'''
  73. self.parse_ipv4(data)
  74. html = BeautifulSoup(data, "lxml")
  75. spans = html.find_all('span', {'class': 'SearchResultSectionHeader__statistic'})
  76. pages = int(spans[0].text.split('/')[1].strip())
  77. count = spans[1].text
  78. for page in range(2, pages + 1):
  79. r = self.session.get(self.url + "ipv4/_search?q=", params={"q": query, "page": page})
  80. data = r.text
  81. self.parse_ipv4(data)
  82. return self.ipv4
  83. def parse_ipv4(self, data):
  84. html = BeautifulSoup(data, "lxml")
  85. results = html.find_all('div', {'class': 'SearchResult result'})
  86. for raw in results:
  87. vhosts = []
  88. urls = []
  89. protocols = []
  90. ip = raw.find_all('span', {'class': 'dns'})[0].get('id')
  91. vhosts_html = raw.find_all('i', {'title': 'names on certificate'})
  92. if vhosts_html:
  93. l = vhosts_html[0].next_sibling.replace(' ', '')
  94. for vhost in l.split(','):
  95. vhosts.append(vhost)
  96. protocols_html = raw.find_all('i', {'title': 'public protocols'})
  97. if protocols_html:
  98. l = protocols_html[0].next_sibling.replace(' ', '')
  99. for protocol in l.split(','):
  100. protocols.append(protocol)
  101. self.ipv4.append({'ip': ip, 'protocols': protocols, 'vhosts': vhosts, 'urls': urls})
  102. return True