Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main : IEntryPoint
- {
- private IEnumerable<IPAddress> _whitelist;
- private List<IntPtr> _realWhiteList;
- private HookInterface _interface;
- private ushort _redirectionPort;
- private LocalHook _connectHook;
- private LocalHook _receiveHook;
- private LocalHook _sendHook;
- public Main(IContext context, string channelName, List<string> ipsWhitelist, int redirectionPort)
- {
- _interface = IpcConnectClient<HookInterface>(channelName);
- _whitelist = ipsWhitelist.Select(ip => IPAddress.Parse(ip));
- _realWhiteList = new List<IntPtr>();
- _redirectionPort = (ushort)redirectionPort;
- _interface.Ping();
- }
- public void Run(IContext context, string channelName, List<string> ipsWhitelist, int redirectionPort)
- {
- _interface.NotifyInstalled(Process.GetCurrentProcess().ProcessName);
- try
- {
- _connectHook = LocalHook.Create(
- LocalHook.GetProcAddress("Ws2_32.dll", "connect"),
- new WinsockConnectDelegate(_onConnect), this);
- _connectHook.ThreadACL.SetExclusiveACL(new[] { 0 });
- _receiveHook = LocalHook.Create(
- LocalHook.GetProcAddress("Ws2_32.dll", "recv"),
- new WinsockRecvDelegate(_onReceive), this);
- _sendHook = LocalHook.Create(
- LocalHook.GetProcAddress("Ws2_32.dll", "send"),
- new WinsockSendDelegate(_onSent), this);
- _receiveHook.ThreadACL.SetExclusiveACL(new[] { 0 });
- _sendHook.ThreadACL.SetExclusiveACL(new[] { 0 });
- }
- catch (Exception ex) { _interface.OnError(ex); }
- WakeUpProcess();
- while (true) Thread.Sleep(1000);
- }
- private int _onReceive(IntPtr s, IntPtr buf, int len, int flags)
- {
- var read = recv(s, buf, len, flags);
- if (_realWhiteList.Contains(s)) _interface.Message($"{read}bytes received from server.");
- return read;
- }
- private int _onSent(IntPtr s, IntPtr buf, int len, int flags)
- {
- var sent = send(s, buf, len, flags);
- if(_realWhiteList.Contains(s)) _interface.Message($"{sent}bytes sent to server.");
- return sent;
- }
- private int _onConnect(IntPtr socket, IntPtr address, int addrSize)
- {
- var structure = Marshal.PtrToStructure<sockaddr_in>(address);
- var ipAddress = new IPAddress(structure.sin_addr.S_addr);
- var port = structure.sin_port;
- if (!_whitelist.Contains(ipAddress)) return connect(socket, address, addrSize);
- else {
- _realWhiteList.Add(socket);
- return connect(socket, address, addrSize);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement