PortScanDlg.cpp
上传用户:zhyl198712
上传日期:2013-02-03
资源大小:35k
文件大小:11k
- // PortScanDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "PortScan.h"
- #include "PortScanDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //#include <Winsock2.h>
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- int v; //
- UINT m_nCounter; //循环中的端口值
- BOOL m_bIsSinglePort; //是不是单端口
- UINT m_nPortFrom,m_nPortTo; //端口范围
- CString m_sIP; //ip地址
- int listCounter; //列表框的纪录行数
- int m_nThreadNumber; //开启线程数
- struct thread m_thread;
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPortScanDlg dialog
- CPortScanDlg::CPortScanDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CPortScanDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CPortScanDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_bIsSinglePort=TRUE;
- }
- CPortScanDlg::~CPortScanDlg()
- {
- WSACleanup();
- }
- void CPortScanDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPortScanDlg)
- DDX_Control(pDX, IDC_THREADNUMBER, m_threadNumber);
- DDX_Control(pDX, IDC_BUTTONSTOP, m_buttonStop);
- DDX_Control(pDX, IDC_BUTTONSCAN, m_buttonStart);
- DDX_Control(pDX, IDC_STATIC_PROMPT, m_prompt);
- DDX_Control(pDX, IDC_PROGRESS_SCAN, m_progressScan);
- DDX_Control(pDX, IDC_EDIT_PORT_FROM, m_portFrom);
- DDX_Control(pDX, IDC_EDIT_PORT_TO, m_portTo);
- DDX_Control(pDX, IDC_EDIT_SINGLE_PORT, m_singlePort);
- DDX_Control(pDX, IDC_IP_ADDRESS, m_IP);
- DDX_Control(pDX, IDC_LISTINFO, m_listInfo);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPortScanDlg, CDialog)
- ON_MESSAGE(WM_USER_PRINT_START,OnPrintStart)
- //{{AFX_MSG_MAP(CPortScanDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_RADIO_SINGLE, OnRadioSingle)
- ON_BN_CLICKED(IDC_RADIO_MULTI, OnRadioMulti)
- ON_BN_CLICKED(IDC_BUTTONSCAN, OnButtonscan)
- ON_BN_CLICKED(IDC_BUTTONSTOP, OnButtonstop)
- ON_BN_CLICKED(IDC_BUTTONABOUT, OnButtonabout)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPortScanDlg message handlers
- BOOL CPortScanDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- //单选按钮
- CheckRadioButton(IDC_RADIO_SINGLE,IDC_RADIO_MULTI,IDC_RADIO_SINGLE);
- m_portFrom.EnableWindow(FALSE);
- m_portTo.EnableWindow(FALSE);
- m_buttonStop.EnableWindow(FALSE);
- //初始化信息列表框
- m_listInfo.InsertColumn(0,"IP地址",LVCFMT_LEFT,0,0);
- m_listInfo.InsertColumn(1,"开放的端口",LVCFMT_LEFT,0,0);
- m_listInfo.SetColumnWidth(0,150);
- m_listInfo.SetColumnWidth(1,150);
- //默认的IP地址
- m_IP.SetAddress(172,21,41,200);
- //默认的线程个数
- m_threadNumber.SetWindowText("200");
- //开始winsock
- WSADATA wsaData;
- if(WSAStartup(0x0002, &wsaData ))
- {
- AfxMessageBox("Init TCP/IP stack error!");
- return 1;
- }
- if(wsaData.wVersion != 0x0002)
- {
- printf("Winsock version is incorrect!");
- WSACleanup();
- return 1;
- }
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CPortScanDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CPortScanDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CPortScanDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CPortScanDlg::OnRadioSingle()
- {
- m_bIsSinglePort=TRUE;
- m_singlePort.EnableWindow(TRUE);
- m_portFrom.EnableWindow(FALSE);
- m_portTo.EnableWindow(FALSE);
- }
- void CPortScanDlg::OnRadioMulti()
- {
- m_bIsSinglePort=FALSE;
- m_singlePort.EnableWindow(FALSE);
- m_portFrom.EnableWindow(TRUE);
- m_portTo.EnableWindow(TRUE);
- }
- void CPortScanDlg::OnButtonscan()
- {
-
- CString ip;
- BYTE f1,f2,f3,f4;
- char temp[10];
- m_listInfo.DeleteAllItems();
- listCounter=0;
- /****获取IP地址****/
- if(m_IP.IsBlank())
- {
- MessageBox("请输入IP地址",_T("错误"),MB_ICONWARNING|MB_OK);
- return;
- }
- if(m_IP.GetAddress(f1,f2,f3,f4)<4)
- {
- MessageBox("请输入完整的IP地址",_T("错误"),MB_ICONWARNING|MB_OK);
- return;
- }
- ip=itoa(f1,temp,10);
- ip+=_T(".");
- ip+=itoa(f2,temp,10);
- ip+=_T(".");
- ip+=itoa(f3,temp,10);
- ip+=_T(".");
- ip+=itoa(f4,temp,10);
- m_sIP=ip;
- /**********************/
- /***获取端口地址****/
- if(m_bIsSinglePort==TRUE)
- {
- CString strPort;
- m_singlePort.GetWindowText(strPort);
- m_nPortFrom=m_nPortTo=atoi(strPort);
- }
- else
- {
- CString strPort;
- m_portFrom.GetWindowText(strPort);
- m_nPortFrom=atoi(strPort);
- m_portTo.GetWindowText(strPort);
- m_nPortTo=atoi(strPort);
- }
- if(m_nPortFrom>m_nPortTo)
- {
- MessageBox("开始端口要小于结束端口",_T("错误"),MB_ICONWARNING|MB_OK);
- return;
- }
- /**********************/
- /*********取得开启线程数*****/
- char strThreadNumber[10];
- m_threadNumber.GetWindowText(strThreadNumber,10);
- m_nThreadNumber=atoi(strThreadNumber);
- if(m_nThreadNumber>1000)
- {
- AfxMessageBox("最大线程数不能超过1000");
- return;
- }
- m_buttonStart.EnableWindow(FALSE);
- m_buttonStop.EnableWindow(TRUE);
- /*********设置进度条**************/
- m_progressScan.SetRange32(0,m_nPortTo-m_nPortFrom+1);
- m_progressScan.SetStep(1);
- /********** v是批量创建线程的次数*************/
- if(m_nPortFrom==m_nPortTo)
- {
- v=1;
- }
- else
- {
- v=(m_nPortTo-m_nPortFrom+1)/m_nThreadNumber;
- if((m_nPortTo-m_nPortFrom+1)%m_nThreadNumber > 0)
- {
- v++;
- }
- }
- m_thread.m_hwnd=this->m_hWnd;
- m_thread.ip=m_sIP;
- m_nCounter=m_nPortFrom;
-
- AfxBeginThread((AFX_THREADPROC)threadM,NULL);
- }
- //一个独立线程,主要是处理循环,与窗体脱离,提高窗体的反应能力
- UINT threadM(LPVOID pParam)
- {
- CPortScanDlg *dlg=(CPortScanDlg*)AfxGetApp()->GetMainWnd();
- /*********创建互斥量************/
- HANDLE hCounter=NULL;
- if( (hCounter=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"sam sp 44"))==NULL)
- {
- //如果没有其他进程创建这个互斥量,则重新创建
- hCounter = CreateMutex(NULL,FALSE,"sam sp 44");
- }
- CWinThread *pT[1001];
- HANDLE hThread[1001];
- for(int i=0;i<v;i++)
- {
- int nThreadCounter=0;//每次批量创建的线程实际个数,最后一次是一个余数值
- int k;
- for(k=0;k<m_nThreadNumber;k++)
- {
-
- if(m_nCounter>m_nPortTo)
- {
- break;
- }
- //设置扫描进度提示
- char temp[10];
- memset(temp,0,10);
- itoa(m_nCounter,temp,10);
- CString strTemp=(CString)temp;
- dlg->m_prompt.SetWindowText("端口:"+strTemp);
-
- //内循环计数
- nThreadCounter++;
- dlg->m_progressScan.StepIt();
- pT[k]=AfxBeginThread((AFX_THREADPROC)threadA,&m_thread);
- hThread[k]=pT[k]->m_hThread;
- Sleep(5);
- }
-
- hThread[k]=NULL;//非常重要,如果不加这一句的话,会经常出错,有点奇怪
- //如果k=0,表示没有开启线程
- if(k!=0)
- {
- //WaitForMultipleObjects(nThreadCounter,hThread,TRUE,INFINITE);
- WaitForMultipleObjects(nThreadCounter,hThread,TRUE,500);
- }
-
- }
- CloseHandle(hCounter);
- //设置提示
- dlg->m_buttonStart.EnableWindow(TRUE);
- dlg->m_buttonStop.EnableWindow(FALSE);
- dlg->m_prompt.SetWindowText(_T(""));
- dlg->m_progressScan.SetPos(0);
- AfxEndThread(0);
- return 0;
- }
- //核心线程体
- UINT threadA(LPVOID pParam)
- {
- struct thread *threada=(struct thread*)pParam;
- //在内部重新打开互斥量
- HANDLE hCounterIn=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"sam sp 44");
- WaitForSingleObject(hCounterIn,INFINITE);
- UINT v=m_nCounter;
- m_nCounter++;
- ReleaseMutex(hCounterIn);
- CloseHandle(hCounterIn);
-
- struct sockaddr_in sin;
-
- SOCKET sd;
- int IpPort;
- char IpAddr[16];
- strcpy(IpAddr,threada->ip);
- IpPort=v;
- // Create the local socket
- if ((sd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET) {
- printf("Create socket error!");
- return 1;
- }
- // Connect to the victim IP Address
- sin.sin_family=AF_INET;
- sin.sin_addr.s_addr=inet_addr(IpAddr);
- sin.sin_port=htons((short)IpPort);
- if (connect (sd, (struct sockaddr *)&sin, sizeof (sin)) == SOCKET_ERROR) {
- printf("Connect the remote IP error!");
- closesocket (sd);
- return 1;
- }
- else
- {
- ::PostMessage(threada->m_hwnd,WM_USER_PRINT_START,v,0);
- }
- closesocket (sd);
- return 0;
- }
- LRESULT CPortScanDlg::OnPrintStart(WPARAM wParam,LPARAM lParam)
- {
- int port=(int)wParam;
- char temp[10];
- memset(temp,0,10);
- itoa(port,temp,10);
- m_listInfo.InsertItem(listCounter,m_sIP,0);
- m_listInfo.SetItemText(listCounter,1,temp);
- listCounter++;
- return 0;
- }
- void CPortScanDlg::OnButtonstop()
- {
- m_nCounter=m_nPortTo+1;
- m_buttonStart.EnableWindow(TRUE);
- m_buttonStop.EnableWindow(FALSE);
-
- }
- void CPortScanDlg::OnButtonabout()
- {
- CAboutDlg dlg;
- dlg.DoModal();
- }