DivertPInvoke
DivertPInvoke copied to clipboard
WinDivertHelperCalcChecksums Not modify of packet check bits and length bits, thx for help
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);
}
}
}