broken
Harald Wolff 2019-03-21 07:43:32 +01:00
parent 0f12475001
commit e124f5e885
16 changed files with 678 additions and 446 deletions

View File

@ -29,25 +29,14 @@ using ln.types.sync;
using ln.skyscanner.crawl;
using System.Threading;
using System.Net.NetworkInformation;
using ln.skyscanner.crawl.tests;
using Castle.DynamicProxy;
namespace ln.skyscanner
{
class MainClass
{
private static void DumpPerfValues(PerfValue[] perfValues)
{
int n = 0;
Logging.Log("----------------------------------------------");
Logging.Log("Dumping {0} perfValues:", perfValues.Length);
foreach (PerfValue perfValue in perfValues)
{
Logging.Log("PerfValue: [{1,6}] {0}", perfValue, n);
n++;
}
Logging.Log("");
}
public static void Main(string[] args)
{
@ -57,81 +46,15 @@ namespace ln.skyscanner
Logger.ConsoleLogger.MaxLogLevel = LogLevel.INFO;
Initialize();
SkyScanner skyScanner = new SkyScanner(args);
skyScanner.Start();
}
public static void TestAuthKey(String filename,USMEndpoint v3endpoint)
private static void Initialize()
{
FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] source = new byte[fileStream.Length];
int nread = fileStream.Read(source, 0, source.Length);
fileStream.Close();
fileStream.Dispose();
ASN1Value asn = new ASN1Value(source);
USMMessage usm = new USMMessage(asn);
byte[] repro1 = asn.AsByteArray;
byte[] repro2 = ((ASN1Value)usm).AsByteArray;
Logging.Log(LogLevel.DEBUG, "Source: {0}", BitConverter.ToString(source));
Logging.Log(LogLevel.DEBUG, "Repro1: {0}", BitConverter.ToString(repro1));
Logging.Log(LogLevel.DEBUG, "Repro2: {0}", BitConverter.ToString(repro2));
if (!source.SequenceEqual(repro1))
Logging.Log(LogLevel.ERROR, "Repro1 does not match!");
else
Logging.Log(LogLevel.ERROR, "Repro1 matches!");
if (!source.SequenceEqual(repro2))
Logging.Log(LogLevel.ERROR, "Repro2 does not match!");
else
Logging.Log(LogLevel.ERROR, "Repro2 matches!");
if (!repro1.SequenceEqual(repro2))
Logging.Log(LogLevel.ERROR, "Repro1 != Repro2!");
else
Logging.Log(LogLevel.ERROR, "Repro1/2 match!");
usm.Dump();
usm.SecurityParameters.Dump();
byte[] auth1 = usm.SecurityParameters.msgAuthenticationParameters.Bytes;
usm.SecurityParameters.msgAuthenticationParameters.Bytes = new byte[12];
Logging.Log(LogLevel.DEBUG, "Source: {0}", BitConverter.ToString(source));
v3endpoint.RemoteEngineID = usm.SecurityParameters.msgAuthoritativeEngineID;
v3endpoint.CacheAuthoritativeEngineTime = usm.SecurityParameters.msgAuthoritativeEngineTime;
v3endpoint.CacheAuthoritativeEngineBoots = usm.SecurityParameters.msgAuthoritativeEngineBoots;
v3endpoint.LocalizeKeys();
v3endpoint.AuthenticateMessage(usm);
byte[] auth2 = usm.SecurityParameters.msgAuthenticationParameters.Bytes;
Logging.Log(LogLevel.DEBUG, "Authenticated: {0}", BitConverter.ToString(((ASN1Value)usm).AsByteArray));
Logging.Log(LogLevel.DEBUG, "Original Auth Token: {0}", BitConverter.ToString(auth1));
Logging.Log(LogLevel.DEBUG, "Calculated Auth Token: {0}", BitConverter.ToString(auth2));
if (auth1.SequenceEqual(auth2))
{
Logging.Log(LogLevel.DEBUG, "MATCH");
}
else
{
Logging.Log(LogLevel.DEBUG, "NO MATCH");
}
}
}

View File

@ -43,6 +43,8 @@ namespace ln.skyscanner
Arguments = args;
}
public SkyScanner()
{ }
public void Start()

View File

@ -1,312 +0,0 @@
// /**
// * File: CrawlHost.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 ln.types.threads;
using ln.logging;
using System.Net.NetworkInformation;
using ln.snmp.endpoint;
using ln.snmp;
using System.Net;
using ln.snmp.types;
using System.Collections.Generic;
using ln.snmp.rfc1213;
using ln.types;
using Newtonsoft.Json;
namespace ln.skyscanner.crawl
{
public class CrawlHost : PoolJob
{
[JsonIgnore]
public Crawler Crawler { get; }
[JsonIgnore]
public CrawledHost CrawledHost { get; }
public CrawlHost(Crawler crawler,CrawledHost crawledHost)
{
Crawler = crawler;
CrawledHost = crawledHost;
Name = String.Format("Host crawl {0} [ {1} ]", crawledHost.Name, crawledHost.PrimaryIP);
}
public override void RunJob()
{
DateTime dateTime = DateTime.Now;
if (crawlPing(CrawledHost))
{
if (CrawledHost.FirstSeen.Equals(DateTime.MinValue))
CrawledHost.FirstSeen = DateTime.Now;
CrawledHost.LastSeen = DateTime.Now;
try
{
CrawlSNMP(CrawledHost);
} catch (Exception e)
{
Logging.Log(LogLevel.ERROR, "CrawlHost: {0}: caught exception",CrawledHost.PrimaryIP);
Logging.Log(e);
}
}
else
{
}
CrawledHost.LastCheck = dateTime;
CrawledHost.NextCheck = dateTime + TimeSpan.FromHours(1);
}
public bool crawlPing(CrawledHost crawledHost)
{
using (Ping ping = new Ping())
{
setState("ICMP check");
int nSuccess = 0;
long roundTripTime = 0;
for (int n = 0; n < 10; n++)
{
setState("ICMP check [{0}/10]",n);
PingReply pingReply = ping.Send(crawledHost.PrimaryIP, 500);
if (pingReply.Status == IPStatus.Success)
{
nSuccess++;
roundTripTime += pingReply.RoundtripTime;
}
else if ((n > 3) && (nSuccess == 0))
{
break;
}
}
if (nSuccess > 0)
{
roundTripTime /= nSuccess;
crawledHost.SetHint("ping.success", true);
crawledHost.SetHint("ping.rta", (int)roundTripTime);
crawledHost.SetHint("ping.out_of_ten", nSuccess);
Logging.Log(LogLevel.INFO, "Host is reachable: {0} RTA={1}ms", crawledHost.PrimaryIP, roundTripTime);
}
else
{
crawledHost.SetHint("ping.success", false);
crawledHost.SetHint("ping.rta", null);
crawledHost.SetHint("ping.out_of_ten", 0);
Logging.Log(LogLevel.INFO, "Host is unreachable: {0}", crawledHost.PrimaryIP);
}
return crawledHost.GetHint<bool>("ping.success");
}
}
public void CrawlSNMP(CrawledHost crawledHost)
{
string[] communities = new string[] { "VhclfC7lfIojYZ", "Vhclf(C7$lfIojYZ", "ByFR4oW98hap", "qVy3hnZJ2fov" };
bool snmpDetected = false;
using (USMEndpoint v3endpoint = new USMEndpoint(Crawler.SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
setState("SNMPv3 check");
try
{
v3endpoint.QueryEngineID();
}
catch (TimeoutException)
{
}
if (v3endpoint.RemoteEngineID != null)
{
crawledHost.SetHint("snmp.version", 3);
Logging.Log(LogLevel.INFO, "{0}: SNMPv3 support detected", crawledHost.PrimaryIP);
bool replied = false;
int c = 0;
foreach (string community in communities)
{
c++;
setState("SNMPv3 check [{0}/{1}]",c,communities.Length);
v3endpoint.Username = "skytron";
v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
v3endpoint.AuthKeyPhrase = community;
try
{
Variable prID = v3endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.username", "skytron");
crawledHost.SetHint("snmp.authkey", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
replied = true;
break;
}
catch (TimeoutException)
{
}
}
if (replied)
{
snmpDetected = true;
}
}
}
if (!snmpDetected)
{
using (SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(Crawler.SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
setState("SNMPv2c check");
foreach (String community in communities)
{
v2endpoint.CommunityString = community;
try
{
Variable prID = v2endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 2);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
snmpDetected = true;
break;
}
catch (SnmpError)
{
}
catch (TimeoutException)
{
}
}
}
}
if (!snmpDetected)
{
using (SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(Crawler.SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
setState("SNMPv1 check");
foreach (String community in communities)
{
v1endpoint.CommunityString = community;
try
{
Variable prID = v1endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 1);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
snmpDetected = true;
break;
}
catch (SnmpError)
{
}
catch (TimeoutException)
{
}
}
}
}
if (!snmpDetected)
{
crawledHost.SetHint("snmp.version", null);
crawledHost.SetHint("snmp.username", null);
crawledHost.SetHint("snmp.authkey", null);
crawledHost.SetHint("snmp.community", null);
crawledHost.SetHint("snmp.sysObjectID", null);
}
else
{
setState("SNMP crawl");
try
{
using (SnmpEndpoint endpoint = crawledHost.GetSnmpEndpoint(Crawler.SNMPEngine))
{
try
{
Sequence[] VorIDs = endpoint.snmpWalk("1.3.6.1.2.1.1.9.1.2").ToArray();
string[] orids = new string[VorIDs.Length];
for (int n = 0; n < orids.Length; n++)
{
orids[n] = (VorIDs[n].Items[1] as ObjectIdentifier).AsString;
}
crawledHost.SetHint("snmp.orids", orids);
}
catch (TimeoutException)
{ }
List<String> ids = new List<string>(crawledHost.GetHint<String[]>("snmp.orids", new string[0]));
if (ids.Contains("1.3.6.1.2.1.31") || crawledHost.GetHint<string>("snmp.sysObjectID", "").Equals("1.3.6.1.4.1.14988.1"))
{
CrawlRFC1213(crawledHost, endpoint);
}
}
}
catch (Exception e)
{
Logging.Log(LogLevel.ERROR, "CrawlHost {0} caught exception: {1}", CrawledHost, e);
Logging.Log(e);
}
}
}
private void CrawlRFC1213(CrawledHost crawledHost, SnmpEndpoint endpoint)
{
RFC1213.Interface[] interfaces = RFC1213.GetInterfaces(endpoint);
crawledHost.Interfaces = interfaces;
foreach (CIDR ip in crawledHost.IPAddresses)
{
if (ip.MaskWidth != 32)
Crawler.CrawlPool.GetSubnet(ip.Network);
}
}
public override int GetHashCode()
{
return CrawledHost.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is CrawlHost)
{
CrawlHost you = obj as CrawlHost;
return Crawler.Equals(you.Crawler) && CrawledHost.Equals(you.CrawledHost);
}
return false;
}
}
}

View File

@ -0,0 +1,33 @@
// /**
// * File: CrawlTests.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 ln.skyscanner.crawl.tests;
using System.Collections.Generic;
namespace ln.skyscanner.crawl
{
public class CrawlTests
{
public CrawlTest[] Tests => crawlTests.ToArray();
private List<CrawlTest> crawlTests = new List<CrawlTest>();
public CrawlTests()
{
}
public void AddTest(CrawlTest crawlTest)
{
}
}
}

View File

@ -20,6 +20,7 @@ using System.Linq;
using ln.snmp;
using ln.snmp.endpoint;
using ln.snmp.rfc1213;
namespace ln.skyscanner.crawl
{
public class CrawledHost
@ -30,13 +31,22 @@ namespace ln.skyscanner.crawl
get => interfaces.ToArray();
set => interfaces = new List<RFC1213.Interface>(value);
}
public CIDR[] IPAddresses => interfaces.Select((intf) => intf.IPAddresses).SelectMany((i) => i).ToArray();
public String[] HWAddresses => interfaces.Select((intf) => intf.HWAddr).ToArray();
public String PrimaryHWAddr => HWAddresses.Where((hwa) => hwa != null && !String.Empty.Equals(hwa)).FirstOrDefault();
public String Name { get; set; }
public CIDR PrimaryIP { get; set; }
public CIDR[] IPAddresses
{
get
{
CIDR[] result = interfaces.Select((intf) => intf.IPAddresses).SelectMany((i) => i).ToArray();
if (result.Length == 0)
return new CIDR[] { PrimaryIP };
return result;
}
}
Dictionary<string, object> hints = new Dictionary<string, object>();
@ -45,9 +55,11 @@ namespace ln.skyscanner.crawl
public DateTime LastSeen;
public DateTime LastCheck;
public DateTime NextCheck;
public TimeSpan LastCheckTime;
public bool SnmpDetected => GetHint<int>("snmp.version", -1) != -1;
public bool SSHDetected => GetHint<int>("ssh.port", -1) != -1;
public bool RFC1213Detected => GetHint<bool>("rfc1213", false);
public CrawledHost()
{
@ -90,33 +102,6 @@ namespace ln.skyscanner.crawl
return hints.ContainsKey(name) && (hints[name] != null);
}
public SnmpEndpoint GetSnmpEndpoint(SNMPEngine engine)
{
int snmpVersion = GetHint<int>("snmp.version", -1);
switch (snmpVersion)
{
case 1:
SnmpV1Endpoint v1 = new SnmpV1Endpoint(engine, new IPEndPoint(PrimaryIP, 161));
v1.CommunityString = GetHint<string>("snmp.community");
return v1;
case 2:
SnmpV2Endpoint v2 = new SnmpV2Endpoint(engine, new IPEndPoint(PrimaryIP, 161));
v2.CommunityString = GetHint<string>("snmp.community");
return v2;
case 3:
USMEndpoint endpoint = new USMEndpoint(engine, new IPEndPoint(PrimaryIP, 161));
endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
endpoint.Username = GetHint<string>("snmp.username");
endpoint.AuthKeyPhrase = GetHint<string>("snmp.authkey");
return endpoint;
default:
return null;
}
}
public override string ToString()
{
return String.Format("[CrawledHost PrimaryIP={0} Name={1}]",PrimaryIP,Name);

View File

@ -48,7 +48,7 @@ namespace ln.skyscanner.crawl
public DiskObject<CrawlPool> _CrawlPool;
public CrawlPool CrawlPool => _CrawlPool?.Instance;
public SNMPEngine SNMPEngine { get; private set; }
//public SNMPEngine SNMPEngine { get; private set; }
[JsonConverter(typeof(StringEnumConverter))]
public ComponentState CrawlerState { get; private set; }
@ -83,11 +83,6 @@ namespace ln.skyscanner.crawl
{
stopping = false;
if (SNMPEngine == null)
SNMPEngine = new SNMPEngine();
SNMPEngine.Timeout = 1250;
if (_CrawlPool == null)
_CrawlPool = new DiskObject<CrawlPool>(String.Format("{0}/pool", BasePath));
@ -131,9 +126,6 @@ namespace ln.skyscanner.crawl
crawlThreadPool.Close();
SNMPEngine.Close();
SNMPEngine = null;
_CrawlPool.Save();
stopping = false;
@ -166,13 +158,13 @@ namespace ln.skyscanner.crawl
if (cidr.MaskWidth == 32)
{
CrawledHost crawledHost = CrawlPool.HostForIP(cidr);
CrawlHost crawlHost = new CrawlHost(this, crawledHost);
HostCrawl crawlHost = new HostCrawl(this, crawledHost);
crawlThreadPool.Enqueue(crawlHost);
}
else
{
Subnet subnet = CrawlPool.GetSubnet(cidr);
CrawlSubnet crawlSubnet = new CrawlSubnet(this, subnet);
SubnetCrawl crawlSubnet = new SubnetCrawl(this, subnet);
crawlThreadPool.Enqueue(crawlSubnet);
}
} catch (Exception e)
@ -195,7 +187,7 @@ namespace ln.skyscanner.crawl
foreach (Subnet subnet in CrawlPool.Subnets.ToArray())
{
if (subnet.NextScan < (DateTime.Now - TimeSpan.FromDays(1)))
if (subnet.NextScan < (DateTime.Now - TimeSpan.FromDays(1)) && subnet.Network.MaskWidth >= 24)
{
Crawl(subnet.Network);
}

75
crawl/HostCrawl.cs 100644
View File

@ -0,0 +1,75 @@
// /**
// * File: CrawlHost.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 ln.types.threads;
using ln.logging;
using System.Net.NetworkInformation;
using ln.snmp.endpoint;
using ln.snmp;
using System.Net;
using ln.snmp.types;
using System.Collections.Generic;
using ln.snmp.rfc1213;
using ln.types;
using Newtonsoft.Json;
using ln.skyscanner.crawl.tests;
using System.Runtime.Remoting.Messaging;
namespace ln.skyscanner.crawl
{
public class HostCrawl : PoolJob
{
[JsonIgnore]
public Crawler Crawler { get; }
[JsonIgnore]
public CrawledHost CrawledHost { get; }
public HostCrawl(Crawler crawler,CrawledHost crawledHost)
{
Crawler = crawler;
CrawledHost = crawledHost;
Name = String.Format("Host crawl {0} [ {1} ]", crawledHost.Name, crawledHost.PrimaryIP);
}
public override void RunJob()
{
State = "Prepare";
DateTime dateTime = DateTime.Now;
if (Crawling.Crawl(this))
{
if (CrawledHost.FirstSeen.Equals(DateTime.MinValue))
CrawledHost.FirstSeen = DateTime.Now;
CrawledHost.LastSeen = DateTime.Now;
}
CrawledHost.LastCheckTime = DateTime.Now - dateTime;
CrawledHost.LastCheck = dateTime;
CrawledHost.NextCheck = dateTime + TimeSpan.FromHours(1);
}
public override int GetHashCode()
{
return CrawledHost.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is HostCrawl)
{
HostCrawl you = obj as HostCrawl;
return Crawler.Equals(you.Crawler) && CrawledHost.Equals(you.CrawledHost);
}
return false;
}
}
}

View File

@ -17,7 +17,7 @@ using Newtonsoft.Json;
using System.Linq;
namespace ln.skyscanner.crawl
{
public class CrawlSubnet : PoolJob
public class SubnetCrawl : PoolJob
{
[JsonIgnoreAttribute]
public Crawler Crawler { get; }
@ -26,7 +26,7 @@ namespace ln.skyscanner.crawl
public CIDR Network { get; }
public CrawlSubnet(Crawler crawler, Subnet subnet)
public SubnetCrawl(Crawler crawler, Subnet subnet)
{
Crawler = crawler;
Subnet = subnet;
@ -34,7 +34,7 @@ namespace ln.skyscanner.crawl
Name = String.Format("Subnet crawl {0}", subnet.Network);
}
public CrawlSubnet(Crawler crawler, Subnet subnet,CIDR network)
public SubnetCrawl(Crawler crawler, Subnet subnet,CIDR network)
{
Crawler = crawler;
Subnet = subnet;
@ -47,7 +47,7 @@ namespace ln.skyscanner.crawl
{
if (Network.MaskWidth < 26)
{
SplitJob(Network.Divide(26 - Network.MaskWidth).Select((n) => new CrawlSubnet(Crawler, Subnet, n)).ToArray());
SplitJob(Network.Divide(26 - Network.MaskWidth).Select((n) => new SubnetCrawl(Crawler, Subnet, n)).ToArray());
return;
}
@ -107,9 +107,9 @@ namespace ln.skyscanner.crawl
}
public override bool Equals(object obj)
{
if (obj is CrawlSubnet)
if (obj is SubnetCrawl)
{
CrawlSubnet you = obj as CrawlSubnet;
SubnetCrawl you = obj as SubnetCrawl;
return Crawler.Equals(you.Crawler) && Subnet.Equals(you.Subnet) && Network.Equals(you.Network);
}
return false;

View File

@ -0,0 +1,64 @@
// /**
// * File: CrawlTest.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 System.Collections.Generic;
using System.Reflection;
using ln.types;
namespace ln.skyscanner.crawl.tests
{
public static class Crawling
{
public static bool Crawl(HostCrawl hostCrawl)
{
hostCrawl.setState("ICMP");
if (!ICMP.IsReachable(hostCrawl.CrawledHost))
{
return false;
}
if (hostCrawl.AbortRequested)
return false;
hostCrawl.setState("SSH");
if (SSH.CanConnect(hostCrawl.CrawledHost))
{
// ToDo: Extract more details...
}
if (hostCrawl.AbortRequested)
return false;
hostCrawl.setState("SNMP");
if (SNMP.HasSNMP(hostCrawl.CrawledHost))
{
if (hostCrawl.AbortRequested)
return false;
hostCrawl.setState("RFC1213");
RFC1213.Check(hostCrawl.CrawledHost);
}
foreach (CIDR ip in hostCrawl.CrawledHost.IPAddresses)
{
if (ip.MaskWidth != 32)
hostCrawl.Crawler.CrawlPool.GetSubnet(ip.Network);
}
return true;
}
}
}

View File

@ -0,0 +1,62 @@
// /**
// * File: ICMP.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 System.Net.NetworkInformation;
namespace ln.skyscanner.crawl.tests
{
public static class ICMP
{
public static bool IsReachable(CrawledHost crawledHost)
{
return true;
//using (Ping ping = new Ping())
//{
// HostCrawl.setState("ICMP check");
// int nSuccess = 0;
// long roundTripTime = 0;
// for (int n = 0; n < 10; n++)
// {
// HostCrawl.setState("ICMP check [{0}/10]", n);
// PingReply pingReply = ping.Send(CrawledHost.PrimaryIP, 500);
// if (pingReply.Status == IPStatus.Success)
// {
// nSuccess++;
// roundTripTime += pingReply.RoundtripTime;
// }
// else if ((n > 3) && (nSuccess == 0))
// {
// break;
// }
// }
// if (nSuccess > 0)
// {
// roundTripTime /= nSuccess;
// CrawledHost.SetHint("ping.success", true);
// CrawledHost.SetHint("ping.rta", (int)roundTripTime);
// CrawledHost.SetHint("ping.out_of_ten", nSuccess);
// }
// else
// {
// CrawledHost.SetHint("ping.success", false);
// CrawledHost.SetHint("ping.rta", null);
// CrawledHost.SetHint("ping.out_of_ten", 0);
// }
// return CrawledHost.GetHint<bool>("ping.success");
//}
}
}
}

View File

@ -0,0 +1,39 @@
// /**
// * File: RFC1213.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 ln.snmp.endpoint;
using ln.snmp;
using System.Linq;
using ln.types;
namespace ln.skyscanner.crawl.tests
{
public static class RFC1213
{
public static void Check(CrawledHost crawledHost)
{
String[] orIDS = crawledHost.GetHint<String[]>("snmp.orids", new string[0]);
String prID = crawledHost.GetHint<string>("snmp.sysObjectID", "");
if (orIDS.Contains("1.3.6.1.2.1.31") || prID.Equals("1.3.6.1.4.1.14988.1"))
{
using (SnmpInterface snmp = SNMP.GetSnmpInterface(crawledHost))
{
ln.snmp.rfc1213.RFC1213.Interface[] interfaces = ln.snmp.rfc1213.RFC1213.GetInterfaces(snmp);
if (interfaces.Length > 0)
{
crawledHost.Interfaces = interfaces;
crawledHost.SetHint("rfc1213", true);
}
}
}
}
}
}

244
crawl/tests/SNMP.cs 100644
View File

@ -0,0 +1,244 @@
// /**
// * File: SNMP.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 System.Net;
using ln.snmp;
using ln.snmp.endpoint;
using ln.types;
using ln.snmp.types;
using System.Runtime.Remoting.Messaging;
namespace ln.skyscanner.crawl.tests
{
public static class SNMP
{
static string[] defaultCommunities = new string[] { "VhclfC7lfIojYZ", "Vhclf(C7$lfIojYZ", "ByFR4oW98hap", "qVy3hnZJ2fov" };
public static bool HasSNMP(CrawledHost crawledHost)
{
if (!TestCurrentHints(crawledHost))
{
}
return false;
}
public static bool TestCurrentHints(CrawledHost crawledHost)
{
SnmpInterface snmpEndpoint = GetSnmpInterface(crawledHost);
if (snmpEndpoint != null)
{
using (snmpEndpoint)
{
try
{
Variable prID = snmpEndpoint.snmpGet("1.3.6.1.2.1.1.2.0");
} catch (SnmpError)
{
return false;
} catch (TimeoutException)
{
return false;
}
return true;
}
}
return false;
}
private static bool TestSnmpV3(CrawledHost crawledHost)
{
CIDR[] ips = crawledHost.IPAddresses;
foreach (CIDR ip in crawledHost.IPAddresses)
{
using (USMEndpoint v3endpoint = new USMEndpoint(SNMPEngine.DefaultEngine, new IPEndPoint(ip, 161)))
{
try
{
v3endpoint.QueryEngineID();
}
catch (TimeoutException)
{
}
if (v3endpoint.RemoteEngineID != null)
{
crawledHost.SetHint("snmp.version", 3);
bool replied = false;
int c = 0;
foreach (string community in defaultCommunities)
{
c++;
v3endpoint.Username = "skytron";
v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
v3endpoint.AuthKeyPhrase = community;
try
{
Variable prID = v3endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.username", "skytron");
crawledHost.SetHint("snmp.authkey", community);
crawledHost.SetHint("snmp.address", ip);
return true;
}
catch (TimeoutException)
{
}
}
}
}
}
return false;
}
private static bool TestSnmpV2(CrawledHost crawledHost)
{
CIDR[] ips = crawledHost.IPAddresses;
foreach (CIDR ip in crawledHost.IPAddresses)
{
using (SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
foreach (String community in defaultCommunities)
{
v2endpoint.CommunityString = community;
try
{
Variable prID = v2endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 2);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.address", ip);
return true;
}
catch (SnmpError)
{
}
catch (TimeoutException)
{
}
}
}
}
return false;
}
private static bool TestSnmpV1(CrawledHost crawledHost)
{
CIDR[] ips = crawledHost.IPAddresses;
foreach (CIDR ip in crawledHost.IPAddresses)
{
using (SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
foreach (String community in defaultCommunities)
{
v1endpoint.CommunityString = community;
try
{
Variable prID = v1endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 1);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.address", ip);
return true;
}
catch (SnmpError)
{
}
catch (TimeoutException)
{
}
}
}
}
return false;
}
public static bool TestDefaults(CrawledHost crawledHost)
{
if (TestSnmpV3(crawledHost) ? true : TestSnmpV2(crawledHost) ? true : TestSnmpV1(crawledHost))
{
using (SnmpInterface snmp = GetSnmpInterface(crawledHost))
{
Variable prID = snmp.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
try
{
Sequence[] seqORids = snmp.snmpWalk("1.3.6.1.2.1.1.9.1.2").ToArray();
string[] ORids = new string[seqORids.Length];
for (int n = 0; n < ORids.Length; n++)
{
ORids[n] = (seqORids[n].Items[1] as ObjectIdentifier).AsString;
}
crawledHost.SetHint("snmp.orids", ORids);
}
catch (TimeoutException)
{ }
}
return true;
}
else
{
crawledHost.SetHint("snmp.version", null);
crawledHost.SetHint("snmp.username", null);
crawledHost.SetHint("snmp.authkey", null);
crawledHost.SetHint("snmp.community", null);
crawledHost.SetHint("snmp.sysObjectID", null);
return false;
}
}
public static SnmpInterface GetSnmpInterface(CrawledHost crawledHost)
{
int snmpVersion = crawledHost.GetHint<int>("snmp.version", -1);
CIDR snmpAddress = crawledHost.GetHint<CIDR>("snmp.address", crawledHost.PrimaryIP);
switch (snmpVersion)
{
case 1:
SnmpV1Endpoint v1 = new SnmpV1Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(snmpAddress, 161));
v1.CommunityString = crawledHost.GetHint<string>("snmp.community");
return v1;
case 2:
SnmpV2Endpoint v2 = new SnmpV2Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(snmpAddress, 161));
v2.CommunityString = crawledHost.GetHint<string>("snmp.community");
return v2;
case 3:
USMEndpoint endpoint = new USMEndpoint(SNMPEngine.DefaultEngine, new System.Net.IPEndPoint(snmpAddress, 161));
endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
endpoint.Username = crawledHost.GetHint<string>("snmp.username");
endpoint.AuthKeyPhrase = crawledHost.GetHint<string>("snmp.authkey");
return endpoint;
default:
return null;
}
}
}
}

106
crawl/tests/SSH.cs 100644
View File

@ -0,0 +1,106 @@
// /**
// * 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.ToString(),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.ToString(), port, "skytron", password))
return true;
}
}
}
return false;
}
private static bool CanConnect(CrawledHost crawledHost, String host, int port, string username, string password)
{
using (SshClient client = new SshClient(host, port, username, password))
{
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1);
try
{
client.Connect();
crawledHost.SetHint("ssh.port", client.ConnectionInfo.Port);
crawledHost.SetHint("ssh.ip", client.ConnectionInfo.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;
}
}
}

View File

@ -36,6 +36,10 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Castle.Core">
<HintPath>..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
@ -55,8 +59,13 @@
<Compile Include="http\SkyScannerHttpManagement.cs" />
<Compile Include="http\CrawlerApi.cs" />
<Compile Include="crawl\CrawlPool.cs" />
<Compile Include="crawl\CrawlSubnet.cs" />
<Compile Include="crawl\CrawlHost.cs" />
<Compile Include="crawl\SubnetCrawl.cs" />
<Compile Include="crawl\HostCrawl.cs" />
<Compile Include="crawl\tests\Crawling.cs" />
<Compile Include="crawl\tests\SSH.cs" />
<Compile Include="crawl\tests\SNMP.cs" />
<Compile Include="crawl\tests\ICMP.cs" />
<Compile Include="crawl\tests\RFC1213.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@ -132,6 +141,7 @@
<Folder Include="templates\static\" />
<Folder Include="templates\static\dist\" />
<Folder Include="templates\static\css\" />
<Folder Include="crawl\tests\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.snmp\ln.snmp.csproj">

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.3.1" targetFramework="net47" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net47" />
<package id="SSH.NET" version="2016.1.0" targetFramework="net47" />
</packages>

View File

@ -51,6 +51,11 @@
<script type="text/javascript">
function scan(primaryIP)
{
skyapi().call("api/crawler","Crawl", [ primaryIP ]);
}
function details(primaryIP)
{
var crawledHost = skyapi().call("api/crawler","GetHostByIP", [ primaryIP ]);
@ -112,7 +117,9 @@
{ title: "Zuletzt gesehen", data: "LastSeen", sorter: "", width: 180, formatter: "datetime", formatterParams: { inputFormat: "", outputFormat: "DD.MM.YYYY hh:mm:ss", invalidPlaceHolder: "-" } },
{ title: "Letzter Scan", data: "LastCheck", sorter: "", width: 180, formatter: "datetime", formatterParams: { inputFormat: "", outputFormat: "DD.MM.YYYY hh:mm:ss", invalidPlaceHolder: "-" } },
{ title: "Nächster Scan",data: "NextCheck",sorter: "",width: 180,formatter: "datetime",formatterParams: {inputFormat: "",outputFormat: "DD.MM.YYYY hh:mm:ss",invalidPlaceHolder: "-"} },
{ title: "SNMP", data: "SnmpDetected", width: 90 }
{ title: "SNMP", data: "SSHDetected", width: 90 },
{ title: "SSH", data: "SnmpDetected", width: 90 },
{ title: "RFC1213", data: "RFC1213Detected", width: 90 }
];
$("#hostTable").DataTable( {
@ -123,12 +130,13 @@
targets: 0,
data: null,
defaultContent: "<button>Details</button>"
defaultContent: "<button id='details'>Details</button><button id='scan'>Scan</button>"
}
]
});
$("#hostTable tbody").on( "click", "button", function(){ details( $("#hostTable").DataTable().row( $(this).parents('tr') ).data().PrimaryIP ); } );
$("#hostTable tbody").on( "click", "button#details", function(){ details( $("#hostTable").DataTable().row( $(this).parents('tr') ).data().PrimaryIP ); } );
$("#hostTable tbody").on( "click", "button#scan", function(){ scan( $("#hostTable").DataTable().row( $(this).parents('tr') ).data().PrimaryIP ); } );
refreshHostTable();
skyapi().addRefresh( refreshHostTable );