private void StartConnect(TCPScanTaskInfo ti)
        {
            if (ti.Socket != null)
            {
                base.CloseSocket(ti.Socket);
            }
            ti.Socket    = this.GetNextSocket(ti.Timeout);
            ti.LastTime  = DateTime.Now;
            ti.IsStarted = true;
            //lock (m_hs_task_running) m_hs_task_running.Add(ti);//repeat
            SocketAsyncEventArgs sae = this.PopSAE();

            sae.SetBuffer(0, 0);
            sae.RemoteEndPoint = ti.EndPoint;
            sae.UserToken      = ti;
            try {
                if (!ti.Socket.ConnectAsync(sae))
                {
                    IOProcessPool.QueueWork(this.ProcessConnect, sae);
                }
            } catch (Exception ex) {
                this.PushSAE(sae);
                lock (m_writer) {
                    if (ex.Message.IndexOf("asynchronous") != -1)
                    {
                        int a = 0;
                        a++;
                    }
                    m_writer.WriteLine("[SOCKET-CONNECT]-" + ex.Message + " tt:" + DateTime.Now.Subtract(ti.StartTime) + " rr" + ti.RunedRetry);
                    m_writer.Flush();
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-CONNECT]-" + ex.Message));
            }
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
0
        static IOProcessPool()
        {
            IOHandlerInfo hi = null;

            m_mre        = new ManualResetEvent(false);
            m_stack_idle = new Stack <IOHandlerInfo>();
            m_queue_work = new Queue <IOHandlerInfo>();
            new Thread(() => {
                while (true)
                {
                    hi = null;
                    lock (m_queue_work) {
                        if (m_queue_work.Count != 0)
                        {
                            hi = m_queue_work.Dequeue();
                        }
                    }
                    if (hi == null)
                    {
                        m_mre.WaitOne();
                        m_mre.Reset();
                        continue;
                    }
                    hi.Handler(hi.Args);
                    IOProcessPool.PushHandler(hi);
                }
            })
            {
                IsBackground = true
            }.Start();
        }
Exemplo n.º 4
0
        private void StartConnect(TCPScanTaskInfo ti)
        {
            if (ti.Socket != null)
            {
                base.CloseSocket(ti.Socket);
            }
            ti.Socket    = this.GetNextSocket(ti.Timeout);
            ti.LastTime  = DateTime.Now;
            ti.IsStarted = true;
            //lock (m_hs_task_running) m_hs_task_running.Add(ti);//repeat
            SocketAsyncEventArgs sae = this.PopSAE();

            sae.SetBuffer(0, 0);
            sae.RemoteEndPoint = ti.EndPoint;
            sae.UserToken      = ti;
            try {
                if (!ti.Socket.ConnectAsync(sae))
                {
                    IOProcessPool.QueueWork(this.ProcessConnect, sae);
                }
            } catch (Exception ex) {
                this.PushSAE(sae);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-CONNECT]-" + ex.Message));
            }
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        private void ProcessRecv(SocketAsyncEventArgs e)
        {
            Socket sock = e.UserToken as Socket;

            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                bool     b       = true;
                TaskInfo ti      = null;
                int      nOffset = (e.Buffer[0] & 0x0F) * 4;
                uint     uID     = (uint)((e.Buffer[nOffset + 6] << 8) + e.Buffer[nOffset + 7]);
                if (e.BytesTransferred < 28)
                {
                    b = false;
                }
                else if (e.Buffer[9] != RAWDefine.PROTO_ICMP)
                {
                    b = false;
                }
                else if (nOffset < 20 || e.BytesTransferred - 9 < nOffset)
                {
                    b = false;
                }
                if (b)
                {
                    lock (m_dic_task_running) {
                        if (m_dic_task_running.ContainsKey(uID))
                        {
                            ti = m_dic_task_running[uID];
                            m_dic_task_running.Remove(uID);
                        }
                    }
                }
                if (ti != null)
                {
                    if (e.Buffer[nOffset] + e.Buffer[nOffset + 1] == 0x00)
                    {
                        this.EndTask(ti, new IcmpEventArgs(uID,
                                                           ((IPEndPoint)ti.EndPoint).Address,
                                                           e.Buffer[8],
                                                           true,
                                                           (int)(m_sw.ElapsedMilliseconds - (ti.RunedRetry > 0 ? ti.StartTime : ti.LastTime)),
                                                           ti.RunedRetry));
                    }
                    else
                    {
                        this.EndTask(ti, new IcmpEventArgs(uID, ((IPEndPoint)ti.EndPoint).Address, ti.RunedRetry));
                    }
                }
            }
            if (!sock.ReceiveFromAsync(e))
            {
                IOProcessPool.QueueWork(this.ProcessRecv, e);
            }
        }
Exemplo n.º 7
0
 public static void QueueWork(IOProcessHandler handler, SocketAsyncEventArgs args)
 {
     lock (m_queue_work) {
         m_queue_work.Enqueue(IOProcessPool.PopHandler(handler, args));
     }
     m_mre.Set();
     if (m_queue_work.Count > 1000)
     {
         Console.WriteLine("======================: " + m_queue_work.Count);
     }
 }
Exemplo n.º 8
0
        private void SendData(SYNScanTaskInfo ti)
        {
            SocketAsyncEventArgs sae = this.PopSAE();

            Array.Copy(ti.SYNPacket, sae.Buffer, ti.SYNPacket.Length);
            ti.LastTime        = DateTime.Now;
            sae.RemoteEndPoint = ti.EndPoint;
            if (!m_sock_raw.SendToAsync(sae))
            {
                IOProcessPool.QueueWork(this.ProcessSend, sae);
            }
        }
Exemplo n.º 9
0
        private static void ProcessRecv(SocketAsyncEventArgs e)
        {
            Socket sock = e.UserToken as Socket;

            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                RawSocket.OnRecvCompleted(e);
            }
            try {
                if (!m_sock_raw.ReceiveFromAsync(e))
                {
                    IOProcessPool.QueueWork(RawSocket.ProcessRecv, e);
                }
            } catch { }
        }
Exemplo n.º 10
0
        private void SendData(TaskInfo ti)
        {
            SocketAsyncEventArgs sae = this.PopSAE();

            if (sae.Buffer.Length < ti.DataLen)
            {
                sae.SetBuffer(new byte[ti.DataLen], 0, ti.DataLen);
            }
            Array.Copy(ti.TempPacket, 0, sae.Buffer, 0, ti.DataLen);
            sae.SetBuffer(0, ti.DataLen);
            sae.RemoteEndPoint = ti.EndPoint;
            ti.LastTime        = m_sw.ElapsedMilliseconds;
            ti.IsStarted       = true;
            if (!m_sock.SendToAsync(sae))
            {
                IOProcessPool.QueueWork(this.ProcessSend, sae);
            }
        }
Exemplo n.º 11
0
        public static void SendData(byte[] byData, int nIndex, int nLen)
        {
            SocketAsyncEventArgs sae = RawSocket.PopSAE();

            if (sae.Buffer == null || sae.Buffer.Length < nLen)
            {
                sae.SetBuffer(byData, nIndex, nLen);
            }
            else
            {
                Array.Copy(byData, nIndex, sae.Buffer, 0, nLen);
            }
            //sae.remo
            if (!m_sock_raw.SendToAsync(sae))
            {
                IOProcessPool.QueueWork(RawSocket.ProcessRecv, sae);
            }
        }
Exemplo n.º 12
0
        private void SendData(UDPScanTaskInfo ti)
        {
            SocketAsyncEventArgs sae = this.PopSAE();

            byte[] byData = ti.SendDatasQueue.Dequeue();
            if (sae.Buffer.Length < byData.Length)
            {
                sae.SetBuffer(new byte[byData.Length], 0, byData.Length);
            }
            Array.Copy(byData, sae.Buffer, byData.Length);
            sae.SetBuffer(0, byData.Length);
            ti.LastTime        = DateTime.Now;
            sae.RemoteEndPoint = ti.EndPoint;
            if (!m_sock.SendToAsync(sae))
            {
                IOProcessPool.QueueWork(this.ProcessSend, sae);
            }
        }
Exemplo n.º 13
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            SmbScanTaskInfo ti = e.UserToken as SmbScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket sock = ti.Socket;

            if (e.SocketError != SocketError.Success)
            {
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            ti.RunedRetry = 0;
            ti.CanConnect = true;
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET]-" + ex.Message));
                return;
            }
            try {
                Array.Copy(m_bySmb1, e.Buffer, m_bySmb1.Length);
                e.SetBuffer(0, m_bySmb1.Length);
                if (!sock.SendAsync(e))
                {
                    IOProcessPool.QueueWork(this.ProcessSend, e);
                }
            } catch {
                this.PushSAE(e);
            }
        }
Exemplo n.º 14
0
        public IcmpScanner(int nMaxTask)
        {
            if (nMaxTask > 60000)
            {
                throw new ArgumentException("The [nMaxTask] must less than 60000");
            }
            m_rnd              = new Random();
            m_que_taskInfo     = new Queue <TaskInfo>();
            m_se               = new Semaphore(nMaxTask, nMaxTask);
            m_que_sae          = new Queue <SocketAsyncEventArgs>();
            m_dic_task_running = new Dictionary <uint, TaskInfo>();

            for (int i = 0; i < nMaxTask; i++)
            {
                TaskInfo ti = new TaskInfo();
                ti.ID         = (uint)(i + 1);
                ti.HeadPacket = new byte[]  {
                    0x08, 0x00,                     //type:8 code:0 - request ping
                    0x00, 0x00,                     //checksum
                    0x00, (byte)((i % 10) + 1),     //flag_id
                    (byte)(ti.ID >> 8), (byte)ti.ID //seq
                };
                ti.TempPacket = new byte[ti.HeadPacket.Length + 1];
                m_que_taskInfo.Enqueue(ti);
            }
            m_sw.Start();
            m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            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();
        }
Exemplo n.º 15
0
        public static void InitRawSocket(EndPoint bindEndPoint)
        {
            lock (m_obj_sync) {
                if (m_sock_raw != null)
                {
                    return;
                }
                m_que_sae = new Queue <SocketAsyncEventArgs>();

                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_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);

                SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
                sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                sae.SetBuffer(new byte[65535], 0, 65535);
                sae.UserToken      = m_sock_raw;
                sae.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                if (!m_sock_raw.ReceiveFromAsync(sae))
                {
                    IOProcessPool.QueueWork(RawSocket.ProcessRecv, sae);
                }
            }
        }
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success: break;

            case SocketError.ConnectionReset:
            case SocketError.ConnectionRefused:
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;

            default:
                if (e.SocketError != SocketError.OperationAborted)
                {
                    DateTime dd = DateTime.Now;
                    lock (m_writer) {
                        if (dd.Subtract(ti.StartTime).TotalSeconds > 70)
                        {
                            int a = 0;
                            a++;
                        }
                        m_writer.WriteLine("ERR:" + e.SocketError + " tt:" + DateTime.Now.Subtract(ti.StartTime) + " rr:" + ti.RunedRetry);
                        m_writer.Flush();
                    }
                }
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            //if (e.SocketError != SocketError.Success) {
            //    m_writer.WriteLine(e.SocketError);
            //    m_writer.Flush();
            //    this.PushSAE(e);
            //    if (++ti.RunedRetry > ti.Retry)
            //        this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
            //    else
            //        this.StartConnect(ti);
            //    return;
            //} else {
            //    //m_writer.WriteLine(e.SocketError);
            //    //m_writer.Flush();
            //}
            ti.RunedRetry = 0;
            ti.CanConnect = true;
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                lock (m_writer) {
                    m_writer.WriteLine("[SOCKET-RECV]-" + ex.Message);
                    m_writer.Flush();
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
            //try {
            //    if (!ti.Socket.ReceiveAsync(ti.RecvSAE)) IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
            //    if (ti.CurrentProbe != null) {
            //        if (e.Buffer.Length < ti.CurrentProbe.Data.Length)
            //            e.SetBuffer(new byte[ti.CurrentProbe.Data.Length], 0, ti.CurrentProbe.Data.Length);
            //        Array.Copy(ti.CurrentProbe.Data, e.Buffer, ti.CurrentProbe.Data.Length);
            //        e.SetBuffer(0, ti.CurrentProbe.Data.Length);
            //        if (!ti.Socket.SendAsync(e)) IOProcessPool.QueueWork(this.ProcessSend, e);
            //    } else this.PushSAE(e);
            //} catch (Exception ex) {
            //    this.PushSAE(e);
            //    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET]-" + ex.Message));
            //}
        }
Exemplo n.º 17
0
        private void ProcessRecv(SocketAsyncEventArgs e)
        {
            lock (m_obj_sync) {
                if (base._IsDisposed)
                {
                    return;
                }
            }
            Socket sock = e.UserToken as Socket;

            if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)
            {
                bool b       = true;
                uint uSIP    = BitConverter.ToUInt32(e.Buffer, 16);
                int  nOffset = (e.Buffer[0] & 0x0F) * 4;
                uint uSEQ    = RAWDefine.GetACKNumber(e.Buffer, nOffset) - 1;
                if (e.BytesTransferred < 40)
                {
                    b = false;
                }
                else if (nOffset < 20 || e.BytesTransferred - 20 < nOffset)
                {
                    b = false;
                }
                else if (e.Buffer[9] != RAWDefine.PROTO_TCP)
                {
                    b = false;
                }
                else if ((ushort)((e.Buffer[nOffset + 2] << 8) | e.Buffer[nOffset + 3]) != m_nLocalPort)
                {
                    b = false;
                }
                else if (e.Buffer[nOffset + 13] != 0x12)
                {
                    b = false;                                     //syn + ack
                }
                else if (uSIP != m_uLocalIP)
                {
                    b = false;
                }
                SYNScanTaskInfo ti = null;
                if (b)
                {
                    lock (m_dic_task_running) {
                        if (m_dic_task_running.ContainsKey(uSEQ))
                        {
                            ti = m_dic_task_running[uSEQ];
                            m_dic_task_running.Remove(uSEQ);
                        }
                    }
                }
                if (ti != null)
                {
                    uint id = m_tcp_scanner.Scan(ti.UIP, ti.Port, ti.Probes, ti.Timeout, ti.Retry, ti.TotalTimeout, ti.IsUseNullProbe);
                    lock (m_dic_uid) m_dic_uid.Add(id, ti);
                }
            }
            if (!sock.ReceiveAsync(e))
            {
                IOProcessPool.QueueWork(this.ProcessRecv, e);
            }
        }
Exemplo n.º 18
0
        private void ProcessRecv(SocketAsyncEventArgs e)
        {
            SmbScanTaskInfo ti = e.UserToken as SmbScanTaskInfo;

            ti.LastTime = DateTime.Now;
            if (e.SocketError != SocketError.Success || e.BytesTransferred < 1)
            {
                //if (ti.RunedRetry < ti.Retry) {
                //    ti.Step = 1;
                //    this.StartConnect(ti);
                //} else this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;
            }
            if (!SmbScanner.CheckHeader(e.Buffer))
            {
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, true, "The header is not smb"));
                return;
            }
            if (ti.Step == 1)
            {
                SocketAsyncEventArgs sae = this.PopSAE();
                try {
                    Array.Copy(m_bySmb2, sae.Buffer, m_bySmb2.Length);
                    sae.SetBuffer(0, m_bySmb2.Length);//buffer
                    if (!ti.Socket.SendAsync(sae))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, sae);
                    }
                } catch {
                    this.PushSAE(sae);
                }
                try {
                    ti.Step++;
                    if (!ti.Socket.ReceiveAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessRecv, e);
                    }
                } catch (Exception ex) {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ex.Message));
                    return;
                }
            }
            else
            {
                string strResult      = string.Empty;
                int    nOffsetNtlmssp = SmbScanner.KmpIndexOf(0, e.Buffer, m_byNTLMSSP, m_byNext);
                if (nOffsetNtlmssp == -1)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, "SMB", -1, "", e.Buffer, e.BytesTransferred));
                    return;
                }
                lock (m_strBuffer) {
                    try {
                        m_strBuffer.Remove(0, m_strBuffer.Length);
                        m_strBuffer.Append("Target Name           :");
                        m_strBuffer.Append(Encoding.Unicode.GetString(e.Buffer, e.Buffer[nOffsetNtlmssp + 16] + nOffsetNtlmssp, e.Buffer[nOffsetNtlmssp + 12]));
                        int nOffsetData = e.Buffer[nOffsetNtlmssp + 16] + nOffsetNtlmssp + e.Buffer[nOffsetNtlmssp + 12];
                        while (e.Buffer[nOffsetData] != 0)
                        {
                            if (m_dic_netbios.ContainsKey(e.Buffer[nOffsetData]))
                            {
                                m_strBuffer.Append("\r\n" + m_dic_netbios[e.Buffer[nOffsetData]] + ":");
                            }
                            else
                            {
                                m_strBuffer.Append("\r\n" + e.Buffer[nOffsetData].ToString("X2") + ":");
                            }
                            if (!m_dic_netbios.ContainsKey(e.Buffer[nOffsetData]) || e.Buffer[nOffsetData] == 6 || e.Buffer[nOffsetData] == 7)
                            {
                                for (int i = nOffsetData + 4; i < nOffsetData + 4 + e.Buffer[nOffsetData + 2]; i++)
                                {
                                    m_strBuffer.Append(((int)e.Buffer[i]).ToString("X2"));
                                }
                            }
                            else
                            {
                                m_strBuffer.Append(Encoding.Unicode.GetString(e.Buffer, nOffsetData + 4, e.Buffer[nOffsetData + 2]));
                            }
                            nOffsetData += e.Buffer[nOffsetData + 2] + 4;
                        }
                        m_strBuffer.Append("\r\n----------OS----------\r\n" + Encoding.Unicode.GetString(e.Buffer, nOffsetData + 4, e.BytesTransferred - nOffsetData - 4).Replace("\0", "\r\n"));
                        strResult = m_strBuffer.ToString();
                    } catch {
                        strResult = "[ERROR] - Can not format the data";
                    }
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, "SMB", -1, strResult, e.Buffer, e.BytesTransferred));
            }
        }
Exemplo n.º 19
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success:
                ti.RunedRetry = 0;
                ti.CanConnect = true;
                break;

            case SocketError.ConnectionReset:           //此连接由远程对等计算机重置
            case SocketError.ConnectionRefused:         //远程主机正在主动拒绝连接
            case SocketError.AddressNotAvailable:       //选定的 IP 地址在此上下文中无效
            case SocketError.Fault:                     //基础套接字提供程序检测到无效的指针地址
            case SocketError.HostDown:                  //由于远程主机被关闭 操作失败
            case SocketError.HostNotFound:              //无法识别这种主机 该名称不是正式的主机名或别名
            case SocketError.HostUnreachable:           //没有到指定主机的网络路由
            case SocketError.NetworkDown:               //网络不可用
            case SocketError.NetworkUnreachable:        //不存在到远程主机的路由
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;
            }
            if (SocketError.Success != e.SocketError || !ti.IsStarted)
            {
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry || ti.IsTotalTimeout)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
        }