Exemple #1
0
 /// <summary>
 ///     Use the provided raw nmaprun object to construct a more sane ScanResult object which contains information about the nmap run
 /// </summary>
 /// <param name="result">The result of parsing an nmaprun </param>
 public ScanResult(nmaprun result)
 {
     Total = int.Parse(result.runstats.hosts.total);
     Up    = int.Parse(result.runstats.hosts.up);
     Down  = int.Parse(result.runstats.hosts.down);
     Hosts = result.Items != null
                 ? result.Items.OfType <host>().Select(
         x => new Host
     {
         Address         = IPAddress.Parse(x.address.addr),
         PhysicalAddress = x.Items.OfType <address>().Where(y => y.addrtype == addressAddrtype.mac).DefaultIfEmpty(null).FirstOrDefault()?.addr,
         Vendor          = x.Items.OfType <address>().Where(y => y.addrtype == addressAddrtype.mac).DefaultIfEmpty(null).FirstOrDefault()?.vendor,
         Ports           =
             PortsSection(
                 x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
         ExtraPorts =
             ExtraPortsSection(
                 x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
         Hostnames =
             HostnamesSection(
                 x.Items.OfType <hostnames>().DefaultIfEmpty(null).FirstOrDefault()),
         OsMatches = OsMatchesSection(
             x.Items.OfType <os>().DefaultIfEmpty(null).FirstOrDefault())
     })
                 : Enumerable.Empty <Host>();
 }
Exemple #2
0
        public void SerializeTest()
        {
            nmaprun scan = Serialization.DeserializeFromFile <nmaprun>("scan_1.xml");
            string  xml  = scan.Serialize();

            Assert.IsNotNull(xml);
        }
Exemple #3
0
 /// <summary>
 ///     Use the provided raw nmaprun object to construct a more sane ScanResult object which contains information about the nmap run
 /// </summary>
 /// <param name="result">The result of parsing an nmaprun </param>
 public ScanResult(nmaprun result)
 {
     if (result?.runstats?.finished?.exit == finishedExit.error)
     {
         throw new NmapException(result.runstats.finished.errormsg);
     }
     Total = int.Parse(result.runstats.hosts.total);
     Up    = int.Parse(result.runstats.hosts.up);
     Down  = int.Parse(result.runstats.hosts.down);
     Hosts = result.Items != null
                 ? result.Items.OfType <host>().Select(
         x => {
         var macAddress = x.Items.OfType <address>().FirstOrDefault(y => y.addrtype == addressAddrtype.mac);
         return(new Host
         {
             Address = IPAddress.Parse(x.address.addr),
             MacAddress = macAddress is null ? null : PhysicalAddress.Parse(macAddress.addr?.Replace(':', '-')),
             MacVendor = macAddress?.vendor,
             Ports =
                 PortsSection(
                     x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
             ExtraPorts =
                 ExtraPortsSection(
                     x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
             Hostnames =
                 HostnamesSection(
                     x.Items.OfType <hostnames>().DefaultIfEmpty(null).FirstOrDefault()),
             OsMatches = OsMatchesSection(
                 x.Items.OfType <os>().DefaultIfEmpty(null).FirstOrDefault()),
             Reason = x.status?.reason,
         });
     })
        public void ScanTest()
        {
            Runner  runner  = new Runner();
            nmaprun nmaprun = runner.Scan(new[] { "scanme.nmap.org" });

            Assert.IsInstanceOfType(nmaprun, typeof(nmaprun));
        }
Exemple #5
0
        /// <summary>
        ///   Scans the specified targets.
        /// </summary>
        /// <param name="targets"> The targets, IP or Hostname </param>
        /// <returns> A resulting nmaprun instance. </returns>
        public nmaprun Scan(IEnumerable <string> targets)
        {
            scan(targets);
            nmaprun nmapScan = Serialization.DeserializeFromFile <nmaprun>(FilePath);

            return(nmapScan);
        }
Exemple #6
0
 /// <summary>
 ///     Use the provided raw nmaprun object to construct a more sane ScanResult object which contains information about the nmap run
 /// </summary>
 /// <param name="result">The result of parsing an nmaprun </param>
 public ScanResult(nmaprun result)
 {
     Total = int.Parse(result.runstats.hosts.total);
     Up    = int.Parse(result.runstats.hosts.up);
     Down  = int.Parse(result.runstats.hosts.down);
     Hosts = result.Items != null
                 ? result.Items.OfType <host>().Select(
         x => new Host
     {
         Address = IPAddress.Parse(x.address.addr),
         Ports   =
             PortsSection(
                 x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
         ExtraPorts =
             ExtraPortsSection(
                 x.Items.OfType <ports>().DefaultIfEmpty(null).FirstOrDefault()),
         Hostnames =
             HostnamesSection(
                 x.Items.OfType <hostnames>().DefaultIfEmpty(null).FirstOrDefault()),
         OsMatches = OsMatchesSection(
             x.Items.OfType <os>().DefaultIfEmpty(null).FirstOrDefault()),
         Mac    = ExtractMac(x),
         Vendor = ExtractVendor(x)
     })
                 : Enumerable.Empty <Host>();
 }
Exemple #7
0
        public void DeserializeTest()
        {
            string  xml  = File.ReadAllText("scan_1.xml");
            nmaprun scan = Serialization.Deserialize <nmaprun>(xml);

            Assert.IsInstanceOfType(scan, typeof(nmaprun));
        }
Exemple #8
0
        public void SerializeToFileTest()
        {
            nmaprun scan = Serialization.DeserializeFromFile <nmaprun>("scan_1.xml");

            scan.SerializeToFile("scan_2.xml");
            Assert.IsTrue(File.Exists("scan_2.xml"));
        }
        public void when_ScanResult_constructed_with_nmaprun_and_no_extraports_then_ExtraPorts_should_be_empty()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[] {}
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.IsFalse(sr.Hosts.First().ExtraPorts.Any());
        }
        /// <summary>
        ///   Scans the specified targets.
        /// </summary>
        /// <param name="targets"> The targets, IP or Hostname </param>
        /// <returns> A resulting nmaprun instance. </returns>
        public nmaprun Scan(string target)
        {
            string filePath = string.Format(@"nmaprun_{0}.xml", Guid.NewGuid());

            scan(target, filePath);
            nmaprun nmapScan = Serialization.DeserializeFromFile <nmaprun>(filePath);

            return(nmapScan);
        }
        public void when_ScanResult_constructed_with_empty_nmaprun_then_Hosts_should_be_empty()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.IsFalse(sr.Hosts.Any());
        }
Exemple #12
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Scanning Targets...");
            if (args.Length == 0)
            {
                Console.WriteLine("No Targets provided");
                Console.WriteLine("Done. Press any key to continue.");
                Console.ReadKey();
            }
            string[]           targets             = args[0].Split(',');
            string             outputDirectoryPath = args.Length == 2 ? args[1] : Directory.GetCurrentDirectory();
            OutputFileProvider outputFileProvider  = new OutputFileProvider(outputDirectoryPath, "scan_*.xml");

            Runner runner = new Runner {
                FilePath = Path.Combine(outputDirectoryPath, outputFileProvider.Next())
            };

            nmaprun lastRun = runner.Scan(targets);

            Console.WriteLine("Scan complete, Output written to " + runner.FilePath);
            ObjectDumper.Write(lastRun, 5);
            Console.WriteLine("Done. Press any key to continue.");
            Console.ReadKey();
        }
        public void when_ScanResult_constructed_with_nmaprun_then_Port_PortNumber_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    port = new[]
                                                        {
                                                            new port
                                                                {
                                                                    portid = "5643",
                                                                    protocol = portProtocol.tcp,
                                                                    state = new state
                                                                        {
                                                                            state1 = "parsed"
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual(5643, sr.Hosts.First().Ports.First().PortNumber);
        }
        public void when_ScanResult_constructed_with_nmaprun_then_ExtraPorts_State_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    extraports = new[]
                                                        {
                                                            new extraports
                                                                {
                                                                    count = "5643",
                                                                    state = "parsed"
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual("parsed", sr.Hosts.First().ExtraPorts.First().State);
        }
        public void when_ScanResult_constructed_with_nmaprun_and_service_is_present_then_Service_Version_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    port = new[]
                                                        {
                                                            new port
                                                                {
                                                                    portid = "5643",
                                                                    protocol = portProtocol.ip,
                                                                    state = new state
                                                                        {
                                                                            state1 = "herpderp"
                                                                        },
                                                                    service = new service
                                                                        {
                                                                            name = "Foobar",
                                                                            product = "Bizbaz",
                                                                            ostype = "DragonFly BSD",
                                                                            version = "2.718281828"
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual("2.718281828", sr.Hosts.First().Ports.First().Service.Version);
        }
        public void when_ScanResult_constructed_with_nmaprun_and_service_is_not_present_then_Port_Service_should_be_default()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    port = new[]
                                                        {
                                                            new port
                                                                {
                                                                    portid = "5643",
                                                                    protocol = portProtocol.ip,
                                                                    state = new state
                                                                        {
                                                                            state1 = "herpderp"
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual(default(Service), sr.Hosts.First().Ports.First().Service);
        }
        public void when_ScanResult_constructed_with_nmaprun_and_port_is_not_filtered_then_Port_Filtered_should_be_false()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    port = new[]
                                                        {
                                                            new port
                                                                {
                                                                    portid = "5643",
                                                                    protocol = portProtocol.ip,
                                                                    state = new state
                                                                        {
                                                                            state1 = "notfiltered"
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.IsFalse(sr.Hosts.First().Ports.First().Filtered);
        }
        public void when_ScanResult_constructed_with_nmaprun_with_sctp_as_protocol_then_Port_Protocol_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new ports
                                                {
                                                    port = new[]
                                                        {
                                                            new port
                                                                {
                                                                    portid = "5643",
                                                                    protocol = portProtocol.sctp,
                                                                    state = new state
                                                                        {
                                                                            state1 = "parsed"
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            // SCTP isn't supported by Windows by default, so Unknown seems the most appropriate protocol to return
            Assert.AreEqual(ProtocolType.Unknown, sr.Hosts.First().Ports.First().Protocol);
        }
 public XRoot(nmaprun root) {
     this.doc = new XDocument(root.Untyped);
     this.rootObject = root;
 }
Exemple #20
0
        public void DeserializeFromFileTest()
        {
            nmaprun actual = Serialization.DeserializeFromFile <nmaprun>("scan_1.xml");

            Assert.IsInstanceOfType(actual, typeof(nmaprun));
        }
Exemple #21
0
 /// <summary>
 ///     Use the provided raw nmaprun object to construct a more sane ScanResult object which contains information about the nmap run
 /// </summary>
 /// <param name="result">The result of parsing an nmaprun </param>
 public ScanResult(nmaprun result)
 {
     Total = int.Parse(result.runstats.hosts.total);
     Up = int.Parse(result.runstats.hosts.up);
     Down = int.Parse(result.runstats.hosts.down);
     Hosts = result.Items != null
                 ? result.Items.OfType<host>().Select(
                     x => new Host
                         {
                             Address = IPAddress.Parse(x.address.addr),
                             Ports =
                                 PortsSection(
                                     x.Items.OfType<ports>().DefaultIfEmpty(null).FirstOrDefault()),
                             ExtraPorts =
                                 ExtraPortsSection(
                                     x.Items.OfType<ports>().DefaultIfEmpty(null).FirstOrDefault()),
                             Hostnames =
                                 HostnamesSection(
                                     x.Items.OfType<hostnames>().DefaultIfEmpty(null).FirstOrDefault()),
                             OsMatches = OsMatchesSection(
                                 x.Items.OfType<os>().DefaultIfEmpty(null).FirstOrDefault())
                         })
                 : Enumerable.Empty<Host>();
 }
        public void when_ScanResult_constructed_with_nmaprun_then_Hostname_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new hostnames
                                                {
                                                    hostname = new[]
                                                        {
                                                            new hostname
                                                                {
                                                                    name = "example.com"
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual("example.com", sr.Hosts.First().Hostnames.First());
        }
        public void when_ScanResult_constructed_with_nmaprun_then_OsMatches__should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "0",
                                    down = "0",
                                    up = "0"
                                }
                        },
                    Items = new object[]
                        {
                            new host
                                {
                                    address = new address
                                        {
                                            addr = "127.0.0.1"
                                        },
                                    Items = new object[]
                                        {
                                            new os
                                                {
                                                    osmatch = new[]
                                                        {
                                                            new osmatch
                                                                {
                                                                    accuracy = "100",
                                                                    name = "Temple OS (www.templeos.org)",
                                                                    osclass = new[]
                                                                        {
                                                                            new osclass
                                                                                {
                                                                                    accuracy = "100",
                                                                                    osfamily = "Temple",
                                                                                    osgen = "apocalypse",
                                                                                    vendor = "Terry A. Davis"
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual("apocalypse", sr.Hosts.First().OsMatches.First().Generation);
        }
        public void when_ScanResult_constructed_with_nmaprun_then_Total_should_be_correct()
        {
            var nr = new nmaprun
                {
                    runstats = new runstats
                        {
                            hosts = new hosts
                                {
                                    total = "5643",
                                    down = "0",
                                    up = "0"
                                }
                        }
                };
            var sr = new ScanResult(nr);

            Assert.AreEqual(5643, sr.Total);
        }