Skip to content

Instantly share code, notes, and snippets.

@Frank-Buss
Created September 17, 2024 07:58
Show Gist options
  • Save Frank-Buss/95d3335b961637c954af76d7981c6b6c to your computer and use it in GitHub Desktop.
Save Frank-Buss/95d3335b961637c954af76d7981c6b6c to your computer and use it in GitHub Desktop.
Counts all possible single bit flip names for domain
import re
import sys
# List of valid TLDs (this is a small subset, you may want to use a more comprehensive list)
VALID_TLDS = set(['com', 'org', 'net', 'edu', 'gov', 'io', 'co', 'uk', 'de', 'fr', 'jp', 'cn', 'au', 'us'])
def is_valid_domain(domain):
pattern = r'^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$'
if not re.match(pattern, domain):
return False
# Check if the TLD is valid
tld = domain.split('.')[-1].lower()
return tld in VALID_TLDS
def count_unique_valid_bit_flips(domain):
valid_domains = set()
domain_bytes = bytearray(domain.encode('ascii'))
original_domain = domain.lower()
original_length = len(original_domain)
for i in range(len(domain_bytes)):
for j in range(8):
# Store the original byte
original_byte = domain_bytes[i]
# Flip the bit
domain_bytes[i] ^= (1 << j)
try:
flipped_domain = domain_bytes.decode('ascii').lower()
# Check if the length is the same and the domain is valid
if (len(flipped_domain) == original_length and
flipped_domain != original_domain and
is_valid_domain(flipped_domain)):
valid_domains.add(flipped_domain)
print(flipped_domain)
except UnicodeDecodeError:
# If we can't decode, it's not a valid ASCII string
pass
# Restore the original byte
domain_bytes[i] = original_byte
return len(valid_domains)
if len(sys.argv) != 2:
print("Usage: python script_name.py <domain>")
print("Example: python script_name.py example.com")
sys.exit(1)
domain = sys.argv[1]
result = count_unique_valid_bit_flips(domain)
print(f"Number of unique valid domain names with single bit flips for '{domain}': {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment