Files
naksyn-PythonMemoryModule/pythonmemorymodule/windows/crypto/sign_verify.py
T
naksyn db1893910c command line support (partial) via PEB stomping
This update include support to passing command line parameters to unmanaged exe via PEB stomping.
This technique is not working with every executable since it depends on which functions are used to pass arguments.
Generally, to get a universally working technique would be required to hook GetCommandlineA GetCommandlineW __getmainargs and __wgetmainargs since PEB stomping won't cover all cases, more details here:
https://blog-30cm-tw.translate.goog/2020/08/windows-c-mainargc-argv.html?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=it&_x_tr_pto=wapp

However, during my testing I found that mimikatz and several go binaries are working just by doing PEB stomping.
On the other hand, cmdline passing via PEB stomping alone to mingw and VS compiled binaries won't likely work.
2023-07-27 06:44:29 -07:00

70 lines
2.8 KiB
Python

import windows
from windows import winproxy
from windows.crypto import DEFAULT_ENCODING
import windows.generated_def as gdef
import ctypes
__all__ = ["sign", "verify_signature"]
def sign(cert, msg, detached_signature=False, algo=gdef.szOID_RSA_SHA256RSA):
# hash algorithm
alg_hash = gdef.CRYPT_ALGORITHM_IDENTIFIER()
alg_hash.pszObjId = algo.encode()
# Signing parameters
sign_para = gdef.CRYPT_SIGN_MESSAGE_PARA()
sign_para.cbSize = ctypes.sizeof(sign_para)
sign_para.dwMsgEncodingType = DEFAULT_ENCODING
sign_para.pSigningCert = gdef.PCERT_CONTEXT(cert)
sign_para.HashAlgorithm = alg_hash
sign_para.pvHashAuxInfo = None
sign_para.cMsgCert = 0
sign_para.rgpMsgCert = None
sign_para.cMsgCrl = 0
sign_para.rgpMsgCrl = None
sign_para.cAuthAttr = 0
sign_para.rgAuthAttr = None
sign_para.cUnauthAttr = 0
sign_para.rgUnauthAttr = None
sign_para.dwFlags = 0
sign_para.dwInnerContentType = 0
sign_para.HashEncryptionAlgorithm = alg_hash
sign_para.pvHashEncryptionAuxInfo = None
ByteBuffer = windows.utils.BUFFER(gdef.BYTE)
result_buffer = ByteBuffer(nbelt=0x2000)
result_size = gdef.DWORD(len(result_buffer))
buff = ByteBuffer(*bytearray(msg))
buff_pr = windows.utils.BUFFER(gdef.LPBYTE, nbelt=1)(buff)
buff_size = gdef.DWORD(len(msg))
try:
windows.winproxy.CryptSignMessage(sign_para, False, 1, buff_pr, buff_size, result_buffer, result_size)
except WindowsError as e:
if not e.winerror == gdef.ERROR_MORE_DATA:
raise
result_buffer = ByteBuffer(nbelt=result_size.value)
windows.winproxy.CryptSignMessage(sign_para, False, 1, buff_pr, buff_size, result_buffer, result_size)
return bytearray(result_buffer[:result_size.value])
def verify_signature(cert, encoded_blob):
# Verify parameters
verif_param = gdef.CRYPT_KEY_VERIFY_MESSAGE_PARA()
verif_param.cbSize = ctypes.sizeof(gdef.CRYPT_KEY_VERIFY_MESSAGE_PARA)
verif_param.dwMsgEncodingType = windows.crypto.DEFAULT_ENCODING
verif_param.hCryptProv = None
# The public key used
pubkey = cert.pCertInfo[0].SubjectPublicKeyInfo
# Preparing in/out buffer/size
signed_buffer = windows.utils.BUFFER(gdef.BYTE).from_buffer_copy(encoded_blob)
decoded_buffer = windows.utils.BUFFER(gdef.BYTE).from_buffer_copy(encoded_blob)
decoded_size = gdef.DWORD(len(decoded_buffer))
winproxy.CryptVerifyMessageSignatureWithKey(verif_param,
pubkey,
signed_buffer,
len(encoded_blob),
decoded_buffer,
decoded_size)
return bytearray(decoded_buffer[:decoded_size.value])