{"rsdb":{"rid":"397519","subhead":"","postdate":"0","aid":"273605","fid":"49","uid":"1","topic":"1","content":"
\n

Relocation\uff08\u91cd\u5b9a\u4f4d\uff09\u662f\u4e00\u79cd\u5c06\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e9b\u5730\u5740\u4fee\u6b63\u4e3a\u8fd0\u884c\u65f6\u53ef\u7528\u7684\u5b9e\u9645\u5730\u5740\u7684\u673a\u5236\u3002\u5728\u7a0b\u5e8f\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e86\u5404\u79cd\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u8fd9\u4e9b\u53d8\u91cf\u548c\u51fd\u6570\u7684\u5730\u5740\u8fd8\u6ca1\u6709\u786e\u5b9a\uff0c\u56e0\u6b64\u5b83\u4eec\u7684\u5730\u5740\u53ea\u80fd\u6682\u65f6\u4f7f\u7528\u4e00\u4e2a\u76f8\u5bf9\u5730\u5740\u3002\u5f53\u7a0b\u5e8f\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u8fd0\u884c\u65f6\uff0c\u8fd9\u4e9b\u76f8\u5bf9\u5730\u5740\u9700\u8981\u88ab\u4fee\u6b63\u4e3a\u5b9e\u9645\u7684\u7edd\u5bf9\u5730\u5740\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f\u91cd\u5b9a\u4f4d\u3002<\/p> \n

\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u7a0b\u5e8f\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u8fd0\u884c\u65f6\uff0c\u9700\u8981\u5c06\u7a0b\u5e8f\u4e2d\u7684\u5404\u79cd\u5185\u5b58\u5730\u5740\u8fdb\u884c\u91cd\u5b9a\u4f4d\uff0c\u4ee5\u4f7f\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5730\u8fd0\u884c\u3002Windows\u7cfb\u7edf\u4f7f\u7528PE\uff08Portable Executable\uff09\u6587\u4ef6\u683c\u5f0f\u6765\u5b58\u50a8\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5305\u62ec\u91cd\u5b9a\u4f4d\u4fe1\u606f\u3002\u5f53\u7a0b\u5e8f\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u65f6\uff0c\u7cfb\u7edf\u4f1a\u89e3\u6790\u8fd9\u4e9b\u91cd\u5b9a\u4f4d\u4fe1\u606f\uff0c\u5e76\u5c06\u7a0b\u5e8f\u4e2d\u7684\u5404\u79cd\u5185\u5b58\u5730\u5740\u8fdb\u884c\u91cd\u5b9a\u4f4d\u3002<\/p> \n

\u91cd\u5b9a\u4f4d\u8868\u4e00\u822c\u51fa\u73b0\u5728DLL<\/code>\u4e2d\uff0c\u56e0\u4e3aDLL<\/code>\u90fd\u662f\u52a8\u6001\u52a0\u8f7d\uff0c\u6240\u4ee5\u5730\u5740\u4e0d\u56fa\u5b9a\uff0cDLL\u7684\u5165\u53e3\u70b9\u5728\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u4e2d\u81f3\u5c11\u8981\u6267\u884c2\u6b21\uff0c\u4e00\u6b21\u662f\u5728\u5f00\u59cb\u65f6\u6267\u884c\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u4e00\u6b21\u5219\u662f\u5728\u7ed3\u675f\u65f6\u505a\u6700\u540e\u7684\u6536\u5c3e\u5de5\u4f5c\uff0c\u91cd\u5b9a\u4f4d\u8868\u5219\u662f\u89e3\u51b3DLL\u7684\u5730\u5740\u95ee\u9898\uff0c\u4e3a\u4e86\u80fd\u627e\u5230\u91cd\u5b9a\u4f4d\u8868\u9996\u5148\u6211\u4eec\u9700\u8981\u4f7f\u7528PeView<\/code>\u5de5\u5177\u67e5\u8be2DataDirectory<\/code>\u6570\u636e\u76ee\u5f55\u8868\uff0c\u5728\u5176\u4e2d\u627e\u5230Base relocation<\/code>\u5b57\u6bb5\uff0c\u91cc\u9762\u76840x00001800<\/code>\u5219\u662f\u91cd\u5b9a\u4f4d\u8868\u57fa\u5730\u5740\uff1b<\/p> \n

\"\"<\/p> \n

\u6211\u4eec\u901a\u8fc7\u4f7f\u7528WinHex<\/code>\u5de5\u5177\u5b9a\u4f4d\u52300x00001800<\/code>\u5373\u53ef\u770b\u5230\u91cd\u5b9a\u4f4d\u8868\u4fe1\u606f\uff0c\u5982\u4e0b\u56fe\u4e2d\u76841000<\/code>\u4ee3\u8868\u7684\u662f\u91cd\u5b9a\u4f4dRVA<\/code>\u5730\u5740\uff0c\u7eff\u8272\u76840104<\/code>\u4ee3\u8868\u7684\u5219\u662f\u91cd\u5b9a\u4f4d\u5757\u7684\u957f\u5ea6\uff0c\u540e\u9762\u5219\u662f\u6bcf\u4e24\u4e2a\u5b57\u8282\u4ee3\u8868\u4e00\u4e2a\u91cd\u5b9a\u4f4d\u5757\uff0c0A\u662f\u91cd\u5b9a\u4f4d\u5730\u5740\uff0c30\u5219\u662f\u91cd\u5b9a\u4f4d\u7684\u7c7b\u578b\uff0c\u4ee5\u6b64\u987a\u5e8f\u5411\u4e0b\u6392\u5217\u3002<\/p> \n

\"\"<\/p> \n

\u91cd\u5b9a\u4f4d\u8868\u4e5f\u662f\u5206\u9875\u6392\u5217\u7684\uff0c\u6bcf\u4e00\u9875\u5927\u5c0f\u90fd\u662f1000<\/code>\u5b57\u8282\uff0c\u901a\u8fc7\u4f7f\u7528FixRelocPage<\/code>\u547d\u4ee4\u5373\u53ef\u67e5\u8be2\u5230\u5f53\u524d\u7a0b\u5e8f\u4e2d\u7684\u91cd\u5b9a\u4f4d\u5757\u4fe1\u606f\uff0c\u5e76\u4ee5\u7b2c\u4e00\u4e2a\u4e3a\u4f8b\uff0c\u67e5\u8be2\u4e00\u4e0b\u8d77\u59cb\u5730\u5740RVA\u4e3a1000<\/code>\u7684\u9875\u4e0a\uff0c\u6709\u54ea\u4e9b\u91cd\u5b9a\u4f4d\u7ed3\u6784\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1b<\/p> \n

\"\"<\/p> \n

\u5176\u4e2d\u7684\u91cd\u5b9a\u4f4dRVA<\/code>\u5730\u57400000100A<\/code>\u662f\u7528\u6807\u9ec4\u8272\u76841000<\/code>\u52a0\u4e0a\u6807\u84dd\u8272\u76840xA<\/code>\u5f97\u5230\u7684\u3002\u800c\u4fee\u6b63RVA\u5730\u574000003000<\/code>\u52a0\u4e0a\u6a21\u5757\u57fa\u5730\u574063FF0000+3000<\/code>\u5f97\u5230\u7684\u5219\u662f\u7b2c\u4e00\u4e2a\u88ab\u4fee\u6b63\u7684\u5185\u5b58\u5730\u5740\uff0c\u8bfb\u8005\u53ef\u4f7f\u7528x64dbg<\/code>\u8df3\u8f6c\u5230\u8be5\u7a0b\u5e8f\u5185\u81ea\u884c\u786e\u8ba4\u3002<\/p> \n

\u91cd\u5b9a\u4f4d\u8868\u7684\u4fee\u590d\u539f\u7406\u4e0eIAT\u4fee\u590d\u5b8c\u5168\u4e00\u81f4\uff0c\u6211\u4eec\u9700\u8981\u5206\u522b\u8bfb\u5165\u8131\u58f3\u524d\u4e0e\u8131\u58f3\u540e\u7684\u4e24\u4e2a\u7a0b\u5e8f\uff0c\u63a5\u7740\u901a\u8fc7\u5faa\u73af\u6b63\u786e\u7684\u91cd\u5b9a\u4f4d\u8868\u4fe1\u606f\uff0c\u5e76\u4f9d\u6b21\u8986\u76d6\u5230\u8131\u58f3\u540e\u7684\u7a0b\u5e8f\u5185\uff0c\u4ee5\u6b64\u5b9e\u73b0\u5bf9\u91cd\u5b9a\u4f4d\u8868\u7684\u4fee\u590d\u529f\u80fd\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1b<\/p> \n

#include <windows.h>\n#include <stdio.h>\n\nstruct TypeOffset\n{\n  WORD Offset : 12;       \/\/ \u4f4e12\u4f4d\u4ee3\u8868\u91cd\u5b9a\u4f4d\u5730\u5740\n  WORD Type : 4;          \/\/ \u9ad84\u4f4d\u4ee3\u8868\u91cd\u5b9a\u4f4d\u7c7b\u578b\n};\n\nDWORD FileSize = 0;  \/\/ \u5b9a\u4e49\u6587\u4ef6\u5927\u5c0f\nDWORD FileBase = 0;  \/\/ \u4fdd\u5b58\u6587\u4ef6\u7684\u57fa\u5730\u5740\n\n\/\/ \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf,\u6765\u5b58\u50a8DOS,NT,Section\u5934\nPIMAGE_DOS_HEADER DosHeader = nullptr;\nPIMAGE_NT_HEADERS NtHeader = nullptr;\nPIMAGE_FILE_HEADER FileHead = nullptr;\n\n\/\/ \u5c06RVA\u8f6c\u6362\u4e3aFOA\u7684\u51fd\u6570\nDWORD RVAtoFOA(DWORD rva)\n{\n  auto SectionTables = IMAGE_FIRST_SECTION(NtHeader);    \/\/ \u83b7\u53d6\u533a\u6bb5\u8868\n  WORD Count = NtHeader->FileHeader.NumberOfSections;    \/\/ \u83b7\u53d6\u533a\u6bb5\u6570\u91cf\n\n  for (int i = 0; i < Count; ++i)\n  {\n    \/\/ \u5224\u65ad\u662f\u5426\u5b58\u5728\u4e8e\u533a\u6bb5\u4e2d\n    DWORD Section_Start = SectionTables[i].VirtualAddress;\n    DWORD Section_Ends = SectionTables[i].VirtualAddress + SectionTables[i].SizeOfRawData;\n    if (rva >= Section_Start && rva < Section_Ends)\n    {\n      \/\/ \u627e\u5230\u4e4b\u540e\u8ba1\u7b97\u4f4d\u7f6e\u5e76\u8fd4\u56de\u503c\n      return rva - SectionTables[i].VirtualAddress + SectionTables[i].PointerToRawData;\n    }\n  }\n  return -1;\n}\n\n\/\/ \u6253\u5f00PE\u6587\u4ef6\nbool OpenPeFile(LPCSTR FileName)\n{\n  \/\/ \u6253\u5f00\u6587\u4ef6\n  HANDLE Handle = CreateFileA(FileName, GENERIC_READ, NULL,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n  if (Handle == INVALID_HANDLE_VALUE)\n    return false;\n\n  \/\/ \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\n  FileSize = GetFileSize(Handle, NULL);\n\n  \/\/ \u8bfb\u53d6\u6587\u4ef6\u6570\u636e\n  DWORD OperSize = 0;\n  FileBase = (DWORD)new BYTE[FileSize];\n  ReadFile(Handle, (LPVOID)FileBase, FileSize, &OperSize, NULL);\n\n  \/\/ \u83b7\u53d6DOS\u5934\u5e76\u5224\u65ad\u662f\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684DOS\u6587\u4ef6\n  DosHeader = (PIMAGE_DOS_HEADER)FileBase;\n  if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)\n    return false;\n\n  \/\/ \u83b7\u53d6 NT \u5934\u5e76\u5224\u65ad\u662f\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684PE\u6587\u4ef6\n  NtHeader = (PIMAGE_NT_HEADERS)(FileBase + DosHeader->e_lfanew);\n  if (NtHeader->Signature != IMAGE_NT_SIGNATURE)\n    return false;\n\n  \/\/ \u5224\u65ad\u662f\u4e0d\u662f\u4e00\u4e2a32\u4f4d\u6587\u4ef6\n  if (NtHeader->OptionalHeader.Magic != 0x010B)\n    return false;\n  \n  CloseHandle(Handle);\n  return true;\n}\n\n\/\/ \u4fee\u590d\u91cd\u5b9a\u4f4d\u8868\nvoid RepairFixReloc(char new_file[])\n{\n  DWORD base = NtHeader->OptionalHeader.ImageBase;\n  \/\/ 1. \u83b7\u53d6\u91cd\u5b9a\u4f4d\u8868\u7684 rva\n  DWORD RelocRVA = NtHeader->OptionalHeader.DataDirectory[5].VirtualAddress;\n  \/\/ 2. \u83b7\u53d6\u91cd\u5b9a\u4f4d\u8868\n  auto Reloc = (PIMAGE_BASE_RELOCATION)(FileBase + RVAtoFOA(RelocRVA));\n\n  \/\/ 3. \u904d\u5386\u91cd\u5b9a\u4f4d\u8868\u4e2d\u7684\u91cd\u5b9a\u4f4d\u5757\uff0c\u4ee50\u7ed3\u5c3e\n  while (Reloc->SizeOfBlock != 0)\n  {\n    \/\/ 3.1 \u8f93\u51fa\u5206\u9875\u57fa\u5740\n    printf("[\u2193] \u5206\u9875\u57fa\u5740: 0x%08X \\n\\n", Reloc->VirtualAddress);\n    \/\/ 3.2 \u627e\u5230\u91cd\u5b9a\u4f4d\u9879\n    auto Offset = (TypeOffset*)(Reloc + 1);\n\n    \/\/ 3.3 \u8ba1\u7b97\u91cd\u5b9a\u4f4d\u9879\u7684\u4e2a\u6570\n    \/\/ Reloc->Size","orderid":"0","title":"2.10 PE\u7ed3\u6784\uff1a\u91cd\u5efa\u91cd\u5b9a\u4f4d\u8868\u7ed3\u6784(\u4e00)","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"673","pages":"2","comments":"0","posttime":"2023-09-23 15:44:27","list":"1695455067","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"2.10<\/A> \u7ed3\u6784<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"2.10 PE\u7ed3\u6784\uff1a\u91cd\u5efa\u91cd\u5b9a\u4f4d\u8868\u7ed3\u6784","lastview":"1715569717","digg_num":"908","digg_time":"1715790113","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}