-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
69 lines (46 loc) · 1.77 KB
/
example.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
#include <iostream>
#include <vector>
#include <fstream>
#include <Windows.h>
#include "pdb.hpp"
std::vector<char> read_file(std::string file_name)
{
std::ifstream file_stream(file_name, std::ios::binary | std::ios::ate);
std::streamsize stream_size = file_stream.tellg();
file_stream.seekg(0, std::ios::beg);
std::vector<char> buffer(stream_size);
file_stream.read(buffer.data(), stream_size);
return buffer;
}
int main()
{
// read ntoskrnl exe and pdb file
auto ntoskrnl_exe = read_file(R"(C:\Windows\System32\ntoskrnl.exe)");
auto ntoskrnl_pdb = read_file(R"(C:\Windows\ntoskrnl.pdb)");
// get ntoskrnl data
auto ntoskrnl_dos = reinterpret_cast<PIMAGE_DOS_HEADER>(ntoskrnl_exe.data());
auto ntoskrnl_nt = reinterpret_cast<PIMAGE_NT_HEADERS64>(ntoskrnl_exe.data() + ntoskrnl_dos->e_lfanew);
auto ntoskrnl_sections = IMAGE_FIRST_SECTION(ntoskrnl_nt);
// parse pdb file
pdb::stream_data_t symbols{};
pdb::parse_buffer(ntoskrnl_pdb.data(), &symbols);
// iterate symbols
auto symbols_current = reinterpret_cast<uintptr_t>(symbols.buffer);
while (symbols_current != (reinterpret_cast<uintptr_t>(symbols.buffer) + symbols.size))
{
const auto it = reinterpret_cast<pdb::pubsym32_t*>(symbols_current);
symbols_current += it->reclen + 2ull;
if (it->rectyp != pdb::S_PUB32)
continue;
if (strcmp(it->name, "MiGetPteAddress") != 0)
continue;
// get function info
printf("S_PUB32: [%04X:%08X], flags: %08X, name: %s\n", it->seg, it->off, it->pubsymflags, it->name);
std::cout << "symbol section: " << ntoskrnl_sections[it->seg - 1].Name << std::endl;
std::cout << "symbol rva: " << std::hex << (ntoskrnl_sections[it->seg - 1].VirtualAddress + it->off) << std::dec << std::endl;
}
// free pdb data
pdb::free_memory(symbols.buffer);
// done
return 0;
}