ln.skyscanner/crawl/service/SSH.cs

120 lines
4.2 KiB
C#
Raw Normal View History

2019-03-26 12:53:42 +01:00
// /**
// * 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;
2019-04-02 01:25:44 +02:00
using ln.types.net;
2019-04-04 00:50:53 +02:00
using ln.skyscanner.crawl.service;
2019-08-03 12:57:32 +02:00
using ln.logging;
2019-03-26 12:53:42 +01:00
namespace ln.skyscanner.crawl.tests
{
2019-04-04 00:50:53 +02:00
public class SSH : CrawlService
2019-03-26 12:53:42 +01:00
{
2019-04-04 00:50:53 +02:00
public SSH()
: base("ssh")
{
}
2019-03-26 12:53:42 +01:00
public static bool CanConnect(CrawledHost crawledHost)
{
int sshPort = crawledHost.GetHint<int>("ssh.port", -1);
2019-04-02 01:25:44 +02:00
IPv4 sshIP = crawledHost.GetHint<IPv4>("ssh.ip", null);
2019-03-26 12:53:42 +01:00
string sshUser = crawledHost.GetHint<string>("ssh.login", null);
string sshPassword = crawledHost.GetHint<string>("ssh.password", null);
2019-04-02 01:25:44 +02:00
if ((sshPort == -1) || !CanConnect(crawledHost,sshIP,sshPort,sshUser,sshPassword))
2019-03-26 12:53:42 +01:00
{
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)
{
2019-04-02 01:25:44 +02:00
foreach (IPv4 ip in crawledHost.IPAddresses)
2019-03-26 12:53:42 +01:00
{
foreach (int port in new int[] { 13022, 22 })
{
2019-08-03 12:57:32 +02:00
if (crawledHost.HasHint(String.Format("tcp.{0}",port)) && crawledHost.GetHint<bool>(String.Format("tcp.{0}", port)))
2019-03-26 12:53:42 +01:00
try
{
2019-08-03 12:57:32 +02:00
foreach (Credential credential in SkyScanner.Instance.Crawler.Credentials)
2019-03-26 12:53:42 +01:00
{
2019-08-03 12:57:32 +02:00
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))
2019-03-26 12:53:42 +01:00
return true;
}
} catch (SocketException)
{
continue;
}
}
}
return false;
}
2019-04-02 01:25:44 +02:00
private static bool CanConnect(CrawledHost crawledHost, IPv4 host, int port, string username, string password,bool throwe = false)
2019-03-26 12:53:42 +01:00
{
using (SshClient client = new SshClient(host.ToString(), port, username, password))
{
2019-08-03 12:57:32 +02:00
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(5);
2019-03-26 12:53:42 +01:00
try
{
2019-08-03 12:57:32 +02:00
String authBanner = null;
client.ConnectionInfo.AuthenticationBanner += (object sender, AuthenticationBannerEventArgs e) => authBanner = e.BannerMessage;
2019-03-26 12:53:42 +01:00
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);
2019-08-03 12:57:32 +02:00
crawledHost.SetHint("ssh.authbanner", authBanner);
2019-03-26 12:53:42 +01:00
client.Disconnect();
return true;
}
2019-08-03 12:57:32 +02:00
catch (SshException sshe)
2019-03-26 12:53:42 +01:00
{
2019-08-03 12:57:32 +02:00
Logging.Log(sshe);
2019-03-26 12:53:42 +01:00
}
catch (SocketException)
{
return false;
}
}
return false;
}
2019-04-04 00:50:53 +02:00
public override bool Check(Crawl crawl)
{
return CanConnect(crawl.Host);
2019-03-26 12:53:42 +01:00
2019-04-04 00:50:53 +02:00
}
2019-03-26 12:53:42 +01:00
2019-04-04 00:50:53 +02:00
public override bool HostProvidesOption(Crawl crawl, params object[] parameters)
{
return crawl.Host.GetHint<IPv4>("ssh.ip", null) != null;
}
2019-03-26 12:53:42 +01:00
}
}