ln.skyscanner/crawl/tests/SSH.cs

108 lines
3.4 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;
namespace ln.skyscanner.crawl.tests
{
public static class SSH
{
static string[] defaultPasswords = new string[]
{
"MNX3oTzhp9am",
"f1whWdj5E2Mo",
"f1whWdj5",
"0Sl71eGw",
"0Sl71eGwVdjI6WeW",
"67E3xpTc",
"67E3xpTcMbwR",
"v1kXbeCux0Td",
"v1kXbeCu",
"YNZRtVUFH94b",
"67E3xpTcMbwR",
"v1kXbeCux0Td",
"DVqxof1JQ9at"
};
public static bool CanConnect(CrawledHost crawledHost)
{
int sshPort = crawledHost.GetHint<int>("ssh.port", -1);
CIDR sshIP = crawledHost.GetHint<CIDR>("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.Host,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 (CIDR ip in crawledHost.IPAddresses)
{
foreach (int port in new int[] { 13022, 22 })
{
foreach (string password in defaultPasswords)
{
if (CanConnect(crawledHost, ip.Host, port, "skytron", password))
return true;
}
}
}
return false;
}
private static bool CanConnect(CrawledHost crawledHost, CIDR host, int port, string username, string password)
{
using (SshClient client = new SshClient(host.ToString(), port, username, password))
{
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1);
try
{
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);
client.Disconnect();
return true;
}
catch (SshException)
{
}
catch (SocketException)
{
return false;
}
}
return false;
}
}
}