Skip to content

Commit

Permalink
Finally it works
Browse files Browse the repository at this point in the history
  • Loading branch information
frkngksl committed Aug 12, 2021
1 parent 888badd commit 4f9ccfd
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 0 additions & 3 deletions HuanLoader/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,9 @@ void peLoader(unsigned char* baseAddr) {
IMAGE_DATA_DIRECTORY* relocTable = getRelocTable(ntHeader);
ULONGLONG preferableAddress = ntHeader->OptionalHeader.ImageBase;
HMODULE ntdllHandler = LoadLibraryA("ntdll.dll");
std::cout << "Test" << std::endl;
//Unmap the preferable address
((int(WINAPI*)(HANDLE, PVOID))GetProcAddress(ntdllHandler, "NtUnmapViewOfSection"))((HANDLE)-1, (LPVOID)ntHeader->OptionalHeader.ImageBase);
std::cout << "Test2" << std::endl;
BYTE *imageBaseForPE = (BYTE*)VirtualAlloc((LPVOID) preferableAddress, ntHeader->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
std::cout << "Test3" << std::endl;
if (!imageBaseForPE && !relocTable){
std::cout << "[!] No Relocation Table and Cannot load to the preferable address" << std::endl;
return;
Expand Down
6 changes: 3 additions & 3 deletions NewSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@



char* readBinary(char* fileName,size_t *givenFileSize) {
char* readBinary(const char* fileName,size_t *givenFileSize) {
FILE* fileHandler = fopen(fileName, "rb+");
char* binaryContent = NULL;
size_t fileSize = 0;
Expand All @@ -36,7 +36,7 @@ bool saveNewPE(char* newFile, size_t lengthOfFile, const char* fileName) {
sectionHeaderArrays(newFile)[ntHeaders(newFile)->FileHeader.NumberOfSections - 1].VirtualAddress +
sectionHeaderArrays(newFile)[ntHeaders(newFile)->FileHeader.NumberOfSections - 1].Misc.VirtualSize;

ntHeaders(newFile)->OptionalHeader.DllCharacteristics &= ~(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE);
ntHeaders(newFile)->OptionalHeader.DllCharacteristics = 0x8160;
FILE* fileHandler = fopen(fileName, "wb");
if (fileHandler) {
fwrite(newFile, 1, lengthOfFile, fileHandler);
Expand Down Expand Up @@ -100,7 +100,7 @@ char * createNewSectionHeader(char* imageBase, unsigned char* packedContent, siz
newSectionHeader->SizeOfRawData = P2ALIGNUP(totalLengthForSection, ntHeaderOfImage->OptionalHeader.FileAlignment);
//Section alignment for memory
newSectionHeader->Misc.VirtualSize = P2ALIGNUP((totalLengthForSection), ntHeaderOfImage->OptionalHeader.SectionAlignment);
newSectionHeader->Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
newSectionHeader->Characteristics = 0x40000040;
//Offset for file
newSectionHeader->PointerToRawData = newSectionOffset;
// Section Alignment trick and put correct address wrt last section
Expand Down
2 changes: 1 addition & 1 deletion NewSection.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
char* readBinary(char* fileName, size_t *fileSize);
char* readBinary(const char* fileName, size_t *fileSize);
char* createNewSectionHeader(char* imageBase,unsigned char* packedContent, size_t packedLength, size_t *newFileSize);
bool saveNewPE(char* newFile, size_t lengthOfFile, const char* fileName);

0 comments on commit 4f9ccfd

Please sign in to comment.