Skip to content

Instantly share code, notes, and snippets.

@pixitha
Last active March 18, 2024 17:06
Show Gist options
  • Save pixitha/207e578aa951b3dfda4fdbda4e0a8828 to your computer and use it in GitHub Desktop.
Save pixitha/207e578aa951b3dfda4fdbda4e0a8828 to your computer and use it in GitHub Desktop.
Get list of CT logs, then ask how many total certs they have.
import requests
import json
import locale
def set_locale():
locales_to_try = ['en_US.UTF-8', 'en_US.utf8', 'English_United States.1252']
for loc in locales_to_try:
try:
locale.setlocale(locale.LC_ALL, loc)
return True
except locale.Error:
continue
return False
locale_set = set_locale()
ctl_log = requests.get('https://www.gstatic.com/ct/log_list/v3/log_list.json').json()
total_certs = 0
def human_format(number):
if locale_set:
return locale.format_string('%d', number, grouping=True)
else:
# Locale not set, fallback to manual formatting
return "{:,}".format(number)
#human_format = lambda x: locale.format_string('%d', x, grouping=True)
# Accessing 'logs' key under 'operators'
for operator in ctl_log['operators']:
for log in operator['logs']:
log_url = log['url']
# Remove extra 'https://' prefix
log_url = log_url.replace('https://', '')
state = log.get('state', {}).get('usable', False)
if state:
try:
# Remove the extra slash from the URL
if log_url.endswith('/'):
log_url = log_url[:-1]
log_info = requests.get('https://{}/ct/v1/get-sth'.format(log_url), timeout=3)
log_info.raise_for_status() # Raise an error for bad status codes
log_info_json = log_info.json()
total_certs += int(log_info_json['tree_size'])
print("{} has {} certificates".format(log_url, human_format(log_info_json['tree_size'])))
except requests.exceptions.RequestException as e:
print("Error fetching data from {}: {}".format(log_url, e))
if hasattr(e, 'response') and e.response:
print("Response content: {}".format(e.response.text)) # Print response content
except (json.decoder.JSONDecodeError, KeyError) as e:
print("Error processing data from {}: {}".format(log_url, e))
else:
print("Skipping {} as it is not in a usable state.".format(log_url))
print("Total certs -> {}".format(human_format(total_certs)))
@pixitha
Copy link
Author

pixitha commented Mar 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment