📄 beastscanerdlg.cpp
字号:
// BeastScanerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BeastScaner.h"
#include "BeastScanerDlg.h"
#include "ScanHandler.h"
#include "Choose.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CBeastScanerDlg dialog
CBeastScanerDlg::CBeastScanerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBeastScanerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBeastScanerDlg)
stCurrentState = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
enCurrentScanType=NO_SCAN;
dwCurrentHostNo=0;
dwCurrentPortInfoNo=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
dwLocalIP=0;
memset(bLocalMac,0,6);
this->enCurrentScanType=NO_SCAN;
}
void CBeastScanerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBeastScanerDlg)
DDX_Control(pDX, IDC_LIST_PORT_INFO, cListPortInfo);
DDX_Control(pDX, IDC_LIST_HOST_INFO, cListHostInfo);
DDX_Control(pDX, IDC_BUTTON1, cButtonPause);
DDX_Control(pDX, IDC_TAB1, cTabCtrl);
DDX_Text(pDX, IDC_EDIT2, stCurrentState);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBeastScanerDlg, CDialog)
//{{AFX_MSG_MAP(CBeastScanerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_DBLCLK, IDC_LIST_HOST_INFO, OnNMDblclkListHostInfo)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedPause)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedStop)
ON_MESSAGE(WM_BEGIN_SCAN, OnBeginScan)
ON_MESSAGE(WM_FINISH_SCAN, OnFinishScan)
ON_MESSAGE(WM_HOST_SCAN_INFO, OnAddHostInfo)
ON_MESSAGE(WM_UPDATA_LOG, OnUpdataLog)
ON_MESSAGE(WM_CONNECT_SCAN,OnHandleConnectScan)
ON_MESSAGE(WM_UDP_SCAN,OnHandleUdpScan)
ON_MESSAGE(WM_SYN_SCAN,OnHandleSynScan)
ON_MESSAGE(WM_FIN_SCAN,OnHandleFinScan)
ON_MESSAGE(WM_ACK_SCAN,OnHandleAckScan)
ON_MESSAGE(WM_NULL_SCAN,OnHandleNullScan)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBeastScanerDlg message handlers
BOOL CBeastScanerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
pMainWindow=this;
// 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
// TODO: Add extra initialization here
this->stCurrentState="Wait for user command.";
this->GetLocalInfo();
this->InitListCtrl();
this->InitTabCtrl();
this->UpdateData(FALSE);
char errbuf[PCAP_ERRBUF_SIZE+1];
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
CString ErrInfo;
ErrInfo.Format("Error in pcap_findalldevs: %s\n", errbuf);
MessageBox(ErrInfo,"Error",MB_OK|MB_ICONERROR);
exit(1);
}
this->SelectDev=alldevs;
CChoose Adapter(this->alldevs,NULL);
/* Scan the list printing every entry */
if(Adapter.DoModal()==IDOK)
{
pcap_if_t *d=alldevs;
for(int i=0;i<=Adapter.CurrentSel;i++)
{
this->SelectDev=d;
d=d->next;
}
}
else
{
exit (0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CBeastScanerDlg::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 CBeastScanerDlg::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 CBeastScanerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBeastScanerDlg::GetLocalInfo()
{char strName[64];
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulLen = 0;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( strName, sizeof(strName)) == 0)
WSACleanup( );
}
::GetAdaptersInfo(pAdapterInfo,&ulLen);
pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);
// 取得本地适配器结构信息
if(::GetAdaptersInfo(pAdapterInfo,&ulLen) == ERROR_SUCCESS)
{
if(pAdapterInfo != NULL)
{
memcpy(this->bLocalMac, pAdapterInfo->Address, 6);
this->dwDefaultGateway= ntohl(::inet_addr(pAdapterInfo->GatewayList.IpAddress.String));
this->dwLocalIP = ntohl(::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String));
this->dwNetMask= ntohl(::inet_addr(pAdapterInfo->IpAddressList.IpMask.String));
}
else
{
exit (0);
}
}
else
{
exit (0);
}
this->cArpScanDlg.dwLocalIP=this->dwLocalIP;
this->cArpScanDlg.strHostName.Format("%s",strName);
this->cArpScanDlg.dwDefaultGateway=this->dwDefaultGateway;
this->cArpScanDlg.dwNetMask=this->dwNetMask;
this->cIcmpScanDlg.dwOriginateIPAddress=1+(this->dwLocalIP&0xffffff00);
this->cIcmpScanDlg.dwLastIPAddress=254+(this->dwLocalIP&0xffffff00);
this->cPortScanDlg.dwOriginateIP=1+(this->dwLocalIP&0xffffff00);
this->cPortScanDlg.dwLastIP=254+(this->dwLocalIP&0xffffff00);
// 为适配器结构申请内存
memcpy(this->cArpScanDlg.bLocalMac,this->bLocalMac,6);
}
void CBeastScanerDlg::InitListCtrl()
{this->cListHostInfo.SetExtendedStyle(this->cListHostInfo.GetExtendedStyle() | LVS_REPORT | LVS_OWNERDRAWFIXED|LVS_EX_FULLROWSELECT);
_TCHAR *HostColumnLabel[2] ={_T("Scan Method"),_T(" Host IP Address"),};
int HostColumnWidth[2] = {155,180};
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for(int i=0;i<2;i++)
{
lvc.iSubItem = i;
lvc.pszText = HostColumnLabel[i];
lvc.cx = HostColumnWidth[i];
lvc.fmt = LVCFMT_LEFT ;
this->cListHostInfo.InsertColumn(i,&lvc);
}
this->cListPortInfo.SetExtendedStyle(this->cListPortInfo.GetExtendedStyle() | LVS_REPORT | LVS_OWNERDRAWFIXED|LVS_EX_FULLROWSELECT);
_TCHAR *PortColumnLabel[4] ={_T("Scan Method"),_T(" Host IP Address"),_T(" Host Port"),_T("Port State"),};
int PortColumnWidth[4] = {100,120,87,87};
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for(i=0;i<4;i++)
{
lvc.iSubItem = i;
lvc.pszText = PortColumnLabel[i];
lvc.cx = PortColumnWidth[i];
lvc.fmt = LVCFMT_LEFT ;
this->cListPortInfo.InsertColumn(i,&lvc);
}
}
void CBeastScanerDlg::InitTabCtrl()
{
this->cArpScanDlg.Create(IDD_ARP_SCAN,&(this->cTabCtrl));
this->cIcmpScanDlg.Create(IDD_ICMP_SCAN,&(this->cTabCtrl));
this->cIcmpScanDlg.m_OriginateIPAddress.SetAddress(1+(this->dwLocalIP&0xffffff00));
this->cIcmpScanDlg.m_LastIPAddress.SetAddress(254+(this->dwLocalIP&0xffffff00));
this->cPortScanDlg.Create(IDD_PORT_SCAN,&(this->cTabCtrl));
this->cPortScanDlg.m_OriginateIP.SetAddress(1+(this->dwLocalIP&0xffffff00));
this->cPortScanDlg.cIPAddressCtrl.SetAddress(254+(this->dwLocalIP&0xffffff00));
this->cTabCtrl.AddTab(&(this->cArpScanDlg),"ARP Scan",0);
this->cTabCtrl.AddTab(&(this->cIcmpScanDlg),"ICMP Scan",1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -