Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinDivertHelperCalcChecksums Not modify of packet check bits and length bits, thx for help #5

Open
boyyao opened this issue Feb 24, 2018 · 0 comments

Comments

@boyyao
Copy link

boyyao commented Feb 24, 2018

code:

    public static string Filter = "outbound && tcp && tcp.DstPort == 8888 && tcp.PayloadLength > 120";
    public unsafe static void RunDiversion()
    {
        IntPtr handle = WinDivertMethods.WinDivertOpen(Filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, -1, 0);
        IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
        if (handle == INVALID_HANDLE_VALUE)
            Console.WriteLine("open error:" + Marshal.GetLastWin32Error());

        const uint MAXBUF = 0xFFFF;
        byte[] buffer = new byte[MAXBUF];

        while (true)
        {
            Array.Clear(buffer, 0, buffer.Length);
            WINDIVERT_ADDRESS windivert_ADDRESS = default(WINDIVERT_ADDRESS);
            uint packetLen = 0u;

            if (WinDivertMethods.WinDivertRecv(handle, buffer, MAXBUF, ref windivert_ADDRESS, ref packetLen))
            {
                fixed (byte* buffer_fixed = buffer)
                {
                    WINDIVERT_IPHDR* IPHDR = null;
                    WINDIVERT_TCPHDR* TCPHDR = null;
                    byte* ppData = null;
                    WinDivertMethods.WinDivertHelperParsePacket(buffer_fixed, packetLen, &IPHDR, null, null, null, &TCPHDR, null, &ppData, null);
                    if (IPHDR != null && TCPHDR != null && ppData != null)
                    {
                        string data = Marshal.PtrToStringAnsi((IntPtr)((void*)ppData));

                        if (data.Contains("need modify data"))
                        {
                            Console.WriteLine("old data:");
                            for (int i = 0; i < packetLen; i++)
                            {
                                Console.Write("{0:X2}", buffer[i]);
                            }
                            Console.WriteLine("");

                            byte[] newbody = System.Text.Encoding.ASCII.GetBytes("new data");

                            Buffer.BlockCopy(newbody, 0, buffer, 40, newbody.Length);
                            packetLen = (uint)newbody.Length + 40;

                            Console.WriteLine("new data:");
                            for (int i = 0; i < packetLen; i++)
                            {
                                Console.Write("{0:X2}", buffer[i]);
                            }
                            Console.WriteLine("");
                        }
                    }
                }
                ....invalid....... buffer not modify   need help.
                WinDivertMethods.WinDivertHelperCalcChecksums(buffer, packetLen, 0UL);
                Console.WriteLine("send data:");
                for (int i = 0; i < packetLen; i++)
                {
                    Console.Write("{0:X2}", buffer[i]);
                }
                Console.WriteLine("");
                WinDivertMethods.WinDivertSendEx(handle, buffer, packetLen, 0UL, ref windivert_ADDRESS, IntPtr.Zero, IntPtr.Zero);
            }
        }

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant