■ ■ ■ ■ ■ ■
SearchAvailableExe/Tools.cpp
skipped 154 lines 155 155 strcat(fileFullPath, fileDir.c_str()); 156 156 int fileDirLength = fileDir.length(); 157 157 map<string, bool> postDllMap; 158 - char* secNames[ ] = { " . rdata " , " . rsrc " } ; 158 + char** secNames; 159 + int cnt = 0; 160 + 161 + if (c.isAllSectionSearch) { 162 + PIMAGE_DOS_HEADER pDH = (PIMAGE_DOS_HEADER)buffer; 163 + _IMAGE_SECTION_HEADER* sectionHeader; 164 + IMAGE_FILE_HEADER fh; 165 + 166 + if (*(PWORD)((size_t)pDH + pDH->e_lfanew + 0x18) == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { 167 + PIMAGE_NT_HEADERS32 pNtH32 = PIMAGE_NT_HEADERS32((size_t)pDH + pDH->e_lfanew); 168 + sectionHeader = (_IMAGE_SECTION_HEADER*)((UINT)pNtH32 + sizeof(_IMAGE_NT_HEADERS)); 169 + fh = pNtH32->FileHeader; 170 + } 171 + else { 172 + PIMAGE_NT_HEADERS64 pNtH64 = PIMAGE_NT_HEADERS64((size_t)pDH + pDH->e_lfanew); 173 + sectionHeader = (_IMAGE_SECTION_HEADER*)((UINT)pNtH64 + sizeof(_IMAGE_NT_HEADERS64)); 174 + fh = pNtH64->FileHeader; 175 + } 159 176 160 - for (int i = 0; i < 2; i++) { 177 + char* temp[0x10]; 178 + secNames = (char**)malloc(sizeof(size_t) * fh.NumberOfSections); 179 + while (cnt < fh.NumberOfSections) { 180 + _IMAGE_SECTION_HEADER* section; 181 + section = (_IMAGE_SECTION_HEADER*)((UINT)sectionHeader + sizeof(_IMAGE_SECTION_HEADER) * cnt); 182 + temp[cnt++] = (char*)(section->Name); 183 + } 184 + secNames = temp; 185 + } 186 + else { 187 + char* temp[] = { ".rdata", ".rsrc" }; 188 + secNames = temp; 189 + cnt = 2; 190 + } 191 + 192 + for (int i = 0; i < cnt; i++) { 161 193 BYTE* rdata = readSectionData(buffer, &rdataLength, secNames[i]); 162 194 if (rdata != 0) { 163 195 DWORD vaule, vaule1; skipped 268 lines 432 464 return payloadFileSize; 433 465 } 434 466 435 - void saveFile(string filePath, char* buffer, DWORD fileSize) 467 + bool saveFile(string filePath, char* buffer, DWORD fileSize)436 468 { 437 469 std::ofstream outFile; 438 470 outFile.open(filePath, std::ios::binary | std::ios::trunc); 471 + if (!outFile.is_open()) { 472 + printf("Failed to open file for writing.\n"); 473 + return false; 474 + } 439 475 outFile.write(buffer, fileSize); 440 476 outFile.close(); 477 + 478 + return true; 441 479 } 442 480 443 481 void str_to_lower(char* str) { skipped 114 lines 558 596 559 597 int fixFile(string targetFilePath, DWORD exitCode) 560 598 { 599 + DWORD attributes = GetFileAttributesA(targetFilePath.c_str()); 600 + if (attributes != INVALID_FILE_ATTRIBUTES) { 601 + attributes &= ~FILE_ATTRIBUTE_READONLY; // 清除只读属性 602 + SetFileAttributesA(targetFilePath.c_str(), attributes); 603 + } 604 + 561 605 bool isExeFile = targetFilePath.back() == 'e' ? true : false; 562 606 563 607 char* targetBuffer; skipped 128 lines 692 736 memset(tmp_ImportTable, 0, 0x14); 693 737 } 694 738 695 - saveFile(targetFilePath, targetBuffer, fileSize); 739 + bool isSucc = saveFile(targetFilePath, targetBuffer, fileSize); 696 740 697 741 delete[] targetBuffer; 698 742 699 - return 0 ; 743 + return isSucc ; 700 744 } 701 745 702 746 bool fixExportTable(string targetFilePath, string sourceFilePath) { skipped 124 lines 827 871 828 872 if (isNeedHook) { 829 873 std::lock_guard<std::mutex> lock(mtx); 830 - fixFile(targetFilePath, exitCode); 874 + bool isSucc = fixFile(targetFilePath, exitCode); 875 + if (!isSucc) 876 + return ""; 831 877 } 832 878 833 879 return targetFilePath; skipped 134 lines