Advertisement
Guest User

Untitled

a guest
Sep 30th, 2011
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.88 KB | None | 0 0
  1. {$apptype console}
  2. program RelDump; uses windows, imagehlp;
  3.  
  4. function HexStr(val:integer):string;
  5. var i:integer; ch:byte;
  6. begin  Result := '';    
  7.    for i:=7 downto 0 do begin  ch := val shr (i*4) and $f or $30;
  8.     if ch > $39 then inc (ch,7); Result := Result + char(ch)
  9.  end;
  10. end;
  11.  
  12. type
  13. //
  14. TReloc_Table = packed record
  15. { Relocations Table - полублоки, патч-смещений, длиной до $FFF }
  16. { |-------------  SIZE --------------| |-------------  SIZE --------------| }
  17. { DWORD VA, DWORD SIZE, WORD,WORD,WORD DWORD VA, DWORD SIZE, WORD,WORD,WORD }
  18.   VA:cardinal; Size:cardinal;OFFSETS: array[1..1000] of word;
  19. end;                  
  20.  
  21. type
  22.      TSections=array[0..10] of TImageSectionHeader;
  23.  
  24.  
  25. // конвертор 'виртуального' адреса в 'реальный' (смещение файла)
  26. function VA_TO_RAW (VA:integer;PE:PImageNtHeaders):integer;
  27. var
  28.   Sections: ^TSections; i:byte; label done1,done2;
  29.   begin
  30. //установить указатель на структуры ImageSectionHeader (.code/.data/etc) }
  31.   Sections:= pointer(integer(@PE.OptionalHeader.Magic) +
  32.                                        PE.FileHeader.SizeOfOptionalHeader);
  33. // перебор секций до совпадения или попадания значения в диапазон секции
  34.   for i:=0 to PE.FileHeader.NumberOfSections-1 do
  35.   if VA = Sections[i].VirtualAddress then goto done1 else
  36.   if VA < Sections[i].VirtualAddress + Sections[i].Misc.VirtualSize  
  37.   then goto done2; Result := 0;  // writeln ('OOOPS!!!');
  38.   exit; // хм...ничего не найдено
  39.   done1: Result := Sections[i].PointerToRawData; exit;
  40.   done2: Result := VA - Sections[i].VirtualAddress +
  41.                         Sections[i].PointerToRawData; exit;
  42. end;  // ps: код древний,ужасный.. но что странно рабочий
  43.  
  44. var
  45.     i: integer;  img: PLoadedImage;
  46.     PE:PImageNtHeaders; str: string;
  47.     RelocTab : ^TReloc_Table;
  48.     data :^integer; position: integer;
  49.  
  50. begin
  51.     str:= ParamStr(1); if str = '' then str := 'FLEngine.dll';
  52.     img:= ImageLoad(pchar(str), nil);
  53.     if img = nil then exit;
  54.     MapAndLoad(pchar(str), nil, img, true, false);
  55.  
  56.     PE := img.FileHeader;
  57.  
  58. // находим смещение файла на таблицу релоков
  59.     RelocTab := pointer(
  60.            VA_to_RAW (img.FileHeader.OptionalHeader.DataDirectory
  61.              [IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress, PE)
  62. // нормализуем его до загруженого в память
  63.              + integer(img.MappedAddress)
  64.                       );
  65.  
  66. (*   // альтернативный вариант
  67. for i := 0 to img.NumberOfSections-1 do
  68. begin
  69.     Section:= pointer(integer(img.Sections)+SizeOf(TImageSectionHeader)*i);
  70.     str:= pchar(@Section.Name);
  71.     RelocTab := pointer(integer(img.MappedAddress)+Section.PointerToRawData);
  72.     if str = '.reloc'then break;
  73. end;  *)
  74.  
  75. repeat  //сканим таблицу релоков
  76.  Position := VA_TO_RAW (RelocTab.VA, PE) + (integer(PE) - $100);
  77.  
  78. // входим в смещения
  79.  for i := 1 to ((RelocTab.Size -8) div 2) do
  80.   begin if RelocTab.Offsets[i] = 0 then break; // 0 - конец секции
  81.  
  82. // todo: желательно ввести проверку типа релока
  83.    data := pointer (position+ (RelocTab.Offsets[i] and not $F000));
  84.  
  85. // печатаем...
  86.    writeln (
  87.             HexStr($100+integer(data)-integer(PE))  ,'  ',  
  88.             HexStr($100+integer(@RelocTab.Offsets[i])-integer(PE)) , '  ',
  89.             HexStr( data^ )
  90.            );
  91.   end; // Конец  перебора смещений
  92.  
  93. //Смотрим следующий блок
  94.       RelocTab := pointer(cardinal(RelocTab)+ RelocTab.Size);
  95. until RelocTab.VA = 0; { конец таблицы VA=0 }
  96.  
  97.     ImageUnload (img);  
  98. end.
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement