2015-08-02 3 views
3

Я пытался перехватить звонки на «recv» из Chrome и Firefox с помощью EasyHook. Однако это не работает - оно не сбой с ошибками, но также и пакеты не попадают. Я попробовал пример программы с помощью «CreateFile» перехватчиков, и это отлично работает ... Поскольку рядом с этим нет документации, у меня возникли проблемы с исправлением этого. Вот мой код:EasyHook не перехватывает какие-либо вызовы recv

// the injected library 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using EasyHook; 
using System.Runtime.InteropServices; 
using System.Threading; 
using System.Windows.Forms; 

namespace SocketMon 
{ 
    public class Injection : EasyHook.IEntryPoint 
    { 
     SocketMonInterface Interface; 
     LocalHook CreateFileHook; 
     Stack<String> Queue = new Stack<String>(); 

     public Injection(RemoteHooking.IContext InContext, String InChannelName) 
     { 
      // connect to host... 
      Interface = 
      RemoteHooking.IpcConnectClient<SocketMonInterface>(InChannelName); 

      // validate connection... 
      Interface.Ping(); 
     } 


     public void Run(RemoteHooking.IContext InContext, String InChannelName) 
     { 
      // install hook... 
      try 
      { 
       CreateFileHook = LocalHook.Create(
        LocalHook.GetProcAddress("Ws2_32.dll", "recv"), 
        new Drecv(recv_Hooked), 
        this); 

       CreateFileHook.ThreadACL.SetInclusiveACL(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); 

        if (Queue.Count > 0) 
        { 
         String[] Package = null; 


         MessageBox.Show(Queue.Count.ToString()); 
         lock (Queue) 
         { 
          Package = Queue.ToArray(); 

          Queue.Clear(); 
         } 


         Interface.OnRecvData(RemoteHooking.GetCurrentProcessId(), Package); 
        } 
        else 
         Interface.Ping(); 
       } 
      } 
      catch 
      { 
       // NET Remoting will raise an exception if host is unreachable 
      } 
     } 

     [UnmanagedFunctionPointer(CallingConvention.StdCall, 
      CharSet = CharSet.Unicode, 
      SetLastError = true)] 


     delegate int Drecv(
        IntPtr socketHandle, 
        IntPtr buf, 
        int count, 
        int socketFlags 
      ); 

     // 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")] 
     static extern int recv(
        IntPtr socketHandle, 
        IntPtr buf, 
        int count, 
        int socketFlags 
      ); 


     public int recv_Hooked(
        IntPtr socketHandle, 
        IntPtr buf, 
        int count, 
        int socketFlags 
      ) 
     { 
      int len = recv(socketHandle, buf, count, socketFlags); 
      Queue.Push(String.Format("Received {0} bytes of data on socket {1}", socketHandle, count)); 
      return len; 
     } 
    } 
} 

*

//the ipc interface 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace SocketMon 
{ 
    public class SocketMonInterface : MarshalByRefObject 
    { 
     public void IsInstalled(Int32 InClientPID) 
     { 
      Console.WriteLine("SocketMon has been installed in target {0}.\r\n", InClientPID); 
     } 

     public void OnRecvData(Int32 InClientPID, String[] InSocketData) 
     { 
      for (int i = 0; i < InSocketData.Length; i++) 
      { 

       Console.WriteLine(InSocketData[i]); 
      } 
     } 

     public void ReportException(Exception InInfo) 
     { 
      Console.WriteLine("The target process has reported" + 
           " an error:\r\n" + InInfo.ToString()); 
     } 

     public void Ping() 
     { 
      Console.WriteLine("Got pinged"); 
     } 
    } 
} 

Я уже попытался изменить SetExclusiveACL к SetInclusiveACL, и это не помогло ...

+0

Что происходит, когда вы устанавливаете точку останова на своем крюке метод и приложить к процессу? Что-то перехватило? –

+0

Также вы можете показать свой основной метод/класс? –

ответ

3

я понял, что мой код был на самом деле работает. ..

вручную вызова 'RECV' с помощью P/Invoke работал ...

Th Проблема в том, что Chrome и Firefox не использовали «recv» - когда я использовал SpyStudio для их подключения, они фактически вызывали другие методы в «wininet.dll», не используя winsocks.

Смежные вопросы