-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cpp
66 lines (55 loc) · 1.88 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
#include <syscall.hpp>
#include <Windows.h>
#include <ntstatus.h>
#include <iostream>
typedef HKL(WINAPI *NtUserGetKeyboardLayout)(DWORD idThread);
typedef BOOL(WINAPI *NtUserGetKeyboardLayoutName)(LPWSTR pwszKLID);
int main()
{
LoadLibraryA("user32.dll");
NTSTATUS status = STATUS_SUCCESS;
PVOID base_address = nullptr;
SIZE_T region_size = 0x1000;
status = SYSCALL(NtAllocateVirtualMemory)(
NtCurrentProcess(),
&base_address,
0,
®ion_size,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
if (!NT_SUCCESS(status))
{
std::cout << "[-] Failed to allocate memory: " << std::hex << status << std::endl;
return 1;
}
std::cout << "[+] Allocated memory at: 0x" << std::hex << base_address << std::endl;
status = SYSCALL(NtWriteVirtualMemory)(
NtCurrentProcess(),
base_address,
"Hello, world!",
14,
nullptr);
if (!NT_SUCCESS(status))
{
std::cout << "[-] Failed to write memory: " << std::hex << status << std::endl;
return 1;
}
std::cout << "[+] Wrote memory" << std::endl;
char *allocPointer = reinterpret_cast<char *>(base_address);
std::cout << "[+] Read memory: " << allocPointer << std::endl;
status = SYSCALL(NtFreeVirtualMemory)(
NtCurrentProcess(),
&base_address,
®ion_size,
MEM_RELEASE);
if (!NT_SUCCESS(status))
{
std::cout << "[-] Failed to free memory: " << std::hex << status << std::endl;
return 1;
}
std::cout << "[+] Freed memory" << std::endl;
auto ntUserGetKeyboardLayout = syscall::get_syscall<syscall::hash_str("NtUserGetKeyboardLayout"), NtUserGetKeyboardLayout>();
printf("NtUserGetKeyboardLayout: 0x%p\n", ntUserGetKeyboardLayout);
HKL hkl = ntUserGetKeyboardLayout(0);
printf("NtUserGetKeyboardLayout(0): 0x%p\n", hkl);
}