/// <summary> /// Synchronously scan a subsequent range of endpoints for open ports and their services. /// </summary> /// <param name="ip1">The starting endpoint which initializaes the scan.</param> /// <param name="ip2">The ending endpoint which the scan will stop after.</param> public List <PKScanObject> PortKnockRange(string ip1, string ip2) { // Track our progress. int progress = 0; // Create a list of scan objects to return. List <PKScanObject> results = new List <PKScanObject>(); try { // Parse our IPs into an IPAddressRange object. IPAddress address1 = IPAddress.Parse(ip1); IPAddress address2 = IPAddress.Parse(ip2); IPAddressRange range = new IPAddressRange(address1, address2); // Loop through our range of IPs and start port knocking. int total = 0; foreach (IPAddress x in range) { total++; } // Grab number of addresses before proceeding. int count = 0; // Amount of addresses scanned. foreach (IPAddress address in range) { // Iterate up so we can report progress to the user. count++; // Let the user know which address we are currently scanning. UpdatePortKnockRangeProgress(progress, address.ToString()); UpdatePortKnockRangeAsyncProgress(progress, address.ToString()); // Scan the current address. PKScanObject result = PortKnock(address.ToString()); results.Add(result); // Calculate our progress percentage. int x = (int)(((double)count / (double)total) * 100); if (x > progress) { progress = x; UpdatePortKnockRangeProgress(progress, address.ToString()); UpdatePortKnockRangeAsyncProgress(progress, address.ToString()); } } } catch (Exception ex) // Catch if we are unable to parse the list. { // Create a new scan object containing our errors instead of returning null. PKScanObject result = new PKScanObject(); result.Errors = ex; results.Add(result); return(results); } // Update our event with our results. UpdatePortKnockRangeResults(results); UpdatePortKnockRangeAsyncResults(results); // Return our scanned ip addresses. return(results); }
/// <summary> /// Synchronously scan a pre-formatted list of IP addresses for open ports and their services. /// </summary> /// <param name="list">A list of IPs formatted as such: "address1, address2, address3, ..."</param> public List <PKScanObject> PortKnockList(string list) { // Keep track of our progress. int progress = 0; // Create a list of scan objects to return. List <PKScanObject> results = new List <PKScanObject>(); try { // Trim all whitespace from our list of addresses. string trimmed = Regex.Replace(list, @"\s+", ""); // Split our list into an array so we can access each address individually. string[] addresses = trimmed.Split(','); // Loop through our addresses and begin scanning each one. int count = 0; // Amount of addresses scanned. foreach (string address in addresses) { // Iterate up to display our progress. count++; // Tell the user what address we're currently scanning. UpdateScanListProgress(progress, address.ToString()); UpdateScanListAsyncProgress(progress, address.ToString()); // Scan our current address. PKScanObject result = PortKnock(address); results.Add(result); // Calculate our progress percentage. int x = (int)(((double)count / (double)addresses.Length) * 100); if (x > progress) { progress = x; UpdateScanListProgress(progress, address.ToString()); UpdateScanListAsyncProgress(progress, address.ToString()); } } } catch (Exception ex) // Catch if we are unable to parse the list. { // Create a new scan object containing our errors instead of returning null. PKScanObject result = new PKScanObject(); result.Errors = ex; results.Add(result); return(results); } // Update our event with the results. UpdatePortKnockListResults(results); UpdatePortKnockListAsyncResults(results); // Return our list of scanned IP addresses. return(results); }
private void UpdatePortKnockAsyncResults(PKScanObject result) { if (PortKnockAsyncComplete == null) { return; } PortKnockAsyncCompleteEventArgs args = new PortKnockAsyncCompleteEventArgs(result); PortKnockAsyncComplete(this, args); }
/// <summary> /// Main Constructor. /// </summary> /// <param name="address">The original IP.</param> /// <param name="pingTime">The average response time in milliseconds for a ping request.</param> /// <param name="hostname">The actual name of a device at an endpoint.</param> /// <param name="mac">The physical address of a device.</param> /// <param name="ports">Collection of scanned ports.</param> /// <param name="online">The online status of a device.</param> /// <param name="elapsed">Total amount of time for current object's scan to compelte.</param> public IPScanObject(string address, long pingTime, string hostname, string mac, PKScanObject ports, bool online, double elapsed = 0) { Address = address; try { IP = IPAddress.Parse(address); } catch { } // The provided IP wasn't formatted correctly. Ping = pingTime; Hostname = hostname; MAC = mac; Ports = ports; isOnline = online; Elapsed = elapsed; }
/// <summary> /// Synchronoulsy scan an endpoint for open ports and their services. /// </summary> /// <param name="ip">The endpoint that should be scanned.</param> public PKScanObject PortKnock(string ip) { // Track our progress. int progress = 0; // Create a new scan object to return. PKScanObject result = new PKScanObject(); try { // Parse our string IP into an IPAddress object. IPAddress address = IPAddress.Parse(ip); // Check all ports by using both TCP and UDP. for (int i = 1; i < 65536; i++) { bool status = false; PKServiceObject port = new PKServiceObject(); // Check port using TCP. status = IsPortOpen(ip, i, new TimeSpan(250), false); if (status) { port = new PKServiceObject(ip, i, PortType.TCP, status); } // Check port using UDP. <<------ Produces a result of all 65535 ports being open/alive. //status = IsPortOpen(ip, i, new TimeSpan(5000), true); //if (status) { port = new PKServiceObject(ip, i, PortType.UDP, "", status); } // Add any open ports to our scan object. result.Services.Add(port); // Update our port knock progress events. int x = (int)(((double)i / (double)65536) * 100); if (x > progress) { progress = x; UpdatePortKnockProgress(progress); UpdatePortKnockAsyncProgress(progress); } } } catch (Exception ex) { result.Errors = ex; } // Update our port knock complete events. UpdatePortKnockResults(result); UpdatePortKnockAsyncResults(result); return(result); }
public PortKnockAsyncCompleteEventArgs(PKScanObject result) { Result = result; }
/// <summary> /// Synchronously scan a single local IP address in order to obtain more information about it. /// </summary> /// <param name="ip">The endpoint to enumerate information from.</param> /// <returns></returns> public IPScanObject Scan(string ip) { // Create a stopwatch to track elapsed time. Stopwatch time = Stopwatch.StartNew(); // Create a blank scan object for our return. IPScanObject result = new IPScanObject(ip, 0, "n/a", "n/a", null, false); try { // Create object shells and try to fill them with data. long ping = 0; string hostname = "n/a"; string mac = "n/a"; PKScanObject ports = new PKScanObject(); bool online = false; //Ping our IP. if (Fetchers.Ping) { ping = GetAveragePingResponse(ip, Probes, Timeout); } UpdateScanProgress(10); UpdateScanAsyncProgress(10); // Get our hostname. if (Fetchers.Hostname) { IPHostEntry entry = Dns.GetHostEntry(ip); if (entry != null) { hostname = entry.HostName; } } UpdateScanProgress(35); UpdateScanAsyncProgress(35); // Get our MAC address. if (Fetchers.MAC) { ArpRequestResult request = ArpRequest.Send(IPAddress.Parse(ip)); if (request.Exception != null) { mac = "n/a"; } else { // Format our address before passing it. int start = 0; string output = null; while (start < request.Address.ToString().Length) { output += request.Address.ToString().Substring(start, Math.Min(2, request.Address.ToString().Length - start)) + ":"; start += 2; } mac = output.Remove(output.Length - 1, 1); } } UpdateScanProgress(58); UpdateScanAsyncProgress(58); // Port knock all ports. // Let the ports be null for now since it's CPU intensive. UpdateScanProgress(75); UpdateScanAsyncProgress(75); // Set our online flag. if (Fetchers.Online) { if (IsHostAlive(ip, Probes, Timeout)) { online = true; } } UpdateScanProgress(100); UpdateScanAsyncProgress(100); // Create a new scan object with our results. time.Stop(); result = new IPScanObject(ip, ping, hostname, mac, ports, online); } catch (Exception ex) { result.Errors = ex; } // Let it return a blank object containing errors. // Return our scanned object even if it's an error shell. result.Elapsed = time.Elapsed.TotalSeconds; UpdateScanResults(result); UpdateScanAsyncResults(result); return(result); }