public void Intercept()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

            socket.Bind(new IPEndPoint(IPAddress.Parse(localIp), 0));
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
            socket.IOControl(IOControlCode.ReceiveAll, new byte[4] {
                1, 0, 0, 0
            }, new byte[4] {
                1, 0, 0, 0
            });

            Action <IAsyncResult> OnReceive = null;

            byte[] buffer = new byte[4096];

            OnReceive = (result) =>
            {
                if (buffer[9] == 6) // TCP packets
                {
                    IPHeader     ipHeader  = new IPHeader(buffer, socket.EndReceive(result));
                    TCPHeader    tcpHeader = new TCPHeader(ipHeader.Data, ipHeader.MessageLength);
                    PacketHeader packet    = new PacketHeader(ipHeader, tcpHeader);
                    if (tcpHeader.Flags.ToString().Contains("0x12"))
                    {
                        Console.WriteLine(packet.ToString());
                    }
                }

                buffer = new byte[4096];
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
            };

            socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
        }
Exemple #2
0
 public PacketHeader(IPHeader ipHeader, TCPHeader tcpHeader)
 {
     mIpHeader  = ipHeader;
     mTcpHeader = tcpHeader;
 }