private void SendSyn(PacketCommunicator communicator) { // Ethernet Layer EthernetLayer ethernetLayer = new EthernetLayer { Source = SourceMac, Destination = DestinationMac, }; // IPv4 Layer IpV4Layer ipV4Layer = new IpV4Layer { Source = SourceIpV4, CurrentDestination = DestinationIpV4, Ttl = 128, Fragmentation = new IpV4Fragmentation(IpV4FragmentationOptions.DoNotFragment, 0), Identification = 1234, }; // TCP Layer TcpLayer tcpLayer = new TcpLayer { SourcePort = _sourcePort, DestinationPort = _destinationPort, SequenceNumber = _seqNumber, ControlBits = TcpControlBits.Synchronize, Window = _windowSize, }; communicator.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer)); _expectedAckNumber = _seqNumber + 1; }
private static void preparePcap() { var source = new MacAddress(getClientMAC()); var dest = new MacAddress(getRouterMAC()); device = LivePacketDevice.AllLocalMachine[0]; communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000); EthernetLayer ethLayer = new EthernetLayer { Source = source, Destination = dest }; ipLayer = new IpV4Layer { Source = new IpV4Address(LocalIPAddress()), Ttl = 128 }; icmpLayer = new IcmpEchoLayer(); icmpBuilder = new PacketBuilder(ethLayer, ipLayer, icmpLayer); TCPLayer = new TcpLayer(); TCPBuilder = new PacketBuilder(ethLayer, ipLayer, TCPLayer); if (!TCPMode) { communicator.SetFilter("icmp[0] = 0"); } else { //tcp[13] = 18 communicator.SetFilter("tcp[13] = 18 and port " + TCPPort); } }
public void RandomUdpTest() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("00:01:02:03:04:05"), Destination = new MacAddress("A0:A1:A2:A3:A4:A5") }; int seed = new Random().Next(); Console.WriteLine("Seed: " + seed); Random random = new Random(seed); for (int i = 0; i != 1000; ++i) { IpV4Layer ipV4Layer = random.NextIpV4Layer(null); ipV4Layer.HeaderChecksum = null; IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Udp, false); EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6; Layer ipLayer = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer); UdpLayer udpLayer = random.NextUdpLayer(); udpLayer.Checksum = null; PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000)); Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, payloadLayer); Assert.IsTrue(packet.IsValid, "IsValid"); // Ethernet ethernetLayer.EtherType = ethernetType; Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer"); ethernetLayer.EtherType = EthernetType.None; // Ip if (ipLayer == ipV4Layer) { // IpV4. ipV4Layer.Protocol = IpV4Protocol.Udp; ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum; Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer"); ipV4Layer.HeaderChecksum = null; } else { // IpV6. Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer"); } // UDP udpLayer.Checksum = packet.Ethernet.Ip.Udp.Checksum; Assert.AreEqual(udpLayer, packet.Ethernet.Ip.Udp.ExtractLayer(), "UDP Layer"); Assert.AreEqual(UdpDatagram.HeaderLength + payloadLayer.Length, packet.Ethernet.Ip.Udp.TotalLength, "Total Length"); Assert.IsTrue(!udpLayer.CalculateChecksum && packet.Ethernet.Ip.Udp.Checksum == 0 || udpLayer.CalculateChecksum && packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect"); Assert.IsTrue(packet.Ethernet.Ip.Udp.IsChecksumOptional, "IsChecksumOptional"); Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Udp.Payload, "Payload"); } }
private void btnAdd_Click(object sender, EventArgs e) { try { ushort?checksum = null; if (networkCheckSum.Enabled == true) { checksum = ushort.Parse(networkCheckSum.Text); } IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(netSourceIP.Text), CurrentDestination = new IpV4Address(netDestIP.Text), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = checksum, // Will be filled automatically. Identification = ushort.Parse(identification.Text), Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = byte.Parse(networkTTL.Text), TypeOfService = byte.Parse(networkTOS.Text), }; if (cmbIPV4Protocols.Enabled) { ipV4Layer.Protocol = (IpV4Protocol)Enum.Parse(typeof(IpV4Protocol), cmbIPV4Protocols.SelectedItem.ToString()); } mainForm.addToPacket(ipV4Layer, "IPv4 -> " + netSourceIP.Text + " -> " + netDestIP.Text); } catch (Exception) { MessageBox.Show("Invalid Parameters"); } }
/// <summary> /// This function build an IPv4 over Ethernet with payload packet. /// </summary> public static Packet BuildIpV4Packet() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(macAddressSource), Destination = new MacAddress(macAddressDest), EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(ipSource), CurrentDestination = new IpV4Address(ipDest), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = IpV4Protocol.Udp, Ttl = 100, TypeOfService = 0, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer); return(builder.Build(DateTime.Now)); }
/// <summary> /// This function build an ICMP over IPv4 over Ethernet packet. /// </summary> private static Packet BuildIcmpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IcmpEchoLayer icmpLayer = new IcmpEchoLayer { Checksum = null, // Will be filled automatically. Identifier = 456, SequenceNumber = 800, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); return(builder.Build(DateTime.Now)); }
/// <summary> /// This function build an IGMP over IPv4 over Ethernet packet. /// </summary> public static Packet BuildIgmpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(macAddressSource), Destination = new MacAddress(macAddressDest), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(ipSource), CurrentDestination = new IpV4Address(ipDest), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IgmpQueryVersion1Layer igmpLayer = new IgmpQueryVersion1Layer { GroupAddress = new IpV4Address(ipSource), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, igmpLayer); return(builder.Build(DateTime.Now)); }
public void SendLayer3Packet(OSI.Layer3Packet packet, PacketSentHandler callback) { IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(packet.SourceIP.AsString), Ttl = packet.Ttl, // The rest of the important parameters will be set for each packet }; Common.IPv4Address ip = null; try { System.Net.IPAddress.Parse(packet.Destination); ip = new Common.IPv4Address(packet.Destination); } catch { ip = Dns.ResolveHost(packet.Destination).IPs[0]; } ipV4Layer.CurrentDestination = new IpV4Address(ip.AsString); OSI.Layer2Packet l2 = new OSI.Layer2Packet(); l2.SourceMac = Configuration.MacAddress; l2.DestinationMac = Arp.ResolveIP(ip); foreach (ILayer layer in packet.NextLayers) { l2.NextLayers.Add(layer); } l2.NextLayers.Insert(0, ipV4Layer); SendLayer2Packet(l2, callback); }
public static Packet CreateRstPacketFor(ScanningOptions options, ushort targetPort) { TcpPacketFactory.RandomizeParameters(); EthernetLayer ethernetLayer = new EthernetLayer { Source = options.SourceMac, Destination = options.TargetMac, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = options.SourceIP, CurrentDestination = options.TargetIP, Ttl = TcpPacketFactory.ttl, Fragmentation = TcpPacketFactory.fragmentation, Identification = TcpPacketFactory.identification }; TcpLayer tcpLayer = new TcpLayer { SourcePort = sourcePort, DestinationPort = targetPort, SequenceNumber = TcpPacketFactory.sequence, ControlBits = TcpControlBits.Reset, Window = TcpPacketFactory.windowSize, }; return(PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, tcpLayer)); }
public override ILayer GetPacket() { var fragmentationOptions = IpV4FragmentationOptions.None; if (this.DontFragment) { fragmentationOptions |= IpV4FragmentationOptions.DoNotFragment; } if (this.MoreFragments) { fragmentationOptions |= IpV4FragmentationOptions.MoreFragments; } var ipv4Layer = new IpV4Layer() { Source = new IpV4Address(this.SourceIP), CurrentDestination = new IpV4Address(this.DestinationIP), TypeOfService = (byte)this.DiffServ, Identification = this.Id, Ttl = (byte)this.TimeToLive, Protocol = (IpV4Protocol)Enum.Parse(typeof(IpV4Protocol), this.SelectedProtocol), Fragmentation = new IpV4Fragmentation(fragmentationOptions, this.FragmentOffset) }; return(ipv4Layer); }
private static Packet BuildIcmpPacket(MacAddress SourceMac, MacAddress DestinationMac, IpV4Address SourceIp, IpV4Address CurrentDestination, string packetType) { EthernetLayer ethernetLayer = new EthernetLayer { Source = SourceMac, Destination = DestinationMac, EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { //Source = new IpV4Address("1.2.3.4"), //CurrentDestination = new IpV4Address("11.22.33.44"), Source = SourceIp, CurrentDestination = CurrentDestination, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IcmpEchoLayer icmpLayer = null; IcmpEchoReplyLayer icmpRLayer = null; PacketBuilder builder = null; if (packetType.Equals("REQUEST")) { Console.WriteLine("Request"); icmpLayer = new IcmpEchoLayer { Checksum = null, // Will be filled automatically. Identifier = 456, SequenceNumber = 800, }; builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); } else { Console.WriteLine("Reply"); icmpRLayer = new IcmpEchoReplyLayer { Checksum = null, // Will be filled automatically. Identifier = 456, SequenceNumber = 800, }; builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpRLayer); } return(builder.Build(DateTime.Now)); }
/// <summary> /// This function build an IPv4 over GRE over IPv4 over Ethernet packet. /// </summary> public static Packet BuildGrePacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(macAddressSource), Destination = new MacAddress(macAddressDest), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(ipSource), CurrentDestination = new IpV4Address(ipDest), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; GreLayer greLayer = new GreLayer { Version = GreVersion.Gre, ProtocolType = EthernetType.None, // Will be filled automatically. RecursionControl = 0, FutureUseBits = 0, ChecksumPresent = true, Checksum = null, // Will be filled automatically. Key = null, SequenceNumber = 123, AcknowledgmentSequenceNumber = null, RoutingOffset = null, Routing = null, StrictSourceRoute = false, }; IpV4Layer innerIpV4Layer = new IpV4Layer { Source = new IpV4Address("100.200.201.202"), CurrentDestination = new IpV4Address("123.254.132.40"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = IpV4Protocol.Udp, Ttl = 120, TypeOfService = 0, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, greLayer, innerIpV4Layer); return(builder.Build(DateTime.Now)); }
private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List <ILayer> layers) { if (random.NextBool(20)) { // Finish with payload. PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100)); layers.Add(payloadLayer); return; } ipV4Layer.Protocol = null; if (random.NextBool()) { ipV4Layer.Fragmentation = IpV4Fragmentation.None; } switch (random.Next(0, 9)) { case 0: // IpV4. case 1: IpV4Layer innerIpV4Layer = random.NextIpV4Layer(); layers.Add(innerIpV4Layer); CreateRandomIpV4Payload(random, innerIpV4Layer, layers); return; case 2: // Igmp. layers.Add(random.NextIgmpLayer()); return; case 3: // Icmp. IcmpLayer icmpLayer = random.NextIcmpLayer(); layers.Add(icmpLayer); layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer)); return; case 4: // Gre. GreLayer greLayer = random.NextGreLayer(); layers.Add(greLayer); CreateRandomEthernetPayload(random, greLayer, layers); return; case 5: // Udp. case 6: UdpLayer udpLayer = random.NextUdpLayer(); layers.Add(udpLayer); CreateRandomUdpPayload(random, udpLayer, layers); return; case 7: // Tcp. case 8: TcpLayer tcpLayer = random.NextTcpLayer(); layers.Add(tcpLayer); CreateRandomTcpPayload(random, tcpLayer, layers); return; default: throw new InvalidOperationException("Invalid value."); } }
//DNS PACKET BUILDER public Packet BuildDnsPacket(Packet origPacket) { EthernetLayer ethernetLayer = new EthernetLayer { Source = origPacket.Ethernet.Source, Destination = lookUpMacAdress(Convert.ToString(origPacket.Ethernet.IpV4.Destination)), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = origPacket.Ethernet.IpV4.Source, CurrentDestination = origPacket.Ethernet.IpV4.Destination, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = origPacket.Ethernet.IpV4.Identification, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = origPacket.Ethernet.IpV4.Ttl, TypeOfService = origPacket.Ethernet.IpV4.TypeOfService, }; UdpLayer udpLayer = new UdpLayer { SourcePort = origPacket.Ethernet.IpV4.Udp.SourcePort, DestinationPort = origPacket.Ethernet.IpV4.Udp.DestinationPort, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true, }; DnsLayer dnsLayer = new DnsLayer { Id = origPacket.Ethernet.IpV4.Udp.Dns.Id, IsResponse = origPacket.Ethernet.IpV4.Udp.Dns.IsResponse, OpCode = origPacket.Ethernet.IpV4.Udp.Dns.OpCode, IsAuthoritativeAnswer = origPacket.Ethernet.IpV4.Udp.Dns.IsAuthoritativeAnswer, IsTruncated = origPacket.Ethernet.IpV4.Udp.Dns.IsTruncated, IsRecursionDesired = origPacket.Ethernet.IpV4.Udp.Dns.IsRecursionDesired, IsRecursionAvailable = origPacket.Ethernet.IpV4.Udp.Dns.IsRecursionAvailable, FutureUse = origPacket.Ethernet.IpV4.Udp.Dns.FutureUse, IsAuthenticData = origPacket.Ethernet.IpV4.Udp.Dns.IsAuthenticData, IsCheckingDisabled = origPacket.Ethernet.IpV4.Udp.Dns.IsCheckingDisabled, ResponseCode = origPacket.Ethernet.IpV4.Udp.Dns.ResponseCode, Queries = origPacket.Ethernet.IpV4.Udp.Dns.Queries, Answers = origPacket.Ethernet.IpV4.Udp.Dns.Answers, Authorities = origPacket.Ethernet.IpV4.Udp.Dns.Authorities, Additionals = origPacket.Ethernet.IpV4.Udp.Dns.Additionals, DomainNameCompressionMode = DnsDomainNameCompressionMode.All, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer); return(builder.Build(DateTime.Now)); }
private void buttonManuelTaklit_Click(object sender, EventArgs e)// Bir önceki metodun aynısı. Yalnızca Mac adresi elle atanıyor. TextBox'dan çekiliyor. { String taklitmacmanuel = textBox1.Text; IList <LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; PacketDevice selectedDevice = allDevices[2]; //Cihaz seçimi. Manuel olarak atanmıştır. using (PacketCommunicator communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { for (int j = 0; j < 10000; j++) { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(taklitmacmanuel), Destination = new MacAddress("ff:ff:ff:ff:ff:ff"), EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(IpAdresim()), CurrentDestination = new IpV4Address(IpParcala(0) + "." + IpParcala(1) + "." + IpParcala(2) + "." + "1"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 123, Options = IpV4Options.None, Protocol = null, Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = 4050, DestinationPort = 25, Checksum = null, CalculateChecksumValue = true, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("Merhaba Dunya")), }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer); Packet arppacket = builder.Build(DateTime.Now); communicator.SendPacket(arppacket); System.Threading.Thread.Sleep(1000); } } }
public void SendIcmpEchoReply(IpV4Address TargetIP, MacAddress TargetMac, IcmpDatagram icmpRequest) { EthernetLayer ethernetLayer = new EthernetLayer { Source = _adapter.MAC, Destination = TargetMac, EtherType = EthernetType.None, // Will be filled automatically. }; VLanTaggedFrameLayer vlanLayer = new VLanTaggedFrameLayer { PriorityCodePoint = ClassOfService.BestEffort, CanonicalFormatIndicator = false, VLanIdentifier = _adapter.VLAN, EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = _adapter.IP, CurrentDestination = TargetIP, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IcmpEchoReplyLayer icmpReplyLayer = new IcmpEchoReplyLayer { Identifier = (ushort)((icmpRequest.Variable >> 16) & 0xFFFF), SequenceNumber = (ushort)(icmpRequest.Variable & 0xFFFF), }; PayloadLayer payloadLayer = new PayloadLayer() { Data = icmpRequest.Payload }; if (_adapter.VLAN > 1) { VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, vlanLayer, ipV4Layer, icmpReplyLayer, payloadLayer)); } else { VirtualNetwork.Instance.SendPacket(PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, icmpReplyLayer, payloadLayer)); } VirtualNetwork.Instance.PostTraceMessage("ICMP Reply: " + TargetIP.ToString()); }
public void IpV4LayerNullChecksumAndProtocolGetHashCodeTest() { IpV4Layer layer = new IpV4Layer { HeaderChecksum = null, Protocol = null, }; Assert.IsNotNull(layer.GetHashCode()); }
private Packet createSynPack() { PacketBuilder builder; MacAddress macSource = new MacAddress(ethSourceAddr.Text); IpV4Address ipSource = new IpV4Address("127.0.0.1"); MacAddress macDest = new MacAddress(ethDestAddr.Text); IpV4Address ipDest = new IpV4Address("127.0.0.1"); //Ethernet EthernetLayer ethernetLayer = new EthernetLayer { Source = macDest, Destination = macDest, EtherType = EthernetType.None }; //Internet Protocol IpV4Layer ipv4Layer = new IpV4Layer { Source = ipSource, CurrentDestination = ipDest, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 1, Options = IpV4Options.None, Protocol = null, Ttl = 100, TypeOfService = 0 }; //Transport TcpLayer tcpLayer = new TcpLayer { SourcePort = 3000, DestinationPort = 2000, Checksum = null, SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Synchronize, Window = 1000, UrgentPointer = 0, Options = TcpOptions.None }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes("This is a tcp flow")) }; builder = new PacketBuilder(ethernetLayer, ipv4Layer, tcpLayer, payloadLayer); return(builder.Build(DateTime.Now)); }
private static void HandleIcmp(Packet packet, int threadID) { //Console.WriteLine("ICMP Packet: " + packet.Length); MyDevice newDev = GetDevice(threadID, "other"); CheckAddress(packet, "Icmp"); if (!packet.IpV4.Source.ToString().Equals("0.0.0.0") || !packet.IpV4.Destination.ToString().Equals("0.0.0.0")) { //Console.WriteLine("Device: " + newDev.ID + "ICMP Addresses list count: " + newDev.ComputerAddresses.Count); Address addr = GetTargetAddress(packet, newDev); IpV4Layer ipLayer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer(); string packetType = "REQUEST"; try { IcmpEchoLayer test = (IcmpEchoLayer)packet.Ethernet.IpV4.Icmp.ExtractLayer(); } catch { packetType = "REPLY"; } // PayloadLayer payload = (PayloadLayer)packet.Ethernet.Payload.ExtractLayer(); //IcmpEchoLayer icmpLayer = (IcmpEchoLayer)packet.Ethernet.IpV4.Icmp.ExtractLayer(); //Console.Write(payload.Data); //Console.WriteLine("ETHER LAYER DESTINATION: " + ethLayer.Destination); Console.WriteLine("ICMP MessageTYPE: " + packet.IpV4.Icmp.MessageType.ToString()); Console.WriteLine("ICMP TYPE: " + packet.IpV4.Icmp.GetType().ToString()); Console.WriteLine("ICMP CODE: " + packet.IpV4.Icmp.Code); Console.WriteLine("PACKET LENGTH: " + packet.Length); if (addr != null && ipLayer != null && packet.Length != 70) { Packet newPacket = BuildIcmpPacket(new MacAddress(newDev.MacAddressWithDots()), new MacAddress(addr.Mac), packet.Ethernet.IpV4.Source, packet.Ethernet.IpV4.Destination, packetType); //Packet newPacket = PacketBuilder.Build(DateTime.Now, ethLayer, ipLayer, icmpLayer); if (newPacket.IsValid) { if (newPacket.Ethernet.Source != newPacket.Ethernet.Destination) { newDev.Communicator.SendPacket(newPacket); Console.WriteLine("Icmp Packet Sent"); } } else { Console.WriteLine("ICMP Packet Is Not Valid :("); } } } }
/// <summary> /// This function build a DNS over UDP over IPv4 over Ethernet packet. /// </summary> private static Packet BuildDnsPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("1.2.3.4"), CurrentDestination = new IpV4Address("11.22.33.44"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; UdpLayer udpLayer = new UdpLayer { SourcePort = 4050, DestinationPort = 53, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true, }; DnsLayer dnsLayer = new DnsLayer { Id = 100, IsResponse = false, OpCode = DnsOpCode.Query, IsAuthoritativeAnswer = false, IsTruncated = false, IsRecursionDesired = true, IsRecursionAvailable = false, FutureUse = false, IsAuthenticData = false, IsCheckingDisabled = false, ResponseCode = DnsResponseCode.NoError, Queries = new[] { new DnsQueryResourceRecord(new DnsDomainName("pcapdot.net"), DnsType.A, DnsClass.Internet), }, Answers = null, Authorities = null, Additionals = null, DomainNameCompressionMode = DnsDomainNameCompressionMode.All, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer); return(builder.Build(DateTime.Now)); }
public void handler(Packet packet, PacketCommunicator device) { //received ping request if (packet.Ethernet.IpV4.Icmp.MessageTypeAndCode.ToString().Equals("Echo")) { PacketCommunicator dev = device; String srcMAC = packet.Ethernet.Destination.ToString(); String dstMAC = packet.Ethernet.Source.ToString(); String srcIP = packet.Ethernet.IpV4.Destination.ToString(); String dstIP = packet.Ethernet.IpV4.Source.ToString(); EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(srcMAC), Destination = new MacAddress(dstMAC), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(srcIP), CurrentDestination = new IpV4Address(dstIP), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; IcmpIdentifiedDatagram icmp = (IcmpIdentifiedDatagram)packet.Ethernet.IpV4.Icmp; IcmpEchoReplyLayer icmpLayer = new IcmpEchoReplyLayer { Checksum = null, // Will be filled automatically. SequenceNumber = icmp.SequenceNumber, Identifier = icmp.Identifier, //switchID }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); //Packet EchoReplyPacket = builder.Build(DateTime.Now); dev.SendPacket(builder.Build(DateTime.Now)); } //received ping response else if (packet.Ethernet.IpV4.Icmp.MessageTypeAndCode.ToString().Equals("EchoReply")) { MessageBox.Show("Ping Reply!"); } }
public void SendUDP() { EthernetLayer ethernetLayer = CreateEthernetLayer(); IpV4Layer ipV4Layer = CreateIpV4Layer(); UdpLayer udpLayer = CreateUdpLayer(); PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer); using (PacketCommunicator communicator = _device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { communicator.SendPacket(builder.Build(DateTime.Now)); } }
private Packet BuildHttpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress("01:01:01:01:01:01"), Destination = new MacAddress("02:02:02:02:02:02"), EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address("0.0.0.0"), CurrentDestination = new IpV4Address("0.0.0.0"), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 123, Options = IpV4Options.None, Protocol = null, Ttl = 100, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = 4050, DestinationPort = 80, Checksum = null, SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Acknowledgment, Window = 100, UrgentPointer = 0, Options = TcpOptions.None, }; HttpRequestLayer httpLayer = new HttpRequestLayer { Version = PcapDotNet.Packets.Http.HttpVersion.Version11, Header = new HttpHeader(new HttpContentLengthField(11)), Body = new Datagram(Encoding.ASCII.GetBytes("?action=getPaLst")), Method = new HttpRequestMethod(HttpRequestKnownMethod.Get), Uri = @"http://", }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, httpLayer); return(builder.Build(DateTime.Now)); }
/// <summary> /// This function build an HTTP over TCP over IPv4 over Ethernet packet. /// </summary> public static Packet BuildHttpPacket() { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(macAddressSource), Destination = new MacAddress(macAddressDest), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(ipSource), CurrentDestination = new IpV4Address(ipDest), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = 123, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = 100, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = 4050, DestinationPort = 80, Checksum = null, // Will be filled automatically. SequenceNumber = 100, AcknowledgmentNumber = 50, ControlBits = TcpControlBits.Acknowledgment, Window = 100, UrgentPointer = 0, Options = TcpOptions.None, }; HttpRequestLayer httpLayer = new HttpRequestLayer { Version = HttpVersion.Version11, Header = new HttpHeader(new HttpContentLengthField(11)), Body = new Datagram(Encoding.ASCII.GetBytes("hello world")), Method = new HttpRequestMethod(HttpRequestKnownMethod.Get), Uri = @"http://pcapdot.net/", }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, httpLayer); return(builder.Build(DateTime.Now)); }
private static void DiscoverNetworkBroadcast(PacketCommunicator communicator, MyDevice device) { // Supposing to be on ethernet, set mac source MacAddress source = new MacAddress(device.MacAddressWithDots()); // set mac destination to broadcast MacAddress destination = new MacAddress("FF:FF:FF:FF:FF:FF"); // Create the packets layers // Ethernet Layer EthernetLayer ethernetLayer = new EthernetLayer { Source = source, Destination = destination }; // IPv4 Layer IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(device.IPAddress), Ttl = 128, // The rest of the important parameters will be set for each packet }; // ICMP Layer IcmpEchoLayer icmpLayer = new IcmpEchoLayer(); // Create the builder that will build our packets PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer); string ipBeg = device.IpWithoutEnd(); //Send 100 Pings to different destination with different parameters for (int i = 0; i < 256; i++) { // Set IPv4 parameters ipV4Layer.CurrentDestination = new IpV4Address(ipBeg + i); ipV4Layer.Identification = (ushort)i; // Set ICMP parameters icmpLayer.SequenceNumber = (ushort)i; icmpLayer.Identifier = (ushort)i; // Build the packet Packet packet = builder.Build(DateTime.Now); // Send down the packet communicator.SendPacket(packet); //Console.WriteLine("172.16.1." + i); } }
/// <summary> /// Assigns from prefilled IpV4 layer (with user values) to other tcp layer (assigns only user values, if they are not default) /// </summary> /// <param name="layerToCopyFrom">IpV4 layer with user filled values</param> /// <param name="layerToCopyTo">IpV4 layer to assign values to</param> /// <returns>Modified IpV4 layer with newly assigned user values</returns> private Layer AssignUserValuesFromFilledIpV4LayerToOtherIpV4Layer(IpV4Layer layerToCopyFrom, IpV4Layer layerToCopyTo) { layerToCopyTo.CurrentDestination = (layerToCopyFrom.CurrentDestination != default) ? layerToCopyFrom.CurrentDestination : layerToCopyTo.CurrentDestination; layerToCopyTo.Fragmentation = (layerToCopyFrom.Fragmentation != default) ? layerToCopyFrom.Fragmentation : layerToCopyTo.Fragmentation; layerToCopyTo.HeaderChecksum = (layerToCopyFrom.HeaderChecksum != default) ? layerToCopyFrom.HeaderChecksum : layerToCopyTo.HeaderChecksum; layerToCopyTo.Identification = (layerToCopyFrom.Identification != default) ? layerToCopyFrom.Identification : layerToCopyTo.Identification; layerToCopyTo.Protocol = (layerToCopyFrom.Protocol != default) ? layerToCopyFrom.Protocol : layerToCopyTo.Protocol; layerToCopyTo.Source = (layerToCopyFrom.Source != default) ? layerToCopyFrom.Source : layerToCopyTo.Source; layerToCopyTo.Ttl = (layerToCopyFrom.Ttl != default) ? layerToCopyFrom.Ttl : layerToCopyTo.Ttl; layerToCopyTo.TypeOfService = (layerToCopyFrom.TypeOfService != default) ? layerToCopyFrom.TypeOfService : layerToCopyTo.TypeOfService; return(layerToCopyTo); }
public Packet BuildTcpPacket(Packet origPacket) { EthernetLayer ethernetLayer = new EthernetLayer { Source = origPacket.Ethernet.Source, Destination = lookUpMacAdress(Convert.ToString(origPacket.Ethernet.IpV4.Destination)), EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = origPacket.Ethernet.IpV4.Source, CurrentDestination = origPacket.Ethernet.IpV4.Destination, Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = origPacket.Ethernet.IpV4.Identification, Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = origPacket.Ethernet.IpV4.Ttl, TypeOfService = origPacket.Ethernet.IpV4.TypeOfService, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = origPacket.Ethernet.IpV4.Tcp.SourcePort, DestinationPort = origPacket.Ethernet.IpV4.Tcp.DestinationPort, Checksum = null, // Will be filled automatically. SequenceNumber = origPacket.Ethernet.IpV4.Tcp.SequenceNumber, AcknowledgmentNumber = origPacket.Ethernet.IpV4.Tcp.AcknowledgmentNumber, ControlBits = TcpControlBits.Acknowledgment, Window = origPacket.Ethernet.IpV4.Tcp.Window, UrgentPointer = origPacket.Ethernet.IpV4.Tcp.UrgentPointer, Options = origPacket.Ethernet.IpV4.Tcp.Options, }; PayloadLayer payloadLayer = new PayloadLayer { Data = origPacket.Ethernet.IpV4.Tcp.Payload, }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); return(builder.Build(DateTime.Now)); }
private PacketBuilder buildLayers(int srcPort, int dstPort) { EthernetLayer ethernetLayer = new EthernetLayer { Source = sourceMac, Destination = destMac, EtherType = EthernetType.None, // Will be filled automatically. }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(txtSrcIP.Text), CurrentDestination = new IpV4Address(txtDestIP.Text), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, // Will be filled automatically. Identification = (txtID.Text.Equals("")) ? (ushort)123 : ushort.Parse(txtID.Text), Options = IpV4Options.None, Protocol = null, // Will be filled automatically. Ttl = txtTTL.Text.Equals("") ? (byte)100 : byte.Parse(txtTTL.Text) > 255 ? (byte)255 : byte.Parse(txtTTL.Text) < 1 ? (byte)1 : byte.Parse(txtTTL.Text), TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = (ushort)srcPort, DestinationPort = (ushort)dstPort, Checksum = null, // Will be filled automatically. SequenceNumber = txtSequence.Text.Equals("") ? 100 : uint.Parse(txtSequence.Text), AcknowledgmentNumber = txtACK.Text.Equals("") ? 50 : uint.Parse(txtACK.Text), ControlBits = (radPSH.Checked) ? TcpControlBits.Push : (radACK.Checked) ? TcpControlBits.Acknowledgment : (radRST.Checked) ? TcpControlBits.Reset : (radFIN.Checked) ? TcpControlBits.Fin : TcpControlBits.Synchronize, Window = txtWindow.Text.Equals("") ? (ushort)100 : ushort.Parse(txtWindow.Text), UrgentPointer = 0, Options = TcpOptions.None, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(Encoding.ASCII.GetBytes(txtInput.Text)), }; // build packet and initiate communicator PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); PacketCommunicator communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000); return(builder); }
// private void SendTcpPacket(string SrcMac, string SrcIP, int SrcPort, string DestMac, string DestIp, int DestPort, string payload) { EthernetLayer ethLayer = new EthernetLayer { Source = new MacAddress(SrcMac), Destination = new MacAddress(DestMac), EtherType = EthernetType.None, }; IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(SrcIP), CurrentDestination = new IpV4Address(DestIp), Fragmentation = IpV4Fragmentation.None, HeaderChecksum = null, Identification = 0, Options = IpV4Options.None, Protocol = IpV4Protocol.Tcp, Ttl = 128, TypeOfService = 0, }; TcpLayer tcpLayer = new TcpLayer { SourcePort = (ushort)SrcPort, DestinationPort = (ushort)DestPort, Checksum = null, SequenceNumber = 0, AcknowledgmentNumber = 0, ControlBits = TcpControlBits.Synchronize, Window = 1024, UrgentPointer = 0, }; PayloadLayer payloadLayer = new PayloadLayer { Data = new Datagram(StringToByteArray(payload)), }; PacketBuilder builder = new PacketBuilder(ethLayer, ipV4Layer, tcpLayer, payloadLayer); this.Invoke(new MethodInvoker(delegate() { using (PacketCommunicator communicator = LivePacketDevice.AllLocalMachine[0].Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { communicator.SendPacket(builder.Build(DateTime.Now)); } })); }
public Packet CreateDnsReply(EthernetDatagram etherpacket, IpV4Address newAddress) { var ipPacket = etherpacket.IpV4; var udpPacket = ipPacket.Udp; var dnsPacket = udpPacket.Dns; if (!dnsPacket.IsQuery) { throw new Exception("Packet should be a dns query!"); } EthernetLayer ethernetLayer = new EthernetLayer { Source = etherpacket.Destination, Destination = etherpacket.Source, }; IpV4Layer ipLayer = new IpV4Layer { Source = ipPacket.Destination, CurrentDestination = ipPacket.Source, }; UdpLayer udpLayer = new UdpLayer { SourcePort = udpPacket.DestinationPort, DestinationPort = udpPacket.SourcePort }; DnsResourceData resourceData = new DnsResourceDataIpV4(newAddress); DnsDataResourceRecord resourceRecord = new DnsDataResourceRecord(dnsPacket.Queries[0].DomainName, dnsPacket.Queries[0].DnsType, dnsPacket.Queries[0].DnsClass, 60, resourceData); DnsLayer dnsLayer = new DnsLayer { Queries = dnsPacket.Queries, IsQuery = false, IsResponse = true, Id = dnsPacket.Id, Answers = new[] { resourceRecord } }; PacketBuilder builder = new PacketBuilder(ethernetLayer, ipLayer, udpLayer, dnsLayer); return(builder.Build(DateTime.Now)); }