public int FillHostInformation(List<Host> hosts) { List<String> hostStrings = (from h in hosts select h.AddressString).ToList(); int serviceCounter = 0; foreach (string file in Directory.GetFiles(m_resultPath)) { string fileName = Path.GetFileName(file); if (!fileName.StartsWith("services-")) continue; if (fileName == "services-Heartbleed") continue; using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (String.IsNullOrEmpty(line)) continue; int hostStringEnd = line.IndexOf(';'); string hostString = line.Substring(0, hostStringEnd); if (hostStrings.Contains(hostString)) { Host host = hosts.Single(h => h.AddressString == hostString); string[] tokens = line.Split(new char[] { ';' }, 4); string service = tokens[1]; int port = Int32.Parse(tokens[2]); Service serv = new Service((ushort)port, service, tokens[3]); host.Services.Add(serv); serviceCounter++; } } } } return serviceCounter; }
public static List<Host> GetHostListFromShodan(List<string> ips) { List<Host> hostList = new List<Host>(); ShodanWeb shodan = new ShodanWeb("APIKEY"); int hostCounter = 0; Console.WriteLine("Total hosts: " + ips.Count); Parallel.ForEach(ips, ip => //foreach (var ip in ips) { JObject hostObject = shodan.GetHost(ip); if (hostObject == null) { Console.WriteLine("Skipped host {0} with ip {1} due to a Shodan error.", ++hostCounter, ip); return; //continue; } JToken errorToken; if (hostObject.TryGetValue("error", out errorToken)) { Console.WriteLine("Skipped host {0} with ip {1}: {2}.", ++hostCounter, ip, errorToken.Value<string>()); return; //continue; } Host host = new Host(IPAddress.Parse(ip)); host.HostNames = new List<string>(); JToken hostNames; if (hostObject.TryGetValue("hostnames", out hostNames)) { JArray hostNamesArray = (JArray)hostNames; host.HostNames.AddRange(hostNamesArray.ToObject<string[]>()); } JToken dataToken; if (hostObject.TryGetValue("data", out dataToken)) { string module = ""; string port = "0"; string product = null; string version = null; string banner = ""; foreach (JToken dataChild in dataToken.Children()) { var dataChildObject = (JObject)dataChild; JToken productToken; if (dataChildObject.TryGetValue("product", out productToken)) product = productToken.Value<string>(); JToken versionToken; if (dataChildObject.TryGetValue("version", out versionToken)) version = versionToken.Value<string>(); JToken shodanToken = dataChildObject.Property("_shodan"); var shodanChildrenArray = shodanToken.Children().ToArray(); if (shodanChildrenArray.Length > 0) { var childrenArray = shodanChildrenArray[0].Children().ToArray(); if (childrenArray.Length > 0) { JProperty moduleToken = (JProperty)childrenArray[0]; module = moduleToken.Value.Value<string>().ToUpper(); } } JToken portToken; if (dataChildObject.TryGetValue("port", out portToken)) port = portToken.Value<string>(); JToken bannerToken; if (dataChildObject.TryGetValue("banner", out bannerToken)) banner = bannerToken.Value<string>(); Service service = new Service(ushort.Parse(port), module, null); service.Product = product; service.Version = version; service.Banner = banner; host.Services.Add(service); lock (hostList) hostList.Add(host); } Console.WriteLine("Loaded {0} hosts from Shodan.", ++hostCounter); } }); return hostList; }