Skip to content

Instantly share code, notes, and snippets.

@m33x
Last active May 7, 2021 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m33x/2f4f8b9f5c3bb4902629bc30c4a449b0 to your computer and use it in GitHub Desktop.
Save m33x/2f4f8b9f5c3bb4902629bc30c4a449b0 to your computer and use it in GitHub Desktop.
Reverses the Hashcat $HEX output format - defaults to utf-8 encoding https://hashcat.net/forum/thread-2483.html
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
:author: Maximilian Golla
:contact: maximilian.golla@rub.de
:version: 0.0.4 2021-05-07
:description: Reverses the Hashcat $HEX output format - defaults to utf-8 encoding
:more: https://hashcat.net/forum/thread-2483.html
:info: Works with Python 2.7 and Python 3.6
:run: python unhexme.py
'''
import platform
import string
import codecs
import sys
def is_ascii(s):
return all((ord(c) >= 32 and ord(c) <= 126) for c in s)
def is_hexstring(line):
return all(c in string.hexdigits for c in line)
def unhex(line):
if isinstance(line, bytes):
line = line.decode('utf-8')
if line.startswith('$HEX[') and line.endswith(']') and is_hexstring(line[5:-1]):
if platform.python_version().startswith('2'):
return line[5:-1].decode('hex')
else:
return bytes.fromhex(line[5:-1]).decode('utf-8')
return line
def rehex(pw):
if is_ascii(pw):
return pw
return '$HEX[' + str(codecs.encode(str.encode(pw), 'hex'), 'UTF-8') + ']'
def main():
# We change Pythons default encoding from ASCII to UTF-8
reload(sys)
sys.setdefaultencoding('utf-8')
print("Testing $HEX to plaintext")
expected = '$HEX[6469652dc3a4727a7465]'
result = rehex('die-ärzte')
if result != expected:
print("FAILED: '{}' != '{}'".format(result, expected))
else:
print("SUCCESS: '{}' == '{}'".format(result, expected))
print("\nTesting plaintext to $HEX")
expected = 'die-ärzte'
result = unhex('$HEX[6469652dc3a4727a7465]')
if result != expected:
print("FAILED: '{}' != '{}'".format(result, expected))
else:
print("SUCCESS: '{}' == '{}'".format(result, expected))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment