static void ScanFromPort() { foreach (var p in ScannerConfiger.PortList) { foreach (var r in ScannerConfiger.IPRange) { for (uint i = r.Start; i <= r.End; i++) { lock (m_obj_sync) m_nTaskCount++; Program.Scan(ScannerStatic.Reverse(i), p); } } } }
static void ScanFromRnd() { foreach (var r in ScannerConfiger.IPRange) { for (uint i = r.Start; i <= r.End; i++) { foreach (var p in ScannerConfiger.PortList) { Program.EnCache(ScannerStatic.Reverse(i), p, false); } } } Program.FlushCache(false); }
public static void OutToFile(string strIP, string strPort, string strType, string strProto, string strBanner, int nLine, byte[] byBuffer, int nLen) { if (m_writer == null) { return; } lock (m_writer) { m_writer.WriteLine( ScannerConfiger.OutputType == "JSON" ? ScannerStatic.GetJSONString(strIP, strPort, strType, strProto, strBanner, nLine, byBuffer, nLen) : ScannerStatic.GetCSVString(strIP, strPort, strType, strProto, strBanner, nLine, byBuffer, nLen) ); m_writer.Flush(); } }
static void OutResult(string strIP, string strPort, string strProtoType, string strProtoFlag, string strBanner, int nLine, byte[] byBuffer, int nLen) { if (ScannerConfiger.ConsoleDisplay > 0) { lock (m_obj_sync) { Console.ForegroundColor = ConsoleColor.Cyan; Console.Write((strIP + ":" + strPort).PadRight(22)); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("[(" + strProtoType + ")" + strProtoFlag + "]"); if (!string.IsNullOrEmpty(strBanner) && ScannerConfiger.ConsoleDisplay > 1) { Console.WriteLine("[RegLine:" + nLine + "]"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("[\r\n" + (strBanner.Length > 256 ? (strBanner.Substring(0, 256).TrimEnd('\r') + "...") : strBanner) + "\r\n]"); } else { Console.WriteLine(); } Console.ForegroundColor = m_clr; } } ScannerStatic.OutToFile(strIP, strPort, strProtoType, strProtoFlag, strBanner, nLine, byBuffer, nLen); }
public static uint IPToINT(string strIP) { return(ScannerStatic.IPToINT(strIP, false)); }
public static PortScanner InitScanner(string[] args, ProbeConfiger pc) { PortScanner ps = null; int nIndex = 0; string strScanner = "tcp"; string strCurrentArg = string.Empty; try { while (nIndex < args.Length) { strCurrentArg = args[nIndex]; switch (args[nIndex].ToLower()) { case "-h": ScannerStatic.GetIpRange(args[++nIndex].Split(',')); break; case "-hf": ScannerStatic.GetIpRange(File.ReadAllLines(args[++nIndex])); break; case "-p": ScannerStatic.GetPortList(args[++nIndex].Split(',')); break; case "-pf": ScannerStatic.GetPortList(File.ReadAllLines(args[++nIndex])); break; case "-pr": ScannerConfiger.ProbesCount = (int)uint.Parse(args[++nIndex]); break; case "-np": ScannerConfiger.IsUserNullProbe = true; break; case "-i": ScannerConfiger.IsIcmp = true; break; case "-is": ScannerConfiger.IsIcmp = ScannerConfiger.IsIcmpScan = true; break; case "-t": ScannerConfiger.Timeout = (int)uint.Parse(args[++nIndex]) * 1000; break; case "-tt": ScannerConfiger.TotalTimeout = (int)uint.Parse(args[++nIndex]) * 1000; break; case "-r": ScannerConfiger.Retry = (int)uint.Parse(args[++nIndex]); break; case "-con": ScannerConfiger.Concurrent = (int)uint.Parse(args[++nIndex]); break; case "-f": string[] strsF = args[++nIndex].ToLower().Split(':'); ScannerConfiger.OutputType = strsF[0].ToLower() == "json" ? "JSON" : "CSV"; ScannerConfiger.OutputFields = strsF[1].Split(','); break; case "-ss": strScanner = "syn"; break; case "-su": strScanner = "udp"; break; case "-smb": ScannerConfiger.IsSMB = true; strScanner = "smb"; break; case "-o": m_writer = new StreamWriter(args[++nIndex], true, Encoding.UTF8); break; case "-stop": foreach (var v in args[++nIndex].Split(',')) { ScannerConfiger.StopProto.Add(v.Trim()); } break; case "-order": ScannerConfiger.ScanningOrder = args[++nIndex]; break; case "-cd": ScannerConfiger.ConsoleDisplay = (int.Parse(args[++nIndex])); break; case "-delay": ScannerConfiger.Delay = (int)(uint.Parse(args[++nIndex])) * 1000; break; case "-cn": using (StreamWriter writer = new StreamWriter(args[2 + nIndex], false, Encoding.UTF8)) { writer.Write(ProbeConfiger.ConvertNmapProbe(File.ReadAllText(args[++nIndex]))); } nIndex++; break; case "-st": break; default: throw new ArgumentException("Invalid argument [" + strCurrentArg + "]"); } nIndex++; } } catch (Exception ex) { throw new Exception("[" + strCurrentArg + "]", ex); } if (ScannerConfiger.IPRange.Count == 0) { throw new ArgumentException("Can not found the target to scan. Please use [-h] or [-hf] to specify the value"); } switch (strScanner) { case "tcp": ps = new TCPScanner(ScannerConfiger.Concurrent, pc); break; case "udp": ScannerConfiger.ProtocolType = "UDP"; ps = new UDPScanner(ScannerConfiger.Concurrent, pc); break; case "syn": ps = new SYNScanner(ScannerConfiger.Concurrent, pc); break; case "smb": ps = new SmbScanner(ScannerConfiger.Concurrent); break; } if (ScannerConfiger.IsSMB) { ScannerConfiger.PortList = new int[] { 445 } } ; ScannerStatic.InitWriter(); return(ps); }
public static void GetIpRange(string[] strIps) { List <Range> lst = new List <Range>(); Regex reg = new Regex(@"^(\d{1,3}\.){3}\d{1,3}"); foreach (var v in strIps) { string strIP = string.Empty; string[] strLine = v.Trim().Trim(',').Trim().Split(m_split); if (strLine.Length == 0) { continue; } Range rg = new Range(); try { if (!reg.IsMatch(v.Trim())) { foreach (var ip in Dns.GetHostEntry(v.Trim()).AddressList) { if (!ip.IsIPv6LinkLocal) { strIP = ip.ToString(); if (!ScannerConfiger.DomainDic.ContainsKey(strIP)) { rg.Start = rg.End = ScannerStatic.IPToINT(strIP, true); ScannerConfiger.DomainDic.Add(strIP, new HashSet <string>()); } ScannerConfiger.DomainDic[strIP].Add(v.Trim()); break; } } if (rg.Start == 0) { throw new Exception("Can not get IPV4"); } } else if (strLine.Length == 1) { rg.Start = rg.End = ScannerStatic.IPToINT(strLine[0].Trim(), true); } else if (strLine.Length == 2) { rg.Start = ScannerStatic.IPToINT(strLine[0].Trim(), true); if (v.Contains("-")) { rg.End = ScannerStatic.IPToINT(strLine[1].Trim(), true); } else { int nBit = (32 - int.Parse(strLine[1])); if (nBit < 0) { throw new Exception(); } if (nBit == 0) { rg.End = rg.Start; } else { uint nMask = 0xFFFFFFFF << nBit; rg.Start = (rg.Start & nMask) + 1; rg.End = (rg.Start | (~nMask) - 1); } } if (rg.End < rg.Start) { throw new ArgumentException("Invalid IP range"); } } else { throw new FormatException("Format error"); } lst.Add(rg); } catch (Exception ex) { throw new Exception("Error:" + v + "\r\n" + ex.Message, ex); } } ScannerConfiger.IPRange = lst; }
static void Main(string[] args) { m_clr = Console.ForegroundColor; //args = "-h 192.140.145.127 -smb".Split(' '); //args = "-h 192.168.10.2 -p 80 -r 10 -tt 10000".Split(' '); //args = "-hf ./vultrcidrs.txt -order port -st -cd 0 -con 12000".Split(' '); //args = "-h 46.101.181.209/0 -order port -st -cd 0 -con 8000".Split(' '); //args = "-h 45.32.249.1/24 -order port -st -cd 1".Split(' '); if (args.Length < 2) { ScannerStatic.ShowInfo(); return; } if (!File.Exists("./config_defports.st")) { ConfigerHelper.CreateConfigFile("./config_defports.st", false); } if (!File.Exists("./config_probes.st")) { ConfigerHelper.CreateConfigFile("./config_probes.st", true); } m_pc = new ProbeConfiger( File.ReadAllText("./config_probes.st"), File.ReadAllText("./config_defports.st") ); try { m_scanner = ScannerStatic.InitScanner(args, m_pc); m_scanner.Completed += new ScanEventHandler(m_scanner_Completed); } catch (Exception ex) { var clr = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message + "\r\n\t" + (ex.InnerException != null ? ex.InnerException.Message : "")); Console.ForegroundColor = clr; Console.Write("Show help info?(y/n):"); if (Console.ReadKey().KeyChar == 'y') { ScannerStatic.ShowInfo(); } return; } DateTime dt = DateTime.Now; m_se = new Semaphore(ScannerConfiger.Concurrent, ScannerConfiger.Concurrent); new Thread(Program.ShowPregress) { IsBackground = true }.Start(); if (ScannerConfiger.IsIcmp) { IcmpScanner icmp = new IcmpScanner(ScannerConfiger.Concurrent); icmp.Completed += new IcmpEventHandler(icmp_Completed); new Thread(Program.IcmpFlushCallBack) { IsBackground = true }.Start(); foreach (var v in ScannerConfiger.IPRange) { for (uint i = v.Start; i <= v.End; i++) { m_se.WaitOne(); lock (m_obj_sync) { m_nRunning++; m_nTaskCount++; } icmp.Ping(new IPAddress(ScannerStatic.Reverse(i)), ScannerConfiger.Timeout, ScannerConfiger.Retry); } } } else { if (ScannerConfiger.ScanningOrder == "host") { Program.ScanFromHost(); } else if (ScannerConfiger.ScanningOrder == "port") { Program.ScanFromPort(); } else { Program.ScanFromRnd(); } } while (m_nTaskCount != m_nRuned) { Thread.Sleep(500); } Console.WriteLine("Queue:" + (m_nTaskCount - m_nRuned) + " Running:" + m_nRunning + " Runed:" + m_nRuned + " Result:" + m_nResult + " Jumped:" + m_nJumped); string strTimeSub = DateTime.Now.Subtract(dt).ToString(); strTimeSub = strTimeSub.Substring(0, strTimeSub.LastIndexOf(':') + 3); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("TIME: [" + dt.ToString("yy-MM-dd_HH:mm:ss") + "]-[" + DateTime.Now.ToString("yy-MM-dd_HH:mm:ss") + "] [" + strTimeSub + "]"); Console.ForegroundColor = m_clr; //Console.ReadKey(); }