-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcensys_hostname.py
54 lines (50 loc) · 2.03 KB
/
censys_hostname.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from recon.core.module import BaseModule
from censys.search import CensysHosts
from censys.common.exceptions import CensysException
class Module(BaseModule):
meta = {
"name": "Censys - Hosts by Hostname",
"author": "Censys, Inc. <[email protected]>",
"version": 2.1,
"description": (
"Retrieves all IPs for a given hostname. This module queries the"
" 'name' field and updates the 'hosts' and 'ports' tables with the"
" results."
),
"query": "SELECT DISTINCT host FROM hosts WHERE host IS NOT NULL",
"required_keys": ["censysio_id", "censysio_secret"],
"dependencies": ["censys>=2.1.2"],
}
def module_run(self, hosts):
api_id = self.get_key("censysio_id")
api_secret = self.get_key("censysio_secret")
c = CensysHosts(api_id, api_secret)
for host in hosts:
host = host.strip('"')
self.heading(host, level=0)
try:
query = c.search(f"name:{host}", virtual_hosts="ONLY")
except CensysException:
self.print_exception()
continue
for hit in query():
common_kwargs = {
"ip_address": hit["ip"],
"host": hit.get("name"),
}
location = hit.get("location", {})
coords = location.get("coordinates", {})
self.insert_hosts(
region=location.get("continent"),
country=location.get("country"),
latitude=coords.get("latitude"),
longitude=coords.get("longitude"),
**common_kwargs,
)
for service in hit.get("services", []):
self.insert_ports(
port=service["port"],
protocol=service["transport_protocol"],
notes=service["service_name"],
**common_kwargs,
)