120 lines
4.2 KiB
C#
120 lines
4.2 KiB
C#
// /**
|
|
// * File: SSH.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using Renci.SshNet;
|
|
using ln.types;
|
|
using Renci.SshNet.Common;
|
|
using System.Net.Sockets;
|
|
using ln.types.net;
|
|
using ln.skyscanner.crawl.service;
|
|
using ln.logging;
|
|
namespace ln.skyscanner.crawl.tests
|
|
{
|
|
public class SSH : CrawlService
|
|
{
|
|
public SSH()
|
|
: base("ssh")
|
|
{
|
|
}
|
|
|
|
public static bool CanConnect(CrawledHost crawledHost)
|
|
{
|
|
int sshPort = crawledHost.GetHint<int>("ssh.port", -1);
|
|
IPv4 sshIP = crawledHost.GetHint<IPv4>("ssh.ip", null);
|
|
string sshUser = crawledHost.GetHint<string>("ssh.login", null);
|
|
string sshPassword = crawledHost.GetHint<string>("ssh.password", null);
|
|
|
|
if ((sshPort == -1) || !CanConnect(crawledHost,sshIP,sshPort,sshUser,sshPassword))
|
|
{
|
|
if (!Scan(crawledHost))
|
|
{
|
|
crawledHost.SetHint("ssh.port", -1);
|
|
crawledHost.SetHint("ssh.ip", null);
|
|
crawledHost.SetHint("ssh.login", null);
|
|
crawledHost.SetHint("ssh.password", null);
|
|
crawledHost.SetHint("ssh.version", null);
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static bool Scan(CrawledHost crawledHost)
|
|
{
|
|
foreach (IPv4 ip in crawledHost.IPAddresses)
|
|
{
|
|
foreach (int port in new int[] { 13022, 22 })
|
|
{
|
|
if (crawledHost.HasHint(String.Format("tcp.{0}",port)) && crawledHost.GetHint<bool>(String.Format("tcp.{0}", port)))
|
|
try
|
|
{
|
|
foreach (Credential credential in SkyScanner.Instance.Crawler.Credentials)
|
|
{
|
|
Logging.Log(LogLevel.DEBUG, "SSH trying {0}:{1}...", credential.Username, credential.Password.Substring(0, 4));
|
|
|
|
if (CanConnect(crawledHost, ip, port, credential.Username, credential.Password, true))
|
|
return true;
|
|
}
|
|
} catch (SocketException)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static bool CanConnect(CrawledHost crawledHost, IPv4 host, int port, string username, string password,bool throwe = false)
|
|
{
|
|
using (SshClient client = new SshClient(host.ToString(), port, username, password))
|
|
{
|
|
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(5);
|
|
try
|
|
{
|
|
String authBanner = null;
|
|
client.ConnectionInfo.AuthenticationBanner += (object sender, AuthenticationBannerEventArgs e) => authBanner = e.BannerMessage;
|
|
|
|
client.Connect();
|
|
|
|
crawledHost.SetHint("ssh.port", client.ConnectionInfo.Port);
|
|
crawledHost.SetHint("ssh.ip", host);
|
|
crawledHost.SetHint("ssh.login", client.ConnectionInfo.Username);
|
|
crawledHost.SetHint("ssh.password", password);
|
|
crawledHost.SetHint("ssh.version", client.ConnectionInfo.ServerVersion);
|
|
crawledHost.SetHint("ssh.authbanner", authBanner);
|
|
|
|
client.Disconnect();
|
|
return true;
|
|
}
|
|
catch (SshException sshe)
|
|
{
|
|
Logging.Log(sshe);
|
|
}
|
|
catch (SocketException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public override bool Check(Crawl crawl)
|
|
{
|
|
return CanConnect(crawl.Host);
|
|
|
|
}
|
|
|
|
public override bool HostProvidesOption(Crawl crawl, params object[] parameters)
|
|
{
|
|
return crawl.Host.GetHint<IPv4>("ssh.ip", null) != null;
|
|
}
|
|
}
|
|
}
|