Python 3.x 如何散列用于HttpNtlmAuth的密码
我可以使用HttpNtlmAuth请求访问组织内的web应用程序,并使用我的明文用户名和密码,但我不希望将密码以明文形式存储在我的代码中。我已经阅读了以下其他答案,因此您可以传递密码散列而不是明文,但我不确定如何做到这一点:Python 3.x 如何散列用于HttpNtlmAuth的密码,python-3.x,hash,python-requests,ntlm-authentication,Python 3.x,Hash,Python Requests,Ntlm Authentication,我可以使用HttpNtlmAuth请求访问组织内的web应用程序,并使用我的明文用户名和密码,但我不希望将密码以明文形式存储在我的代码中。我已经阅读了以下其他答案,因此您可以传递密码散列而不是明文,但我不确定如何做到这一点: 选择的答案提到“向HttpNtlmAuth提供密码或密码哈希”,但没有详细说明 一篇评论提到了对密码进行哈希运算,但对于使用哪种算法没有回应 我曾尝试过根据使用MD4/UTF-16,也尝试过根据使用MD5,但都返回了401(未经授权)响应。下面的代码使用我的明文密码返回20
import hashlib
import requests
from requests_ntlm import HttpNtlmAuth
# these are not the actual values
USERNAME = 'domain\\username'
PASSWORD = 'password'
URL = 'https://internal_web_app/url.aspx?Parameters=values'
hashes = [
PASSWORD, # No hash
hashlib.new('md4', PASSWORD.encode('utf-16le')).hexdigest(), # NTLM
hashlib.md5(PASSWORD.encode('utf-8')).hexdigest() # MD5
]
for hash in hashes:
# create a session
session = requests.Session()
# set up the authentication object with the respective string
session.auth = HttpNtlmAuth(USERNAME, hash)
# send a GET request (verify=False because the SSL cert is crappy)
response = session.get(URL, verify=False)
# expecting 200 for success and 401 for authentication failure
print(response.status_code)
输出:
200
401
401
我的目标是避免将密码存储在笔记本中。我意识到一个解决方法是让用户在运行脚本时输入密码,使用
import getpass
password = getpass.getpass()
我已经解决了眼前的问题,但是我仍然对散列很好奇(如果可能的话,让这个脚本在无人参与的情况下运行),所以如果有人发现了,请提交一个响应