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); }
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(); }