Created
April 13, 2012 16:07
-
-
Save doskir/2377984 to your computer and use it in GitHub Desktop.
C# 4.0 Ping BSOD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.NetworkInformation; | |
using System.Text; | |
using System.Threading; | |
namespace NetMapper | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//giving it a large amount of ips to scan and setting the timeout to 1000ms will produce the BSOD | |
//BSOD message will be PROCESS_HAS_LOCKED_PAGES | |
string network = "10.0.*.*"; | |
var validHosts = FindValidHosts(network, 100); | |
Console.WriteLine("----------------"); | |
foreach(string ip in validHosts) | |
{ | |
Console.WriteLine(ip); | |
} | |
Console.WriteLine("Done"); | |
Console.ReadLine(); | |
} | |
static List<string> FindValidHosts(string ipRange,int numThreads) | |
{ | |
var ips = GenerateIPs(ipRange); | |
int neededPings = ips.Count; | |
int startedPings = 0; | |
PingReplyTracker replyTracker = new PingReplyTracker(); | |
while(replyTracker.FinishedCalls != neededPings && startedPings < neededPings) | |
{ | |
int runningCalls = startedPings - replyTracker.FinishedCalls; | |
int availableCalls = numThreads - runningCalls; | |
for(int i = 0;i < availableCalls;i++) | |
{ | |
AsyncPing(ips[startedPings], ref replyTracker); | |
startedPings++; | |
if (startedPings >= neededPings) | |
break; | |
} | |
Thread.Sleep(100); | |
} | |
return replyTracker.ValidIps; | |
} | |
static List<string> GenerateIPs(string iprange) | |
{ | |
List<string> ips = new List<string>(); | |
string[] foundOctets = iprange.Split(new string[] {"."}, StringSplitOptions.RemoveEmptyEntries); | |
string[] octets = new string[4]; | |
if (foundOctets.Length == 4 && !foundOctets.Contains("*")) | |
{ | |
//we were given a valid ip | |
return new List<string>() {iprange}; | |
} | |
for(int i = 0;i < 4;i++) | |
{ | |
if (i >= foundOctets.Length) | |
octets[i] = "*"; | |
else | |
octets[i] = foundOctets[i]; | |
} | |
StringBuilder ipMask = new StringBuilder(); | |
for(int i = 0;i < octets.Length;i++) | |
{ | |
if (octets[i] == "*") | |
{ | |
//loop from 1 to 254 | |
for (int octet = 1; octet <= 254; octet++) | |
{ | |
ips.AddRange(GenerateIPs(ipMask.ToString() + "." + octet)); | |
} | |
break; | |
} | |
else | |
{ | |
if (ipMask.Length != 0) | |
ipMask.Append("."); | |
ipMask.Append(octets[i]); | |
} | |
} | |
return ips; | |
} | |
private static byte[] pingBuffer = Encoding.ASCII.GetBytes("PING"); | |
static void AsyncPing(string ip,ref PingReplyTracker pingReplyTracker) | |
{ | |
Ping ping = new Ping(); | |
ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); | |
ping.SendAsync(ip, 1000, pingBuffer, pingReplyTracker); | |
} | |
static void ping_PingCompleted(object sender, PingCompletedEventArgs e) | |
{ | |
PingReplyTracker pingReplyTracker = (PingReplyTracker) e.UserState; | |
pingReplyTracker.FinishedCall(); | |
if (e.Reply.Status == IPStatus.Success) | |
pingReplyTracker.AddValidIp(e.Reply.Address.ToString()); | |
} | |
} | |
class PingReplyTracker | |
{ | |
public int FinishedCalls; | |
public List<string> ValidIps = new List<string>(); | |
public void FinishedCall() | |
{ | |
FinishedCalls++; | |
} | |
public void AddValidIp(string ip) | |
{ | |
ValidIps.Add(ip); | |
Console.WriteLine(ip); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment