Created
June 3, 2016 15:53
-
-
Save KyleHanslovan/9e044d336008606944faf3dad871410f 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
from ctypes import * | |
from ctypes.wintypes import * | |
import winreg | |
NTSTATUS = c_long | |
PVOID = c_void_p | |
PWSTR = c_wchar_p | |
OBJ_CASE_INSENSITIVE = 0x00000040 | |
STATUS_SUCCESS = NTSTATUS(0x00000000).value | |
class UNICODE_STRING(Structure): | |
_fields_ = [('Length', USHORT), | |
('MaximumLength', USHORT), | |
('Buffer', PWSTR)] | |
class OBJECT_ATTRIBUTES(Structure): | |
_fields_ = [('Length', ULONG), | |
('RootDirectory', HANDLE), | |
('ObjectName', POINTER(UNICODE_STRING)), | |
('Attributes', ULONG), | |
('SecurityDescriptor', PVOID), | |
('SecurityQualityOfService', PVOID)] | |
def InitializeObjectAttributes(InitializedAttributes, | |
ObjectName, | |
Attributes, | |
RootDirectory=None, | |
SecurityDescriptor=None): | |
""" | |
Initializes the OBJECT_ATTRIBUTES structure which specifies the properties of an object | |
handle to routines that open handles. | |
VOID InitializeObjectAttributes( | |
[out] POBJECT_ATTRIBUTES InitializedAttributes, | |
[in] PUNICODE_STRING ObjectName, | |
[in] ULONG Attributes, | |
[in] HANDLE RootDirectory, | |
[in, optional] PSECURITY_DESCRIPTOR SecurityDescriptor); | |
""" | |
memset(addressof(InitializedAttributes), 0, sizeof(InitializedAttributes)) | |
InitializedAttributes.Length = sizeof(InitializedAttributes) | |
InitializedAttributes.ObjectName = ObjectName | |
InitializedAttributes.Attributes = Attributes | |
InitializedAttributes.RootDirectory = RootDirectory | |
InitializedAttributes.SecurityDescriptor = SecurityDescriptor | |
InitializedAttributes.SecurityQualityOfService = None | |
return | |
def RtlUnicodeStringInit(DestinationString, Src): | |
""" | |
Initializes a UNICODE_STRING structure. | |
NTSTATUS RtlUnicodeStringInit( | |
[out] PUNICODE_STRING DestinationString, | |
[in] PCWSTR pszSrc) | |
""" | |
memset(addressof(DestinationString), 0, sizeof(DestinationString)) | |
DestinationString.Buffer = cast(Src, PWSTR) | |
DestinationString.Length = sizeof(Src) | |
DestinationString.MaximumLength = DestinationString.Length | |
return STATUS_SUCCESS | |
def main(): | |
ntdll = WinDLL('ntdll') | |
''' | |
NTSTATUS ZwOpenKey( | |
_Out_ PHANDLE KeyHandle, | |
_In_ ACCESS_MASK DesiredAccess, | |
_In_ POBJECT_ATTRIBUTES ObjectAttributes); | |
''' | |
hKey = HANDLE() | |
DesiredAccess = winreg.KEY_ALL_ACCESS | |
# Create ObjectName structure | |
ObjectName = UNICODE_STRING() | |
strKeyPath = '\\Registry\\Machine\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\' | |
bufKeyPath = create_unicode_buffer(strKeyPath) | |
NtStatus = RtlUnicodeStringInit(ObjectName, bufKeyPath) | |
if NtStatus != STATUS_SUCCESS: | |
raise WindowsError('RtlUnicodeStringInit failed and returned {!r}'.format(NtStatus)) | |
# Create ObjectAttributes structure | |
ObjectAttributes = OBJECT_ATTRIBUTES() | |
InitializeObjectAttributes(ObjectAttributes, pointer(ObjectName), OBJ_CASE_INSENSITIVE) | |
# Get handle to the desired key | |
NtStatus = ntdll.ZwOpenKey(pointer(hKey), DesiredAccess, ObjectAttributes) | |
if NtStatus != STATUS_SUCCESS: | |
raise WindowsError('ZwOpenKey failed and returned {!r}'.format(NtStatus)) | |
''' | |
NTSTATUS ZwDeleteValueKey( | |
_In_ HANDLE KeyHandle, | |
_In_ PUNICODE_STRING ValueName); | |
''' | |
# Create ValueName structure | |
ValueName = UNICODE_STRING() | |
strValueName = '\0HuntressLabs' | |
bufValueName = create_unicode_buffer(strValueName) | |
NtStatus = RtlUnicodeStringInit(ValueName, bufValueName) | |
if NtStatus != STATUS_SUCCESS: | |
raise WindowsError('RtlUnicodeStringInit failed and returned {!r}'.format(NtStatus)) | |
# Delete the desired value | |
NtStatus = ntdll.ZwDeleteValueKey(hKey, | |
pointer(ValueName)) | |
if NtStatus != STATUS_SUCCESS: | |
raise WindowsError('ZwDeleteValueKey failed and returned {!r}'.format(NtStatus)) | |
ntdll.ZwClose(hKey) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment