Advertisement
Guest User

Untitled

a guest
Aug 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. public class Main : IEntryPoint
  2.     {
  3.         private IEnumerable<IPAddress> _whitelist;
  4.         private List<IntPtr> _realWhiteList;
  5.         private HookInterface _interface;
  6.         private ushort _redirectionPort;
  7.  
  8.         private LocalHook _connectHook;
  9.         private LocalHook _receiveHook;
  10.         private LocalHook _sendHook;
  11.  
  12.         public Main(IContext context, string channelName, List<string> ipsWhitelist, int redirectionPort)
  13.         {
  14.             _interface = IpcConnectClient<HookInterface>(channelName);
  15.             _whitelist = ipsWhitelist.Select(ip => IPAddress.Parse(ip));
  16.             _realWhiteList = new List<IntPtr>();
  17.             _redirectionPort = (ushort)redirectionPort;
  18.  
  19.             _interface.Ping();
  20.         }
  21.  
  22.         public void Run(IContext context, string channelName, List<string> ipsWhitelist, int redirectionPort)
  23.         {
  24.             _interface.NotifyInstalled(Process.GetCurrentProcess().ProcessName);
  25.            
  26.             try
  27.             {
  28.                 _connectHook = LocalHook.Create(
  29.                     LocalHook.GetProcAddress("Ws2_32.dll", "connect"),
  30.                     new WinsockConnectDelegate(_onConnect), this);
  31.  
  32.                 _connectHook.ThreadACL.SetExclusiveACL(new[] { 0 });
  33.  
  34.                 _receiveHook = LocalHook.Create(
  35.                     LocalHook.GetProcAddress("Ws2_32.dll", "recv"),
  36.                     new WinsockRecvDelegate(_onReceive), this);
  37.  
  38.                 _sendHook = LocalHook.Create(
  39.                     LocalHook.GetProcAddress("Ws2_32.dll", "send"),
  40.                     new WinsockSendDelegate(_onSent), this);
  41.  
  42.                 _receiveHook.ThreadACL.SetExclusiveACL(new[] { 0 });
  43.                 _sendHook.ThreadACL.SetExclusiveACL(new[] { 0 });
  44.             }
  45.             catch (Exception ex) { _interface.OnError(ex); }
  46.  
  47.             WakeUpProcess();
  48.             while (true) Thread.Sleep(1000);
  49.         }
  50.  
  51.         private int _onReceive(IntPtr s, IntPtr buf, int len, int flags)
  52.         {
  53.             var read = recv(s, buf, len, flags);
  54.             if (_realWhiteList.Contains(s)) _interface.Message($"{read}bytes received from server.");
  55.             return read;
  56.         }
  57.  
  58.         private int _onSent(IntPtr s, IntPtr buf, int len, int flags)
  59.         {
  60.             var sent = send(s, buf, len, flags);
  61.             if(_realWhiteList.Contains(s)) _interface.Message($"{sent}bytes sent to server.");
  62.             return sent;
  63.         }
  64.  
  65.         private int _onConnect(IntPtr socket, IntPtr address, int addrSize)
  66.         {
  67.             var structure = Marshal.PtrToStructure<sockaddr_in>(address);
  68.             var ipAddress = new IPAddress(structure.sin_addr.S_addr);
  69.             var port = structure.sin_port;
  70.  
  71.             if (!_whitelist.Contains(ipAddress)) return connect(socket, address, addrSize);
  72.             else {
  73.                 _realWhiteList.Add(socket);
  74.                 return connect(socket, address, addrSize);
  75.             }
  76.         }
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement