How to calculate NTLM hash in python?
Asked Answered
P

3

11

How can I calculate NTLM hash of a passowrd in python? Is there any library or sample code?

I want it for writing a NTLM brute force tools with python (Like Cain & Abel )

Prudential answered 24/3, 2013 at 20:30 Comment(0)
D
14

It is actually very quite simple use hashlib here

import hashlib,binascii
hash = hashlib.new('md4', "password".encode('utf-16le')).digest()
print binascii.hexlify(hash)

Or you can additionally use the python-ntlm library here

Dollfuss answered 24/3, 2013 at 20:47 Comment(1)
A slightly more simpler form - import hashlib print(hashlib.new('md4', "password".encode('utf-16le')).hexdigest())Rhpositive
D
6

You can make use of the hashlib and binascii modules to compute your NTLM hash:

import binascii, hashlib
input_str = "SOMETHING_AS_INPUT_TO_HASH"
ntlm_hash = binascii.hexlify(hashlib.new('md4', input_str.encode('utf-16le')).digest())
print ntlm_hash
Discommodity answered 24/3, 2013 at 20:48 Comment(3)
why not this? pythonhosted.org/passlib/lib/passlib.hash.nthash.html not familiar with NTLM hash at already.Stylolite
passlib is a separate python package, but binascii and hashlib are part of the standard python library. Not saying you should not use passlib, it is upto the author's preference. There are other libraries as well like python-ntlm.Discommodity
Thanks. interesting how OP checked the other guy. He just copy and paste from an online source code and you being the first one didn't get checked.Stylolite
D
1

I had similar problem with Python version 3.12.5, here is the solution:

from passlib.hash import nthash

def hashing_NTLM(provided_password):
    return nthash.hash(provided_password)

# Example usage:
password = "insert your password here"
hashed_password = hashing_NTLM(password)
print(f"Hash: {hashed_password}")

Remember to have passlib installed:

pip install passlib
Dry answered 25/8 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.