ChangeModuleName - Modifies the module name of a loaded module at runtime, which might trip up attackers and make certain parts of their code fail. Please see my project "ChangeModuleName" for more details requirements: ensure the new module name is the same or less length of the one you are changing or else you need to shift memory properly where all module names are being stored, and requir
| 149 | returns `true` on successfully renaming `szModule` to `newName`. |
| 150 | */ |
| 151 | bool Process::ChangeModuleName(__in const wstring moduleName, __in const wstring newName) |
| 152 | { |
| 153 | #ifdef _M_IX86 |
| 154 | MYPEB* PEB = (MYPEB*)__readfsdword(0x30); |
| 155 | #else |
| 156 | MYPEB* PEB = (MYPEB*)__readgsqword(0x60); |
| 157 | #endif |
| 158 | |
| 159 | _LIST_ENTRY* f = PEB->Ldr->InMemoryOrderModuleList.Flink; |
| 160 | |
| 161 | bool Found = FALSE; |
| 162 | int count = 0; |
| 163 | |
| 164 | while (!Found && count < 1024) //traverse module list , stops at 1024 loops to prevent any possible infinite looping |
| 165 | { |
| 166 | MY_PLDR_DATA_TABLE_ENTRY dataEntry = CONTAINING_RECORD(f, MY_LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); |
| 167 | |
| 168 | if (wcsstr(dataEntry->FullDllName.Buffer, moduleName.c_str())) |
| 169 | { |
| 170 | wcscpy_s(dataEntry->FullDllName.Buffer, moduleName.size() + 1, newName.c_str()); //..then modify the string modulename to newName |
| 171 | dataEntry->FullDllName.Length = (newName.length() * 2) + 1; |
| 172 | dataEntry->FullDllName.MaximumLength = (newName.length() * 2) + 1; |
| 173 | Found = TRUE; |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | f = dataEntry->InMemoryOrderLinks.Flink; |
| 178 | count++; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | ChangeNumberOfSections - changes the number of sections in the NT Headers to `newSectionsCount`, which can stop attackers from traversing sections in our program |