示例#1
0
        public PTRRecord ReverseDnsLookup(Common.IPv4Address ip)
        {
            string lookup = ip.AsString + " PTR";
            string ipStr  = ip.AsReverseString + ".in-addr.arpa".ToLower();

            // Do we already know?
            if (DnsCache.ContainsKey(lookup))
            {
                return(DnsCache[lookup] as PTRRecord);
            }

            // We don't know. Send packet and wait for response.
            SendRecordRequest(ipStr, DnsType.Ptr);

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalSeconds < TIMEOUT)
            {
                if (DnsCache.ContainsKey(lookup))
                {
                    return(DnsCache[lookup] as PTRRecord);
                }
                System.Threading.Thread.Sleep(100);
            }

            if (DnsCache.ContainsKey(lookup))
            {
                return(DnsCache[lookup] as PTRRecord);
            }
            throw new Exception("Host not found.");
        }
示例#2
0
        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);
        }
示例#3
0
        public PingResult(Packet packet, DateTime ts, PingRequest req)
        {
            Result = PingResultType.Reply;
            var reply = packet.Ethernet.IpV4.Icmp as IcmpEchoReplyDatagram;

            Ttl            = packet.Ethernet.IpV4.Ttl;
            RespondingHost = new Common.IPv4Address(packet.Ethernet.IpV4.Source.ToString());
            Response       = (int)(ts - req.TimeStamp).TotalMilliseconds;
        }
示例#4
0
        public override void OnReceivePacket(Common.PacketData pdata)
        {
            var ip       = pdata.Packet.Ethernet.IpV4;
            var tcp      = ip.Tcp;
            var destPort = tcp.DestinationPort;
            var destIp   = new Common.IPv4Address(ip.Destination.ToString());

            Sessions[destPort].AddPacketToQueue(pdata);
        }
示例#5
0
 private void TryFireArpReplyReceied(Common.IPv4Address ip, Common.MacAddress mac)
 {
     try
     {
         ArpReplyReceived(ip, mac);
     }
     catch
     {
     }
 }
示例#6
0
 private Common.IPv4Address[] SplitIps(string ips)
 {
     string[]             stuff = ips.Split(',');
     Common.IPv4Address[] ip    = new Common.IPv4Address[stuff.Length];
     for (int i = 0; i < stuff.Length; i++)
     {
         ip[i] = new Common.IPv4Address(stuff[i]);
     }
     return(ip);
 }
示例#7
0
        public Common.MacAddress ResolveIP(Common.IPv4Address ip)
        {
            // Do we already know?
            if (ArpCache.ContainsKey(ip.AsString) && (ArpCache[ip.AsString].Expiration > DateTime.Now || ArpCache[ip.AsString].Type == ArpEntryType.Static))
            {
                Common.MacAddress mac = ArpCache[ip.AsString].Mac;
                if (DisableCache)
                {
                    ArpCache.Clear();
                }
                return(mac);
            }

            // Is this the broadcast IP for our network?
            if (ip.AsString == _client.Configuration.BroadcastAddress.AsString)
            {
                return(Common.MacAddress.Broadcast);
            }

            // Is this me?
            if (ip.AsString == _client.Configuration.IpAddress.AsString)
            {
                return(_client.Configuration.MacAddress);
            }

            // Is this IP in our subnet?
            if (!Common.IPv4Address.IsInSameSubnet(ip, _client.Configuration.IpAddress, _client.Configuration.SubnetMask))
            {
                return(ResolveIP(_client.Configuration.DefaultGateway));
            }

            // We don't know. Send packet and wait for response.
            SendArpResolutionPacket(ip);

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalSeconds < TIMEOUT)
            {
                if (ArpCache.ContainsKey(ip.AsString) && (ArpCache[ip.AsString].Expiration > DateTime.Now || ArpCache[ip.AsString].Type == ArpEntryType.Static))
                {
                    Common.MacAddress mac = ArpCache[ip.AsString].Mac;
                    if (DisableCache)
                    {
                        ArpCache.Clear();
                    }
                }
                System.Threading.Thread.Sleep(100);
            }
            if (ArpCache.ContainsKey(ip.AsString))
            {
                return(ArpCache[ip.AsString].Mac);
            }
            throw new IpResolutionFailed(ip);
        }
示例#8
0
        public TcpSession(TcpClient client, Common.IPv4Address myip, ushort myport)
        {
            _client = client;

            SequenceNumber        = 0;
            AcknowledgementNumber = 0;

            Source     = myip;
            Queue      = new ConcurrentQueue <Common.PacketData>();
            SourcePort = myport;

            State = TcpState.LISTEN;
        }
示例#9
0
        public ARecord ResolveHost(string dns)
        {
            dns = dns.ToLower();
            string lookup  = dns + " A";
            string clookup = dns + " CNAME";

            // Is this an IP?
            HavokNet.Common.IPv4Address ip = null;
            try
            {
                ip = new Common.IPv4Address(dns);
                return(new ARecord(dns, 0, ip));
            }
            catch { }

            // Do we already know?
            if (DnsCache.ContainsKey(lookup))
            {
                return(DnsCache[lookup] as ARecord);
            }
            if (DnsCache.ContainsKey(clookup))
            {
                return(ResolveHost((DnsCache[clookup] as CNAMERecord).AliasTarget));
            }

            // We don't know. Send packet and wait for response.
            SendRecordRequest(dns, DnsType.A);

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalSeconds < TIMEOUT)
            {
                if (DnsCache.ContainsKey(lookup))
                {
                    return(DnsCache[lookup] as ARecord);
                }
                if (DnsCache.ContainsKey(clookup))
                {
                    return(ResolveHost((DnsCache[clookup] as CNAMERecord).AliasTarget));
                }
                System.Threading.Thread.Sleep(100);
            }

            throw new Exception("Host not found.");
        }
示例#10
0
 public void Ping(Common.IPv4Address ip, PingSettings settings, PingResultHandler callback)
 {
     for (int i = 0; i < (int)Math.Max(settings.Repeat, 1); i++)
     {
         try
         {
             SendIcmpRequestPacket(ip, settings.Ttl, settings.Bytes, 456, settings.Timeout, callback);
         }
         catch (ARP.IpResolutionFailed arp)
         {
             callback(new PingResult(PingResultType.DestinationHostUnreachable)
             {
                 RespondingHost = _client.Configuration.IpAddress
             });
         }
         System.Threading.Thread.Sleep(settings.Delay);
     }
 }
示例#11
0
        private void SendArpReplyPacket(Common.IPv4Address ip)
        {
            PcapDotNet.Packets.Arp.ArpLayer arpLayer =
                new PcapDotNet.Packets.Arp.ArpLayer
            {
                ProtocolType          = PcapDotNet.Packets.Ethernet.EthernetType.IpV4,
                Operation             = ArpOperation.Reply,
                SenderHardwareAddress = _client.Configuration.MacAddress.AsBytes.AsReadOnly(),
                SenderProtocolAddress = _client.Configuration.IpAddress.AsBytes.AsReadOnly(),
                TargetHardwareAddress = ArpCache[ip.AsString].Mac.AsBytes.AsReadOnly(),
                TargetProtocolAddress = ip.AsBytes.AsReadOnly(),
            };

            OSI.Layer2Packet packet = new OSI.Layer2Packet();
            packet.DestinationMac = ArpCache[ip.AsString].Mac;
            packet.SourceMac      = _client.Configuration.MacAddress;
            packet.NextLayers.Add(arpLayer);
            SendPacket(packet);
        }
示例#12
0
        public void SendArpResolutionPacket(Common.IPv4Address ip)
        {
            PcapDotNet.Packets.Arp.ArpLayer arpLayer =
                new PcapDotNet.Packets.Arp.ArpLayer
            {
                ProtocolType          = PcapDotNet.Packets.Ethernet.EthernetType.IpV4,
                Operation             = ArpOperation.Request,
                SenderHardwareAddress = _client.Configuration.MacAddress.AsBytes.AsReadOnly(),
                SenderProtocolAddress = _client.Configuration.IpAddress.AsBytes.AsReadOnly(),
                TargetHardwareAddress = Common.MacAddress.Broadcast.AsBytes.AsReadOnly(),
                TargetProtocolAddress = ip.AsBytes.AsReadOnly(),
            };

            OSI.Layer2Packet packet = new OSI.Layer2Packet();
            packet.DestinationMac = Common.MacAddress.Broadcast;
            packet.SourceMac      = _client.Configuration.MacAddress;
            packet.NextLayers.Add(arpLayer);
            SendPacket(packet);
        }
示例#13
0
        public override void OnReceivePacket(Common.PacketData pdata)
        {
            var packet = pdata.Packet;
            var fromIP = new Common.IPv4Address(packet.Ethernet.IpV4.Source.ToString());
            var toIP   = new Common.IPv4Address(packet.Ethernet.IpV4.Destination.ToString());

            var ipv4 = packet.Ethernet.IpV4;
            var icmp = ipv4.Icmp;

            // Is this an echo request to our IP?
            if (icmp.MessageType == PcapDotNet.Packets.Icmp.IcmpMessageType.Echo && toIP.AsString == _client.Configuration.IpAddress.AsString)
            {
                SendIcmpReplyPacket(packet);
            }
            else if (icmp.MessageType == IcmpMessageType.EchoReply && CurrentPings.ContainsKey((packet.Ethernet.IpV4.Icmp as IcmpEchoReplyDatagram).Identifier))
            {
                var        reply   = packet.Ethernet.IpV4.Icmp as IcmpEchoReplyDatagram;
                var        request = CurrentPings[reply.Identifier];
                PingResult result  = new PingResult(packet, pdata.TimeStamp, request);
                result.Bytes = request.Bytes;
                request.Callback(result);
            }
            else if (icmp.MessageType == IcmpMessageType.TimeExceeded)
            {
                var        reply     = packet.Ethernet.IpV4.Icmp as IcmpTimeExceededDatagram;
                var        replyEcho = reply.IpV4.Icmp as IcmpEchoDatagram;
                var        request   = CurrentPings[replyEcho.Identifier];
                PingResult result    = new PingResult(PingResultType.TtlExpired);
                result.RespondingHost = fromIP;
                result.Response       = (int)(DateTime.Now - request.TimeStamp).TotalMilliseconds;
                request.Callback(result);
            }
            else if (icmp.MessageType == IcmpMessageType.DestinationUnreachable)
            {
                var reply     = packet.Ethernet.IpV4.Icmp as IcmpDestinationUnreachableDatagram;
                var replyEcho = reply.IpV4.Icmp as IcmpDestinationUnreachableDatagram;
                //var request = CurrentPings[replyEcho.Identifier];
                //PingResult result = new PingResult(PingResultType.DestinationHostUnreachable);
                //result.RespondingHost = fromIP;
                //result.Response = (int)(DateTime.Now - request.TimeStamp).TotalMilliseconds;
                //request.Callback(result);
            }
        }
示例#14
0
        public void AddStaticEntry(Common.IPv4Address ip, Common.MacAddress mac)
        {
            ArpCacheEntry entry = new ArpCacheEntry()
            {
                Ip         = ip,
                Mac        = mac,
                Expiration = DateTime.Now,
                Type       = ArpEntryType.Static,
            };

            if (ArpCache.ContainsKey(ip.AsString))
            {
                ArpCache[ip.AsString] = entry;
            }
            else
            {
                ArpCache.AddOrUpdate(ip.AsString, entry, null);
            }
            TryFireArpCacheChanged();
        }
示例#15
0
        public override void OnReceivePacket(Common.PacketData pdata)
        {
            var packet    = pdata.Packet;
            var arp       = packet.Ethernet.Arp;
            var senderMac = new Common.MacAddress(arp.SenderHardwareAddress.ToArray());
            var destMac   = new Common.MacAddress(arp.TargetHardwareAddress.ToArray());
            var senderIp  = new Common.IPv4Address(arp.SenderProtocolAddress.ToArray());
            var destIp    = new Common.IPv4Address(arp.TargetProtocolAddress.ToArray());

            // We can cache their MAC and IP
            if (AcceptGratuitousReplies || destMac.AsString != Common.MacAddress.Broadcast.AsString)
            {
                ArpCache.AddOrUpdate(senderIp.AsString, new ArpCacheEntry()
                {
                    Mac        = senderMac,
                    Ip         = senderIp,
                    Type       = ArpEntryType.Dynamic,
                    Expiration = GetNextExpirationTime(),
                }, (str, myarp) => myarp);

                if (arp.Operation == ArpOperation.Reply)
                {
                    TryFireArpReplyReceied(senderIp, senderMac);
                }

                // Fire the Cache Changed event
                TryFireArpCacheChanged();
            }

            // If it is a request for our IP, respond
            if (arp.Operation == ArpOperation.Request && destIp.AsString == _client.Configuration.IpAddress.AsString)
            {
                // Send Reply Packet
                SendArpReplyPacket(senderIp);
            }
        }
示例#16
0
 public void Ping(Common.IPv4Address ip, PingResultHandler callback)
 {
     SendIcmpRequestPacket(ip, 225, 32, 123, 3, callback);
 }
示例#17
0
 public void Ping(string host, PingSettings settings, PingResultHandler callback)
 {
     Common.IPv4Address ip = _client.Dns.ResolveHost(host).IPs[0];
     Ping(ip, settings, callback);
 }
示例#18
0
        private void SendIcmpRequestPacket(Common.IPv4Address ip, byte ttl, ushort bytes, int seqNum, int timeout, PingResultHandler callback)
        {
            OSI.Layer3Packet packet = new OSI.Layer3Packet();
            packet.Destination = ip.AsString;
            packet.SourceIP    = _client.Configuration.IpAddress;
            packet.Ttl         = ttl;

            var data = new byte[bytes];

            _random.NextBytes(data);

            var shor = new byte[2];

            _random.NextBytes(shor);
            ushort id = BitConverter.ToUInt16(shor, 0);

            // Save ping
            if (CurrentPings.ContainsKey(id))
            {
                CurrentPings[id] = new PingRequest(callback);
            }
            else
            {
                PingRequest req = new PingRequest(callback);
                req.Bytes = bytes;
                CurrentPings.Add(id, req);
            }

            PcapDotNet.Packets.Icmp.IcmpEchoLayer icmpLayer =
                new PcapDotNet.Packets.Icmp.IcmpEchoLayer
            {
                Checksum       = null,
                Identifier     = id,
                SequenceNumber = (ushort)(seqNum),
            };
            PcapDotNet.Packets.PayloadLayer extra = new PcapDotNet.Packets.PayloadLayer()
            {
                Data = new PcapDotNet.Packets.Datagram(data)
            };

            packet.NextLayers.Add(icmpLayer);
            packet.NextLayers.Add(extra);



            SendPacket(packet, () =>
            {
                CurrentPings[id].TimeStamp = DateTime.Now;
            });

            DateTime now = DateTime.Now;

            while ((DateTime.Now - now).TotalMilliseconds < timeout)
            {
                if (CurrentPings[id].ReplyReceived)
                {
                    break;
                }
                System.Threading.Thread.Sleep(100);
            }

            if (!CurrentPings[id].ReplyReceived)
            {
                CurrentPings[id].Callback(new PingResult(PingResultType.RequestTimedOut));
            }
            CurrentPings.Remove(id);
        }