调试api初步学习

  1. //加密解密技术内幕,示例,asm转delphi
  2. unit Unit1;
  3. interface
  4. uses
  5.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6.   Dialogs, StdCtrls, DB, ADODB;
  7. type
  8.   TForm1 = class(TForm)
  9.     OpenDialog1: TOpenDialog;
  10.     Button1: TButton;
  11.     Memo1: TMemo;
  12.     ADOConnection1: TADOConnection;
  13.     procedure Button1Click(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.     procedure getREGISTER(handle:THandle);
  17.   public
  18.     { Public declarations }
  19.   end;
  20. var
  21.   Form1: TForm1;
  22. implementation
  23. {$R *.dfm}
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. const
  26.   APPNAME = 'Win32 Debug Example no.1';
  27.   NEWTHREAD = 'A new thread is created';
  28.   ENDTHREAD = 'A thread is destroyed';
  29. var
  30.   startinfo :STARTUPINFO;
  31.   pi:PROCESS_INFORMATION;
  32.   DBEvent :DEBUG_EVENT;
  33.   filename :string;
  34.   buffer : string;
  35.   ProcessInfo :string;
  36. begin
  37.   ProcessInfo := 'File Handle   : %x '+#13#10+'Process Handle: %x' + #13#10+'Thread Handle : %x'+#13#10+'Image Base    : %x'+#13#10+'Start Address : %x';
  38.   if OpenDialog1.Execute then
  39.   begin
  40.     filename := OpenDialog1.FileName;
  41.     GetStartupInfo(startinfo);   //填充startinfo结构
  42.     createprocess(PAnsiChar(filename),nil,nil,nil,False,DEBUG_PROCESS + DEBUG_ONLY_THIS_PROCESS,nil,nil,startinfo,pi);   //调试模式加载进程
  43.     while True do
  44.     begin
  45.       WaitForDebugEvent(DBEvent,INFINITE) ;       //等待调试事件
  46.       if DBEvent.dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT THEN       //进程退出
  47.       begin
  48.         MessageBox(0,'The debuggee exits','Win32 Debug Example no.1',mb_ok + MB_ICONINFORMATION);
  49.         Exit;
  50.       end
  51.       else if DBEvent.dwDebugEventCode = create_process_debug_event then    //进程被创建
  52.       begin
  53.         buffer := Format(ProcessInfo,[DBEvent.CreateProcessInfo.hFile,DBEvent.CreateProcessInfo.hProcess,DBEvent.CreateProcessInfo.hThread,integer(DBEvent.CreateProcessInfo.lpBaseOfImage),Integer(DBEvent.CreateProcessInfo.lpStartAddress)]);
  54.         MessageBox(0,PAnsiChar(buffer),'Win32 Debug Example no.1',MB_OK + mb_iconinformation);
  55.         getREGISTER(DBEvent.CreateProcessInfo.hThread);
  56.       end
  57.       else if DBEvent.dwDebugEventCode = exception_debug_event then
  58.       begin
  59.         if DBEvent.Exception.ExceptionRecord.ExceptionCode = exception_breakpoint then
  60.           begin
  61.             ContinueDebugEvent(DBEvent.dwProcessId,DBEvent.dwThreadId,DBG_CONTINUE);       //继续运行
  62.             Continue;
  63.           end;
  64.       end
  65.       else if DBEvent.dwDebugEventCode = create_thread_debug_event then      //线程被创建
  66.         MessageBox(0,NEWTHREAD,APPNAME,MB_OK+MB_ICONINFORMATION)
  67.       else if DBEvent.dwDebugEventCode = exit_thread_debug_event then        //线程退出
  68.         MessageBox(0,ENDTHREAD,APPNAME,MB_OK+MB_ICONINFORMATION);
  69.       ContinueDebugEvent(DBEvent.dwProcessId,DBEvent.dwThreadId,DBG_EXCEPTION_NOT_HANDLED);
  70.       end;
  71.       CloseHandle(pi.hProcess);
  72.       CloseHandle(pi.hThread);
  73.     end;
  74. end;
  75. procedure TForm1.getREGISTER(handle:THandle);
  76. var
  77.   ctext : CONTEXT;
  78. begin
  79.   Memo1.Clear;
  80.   ctext.ContextFlags := CONTEXT_FULL;
  81.   GetThreadContext(handle,ctext);                            //获得进程上下文内容
  82.   Memo1.Lines.Add(Format('DR0 : %x',[ctext.Dr0]));
  83.   Memo1.Lines.Add(Format('DR1 : %x',[ctext.Dr1]));
  84.   Memo1.Lines.Add(Format('DR2 : %x',[ctext.Dr2]));
  85.   Memo1.Lines.Add(Format('DR3 : %x',[ctext.Dr3]));
  86.   Memo1.Lines.Add(Format('EDI : %x',[ctext.Edi]));
  87.   Memo1.Lines.Add(Format('Esi : %x',[ctext.Esi]));
  88.   Memo1.Lines.Add(Format('Ebx : %x',[ctext.Ebx]));
  89.   Memo1.Lines.Add(Format('Edx : %x',[ctext.Edx]));
  90.   Memo1.Lines.Add(Format('Ecx : %x',[ctext.Ecx]));
  91.   Memo1.Lines.Add(Format('Eax : %x',[ctext.Eax]));
  92.   Memo1.Lines.Add(Format('Ebp : %x',[ctext.Ebp]));
  93.   Memo1.Lines.Add(Format('Eip : %x',[ctext.Eip]));
  94.   Memo1.Lines.Add(Format('Esp : %x',[ctext.Esp]));
  95. end;
  96. end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值