Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2010
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.45 KB | None | 0 0
  1. library Project2;
  2.  
  3. { Important note about DLL memory management: ShareMem must be the
  4.   first unit in your library's USES clause AND your project's (select
  5.   Project-View Source) USES clause if your DLL exports any procedures or
  6.   functions that pass strings as parameters or function results. This
  7.   applies to all strings passed to and from your DLL--even those that
  8.   are nested in records and classes. ShareMem is the interface unit to
  9.   the BORLNDMM.DLL shared memory manager, which must be deployed along
  10.   with your DLL. To avoid using BORLNDMM.DLL, pass string information
  11.   using PChar or ShortString parameters. }
  12.  
  13. uses
  14.   windows,
  15.   SysUtils,
  16.   Classes;
  17.  
  18. {$R *.res}
  19. (******************************************************************************)
  20. function CalcJump(Src,Dest:DWORD):DWORD;
  21. begin
  22.  if(Dest<Src) then begin
  23.    Result:=Src-Dest;
  24.    Result:=$FFFFFFFF-Result;
  25.    Result:=Result-4;
  26.   end
  27.   else begin
  28.    Result:=Dest-Src;
  29.    Result:=Result-5;
  30.   end;
  31. end;
  32. (******************************************************************************)
  33. function OpCodeLength(Address:DWORD):DWORD; cdecl; assembler;
  34. const
  35.   O_UNIQUE = 0;
  36.   O_PREFIX = 1;
  37.   O_IMM8 = 2;
  38.   O_IMM16 = 3;
  39.   O_IMM24 = 4;
  40.   O_IMM32 = 5;
  41.   O_IMM48 = 6;
  42.   O_MODRM = 7;
  43.   O_MODRM8 = 8;
  44.   O_MODRM32 = 9;
  45.   O_EXTENDED = 10;
  46.   O_WEIRD = 11;
  47.   O_ERROR = 12;
  48. asm
  49.     pushad
  50.     cld
  51.     xor edx, edx
  52.     mov esi, Address
  53.     mov ebp, esp
  54.     push    1097F71Ch
  55.     push    0F71C6780h
  56.     push    17389718h
  57.     push    101CB718h
  58.     push    17302C17h
  59.     push    18173017h
  60.     push    0F715F547h
  61.     push    4C103748h
  62.     push    272CE7F7h
  63.     push    0F7AC6087h
  64.     push    1C121C52h
  65.     push    7C10871Ch
  66.     push    201C701Ch
  67.     push    4767602Bh
  68.     push    20211011h
  69.     push    40121625h
  70.     push    82872022h
  71.     push    47201220h
  72.     push    13101419h
  73.     push    18271013h
  74.     push    28858260h
  75.     push    15124045h
  76.     push    5016A0C7h
  77.     push    28191812h
  78.     push    0F2401812h
  79.     push    19154127h
  80.     push    50F0F011h
  81.     mov ecx, 15124710h
  82.     push    ecx
  83.     push    11151247h
  84.     push    10111512h
  85.     push    47101115h
  86.     mov eax, 12472015h
  87.     push    eax
  88.     push    eax
  89.     push    12471A10h
  90.     add cl, 10h
  91.     push    ecx
  92.     sub cl, 20h
  93.     push    ecx
  94.     xor ecx, ecx
  95.     dec ecx
  96. @@ps:
  97.     inc  ecx
  98.     mov  edi, esp
  99. @@go:
  100.     lodsb
  101.     mov  bh, al
  102. @@ft:
  103.     mov  ah, [edi]
  104.     inc  edi
  105.     shr  ah, 4
  106.     sub  al, ah
  107.     jnc  @@ft
  108.     mov al, [edi-1]
  109.     and al, 0Fh
  110.     cmp  al, O_ERROR
  111.     jnz  @@i7
  112.     pop edx
  113.     not edx
  114. @@i7:
  115.     inc edx
  116.     cmp al, O_UNIQUE
  117.     jz  @@t_exit
  118.     cmp al, O_PREFIX
  119.     jz  @@ps
  120.     add  edi, 51h
  121.     cmp  al, O_EXTENDED
  122.     jz   @@go
  123.         mov edi, [ebp+((1+8)*4)+4]
  124. @@i6:
  125.     inc  edx
  126.     cmp  al, O_IMM8
  127.     jz   @@t_exit
  128.     cmp  al, O_MODRM
  129.     jz   @@t_modrm
  130.     cmp  al, O_WEIRD
  131.     jz   @@t_weird
  132. @@i5:
  133.     inc  edx
  134.     cmp  al, O_IMM16
  135.     jz   @@t_exit
  136.     cmp  al, O_MODRM8
  137.     jz   @@t_modrm
  138. @@i4:
  139.     inc  edx
  140.     cmp  al, O_IMM24
  141.     jz   @@t_exit
  142. @@i3:
  143.     inc  edx
  144. @@i2:
  145.     inc  edx
  146.     pushad
  147.     mov  al, 66h
  148.     repnz scasb
  149.     popad
  150.     jnz  @@c32
  151. @@d2:
  152.     dec  edx
  153.     dec  edx
  154. @@c32:
  155.     cmp  al, O_MODRM32
  156.     jz   @@t_modrm
  157.     sub  al, O_IMM32
  158.     jz   @@t_imm32
  159. @@i1:
  160.     inc  edx
  161. @@t_exit:
  162.     jmp @@ASMEnded
  163. @@t_modrm:
  164.        lodsb
  165.        mov  ah, al
  166.        shr  al, 7
  167.        jb   @@prmk
  168.        jz   @@prm
  169.        add  dl, 4
  170.        pushad
  171.        mov  al, 67h
  172.        repnz scasb
  173.        popad
  174.        jnz  @@prm
  175. @@d3:  sub  dl, 3
  176.        dec  al
  177. @@prmk:jnz  @@t_exit
  178.        inc  edx
  179.        inc  eax
  180. @@prm:
  181.        and  ah, 00000111b
  182.        pushad
  183.        mov  al, 67h
  184.        repnz scasb
  185.        popad
  186.        jz   @@prm67chk
  187.        cmp  ah, 04h
  188.        jz   @@prmsib
  189.        cmp  ah, 05h
  190.        jnz  @@t_exit
  191. @@prm5chk:
  192.        dec  al
  193.        jz   @@t_exit
  194. @@i42: add  dl, 4
  195.        jmp  @@t_exit
  196. @@prm67chk:
  197.        cmp  ax, 0600h
  198.        jnz  @@t_exit
  199.        inc  edx
  200.        jmp  @@i1
  201. @@prmsib:
  202.        cmp  al, 00h
  203.        jnz  @@i1
  204.        lodsb
  205.        and  al, 00000111b
  206.        sub  al, 05h
  207.        jnz  @@i1
  208.        inc  edx
  209.        jmp  @@i42
  210. @@t_weird:
  211.        test byte ptr [esi], 00111000b
  212.        jnz  @@t_modrm
  213.        mov  al, O_MODRM8
  214.        shr  bh, 1
  215.        adc  al, 0
  216.        jmp  @@i5
  217. @@t_imm32:
  218.        sub  bh, 0A0h
  219.        cmp  bh, 04h
  220.        jae  @@d2
  221.        pushad
  222.        mov  al, 67h
  223.        repnz scasb
  224.        popad
  225.        jnz  @@chk66t
  226. @@d4:  dec  edx
  227.        dec  edx
  228. @@chk66t:
  229.        pushad
  230.        mov  al, 66h
  231.        repnz scasb
  232.        popad
  233.        jz   @@i1
  234.        jnz  @@d2
  235. @@ASMEnded:
  236.     mov esp, ebp
  237.     mov [result+(9*4)], edx
  238.     popad
  239. end;
  240. (******************************************************************************)
  241. function IsWin9x:Boolean;
  242. asm
  243.  MOV     EAX, FS:[030H]
  244.  TEST    EAX, EAX
  245.  SETS    AL
  246. end;
  247. (******************************************************************************)
  248. function ApiUnHook(ModName,ApiName:Pchar; FuncAddr,HookedApi:Pointer; var MainApi:Pointer;codigo:integer):Boolean;
  249. var
  250.  dwCount,Cnt,i,Jmp: DWORD;
  251.  P: Pointer;
  252.  hMod,OldP,TMP: Cardinal;
  253. begin
  254.  Result:=False;
  255.  if IsWin9x then Exit;
  256.  if not codigo = 585 then Exit;
  257.  P:=FuncAddr;
  258.  if P=nil then begin
  259.   hMod:=GetModuleHandle(Pchar(ModName));
  260.   P:=GetProcAddress(hMod,Pchar(ApiName));
  261.  end;
  262.  if (P=nil) or (MainApi=nil) or (HookedApi=nil) then Exit;
  263.  if not VirtualProtect(P,$40,PAGE_EXECUTE_READWRITE,@OldP) then Exit;
  264.  if ((Byte(P^)<>$68) or (DWORD(Pointer(DWORD(P)+1)^)<>DWORD(HookedApi))) then Exit;
  265.  Cnt:=0;
  266.  for dwCount:=0 to $3F do begin
  267.   Inc(Cnt,OpCodeLength(DWORD(MainApi)+Cnt));
  268.   if (Byte(Pointer(DWORD(MainApi)+Cnt)^)=$C3) and (Byte(Pointer(DWORD(MainApi)+Cnt+1)^)=$99) then Break;
  269.   for i:=0 to Cnt-1 do Pchar(P)[i]:=Pchar(MainApi)[i];
  270.  end;
  271.  if (OpCodeLength(DWORD(P))=5) and ((Byte(P^)=$E8) or (Byte(P^)=$E9)) then begin
  272.   Jmp:=DWORD(MainApi)+DWORD(Pointer(DWORD(MainApi)+1)^)+5;
  273.   DWORD(Pointer(DWORD(P)+1)^):=CalcJump(DWORD(P),Jmp);
  274.  end;
  275.  VirtualProtect(P,$40,OldP,@TMP);
  276.  VirtualFree(MainApi,0,MEM_RELEASE);
  277.  Result:=True;
  278. end;
  279. (******************************************************************************)
  280. (******************************************************************************)
  281. function ApiHook(ModName,ApiName:Pchar; FuncAddr,HookedApi:Pointer; var MainApi:Pointer; codigo:integer):Boolean;
  282. var
  283.  dwCount,Cnt,i,Jmp: DWORD;
  284.  P: Pointer;
  285.  hMod,OldP,TMP: Cardinal;
  286. begin
  287.  Result:=False;
  288.  if IsWin9x then Exit;
  289.  if not codigo = 585 then Exit;
  290.  P:=FuncAddr;
  291.  if P=nil then begin
  292.   hMod:=GetModuleHandle(ModName);
  293.   if hMod=0 then hMod:=LoadLibrary(ModName);
  294.   P:=GetProcAddress(hMod,ApiName);
  295.  end;
  296.  if (P=nil) or (HookedApi=nil) then Exit;
  297.  if not VirtualProtect(P,$40,PAGE_EXECUTE_READWRITE,@OldP) then Exit;
  298.  if ((Byte(P^)=$68) and (DWORD(Pointer(DWORD(P)+1)^)=DWORD(HookedApi))) then Exit;
  299.  MainApi:=VirtualAlloc(nil,$1000,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
  300.  if MainApi=nil then Exit;
  301.  Cnt:=0;
  302.  for dwCount:=0 to $3F do begin
  303.   Inc(Cnt,OpCodeLength(DWORD(P)+Cnt));
  304.   for i:=0 to Cnt-1 do Pchar(MainApi)[i]:=Pchar(P)[i];
  305.   if Cnt>5 then Break;
  306.  end;
  307.  Pchar(MainApi)[Cnt]:=Char($68);
  308.  DWORD(Pointer(DWORD(MainApi)+Cnt+1)^):=DWORD(P)+Cnt;
  309.  Pchar(MainApi)[Cnt+5]:=Char($C3);
  310.  Pchar(MainApi)[Cnt+6]:=Char($99);
  311.  if (OpCodeLength(DWORD(MainApi))=5) and ((Byte(MainApi^)=$E8) or (Byte(MAinApi^)=$E9)) then begin
  312.   Jmp:=DWORD(P)+DWORD(Pointer(DWORD(MainApi)+1)^)+5;
  313.   DWORD(Pointer(DWORD(MainApi)+1)^):=CalcJump(DWORD(MainApi),Jmp);
  314.  end;
  315.  Pchar(P)[0]:=Char($68);
  316.  DWORD(Pointer(DWORD(P)+1)^):=DWORD(HookedApi);
  317.  Pchar(P)[5]:=Char($C3);
  318.  VirtualProtect(P,$40,OldP,@TMP);
  319.  Result:=True;
  320. end;
  321.  
  322.  
  323. exports
  324.      APIHook,
  325.      ApiUnHook;
  326. begin
  327.  
  328. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement