RecvHook.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using EasyHook;
  4. using Log;
  5. namespace Hooks
  6. {
  7. [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode,
  8. SetLastError = true)]
  9. public delegate int DRecv(IntPtr handle, IntPtr buf, int count, int flag);
  10. public class RecvHook: IDisposable
  11. {
  12. [DllImport("Ws2_32.dll", EntryPoint = "recv")]
  13. public static extern int Recv(IntPtr handle, IntPtr buf, int count, int flag);
  14. private readonly LocalHook localHook;
  15. public RecvHook(DRecv dRecv)
  16. {
  17. try
  18. {
  19. this.localHook = LocalHook.Create(
  20. LocalHook.GetProcAddress("Ws2_32.dll", "recv"), new DRecv(dRecv), this);
  21. this.localHook.ThreadACL.SetInclusiveACL(new[] { 0 });
  22. }
  23. catch (Exception)
  24. {
  25. Logger.Debug("Error creating recv Hook");
  26. throw;
  27. }
  28. }
  29. public void Dispose()
  30. {
  31. this.localHook.Dispose();
  32. }
  33. //static int RecvHooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
  34. //{
  35. // int bytesCount = recv(socketHandle, buf, count, socketFlags);
  36. // if (bytesCount > 0)
  37. // {
  38. // var newBuffer = new byte[bytesCount];
  39. // Marshal.Copy(buf, newBuffer, 0, bytesCount);
  40. // string s = Encoding.ASCII.GetString(newBuffer);
  41. // TextWriter tw = new StreamWriter("recv.txt");
  42. // tw.Write(s);
  43. // tw.Close();
  44. // Logger.Debug(string.Format("Hooked:>{0}", s));
  45. // }
  46. // return bytesCount;
  47. //}
  48. }
  49. }