Пример #1
0
        private void StartConnect(SmbScanTaskInfo 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);
            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]-" + ex.Message));
            }
        }
Пример #2
0
        //private bool _IsDisposed = false;

        //public bool IsDisposed {
        //    get { return _IsDisposed; }
        //}

        public SmbScanner(int nMaxTask)
        {
            m_dic_netbios.Add(1, "NetBIOS computer name ");
            m_dic_netbios.Add(2, "NetBIOS domain name   ");
            m_dic_netbios.Add(3, "DNS computer name     ");
            m_dic_netbios.Add(4, "DNS domain name       ");
            m_dic_netbios.Add(6, "Flags                 ");
            m_dic_netbios.Add(7, "Timestamp             ");
            m_se              = new Semaphore(nMaxTask, nMaxTask);
            m_que_sae         = new Queue <SocketAsyncEventArgs>();
            m_que_task        = new Queue <SmbScanTaskInfo>();
            m_hs_task_running = new HashSet <SmbScanTaskInfo>();
            for (int i = 0; i < nMaxTask; i++)
            {
                SmbScanTaskInfo      ti  = new SmbScanTaskInfo();
                SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
                sae.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                sae.SetBuffer(new byte[1500], 0, 1500);
                ti.RecvSAE    = sae;
                sae.UserToken = ti;
                m_que_task.Enqueue(ti);
            }
            m_byNTLMSSP = Encoding.UTF8.GetBytes("NTLMSSP\0");
            m_byNext    = SmbScanner.GetNextVal(m_byNTLMSSP);
            new Thread(this.CheckTimeout)
            {
                IsBackground = true
            }.Start();
        }
Пример #3
0
 private void EndTask(SmbScanTaskInfo ti, ScanEventArgs e)
 {
     this.CloseSocket(ti.Socket);
     ti.Socket    = null;
     ti.IsStarted = false;
     lock (m_hs_task_running) m_hs_task_running.Remove(ti);
     lock (m_obj_sync) {
         if (!base._IsDisposed)
         {
             m_que_task.Enqueue(ti);
         }
     }
     base.OnCompleted(e);
     m_se.Release();
 }
Пример #4
0
        protected override uint OnScan(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbes)
        {
            lock (m_obj_sync) {
                if (base._IsDisposed)
                {
                    throw new ObjectDisposedException("TCPScanner", "The scanner was disposed");
                }
            }
            m_se.WaitOne();
            SmbScanTaskInfo ti = this.CreateTaskInfo(445, endPoint, 0, nTimeout, nRetry, nTotalTimeout, false);

            ti.StartTime = DateTime.Now;
            this.StartConnect(ti);
            return(ti.TaskID);
        }
Пример #5
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);
            }
        }
Пример #6
0
        private SmbScanTaskInfo CreateTaskInfo(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbe)
        {
            SmbScanTaskInfo ti = null;

            lock (m_obj_sync) {
                ti = m_que_task.Dequeue();
            }
            ti.Retry        = nRetry;
            ti.RunedRetry   = 0;
            ti.Port         = nPort;
            ti.EndPoint     = endPoint;
            ti.IsStarted    = false;
            ti.Socket       = this.GetNextSocket(nTimeout);
            ti.CanConnect   = false;
            ti.Timeout      = nTimeout;
            ti.TotalTimeout = nTotalTimeout;
            ti.Step         = 1;
            return(ti);
        }
Пример #7
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));
            }
        }