Skip to content

Instantly share code, notes, and snippets.

@lsowen
Last active August 29, 2023 22:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lsowen/1a46f9d5fc026e6efc7d to your computer and use it in GitHub Desktop.
Save lsowen/1a46f9d5fc026e6efc7d to your computer and use it in GitHub Desktop.
Create valid ONVIF request to Dahua IPC-HDW2100
#!/usr/bin/env python3
import hashlib
import os
import base64
from datetime import datetime
username = "admin"
password = "admin"
created = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z")
raw_nonce = os.urandom(20)
nonce = base64.b64encode(raw_nonce)
sha1 = hashlib.sha1()
sha1.update(raw_nonce + created.encode('utf8') + password.encode('utf8'))
raw_digest = sha1.digest()
digest = base64.b64encode(raw_digest)
#print("NONCE: {}".format(nonce))
#print("CREATED: {}".format(created))
#print("DIGEST: {}".format(digest))
# Request XML derived from https://github.com/ltoscano/ponvif
template = """<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<Security s:mustUnderstand="1" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>{username}</Username>
<Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">{digest}</Password>
<Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">{nonce}</Nonce>
<Created xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">{created}</Created>
</UsernameToken>
</Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetCapabilities xmlns="http://www.onvif.org/ver10/device/wsdl">
<Category>All</Category>
</GetCapabilities>
</s:Body>
</s:Envelope>"""
req_body = template.format(username=username, nonce=nonce.decode('utf8'), created=created, digest=digest.decode('utf8'))
print(req_body)
#!/bin/bash
python3 digest.py > request.xml
curl --silent -X POST --header 'Content-Type: text/xml; charset=utf-8' -d @request.xml 'http://camera_ip/onvif/device_service' | xmllint --format -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment