防止全局hook入侵Delphi版2000以上系统适用(part2)

2016-02-19 18:49 12 1 收藏

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是防止全局hook入侵Delphi版2000以上系统适用(part2),一起来学习了解下吧!

【 tulaoshi.com - 编程语言 】

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } procedure ShowMsg(s: string); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses MLDE32Unit; const DesPath = 'C:Program FilesBorlandDelphi6ProjectsAdv APIHOOKTestvt.exe'; Func2Hook = 'FreeLibrary'; var //must be a globle variable PtrReal: Pointer; cbStolen: Cardinal; NtDllBase, NtDllLength: integer; p: pointer; h:dword; procedure TForm1.ShowMsg(s: string); begin Memo1.Lines.Add(s); end; procedure TForm1.Button1Click(Sender: TObject); label FakeCode, RtnCode; var // si: STARTUPINFO; // pi: PROCESS_INFORMATION; OriginalBytes: Array [0..4] of Char; HookJmp: PChar; Rtn: Cardinal; Bytes: Array [0..4] of Char; tmp: Cardinal; peb, ldr, flink: pointer; bs: DWORD; begin PtrReal := nil; NtDllLength := 0; NtDllBase := GetModuleHandle('ntdll.dll'); asm mov eax,fs:[$30] mov peb,eax end; ldr := pointer(dword(pointer(dword(peb)+12)^)); flink := pointer(dword(pointer(dword(ldr)+12)^)); p := flink; repeat bs := DWORD(pointer(dword(p)+$18)^); if bs = NtDllBase then begin NtDllLength := DWORD(pointer(dword(p)+$20)^); break; end; p := pointer(dword(p^)); until dword(flink) = dword(p^); if NtDllLength = 0 then ShowMsg('Can''t get ntdll.dll image size!'); { ShowMsg('Creating suspended process ...'); ZeroMemory(@si, sizeof(STARTUPINFO)); si.cb := sizeof(STARTUPINFO); CreateProcess(DesPath, nil, nil, nil, False, CREATE_SUSPENDED, nil, nil, si, pi); } ShowMsg('Preparing HOOK ' + Func2Hook + ' ...'); PtrReal := GetProcAddress(GetModuleHandle('Kernel32.dll'), Func2Hook); if Assigned(PtrReal) then ShowMsg('Real ' + Func2Hook + ' Addr: ' + inttohex(DWORD(PtrReal), 8)) else begin ShowMsg(' Addr: ' + Func2Hook + ' is unreadable! Exit!'); // ResumeThread(pi.hThread); Exit; end; ReadProcessMemory(GetCurrentProcess, PtrReal, @Bytes, 5, Rtn); // ReadProcessMemory(pi.hProcess, PtrReal, @Bytes, 5, Rtn); if Bytes[0] Chr($E9) then begin CopyMemory(@OriginalBytes, @Bytes, 5); ShowMsg(Func2Hook + ' havn''t been hooked!'); end else begin ShowMsg(Func2Hook + ' have been hooked! Exit!'); // ResumeThread(pi.hThread); exit; end; cbStolen :=0; while cbStolen 5 do cbStolen := cbStolen + LDE32(Pointer(DWORD(PtrReal) + cbStolen)); ShowMsg('Let''s steal the first ' + inttostr(cbStolen) + ' bytes :)'); ShowMsg('But make it writable first ...'); if VirtualProtect(PtrReal ,cbStolen , PAGE_EXECUTE_READWRITE, @tmp) then ShowMsg('Make ' + inttohex(DWORD(PtrReal), 8) + ' writable succeed!') else begin ShowMsg('Hoops! Make ' + inttohex(DWORD(PtrReal), 8) + ' writable failed! Exit!!'); // ResumeThread(pi.hThread); exit; end; ShowMsg('Assemble Jmp codes & hook ' + Func2Hook + '...'); GetMem(HookJmp, 5); try 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 + ' succeed! Resume thread!'); finally Freemem(HookJmp); // ResumeThread(pi.hThread); end; exit; FakeCode: //No strings from here on asm int 3 end; asm push eax lea eax, [esp+4] mov p, eax pop eax end; if dword(p^) - ntdllbase NtDllLength then asm pop p pop eax pop eax pop eax mov eax, 0 jmp p // push p // ret end; //messagebox(0,pchar(p),'',0); RtnCode: asm nop nop nop nop nop nop nop nop nop nop nop nop nop mov eax, PtrReal add eax, cbStolen jmp eax end; end; var Ptr, ppp: Pointer; procedure TForm1.Button2Click(Sender: TObject); begin {asm call ppp; end; exit; } Button3Click(nil); Ptr := VirtualAlloc(nil, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if not Assigned(Ptr) then Memo1.Lines.Add('Fatal Error: VirtualAlloc failed!') else Memo1.Lines.Add('VirtualAlloc succeed! Ptr = ' + inttohex(DWORD(Ptr), 8)); end; procedure TForm1.FormDestroy(Sender: TObject); begin Button3Click(nil); UnmapViewOfFile(ppp); CloseHandle(h); end; procedure TForm1.Button3Click(Sender: TObject); begin if Assigned(Ptr) then VirtualFree(Ptr, 0, MEM_RELEASE); end; procedure TForm1.FormCreate(Sender: TObject); begin h := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE or SEC_COMMIT, 0, 1, 'pe'); ppp := MapViewOfFile(h,FILE_MAP_ALL_ACCESS,0,0,0); caption := inttohex(dword(ppp),8); char(ppp^) := Chr($C3); end; end. ====== Unit1里有很多垃圾代码,因为这个防hook的程序只是一个副产品。有用代码写成dll注入其他进程就可以防hook了,已经试过没问题。代码风格比较差,不过不知道怎么改的更好(如将FakeCode部分放到单独过程中)。如果你改好了希望能发给我一份。 MLDE32Unit代码来自29A第七期,作者忘记了,不好意思。

