-
Notifications
You must be signed in to change notification settings - Fork 1
/
NtPebTeb.cpp
139 lines (126 loc) · 5.02 KB
/
NtPebTeb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
///////////////////////////////////////////////////////////////////////////////
///
/// Small program to learn about the PEB and TEB on various Windows versions
///
///////////////////////////////////////////////////////////////////////////////
///
/// Copyright (c) 2021, 2023 Oliver Schneider (assarbad.net)
///
/// Permission is hereby granted, free of charge, to any person obtaining a
/// copy of this software and associated documentation files (the "Software"),
/// to deal in the Software without restriction, including without limitation
/// the rights to use, copy, modify, merge, publish, distribute, sublicense,
/// and/or sell copies of the Software, and to permit persons to whom the
/// Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS IN THE SOFTWARE.
///
/// SPDX-License-Identifier: MIT
///
///////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <cstdio>
#include <tchar.h>
#ifdef _DEBUG
# include <crtdbg.h>
#else
# define _ASSERTE(...) \
do \
{ \
if (__VA_ARGS__) \
{ \
} \
} while (0)
#endif // _DEBUG
#define NTPEBLDR_UNDERHANDED
#define NTPEBLDR_PRINT_FUNCS
// #include "ntnative.h"
// #include "ntpebldr.h"
// #include "ntnative.h"
#include "ntpebldr.hpp"
using NT::PebLdrOrder;
NTSTATUS CALLBACK printcb(NT::LDR_DATA_TABLE_ENTRY_CTX const& ldrctx, NT::LDR_DATA_TABLE_ENTRY const* /*ldrdataentry*/, ULONG&) // not using the ULONG at all
{
print_ldr_entry_ctx(ldrctx);
return STATUS_NOT_FOUND;
}
constexpr wchar_t const* GetPebLdrOrderName(PebLdrOrder order)
{
switch (order)
{
case NT::PebLdrOrder::load:
return L"InLoadOrder";
case NT::PebLdrOrder::memory:
return L"InMemoryOrder";
case NT::PebLdrOrder::init:
return L"InInitializationOrder";
default:
return L"<unknown>";
}
}
template <PebLdrOrder order_v = PebLdrOrder::load> void showPebLdrData()
{
ULONG ctx{};
wprintf(L"%s\n", GetPebLdrOrderName(order_v));
NTSTATUS Status = NT::IteratePebLdrDataTable<ULONG, order_v>(printcb, ctx);
if (STATUS_NO_MORE_ENTRIES != Status)
{
wprintf(L" WARNING: got NT status %08X when we expected %08X\n", Status, STATUS_NO_MORE_ENTRIES);
}
}
int wmain(int, wchar_t**)
{
wprintf(L"SystemRoot = %ws\n", NT::SystemRoot);
wprintf(L"NT = %u.%u (arch: %u)\n", NT::MajorVersion, NT::MinorVersion, NT::NativeProcessorArchitecture);
_ASSERTE(NT::GetNtDll() == ::GetModuleHandleW(L"ntdll.dll"));
_ASSERTE(NT::GetNtDll() == ::GetModuleHandleW(L"ntdll"));
_ASSERTE(NT::GetNtDll() == NT::GetModHandleByLoadOrderIndex(1));
_ASSERTE(NT::GetNtDll() == NT::GetModHandleByInitializationOrderIndex(0));
_ASSERTE(NT::GetKernel32() == ::GetModuleHandleW(L"kernel32.dll"));
_ASSERTE(NT::GetKernel32() == ::GetModuleHandleW(L"kernel32"));
wprintf(L"Module #2 ntdll : 0x%08p\n", NT::GetNtDll());
auto usNtDllDir = NT::GetNtDllDirectory();
wprintf(L"\t-> %wZ\n", &usNtDllDir);
auto* ldrdata = NT::GetPebLdr();
if (ldrdata)
{
WCHAR const name[] = L"kernelbase.dll";
HMODULE hMod = NT::GetModuleHandleW(name);
_ASSERTE(hMod == ::GetModuleHandleW(name));
if (!hMod)
{
fwprintf(stderr, L"Failed to retrieve module handle for %ws: 0x%08p\n", name, hMod);
return 1;
}
wprintf(L"Module handle retrieved for %ws: 0x%08p\n", name, hMod);
FARPROC pFunc1Native = NT::GetProcAddress(hMod, "GetProcAddress");
FARPROC pFunc2Native = NT::GetProcAddress(hMod, (LPCSTR)682U);
wprintf(L"via name : 0x%08p (PEB)\n", pFunc1Native);
wprintf(L"via ordinal: 0x%08p (PEB)\n", pFunc2Native);
FARPROC pFunc1Win32 = ::GetProcAddress(hMod, "GetProcAddress");
FARPROC pFunc2Win32 = ::GetProcAddress(hMod, (LPCSTR)682U);
wprintf(L"via name : 0x%08p (Win32)\n", pFunc1Win32);
wprintf(L"via ordinal: 0x%08p (Win32)\n", pFunc2Win32);
wprintf(L"================================================\n");
print_ldr_data(*ldrdata);
showPebLdrData<PebLdrOrder::load>();
showPebLdrData<PebLdrOrder::init>();
showPebLdrData<PebLdrOrder::memory>();
}
else
{
fwprintf(stderr, L"Failed to retrieve loader data from PEB!\n");
return 1;
}
return 0;
}