Created
February 12, 2021 13:44
-
-
Save mizobuchi/d9915ec725f6929dd77eac698bd397db to your computer and use it in GitHub Desktop.
This file contains 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
import requests | |
import hashlib | |
from bs4 import BeautifulSoup as bs | |
# URL | |
url = "http://example.com/digest/index.html" | |
# response | |
md5a1 = "secret_md5" | |
nonce = "" | |
nc = "00000001" | |
cnonce= "0000000000000000" | |
qop = "auth" | |
a2 = "GET:/digest/index.html" | |
# Authorization | |
username = "digestman" | |
realm = "digest" | |
algorithm = "MD5" | |
uri = "/digest/index.html" | |
# MD5 | |
def get_md5(arg): | |
return hashlib.md5(arg.encode('utf-8')).hexdigest() | |
def main(): | |
auth_header = requests.get(url).headers["WWW-Authenticate"] | |
print(auth_header) | |
nonce = auth_header.split(" ")[2][7:-2] | |
not_md5_response = md5a1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + get_md5(a2) | |
md5_response = get_md5(not_md5_response) | |
print(not_md5_response) | |
headers = { | |
"Authorization": \ | |
"Digest username=\"" + username + "\"" + \ | |
", realm=\"" + realm + "\"" + \ | |
", nonce=\"" + nonce + "\"" + \ | |
", uri=\"" + uri + "\"" + \ | |
", algorithm=\"" + algorithm + "\"" + \ | |
", response=\"" + md5_response + "\"" + \ | |
", qop=" + qop + \ | |
", nc=" + nc + \ | |
", cnonce=\"" + cnonce + "\"" | |
} | |
print(headers) | |
answer = requests.get(url, headers=headers) | |
print(answer.headers) | |
print(answer.text) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment