Skip to content

Instantly share code, notes, and snippets.

@Bigpet
Created July 6, 2018 13:05
Show Gist options
  • Save Bigpet/7b9740b8af8a8aa7ed60746654f71a6a to your computer and use it in GitHub Desktop.
Save Bigpet/7b9740b8af8a8aa7ed60746654f71a6a to your computer and use it in GitHub Desktop.
NtCreateFileRepro
#include <string>
#include <iostream>
#include <Windows.h>
#include <winternl.h>
#include <ntstatus.h>
#pragma comment(lib, "NtDll.lib")
int main(int argc, char *argv[])
{
HANDLE h;
PHANDLE hndl = &h;
ACCESS_MASK desiredAccess = FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | SYNCHRONIZE;
POBJECT_ATTRIBUTES attr;
IO_STATUS_BLOCK blk;
PIO_STATUS_BLOCK block = &blk;
OBJECT_ATTRIBUTES att;
std::wstring fstr = LR"(\??\c:\t\p\)";
_UNICODE_STRING str{ fstr.size() * 2, fstr.size() + 1 * 2, (PWSTR)fstr.c_str() };
InitializeObjectAttributes(&att, &str, OBJ_CASE_INSENSITIVE, NULL , NULL);
attr = &att;
NTSTATUS ret = NtCreateFile(
hndl,
desiredAccess,
attr,
block,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN,
FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_ALERT,
NULL,
0
);
if (ret == STATUS_SUCCESS) {
std::cout << "opened successfully" << std::endl;
}
else {
std::cout << "got error" << std::endl;
auto err = GetLastError();
wchar_t buf[256];
if (err)
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
std::wcout << buf << std::endl;
if (ret == STATUS_OBJECT_NAME_NOT_FOUND)
{
std::cout << "STATUS_OBJECT_NAME_NOT_FOUND" << std::endl;
}
else if (ret == STATUS_OBJECT_PATH_NOT_FOUND)
{
std::cout << "STATUS_OBJECT_PATH_NOT_FOUND" << std::endl;
}
else
{
std::cout << "unknown ntstatus: " << ret << std::endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment