Skip to content

Commit

Permalink
Use Unicode version of function
Browse files Browse the repository at this point in the history
  • Loading branch information
llccd committed Aug 21, 2021
1 parent 6f70250 commit 19eb457
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs/
x64/
[Rr]elease/
[Dd]ebug/
65 changes: 37 additions & 28 deletions Shell32Patcher/Shell32Patcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,67 @@
#include <Dbghelp.h>
#include <wtsapi32.h>
#include <psapi.h>
#pragma comment(lib, "Dbghelp.lib")
#pragma comment(lib, "Wtsapi32.lib")

#ifndef _CONSOLE
#define printf(...) {}
#endif

HMODULE GetModule(HANDLE hProcess, LPCWSTR target)
{
DWORD cb, cbNeeded;
WCHAR szModName[MAX_PATH];
EnumProcessModulesEx(hProcess, NULL, 0, &cb, LIST_MODULES_64BIT);
auto hMods = (HMODULE*)LocalAlloc(NONZEROLPTR, cb);
if (!hMods) return NULL;
EnumProcessModulesEx(hProcess, hMods, cb, &cbNeeded, LIST_MODULES_64BIT);
if (cbNeeded < cb) cb = cbNeeded;

HMODULE hMod = NULL;
for (DWORD i = 0; i < cb / sizeof(HMODULE); i++) {
if (!GetModuleFileNameExW(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(WCHAR))) continue;
if (!lstrcmpiW(szModName, target)) {
hMod = hMods[i];
break;
}
}
LocalFree(hMods);
return hMod;
}

int main()
{
HANDLE hProcess = GetCurrentProcess();
auto hProcess = GetCurrentProcess();
SymSetOptions(SYMOPT_EXACT_SYMBOLS | SYMOPT_ALLOW_ABSOLUTE_SYMBOLS | SYMOPT_DEBUG | SYMOPT_UNDNAME);
const char* symPath = NULL;
GetEnvironmentVariableA("_NT_SYMBOL_PATH", NULL, 0);
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) symPath = "cache*;srv*https://msdl.microsoft.com/download/symbols";
if (!SymInitialize(hProcess, symPath, FALSE)) return -1;
LPCWSTR symPath = NULL;
GetEnvironmentVariableW(L"_NT_SYMBOL_PATH", NULL, 0);
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) symPath = L"cache*;srv*https://msdl.microsoft.com/download/symbols";
if (!SymInitializeW(hProcess, symPath, FALSE)) return -1;

WCHAR szShell32[MAX_PATH];
lstrcpyW(szShell32 + GetSystemDirectoryW(szShell32, sizeof(szShell32) / sizeof(WCHAR)), L"\\shell32.dll");
if (!SymLoadModuleExW(hProcess, NULL, szShell32, NULL, 0, 0, NULL, 0)) return -2;

SYMBOL_INFO symbol;
symbol.SizeOfStruct = sizeof(SYMBOL_INFO);
SYMBOL_INFOW symbol;
symbol.SizeOfStruct = sizeof(SYMBOL_INFOW);
symbol.MaxNameLen = 0;
if (!SymFromName(hProcess, "CDefView::TryGetContextMenuPresenter", &symbol)) return -3;
if (!SymFromNameW(hProcess, L"CDefView::TryGetContextMenuPresenter", &symbol)) return -3;
printf("Found offset %llX\n", symbol.Address - symbol.ModBase);

PWTS_PROCESS_INFOW processList;
DWORD processCount = 0, pLevel = 0;
WTSEnumerateProcessesExW(WTS_CURRENT_SERVER_HANDLE, &pLevel, WTS_CURRENT_SESSION, (LPWSTR *)&processList, &processCount);
if(!WTSEnumerateProcessesExW(WTS_CURRENT_SERVER_HANDLE, &pLevel, WTS_CURRENT_SESSION, (LPWSTR *)&processList, &processCount))
return -4;

for (DWORD i = 0; i < processCount; i++) {
if (!lstrcmpiW(processList[i].pProcessName, L"explorer.exe")) {
printf("Found explorer PID:%u\n", processList[i].ProcessId);
HANDLE hExplorer = OpenProcess(PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, TRUE, processList[i].ProcessId);
auto hExplorer = OpenProcess(PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, TRUE, processList[i].ProcessId);
if (!hExplorer) continue;

DWORD cb, cbNeeded;
WCHAR szModName[MAX_PATH];
EnumProcessModulesEx(hExplorer, NULL, 0, &cb, LIST_MODULES_64BIT);
auto hMods = (HMODULE *)LocalAlloc(NONZEROLPTR, cb);
if (!hMods) continue;
EnumProcessModulesEx(hExplorer, hMods, cb, &cbNeeded, LIST_MODULES_64BIT);
if (cbNeeded < cb) cb = cbNeeded;

HANDLE hModShell32 = INVALID_HANDLE_VALUE;
for (DWORD i = 0; i < cb / sizeof(HMODULE); i++) {
if (!GetModuleFileNameExW(hExplorer, hMods[i], szModName, sizeof(szModName) / sizeof(WCHAR))) continue;
if (!lstrcmpiW(szModName, szShell32)) {
hModShell32 = hMods[i];
break;
}
}
LocalFree(hMods);
if (hModShell32 == INVALID_HANDLE_VALUE) continue;
auto hModShell32 = GetModule(hExplorer, szShell32);
if (!hModShell32) continue;

// and qword ptr [rdx], 0
// and rax, 0
Expand Down
4 changes: 1 addition & 3 deletions Shell32Patcher/Shell32Patcher.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Dbghelp.lib;Wtsapi32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -97,7 +96,6 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>Dbghelp.lib;Wtsapi32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<EntryPointSymbol>
</EntryPointSymbol>
</Link>
Expand All @@ -111,13 +109,13 @@
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<DebugInformationFormat>None</DebugInformationFormat>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>Dbghelp.lib;Wtsapi32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<EntryPointSymbol>main</EntryPointSymbol>
</Link>
</ItemDefinitionGroup>
Expand Down

0 comments on commit 19eb457

Please sign in to comment.