Skip to content

Commit 37824da

Browse files
Fixed bugs in the RegistryManager
1 parent 91c418f commit 37824da

File tree

8 files changed

+154
-8
lines changed

8 files changed

+154
-8
lines changed

PEParser/PEParser.cpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99

1010

1111
PEParser::PEParser(const wchar_t* path) :_path(path) {
12-
_hFile = ::CreateFile(path, GENERIC_READ | GENERIC_WRITE,
13-
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
12+
_hFile = ::CreateFile(path, GENERIC_READ,
13+
FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1414
if (_hFile == INVALID_HANDLE_VALUE)
1515
return;
1616
::GetFileSizeEx(_hFile, &_fileSize);
17-
_hMemMap = ::CreateFileMapping(_hFile, nullptr, PAGE_READWRITE, 0, 0, nullptr);
17+
_hMemMap = ::CreateFileMapping(_hFile, nullptr, PAGE_READONLY, 0, 0, nullptr);
1818
if (!_hMemMap)
1919
return;
2020

21-
_address = (PBYTE)::MapViewOfFile(_hMemMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
21+
_address = (PBYTE)::MapViewOfFile(_hMemMap, FILE_MAP_READ, 0, 0, 0);
2222
if (!_address)
2323
return;
2424

@@ -496,4 +496,54 @@ void PEParser::RelocateImageByDelta(std::vector<RelocInfo>& relocs, const uint64
496496

497497
PVOID PEParser::GetDataDirectoryAddress(UINT index, PULONG size) const {
498498
return ::ImageDirectoryEntryToData(_address, FALSE, index, size);
499+
}
500+
501+
void PEParser::SetDefaultFileAligment() {
502+
if (IsPe64()) {
503+
GetOptionalHeader64().FileAlignment = _fileAlignmentConstant;
504+
}
505+
else {
506+
GetOptionalHeader32().FileAlignment = _fileAlignmentConstant;
507+
}
508+
}
509+
510+
DWORD PEParser::GetSectionAlignment() {
511+
if (IsPe64()) {
512+
return GetOptionalHeader64().SectionAlignment;
513+
}
514+
else {
515+
return GetOptionalHeader32().SectionAlignment;
516+
}
517+
}
518+
519+
DWORD PEParser::GetFileAlignment() {
520+
if (IsPe64()) {
521+
return GetOptionalHeader64().FileAlignment;
522+
}
523+
else {
524+
return GetOptionalHeader32().FileAlignment;
525+
}
526+
}
527+
528+
DWORD PEParser::AlignValue(DWORD badValue, DWORD alignTo) {
529+
return (badValue + alignTo - 1) & ~(alignTo - 1);
530+
}
531+
532+
void PEParser::AlignAllSectionHeaders() {
533+
auto sections = _sections;
534+
DWORD sectionAlignment = GetSectionAlignment();
535+
DWORD fileAlignment = GetFileAlignment();
536+
DWORD newFileSize = 0;
537+
538+
newFileSize = _dosHeader->e_lfanew + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) +
539+
_ntHeader->FileHeader.SizeOfOptionalHeader * sizeof(IMAGE_SECTION_HEADER);
540+
541+
for (int i = 0; i < GetSectionCount(); ++i) {
542+
sections[i].VirtualAddress = AlignValue(sections[i].VirtualAddress, sectionAlignment);
543+
sections[i].Misc.VirtualSize = AlignValue(sections[i].Misc.VirtualSize, sectionAlignment);
544+
545+
sections[i].PointerToRawData = AlignValue(newFileSize, fileAlignment);
546+
547+
newFileSize = sections[i].PointerToRawData + sections[i].SizeOfRawData;
548+
}
499549
}

PEParser/PEParser.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct RelocInfo {
152152
uint32_t count;
153153
};
154154

155-
class PEParser final {
155+
class PEParser {
156156
public:
157157
explicit PEParser(const wchar_t* path);
158158
~PEParser();
@@ -171,6 +171,8 @@ class PEParser final {
171171
const IMAGE_DATA_DIRECTORY* GetDataDirectory(int index) const;
172172
const IMAGE_DOS_HEADER& GetDosHeader() const;
173173
void* GetBaseAddress() const;
174+
void AlignAllSectionHeaders();
175+
DWORD AlignValue(DWORD badValue, DWORD alignTo);
174176

175177
ULONGLONG GetImageBase() const;
176178

@@ -214,6 +216,9 @@ class PEParser final {
214216
//const IMAGE_LOAD_CONFIG_DIRECTORY64* GetLoadConfiguration64() const;
215217
//const IMAGE_LOAD_CONFIG_DIRECTORY32* GetLoadConfiguration32() const;
216218
PVOID GetDataDirectoryAddress(UINT index, PULONG size) const;
219+
void SetDefaultFileAligment();
220+
DWORD GetSectionAlignment();
221+
DWORD GetFileAlignment();
217222

218223
bool IsImportLib() const;
219224
bool IsObjectFile() const;
@@ -229,6 +234,8 @@ class PEParser final {
229234
std::vector<RelocInfo> GetRelocs(void* imageBase);
230235
static void RelocateImageByDelta(std::vector<RelocInfo>& relocs, const uint64_t delta);
231236

237+
protected:
238+
static const DWORD _fileAlignmentConstant = 0x200;
232239

233240
private:
234241
bool IsObjectPe64() const;

WinArk/GotoKeyDlg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ LRESULT CGotoKeyDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lPa
6969
{ L"Lsa",LR"(HKLM\SYSTEM\CurrentControlSet\Control\Lsa)"},
7070
{ L"LogonUI",LR"(HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI)"},
7171
{ L"Credential Providers",LR"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\Credential Providers)"},
72-
{ L"DisallowRun",LR"(HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun)"},
72+
{ L"DisallowCpl",LR"(HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowCpl)"},
7373
{ L"DisablePath",LR"(HKEY_CURRENT_USER\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths)"},
7474
{ L"Internet Settings",LR"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings)"},
7575
{ L"Session Manager",LR"(HKLM\System\CurrentControlSet\Control\Session Manager)"},

WinArk/ImportRebuilder.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "stdafx.h"
2+
#include "ImportRebuilder.h"
3+
4+
bool ImportRebuilder::RebuildImportTable(const WCHAR* newFilePath,
5+
std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap){
6+
bool ret = false;
7+
std::map<DWORD_PTR, ImportModuleThunk> copyModule;
8+
copyModule.insert(moduleThunkMap.begin(), moduleThunkMap.end());
9+
10+
if (IsValid()) {
11+
SetDefaultFileAligment();
12+
13+
ret = BuildNewImportTable(copyModule);
14+
if (ret) {
15+
AlignAllSectionHeaders();
16+
17+
}
18+
}
19+
20+
return ret;
21+
}
22+
23+
bool ImportRebuilder::BuildNewImportTable(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap) {
24+
25+
return true;
26+
}

WinArk/ImportRebuilder.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <PEParser.h>
5+
#include "Thunks.h"
6+
#include "IATReferenceScan.h"
7+
#include <PEParser.h>
8+
9+
class ImportRebuilder: public PEParser{
10+
public:
11+
ImportRebuilder(const WCHAR* file): PEParser(file) {
12+
}
13+
bool RebuildImportTable(const WCHAR* newFilePath, std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
14+
void EnableOFTSupport();
15+
void EnableNewIATInSection(DWORD_PTR iatAddress, DWORD iatSize);
16+
17+
private:
18+
PIMAGE_IMPORT_DESCRIPTOR _pImportDescriptor = nullptr;
19+
PIMAGE_THUNK_DATA _pThunkData = nullptr;
20+
PIMAGE_IMPORT_BY_NAME _pImportByName = nullptr;
21+
22+
size_t _numberOfImportDescriptors;
23+
size_t _sizeOfImportSection;
24+
size_t _sizeOfApiAndModuleNames;
25+
size_t _importSectionIndex;
26+
27+
// OriginalFirstThunk Array in import section
28+
size_t _sizeOfOFTArray;
29+
bool _useOFT;
30+
bool _newIATInSection;
31+
DWORD_PTR _iatAddress;
32+
33+
DWORD _iatSize;
34+
DWORD _sizeOfJumpTable;
35+
36+
DWORD _directImportsJumpTableRVA;
37+
BYTE* _pJmpTableMemory;
38+
DWORD _newIATBaseAddressRVA;
39+
40+
DWORD FillImportSection(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
41+
BYTE* GetMemoryPointerFromRVA(DWORD_PTR rva);
42+
bool CreateNewImportSection(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
43+
bool BuildNewImportTable(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
44+
void SetFlagToIATSection(DWORD_PTR iatAddress);
45+
size_t AddImportToImportTable(ImportThunk* pImportThunk, PIMAGE_THUNK_DATA* pThunkData, PIMAGE_IMPORT_BY_NAME pImportByName,
46+
DWORD sectionOffset);
47+
size_t AddImportDescriptor(ImportModuleThunk* pImportThunk, DWORD sectionOffset, DWORD sectionOffsetOFTArray);
48+
49+
void CalculateImportSize(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
50+
51+
void AddSepecialImportDescriptor(DWORD_PTR rvaFirstThunk, DWORD sectionOffsetOFTArray);
52+
void PatchFileForNewIATLocation();
53+
void ChangeIATBaseAddress(std::map<DWORD_PTR, ImportModuleThunk>& moduleThunkMap);
54+
void PatchFileForDirectImportJumpTable();
55+
};

WinArk/View.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ LRESULT CRegistryManagerView::OnEditDelete(WORD, WORD, HWND, BOOL&){
13751375
}
13761376
list->AddCommand(cmd);
13771377
}
1378-
if (count == 1) // only up key selected
1378+
if (count == 0) // only up key selected
13791379
return 0;
13801380

13811381
if (count == 1)

WinArk/WinArk.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<Optimization>Disabled</Optimization>
110110
<PreprocessorDefinitions>_WIN64;_WINDOWS;STRICT;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
111111
<LanguageStandard>stdcpplatest</LanguageStandard>
112-
<AdditionalIncludeDirectories>..\PEParser;..\WinSysCore;..\PdbParser;..\Utils</AdditionalIncludeDirectories>
112+
<AdditionalIncludeDirectories>..\PEParser;..\WinSysCore;..\PdbParser;..\Utils;..\diStorm3\include</AdditionalIncludeDirectories>
113113
<ConformanceMode>true</ConformanceMode>
114114
<ExceptionHandling>Sync</ExceptionHandling>
115115
<TreatWarningAsError>false</TreatWarningAsError>
@@ -261,6 +261,7 @@
261261
<ClCompile Include="HexEdit.cpp" />
262262
<ClCompile Include="IATReferenceScan.cpp" />
263263
<ClCompile Include="IATSearcher.cpp" />
264+
<ClCompile Include="ImportRebuilder.cpp" />
264265
<ClCompile Include="ImportsHandling.cpp" />
265266
<ClCompile Include="KernelEATHookDlg.cpp" />
266267
<ClCompile Include="KernelEATHookTable.cpp" />
@@ -434,6 +435,7 @@
434435
<ClInclude Include="HexEdit.h" />
435436
<ClInclude Include="IATReferenceScan.h" />
436437
<ClInclude Include="IATSearcher.h" />
438+
<ClInclude Include="ImportRebuilder.h" />
437439
<ClInclude Include="ImportsHandling.h" />
438440
<ClInclude Include="KernelEATHookDlg.h" />
439441
<ClInclude Include="KernelEATHookTable.h" />

WinArk/WinArk.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@
531531
<ClCompile Include="IATReferenceScan.cpp">
532532
<Filter>Scylla</Filter>
533533
</ClCompile>
534+
<ClCompile Include="ImportRebuilder.cpp">
535+
<Filter>Scylla</Filter>
536+
</ClCompile>
534537
</ItemGroup>
535538
<ItemGroup>
536539
<ClInclude Include="stdafx.h">
@@ -1070,6 +1073,9 @@
10701073
<ClInclude Include="IATReferenceScan.h">
10711074
<Filter>Scylla</Filter>
10721075
</ClInclude>
1076+
<ClInclude Include="ImportRebuilder.h">
1077+
<Filter>Scylla</Filter>
1078+
</ClInclude>
10731079
</ItemGroup>
10741080
<ItemGroup>
10751081
<ResourceCompile Include="WinArk.rc">

0 commit comments

Comments
 (0)