Skip to content

Commit ab070b5

Browse files
authored
Merge pull request #795 from jbx81-1337/feat/direct-syscall-new-format
Fixing direct syscall to use new format
2 parents 947d971 + aab5b43 commit ab070b5

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

c/meterpreter/source/common/common_winapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef struct _WinApiNtdll {
5050
NTSTATUS (*ZwFreeVirtualMemory)(HANDLE ProcessHandle, PVOID* BaseAddress, PSIZE_T RegionSize, ULONG FreeType);
5151
NTSTATUS (*NtQueueApcThread)(HANDLE ThreadHandle, PVOID ApcRoutine, PVOID ApcContext, PVOID Argument1, PVOID Argument2);
5252
NTSTATUS (*NtOpenThread)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, OBJECT_ATTRIBUTES* ObjectAttributes, CLIENT_ID* ClientId);
53+
NTSTATUS (*RtlGetVersion)(PRTL_OSVERSIONINFOEXW os);
5354
} WinApiNtdll;
5455

5556
// kernel32.dll

c/meterpreter/source/metsrv/metapi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ MetApi api_instance = {
161161
winapi_ntdll_ZwQueryVirtualMemory,
162162
winapi_ntdll_ZwFreeVirtualMemory,
163163
winapi_ntdll_NtQueueApcThread,
164-
winapi_ntdll_NtOpenThread
164+
winapi_ntdll_NtOpenThread,
165+
winapi_ntdll_RtlGetVersion
165166
},
166167
// kernel32
167168
{

c/meterpreter/source/metsrv/winapi.c

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum HashedFunctions {
4242
H_ZwFreeVirtualMemory = 0xDE63B5C3,
4343
H_NtQueueApcThread = 0x52E9A746,
4444
H_NtOpenThread = 0x59651E8C,
45+
H_RtlGetVersion = 0xD0C1869C,
4546
H_WriteProcessMemory = 0xD83D6AA1,
4647
H_ReadProcessMemory = 0x579D1BE9,
4748
H_OpenProcess = 0xEFE297C0,
@@ -144,7 +145,21 @@ NtDllFunction lpFunctionsTobeLoaded[] = {
144145
#define STATUS_SUCCESS 0
145146
Syscall** lpWinApiSyscalls = NULL;
146147

147-
extern NTSTATUS SyscallStub(Syscall* pSyscall, ...);
148+
extern NTSTATUS SyscallStub(Syscall *pSyscall, DWORD dwNumberOfArgs, ULONG_PTR *lpArgs);
149+
150+
DWORD GetWindowsMajorMinVer() {
151+
DWORD dwResult = 0;
152+
OSVERSIONINFOEXW os = {0};
153+
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
154+
155+
NTSTATUS status = winapi_ntdll_RtlGetVersion(&os);
156+
if(status != STATUS_SUCCESS) {
157+
dprintf("[WINAPI][GetWindowsMajorMinVer] RtlGetVersion returned %p", status);
158+
return 0;
159+
}
160+
dwResult = (os.dwMajorVersion << 8 & 0xff00) | (os.dwMinorVersion & 0xff);
161+
return dwResult;
162+
}
148163

149164
Syscall** GetOrInitWinApiSyscalls() {
150165
if (lpWinApiSyscalls == NULL) {
@@ -202,12 +217,16 @@ Syscall** GetOrInitWinApiSyscalls() {
202217
}
203218

204219
BOOL hasDirectSyscallSupport() {
205-
if (lpWinApiSyscalls == NULL) {
206-
if (GetOrInitWinApiSyscalls() == NULL) {
207-
return FALSE;
220+
DWORD dwVersion = GetWindowsMajorMinVer();
221+
DWORD dwMajor = (dwVersion & 0xff00) >> 8;
222+
DWORD dwMinor = dwVersion & 0xff;
223+
if(dwVersion != 0 && (dwMajor >= 6 && dwMinor >= 1)) {
224+
if(lpWinApiSyscalls == NULL) {
225+
GetOrInitWinApiSyscalls();
208226
}
227+
return lpWinApiSyscalls != NULL;
209228
}
210-
return TRUE;
229+
return FALSE;
211230
}
212231

213232

@@ -318,31 +337,38 @@ void* GetFunction(LPCSTR lpModuleName, LPCSTR lpFunctionName) {
318337
// START: ntdll.dll
319338

320339
NTSTATUS winapi_ntdll_ZwAllocateVirtualMemory(HANDLE hProcess, PVOID* pBaseAddress, ULONG_PTR pZeroBits, PSIZE_T pRegionSize, ULONG ulAllocationType, ULONG ulProtect) {
321-
return SyscallStub(lpWinApiSyscalls[ZwAllocateVirtualMemory], hProcess, pBaseAddress, pZeroBits, pRegionSize, ulAllocationType, ulProtect);
340+
ULONG_PTR lpArgs[] = { (ULONG_PTR)hProcess, (ULONG_PTR)pBaseAddress, (ULONG_PTR)pZeroBits, (ULONG_PTR)pRegionSize, (ULONG_PTR)ulAllocationType, (ULONG_PTR)ulProtect };
341+
return SyscallStub(lpWinApiSyscalls[ZwAllocateVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
322342
}
323343

324344
NTSTATUS winapi_ntdll_ZwOpenProcess(PHANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PCLIENT_ID ClientId) {
325-
return SyscallStub(lpWinApiSyscalls[ZwOpenProcess], ProcessHandle, DesiredAccess, ObjectAttributes, ClientId);
345+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)DesiredAccess, (ULONG_PTR)ObjectAttributes, (ULONG_PTR)ClientId };
346+
return SyscallStub(lpWinApiSyscalls[ZwOpenProcess], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
326347
}
327348

328349
NTSTATUS winapi_ntdll_ZwWriteVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, ULONG NumberOfBytesToWrite, PULONG NumberOfBytesWritten) {
329-
return SyscallStub(lpWinApiSyscalls[ZwWriteVirtualMemory], ProcessHandle, BaseAddress, Buffer, NumberOfBytesToWrite, NumberOfBytesWritten);
350+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)BaseAddress, (ULONG_PTR)Buffer, (ULONG_PTR)NumberOfBytesToWrite, (ULONG_PTR)NumberOfBytesWritten };
351+
return SyscallStub(lpWinApiSyscalls[ZwWriteVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
330352
}
331353

332354
NTSTATUS winapi_ntdll_ZwReadVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, ULONG NumberOfBytesToRead, PULONG NumberOfBytesRead) {
333-
return SyscallStub(lpWinApiSyscalls[ZwReadVirtualMemory], ProcessHandle, BaseAddress, Buffer, NumberOfBytesRead, NumberOfBytesRead);
355+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)BaseAddress, (ULONG_PTR)Buffer, (ULONG_PTR)NumberOfBytesToRead, (ULONG_PTR)NumberOfBytesRead };
356+
return SyscallStub(lpWinApiSyscalls[ZwReadVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
334357
}
335358

336359
NTSTATUS winapi_ntdll_ZwProtectVirtualMemory(HANDLE ProcessHandle, PVOID* BaseAddress, PSIZE_T RegionSize, ULONG NewProtect, PULONG OldProtect) {
337-
return SyscallStub(lpWinApiSyscalls[ZwProtectVirtualMemory], ProcessHandle, BaseAddress, RegionSize, NewProtect, OldProtect);
360+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)BaseAddress, (ULONG_PTR)RegionSize, (ULONG_PTR)NewProtect, (ULONG_PTR)OldProtect };
361+
return SyscallStub(lpWinApiSyscalls[ZwProtectVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
338362
}
339363

340364
NTSTATUS winapi_ntdll_ZwQueryVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddress, MEMORY_INFORMATION_CLASS MemoryInformationClass, PVOID MemoryInformation, SIZE_T MemoryInformationLength, PSIZE_T ReturnLength) {
341-
return SyscallStub(lpWinApiSyscalls[ZwQueryVirtualMemory], ProcessHandle, BaseAddress, MemoryInformationClass, MemoryInformation, MemoryInformationLength, ReturnLength);
365+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)BaseAddress, (ULONG_PTR)MemoryInformationClass, (ULONG_PTR)MemoryInformation, (ULONG_PTR)MemoryInformationLength, (ULONG_PTR)ReturnLength };
366+
return SyscallStub(lpWinApiSyscalls[ZwQueryVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
342367
}
343368

344369
NTSTATUS winapi_ntdll_ZwFreeVirtualMemory(HANDLE ProcessHandle, PVOID* BaseAddress, PSIZE_T RegionSize, ULONG FreeType) {
345-
return SyscallStub(lpWinApiSyscalls[ZwFreeVirtualMemory], ProcessHandle, BaseAddress, RegionSize, FreeType);
370+
ULONG_PTR lpArgs[] = { (ULONG_PTR)ProcessHandle, (ULONG_PTR)BaseAddress, (ULONG_PTR)RegionSize, (ULONG_PTR)FreeType };
371+
return SyscallStub(lpWinApiSyscalls[ZwFreeVirtualMemory], sizeof(lpArgs) / sizeof(ULONG_PTR), (ULONG_PTR *)&lpArgs);
346372
}
347373

348374
NTSTATUS winapi_ntdll_NtQueueApcThread(HANDLE ThreadHandle, PVOID ApcRoutine, PVOID ApcContext, PVOID Argument1, PVOID Argument2) {
@@ -363,6 +389,14 @@ NTSTATUS winapi_ntdll_NtOpenThread(PHANDLE ThreadHandle, ACCESS_MASK DesiredAcce
363389
return 0xC0000001; // STATUS_UNSUCCESSFUL
364390
}
365391

392+
NTSTATUS winapi_ntdll_RtlGetVersion(PRTL_OSVERSIONINFOEXW os) {
393+
NTSTATUS (NTAPI *pRtlGetVersion)(PRTL_OSVERSIONINFOEXW os) = GetFunctionH(NTDLL_DLL, H_RtlGetVersion);
394+
dprintf("[WINAPI][winapi_ntdll_RtlGetVersion] Calling RtlGetVersion @ %p", pRtlGetVersion);
395+
if(pRtlGetVersion) {
396+
return pRtlGetVersion(os);
397+
}
398+
return 0xC0000001;
399+
}
366400
// END: ntdll.dll
367401
// START: kernel32.dll
368402

c/meterpreter/source/metsrv/winapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ NTSTATUS winapi_ntdll_ZwQueryVirtualMemory(HANDLE ProcessHandle, PVOID BaseAddre
4848
NTSTATUS winapi_ntdll_ZwFreeVirtualMemory(HANDLE ProcessHandle, PVOID* BaseAddress, PSIZE_T RegionSize, ULONG FreeType);
4949
NTSTATUS winapi_ntdll_NtQueueApcThread(HANDLE ThreadHandle, PVOID ApcRoutine, PVOID ApcContext, PVOID Argument1, PVOID Argument2);
5050
NTSTATUS winapi_ntdll_NtOpenThread(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PCLIENT_ID ClientId);
51+
NTSTATUS winapi_ntdll_RtlGetVersion(PRTL_OSVERSIONINFOEXW os);
5152
BOOL winapi_kernel32_WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten);
5253
BOOL winapi_kernel32_ReadProcessMemory(HANDLE hProcess, LPCVOID lpBaseAddress, LPVOID lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead);
5354
HANDLE winapi_kernel32_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);

0 commit comments

Comments
 (0)