-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMain.cpp
193 lines (177 loc) · 8.54 KB
/
Main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <Windows.h>
#include <winnt.h>
#include <iostream>
#include <clocale>
#include "Structs.h"
#include <conio.h>
PTEB RtlGetThreadEnvironmentBlock() {
#if _WIN64
return (PTEB)__readgsqword(0x30);
#else
return (PTEB)__readfsdword(0x16);
#endif
}
void printBanner() {
const char* banner ="\n"
" .oooooo. oooo .o8 \n"
" d8P' `Y8b `888 \"888 \n"
" 888 .ooooo. 888 .ooooo. 888oooo. .ooooo. oooo d8b ooo. .oo. \n"
" 888 d88' `88b 888 d88' `88b d88' `88b d88' `88b `888\"\"8P `888P\"Y88b \n"
" 888 888ooo888 888 888ooo888 888 888 888 888 888 888 888 \n"
" 88b ooo 888 .o 888 888 .o 888 888 888 888 888 888 888 \n"
" `Y8bood8P' `Y8bod8P' o888o `Y8bod8P' `Y8bod8P' `Y8bod8P' d888b o888o o888o \n"
" by @R0h1rr1m \n";
std::cout << banner << std::endl;
}
int nameException(const char* functionName) {
const char* listOfNames[] = { "NtGetTickCount","NtQuerySystemTime","NtdllDefWindowProc_A","NtdllDefWindowProc_W","NtdllDialogWndProc_A","NtdllDialogWndProc_W" };
for (int i = 0; i < 6; i++) {
if (strcmp(functionName, listOfNames[i]) == 0) {
return 0;
}
}
return 1;
}
bool replaceTheContentOfFunction(const PCHAR funcNameForUnhook, PBYTE destinationAddress, PBYTE cleanNTDLLModule) {
PBYTE imageBaseAddressOfNTDLL = (PBYTE)cleanNTDLLModule;
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)imageBaseAddressOfNTDLL;
PIMAGE_NT_HEADERS imageNTHeaders = (PIMAGE_NT_HEADERS)(imageBaseAddressOfNTDLL + dosHeader->e_lfanew);
PIMAGE_EXPORT_DIRECTORY imageExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(imageBaseAddressOfNTDLL + imageNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
PDWORD nameArray = (PDWORD)(imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfNames);
PWORD ordinalArray = (PWORD)(imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfNameOrdinals);
PDWORD addressArray = (PDWORD)(imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfFunctions);
PCHAR functionNameTemp;
PBYTE functionAddrTemp;
bool fixedOrNot = false;
for (unsigned int i = 0; i < imageExportDirectory->NumberOfNames; i++) {
functionNameTemp = (PCHAR)(imageBaseAddressOfNTDLL + nameArray[i]);
functionAddrTemp = (PBYTE)(imageBaseAddressOfNTDLL + addressArray[ordinalArray[i]]);
if (strncmp(functionNameTemp, funcNameForUnhook, strlen(funcNameForUnhook)) == 0) {
memcpy(destinationAddress,functionAddrTemp,24);
fixedOrNot = (destinationAddress[0] == 0x4C && destinationAddress[1] == 0x8B && destinationAddress[2] == 0xD1 && destinationAddress[3] == 0xB8);
break;
}
}
return fixedOrNot;
}
PVOID loadModuleAsSection(UNICODE_STRING * dllPath) {
HANDLE ntdllHandle = NULL;
HANDLE hSection = NULL;
IO_STATUS_BLOCK IoStatusBlock;
ZeroMemory(&IoStatusBlock, sizeof(IoStatusBlock));
OBJECT_ATTRIBUTES FileObjectAttributes;
PVOID sectionBaseAddress = 0;
SIZE_T viewSize = 0;
UNICODE_STRING ucFilepath;
WCHAR wcFilepath[100] = L"\\??\\\\";
wcscat_s(wcFilepath, dllPath->Buffer);
RtlInitUnicodeString(&ucFilepath, wcFilepath);
InitializeObjectAttributes(&FileObjectAttributes,&ucFilepath, 0x00000040L, NULL, NULL);
// Ask 1
NTSTATUS status = NtCreateFileArbitrary(&ntdllHandle, FILE_GENERIC_READ, &FileObjectAttributes, &IoStatusBlock, 0,
FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, 0x1, 0x00000020, NULL, 0);
if (ntdllHandle == INVALID_HANDLE_VALUE || status != 0) {
std::cout << "[ERROR] Cannot open the clean version" << std::endl;
exit(1);
}
status = NtCreateSectionArbitrary(&hSection, SECTION_ALL_ACCESS,NULL,0, PAGE_READONLY, SEC_IMAGE,ntdllHandle);
if (status != 0) {
std::cout << "[ERROR] Cannot create a section" << std::endl;
exit(1);
}
status = ZwMapViewOfSectionArbitrary(hSection, GetCurrentProcess(), §ionBaseAddress, NULL, NULL, NULL, &viewSize, ViewShare, NULL, PAGE_READONLY);
if (status != 0x40000003){
std::cout << "[ERROR] Cannot map the section failed" << std::endl;
exit(1);
}
std::cout << "[DONE] New section is created for clean NTDLL.dll at 0x" << std::hex << (ULONG_PTR)sectionBaseAddress << "\n";
CloseHandle(hSection);
CloseHandle(ntdllHandle);
return sectionBaseAddress;
}
int main(char *argv,int argc) {
printBanner();
PTEB pCurrentTeb = RtlGetThreadEnvironmentBlock();
PPEB pCurrentPeb = pCurrentTeb->ProcessEnvironmentBlock;
if (!pCurrentPeb || !pCurrentTeb || pCurrentPeb->OSMajorVersion != 0xA)
return 0;
PVOID newSectionForNTDLL;
PLDR_DATA_TABLE_ENTRY ntdllModule = NULL;
PLIST_ENTRY beginningOfTheList = &pCurrentPeb->LoaderData->InMemoryOrderModuleList;
PLIST_ENTRY cursorOfModules = beginningOfTheList->Flink;
PLDR_DATA_TABLE_ENTRY currentModule;
int count = 0;
//ZwOpenProcessArbitrary();
while (cursorOfModules != beginningOfTheList) {
currentModule = (PLDR_DATA_TABLE_ENTRY) ((PBYTE)cursorOfModules - 0x10);
if (wcscmp(currentModule->BaseDllName.Buffer, L"ntdll.dll") == 0) {
std::cout << "[FOUND] Loaded Module Index of NTDLL.dll is " << count << std::endl;
ntdllModule = currentModule;
}
count++;
//std::wcout << currentModule->BaseDllName.Buffer << std::endl;
cursorOfModules = cursorOfModules->Flink;
}
if (ntdllModule) {
newSectionForNTDLL = loadModuleAsSection(&ntdllModule->FullDllName);
PBYTE imageBaseAddressOfNTDLL = (PBYTE) ntdllModule->DllBase;
//PBYTE imageBaseAddressOfNTDLL = (PBYTE)newSectionForNTDLL;
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)imageBaseAddressOfNTDLL;
PIMAGE_NT_HEADERS imageNTHeaders = (PIMAGE_NT_HEADERS)(imageBaseAddressOfNTDLL + dosHeader->e_lfanew);
PIMAGE_EXPORT_DIRECTORY imageExportDirectory = (PIMAGE_EXPORT_DIRECTORY)(imageBaseAddressOfNTDLL + imageNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
PDWORD nameArray = (PDWORD) (imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfNames);
PWORD ordinalArray = (PWORD) (imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfNameOrdinals);
PDWORD addressArray = (PDWORD) (imageBaseAddressOfNTDLL + imageExportDirectory->AddressOfFunctions);
PCHAR functionName;
PBYTE functionAddr;
PIMAGE_SECTION_HEADER textSection = (PIMAGE_SECTION_HEADER) (((PBYTE)imageNTHeaders) + sizeof(IMAGE_NT_HEADERS));
//Change permission of the section
for (unsigned int i = 0; i < imageNTHeaders->FileHeader.NumberOfSections; i++) {
if (strcmp((const char *)textSection[i].Name,".text") == 0) {
std::cout << "[FOUND] Text Section Found" << std::endl;
textSection = &textSection[i];
break;
}
}
ULONG oldProtection = 0;
LPVOID lpBaseAddress = imageBaseAddressOfNTDLL + textSection->VirtualAddress;
SIZE_T sizeOfSection= textSection->Misc.VirtualSize;
bool returnFlag;
//VirtualProtect((LPVOID)((DWORD_PTR)imageBaseAddressOfNTDLL + (DWORD_PTR)textSection->VirtualAddress), textSection->Misc.VirtualSize, PAGE_EXECUTE_READWRITE, &oldProtection);
NTSTATUS status = ZwProtectVirtualMemoryArbitrary(GetCurrentProcess(), &lpBaseAddress, &sizeOfSection, PAGE_EXECUTE_READWRITE, &oldProtection);
if (status != 0) {
std::cout << "[ERROR] Cannot change the permission of Text Section" << std::endl;
exit(0);
}
//Print exported functions
for (unsigned int i = 0; i < imageExportDirectory->NumberOfNames; i++) {
functionName = (PCHAR)( imageBaseAddressOfNTDLL + nameArray[i]);
functionAddr = (PBYTE)(imageBaseAddressOfNTDLL + addressArray[ordinalArray[i]]);
if (strncmp(functionName, "Nt", 2) == 0){
//std::cout << "bulundu" << functionName << std::endl;
if (!(functionAddr[0] == 0x4C && functionAddr[1] == 0x8B && functionAddr[2] == 0xD1 && functionAddr[3] == 0xB8) && nameException(functionName)) {
std::cout << "[WARNING] Potential Hook : " << functionName << std::endl;
returnFlag = replaceTheContentOfFunction(functionName, functionAddr,(PBYTE) newSectionForNTDLL);
if (returnFlag) {
std::cout << "[SUCCESS] Unhook success for " << functionName << std::endl;
}
else {
std::cout << "[FAILED] Unhook failed for " << functionName << std::endl;
}
}
}
//std::wcout << functionName << std::endl;
}
status = ZwProtectVirtualMemoryArbitrary(GetCurrentProcess(), &lpBaseAddress, &sizeOfSection, oldProtection, &oldProtection);
if (status != 0) {
std::cout << "[ERROR] Cannot restore the permission of Text Section" << std::endl;
exit(0);
}
}
else {
std::cout << "[ERROR] Cannot Find NTDLL.dll" << std::endl;
}
std::cout << "Press a key to continue ..." << std::endl;
_getch();
return 0;
}