CnHardwareBreakpoint.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2018 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnHardwareBreakpoint;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:不可视工具组件包
  24. * 单元名称:硬件断点类,代码硬件HOOK单元
  25. * 单元作者:CodeGame
  26. * 备 注:提供类表:TCGL_VectoredException, TCGL_HardwareBreakpoints
  27. * 开发平台:PWinXP + Delphi 2007
  28. * 兼容测试:暂无
  29. * 单元标识:$Id: CnHardwareBreakpoint.pas 1146 2012-10-24 06:25:41Z liuxiaoshanzhashu@gmail.com $
  30. * 修改记录:2013.08.08 v1.0
  31. * 移植单元
  32. ================================================================================
  33. |</PRE>}
  34. interface
  35. {$I CnPack.inc}
  36. uses
  37. Windows, SysUtils, Classes;
  38. const
  39. THREAD_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or $3FF);
  40. type
  41. TCallbackInstance = array[1..12] of Byte; //对象成员回调
  42. PExceptionPointers = ^TExceptionPointers;
  43. TVEHCallbackProc = function(pException: PExceptionPointers): Integer of object; stdcall;
  44. THardwareBreakError = procedure(ErrorId: Integer; Error: Exception; pException: PExceptionPointers) of object;
  45. TCnVectoredException = class(TComponent) //VEH 基类
  46. private
  47. { Private declarations }
  48. FHandler: HWND; //VEH句柄
  49. FOnCallback: TVEHCallbackProc;
  50. FOnCallback_Instance: TCallbackInstance;
  51. procedure MakeCallbackInstance(var Instance: TCallbackInstance; ObjectAddr, FunctionAddr: Pointer);
  52. procedure SetBreakCallbackProc(FunctionAddr: Pointer); //设置回调函数地址
  53. protected
  54. { protected declarations }
  55. function DoVEHCallback(pException: PExceptionPointers): Integer; virtual; stdcall;
  56. public
  57. { Public declarations }
  58. constructor Create(AOwner: TComponent); override;
  59. destructor Destroy; override;
  60. function InstallVEH: boolean; virtual; //安装VEH
  61. procedure RemoveVEH; virtual; //卸载VEH
  62. property OnCallback: TVEHCallbackProc read FOnCallback write FOnCallback;
  63. published
  64. { published declarations }
  65. end;
  66. PBreakpointsProc = ^TBreakpointProc;
  67. TBreakpointProc = procedure(pException: PExceptionPointers) of object;
  68. TCnHardwareBreakpoint = class(TCnVectoredException) //硬断点类
  69. private
  70. { Private declarations }
  71. FDr1: DWORD;
  72. FDr2: DWORD;
  73. FDr3: DWORD;
  74. FDr4: DWORD;
  75. FOnBreakpoint1: TBreakpointProc;
  76. FOnBreakpoint2: TBreakpointProc;
  77. FOnBreakpoint3: TBreakpointProc;
  78. FOnBreakpoint4: TBreakpointProc;
  79. FOnHardwareBreakError: THardwareBreakError;
  80. protected
  81. { protected declarations }
  82. function DoVEHCallback(pException: PExceptionPointers): Integer; override; stdcall;
  83. procedure DoBreakpoint1(pException: PExceptionPointers); virtual;
  84. procedure DoBreakpoint2(pException: PExceptionPointers); virtual;
  85. procedure DoBreakpoint3(pException: PExceptionPointers); virtual;
  86. procedure DoBreakpoint4(pException: PExceptionPointers); virtual;
  87. procedure DoHardwareBreakError(ErrorId: Integer; Error: Exception; pException: PExceptionPointers); virtual;
  88. public
  89. { Public declarations }
  90. constructor Create(AOwner: TComponent); override;
  91. destructor Destroy; override;
  92. procedure ClearBreakpoints;
  93. procedure SetBreakpoints; //设置硬件断点使其生效
  94. published
  95. { published declarations }
  96. property BreakpointsAdderss1: DWORD read FDr1 write FDr1 default 0;
  97. property BreakpointsAdderss2: DWORD read FDr2 write FDr2 default 0;
  98. property BreakpointsAdderss3: DWORD read FDr3 write FDr3 default 0;
  99. property BreakpointsAdderss4: DWORD read FDr4 write FDr4 default 0;
  100. property OnBreakpoint1: TBreakpointProc read FOnBreakpoint1 write FOnBreakpoint1;
  101. property OnBreakpoint2: TBreakpointProc read FOnBreakpoint2 write FOnBreakpoint2;
  102. property OnBreakpoint3: TBreakpointProc read FOnBreakpoint3 write FOnBreakpoint3;
  103. property OnBreakpoint4: TBreakpointProc read FOnBreakpoint4 write FOnBreakpoint4;
  104. property OnHardwareBreakError: THardwareBreakError read FOnHardwareBreakError write FOnHardwareBreakError;
  105. end;
  106. implementation
  107. { TCnVectoredException }
  108. procedure TCnVectoredException.MakeCallbackInstance(var Instance: TCallbackInstance; ObjectAddr, FunctionAddr: Pointer);
  109. {----------------------------}
  110. { CallbackCode DASM }
  111. {----------------------------}
  112. { POP EAX; }
  113. { PUSH ObjectAddr; }
  114. { PUSH EAX; }
  115. { JMP FunctionAddr; }
  116. {----------------------------}
  117. const CallbackCode: TCallbackInstance =
  118. //($8B,$04,$24,$50,$B8,$00,$00,$00,$00,$89,$44,$24,$04,$E9,$00,$00,$00,$00);
  119. ($58, $68, $00, $00, $00, $00, $50, $E9, $00, $00, $00, $00);
  120. begin
  121. Move(CallbackCode, Instance, SizeOf(TCallbackInstance));
  122. PDWORD(@Instance[3])^ := DWORD(ObjectAddr);
  123. PDWORD(@Instance[9])^ := DWORD(DWORD(FunctionAddr) - DWORD(@Instance) - 12);
  124. end;
  125. procedure TCnVectoredException.SetBreakCallbackProc(FunctionAddr: Pointer);
  126. begin
  127. MakeCallbackInstance(FOnCallback_Instance, Self, FunctionAddr);
  128. end;
  129. constructor TCnVectoredException.Create(AOwner: TComponent);
  130. begin
  131. inherited;
  132. FHandler := 0;
  133. SetBreakCallbackProc(@TCnVectoredException.DoVEHCallback);
  134. InstallVEH;
  135. end;
  136. destructor TCnVectoredException.Destroy;
  137. begin
  138. RemoveVEH;
  139. inherited;
  140. end;
  141. function TCnVectoredException.InstallVEH: boolean;
  142. type
  143. TAddVectored = function(FirstHandler: Integer; VectoredHandler: Pointer): HWND; stdcall;
  144. var
  145. _pAddVectored: TAddVectored;
  146. begin
  147. Result := False;
  148. if FHandler <> 0 then Exit;
  149. _pAddVectored := GetProcAddress(LoadLibrary('Kernel32.dll'), 'AddVectoredExceptionHandler');
  150. if not Assigned(_pAddVectored) then Exit;
  151. FHandler := _pAddVectored(1, @Self.FOnCallback_Instance); //安装VEH
  152. Result := True;
  153. end;
  154. procedure TCnVectoredException.RemoveVEH;
  155. type
  156. TRemoveVectored = function(VectoredHandler: HWND): Integer; stdcall;
  157. var
  158. _pRemoveVectored: TRemoveVectored;
  159. begin
  160. if FHandler = 0 then Exit;
  161. _pRemoveVectored := GetProcAddress(LoadLibrary('Kernel32.dll'), 'RemoveVectoredExceptionHandler');
  162. if Assigned(_pRemoveVectored) then _pRemoveVectored(FHandler); //卸载VEH
  163. FHandler := 0;
  164. end;
  165. function TCnVectoredException.DoVEHCallback(pException: PExceptionPointers): Integer;
  166. begin
  167. Result := 0;
  168. if Assigned(Self.FOnCallback) then
  169. try
  170. Result := Self.FOnCallback(pException);
  171. except
  172. end;
  173. end;
  174. { TCnHardwareBreakpoint }
  175. procedure TCnHardwareBreakpoint.ClearBreakpoints;
  176. var
  177. _Regs: CONTEXT;
  178. begin
  179. {设置断点}
  180. FDr1 := 0;
  181. FDr2 := 0;
  182. FDr3 := 0;
  183. FDr4 := 0;
  184. _Regs.ContextFlags := CONTEXT_DEBUG_REGISTERS;
  185. GetThreadContext(GetCurrentThread, _Regs);
  186. _Regs.Dr0 := FDr1;
  187. _Regs.Dr1 := FDr2;
  188. _Regs.Dr2 := FDr3;
  189. _Regs.Dr3 := FDr4;
  190. _Regs.Dr7 := $7FF;
  191. SetThreadContext(GetCurrentThread, _Regs);
  192. end;
  193. constructor TCnHardwareBreakpoint.Create(AOwner: TComponent);
  194. begin
  195. inherited;
  196. FDr1 := 0;
  197. FDr2 := 0;
  198. FDr3 := 0;
  199. FDr4 := 0;
  200. SetBreakCallbackProc(@TCnHardwareBreakpoint.DoVEHCallback);
  201. end;
  202. destructor TCnHardwareBreakpoint.Destroy;
  203. begin
  204. ClearBreakpoints;
  205. inherited;
  206. end;
  207. procedure TCnHardwareBreakpoint.DoBreakpoint1(pException: PExceptionPointers);
  208. begin
  209. if Assigned(Self.FOnBreakpoint1) then
  210. try
  211. Self.FOnBreakpoint1(pException);
  212. except
  213. on Error: Exception do DoHardwareBreakError(1, Error, pException);
  214. end;
  215. end;
  216. procedure TCnHardwareBreakpoint.DoBreakpoint2(pException: PExceptionPointers);
  217. begin
  218. if Assigned(Self.FOnBreakpoint2) then
  219. try
  220. Self.FOnBreakpoint2(pException);
  221. except
  222. on Error: Exception do DoHardwareBreakError(2, Error, pException);
  223. end;
  224. end;
  225. procedure TCnHardwareBreakpoint.DoBreakpoint3(pException: PExceptionPointers);
  226. begin
  227. if Assigned(Self.FOnBreakpoint3) then
  228. try
  229. Self.FOnBreakpoint3(pException);
  230. except
  231. on Error: Exception do DoHardwareBreakError(3, Error, pException);
  232. end;
  233. end;
  234. procedure TCnHardwareBreakpoint.DoBreakpoint4(pException: PExceptionPointers);
  235. begin
  236. if Assigned(Self.FOnBreakpoint4) then
  237. try
  238. Self.FOnBreakpoint4(pException);
  239. except
  240. on Error: Exception do DoHardwareBreakError(4, Error, pException);
  241. end;
  242. end;
  243. procedure TCnHardwareBreakpoint.DoHardwareBreakError(ErrorId: Integer; Error: Exception;
  244. pException: PExceptionPointers); //异常处理
  245. begin
  246. if Assigned(Self.FOnHardwareBreakError) then
  247. Self.FOnHardwareBreakError(ErrorId, Error, pException);
  248. end;
  249. function TCnHardwareBreakpoint.DoVEHCallback(pException: PExceptionPointers): Integer;
  250. begin
  251. Result := 0;
  252. case PException^.ExceptionRecord^.ExceptionCode of
  253. EXCEPTION_SINGLE_STEP:
  254. begin
  255. if PException^.ContextRecord^.Eip = FDr1 then Self.DoBreakpoint1(pException) else
  256. if PException^.ContextRecord^.Eip = FDr2 then Self.DoBreakpoint2(pException) else
  257. if PException^.ContextRecord^.Eip = FDr3 then Self.DoBreakpoint3(pException) else
  258. if PException^.ContextRecord^.Eip = FDr4 then Self.DoBreakpoint4(pException);
  259. Result := -1;
  260. end;
  261. end;
  262. end;
  263. procedure TCnHardwareBreakpoint.SetBreakpoints;
  264. var
  265. _Regs: CONTEXT;
  266. begin
  267. {设置断点}
  268. _Regs.ContextFlags := CONTEXT_DEBUG_REGISTERS;
  269. GetThreadContext(GetCurrentThread, _Regs);
  270. _Regs.Dr0 := FDr1;
  271. _Regs.Dr1 := FDr2;
  272. _Regs.Dr2 := FDr3;
  273. _Regs.Dr3 := FDr4;
  274. _Regs.Dr7 := $7FF;
  275. SetThreadContext(GetCurrentThread, _Regs);
  276. end;
  277. end.