From fe22a5ad0d18684903134b10af93d15c362ec8c0 Mon Sep 17 00:00:00 2001 From: hakril Date: Thu, 13 Feb 2020 23:16:46 +0100 Subject: [PATCH] Testing/Fixing some sample for py3 --- samples/crypto/certificate.py | 11 ++++++----- samples/find_value.py | 16 ++++++++++++++-- samples/scheduled_tasks/scheduled_task.py | 4 ++-- samples/security/explain_security_desciptor.py | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 samples/security/explain_security_desciptor.py diff --git a/samples/crypto/certificate.py b/samples/crypto/certificate.py index 4bf293d..ad5724a 100644 --- a/samples/crypto/certificate.py +++ b/samples/crypto/certificate.py @@ -1,7 +1,8 @@ import hashlib +import base64 import windows.crypto -windowscert = """-----BEGIN CERTIFICATE----- +windowscert = b"""-----BEGIN CERTIFICATE----- MIIFBDCCA+ygAwIBAgITMwAAAQZuwyXEMckYDgAAAAABBjANBgkqhkiG9w0BAQsF ADCBhDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCldhc2hpbmd0b24xEDAOBgNVBAcT B1JlZG1vbmQxHjAcBgNVBAoTFU1pY3Jvc29mdCBDb3Jwb3JhdGlvbjEuMCwGA1UE @@ -32,7 +33,7 @@ l2ec6CyjDQc6HcQBNCsbJVq6qGtQbYNE+ih+KhIU4tO5jf25xthf2g== -----END CERTIFICATE-----""" -raw_cert = ("".join(windowscert.split("\n")[1:-1])).decode('base64') +raw_cert = base64.decodestring(b"".join(windowscert.split(b"\n")[1:-1])) cert = windows.crypto.Certificate.from_buffer(raw_cert) print("Analysing certificate: {0}".format(cert)) @@ -42,7 +43,7 @@ print(" * raw_serial: <{0}>".format(cert.raw_serial)) print(" * serial: <{0}>".format(cert.serial)) print(" * encoded start: <{0!r}>".format(cert.encoded[:20])) -print "" +print("") chains = cert.chains print("This certificate has {0} certificate chain(s)".format(len(chains))) for i, chain in enumerate(chains): @@ -52,7 +53,7 @@ for i, chain in enumerate(chains): print(" {0}:".format(ccert)) print(" * issuer: <{0}>".format(ccert.issuer)) -print "" +print ("") cert_to_verif = ccert print("Looking for <{0}> in trusted certificates".format(cert_to_verif.name)) root_store = windows.crypto.CertificateStore.from_system_store("Root") @@ -78,7 +79,7 @@ print("Analysing {0}".format(cryptobj)) print("File has {0} signer(s):".format(cryptobj.crypt_msg.nb_signer)) for i, signer in enumerate(cryptobj.crypt_msg.signers): print("Signer {0}:".format(i)) - print(" * Issuer: {0!r}".format(windows.crypto.ECRYPT_DATA_BLOB(signer.Issuer.cbData, signer.Issuer.pbData).data)) + print(" * Issuer: {0!r}".format(signer.Issuer.data)) print(" * HashAlgorithme: {0}".format(signer.HashAlgorithm.pszObjId)) cert = cryptobj.cert_store.find(signer.Issuer, signer.SerialNumber) print(" * Certificate: {0}".format(cert)) diff --git a/samples/find_value.py b/samples/find_value.py index a0c54a5..c5a19da 100644 --- a/samples/find_value.py +++ b/samples/find_value.py @@ -34,6 +34,12 @@ def search_name_in_windef(target): if match(target, name): print(repr(windef)) +def search_name_in_winerror(target): + for name, windef in meta.errors_walker(): + if match(target, name): + print(repr(windef)) + + def search_name_in_interface(target): for name, interface in meta.interfaces_walker(): if not issubclass(interface, windows.generated_def.interfaces.COMInterface): @@ -54,17 +60,23 @@ def search_name(target): search_name_in_struct(target) print("== Windef ==") search_name_in_windef(target) + print("== Winerror ==") + search_name_in_winerror(target) print("== Interfaces ==") search_name_in_interface(target) def search_value(target): for name, windef in meta.windef_walker(): if target == windef: - print(windef) + print(repr(windef)) for status in gdef.ntstatus.NtStatusException.ALL_STATUS.values(): if target == status[0]: - print(status) + print(repr(windef)) + + for name, windef in meta.errors_walker(): + if target == windef: + print(repr(windef)) parser = argparse.ArgumentParser(prog=__file__) parser.add_argument('target', help='The name or value to research in PythonForWindows generated definition') diff --git a/samples/scheduled_tasks/scheduled_task.py b/samples/scheduled_tasks/scheduled_task.py index 28d6716..9bb780a 100644 --- a/samples/scheduled_tasks/scheduled_task.py +++ b/samples/scheduled_tasks/scheduled_task.py @@ -30,7 +30,7 @@ print(" * Definition: <{task.definition}>".format(task=task)) print("Listing actions:") for action in task.definition.actions: print(" * Action: <{action}>".format(action=action)) - print(" * Type: <{action.type}>".format(action=action)) + print(" * Type: <{action.type!r}>".format(action=action)) if getattr(action, "path", None): print(" * path: <{action.path}>".format(action=action)) print(" * arguments: <{action.arguments}>".format(action=action)) @@ -38,7 +38,7 @@ for action in task.definition.actions: # import pdb;pdb.set_trace() print("Listing triggers:") for trigger in task.definition.triggers: - print(" * Trigger type: <{trigger.type}>".format(trigger=trigger)) + print(" * Trigger type: <{trigger.type!r}>".format(trigger=trigger)) print("") DEMO_FOLDER_NAME = "PFW_DEMO_FOLDER" diff --git a/samples/security/explain_security_desciptor.py b/samples/security/explain_security_desciptor.py new file mode 100644 index 0000000..6aad380 --- /dev/null +++ b/samples/security/explain_security_desciptor.py @@ -0,0 +1,16 @@ +import argparse +import os.path + +import windows.security + + + +parser = argparse.ArgumentParser(prog=__file__) +parser.add_argument('sddl', help='The SDDL to explain') +parser.add_argument('--type', help='The type of object described by the SDDL (used for the explication of values in the access mask)') +res = parser.parse_args() + +if os.path.exists(res.sddl): + windows.security.SecurityDescriptor.from_filename(res.sddl).explain("file") +else: + windows.security.SecurityDescriptor.from_string(res.sddl).explain(res.type)