|
| 1 | +from bbot.modules.templates.censys import censys |
| 2 | + |
| 3 | + |
| 4 | +class censys_ip(censys): |
| 5 | + """ |
| 6 | + Query the Censys /v2/hosts/{ip} endpoint for associated hostnames, IPs, and URLs. |
| 7 | + """ |
| 8 | + |
| 9 | + watched_events = ["IP_ADDRESS"] |
| 10 | + produced_events = [ |
| 11 | + "IP_ADDRESS", |
| 12 | + "DNS_NAME", |
| 13 | + "URL_UNVERIFIED", |
| 14 | + "OPEN_TCP_PORT", |
| 15 | + "OPEN_UDP_PORT", |
| 16 | + "TECHNOLOGY", |
| 17 | + "PROTOCOL", |
| 18 | + ] |
| 19 | + flags = ["passive", "safe"] |
| 20 | + meta = { |
| 21 | + "description": "Query the Censys API for hosts by IP address", |
| 22 | + "created_date": "2026-01-26", |
| 23 | + "author": "@TheTechromancer", |
| 24 | + "auth_required": True, |
| 25 | + } |
| 26 | + options = {"api_key": "", "dns_names_limit": 100, "in_scope_only": True} |
| 27 | + options_desc = { |
| 28 | + "api_key": "Censys.io API Key in the format of 'key:secret'", |
| 29 | + "dns_names_limit": "Maximum number of DNS names to extract from dns.names (default 100)", |
| 30 | + "in_scope_only": "Only query in-scope IPs. If False, will query up to distance 1.", |
| 31 | + } |
| 32 | + scope_distance_modifier = 1 |
| 33 | + |
| 34 | + async def setup(self): |
| 35 | + self.dns_names_limit = self.config.get("dns_names_limit", 100) |
| 36 | + self.warning( |
| 37 | + "This module may consume a lot of API queries. Unless you specifically want to query on each individual IP, we recommend using the censys_dns module instead." |
| 38 | + ) |
| 39 | + return await super().setup() |
| 40 | + |
| 41 | + async def filter_event(self, event): |
| 42 | + in_scope_only = self.config.get("in_scope_only", True) |
| 43 | + max_scope_distance = 0 if in_scope_only else (self.scan.scope_search_distance + 1) |
| 44 | + if event.scope_distance > max_scope_distance: |
| 45 | + return False, "event is not in scope" |
| 46 | + return True |
| 47 | + |
| 48 | + async def handle_event(self, event): |
| 49 | + ip = str(event.host) |
| 50 | + url = f"{self.base_url}/v2/hosts/{ip}" |
| 51 | + |
| 52 | + resp = await self.api_request(url) |
| 53 | + if resp is None: |
| 54 | + self.debug(f"No response for {ip}") |
| 55 | + return |
| 56 | + |
| 57 | + if resp.status_code == 404: |
| 58 | + self.debug(f"No data found for {ip}") |
| 59 | + return |
| 60 | + |
| 61 | + if resp.status_code != 200: |
| 62 | + self.verbose(f"Non-200 status code ({resp.status_code}) for {ip}") |
| 63 | + return |
| 64 | + |
| 65 | + try: |
| 66 | + data = resp.json() |
| 67 | + except Exception as e: |
| 68 | + self.warning(f"Failed to parse JSON response for {ip}: {e}") |
| 69 | + return |
| 70 | + |
| 71 | + result = data.get("result", {}) |
| 72 | + if not result: |
| 73 | + return |
| 74 | + |
| 75 | + # Track what we've already emitted to avoid duplicates |
| 76 | + seen = set() |
| 77 | + |
| 78 | + # Extract data from services |
| 79 | + for service in result.get("services", []): |
| 80 | + port = service.get("port") |
| 81 | + transport = service.get("transport_protocol", "TCP").upper() |
| 82 | + |
| 83 | + # Emit OPEN_TCP_PORT or OPEN_UDP_PORT for services with a port |
| 84 | + # QUIC uses UDP as transport, so treat it as UDP |
| 85 | + if port and (port, transport) not in seen: |
| 86 | + seen.add((port, transport)) |
| 87 | + if transport in ("UDP", "QUIC"): |
| 88 | + event_type = "OPEN_UDP_PORT" |
| 89 | + else: |
| 90 | + event_type = "OPEN_TCP_PORT" |
| 91 | + await self.emit_event( |
| 92 | + self.helpers.make_netloc(ip, port), |
| 93 | + event_type, |
| 94 | + parent=event, |
| 95 | + context="{module} found open port on {event.parent.data}", |
| 96 | + ) |
| 97 | + |
| 98 | + # Emit PROTOCOL for non-HTTP services |
| 99 | + # Use extended_service_name (more specific) falling back to service_name |
| 100 | + # Also check transport_protocol for protocols like QUIC |
| 101 | + service_name = service.get("extended_service_name") or service.get("service_name", "") |
| 102 | + # If service_name is UNKNOWN but transport_protocol is meaningful, use that |
| 103 | + if service_name.upper() == "UNKNOWN" and transport and transport not in ("TCP", "UDP"): |
| 104 | + service_name = transport |
| 105 | + if service_name and service_name.upper() not in ("HTTP", "HTTPS", "UNKNOWN"): |
| 106 | + protocol_key = ("protocol", service_name.upper(), port) |
| 107 | + if protocol_key not in seen: |
| 108 | + seen.add(protocol_key) |
| 109 | + protocol_data = {"host": str(event.host), "protocol": service_name} |
| 110 | + if port: |
| 111 | + protocol_data["port"] = port |
| 112 | + await self.emit_event( |
| 113 | + protocol_data, |
| 114 | + "PROTOCOL", |
| 115 | + parent=event, |
| 116 | + context="{module} found {event.type}: {event.data[protocol]} on {event.parent.data}", |
| 117 | + ) |
| 118 | + |
| 119 | + # Extract URLs from HTTP services |
| 120 | + http_data = service.get("http", {}) |
| 121 | + request = http_data.get("request", {}) |
| 122 | + uri = request.get("uri") |
| 123 | + if uri and uri not in seen: |
| 124 | + seen.add(uri) |
| 125 | + await self.emit_event( |
| 126 | + uri, |
| 127 | + "URL_UNVERIFIED", |
| 128 | + parent=event, |
| 129 | + context="{module} found {event.data} in HTTP service of {event.parent.data}", |
| 130 | + ) |
| 131 | + |
| 132 | + # Extract TLS certificate data |
| 133 | + tls_data = service.get("tls", {}) |
| 134 | + certs = tls_data.get("certificates", {}) |
| 135 | + leaf_data = certs.get("leaf_data", {}) |
| 136 | + |
| 137 | + # Extract names from leaf_data.names |
| 138 | + for name in leaf_data.get("names", []): |
| 139 | + await self._emit_host(name, event, seen, "TLS certificate") |
| 140 | + |
| 141 | + # Extract common_name from leaf_data.subject |
| 142 | + subject = leaf_data.get("subject", {}) |
| 143 | + for cn in subject.get("common_name", []): |
| 144 | + await self._emit_host(cn, event, seen, "TLS certificate subject") |
| 145 | + |
| 146 | + # Extract software/technologies |
| 147 | + for software in service.get("software", []): |
| 148 | + product = software.get("uniform_resource_identifier", software.get("product", "")) |
| 149 | + if product: |
| 150 | + await self.emit_event( |
| 151 | + {"technology": product, "host": str(event.host)}, |
| 152 | + "TECHNOLOGY", |
| 153 | + parent=event, |
| 154 | + context="{module} found {event.type}: {event.data[technology]} on {event.parent.data}", |
| 155 | + ) |
| 156 | + |
| 157 | + # Extract dns.names (limit to configured max) |
| 158 | + dns_data = result.get("dns", {}) |
| 159 | + dns_names = dns_data.get("names", []) |
| 160 | + for name in dns_names[: self.dns_names_limit]: |
| 161 | + await self._emit_host(name, event, seen, "reverse DNS") |
| 162 | + |
| 163 | + async def _emit_host(self, host, event, seen, source): |
| 164 | + """Emit IP_ADDRESS or DNS_NAME for a host value.""" |
| 165 | + # Validate and emit as DNS_NAME |
| 166 | + try: |
| 167 | + validated = self.helpers.validators.validate_host(host) |
| 168 | + except ValueError as e: |
| 169 | + self.debug(f"Error validating host {host} in {source}: {e}") |
| 170 | + if validated and validated not in seen: |
| 171 | + seen.add(validated) |
| 172 | + await self.emit_event( |
| 173 | + validated, |
| 174 | + "DNS_NAME", |
| 175 | + parent=event, |
| 176 | + context=f"{{module}} found {{event.data}} in {source} of {{event.parent.data}}", |
| 177 | + ) |
0 commit comments