As Mr. Harris says, you can use WinDivert to do what you want. For instance. to just do a TCP handshake, you can write something like this:
// TCP handshake using WinDivert: HANDLE handle = DivertOpen("inbound && tcp.SrcPort == 80 && tcp.Syn && tcp.Ack", 0, 0, 0); DivertSend(handle, synPacket, sizeof(synPacket), dstAddr, NULL); ... DivertRecv(handle, synAckPacket, sizeof(synAckPacket), &srcAddr, &length); ... DivertSend(handle, ackPacket, sizeof(ackPacket), dstAddr, NULL); ...
The DivertRecv () function redirects the server response to user space before it is processed by the Windows TCP / IP stack. Thus, TCP RST does not bother. DivertSend () injects packets.
These are the main differences between WinDivert and WinPCAP. The latter is just a packet sniffer, while the former can intercept / filter / block traffic.
WinDivert is written in C, so you need to write your own .NET wrapper.
(regular disclosure: WinDivert is my project).
source share