public void OnetimeSetup() { var host = new ScanHost("10.20.30.40") { HostName = "server1.domain.com", SSH = true }; _deserializer = new PortScanDeserializer(new [] {host}, ProtocolType.SSH2); _deserializer.Deserialize(); var connectionTreeModel = _deserializer.Deserialize(); var root = connectionTreeModel.RootNodes.First(); _importedConnectionInfo = root.Children.First(); }
private void ImportScannedHost(ScanHost host, ContainerInfo parentContainer) { var finalProtocol = default(ProtocolType); var protocolValid = true; switch (_targetProtocolType) { case ProtocolType.SSH2: if (host.SSH) finalProtocol = ProtocolType.SSH2; break; case ProtocolType.Telnet: if (host.Telnet) finalProtocol = ProtocolType.Telnet; break; case ProtocolType.HTTP: if (host.HTTP) finalProtocol = ProtocolType.HTTP; break; case ProtocolType.HTTPS: if (host.HTTPS) finalProtocol = ProtocolType.HTTPS; break; case ProtocolType.Rlogin: if (host.Rlogin) finalProtocol = ProtocolType.Rlogin; break; case ProtocolType.RDP: if (host.RDP) finalProtocol = ProtocolType.RDP; break; case ProtocolType.VNC: if (host.VNC) finalProtocol = ProtocolType.VNC; break; default: protocolValid = false; break; } if (!protocolValid) return; var newConnectionInfo = new ConnectionInfo { Name = host.HostNameWithoutDomain, Hostname = host.HostName, Protocol = finalProtocol }; newConnectionInfo.SetDefaultPort(); parentContainer.AddChild(newConnectionInfo); }
/* Some examples found here: * http://stackoverflow.com/questions/2114266/convert-ping-application-to-multithreaded-version-to-increase-speed-c-sharp */ private void PingSender_PingCompleted(object sender, PingCompletedEventArgs e) { // UserState is the IP Address var ip = e.UserState.ToString(); var scanHost = new ScanHost(ip); hostCount++; Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Scanning {hostCount} of {_ipAddresses.Count} hosts: {scanHost.HostIp}", true); if (e.Error != null) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Ping failed to {e.UserState} {Environment.NewLine} {e.Error.Message}", true); scanHost.ClosedPorts.AddRange(_ports); scanHost.SetAllProtocols(false); } else if (e.Reply.Status == IPStatus.Success) { /* ping was successful, try to resolve the hostname */ try { scanHost.HostName = Dns.GetHostEntry(scanHost.HostIp).HostName; } catch (Exception dnsex) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Could not resolve {scanHost.HostIp} {Environment.NewLine} {dnsex.Message}", true); } if (string.IsNullOrEmpty(scanHost.HostName)) { scanHost.HostName = scanHost.HostIp; } foreach (var port in _ports) { bool isPortOpen; try { var tcpClient = new System.Net.Sockets.TcpClient(ip, port); isPortOpen = true; scanHost.OpenPorts.Add(port); tcpClient.Close(); } catch (Exception) { isPortOpen = false; scanHost.ClosedPorts.Add(port); } if (port == ScanHost.SSHPort) { scanHost.SSH = isPortOpen; } else if (port == ScanHost.TelnetPort) { scanHost.Telnet = isPortOpen; } else if (port == ScanHost.HTTPPort) { scanHost.HTTP = isPortOpen; } else if (port == ScanHost.HTTPSPort) { scanHost.HTTPS = isPortOpen; } else if (port == ScanHost.RloginPort) { scanHost.Rlogin = isPortOpen; } else if (port == ScanHost.RDPPort) { scanHost.RDP = isPortOpen; } else if (port == ScanHost.VNCPort) { scanHost.VNC = isPortOpen; } } } else if (e.Reply.Status != IPStatus.Success) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Ping did not complete to {e.UserState} : {e.Reply.Status}", true); scanHost.ClosedPorts.AddRange(_ports); scanHost.SetAllProtocols(false); } // cleanup var p = (Ping)sender; p.PingCompleted -= PingSender_PingCompleted; p.Dispose(); var h = string.IsNullOrEmpty(scanHost.HostName) ? "HostNameNotFound" : scanHost.HostName; Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Scan of {scanHost.HostIp} ({h}) complete.", true); _scannedHosts.Add(scanHost); HostScannedEvent?.Invoke(scanHost, hostCount, _ipAddresses.Count); if (_scannedHosts.Count == _ipAddresses.Count) { ScanCompleteEvent?.Invoke(_scannedHosts); } }
private void PortScanner_HostScanned(ScanHost host, int scannedCount, int totalCount) { if (InvokeRequired) { Invoke(new PortScannerHostScannedDelegate(PortScanner_HostScanned), new object[] {host, scannedCount, totalCount}); return ; } Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, "Host scanned " + host.HostIp, true); ListViewItem listViewItem = host.ToListViewItem(); if (listViewItem != null) { lvHosts.Items.Add(listViewItem); listViewItem.EnsureVisible(); } prgBar.Maximum = totalCount; prgBar.Value = scannedCount; }
private void RaiseHostScannedEvent(ScanHost scanHost, int scannedHostCount, int totalHostCount) { HostScanned?.Invoke(scanHost, scannedHostCount, totalHostCount); }
/* Some examples found here: * http://stackoverflow.com/questions/2114266/convert-ping-application-to-multithreaded-version-to-increase-speed-c-sharp */ private void PingSender_PingCompleted(object sender, PingCompletedEventArgs e) { // UserState is the IP Address var ip = e.UserState.ToString(); ScanHost scanHost = new ScanHost(ip); hostCount++; Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Scanning {hostCount} of {_ipAddresses.Count} hosts: {scanHost.HostIp}", true); if (e.Error != null) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Ping failed to {e.UserState} {Environment.NewLine} {e.Error.Message}", true); scanHost.ClosedPorts.AddRange(_ports); scanHost.SetAllProtocols(false); } else if (e.Reply.Status == IPStatus.Success) { /* ping was successful, try to resolve the hostname */ try { scanHost.HostName = Dns.GetHostEntry(scanHost.HostIp).HostName; } catch (Exception dnsex) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Could not resolve {scanHost.HostIp} {Environment.NewLine} {dnsex.Message}", true); } if (string.IsNullOrEmpty(scanHost.HostName)) { scanHost.HostName = scanHost.HostIp; } foreach (int port in _ports) { bool isPortOpen; try { System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient(ip, port); isPortOpen = true; scanHost.OpenPorts.Add(port); tcpClient.Close(); } catch (Exception) { isPortOpen = false; scanHost.ClosedPorts.Add(port); } if (port == ScanHost.SSHPort) { scanHost.SSH = isPortOpen; } else if (port == ScanHost.TelnetPort) { scanHost.Telnet = isPortOpen; } else if (port == ScanHost.HTTPPort) { scanHost.HTTP = isPortOpen; } else if (port == ScanHost.HTTPSPort) { scanHost.HTTPS = isPortOpen; } else if (port == ScanHost.RloginPort) { scanHost.Rlogin = isPortOpen; } else if (port == ScanHost.RDPPort) { scanHost.RDP = isPortOpen; } else if (port == ScanHost.VNCPort) { scanHost.VNC = isPortOpen; } } } else if(e.Reply.Status != IPStatus.Success) { Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Ping did not complete to {e.UserState} : {e.Reply.Status}", true); scanHost.ClosedPorts.AddRange(_ports); scanHost.SetAllProtocols(false); } // cleanup var p = (Ping)sender; p.PingCompleted -= PingSender_PingCompleted; p.Dispose(); var h = string.IsNullOrEmpty(scanHost.HostName) ? "HostNameNotFound" : scanHost.HostName; Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, $"Tools.PortScan: Scan of {scanHost.HostIp} ({h}) complete.", true); _scannedHosts.Add(scanHost); HostScannedEvent?.Invoke(scanHost, hostCount, _ipAddresses.Count); if (_scannedHosts.Count == _ipAddresses.Count) ScanCompleteEvent?.Invoke(_scannedHosts); }