c ++/c#からのアプリケーションフック(winsock)
質問
WinSocket関数を呼び出す(古い)アプリケーションを取得しました。
struct hostent* FAR gethostbyname(
__in const char *name
);
現在、それをインポートしています WS32_DLL。#52 代わりに、通常の名前の呼び出し。
私の意図は、ホスト検索が行われたときにメッセージボックスを開くようなことをすることができることです(アプリの開始時に)。
プラグマコメントが#52を指し、アプリdir(「exe.local」および「exe.manifest」ファイルを含むためにリダイレクトを試みる)を使用してC ++ DLLを作成しようとしましたが、C:をロードしました。代わりにWindows System32。
その後、AC#プロジェクトを作成してプロセス自体を起動し(プロセスオブジェクトからPIDを取得します)、EasyHook DLLを追加しました。
で例を確認しました: http://www.codeproject.com/kb/dll/easyhook64.aspx
通話の変更:
FileMon.FileMonInterface Interface;
LocalHook CreateFileHook;
Stack<String> Queue = new Stack<String>();
public Main(
RemoteHooking.IContext InContext,
String InChannelName)
{
// connect to host...
Interface =
RemoteHooking.IpcConnectClient<FileMon.FileMonInterface>(InChannelName);
}
public void Run(
RemoteHooking.IContext InContext,
String InChannelName)
{
// install hook...
try
{
CreateFileHook = LocalHook.Create(
LocalHook.GetProcAddress("ws2_32.dll", "gethostbyname"),
new DCreateFile(GetHostByName_Hooked),
this);
CreateFileHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}
catch (Exception ExtInfo)
{
Interface.ReportException(ExtInfo);
return;
}
Interface.IsInstalled(RemoteHooking.GetCurrentProcessId());
// wait for host process termination...
try
{
while (true)
{
Thread.Sleep(500);
// transmit newly monitored file accesses...
if (Queue.Count > 0)
{
String[] Package = null;
lock (Queue)
{
Package = Queue.ToArray();
Queue.Clear();
}
Interface.OnCreateFile(RemoteHooking.GetCurrentProcessId(), Package);
}
else
Interface.Ping();
}
}
catch
{
// NET Remoting will raise an exception if host is unreachable
}
}
[UnmanagedFunctionPointer(CallingConvention.StdCall,
CharSet = CharSet.Auto,
SetLastError = true)]
delegate IntPtr DGetHostByName(
String name);
// just use a P-Invoke implementation to get native API access
// from C# (this step is not necessary for C++.NET)
[DllImport("ws2_32.dll",
CharSet = CharSet.Auto,
SetLastError = true,
CallingConvention = CallingConvention.StdCall)]
static extern IntPtr gethostbyname(
String name);
// this is where we are intercepting all file accesses!
static IntPtr GetHostByName_Hooked(
String name)
{
try
{
Main This = (Main)HookRuntimeInfo.Callback;
MessageBox.Show("hi!");
}
catch
{
}
// call original API...
return GetHostByName(
name);
}
}
}
(ここでタイプミスを書いたかもしれませんが、プロジェクトは @ home @ homeをcouncefully compiledしました)。
問題は、私がこの方法を引っ掛けるために必要なこと<->アプリケーション自体を知らないということです。
つまり、c#easyhookでフックをするだけのことは何ですか(アプリが「foo.exe」であると仮定します)? EasyHookのためにカスタムDLLを作成する必要がありますか?(その場合、どのコンテンツを内側に定義する必要がありますか?)
私はそれを少し見つけました... helloworldフックのための「複雑」、彼。
前もって感謝します ;)
解決
最終的に、「Gray Hack Python」の本を使用して、これをすべてより少ない行でこれと同じように作成する方法と同じように、私が望んでいたものと同じです。
まだexeはありませんが...それだけです。
pydbg +フックを使用します。