{"id":354,"date":"2022-07-20T10:34:00","date_gmt":"2022-07-20T02:34:00","guid":{"rendered":"http:\/\/www.aqwu.net\/wp\/?p=354"},"modified":"2022-07-20T10:34:00","modified_gmt":"2022-07-20T02:34:00","slug":"addexeimport-%e5%b0%86%e7%a1%ac%e7%bc%96%e7%a0%81%e7%9a%84-dll-%e4%be%9d%e8%b5%96%e9%a1%b9%e6%b7%bb%e5%8a%a0%e5%88%b0%e4%bb%bb%e4%bd%95-exe","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=354","title":{"rendered":"AddExeImport &#8211; \u5c06\u786c\u7f16\u7801\u7684 DLL \u4f9d\u8d56\u9879\u6dfb\u52a0\u5230\u4efb\u4f55 EXE"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.x86matthew.com\/view_post?id=add_exe_import<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u9879\u76ee\u677e\u6563\u5730\u57fa\u4e8e\u6211\u51e0\u4e2a\u6708\u524d\u53d1\u5e03\u7684\u53e6\u4e00\u4e2a\u9879\u76ee\uff08ImportDLLInjection\uff09\u3002ImportDLLInjection \u5c06\u5728\u6302\u8d77\u72b6\u6001\u4e0b\u542f\u52a8 EXE\uff0c\u4fee\u6539\u5185\u5b58\u4e2d\u7684 IAT \u8868\u4ee5\u5305\u542b\u989d\u5916\u7684 DLL\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u5c06\u5bfc\u81f4\u6ce8\u5165\u6211\u4eec\u7684\u76ee\u6807 DLL \u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u5728\u6b64\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u7c7b\u4f3c\u7684\u6982\u5ff5\u6765\u4fee\u6539\u78c1\u76d8\u4e0a\u7684 EXE \u6587\u4ef6\uff0c\u4ee5\u6dfb\u52a0\u989d\u5916\u7684 DLL \u5bfc\u5165\u6761\u76ee\u3002\u8fd9\u6bd4\u4fee\u6539\u5185\u5b58\u4e2d\u7684 PE \u6807\u5934\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5728\u5411\u6807\u5934\u6dfb\u52a0\u989d\u5916\u6761\u76ee\u7684\u540c\u65f6\u7ef4\u62a4 EXE \u6587\u4ef6\u7684\u539f\u59cb\u7ed3\u6784\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.x86matthew.com\/img\/add_exe_import.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6b64\u65b9\u6cd5\u53ef\u80fd\u5bf9\u6301\u4e45\u6027\u6709\u7528 &#8211; \u4f8b\u5982\uff0c\u5982\u679c\u7cfb\u7edf\u5df2\u7ecf\u5728\u542f\u52a8\u65f6\u8fd0\u884c\u7279\u5b9a\u8fdb\u7a0b\uff08abc.exe\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6b64EXE\u4ee5\u5728\u540e\u53f0\u81ea\u52a8\u52a0\u8f7dDLL\u3002<\/p>\n\n\n\n<p>\u5982\u4e0a\u4e00\u7bc7\u6587\u7ae0\u6240\u8ff0\uff0cIAT \u8868\u4e2d\u7684 DLL \u6761\u76ee\u9700\u8981\u5bfc\u5165\u81f3\u5c11\u4e00\u4e2a\u51fd\u6570\u3002\u6211\u4eec\u5c06\u518d\u6b21\u4f7f\u7528\u5e8f\u6570 #1\u3002IAT \u8868\u4e5f\u5c06\u91cd\u65b0\u5b9a\u4f4d\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u56e0\u4e3a\u73b0\u6709\u6807\u5934\u4e2d\u4e0d\u592a\u53ef\u80fd\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u6765\u6dfb\u52a0\u989d\u5916\u7684 DLL\u3002<\/p>\n\n\n\n<p>\u6211\u8fd8\u9996\u6b21\u4e3a\u6b64\u9879\u76ee\u6dfb\u52a0\u4e86 64 \u4f4d\u652f\u6301\u3002\u6b64\u5de5\u5177\u53ef\u4ee5\u7f16\u8bd1\u4e3a 32\/64 \u4f4d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u540c\u65f6\u9002\u7528\u4e8e\u8fd9\u4e24\u79cd\u4f53\u7cfb\u7ed3\u6784\u3002<\/p>\n\n\n\n<p>\u6b64\u5de5\u5177\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1.\u5c06\u539f\u59cbEXE\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<br>2. \u68c0\u67e5 EXE \u662f 32 \u4f4d\u8fd8\u662f 64 \u4f4d &#8211; \u8fd9\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a NT \u6807\u5934\u7684\u957f\u5ea6\u4e0d\u540c\u3002<br>3. \u4ece<strong>IMAGE_DIRECTORY_ENTRY_IMPORT<\/strong>\u6570\u636e\u76ee\u5f55\u4e2d\u67e5\u627e\u539f\u59cb\u5bfc\u5165\u8868\u4f4d\u7f6e\u3002<br>4. \u5c06\u5bfc\u5165\u8868\u4f4d\u7f6e\u4ece\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u6587\u4ef6\u6307\u9488 &#8211; \u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u81ea\u52a8\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u79f0\u4e3a <strong>VirtualAddressToFilePtr<\/strong>\u3002<br>5. \u83b7\u53d6\u539f\u59cb\u5bfc\u5165\u8868\u7684\u526f\u672c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002<br>6. \u521b\u5efa\u6b64\u5bfc\u5165\u8868\u7684\u514b\u9686\u7248\u672c\uff0c\u5e76\u4e3a\u6211\u4eec\u7684\u65b0 DLL \u6587\u4ef6\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u6761\u76ee\u3002<br>7. \u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u7684 EXE \u8f93\u51fa\u6587\u4ef6\u3002<br>8. \u5c06\u539f\u59cb EXE \u6587\u4ef6\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u8f93\u51fa\u6587\u4ef6\uff08\u76f4\u5230\u6700\u540e\u4e00\u8282\u7684\u672b\u5c3e\uff09\u3002<br>9. \u5c06\u65b0\u7684\u6269\u5c55 IAT \u8868\u9644\u52a0\u5230\u65b0\u7684 EXE \u6587\u4ef6\uff08\u7d27\u8ddf\u5728\u6700\u540e\u4e00\u8282\u4e4b\u540e\uff09\u3002<br>10. \u66f4\u65b0<strong>IMAGE_DIRECTORY_ENTRY_IMPORT<\/strong>\u76ee\u5f55\u4ee5\u6307\u5411\u65b0\u7684 IAT \u8868\u3002<br>11. \u589e\u52a0\u4e0a\u4e00\u8282\u7684 <strong>SizeOfRawData\u00a0<\/strong>\u548c\u00a0<strong>VirtualSize\u00a0<\/strong>\u5b57\u6bb5\uff0c\u4ee5\u786e\u4fdd\u65b0\u7684 IAT \u8868\u6b63\u786e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u78c1\u76d8\u4e0a\u7684\u5206\u533a\u6570\u636e\u5fc5\u987b\u4e0e\u00a0<strong>pImageNtHeader->OptionalHeader.FileAlignment\u00a0<\/strong>\u7684\u503c\u5bf9\u9f50\uff0c\u56e0\u6b64\u6b64\u5904\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u586b\u5145\u3002<br>12. \u68c0\u67e5\u539f\u59cb EXE \u4e2d\u5728\u6700\u540e\u4e00\u4e2a\u6807\u5934\u4e4b\u540e\u662f\u5426\u5b58\u5728\u4efb\u4f55\u5269\u4f59\u6570\u636e\u3002\u8c03\u8bd5\u7b26\u53f7\u548c\u5d4c\u5165\u6587\u4ef6\u901a\u5e38\u9644\u52a0\u5230EXE\u6587\u4ef6\u7684\u672b\u5c3e &#8211; \u8fd9\u4e9b\u4e0d\u662fEXE\u683c\u5f0f\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5b83\u4eec\u4e0d\u4f1a\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u5982\u6709\u5fc5\u8981\uff0c<strong>pImageNtHeader->FileHeader.PointerToSymbolTable\u00a0<\/strong>\u503c\u5c06\u66f4\u65b0\u4e3a\u6307\u5411\u6b63\u786e\u7684\u4f4d\u7f6e\uff0c\u5c3d\u7ba1\u8fd9\u4e9b\u503c\u4e0d\u592a\u53ef\u80fd\u5b58\u5728\u4e8e\u751f\u4ea7\u8f6f\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;windows.h>\n\nDWORD LoadFileIntoMemory(char *pPath, BYTE **pFileData, DWORD *pdwFileSize)\n{\n\tHANDLE hFile = NULL;\n\tDWORD dwFileSize = 0;\n\tBYTE *pFileDataBuffer = NULL;\n\tDWORD dwBytesRead = 0;\n\n\t\/\/ open file\n\thFile = CreateFile(pPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n\tif(hFile == INVALID_HANDLE_VALUE)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ calculate file size\n\tdwFileSize = GetFileSize(hFile, NULL);\n\n\t\/\/ allocate buffer\n\tpFileDataBuffer = (BYTE*)malloc(dwFileSize);\n\tif(pFileDataBuffer == NULL)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ read file contents\n\tif(ReadFile(hFile, pFileDataBuffer, dwFileSize, &amp;dwBytesRead, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ verify byte count\n\tif(dwBytesRead != dwFileSize)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ close file handle\n\tCloseHandle(hFile);\n\n\t\/\/ store values\n\t*pFileData = pFileDataBuffer;\n\t*pdwFileSize = dwFileSize;\n\n\treturn 0;\n}\n\nDWORD WriteToFile(char *pPath, BYTE *pFileData, DWORD dwOrigFileSize, DWORD dwNewDataFilePosition, BYTE *pNewImportDirectory, DWORD dwNewImportDirectorySize, char *pDllName, BYTE *pImportLookupTable, DWORD dwImportLookupTableSize, DWORD dwPaddingBytes)\n{\n\tHANDLE hFile = NULL;\n\tDWORD dwBytesWritten = 0;\n\tBYTE bPaddingByte = 0;\n\n\t\/\/ create file\n\thFile = CreateFile(pPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);\n\tif(hFile == INVALID_HANDLE_VALUE)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write original EXE data\n\tif(WriteFile(hFile, (void *)pFileData, dwNewDataFilePosition, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write new import directory\n\tif(WriteFile(hFile, (void *)pNewImportDirectory, dwNewImportDirectorySize, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write DLL name\n\tif(WriteFile(hFile, (void *)pDllName, (DWORD)(strlen(pDllName) + 1), &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write import lookup table\n\tif(WriteFile(hFile, (void *)pImportLookupTable, dwImportLookupTableSize, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write import lookup table\n\tif(WriteFile(hFile, (void*)pImportLookupTable, dwImportLookupTableSize, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ write section padding\n\tfor(DWORD i = 0; i &lt; dwPaddingBytes; i++)\n\t{\n\t\tif(WriteFile(hFile, (void*)&amp;bPaddingByte, 1, &amp;dwBytesWritten, NULL) == 0)\n\t\t{\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\t\/\/ write original appended data (debug symbols, installation data, etc)\n\tif(WriteFile(hFile, (void *)(pFileData + dwNewDataFilePosition), dwOrigFileSize - dwNewDataFilePosition, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ close file handle\n\tCloseHandle(hFile);\n\n\treturn 0;\n}\n\nBYTE *VirtualAddressToFilePtr(BYTE *pFileData, IMAGE_NT_HEADERS32 *pImageNtHeader, DWORD dwVirtualAddress)\n{\n\tIMAGE_SECTION_HEADER *pCurrSectionHeader = NULL;\n\tBYTE *pFilePtr = NULL;\n\tDWORD dwSectionDataLength = 0;\n\n\t\/\/ loop through all sections\n\tfor(DWORD i = 0; i &lt; pImageNtHeader->FileHeader.NumberOfSections; i++)\n\t{\n\t\t\/\/ get current section header\n\t\tpCurrSectionHeader = (IMAGE_SECTION_HEADER *)((BYTE*)&amp;pImageNtHeader->OptionalHeader + pImageNtHeader->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));\n\t\tif(pCurrSectionHeader->SizeOfRawData != 0)\n\t\t{\n\t\t\t\/\/ calculate section data length (on disk)\n\t\t\tdwSectionDataLength = pCurrSectionHeader->SizeOfRawData;\n\t\t\tif(dwVirtualAddress >= pCurrSectionHeader->VirtualAddress &amp;&amp; dwVirtualAddress &lt; (pCurrSectionHeader->VirtualAddress + dwSectionDataLength))\n\t\t\t{\n\t\t\t\tpFilePtr = pFileData;\n\t\t\t\tpFilePtr += pCurrSectionHeader->PointerToRawData;\n\t\t\t\tpFilePtr += (dwVirtualAddress - pCurrSectionHeader->VirtualAddress);\n\n\t\t\t\treturn pFilePtr;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nint main(int argc, char *argv&#91;])\n{\n\tDWORD dwFileSize = 0;\n\tBYTE *pFileData = NULL;\n\tIMAGE_DOS_HEADER *pImageDosHeader = NULL;\n\tIMAGE_NT_HEADERS32 *pImageNtHeader = NULL;\n\tIMAGE_NT_HEADERS64 *pImageNtHeader64 = NULL;\n\tIMAGE_DATA_DIRECTORY *pImageDataDirectory = NULL;\n\tchar *pInputFilePath = NULL;\n\tchar *pDllName = NULL;\n\tchar szOutputFilePath&#91;512];\n\tIMAGE_THUNK_DATA32 ImportLookupTable32&#91;2];\n\tIMAGE_THUNK_DATA64 ImportLookupTable64&#91;2];\n\tDWORD dwTotalAddedSize = 0;\n\tIMAGE_IMPORT_DESCRIPTOR *pImageImportDescriptor = NULL;\n\tBYTE *pImportBaseAddr = NULL;\n\tDWORD dwCurrImportBlockOffset = 0;\n\tIMAGE_SECTION_HEADER *pCurrSectionHeader = NULL;\n\tIMAGE_SECTION_HEADER *pLastSectionHeader = NULL;\n\tDWORD dwNewDataVirtualAddress = 0;\n\tDWORD dwModuleCount = 0;\n\tDWORD dwNewDataFilePosition = 0;\n\tIMAGE_IMPORT_DESCRIPTOR NewDllImportDescriptors&#91;2];\n\tDWORD dwOrigImportSize = 0;\n\tDWORD dwNewImportDirectorySize = 0;\n\tBYTE *pNewImportDirectory = NULL;\n\tBYTE *pCopyImportPtr = NULL;\n\tDWORD dwFileAlignment = 0;\n\tDWORD dwPaddingBytes = 0;\n\tBYTE *pImportLookupTable = NULL;\n\tDWORD dwImportLookupTableSize = 0;\n\n\tprintf(\"AddExeImport - www.x86matthew.com\\n\\n\");\n\n\tif(argc != 3)\n\t{\n\t\tprintf(\"Usage: %s &#91;input_exe_path] &#91;add_dll_name]\\n\\n\", argv&#91;0]);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ get cmd param\n\tpInputFilePath = argv&#91;1];\n\tpDllName = argv&#91;2];\n\n\tprintf(\"Opening EXE: '%s'...\\n\", pInputFilePath);\n\n\t\/\/ load dll into memory\n\tif(LoadFileIntoMemory(pInputFilePath, &amp;pFileData, &amp;dwFileSize) != 0)\n\t{\n\t\tprintf(\"Error: Failed to load EXE into memory\\n\");\n\n\t\treturn 1;\n\t}\n\n\t\/\/ get dos header\n\tpImageDosHeader = (IMAGE_DOS_HEADER *)pFileData;\n\tif(pImageDosHeader->e_magic != 0x5A4D)\n\t{\n\t\tprintf(\"Error: Invalid EXE\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ get nt header\n\tpImageNtHeader = (IMAGE_NT_HEADERS32 *)(pFileData + pImageDosHeader->e_lfanew);\n\tif(pImageNtHeader->Signature != IMAGE_NT_SIGNATURE)\n\t{\n\t\tprintf(\"Error: Invalid EXE\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ check exe type\n\tif(pImageNtHeader->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)\n\t{\n\t\t\/\/ 64-bit\n\t\tprintf(\"64-bit EXE detected\\n\");\n\t\tpImageNtHeader64 = (IMAGE_NT_HEADERS64 *)pImageNtHeader;\n\t\tpImageDataDirectory = pImageNtHeader64->OptionalHeader.DataDirectory;\n\t}\n\telse if(pImageNtHeader->FileHeader.Machine == IMAGE_FILE_MACHINE_I386)\n\t{\n\t\t\/\/ 32-bit\n\t\tprintf(\"32-bit EXE detected\\n\");\n\t\tpImageNtHeader64 = NULL;\n\t\tpImageDataDirectory = pImageNtHeader->OptionalHeader.DataDirectory;\n\t}\n\telse\n\t{\n\t\tprintf(\"Error: Invalid EXE\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ find import table\n\tpImportBaseAddr = VirtualAddressToFilePtr(pFileData, pImageNtHeader, pImageDataDirectory&#91;IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);\n\tif(pImportBaseAddr == NULL)\n\t{\n\t\tprintf(\"Error: Invalid EXE\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ find last section in file (this should be the last entry in the list but this is not necessarily the case)\n\tfor(DWORD i = 0; i &lt; pImageNtHeader->FileHeader.NumberOfSections; i++)\n\t{\n\t\t\/\/ get current section header\n\t\tpCurrSectionHeader = (IMAGE_SECTION_HEADER*)((BYTE*)&amp;pImageNtHeader->OptionalHeader + pImageNtHeader->FileHeader.SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER)));\n\n\t\tif(pLastSectionHeader == NULL)\n\t\t{\n\t\t\t\/\/ set initial value\n\t\t\tpLastSectionHeader = pCurrSectionHeader;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\/\/ check if this section is the last entry so far\n\t\t\tif(pCurrSectionHeader->PointerToRawData > pLastSectionHeader->PointerToRawData)\n\t\t\t{\n\t\t\t\t\/\/ store current value\n\t\t\t\tpLastSectionHeader = pCurrSectionHeader;\n\t\t\t}\n\t\t}\n\t}\n\n\t\/\/ ensure the last section was found\n\tif(pLastSectionHeader == NULL)\n\t{\n\t\tprintf(\"Error: Invalid EXE\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ store positions of the end of the current EXE contents (virtual address + file position)\n\tdwNewDataVirtualAddress = pLastSectionHeader->VirtualAddress + pLastSectionHeader->SizeOfRawData;\n\tdwNewDataFilePosition = pLastSectionHeader->PointerToRawData + pLastSectionHeader->SizeOfRawData;\n\n\t\/\/ check if the exe already contains imports\n\tif(pImageDataDirectory&#91;IMAGE_DIRECTORY_ENTRY_IMPORT].Size != 0)\n\t{\n\t\t\/\/ calculate number of existing imported modules\n\t\tfor(;;)\n\t\t{\n\t\t\tpImageImportDescriptor = (IMAGE_IMPORT_DESCRIPTOR*)(pImportBaseAddr + dwCurrImportBlockOffset);\n\t\t\tif(pImageImportDescriptor->Name == 0)\n\t\t\t{\n\t\t\t\t\/\/ finished\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t\/\/ increase counter\n\t\t\tdwModuleCount++;\n\n\t\t\t\/\/ update import block offset\n\t\t\tdwCurrImportBlockOffset += sizeof(IMAGE_IMPORT_DESCRIPTOR);\n\t\t}\n\t}\n\n\tprintf(\"Adding '%s' to import table...\\n\", pDllName);\n\n\t\/\/ allocate memory for new (enlarged) import table\n\tdwOrigImportSize = dwModuleCount * sizeof(IMAGE_IMPORT_DESCRIPTOR);\n\tdwNewImportDirectorySize = dwOrigImportSize + sizeof(NewDllImportDescriptors);\n\tpNewImportDirectory = (BYTE*)malloc(dwNewImportDirectorySize);\n\tif(pNewImportDirectory == NULL)\n\t{\n\t\tprintf(\"Error: Failed to allocate memory\\n\");\n\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\t\/\/ set import descriptor values for new dll\n\tNewDllImportDescriptors&#91;0].Name = dwNewDataVirtualAddress + dwNewImportDirectorySize;\n\tNewDllImportDescriptors&#91;0].OriginalFirstThunk = NewDllImportDescriptors&#91;0].Name + (DWORD)strlen(pDllName) + 1;\n\tNewDllImportDescriptors&#91;0].FirstThunk = NewDllImportDescriptors&#91;0].OriginalFirstThunk;\n\tif(pImageNtHeader64 == NULL)\n\t{\n\t\t\/\/ 32-bit\n\t\tNewDllImportDescriptors&#91;0].FirstThunk += sizeof(ImportLookupTable32);\n\t}\n\telse\n\t{\n\t\t\/\/ 64-bit\n\t\tNewDllImportDescriptors&#91;0].FirstThunk += sizeof(ImportLookupTable64);\n\t}\n\tNewDllImportDescriptors&#91;0].TimeDateStamp = 0;\n\tNewDllImportDescriptors&#91;0].ForwarderChain = 0;\n\n\t\/\/ end of import descriptor chain\n\tNewDllImportDescriptors&#91;1].OriginalFirstThunk = 0;\n\tNewDllImportDescriptors&#91;1].TimeDateStamp = 0;\n\tNewDllImportDescriptors&#91;1].ForwarderChain = 0;\n\tNewDllImportDescriptors&#91;1].Name = 0;\n\tNewDllImportDescriptors&#91;1].FirstThunk = 0;\n\n\t\/\/ copy original imports to the buffer\n\tpCopyImportPtr = pNewImportDirectory;\n\tif(dwModuleCount != 0)\n\t{\n\t\tmemcpy(pNewImportDirectory, pImportBaseAddr, dwOrigImportSize);\n\t\tpCopyImportPtr += dwOrigImportSize;\n\t}\n\n\t\/\/ append the new imported module to the end of the list\n\tmemcpy((void*)pCopyImportPtr, (void*)&amp;NewDllImportDescriptors, sizeof(NewDllImportDescriptors));\n\n\t\/\/ initialise import lookup table for the new DLL (1 import - ordinal #1) - 32-bit\n\tImportLookupTable32&#91;0].u1.Ordinal = 0x80000001;\n\tImportLookupTable32&#91;1].u1.Ordinal = 0;\n\n\t\/\/ initialise import lookup table for the new DLL (1 import - ordinal #1) - 64-bit\n\tImportLookupTable64&#91;0].u1.Ordinal = 0x8000000000000001;\n\tImportLookupTable64&#91;1].u1.Ordinal = 0;\n\n\t\/\/ update IAT directory position\n\tpImageDataDirectory&#91;IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = dwNewDataVirtualAddress;\n\tpImageDataDirectory&#91;IMAGE_DIRECTORY_ENTRY_IMPORT].Size = dwNewImportDirectorySize;\n\n\t\/\/ calculate total length of additional data to append\n\tdwTotalAddedSize = dwNewImportDirectorySize;\n\tdwTotalAddedSize += (DWORD)strlen(pDllName) + 1;\n\tif(pImageNtHeader64 == NULL)\n\t{\n\t\t\/\/ 32-bit\n\t\tdwTotalAddedSize += (sizeof(ImportLookupTable32) * 2);\n\t}\n\telse\n\t{\n\t\t\/\/ 64-bit\n\t\tdwTotalAddedSize += (sizeof(ImportLookupTable64) * 2);\n\t}\n\n\t\/\/ get file alignment value\n\tif(pImageNtHeader64 == NULL)\n\t{\n\t\t\/\/ 32-bit\n\t\tdwFileAlignment = pImageNtHeader->OptionalHeader.FileAlignment;\n\t}\n\telse\n\t{\n\t\t\/\/ 64-bit\n\t\tdwFileAlignment = pImageNtHeader64->OptionalHeader.FileAlignment;\n\t}\n\n\t\/\/ calculate number of bytes to pad (section data in file must be aligned)\n\tdwPaddingBytes = dwFileAlignment - (dwTotalAddedSize % dwFileAlignment);\n\tif(dwPaddingBytes == dwFileAlignment)\n\t{\n\t\tdwPaddingBytes = 0;\n\t}\n\tdwTotalAddedSize += dwPaddingBytes;\n\n\t\/\/ the last section must have read\/write permissions at minimum to allow the loader to store the resolved IAT value\n\tpLastSectionHeader->Characteristics |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;\n\tpLastSectionHeader->SizeOfRawData += dwTotalAddedSize;\n\tpLastSectionHeader->Misc.VirtualSize += dwTotalAddedSize;\n\tif(pImageNtHeader64 == NULL)\n\t{\n\t\t\/\/ 32-bit\n\t\tpImageNtHeader->OptionalHeader.SizeOfImage += dwTotalAddedSize;\n\t}\n\telse\n\t{\n\t\t\/\/ 64-bit\n\t\tpImageNtHeader64->OptionalHeader.SizeOfImage += dwTotalAddedSize;\n\t}\n\n\t\/\/ check if debug symbols are currently stored at the end of the exe\n\tif(pImageNtHeader->FileHeader.PointerToSymbolTable == dwNewDataFilePosition)\n\t{\n\t\t\/\/ adjust debug symbol ptr\n\t\tpImageNtHeader->FileHeader.PointerToSymbolTable += dwTotalAddedSize;\n\t}\n\n\t\/\/ get import lookup table values\n\tif(pImageNtHeader64 == NULL)\n\t{\n\t\t\/\/ 32-bit\n\t\tpImportLookupTable = (BYTE*)&amp;ImportLookupTable32&#91;0];\n\t\tdwImportLookupTableSize = sizeof(ImportLookupTable32);\n\t}\n\telse\n\t{\n\t\t\/\/ 64-bit\n\t\tpImportLookupTable = (BYTE*)&amp;ImportLookupTable64&#91;0];\n\t\tdwImportLookupTableSize = sizeof(ImportLookupTable64);\n\t}\n\n\t\/\/ write new exe to file\n\tmemset(szOutputFilePath, 0, sizeof(szOutputFilePath));\n\t_snprintf_s(szOutputFilePath, sizeof(szOutputFilePath) - 1, \"%s_modified.exe\", pInputFilePath);\n\tprintf(\"Writing new file to '%s'...\\n\", szOutputFilePath);\n\tif(WriteToFile(szOutputFilePath, pFileData, dwFileSize, dwNewDataFilePosition, pNewImportDirectory, dwNewImportDirectorySize, pDllName, pImportLookupTable, dwImportLookupTableSize, dwPaddingBytes) != 0)\n\t{\n\t\tprintf(\"Error: Failed to write new EXE\\n\");\n\n\t\tfree(pNewImportDirectory);\n\t\tfree(pFileData);\n\t\treturn 1;\n\t}\n\n\tprintf(\"Finished\\n\");\n\n\t\/\/ free memory\n\tfree(pNewImportDirectory);\n\tfree(pFileData);\n\n\treturn 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.x86matthew.com\/view_post?id=add_exe_im [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45,37,43],"tags":[97,73],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-x86matthew-com","category-samples","category-infoarticle","tag-import","tag-windows"],"views":2894,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":1,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}