249 lines
8.6 KiB
C#
249 lines
8.6 KiB
C#
// /**
|
|
// * 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 TestDefaults(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:
|
|
if (crawledHost.GetHint<string>("snmp.username", null) != null)
|
|
{
|
|
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;
|
|
}
|
|
return null;
|
|
default:
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|