■ ■ ■ ■ ■ ■ ■
SearchAvailableExe/Tools.cpp
| skipped 671 lines |
672 | 672 | | return 0; |
673 | 673 | | } |
674 | 674 | | |
| 675 | + | bool fixExportTable(string targetFilePath, string sourceFilePath) { |
| 676 | + | char* targetBuffer; |
| 677 | + | DWORD fileSize = readFileContext(targetFilePath, &targetBuffer); |
| 678 | + | |
| 679 | + | PIMAGE_DOS_HEADER pDH = (PIMAGE_DOS_HEADER)targetBuffer; |
| 680 | + | PIMAGE_NT_HEADERS pNtH = (PIMAGE_NT_HEADERS)((DWORD)pDH + pDH->e_lfanew); |
| 681 | + | PIMAGE_OPTIONAL_HEADER pOH = &pNtH->OptionalHeader; |
| 682 | + | IMAGE_DATA_DIRECTORY exportDirectory; |
| 683 | + | |
| 684 | + | if (*(PWORD)((size_t)pDH + pDH->e_lfanew + 0x18) == IMAGE_NT_OPTIONAL_HDR32_MAGIC) |
| 685 | + | { |
| 686 | + | PIMAGE_NT_HEADERS32 pNtH32 = PIMAGE_NT_HEADERS32((size_t)pDH + pDH->e_lfanew); |
| 687 | + | PIMAGE_OPTIONAL_HEADER32 pOH32 = &pNtH32->OptionalHeader; |
| 688 | + | |
| 689 | + | exportDirectory = pOH32->DataDirectory[0]; |
| 690 | + | } |
| 691 | + | else { |
| 692 | + | PIMAGE_NT_HEADERS64 pNtH64 = PIMAGE_NT_HEADERS64((size_t)pDH + pDH->e_lfanew); |
| 693 | + | PIMAGE_OPTIONAL_HEADER64 pOH64 = &pNtH64->OptionalHeader; |
| 694 | + | |
| 695 | + | exportDirectory = pOH64->DataDirectory[0]; |
| 696 | + | } |
| 697 | + | |
| 698 | + | IMAGE_EXPORT_DIRECTORY* exportDir = (IMAGE_EXPORT_DIRECTORY*)(targetBuffer + rvaToFOA(targetBuffer, exportDirectory.VirtualAddress)); |
| 699 | + | |
| 700 | + | DWORD* nameRVAs = (DWORD*)(targetBuffer + rvaToFOA(targetBuffer, exportDir->AddressOfNames)); |
| 701 | + | |
| 702 | + | char* sourceBuffer; |
| 703 | + | readFileContext(sourceFilePath, &sourceBuffer); |
| 704 | + | |
| 705 | + | pDH = (PIMAGE_DOS_HEADER)sourceBuffer; |
| 706 | + | pNtH = (PIMAGE_NT_HEADERS)((size_t)pDH + pDH->e_lfanew); |
| 707 | + | pOH = &pNtH->OptionalHeader; |
| 708 | + | |
| 709 | + | if (*(PWORD)((size_t)pDH + pDH->e_lfanew + 0x18) == IMAGE_NT_OPTIONAL_HDR32_MAGIC) |
| 710 | + | { |
| 711 | + | PIMAGE_NT_HEADERS32 pNtH32 = PIMAGE_NT_HEADERS32((size_t)pDH + pDH->e_lfanew); |
| 712 | + | PIMAGE_OPTIONAL_HEADER32 pOH32 = &pNtH32->OptionalHeader; |
| 713 | + | |
| 714 | + | exportDirectory = pOH32->DataDirectory[0]; |
| 715 | + | } |
| 716 | + | else { |
| 717 | + | PIMAGE_NT_HEADERS64 pNtH64 = PIMAGE_NT_HEADERS64((size_t)pDH + pDH->e_lfanew); |
| 718 | + | PIMAGE_OPTIONAL_HEADER64 pOH64 = &pNtH64->OptionalHeader; |
| 719 | + | |
| 720 | + | exportDirectory = pOH64->DataDirectory[0]; |
| 721 | + | } |
| 722 | + | |
| 723 | + | IMAGE_EXPORT_DIRECTORY* exportDir_source = (IMAGE_EXPORT_DIRECTORY*)(sourceBuffer + rvaToFOA(sourceBuffer, exportDirectory.VirtualAddress)); |
| 724 | + | |
| 725 | + | DWORD* nameRVAs_source = (DWORD*)(sourceBuffer + rvaToFOA(sourceBuffer, exportDir_source->AddressOfNames)); |
| 726 | + | |
| 727 | + | if (exportDir_source->NumberOfNames > 100) { |
| 728 | + | delete[] targetBuffer; |
| 729 | + | delete[] sourceBuffer; |
| 730 | + | return false; |
| 731 | + | } |
| 732 | + | |
| 733 | + | for (int i = 0; i < exportDir_source->NumberOfNames; i++) |
| 734 | + | { |
| 735 | + | DWORD nameRVA_source = nameRVAs_source[i]; |
| 736 | + | char* exportFunctionName_source = sourceBuffer + rvaToFOA(sourceBuffer, nameRVA_source); |
| 737 | + | |
| 738 | + | DWORD nameRVA = nameRVAs[i]; |
| 739 | + | char* exportFunctionName = targetBuffer + rvaToFOA(targetBuffer, nameRVA); |
| 740 | + | |
| 741 | + | memcpy(exportFunctionName, exportFunctionName_source, strlen(exportFunctionName_source) + 1); |
| 742 | + | } |
| 743 | + | |
| 744 | + | saveFile(targetFilePath, targetBuffer, fileSize); |
| 745 | + | |
| 746 | + | delete[] targetBuffer; |
| 747 | + | delete[] sourceBuffer; |
| 748 | + | |
| 749 | + | return true; |
| 750 | + | } |
| 751 | + | |
675 | 752 | | std::string GetCurrentPath() { |
676 | 753 | | char buffer[MAX_PATH]; |
677 | 754 | | GetModuleFileNameA(NULL, buffer, MAX_PATH); |
| skipped 90 lines |
768 | 845 | | return true; |
769 | 846 | | } |
770 | 847 | | |
771 | | - | int TestCreateProcess(string runFilePath) { |
772 | | - | // 定义进程信息结构体 |
| 848 | + | int TestCreateProcess(string runFilePath, DWORD dwMilliseconds) { |
773 | 849 | | STARTUPINFOA si = { sizeof(si) }; |
774 | 850 | | PROCESS_INFORMATION pi; |
775 | 851 | | |
776 | | - | // 创建进程 |
777 | 852 | | if (!CreateProcessA( |
778 | 853 | | nullptr, // 指向可执行文件名的指针(在这里,nullptr表示使用当前可执行文件) |
779 | 854 | | (char*)runFilePath.c_str(), // 可执行文件的路径 |
| skipped 9 lines |
789 | 864 | | } |
790 | 865 | | |
791 | 866 | | // 等待进程结束 |
792 | | - | WaitForSingleObject(pi.hProcess, 500); |
| 867 | + | WaitForSingleObject(pi.hProcess, dwMilliseconds); |
793 | 868 | | |
794 | 869 | | TerminateProcess(pi.hProcess, 0); |
795 | 870 | | |
| skipped 31 lines |
827 | 902 | | exitCode++; |
828 | 903 | | } |
829 | 904 | | |
830 | | - | DWORD retExitCode = TestCreateProcess(runFilePath); |
| 905 | + | DWORD retExitCode = TestCreateProcess(runFilePath, 500); |
831 | 906 | | result->exploitDllPath = hookDllMap[retExitCode]; |
832 | 907 | | |
833 | | - | while(!DeleteDirectory(folderPath.c_str())){} |
| 908 | + | if (result->exploitDllPath != "") { |
| 909 | + | string hookFilePath = currentPath + "\\TestLoad_x86.dll"; |
| 910 | + | if (result->bit == 64) |
| 911 | + | hookFilePath = currentPath + "\\TestLoad_x64.dll"; |
| 912 | + | |
| 913 | + | string targetFilePath = folderPath + "\\" + result->exploitDllPath; |
| 914 | + | CopyFileA(hookFilePath.c_str(), targetFilePath.c_str(), FALSE); |
| 915 | + | bool isSucc = fixExportTable(targetFilePath, result->fileDir + result->exploitDllPath); |
| 916 | + | |
| 917 | + | if (isSucc) { |
| 918 | + | targetFilePath = folderPath + "\\" + result->filePath.substr(result->filePath.find_last_of("\\/") + 1); |
| 919 | + | |
| 920 | + | TestCreateProcess(targetFilePath, 3000); |
| 921 | + | } |
| 922 | + | |
| 923 | + | if (!std::filesystem::exists(folderPath + "\\test.txt")) |
| 924 | + | result->exploitDllPath = ""; |
| 925 | + | } |
| 926 | + | |
| 927 | + | while (!DeleteDirectory(folderPath.c_str())) {} |
834 | 928 | | } |