Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

DDetoursライブラリを利用してWindowsアプリケーションを迅速かつ簡単にフックする

hook-delphi-and-windows-api-functions-with-ddetours-ja

「フック(フッキング)」とは、ソフトウェア/コンポーネント間で渡されるWindows API関数などの呼び出し、メッセージ、またはイベントをインターセプト(傍受)することにより、オペレーティングシステム、アプリケーション、または他のソフトウェア部品の動作を変更または拡張するための方法です。

DDetoursは、関数にフックを挿入、あるいは関数からフックを削除可能なライブラリで、Windows 32ビット/64ビットの両方のアーキテクチャをサポートしています。このライブラリの基本的な考え方は、インターセプトされた関数への無条件ジャンプ命令を挿入することで、ターゲット関数のプロローグを置換することです。

フッキングのルール

DDetoursライブラリを利用して正しくフッキングを実行するには、以下のルールに従う必要があります。

DDetoursライブラリを利用したサンプルコードは、以下の通りです。

type
  TMessageBox = function(hWnd: hWnd; lpText, lpCaption: LPCWSTR; uType: UINT): Integer; stdcall;
 
var
  TrampolineMessageBox: TMessageBox = nil;
 
function InterceptMessageBox(hWnd: hWnd; lpText, lpCaption: LPCWSTR; uType: UINT): Integer; stdcall;
var
  Self: TMain;
begin
  Self := GetTrampolineParam(TrampolineMessageBox);
  Self.Caption := 'MessageBox hooked !';
  Result := TrampolineMessageBox(hWnd, 'this text was hooked', 'this title was hooked', MB_ICONWARNING);
end;
 
procedure TMain.FormCreate(Sender: TObject);
begin
  BtnUnHook.Enabled := False;
end;
 
procedure TMain.BtnHookClick(Sender: TObject);
begin
  TrampolineMessageBox := InterceptCreate(@MessageBox, @InterceptMessageBox, Self);
  BtnUnHook.Enabled := True;
  BtnHook.Enabled := False;
end;
 
procedure TMain.BtnMsgBoxClick(Sender: TObject);
begin
  MessageBox(0, 'text', 'caption', 0);
end;
 
procedure TMain.BtnUnHookClick(Sender: TObject);
begin
  if Assigned(TrampolineMessageBox) then
  begin
    InterceptRemove(@TrampolineMessageBox);
    TrampolineMessageBox := nil;
    BtnHook.Enabled := True;
    BtnUnHook.Enabled := False;
  end;
end;
 
initialization
 
finalization
 
if Assigned(TrampolineMessageBox) then
  InterceptRemove(@TrampolineMessageBox);

DDetoursライブラリは、無償で利用可能なオープンソースですが、Mozilla Public License 2.0として規定されています。このライブラリはGitHub経由でこちらから入手できます。なお、エンバカデロではこのライブラリに関するテクニカルサポートサービスは提供しておりません。

Exit mobile version