Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_hosts(cert_fingerprints, api_id, api_secret):
try:
censys_hosts = censys.ipv4.CensysIPv4(api_id=api_id, api_secret=api_secret)
hosts_query = ' OR '.join(cert_fingerprints)
hosts_search_results = censys_hosts.search(hosts_query)
return set([ host_search_result['ip'] for host_search_result in hosts_search_results ])
except censys.base.CensysUnauthorizedException:
sys.stderr.write('[-] Your Censys credentials look invalid.\n')
exit(1)
except censys.base.CensysRateLimitExceededException:
sys.stderr.write('[-] Looks like you exceeded your Censys account limits rate. Exiting\n')
exit(1)
def get_certificates():
try:
if not CENSYS_API_ID or not CENSYS_API_SECRET:
logging.info("\033[1;31m[!] API KEY or Secret for Censys not provided.\033[1;m" \
"\nYou'll have to provide them in the script")
sys.exit()
logging.info("[+] Extracting certificates for {} using Censys".format(domain))
censys_certificates = censys.certificates.CensysCertificates(CENSYS_API_ID, CENSYS_API_SECRET)
return censys_certificates
except censys.base.CensysUnauthorizedException:
logging.info('[!] Your Censys credentials look invalid.\n')
exit(1)
except censys.base.CensysRateLimitExceededException:
logging.info('[!] Looks like you exceeded your Censys account limits rate. Exiting\n')
exit(1)
def find_subdomains(domain, api_id, api_secret):
try:
censys_certificates = censys.certificates.CensysCertificates(api_id=api_id, api_secret=api_secret)
certificate_query = 'parsed.names: %s' % domain
certificates_search_results = censys_certificates.search(certificate_query, fields=['parsed.names'])
# Flatten the result, and remove duplicates
subdomains = []
for search_result in certificates_search_results:
subdomains.extend(search_result['parsed.names'])
return set(subdomains)
except censys.base.CensysUnauthorizedException:
sys.stderr.write('[-] Your Censys credentials look invalid.\n')
exit(1)
except censys.base.CensysRateLimitExceededException:
sys.stderr.write('[-] Looks like you exceeded your Censys account limits rate. Exiting\n')
exit(1)
def CensysSearch(value, api_id, api_secret):
try:
censys_certificates = censys.certificates.CensysCertificates(api_id=api_id, api_secret=api_secret)
certificate_query = 'parsed.names: %s' % value
certificates_search_results = censys_certificates.search(certificate_query, fields=['parsed.names'])
subdomains = []
for search_result in certificates_search_results:
subdomains.extend(search_result['parsed.names'])
return set(subdomains)
except censys.base.CensysUnauthorizedException:
sys.stderr.write('[-] Your Censys credentials look invalid.\n')
exit(1)
except censys.base.CensysRateLimitExceededException:
sys.stderr.write('[-] Looks like you exceeded your Censys account limits rate. Exiting\n')
exit(1)
def get_certificates():
try:
if not CENSYS_API_ID or not CENSYS_API_SECRET:
logging.info("\033[1;31m[!] API KEY or Secret for Censys not provided.\033[1;m" \
"\nYou'll have to provide them in the script")
sys.exit()
logging.info("[+] Extracting certificates using Censys")
censys_certificates = censys.certificates.CensysCertificates(CENSYS_API_ID, CENSYS_API_SECRET)
return censys_certificates
except censys.base.CensysUnauthorizedException:
logging.info('\033[93m[!] Your Censys credentials look invalid.\n\033[1;m')
sys.exit(1)
except censys.base.CensysRateLimitExceededException:
logging.info('\033[93m[!] Looks like you exceeded your Censys account limits rate. Exiting\n\033[1;m')
sys.exit(1)
self.urlvoid_api_key = helpers.config_section_map("URLVoid")["api_key"]
except Exception:
self.urlvoid_api_key = ""
click.secho("[!] Did not find a URLVoid API key.", fg="yellow")
try:
self.contact_api_key = helpers.config_section_map("Full Contact")["api_key"]
except Exception:
self.contact_api_key = None
click.secho("[!] Did not find a Full Contact API key.", fg="yellow")
try:
censys_api_id = helpers.config_section_map("Censys")["api_id"]
censys_api_secret = helpers.config_section_map("Censys")["api_secret"]
self.censys_cert_search = censys.certificates.CensysCertificates(api_id=censys_api_id, api_secret=censys_api_secret)
except censys.base.CensysUnauthorizedException:
self.censys_cert_search = None
click.secho("[!] Censys reported your API information is invalid, so Censys searches \
will be skipped.", fg="yellow")
click.secho("L.. You provided ID %s & Secret %s" % (censys_api_id, censys_api_secret), fg="yellow")
except Exception as error:
self.censys_cert_search = None
click.secho("[!] Did not find a Censys API ID/secret.", fg="yellow")
click.secho("L.. Details: {}".format(error0), fg="yellow")
# try:
# self.chrome_driver_path = helpers.config_section_map("WebDriver")["driver_path"]
# # Try loading the driver as a test
# self.chrome_options = Options()
# self.chrome_options.add_argument("--headless")
# self.chrome_options.add_argument("--window-size=1920x1080")
# self.browser = webdriver.Chrome(chrome_options=self.chrome_options, executable_path=self.chrome_driver_path)
def __init__(self):
"""Everything that should be initiated with a new object goes here."""
try:
censys_api_id = helpers.config_section_map("Censys")["api_id"]
censys_api_secret = helpers.config_section_map("Censys")["api_secret"]
self.censys_cert_search = censys.certificates.CensysCertificates(api_id=censys_api_id,api_secret=censys_api_secret)
except censys.base.CensysUnauthorizedException:
self.censys_cert_search = None
click.secho("[!] Censys reported your API information is invalid, so Censys searches will be skipped.",fg="yellow")
click.secho("L.. You provided ID %s & Secret %s" % (censys_api_id,censys_api_secret),fg="yellow")
except Exception as error:
self.censys_cert_search = None
click.secho("[!] Did not find a Censys API ID/secret.",fg="yellow")
click.secho("L.. Details: {}".format(error),fg="yellow")
def __init__(
self,
api_id: str = DefaultValues.CENSYS_API_ID,
api_secret: str = DefaultValues.CENSYS_API_SECRET,
):
"""
Initialize Censys Search Engine API
:param api_id: Censys ID key
:param api_secret: Censys SECRET key
"""
try:
self.api = CensysIPv4(api_id=api_id, api_secret=api_secret)
except CensysUnauthorizedException as invalid_api_err:
print(f"Censys invalid API keys error: {invalid_api_err}")
except CensysException as api_err:
print(f"Censys API error: {api_err}")
self.results: list = []
self.censys_results_count: int = 0
self.search_fields = [
"ip",
"location.country",
"location.latitude",
"location.longitude",
"ports",
"protocols",
"autonomous_system.name",
]
self.convert_dict = {
"ip": "ip",
class CensysUnauthorizedException(CensysException):
pass
class CensysJSONDecodeException(CensysException):
pass
class CensysAPIBase(object):
DEFAULT_URL = "https://www.censys.io/api/v1"
DEFAULT_TIMEOUT = 30
EXCEPTIONS = {
403: CensysUnauthorizedException,
404: CensysNotFoundException,
429: CensysRateLimitExceededException
}
def __init__(self, api_id=None, api_secret=None, url=None, timeout=None):
self.api_id = api_id or os.environ.get("CENSYS_API_ID", None)
self.api_secret = api_secret or os.environ.get("CENSYS_API_SECRET", None)
if not self.api_id or not self.api_secret:
raise CensysException(401, "No API ID or API secret configured.")
timeout = timeout or self.DEFAULT_TIMEOUT
self._api_url = url or os.environ.get("CENSYS_API_URL", None) or self.DEFAULT_URL
# create a session that we'll use for making requests
self._session = requests.Session()
self._session.auth = (self.api_id, self.api_secret)
self._session.timeout = timeout
self._session.headers.update({"accept": "text/json, application/json, */8"})