import hashlib
import requests
from requests.auth import HTTPDigestAuth
# Parameters
username = "admin"
password = "autvix123456"
url = "http://192.168.1.108/cgi-bin/magicBox.cgi?action=getLanguageCaps%20HTTP/1.1"
# Get the initial challenge
response = requests.get(url, auth=HTTPDigestAuth(username, password), verify=False)
# Check if the challenge requires digest authentication
if response.status_code == 401:
headers = response.headers
auth_header = headers.get('www-authenticate', '')
# Extract parameters from the WWW-Authenticate header
# This parsing might need adjustment based on the actual header format
import re
match = re.search(r'realm="([^"]+)"', auth_header)
realm = match.group(1) if match else ""
match = re.search(r'nonce="([^"]+)"', auth_header)
nonce = match.group(1) if match else ""
match = re.search(r'qop="([^"]+)"', auth_header)
qop = match.group(1) if match else ""
# Generate HA1, HA2, and Response
HA1 = hashlib.md5(f"{username}:{realm}:{password}".encode()).hexdigest()
HA2 = hashlib.md5(f"GET:{url}".encode()).hexdigest()
response_digest = hashlib.md5(f"{HA1}:{nonce}:00000001:0a4f113b:{qop}:{HA2}".encode()).hexdigest()
# Send the authenticated request
headers = {
'Authorization': f'Digest username="{username}", realm="{realm}", nonce="{nonce}", uri="{url}", response="{response_digest}", qop="{qop}", nc=00000001, cnonce="0a4f113b"'
}
final_response = requests.get(url, headers=headers, verify=False)
if final_response.status_code == 200:
print("Access successful!")
print(final_response.json())
else:
print(f"Failed to access the URL. Status code: {final_response.status_code}")
else:
print(response.content)
print(f"Failed to obtain challenge. Status code: {response.status_code}")