public static IPv4Packet CreateIpV4Packet(IPAddress sourceIpAddress, IPAddress destinationIpAddress, TcpPacket payloadPacket) { var result = new IPv4Packet(sourceIpAddress, destinationIpAddress) { PayloadPacket = payloadPacket }; payloadPacket.UpdateTCPChecksum(); result.UpdateIPChecksum(); result.UpdateCalculatedValues(); return result; }
public void GenerateLLMNRResponse(Packet packet) { try { LLMNR.LLMNRPacket llmnr = new LLMNR.LLMNRPacket(); llmnr.ParsePacket(((UdpPacket)(packet.PayloadPacket.PayloadPacket)).PayloadData); if (llmnr.Query.Name.ToLower().Equals("wpad") && llmnr.Query.Type.Value == LLMNR.DNSType.A) { WinPcapDevice dev = Program.CurrentProject.data.GetDevice(); IPAddress ip = Program.CurrentProject.data.GetIPv4FromDevice(dev); byte[] ipv4Addr = ip.GetAddressBytes(); llmnr.AnswerList.Add(new LLMNR.DNSAnswer() { Class = evilfoca.LLMNR.DNSClass.IN, Name = llmnr.Query.Name, Type = evilfoca.LLMNR.DNSType.A, RData = ipv4Addr, RDLength = (short)ipv4Addr.Length, TTL = 12 }); llmnr.IsResponse = true; EthernetPacket ethDns = new EthernetPacket(dev.MacAddress, ((EthernetPacket)packet).SourceHwAddress, EthernetPacketType.IpV4); IPv4Packet ipv4Dns = new IPv4Packet(ip, ((IPv4Packet)((EthernetPacket)packet).PayloadPacket).SourceAddress); UdpPacket udpDns = new UdpPacket(((UdpPacket)(packet.PayloadPacket.PayloadPacket)).DestinationPort, ((UdpPacket)(packet.PayloadPacket.PayloadPacket)).SourcePort); udpDns.PayloadData = llmnr.BuildPacket(); ipv4Dns.PayloadPacket = udpDns; udpDns.UpdateCalculatedValues(); udpDns.UpdateUDPChecksum(); ipv4Dns.UpdateIPChecksum(); ipv4Dns.UpdateCalculatedValues(); ethDns.PayloadPacket = ipv4Dns; Program.CurrentProject.data.SendPacket(ethDns); } } catch (Exception) { } }
public int sendPacket() { byte[] bytes = new byte[4096]; ByteArraySegment bas = new ByteArraySegment(bytes); ushort srcPort = 23444; ushort dstPort = 12345; PacketDotNet.UdpPacket udpPacket = new UdpPacket(srcPort, dstPort); string cmdString = "xxxxyyyyHello world!"; byte[] sendBuffer = Encoding.ASCII.GetBytes(cmdString); udpPacket.PayloadData = sendBuffer; ICMPv4Packet icmpPacket = new ICMPv4Packet(new ByteArraySegment(sendBuffer)); // sanity check: //bas.BytesLength = 10; //Console.WriteLine("bas - Offset = " + bas.Offset + " len= " + bas.Length); IPAddress ipSrcAddr = System.Net.IPAddress.Parse("192.168.0.186"); // laptop IPAddress ipDestAddr = System.Net.IPAddress.Parse("192.168.0.185"); // my linux box IpPacket ipPacket = new IPv4Packet(ipSrcAddr, ipDestAddr); ipPacket.PayloadPacket = udpPacket; icmpPacket.TypeCode = ICMPv4TypeCodes.Unassigned1; icmpPacket.Sequence = 1; Console.WriteLine("icmpPacket - TypeCode = " + icmpPacket.TypeCode + " Sequence= " + icmpPacket.Sequence); Console.WriteLine("icmpPacket : " + icmpPacket.PrintHex()); icmpPacket.UpdateCalculatedValues(); Console.WriteLine("icmpPacket : " + icmpPacket.PrintHex()); //ushort etype = 0xFF00; //EthernetPacketType RAW ? System.Net.NetworkInformation.PhysicalAddress ethSrcAddr = System.Net.NetworkInformation.PhysicalAddress.Parse("02-1E-EC-8F-7F-E1"); System.Net.NetworkInformation.PhysicalAddress ethDstAddr = System.Net.NetworkInformation.PhysicalAddress.Parse("48-5B-39-ED-96-36"); EthernetPacket ethernetPacket = new EthernetPacket(ethSrcAddr, ethDstAddr, EthernetPacketType.IpV4); // I thought "None" for type would fill in type automatically; but it remained zero on the wire and was flagged "Malformed" ethernetPacket.PayloadPacket = icmpPacket; Console.WriteLine("ethernetPacket : " + ethernetPacket.PrintHex()); ethernetPacket.Type = EthernetPacketType.IpV4; Console.WriteLine("ethernetPacket : " + ethernetPacket.PrintHex()); //ipPacket.PayloadPacket = udpPacket; //ethernetPacket.PayloadPacket = gmpPacket; //ipPacket; ethernetPacket.UpdateCalculatedValues(); Console.WriteLine(ethernetPacket.ToString()); ipPacket.UpdateCalculatedValues(); //udpPacket.UpdateUDPChecksum(); // Experiment with raw ip packet? ipPacket.Protocol = IPProtocolType.RAW; // Why isn't ValidChecksum true? //Console.WriteLine("After updating calculated values, ValidUDPChecksum = " + udpPacket.ValidUDPChecksum + " ValidChecksum = " + udpPacket.ValidChecksum); //Console.WriteLine(ethernetPacket.ToString()); device.Open(DeviceMode.Normal, 15000); // 15 sec timeout //ushort checksum = csum(ipPacket.BytesHighPerformance, ipPacket.P); device.SendPacket(ethernetPacket); return 0; }
public void sendDanMsg(DAN_gui_msg message) { byte[] bytes = message.marshal(); // IP addressing is not needed; just keeping entertained... IpPacket ipPacket = new IPv4Packet(ipSrcAddr, ipDestAddr); ipPacket.PayloadData = bytes; ipPacket.UpdateCalculatedValues(); ipPacket.Version = (PacketDotNet.IpVersion)DAN_GUI_MESSAGE_IP_V; EthernetPacket ethernetPacket = new EthernetPacket(device.MacAddress, ethDstAddr, EthernetPacketType.IpV4); ethernetPacket.PayloadPacket = ipPacket; device.SendPacket(ethernetPacket); }