示例#1
0
        public UDPScanner(int nMaxTask, ProbeConfiger probes)
        {
            m_se               = new Semaphore(nMaxTask, nMaxTask);
            m_configer         = probes;
            m_que_sae          = new Queue <SocketAsyncEventArgs>();
            m_dic_task_running = new Dictionary <string, UDPScanTaskInfo>();
            m_que_task         = new Queue <UDPScanTaskInfo>();
            for (int i = 0; i < nMaxTask; i++)
            {
                UDPScanTaskInfo ti = new UDPScanTaskInfo();
                ti.TaskID         = (uint)i + 1;
                ti.SendDatas      = new List <byte[]>();
                ti.SendDatasQueue = new Queue <byte[]>();
                m_que_task.Enqueue(ti);
            }
            m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            m_sock.Bind(new IPEndPoint(IPAddress.Any, 0));
            SocketAsyncEventArgs sae = new SocketAsyncEventArgs();

            sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
            sae.SetBuffer(new byte[65535], 0, 65535);
            sae.UserToken      = m_sock;
            sae.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            if (!m_sock.ReceiveFromAsync(sae))
            {
                IOProcessPool.QueueWork(this.ProcessRecv, sae);
            }
            new Thread(this.CheckTimeout)
            {
                IsBackground = true
            }.Start();
        }
示例#2
0
 public TCPScanner(int nMaxTask, ProbeConfiger probes)
 {
     if (nMaxTask > 60000 || nMaxTask < 1)
     {
         throw new ArgumentOutOfRangeException("the MaxTask must be between 1 and 30000");
     }
     m_configer        = probes;
     m_que_task        = new Queue <TCPScanTaskInfo>();
     m_hs_task_running = new HashSet <TCPScanTaskInfo>();
     m_que_sae         = new Queue <SocketAsyncEventArgs>();
     for (int i = 0; i < nMaxTask; i++)
     {
         TCPScanTaskInfo ti = new TCPScanTaskInfo();
         ti.TaskID = (uint)(i + 1);
         SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
         sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         sae.SetBuffer(new byte[2048], 0, 2048);
         sae.UserToken = ti;
         ti.RecvSAE    = sae;
         m_que_task.Enqueue(ti);
     }
     m_se             = new Semaphore(nMaxTask, nMaxTask);
     m_thread_timeout = new Thread(this.CheckTimeout);
     m_thread_timeout.IsBackground = true;
     m_thread_timeout.Start();
 }
        public SYNScanner(int nMaxTask, ProbeConfiger probes, EndPoint bindEndPoint)
        {
            if (nMaxTask > 60000 || nMaxTask < 1)
            {
                throw new ArgumentOutOfRangeException("the MaxTask must be between 1 and 30000");
            }
            m_probes = probes;
            if (bindEndPoint == null)
            {
                foreach (var v in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (v.IsIPv6LinkLocal || v.IsIPv6Multicast || v.IsIPv6SiteLocal)
                    {
                        continue;
                    }
                    bindEndPoint = new IPEndPoint(v, 0);
                }
            }
            m_rnd                    = new Random();
            m_dic_uid                = new Dictionary <uint, SYNScanTaskInfo>();// new Dictionary<uint, uint>();
            m_dic_task_running       = new Dictionary <uint, SYNScanTaskInfo>();
            m_tcp_scanner            = new TCPScanner(nMaxTask, probes);
            m_tcp_scanner.Completed += new ScanEventHandler(m_tcp_Completed);
            m_sock_bind              = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_sock_bind.Bind(bindEndPoint);
            bindEndPoint = m_sock_bind.LocalEndPoint;
            m_strLocalIP = bindEndPoint.ToString().Split(':')[0];
            m_uLocalIP   = RAWDefine.IPToINT(m_strLocalIP);
            m_nLocalPort = ushort.Parse(bindEndPoint.ToString().Split(':')[1]);
            m_se         = new Semaphore(nMaxTask, nMaxTask);

            m_sock_raw = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            m_sock_raw.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
            m_sock_raw.Bind(bindEndPoint);
            m_sock_raw.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, null);

            m_que_task = new Queue <SYNScanTaskInfo>();
            m_que_sae  = new Queue <SocketAsyncEventArgs>();
            for (int i = 0; i < nMaxTask; i++)
            {
                SYNScanTaskInfo ti = new SYNScanTaskInfo();
                ti.TaskID    = (uint)((i + 1) << 8);
                ti.SYNPacket = new byte[40];
                m_que_task.Enqueue(ti);
            }

            SocketAsyncEventArgs sae = new SocketAsyncEventArgs();

            sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
            sae.SetBuffer(new byte[65535], 0, 65535);
            sae.UserToken = m_sock_raw;
            if (!m_sock_raw.ReceiveAsync(sae))
            {
                IOProcessPool.QueueWork(this.ProcessRecv, sae);
            }
            m_thread_timeout = new Thread(this.CheckTimeout);
            m_thread_timeout.IsBackground = true;
            m_thread_timeout.Start();
        }
示例#4
0
 public SYNScanner(int nMaxTask, ProbeConfiger probes) : this(nMaxTask, probes, null)
 {
 }
示例#5
0
        public MatchResult MatchData(byte[] byBuffer, int nLen, int nPort, ProbeType type, ProbeInfo probeInfo)
        {
            StringBuilder sb_m      = new StringBuilder();
            StringBuilder sb_r      = new StringBuilder();
            string        strMatch  = string.Empty;
            string        strResult = string.Empty;

            for (int i = 0; i < nLen; i++)
            {
                sb_m.Append((char)byBuffer[i]);
                sb_r.Append(ProbeConfiger.ByteToChar(byBuffer[i]));
            }
            strMatch  = sb_m.ToString();
            strResult = sb_r.ToString();

            var probesDic = this._ProbesDictionary;

            if (probeInfo != null)
            {
                foreach (var r in probeInfo.RegexList)
                {
                    if (r.Regex.IsMatch(strMatch))
                    {
                        return(new MatchResult(r.Name, strResult, r.RegLine));
                    }
                }
            }
            HashSet <ProbeInfo> hs = new HashSet <ProbeInfo>();

            hs.Add(probeInfo);
            if (probesDic.ContainsKey(nPort))
            {
                foreach (var p in probesDic[nPort])
                {
                    if (!p.IsTcp)
                    {
                        continue;
                    }
                    if (type == ProbeType.Tcp && !p.IsTcp)
                    {
                        continue;
                    }
                    if (type == ProbeType.Udp && p.IsTcp)
                    {
                        continue;
                    }
                    if (hs.Contains(p))
                    {
                        continue;
                    }
                    foreach (var r in p.RegexList)
                    {
                        if (r.Regex.IsMatch(strMatch))
                        {
                            return(new MatchResult(r.Name, strResult, r.RegLine));
                        }
                    }
                    hs.Add(p);
                }
            }
            var allProbes = this._AllProbes;

            foreach (var p in allProbes)
            {
                if (type == ProbeType.Tcp && !p.IsTcp)
                {
                    continue;
                }
                if (type == ProbeType.Udp && p.IsTcp)
                {
                    continue;
                }
                if (hs.Contains(p))
                {
                    continue;
                }
                foreach (var r in p.RegexList)
                {
                    if (r.Regex.IsMatch(strMatch))
                    {
                        return(new MatchResult(r.Name, strResult, r.RegLine));
                    }
                }
            }
            return(new MatchResult(null, strResult, 0));
        }
示例#6
0
 public void LoadNmapProbeConfig(string strNmapProbesConfig)
 {
     this.LoadProbeConfig(ProbeConfiger.ConvertNmapProbe(strNmapProbesConfig));
 }
示例#7
0
        public void LoadProbeConfig(string strConfigProbes)
        {
            string[]         strLines = strConfigProbes.Split('\n');
            List <ProbeInfo> lst      = new List <ProbeInfo>();
            ProbeInfo        pi       = null;

            char[] spliter = new char[] { ',', '-' };
            int    nLine   = 0;
            string strLine = string.Empty;

            try {
                foreach (var n in strLines)
                {
                    nLine++;
                    strLine = n.Trim();
                    if (strLine == string.Empty || strLine[0] == '#')
                    {
                        continue;
                    }
                    if (strLine.StartsWith("**[PROBE_S]**"))
                    {
                        pi = new ProbeInfo();
                    }
                    if (strLine.StartsWith("**[PROBE_E]**"))
                    {
                        if (pi.Ports.Count == 0)
                        {
                            pi.Ports.Add(0);
                        }
                        lst.Add(pi);
                        pi = null;
                    }
                    if (strLine.StartsWith("PROBE_DATA"))
                    {
                        var m = Regex.Match(strLine, @"\[(.*?)\]\s*\{(.*)\}");
                        pi.IsTcp = m.Groups[1].Value.Trim().ToLower() == "tcp";
                        pi.Data  = ProbeConfiger.StringToByte(m.Groups[2].Value);
                    }
                    else if (strLine.StartsWith("PROBE_PORTS"))
                    {
                        foreach (var v in Regex.Match(strLine, @"\[(.*)\]").Groups[1].Value.Trim().Trim(',').Split(','))
                        {
                            foreach (var p in v.Trim().Trim('-').Split('-'))
                            {
                                pi.Ports.Add(int.Parse(p));
                            }
                        }
                    }
                    else if (strLine.StartsWith("PROBE_INDEX"))
                    {
                        pi.Index = int.Parse(Regex.Match(strLine, @"\[(.*)\]").Groups[1].Value);
                    }
                    else if (strLine.StartsWith("PROBE_REGEX"))
                    {
                        Match mr = Regex.Match(strLine, @"\[(.*?)\]\s*\{(.*)\}");
                        ProbeInfo.RegexInfo ri = new ProbeInfo.RegexInfo();
                        if (mr.Groups[1].Value == string.Empty || mr.Groups[2].Value == string.Empty)
                        {
                            throw new Exception();
                        }
                        ri.Name    = mr.Groups[1].Value;
                        ri.RegLine = nLine;
                        ri.Regex   = new Regex(mr.Groups[2].Value);
                        pi.RegexList.Add(ri);
                    }
                }
            } catch (ArgumentException ex) {
                throw new Exception("Load probes error on Line:" + nLine, ex);
            }
            int nLen  = lst.Count;
            int nFlag = nLen;

            while (nFlag > 0)
            {
                nLen  = nFlag;
                nFlag = 0;
                for (int i = 1; i < nLen; i++)
                {
                    if (lst[i - 1].Index > lst[i].Index)
                    {
                        var temp = lst[i - 1];
                        lst[i - 1] = lst[i];
                        lst[i]     = temp;
                    }
                }
            }
            Dictionary <int, List <ProbeInfo> > dic = new Dictionary <int, List <ProbeInfo> >();

            lst.ForEach(p => {
                foreach (var x in p.Ports)
                {
                    if (dic.ContainsKey(x))
                    {
                        dic[x].Add(p);
                    }
                    else
                    {
                        List <ProbeInfo> l = new List <ProbeInfo>();
                        l.Add(p);
                        dic.Add(x, l);
                    }
                }
            });
            lock (m_obj_sync) {
                this._ProbesDictionary = dic;
                this._AllProbes        = lst;
            }
        }