private static void BlockPacket(ILiveDevice device, WinpkFilterHeader header, Packet packet) { var ip = packet.Extract <IPv4Packet>(); var tcp = packet.Extract <TcpPacket>(); if (WebHelper.Address.Equals(ip?.DestinationAddress) && tcp?.DestinationPort == 80) { // No packet shall pass return; } AllowPacket(device, header, packet); }
private static void DirtyPacket(ILiveDevice device, WinpkFilterHeader header, Packet packet) { var ip = packet.Extract <IPv4Packet>(); var tcp = packet.Extract <TcpPacket>(); if (WebHelper.Address.Equals(ip?.DestinationAddress) && tcp?.DestinationPort == 80) { // Let's mess with the request a bit var data = Encoding.UTF8.GetString(tcp.PayloadData); data = data.Replace("HTTP", "HTML"); tcp.PayloadData = Encoding.UTF8.GetBytes(data); ip.UpdateIPChecksum(); tcp.UpdateTcpChecksum(); } AllowPacket(device, header, packet); }
internal static PhysicalAddress Resolve( ILiveDevice device, System.Net.IPAddress destIP, System.Net.IPAddress localIP, PhysicalAddress localMAC, TimeSpan timeout) { //Build a new ARP request packet var request = BuildRequest(destIP, localMAC, localIP); //create a "tcpdump" filter for allowing only arp replies to be read String arpFilter = "arp and ether dst " + localMAC.ToString(); //set the filter device.Filter = arpFilter; // set a last request time that will trigger sending the // arp request immediately var lastRequestTime = DateTime.FromBinary(0); var requestInterval = new TimeSpan(0, 0, 1); PacketDotNet.ArpPacket arpPacket = null; // attempt to resolve the address with the current timeout var timeoutDateTime = DateTime.Now + timeout; while (DateTime.Now < timeoutDateTime) { if (requestInterval < (DateTime.Now - lastRequestTime)) { // inject the packet to the wire device.SendPacket(request); lastRequestTime = DateTime.Now; } //read the next packet from the network var retval = device.GetNextPacket(out PacketCapture e); if (retval != GetPacketStatus.PacketRead) { continue; } var reply = e.GetPacket(); // parse the packet var packet = PacketDotNet.Packet.ParsePacket(reply.LinkLayerType, reply.Data); // is this an arp packet? arpPacket = packet.Extract <PacketDotNet.ArpPacket>(); if (arpPacket == null) { continue; } //if this is the reply we're looking for, stop if (arpPacket.SenderProtocolAddress.Equals(destIP)) { break; } } // the timeout happened if (DateTime.Now >= timeoutDateTime) { return(null); } else { //return the resolved MAC address return(arpPacket.SenderHardwareAddress); } }
private static void AllowPacket(ILiveDevice device, WinpkFilterHeader header, Packet packet) { device.SendPacket(packet.Bytes, header); }