RecvHook.cs 1.3 KB

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