Unregistered
|
Здравствуйте! Не могу разобраться, почему не происходит пошаговое выполнение и запуск программы? Программа почему-то циклится в EXCEPTION_SINGLE_STEP и до запуска дело не доходит, если закомментировать следующие строки: | Код | Context.EFlags := Context.EFlags or $100; SetThreadContext(pi.hThread, Context);
|
то программа запуститься, но EXCEPTION_SINGLE_STEP, соответственно, выполниться только один раз. Вот мой код: | Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
var fName: PChar = 'Test.exe'; old : BYTE; int3 : BYTE = $CC;
procedure TForm1.Button1Click(Sender: TObject); var hFile : THandle; hFileMap : THandle; ImageDosHeader : PImageDosHeader; ImageFileHeader : PImageFileHeader; ImageOptionalHeader: PImageOptionalHeader; EntryPoint : DWORD;
si : TStartupInfo; pi : TProcessInformation;
DbgEvent : _DEBUG_EVENT; Context : _CONTEXT;
NumOfRead : DWORD; begin hFile := FileOpen(fName, fmOpenReadWrite or fmShareDenyNone); hFileMap := CreateFileMapping(hFile, nil, PAGE_READWRITE, 0, 0, nil); ImageDosHeader := MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0); ImageFileHeader := Pointer(Cardinal(ImageDosHeader) + ImageDosHeader._lfanew); ImageOptionalHeader := Pointer(Cardinal(ImageFileHeader) + SizeOf(ImageFileHeader^) + 4); EntryPoint := ImageOptionalHeader.AddressOfEntryPoint + ImageOptionalHeader.ImageBase; UnmapViewOfFile(ImageDosHeader); CloseHandle(hFileMap); CloseHandle(hFile);
Memo1.Lines.Add('Entry Point: ' + IntToHex(EntryPoint, 8));
ZeroMemory(@si, SizeOf(si)); if not CreateProcess(nil, fName, nil, nil, False, DEBUG_PROCESS or DEBUG_ONLY_THIS_PROCESS, nil, nil, si, pi) then begin Memo1.Lines.Add('Error: Cannot to create process!'); Exit; end;
while WaitForDebugEvent(DbgEvent, INFINITE) do begin Application.ProcessMessages; Sleep(55);
case DbgEvent.dwDebugEventCode of
EXIT_PROCESS_DEBUG_EVENT: begin CloseHandle(pi.hProcess); CloseHandle(pi.hThread); ExitProcess(0); end;
CREATE_PROCESS_DEBUG_EVENT: begin ReadProcessMemory(pi.hProcess, Pointer(EntryPoint), @old, 1, NumOfRead); WriteProcessMemory(pi.hProcess, Pointer(EntryPoint), @int3, 1, NumOfRead); end;
EXCEPTION_DEBUG_EVENT: begin case DbgEvent.Exception.ExceptionRecord.ExceptionCode of
EXCEPTION_BREAKPOINT: begin Context.ContextFlags := CONTEXT_CONTROL; GetThreadContext(pi.hThread, Context); if Context.eip - 1 = EntryPoint then begin Context.eip := Context.eip - 1; WriteProcessMemory(pi.hProcess, Pointer(EntryPoint), @old, 1, NumOfRead); Context.EFlags := Context.EFlags or $100; SetThreadContext(pi.hThread, Context);
Memo1.Lines.Add('Entry point restored...'#13#10'Tracing...'); end; ContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_CONTINUE); end; // EXCEPTION_BREAKPOINT
EXCEPTION_SINGLE_STEP: begin Context.ContextFlags := CONTEXT_CONTROL; GetThreadContext(pi.hThread, Context); Context.EFlags := Context.EFlags or $100; SetThreadContext(pi.hThread, Context);
Form1.Caption := IntToHex(Context.Eip, 8); ContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_CONTINUE); end; // EXCEPTION_SINGLE_STEP
end; // case DbgEvent.Exception.ExceptionRecord.ExceptionCode end; // end case of EXCEPTION_DEBUG_EVENT end; // case DbgEvent.dwDebugEventCode
ContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_EXCEPTION_NOT_HANDLED); end; // while WaitForDebugEvent
end;
end.
|
Ну и к примеру 'Test.exe': | Код | uses Windows; begin MessageBox(0, 'Some text message...', 'Caption', MB_OK); end.
|
|