Domanda

Ho ricevuto una (vecchia) applicazione che chiama la funzione WinSocket:

struct hostent* FAR gethostbyname(
  __in  const char *name
);

Attualmente lo importa come WS32_DLL.#52 invece il nome normale che chiama.

La mia intenzione è solo quella di poter fare qualcosa come l'apertura di una cassetta quando si verifica una ricerca host (che dovrebbe essere all'inizio dell'app).

Ho provato a creare una DLL C ++ con i commenti di Pragma che puntano al numero 52 e mettendola sull'app Dir (incluso un file "ex.local" ed "ex.manifest" per provare a reindirizzarlo) ma ha caricato il C: Windows System32 invece.

Successivamente, ho creato il progetto AC# che ha lanciato il processo stesso (quindi ottenendo il PID dall'oggetto processo), aggiungendo la DLL Easyhook ad esso.

Ho controllato l'esempio a: http://www.codeproject.com/kb/dll/easyhook64.aspx

Cambiare le chiamate a:

    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);
    }
}

}

(Potrebbe aver realizzato errori di battitura scrivendolo qui, ma il progetto ha compilato con successo @ home).

Il fatto è che non so cosa ho bisogno di fare il collegamento di questi metodi <--> l'applicazione stessa.

Voglio dire .. cosa è rimasto solo per fare il aggancio con c# easyhook (supponendo che l'app sia "foo.exe")? Devo creare una DLL personalizzata per Easyhook? (In quel caso, di quale contenuto devo definire all'interno?)

L'ho trovato un po '... "complesso" per un gancio Helloworld, hehe.

Grazie in anticipo ;)

È stato utile?

Soluzione

Alla fine, usando il libro "Grey Hack Python", Taugh Me Come fare tutto questo con meno linee, e lo stesso che volevo.

Nessun exe ma ... tutto qui.

Usando i ganci PYDBG +.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top