From 63dd0130f13edf95d548bcf4a3acc3996a7578d5 Mon Sep 17 00:00:00 2001 From: clement rouault Date: Fri, 27 May 2022 15:34:27 +0200 Subject: [PATCH] POC of windows.system.domain --- .../definitions/functions/sysinfo.txt | 62 +++++++++++++++++++ .../definitions/functions/winfunc.txt | 49 --------------- .../definitions/structures/system_info.txt | 13 ++++ windows/winobject/system.py | 17 +++++ windows/winproxy/apis/kernel32.py | 13 ++++ 5 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 ctypes_generation/definitions/functions/sysinfo.txt diff --git a/ctypes_generation/definitions/functions/sysinfo.txt b/ctypes_generation/definitions/functions/sysinfo.txt new file mode 100644 index 0000000..dc3ed30 --- /dev/null +++ b/ctypes_generation/definitions/functions/sysinfo.txt @@ -0,0 +1,62 @@ +BOOL GetComputerNameExA( + [in] COMPUTER_NAME_FORMAT NameType, + [out] LPSTR lpBuffer, + [in, out] LPDWORD nSize +); + +BOOL GetComputerNameExW( + [in] COMPUTER_NAME_FORMAT NameType, + [out] LPWSTR lpBuffer, + [in, out] LPDWORD nSize +); + + +BOOL WINAPI GetComputerNameA( + _Out_ LPCSTR lpBuffer, + _Inout_ LPDWORD lpnSize +); + +BOOL WINAPI GetComputerNameW( + _Out_ LPWSTR lpBuffer, + _Inout_ LPDWORD lpnSize +); + +BOOL WINAPI LookupAccountSidA( + _In_opt_ LPCSTR lpSystemName, + _In_ PSID lpSid, + _Out_opt_ LPCSTR lpName, + _Inout_ LPDWORD cchName, + _Out_opt_ LPCSTR lpReferencedDomainName, + _Inout_ LPDWORD cchReferencedDomainName, + _Out_ PSID_NAME_USE peUse +); + +BOOL WINAPI LookupAccountSidW( + _In_opt_ LPWSTR lpSystemName, + _In_ PSID lpSid, + _Out_opt_ LPWSTR lpName, + _Inout_ LPDWORD cchName, + _Out_opt_ LPWSTR lpReferencedDomainName, + _Inout_ LPDWORD cchReferencedDomainName, + _Out_ PSID_NAME_USE peUse +); + +BOOL LookupAccountNameA( + LPCSTR lpSystemName, + LPCSTR lpAccountName, + PSID Sid, + LPDWORD cbSid, + LPSTR ReferencedDomainName, + LPDWORD cchReferencedDomainName, + PSID_NAME_USE peUse +); + +BOOL LookupAccountNameW( + LPCWSTR lpSystemName, + LPCWSTR lpAccountName, + PSID Sid, + LPDWORD cbSid, + LPWSTR ReferencedDomainName, + LPDWORD cchReferencedDomainName, + PSID_NAME_USE peUse +); \ No newline at end of file diff --git a/ctypes_generation/definitions/functions/winfunc.txt b/ctypes_generation/definitions/functions/winfunc.txt index c8a15cb..fecd119 100644 --- a/ctypes_generation/definitions/functions/winfunc.txt +++ b/ctypes_generation/definitions/functions/winfunc.txt @@ -961,55 +961,6 @@ INT WINAPI GetSystemMetrics( _In_ INT nIndex ); -BOOL WINAPI GetComputerNameA( - _Out_ LPCSTR lpBuffer, - _Inout_ LPDWORD lpnSize -); - -BOOL WINAPI GetComputerNameW( - _Out_ LPWSTR lpBuffer, - _Inout_ LPDWORD lpnSize -); - -BOOL WINAPI LookupAccountSidA( - _In_opt_ LPCSTR lpSystemName, - _In_ PSID lpSid, - _Out_opt_ LPCSTR lpName, - _Inout_ LPDWORD cchName, - _Out_opt_ LPCSTR lpReferencedDomainName, - _Inout_ LPDWORD cchReferencedDomainName, - _Out_ PSID_NAME_USE peUse -); - -BOOL WINAPI LookupAccountSidW( - _In_opt_ LPWSTR lpSystemName, - _In_ PSID lpSid, - _Out_opt_ LPWSTR lpName, - _Inout_ LPDWORD cchName, - _Out_opt_ LPWSTR lpReferencedDomainName, - _Inout_ LPDWORD cchReferencedDomainName, - _Out_ PSID_NAME_USE peUse -); - -BOOL LookupAccountNameA( - LPCSTR lpSystemName, - LPCSTR lpAccountName, - PSID Sid, - LPDWORD cbSid, - LPSTR ReferencedDomainName, - LPDWORD cchReferencedDomainName, - PSID_NAME_USE peUse -); - -BOOL LookupAccountNameW( - LPCWSTR lpSystemName, - LPCWSTR lpAccountName, - PSID Sid, - LPDWORD cbSid, - LPWSTR ReferencedDomainName, - LPDWORD cchReferencedDomainName, - PSID_NAME_USE peUse -); DWORD WINAPI GetInterfaceInfo( _Out_ PIP_INTERFACE_INFO pIfTable, diff --git a/ctypes_generation/definitions/structures/system_info.txt b/ctypes_generation/definitions/structures/system_info.txt index e6c82d0..53d8570 100644 --- a/ctypes_generation/definitions/structures/system_info.txt +++ b/ctypes_generation/definitions/structures/system_info.txt @@ -54,3 +54,16 @@ typedef struct _SYSTEM_BASIC_INFORMATION { PVOID Reserved2[4]; CHAR NumberOfProcessors; } SYSTEM_BASIC_INFORMATION, *PSYSTEM_BASIC_INFORMATION; + + +typedef enum _COMPUTER_NAME_FORMAT { + ComputerNameNetBIOS, + ComputerNameDnsHostname, + ComputerNameDnsDomain, + ComputerNameDnsFullyQualified, + ComputerNamePhysicalNetBIOS, + ComputerNamePhysicalDnsHostname, + ComputerNamePhysicalDnsDomain, + ComputerNamePhysicalDnsFullyQualified, + ComputerNameMax +} COMPUTER_NAME_FORMAT; diff --git a/windows/winobject/system.py b/windows/winobject/system.py index c5105ec..7476c51 100644 --- a/windows/winobject/system.py +++ b/windows/winobject/system.py @@ -174,6 +174,23 @@ class System(object): winproxy.GetComputerNameA(buf, ctypes.byref(size)) return buf[:size.value] + def _computer_name_ex(self, nametype): + size = gdef.DWORD(0) + try: + winproxy.GetComputerNameExW(nametype, None, ctypes.byref(size)) + except WindowsError as e: + if e.winerror != gdef.ERROR_MORE_DATA: + raise + + buf = ctypes.create_unicode_buffer(size.value) + winproxy.GetComputerNameExW(nametype, buf, ctypes.byref(size)) + return buf[:size.value] + + @utils.fixedproperty + def domain(self): + # [WIP] name of the domain joined by the computer, None is no domain joined + return self._computer_name_ex(gdef.ComputerNameDnsDomain) or None + @utils.fixedpropety def version(self): """The version of the system diff --git a/windows/winproxy/apis/kernel32.py b/windows/winproxy/apis/kernel32.py index 3b6883c..b763d41 100644 --- a/windows/winproxy/apis/kernel32.py +++ b/windows/winproxy/apis/kernel32.py @@ -335,6 +335,8 @@ def GetVersionExA(lpVersionInformation): def GetVersionExW(lpVersionInformation): return GetVersionExW.ctypes_function(lpVersionInformation) + + ## Hardware @Kernel32Proxy() @@ -373,6 +375,14 @@ def GetComputerNameA(lpBuffer, lpnSize): def GetComputerNameW(lpBuffer, lpnSize): return GetComputerNameW.ctypes_function(lpBuffer, lpnSize) +@Kernel32Proxy() +def GetComputerNameExA(NameType, lpBuffer, nSize): + return GetComputerNameExA.ctypes_function(NameType, lpBuffer, nSize) + +@Kernel32Proxy() +def GetComputerNameExW(NameType, lpBuffer, nSize): + return GetComputerNameExW.ctypes_function(NameType, lpBuffer, nSize) + @Kernel32Proxy() def GetWindowsDirectoryA(lpBuffer, uSize=None): if uSize is None: @@ -389,6 +399,9 @@ def GetWindowsDirectoryW(lpBuffer, uSize=None): def GetProductInfo(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion, pdwReturnedProductType): return GetProductInfo.ctypes_function(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion, pdwReturnedProductType) + + + ## Other @Kernel32Proxy(error_check=no_error_check)