Created
July 28, 2022 21:48
-
-
Save gwire/c9ab016d2c739c112d509ee5b00e367e to your computer and use it in GitHub Desktop.
Generate an email hash to check the Spamhaus Hash Blocklist (HBL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# hblhash.py - generate hashes to check against the Spamhaus Hash Blocklist | |
# | |
# Spamhaus operates a blocklist of hashed email addresses (and other data types) | |
# it can't be used without an API key - but single hash queries can be made | |
# via check.spamhaus.org (eg useful for diagnosing specific delivery issues) | |
# | |
# This script will take an email address and generate a URL to check the HBL | |
# | |
# $ hblhash.py -e -l [email protected] | |
# https://check.spamhaus.org/listed/?searchterm=GHCVIPAXGTJFY4QG6X6VSFJF2AUVX3DP5BH7QL4UNI2P5FYKDZTA | |
# | |
# 2022 Lee Maguire | |
import sys | |
import getopt | |
from hashlib import sha256, sha1 | |
from base64 import b32encode, b32decode | |
import re | |
source = "[email protected]" | |
opt_email = 0 | |
opt_sha1 = 0 | |
opt_lookup = 0 | |
opt_verbose = 0 | |
## https://docs.spamhaus.com/datasets/docs/source/10-data-type-documentation/datasets/030-datasets.html#hbl | |
def mungeEmail( email ): | |
local_part, domain_part = email.lower().split("@") | |
local_part = re.sub('\+.*$', '', local_part) | |
if (domain_part == 'googlemail.com'): | |
domain_part = 'gmail.com' | |
if (domain_part == 'gmail.com'): | |
local_part = local_part.replace('.','') | |
return( local_part + '@' + domain_part ) | |
opts, args = getopt.getopt(sys.argv[1:],"heclv",["email","cw","sha1","lookup"]) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('Usage: hblhash.py [--email][--lookup][--sha1][-v] [email protected]') | |
sys.exit() | |
elif opt in ("-e", "--email"): | |
opt_email = 1 | |
elif opt in ("--sha1"): | |
opt_sha1 = 1 | |
opt_email = 1 | |
elif opt in ("-c", "--cw"): | |
opt_email = 0 | |
elif opt in ("-l", "--lookup"): | |
opt_lookup = 1 | |
elif opt in ("-v"): | |
opt_verbose = 1 | |
if args: | |
source = args[0] | |
else: | |
opt_verbose = 1 | |
if opt_verbose: | |
sys.stdout.write('source: ' + source + '\n') | |
if opt_email: | |
if opt_verbose: | |
sys.stdout.write('source_munged: '+ mungeEmail(source)+'\n') | |
byte_str = bytes(mungeEmail(source), 'ascii') | |
else: | |
byte_str = bytes(source, 'ascii') | |
if opt_sha1: | |
hash_str = sha1(byte_str).hexdigest() | |
else: | |
hash_str = b32encode(sha256(byte_str).digest()).decode().replace('=','') | |
if opt_lookup: | |
sys.stdout.write('https://check.spamhaus.org/listed/?searchterm=' + hash_str +'\n') | |
else: | |
if opt_verbose: | |
sys.stdout.write('hash: ') | |
sys.stdout.write(hash_str +'\n') | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment