Unidad Unidad1; La interfaz utiliza Windows, mensajes, sysutils, variantes, clases, gráficos, controles, formularios, diálogos, stdctrls; tipo tForm1 = class (tForm) botón1: tbutton; Memo1: tmemo; Button2: tbutton; Button3: tbutton; Procedimiento botón1Click (remitente: tobject); procedimiento Button2Click (remitente: tobject); procedimiento Button3Click (remitente: tobject); procedimiento FormDestrOY (remitente: tobject); procedimiento formulcreate (remitente: tobject); privado {declaraciones privadas} procedimiento showmsg (s: string); público {declaraciones públicas} end; VAR Form1: tForm1; implementación {$ r *.dfm} usa mlde32Unit; consttEpath = 'c:/programas de programa/Borland/Delphi6/Projects/Adv apihook/test/vt.exe'; Func2hook = 'freelibrary'; Var // debe ser una variable de global Ptrreal: puntero; Cbstolen: Cardinal; Ntdllbase, ntdlllength: integer; P: puntero; H: DWORD; procedimiento tform1.showmsg (s: string); Comenzar memo1.lines.add (s); fin; procedimiento tForm1.Button1Click (remitente: tobject); etiqueta fakecode, rtncode; var // si: startupinfo; // pi: process_information; OriginalBytes: Array [0..4] de char; HookJMP: PCHAR; RTN: Cardinal; Bytes: matriz [0..4] de char; TMP: Cardinal; PEB, LDR, Flink: puntero; BS: DWORD; comenzar ptrreal: = nil; Ntdlllength: = 0; Ntdllbase: = getModuleHandle ('ntdll.dll'); ASM MOV EAX, FS: [$ 30] Mov Peb, Eax End; ldr: = puntero (dword (puntero (dword (PEB) +12)^)); flink: = puntero (dword (puntero (dword (ldr) +12)^)); p: = flink; Repita bs: = dword (puntero (dword (p)+$ 18)^); Si bs = ntdllbase entonces comience ntdlllength: = dword (puntero (dword (p)+$ 20)^); romper; fin; p: = puntero (dword (p^)); hasta dword (flink) = dword (p^); Si ntdlllength = 0 entonces showmsg ('puede obtener el tamaño de la imagen de ntdll.dll!'); {Showmsg ('Creación de proceso suspendido ...'); Zeromemory (@si, sizeof (startupInfo)); si.cb: = sizeof (startupInfo); CreateProcess (Dessopath, Nil, Nil, Nil, falso, create_suspended, nil, nil, si, pi); } Showmsg ('preparar gancho' + func2hook + '...'); PtrReal: = GetProcAddress (getModuleHandle ('kernel32.dll'), func2hook); Si se le asigna (ptrReal), entonces showmsg ('real' + func2hook + 'addr:' + inttoHex (dword (ptrreal), 8)) de lo contrario comience showmsg ('addr:' + func2Hook + 'es ilegible!'); // reanude (pi.hthread); Salida; fin; ReadProcessMemory (GetCurrentProcess, Ptrreal, @bytes, 5, RTN); // ReadProcessMemory (pi.hprocess, ptrreal, @bytes, 5, rtn); Si bytes [0] <> chr ($ e9) entonces comience copymemory (@originalbytes, @bytes, 5); Showmsg (Func2Hook + 'Havn' no ha sido enganchado! '); End el otro comienza showmsg (func2hook + '¡se han enganchado! ¡Salga!'); // reanude (pi.hthread); salida; fin; cbstolen: = 0; mientras que cbstolen <5 do cbstolen: = cbstolen + lde32 (puntero (dword (ptrreal) + cbstolen)); Showmsg ('Let' robe el primer ' + intToStr (cbstolen) +' bytes :) '); Showmsg ('Pero hazlo primero que se escriba primero ...'); si virtualProtect (ptrReal, cbstolen, page_execute_readwrite, @tmp) luego showmsg ('make' + inttoHex (dword (ptrreal), 8) + 'strenspceed whititit!') Else comienza showmsg ('hoops! make' + inttohex (dWord (ptrreal (ptrreal (ptrreal ), 8) + 'Fallado de escritura!'); // reanude (pi.hthread); salida; fin; Showmsg ('ensamblar los códigos JMP y el gancho' + func2hook + '...'); GetMem (Hookjmp, 5); prueba Hookjmp [0]: = chr ($ e9); ASM PUSH EAX LEA EAX, FAKECODE MOV TMP, EAX POP EAX END; tmp: = tmp - dword (ptrreal) - 5; CopyMemory (@HookJMP [1], @tmp, 4); ASM PUSH EAX LEA EAX, RTNCODE MOV TMP, EAX POP EAX END; VirtualProtect (Pointer (TMP), CBStolen, Page_Execute_readwrite, @RTN); CopyMemory (Pointer (TMP), Ptrreal, Cbstolen); WriteProCessMemory (GetCurrentProcess, Ptrreal, Hookjmp, 5, RTN); // WriteProCessMemory (pi.hprocess, Ptrreal, Hookjmp, 5, RTN); Showmsg ('Hook' + Func2Hook + '¡SUCHUDE! Hilo de reanudación!'); Finalmente Freemem (HookJMP); // reanude (pi.hthread); fin; salida; Fakecode: // No hay cuerdas desde aquí en ASM Int 3 End; ASM PUSH EAX LEA EAX, [ESP+4] MOV P, EAX POP EAX END; Si dword (p^) - ntdllbase <ntdlllength entonces asm pop p pop eax pop eax pop eax mov eax, 0 jmp p // empuje p // retin; // MessageBox (0, pChar (P), '', 0); Rtncode: ASM NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP MOVA EX, PtrReal Add Eax, Cbstolen JMP Eax End; fin; var ptr, ppp: puntero; procedimiento tForm1.Button2Click (remitente: tobject); begin {asm llame ppp; fin; salida; } Button3Click (nil); Ptr: = virtualAlloc (nil, 1024, mem_commit, page_execute_readwrite); Si no se asigna (PTR), entonces memo1.lines.add ('Error fatal: virtualAlloc falló!') else Memo1.lines.add ('virtualAlloc sucdeed! Ptr =' + inttoHex (dword (ptr), 8)); fin; procedimiento tForm1.FormDestroy (remitente: tobject); BOTY3CHICK (NIL); UnmapViewoffile (PPP); CloseHandle (H); fin; procedimiento tForm1.Button3Click (remitente: tobject); Comience si se le asigna (PTR) entonces VirtualFree (PTR, 0, MEM_RELEASE); fin; procedimiento tForm1.FormCreate (remitente: tobject); begin h: = createFileMapping ($ ffffffff, nil, page_readwrite o sec_commit, 0, 1, 'pe'); PPP: = MAPVIEWOFFILE (H, FILE_MAP_ALL_ACCESS, 0,0,0); Leyenda: = inttoHex (dword (PPP), 8); char (ppp^): = chr ($ c3); fin; fin. ====== Unidad1 里有很多垃圾代码 , 因为这个防 Hook 的程序只是一个副产品。有用代码写成 dll 注入其他进程就可以防 gancho 了 , 已经试过没问题。代码风格比较差 不过不知道 不过不知道怎么改的更好(如将 Fakecode 部分放到单独过程中)。如果你改好了希望能发给我一份。 Mlde32Unit 代码来自 29a 第七期 , 作者忘记了 不好意思。 不好意思。