来源:https://www.tulaoshi.com/n/20160219/1619081.html

延伸阅读
标签: 办公软件
摘 要:Excel是当前最流行的数据报表制作工具。本文介绍如何使用Delphi来控制Excel完成数据库与报表之间的数据交换,讨论了报表制作工程中的一些细节性问题。 关键字:Delphi,Excel,报表 引言 数据报表作为企事业单位上报和下达的重要信息载体,随着信息化建设的不断推进,在实际的工作中得到了前所未有的应用。因...
标签: 电脑入门
搜索功能这么重要的东西当然不会消失,在Win8中也只是换了个地方而已,而且变得比以前更加强大了。首先我们来看看怎么调出搜索框,这里我们主要推荐两个方法。第一个是将鼠标滑动到屏幕右下角然后上提,在Charm Bar上的顶部第一个就是搜索按键了。 搜索键位于Charm Bar中 还有一种方法就是Windows键+X的组合键也能找到搜索,进入搜索之后...
标签: Delphi
  一、Win2000服务简介 服务程序(Service Application)是一种运行于WinNT的后台程序,每个服务程序(Service Application)中可能包含若干个服务(Service),每个服务就是其中的一个线程(该服务也可以创建多个子线程)。采用服务,应用程序可以获得特殊的权限,而且不会被用户通过Win2000的任务管理器直接结束程序,所以服务...
LINE香港官方教你防止帐号被恶意入侵   LINE香港官方教你防止帐号被恶意入侵,随着LINE使用量不断提升,不法之徒亦滥用LINE平台进行非法活动,LINE团队表示高度关注。在此,我们希望再次提醒各位用户在使用LINE以及各种SNS服务时请采取适当的防护措施,避免任何损失。 1) 请不要打开任何可疑的附件和链接。 2) 请不要随...
标签: 电脑入门
随着网络科技技术的不断发展,作为专门入侵他人电脑系统窃取他人资料或破坏他人系统的网络黑客也是无处不在,不管是旧系统还是新系统都很难幸免,那么作为刚出炉的win8系统该怎么有效的来抵御黑客的入侵呢? 操作步骤: 1、打开Windows本地安全策略-搜索键入secpol.msc后回车。 2、防止黑客或恶意程序暴力破解系统密码。 确保被选...

经验教程

51

收藏

62
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部