From 43143ba8716382dc8a9edd427f024c0e0a3cb231 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Thu, 18 May 2023 23:39:56 +0200 Subject: Added wrapper generator for functions that require an import library. * generate wrapper functions that can be retrieved via `MmGetSystemRoutineAddress` * for now, only ZwTraceControl and ZwTraceEvent from ntdll are used Signed-off-by: Toni Uhlig --- CRT/gen_wrapper.sh | 99 ++++++++++++++++++++++++++++++++++++++++++++++ CRT/kcrt.c | 13 ++++++ CRT/ntdll_zw_functions.c | 49 +++++++++++++++++++++++ CRT/ntdll_zw_functions.txt | 2 + 4 files changed, 163 insertions(+) create mode 100755 CRT/gen_wrapper.sh create mode 100644 CRT/ntdll_zw_functions.c create mode 100644 CRT/ntdll_zw_functions.txt (limited to 'CRT') diff --git a/CRT/gen_wrapper.sh b/CRT/gen_wrapper.sh new file mode 100755 index 0000000..565859b --- /dev/null +++ b/CRT/gen_wrapper.sh @@ -0,0 +1,99 @@ +#!/usr/bin/env sh + +MYDIR="$(dirname ${0})" +FN_FILE="${1:-${MYDIR}/ntdll_zw_functions.txt}" + +TYPEDEFS="" +STATICS="" +CURLINE=0 +while read -r line; do + CURLINE=$(expr ${CURLINE} + 1) + VALID=1 + + rtype=$(printf '%s\n' "${line}" | grep -oE '(NTSTATUS NTAPI|VOID NTAPI)') + if [ -z "${rtype}" ]; then + printf '%s\n' "Line ${CURLINE}: Missing return value of either type 'NTSTATUS NTAPI' or 'VOID NTAPI'." >&2 + VALID=0 + fi + + fnname=$(printf '%s\n' "${line}" | grep -oE 'Zw[^ (]*') + if [ -z "${fnname}" ]; then + printf '%s\n' "Line ${CURLINE}: Missing function name." >&2 + VALID=0 + fi + + fnsig=$(printf '%s\n' "${line}" | grep -oE '\([^;]*') + if [ -z "${fnsig}" ]; then + printf '%s\n' "Line ${CURLINE}: Missing function signature." >&2 + VALID=0 + fi + + param_names=$(printf '%s\n' "${fnsig}" | tr -d '()' | sed 's/\([^,]*\)/\1\n/g' | grep -oE '[^ ]*$') + if [ -z "${param_names}" ]; then + printf '%s\n' "Line ${CURLINE}: Could not parse function parameters." >&2 + VALID=0 + fi + params="" + for param in ${param_names}; do + if [ -z "${param}" ]; then + printf '%s\n' "Line ${CURLINE}: Invalid parameter found. Please re-check regex'es used." >&2 + VALID=0 + fi + params="${params}${param}, " + done + params=$(printf '%s\n' "${params}" | sed 's/^\(.*\), $/\1/g') + if [ -z "${params}" ]; then + printf '%s\n' "Line ${CURLINE}: Parameters empty. Please re-check regex'es used." >&2 + VALID=0 + fi + + if [ ${VALID} -eq 1 ]; then + TYPEDEFS="${TYPEDEFS}\ntypedef ${rtype} (*${fnname}_t) ${fnsig};" + STATICS="${STATICS}\nstatic ${fnname}_t _${fnname} = NULL;" + INITS=$(cat < + +EOF +echo "${TYPEDEFS}" +echo "${STATICS}" +cat < + + +typedef NTSTATUS NTAPI (*ZwTraceControl_t) (_In_ ULONG FunctionCode, PVOID InBuffer, _In_ ULONG InBufferLen, PVOID OutBuffer, _In_ ULONG OutBufferLen, _Out_ PULONG ReturnLength); +typedef NTSTATUS NTAPI (*ZwTraceEvent_t) (_In_ HANDLE TraceHandle, _In_ ULONG Flags, _In_ ULONG FieldSize, _In_ PVOID Fields); + +static ZwTraceControl_t _ZwTraceControl = NULL; +static ZwTraceEvent_t _ZwTraceEvent = NULL; + +int __cdecl ntdll_zw_functions (void) +{ + int retval = 0; + UNICODE_STRING fnName; + + RtlInitUnicodeString(&fnName, L"ZwTraceControl"); + _ZwTraceControl = MmGetSystemRoutineAddress(&fnName); + if (_ZwTraceControl == NULL) + { + DbgPrint("%s\n", "System routine ZwTraceControl not found."); + retval++; + } + RtlInitUnicodeString(&fnName, L"ZwTraceEvent"); + _ZwTraceEvent = MmGetSystemRoutineAddress(&fnName); + if (_ZwTraceEvent == NULL) + { + DbgPrint("%s\n", "System routine ZwTraceEvent not found."); + retval++; + } + + return retval; +} + + +NTSTATUS NTAPI ZwTraceControl (_In_ ULONG FunctionCode, PVOID InBuffer, _In_ ULONG InBufferLen, PVOID OutBuffer, _In_ ULONG OutBufferLen, _Out_ PULONG ReturnLength) +{ + if (_ZwTraceControl == NULL) + return STATUS_PROCEDURE_NOT_FOUND; + + return _ZwTraceControl (FunctionCode, InBuffer, InBufferLen, OutBuffer, OutBufferLen, ReturnLength); +} + +NTSTATUS NTAPI ZwTraceEvent (_In_ HANDLE TraceHandle, _In_ ULONG Flags, _In_ ULONG FieldSize, _In_ PVOID Fields) +{ + if (_ZwTraceEvent == NULL) + return STATUS_PROCEDURE_NOT_FOUND; + + return _ZwTraceEvent (TraceHandle, Flags, FieldSize, Fields); +} diff --git a/CRT/ntdll_zw_functions.txt b/CRT/ntdll_zw_functions.txt new file mode 100644 index 0000000..2581487 --- /dev/null +++ b/CRT/ntdll_zw_functions.txt @@ -0,0 +1,2 @@ +NTSYSCALLAPI NTSTATUS NTAPI ZwTraceControl (_In_ ULONG FunctionCode, PVOID InBuffer, _In_ ULONG InBufferLen, PVOID OutBuffer, _In_ ULONG OutBufferLen, _Out_ PULONG ReturnLength) +NTSYSCALLAPI NTSTATUS NTAPI ZwTraceEvent (_In_ HANDLE TraceHandle, _In_ ULONG Flags, _In_ ULONG FieldSize, _In_ PVOID Fields) -- cgit v1.2.3