2013-08-22 18:18:38 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Windows Native System Services
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2013-12-04 14:37:57 +04:00
|
|
|
* Copyright 2013 Thincast Technologies GmbH
|
|
|
|
* Copyright 2013 Norbert Federa <norbert.federa@thincast.com>
|
2013-08-22 18:18:38 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-10-24 02:15:10 +04:00
|
|
|
#include <winpr/library.h>
|
2016-06-01 17:26:26 +03:00
|
|
|
#include <winpr/wlog.h>
|
2013-08-22 18:18:38 +04:00
|
|
|
#include <winpr/nt.h>
|
2017-11-23 21:51:57 +03:00
|
|
|
#include <winpr/endian.h>
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
#include "../log.h"
|
|
|
|
#define TAG WINPR_TAG("nt")
|
|
|
|
|
|
|
|
|
2013-08-22 18:18:38 +04:00
|
|
|
/**
|
|
|
|
* NtXxx Routines:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff557720/
|
|
|
|
*/
|
|
|
|
|
2013-10-24 02:57:41 +04:00
|
|
|
/**
|
|
|
|
* InitializeObjectAttributes macro
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff547804/
|
|
|
|
*/
|
|
|
|
|
|
|
|
VOID _InitializeObjectAttributes(POBJECT_ATTRIBUTES InitializedAttributes,
|
|
|
|
PUNICODE_STRING ObjectName, ULONG Attributes, HANDLE RootDirectory,
|
|
|
|
PSECURITY_DESCRIPTOR SecurityDescriptor)
|
|
|
|
{
|
2016-02-06 00:28:45 +03:00
|
|
|
#if defined(_WIN32) && !defined(_UWP)
|
2013-10-24 02:57:41 +04:00
|
|
|
InitializeObjectAttributes(InitializedAttributes, ObjectName,
|
|
|
|
Attributes, RootDirectory, SecurityDescriptor);
|
|
|
|
#else
|
|
|
|
InitializedAttributes->Length = sizeof(OBJECT_ATTRIBUTES);
|
|
|
|
InitializedAttributes->ObjectName = ObjectName;
|
|
|
|
InitializedAttributes->Attributes = Attributes;
|
|
|
|
InitializedAttributes->RootDirectory = RootDirectory;
|
|
|
|
InitializedAttributes->SecurityDescriptor = SecurityDescriptor;
|
|
|
|
InitializedAttributes->SecurityQualityOfService = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2015-12-16 18:25:09 +03:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
|
|
|
struct winpr_nt_file
|
|
|
|
{
|
|
|
|
WINPR_HANDLE_DEF();
|
|
|
|
|
|
|
|
ACCESS_MASK DesiredAccess;
|
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
|
|
ULONG FileAttributes;
|
|
|
|
ULONG ShareAccess;
|
|
|
|
ULONG CreateDisposition;
|
|
|
|
ULONG CreateOptions;
|
|
|
|
};
|
|
|
|
typedef struct winpr_nt_file WINPR_NT_FILE;
|
|
|
|
|
2013-10-24 02:57:41 +04:00
|
|
|
static pthread_once_t _TebOnceControl = PTHREAD_ONCE_INIT;
|
|
|
|
static pthread_key_t _TebKey;
|
|
|
|
|
|
|
|
static void _TebDestruct(void *teb)
|
|
|
|
{
|
|
|
|
free(teb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _TebInitOnce(void)
|
|
|
|
{
|
|
|
|
pthread_key_create(&_TebKey, _TebDestruct);
|
|
|
|
}
|
|
|
|
|
|
|
|
PTEB NtCurrentTeb(void)
|
|
|
|
{
|
|
|
|
PTEB teb = NULL;
|
|
|
|
|
|
|
|
if (pthread_once(&_TebOnceControl, _TebInitOnce) == 0)
|
|
|
|
{
|
|
|
|
if ((teb = pthread_getspecific(_TebKey)) == NULL)
|
|
|
|
{
|
2015-06-14 01:55:09 +03:00
|
|
|
teb = calloc(1, sizeof(TEB));
|
2013-10-24 02:57:41 +04:00
|
|
|
if (teb)
|
|
|
|
pthread_setspecific(_TebKey, teb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return teb;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:30:39 +04:00
|
|
|
/**
|
|
|
|
* RtlInitAnsiString routine:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff561918/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
VOID _RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
|
2013-08-22 21:30:39 +04:00
|
|
|
{
|
|
|
|
DestinationString->Buffer = (PCHAR) SourceString;
|
|
|
|
|
|
|
|
if (!SourceString)
|
|
|
|
{
|
|
|
|
DestinationString->Length = 0;
|
|
|
|
DestinationString->MaximumLength = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
USHORT length = (USHORT) strlen(SourceString);
|
|
|
|
DestinationString->Length = length;
|
|
|
|
DestinationString->MaximumLength = length + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RtlInitUnicodeString routine:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff561934/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
VOID _RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
|
2013-08-22 21:30:39 +04:00
|
|
|
{
|
|
|
|
DestinationString->Buffer = (PWSTR) SourceString;
|
|
|
|
|
|
|
|
if (!SourceString)
|
|
|
|
{
|
|
|
|
DestinationString->Length = 0;
|
|
|
|
DestinationString->MaximumLength = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
USHORT length = (USHORT) _wcslen(SourceString);
|
|
|
|
DestinationString->Length = length * 2;
|
|
|
|
DestinationString->MaximumLength = (length + 1) * 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RtlAnsiStringToUnicodeString function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms648413/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString,
|
2013-08-22 21:30:39 +04:00
|
|
|
PCANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (!SourceString)
|
2016-06-01 17:26:26 +03:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
2013-08-22 21:30:39 +04:00
|
|
|
|
|
|
|
if (AllocateDestinationString)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
PWSTR wbuf = NULL;
|
2013-08-22 21:30:39 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
if (SourceString->MaximumLength)
|
2013-08-22 21:30:39 +04:00
|
|
|
{
|
2017-05-30 11:46:43 +03:00
|
|
|
if (!(wbuf = (PWSTR) calloc(SourceString->MaximumLength, 2)))
|
2016-06-01 17:26:26 +03:00
|
|
|
return STATUS_NO_MEMORY;
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
2016-06-01 17:26:26 +03:00
|
|
|
|
|
|
|
DestinationString->MaximumLength = SourceString->MaximumLength * 2;
|
|
|
|
DestinationString->Buffer = wbuf;
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
if (DestinationString->MaximumLength < SourceString->MaximumLength * 2)
|
|
|
|
return STATUS_BUFFER_OVERFLOW;
|
|
|
|
}
|
2013-08-22 21:30:39 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
for (index = 0; index < SourceString->MaximumLength; index++)
|
|
|
|
{
|
2017-11-23 21:51:57 +03:00
|
|
|
Data_Write_UINT16(&DestinationString->Buffer[index], SourceString->Buffer[index]);
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
DestinationString->Length = SourceString->Length * 2;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RtlFreeUnicodeString function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms648418/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
VOID _RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
|
2013-08-22 21:30:39 +04:00
|
|
|
{
|
|
|
|
if (UnicodeString)
|
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(UnicodeString->Buffer);
|
2013-08-22 21:30:39 +04:00
|
|
|
|
|
|
|
UnicodeString->Length = 0;
|
|
|
|
UnicodeString->MaximumLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 03:20:59 +04:00
|
|
|
/**
|
|
|
|
* RtlNtStatusToDosError function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms680600/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
ULONG _RtlNtStatusToDosError(NTSTATUS status)
|
2013-08-23 03:20:59 +04:00
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:30:39 +04:00
|
|
|
/**
|
|
|
|
* NtCreateFile function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/bb432380/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtCreateFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
2013-08-22 21:30:39 +04:00
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess,
|
|
|
|
ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
2015-12-16 18:25:09 +03:00
|
|
|
WINPR_NT_FILE* pFileHandle;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
2015-12-16 18:25:09 +03:00
|
|
|
pFileHandle = (WINPR_NT_FILE*) calloc(1, sizeof(WINPR_NT_FILE));
|
2013-10-23 04:47:29 +04:00
|
|
|
if (!pFileHandle)
|
2015-04-03 17:21:01 +03:00
|
|
|
return STATUS_NO_MEMORY;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
|
|
|
pFileHandle->DesiredAccess = DesiredAccess;
|
|
|
|
pFileHandle->FileAttributes = FileAttributes;
|
|
|
|
pFileHandle->ShareAccess = ShareAccess;
|
|
|
|
pFileHandle->CreateDisposition = CreateDisposition;
|
|
|
|
pFileHandle->CreateOptions = CreateOptions;
|
|
|
|
|
|
|
|
*((PULONG_PTR) FileHandle) = (ULONG_PTR) pFileHandle;
|
|
|
|
|
2013-10-24 03:32:32 +04:00
|
|
|
//STATUS_ACCESS_DENIED
|
|
|
|
//STATUS_OBJECT_NAME_INVALID
|
2013-10-24 02:15:10 +04:00
|
|
|
//STATUS_OBJECT_PATH_NOT_FOUND
|
|
|
|
//STATUS_OBJECT_NAME_NOT_FOUND
|
|
|
|
|
2013-10-23 04:47:29 +04:00
|
|
|
return STATUS_SUCCESS;
|
2016-06-01 17:26:26 +03:00
|
|
|
#endif
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NtOpenFile function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/bb432381/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtOpenFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
2013-08-22 21:30:39 +04:00
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG ShareAccess, ULONG OpenOptions)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
2015-12-16 18:25:09 +03:00
|
|
|
WINPR_NT_FILE* pFileHandle;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
2015-12-16 18:25:09 +03:00
|
|
|
pFileHandle = (WINPR_NT_FILE*) calloc(1, sizeof(WINPR_NT_FILE));
|
2013-10-23 04:47:29 +04:00
|
|
|
|
|
|
|
if (!pFileHandle)
|
2015-04-03 17:21:01 +03:00
|
|
|
return STATUS_NO_MEMORY;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
|
|
|
pFileHandle->DesiredAccess = DesiredAccess;
|
|
|
|
pFileHandle->ShareAccess = ShareAccess;
|
|
|
|
|
|
|
|
*((PULONG_PTR) FileHandle) = (ULONG_PTR) pFileHandle;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2016-06-01 17:26:26 +03:00
|
|
|
#endif
|
2013-10-23 04:47:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NtReadFile function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff567072/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
2013-10-23 04:47:29 +04:00
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
2013-10-23 04:47:29 +04:00
|
|
|
return STATUS_SUCCESS;
|
2016-06-01 17:26:26 +03:00
|
|
|
#endif
|
2013-10-23 04:47:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NtWriteFile function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff567121/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtWriteFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
2013-10-23 04:47:29 +04:00
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
2013-10-23 04:47:29 +04:00
|
|
|
return STATUS_SUCCESS;
|
2016-06-01 17:26:26 +03:00
|
|
|
#endif
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NtDeviceIoControlFile function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms648411/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtDeviceIoControlFile(HANDLE FileHandle, HANDLE Event,
|
2013-08-22 21:30:39 +04:00
|
|
|
PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG IoControlCode, PVOID InputBuffer, ULONG InputBufferLength,
|
|
|
|
PVOID OutputBuffer, ULONG OutputBufferLength)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
#endif
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NtClose function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms648410/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtClose(HANDLE Handle)
|
2013-08-22 21:30:39 +04:00
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
2015-12-16 18:25:09 +03:00
|
|
|
WINPR_NT_FILE* pFileHandle;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
|
|
|
if (!Handle)
|
2016-06-01 17:26:26 +03:00
|
|
|
return STATUS_SUCCESS;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
2015-12-16 18:25:09 +03:00
|
|
|
pFileHandle = (WINPR_NT_FILE*) Handle;
|
2013-10-23 04:47:29 +04:00
|
|
|
|
|
|
|
free(pFileHandle);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2016-06-01 17:26:26 +03:00
|
|
|
#endif
|
2013-08-22 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 03:20:59 +04:00
|
|
|
/**
|
|
|
|
* NtWaitForSingleObject function:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms648412/
|
|
|
|
*/
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtWaitForSingleObject(HANDLE Handle, BOOLEAN Alertable, PLARGE_INTEGER Timeout)
|
2013-08-23 03:20:59 +04:00
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
#if 1
|
|
|
|
WLog_ERR(TAG, "%s: Not implemented", __FUNCTION__);
|
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
#endif
|
2013-08-23 03:20:59 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
#else
|
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
#include <winpr/synch.h>
|
2013-10-24 02:15:10 +04:00
|
|
|
|
2013-10-24 02:57:41 +04:00
|
|
|
typedef VOID (WINAPI * RTL_INIT_ANSI_STRING_FN)(PANSI_STRING DestinationString, PCSZ SourceString);
|
|
|
|
|
|
|
|
typedef VOID (WINAPI * RTL_INIT_UNICODE_STRING_FN)(PUNICODE_STRING DestinationString, PCWSTR SourceString);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * RTL_ANSI_STRING_TO_UNICODE_STRING_FN)(PUNICODE_STRING DestinationString,
|
|
|
|
PCANSI_STRING SourceString, BOOLEAN AllocateDestinationString);
|
|
|
|
|
|
|
|
typedef VOID (WINAPI * RTL_FREE_UNICODE_STRING_FN)(PUNICODE_STRING UnicodeString);
|
|
|
|
|
|
|
|
typedef ULONG (WINAPI * RTL_NT_STATUS_TO_DOS_ERROR_FN)(NTSTATUS status);
|
|
|
|
|
2013-10-24 02:15:10 +04:00
|
|
|
typedef NTSTATUS (WINAPI * NT_CREATE_FILE_FN)(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess,
|
|
|
|
ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * NT_OPEN_FILE_FN)(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG ShareAccess, ULONG OpenOptions);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * NT_READ_FILE_FN)(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * NT_WRITE_FILE_FN)(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * NT_DEVICE_IO_CONTROL_FILE_FN)(HANDLE FileHandle, HANDLE Event,
|
|
|
|
PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG IoControlCode, PVOID InputBuffer, ULONG InputBufferLength,
|
|
|
|
PVOID OutputBuffer, ULONG OutputBufferLength);
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI * NT_CLOSE_FN)(HANDLE Handle);
|
|
|
|
|
2014-10-13 18:55:11 +04:00
|
|
|
typedef NTSTATUS (WINAPI * NT_WAIT_FOR_SINGLE_OBJECT_FN)(HANDLE Handle, BOOLEAN Alertable, PLARGE_INTEGER Timeout);
|
|
|
|
|
2013-10-24 02:57:41 +04:00
|
|
|
static RTL_INIT_ANSI_STRING_FN pRtlInitAnsiString = NULL;
|
|
|
|
static RTL_INIT_UNICODE_STRING_FN pRtlInitUnicodeString = NULL;
|
|
|
|
static RTL_ANSI_STRING_TO_UNICODE_STRING_FN pRtlAnsiStringToUnicodeString = NULL;
|
|
|
|
static RTL_FREE_UNICODE_STRING_FN pRtlFreeUnicodeString = NULL;
|
|
|
|
static RTL_NT_STATUS_TO_DOS_ERROR_FN pRtlNtStatusToDosError = NULL;
|
2013-10-24 02:15:10 +04:00
|
|
|
static NT_CREATE_FILE_FN pNtCreateFile = NULL;
|
|
|
|
static NT_OPEN_FILE_FN pNtOpenFile = NULL;
|
|
|
|
static NT_READ_FILE_FN pNtReadFile = NULL;
|
|
|
|
static NT_WRITE_FILE_FN pNtWriteFile = NULL;
|
|
|
|
static NT_DEVICE_IO_CONTROL_FILE_FN pNtDeviceIoControlFile = NULL;
|
|
|
|
static NT_CLOSE_FN pNtClose = NULL;
|
2014-10-13 18:55:11 +04:00
|
|
|
static NT_WAIT_FOR_SINGLE_OBJECT_FN pNtWaitForSingleObject = NULL;
|
2013-10-24 02:15:10 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
static INIT_ONCE ntdllInitOnce = INIT_ONCE_STATIC_INIT;
|
2013-10-24 02:15:10 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
static BOOL CALLBACK NtdllModuleInit(PINIT_ONCE once, PVOID param, PVOID *context)
|
|
|
|
{
|
|
|
|
HMODULE NtdllModule = LoadLibraryA("ntdll.dll");
|
2013-10-24 02:15:10 +04:00
|
|
|
|
2016-06-01 17:26:26 +03:00
|
|
|
if (NtdllModule)
|
|
|
|
{
|
|
|
|
pRtlInitAnsiString = (RTL_INIT_ANSI_STRING_FN)GetProcAddress(NtdllModule, "RtlInitAnsiString");
|
|
|
|
pRtlInitUnicodeString = (RTL_INIT_UNICODE_STRING_FN)GetProcAddress(NtdllModule, "RtlInitUnicodeString");
|
|
|
|
pRtlAnsiStringToUnicodeString = (RTL_ANSI_STRING_TO_UNICODE_STRING_FN)GetProcAddress(NtdllModule, "RtlAnsiStringToUnicodeString");
|
|
|
|
pRtlFreeUnicodeString = (RTL_FREE_UNICODE_STRING_FN)GetProcAddress(NtdllModule, "RtlFreeUnicodeString");
|
|
|
|
pRtlNtStatusToDosError = (RTL_NT_STATUS_TO_DOS_ERROR_FN)GetProcAddress(NtdllModule, "RtlNtStatusToDosError");
|
|
|
|
pNtCreateFile = (NT_CREATE_FILE_FN)GetProcAddress(NtdllModule, "NtCreateFile");
|
|
|
|
pNtOpenFile = (NT_OPEN_FILE_FN)GetProcAddress(NtdllModule, "NtOpenFile");
|
|
|
|
pNtReadFile = (NT_READ_FILE_FN)GetProcAddress(NtdllModule, "NtReadFile");
|
|
|
|
pNtWriteFile = (NT_WRITE_FILE_FN)GetProcAddress(NtdllModule, "NtWriteFile");
|
|
|
|
pNtDeviceIoControlFile = (NT_DEVICE_IO_CONTROL_FILE_FN)GetProcAddress(NtdllModule, "NtDeviceIoControlFile");
|
|
|
|
pNtClose = (NT_CLOSE_FN)GetProcAddress(NtdllModule, "NtClose");
|
|
|
|
pNtWaitForSingleObject = (NT_WAIT_FOR_SINGLE_OBJECT_FN)GetProcAddress(NtdllModule, "NtWaitForSingleObject");
|
|
|
|
}
|
|
|
|
return TRUE;
|
2013-10-24 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2013-10-24 02:57:41 +04:00
|
|
|
VOID _RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:57:41 +04:00
|
|
|
|
|
|
|
if (!pRtlInitAnsiString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pRtlInitAnsiString(DestinationString, SourceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID _RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:57:41 +04:00
|
|
|
|
|
|
|
if (!pRtlInitUnicodeString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pRtlInitUnicodeString(DestinationString, SourceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString,
|
|
|
|
PCANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:57:41 +04:00
|
|
|
|
|
|
|
if (!pRtlAnsiStringToUnicodeString)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pRtlAnsiStringToUnicodeString(DestinationString,
|
|
|
|
SourceString, AllocateDestinationString);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID _RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:57:41 +04:00
|
|
|
|
|
|
|
if (!pRtlFreeUnicodeString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pRtlFreeUnicodeString(UnicodeString);
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG _RtlNtStatusToDosError(NTSTATUS status)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:57:41 +04:00
|
|
|
|
|
|
|
if (!pRtlNtStatusToDosError)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
return pRtlNtStatusToDosError(status);
|
|
|
|
}
|
|
|
|
|
2013-10-23 05:38:16 +04:00
|
|
|
NTSTATUS _NtCreateFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess,
|
|
|
|
ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtCreateFile)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtCreateFile(FileHandle, DesiredAccess, ObjectAttributes,
|
|
|
|
IoStatusBlock, AllocationSize, FileAttributes, ShareAccess,
|
|
|
|
CreateDisposition, CreateOptions, EaBuffer, EaLength);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _NtOpenFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
|
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG ShareAccess, ULONG OpenOptions)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtOpenFile)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtOpenFile(FileHandle, DesiredAccess, ObjectAttributes,
|
|
|
|
IoStatusBlock, ShareAccess, OpenOptions);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtReadFile)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtReadFile(FileHandle, Event, ApcRoutine, ApcContext,
|
|
|
|
IoStatusBlock, Buffer, Length, ByteOffset, Key);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _NtWriteFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext,
|
|
|
|
PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtWriteFile)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtWriteFile(FileHandle, Event, ApcRoutine, ApcContext,
|
|
|
|
IoStatusBlock, Buffer, Length, ByteOffset, Key);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _NtDeviceIoControlFile(HANDLE FileHandle, HANDLE Event,
|
|
|
|
PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock,
|
|
|
|
ULONG IoControlCode, PVOID InputBuffer, ULONG InputBufferLength,
|
|
|
|
PVOID OutputBuffer, ULONG OutputBufferLength)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtDeviceIoControlFile)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtDeviceIoControlFile(FileHandle, Event,
|
|
|
|
ApcRoutine, ApcContext, IoStatusBlock, IoControlCode,
|
|
|
|
InputBuffer, InputBufferLength, OutputBuffer, OutputBufferLength);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS _NtClose(HANDLE Handle)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2013-10-24 02:15:10 +04:00
|
|
|
|
|
|
|
if (!pNtClose)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtClose(Handle);
|
2013-10-23 05:38:16 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 18:55:11 +04:00
|
|
|
NTSTATUS _NtWaitForSingleObject(HANDLE Handle, BOOLEAN Alertable, PLARGE_INTEGER Timeout)
|
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
InitOnceExecuteOnce(&ntdllInitOnce, NtdllModuleInit, NULL, NULL);
|
2014-10-13 18:55:11 +04:00
|
|
|
|
|
|
|
if (!pNtWaitForSingleObject)
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return pNtWaitForSingleObject(Handle, Alertable, Timeout);
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:18:38 +04:00
|
|
|
#endif
|
|
|
|
|