Retrieve User_ids encrypted by enrichment

I cobbled together a quick Python script to reverse hashed data for IPs. However it could also be used/modified to work with your User IDs.

It accepts input via arguments or stdin - so it’s not really limited to just IPs.

import hashlib, os, sys

salt = os.getenv('SNOWPLOW_PII_SALT')


def hash_ip(input_ip):
    hashed_ip = hashlib.md5()
    hashed_ip.update(f'{input_ip}{salt}'.encode('utf-8'))
    return hashed_ip.hexdigest()


def hash_range(start, end):
    import socket, struct
    start = struct.unpack('>I', socket.inet_aton(start))[0]
    end = struct.unpack('>I', socket.inet_aton(end))[0]
    return [hash_ip(socket.inet_ntoa(struct.pack('>I', i))) for i in range(start, end)]


if __name__ == '__main__':
    if len(sys.argv) == 2 or len(sys.argv) == 3:
        if len(sys.argv) == 2:
            print(hash_ip(sys.argv[1]))
        if len(sys.argv) == 3:
            hashed_list = hash_range(sys.argv[1], sys.argv[2])
            [print(hashed_ip) for hashed_ip in hashed_list]
    else:
        [print(hash_ip(line.strip('\n'))) for line in sys.stdin]


  1. Add it to a file e.g. ip_tools.py
  2. Add your salt to your environment by setting your ~/.bash_profile up with export SNOWPLOW_PII_SALT=LotsAndlotsOfDeliciousSalt
  3. (Optional) Create a virtual environment for Python 3.6 if you’re not running it by default
  4. Run it with the syntax: python ip_tools.py $USER_ID or if you have a list of user IDs, you can feed it via stdin and write it to a file cat list_of_user_ids | python ip_tools.py > output_file