📄 f_winsniffdlg.cpp
字号:
// f_WinSniffDlg.cpp : implementation file
//
#include "stdafx.h"
#include "f_WinSniff.h"
#include "f_WinSniffDlg.h"
#include "winsock2.h"
#include "Iphlpapi.h"
#include "string.h"
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"iphlpapi.lib")
#define NDIS_PACKET_TYPE_PROMISCUOUS 0x0020 //混杂模式
#define NDIS_PACKET_TYPE_DIRECTED 0x0001 //直接模式
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////
#pragma pack(push, 1)
typedef struct _et_header
{
unsigned char eh_dst[6];
unsigned char eh_src[6];
unsigned short eh_type;
}ET_HEADER;
typedef struct _arp_header
{
unsigned short arp_hdr;
unsigned short arp_pro;
unsigned char arp_hln;
unsigned char arp_pln;
unsigned short arp_opt;
unsigned char arp_sha[6];
unsigned long arp_spa;
unsigned char arp_tha[6];
unsigned long arp_tpa;
}ARP_HEADER;
typedef struct _ip_header
{
char m_ver_hlen; //4位版本号,4位ip头部长
char m_tos;
USHORT m_tlen;
USHORT m_ident;
USHORT m_flag_frag; //3位标志位(1位未用位,1位DF,1位MF),13位片断偏移量
char m_ttl;
char m_protocol;
USHORT m_cksum;
ULONG m_sIP;
ULONG m_dIP;
}IP_HEADER;
typedef struct _icmp_header //ICMP头部
{
char type;
char code;
USHORT cksum;
USHORT id;
USHORT seq;
ULONG timestamp;
}ICMP_HEADER;
typedef struct _tcp_header
{
USHORT m_sport;
USHORT m_dport;
ULONG m_sek;
ULONG m_ack;
char m_hlen_res4; //4位tcp头部长,6位保留的前4位
char m_res2_flag; //6位保留的后2位,6位标志
USHORT m_win;
USHORT m_cksum;
USHORT m_urp;
}TCP_HEADER;
typedef struct _udp_header
{
USHORT m_sport;
USHORT m_dport;
USHORT m_tlen;
USHORT m_cksum;
}UDP_HEADER;
struct CHEATARP_INFO
{
char targetIP[20];
char targetMac[6];
char simulateIP[20];
char localMac[6];
};
#pragma pack(pop)
//////////////////////////////////////////////////////////////////////
LPADAPTER lpAdapter=NULL;
bool isRun=FALSE;
CF_WinSniffDlg *pthis=NULL;
DWORD WINAPI normalSniff(void *no);
DWORD WINAPI exchangeSniff(void *no);
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CF_WinSniffDlg dialog
CF_WinSniffDlg::CF_WinSniffDlg(CWnd* pParent /*=NULL*/)
: CDialog(CF_WinSniffDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CF_WinSniffDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
memset(m_hostName,0,20);
for(int i=0;i<5;i++)
{
memset(m_localMac[i],0,20);
memset(m_localIP[i],0,20);
}
m_localAdapterNum=0;
}
void CF_WinSniffDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CF_WinSniffDlg)
DDX_Control(pDX, IDC_ADAPTER, m_adapter);
DDX_Control(pDX, IDC_MODAL, m_modal);
DDX_Control(pDX, IDC_LIST1, m_dataList);
DDX_Control(pDX, IDC_LISTINFOR, m_localInfor);
DDX_Control(pDX, IDC_IPADDR2, m_ip2);
DDX_Control(pDX, IDC_IPADDR1, m_ip1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CF_WinSniffDlg, CDialog)
//{{AFX_MSG_MAP(CF_WinSniffDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDQUIT, OnQuit)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_CHECKALL, OnCheckAll)
ON_BN_CLICKED(IDC_CHECKPTOP, OnCheckPtop)
ON_BN_CLICKED(IDC_SNIFF, OnSniff)
ON_BN_CLICKED(IDC_CLEAN, OnClean)
ON_BN_CLICKED(IDC_SAVE_LOG, OnSaveLog)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_CBN_SELCHANGE(IDC_MODAL, OnChangeModal)
ON_CBN_SELCHANGE(IDC_ADAPTER, OnChangeAdapter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CF_WinSniffDlg message handlers
BOOL CF_WinSniffDlg::OnInitDialog()
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
MessageBox("WSAStartup error!");
return FALSE;
}
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
// TODO: Add extra initialization here
DWORD dwStyle = GetWindowLong(m_dataList.GetSafeHwnd(), GWL_STYLE);
dwStyle &= ~LVS_TYPEMASK;
dwStyle |= LVS_REPORT;
SetWindowLong(m_dataList.GetSafeHwnd(), GWL_STYLE, dwStyle);
m_dataList.InsertColumn(0, "关键字序号", LVCFMT_LEFT, 80);
m_dataList.InsertColumn(0, "数据包大小", LVCFMT_LEFT, 80);
m_dataList.InsertColumn(0, "时间", LVCFMT_LEFT, 169);
m_dataList.InsertColumn(0, "目标", LVCFMT_LEFT, 180);
m_dataList.InsertColumn(0, "来源", LVCFMT_LEFT, 180);
m_dataList.InsertColumn(0, "协议", LVCFMT_LEFT, 50);
::SendMessage(m_dataList.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
m_modal.AddString("一般模式");
m_modal.AddString("交换环境模式");
m_modal.SetCurSel(1);
m_isNormal=TRUE;
this->CheckRadioButton(IDC_CHECKALL,IDC_CHECKPTOP,IDC_CHECKALL);
this->GetDlgItem(IDC_IPADDR1)->EnableWindow(FALSE);
this->GetDlgItem(IDC_IPADDR2)->EnableWindow(FALSE);
m_isAll=TRUE;
if(getLocalInfor()==FALSE)
{
return FALSE;
}
displayLocalInfor();
getAdapter();
m_indexOfAdapter=0;
UpdateData(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CF_WinSniffDlg::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 CF_WinSniffDlg::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 CF_WinSniffDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CF_WinSniffDlg::OnQuit()
{
// TODO: Add your control notification handler code here
if(isRun==TRUE)
{
isRun=FALSE;
pthis=NULL;
Sleep(100);
if(lpAdapter!=NULL)
{
PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_DIRECTED);
PacketCloseAdapter(lpAdapter); //关闭适配器
}
}
WSACleanup();
CDialog::OnCancel();
return;
}
void CF_WinSniffDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
return;
}
void CF_WinSniffDlg::OnCheckAll()
{
// TODO: Add your control notification handler code here
this->GetDlgItem(IDC_IPADDR1)->EnableWindow(FALSE);
this->GetDlgItem(IDC_IPADDR2)->EnableWindow(FALSE);
m_isAll=TRUE;
return;
}
void CF_WinSniffDlg::OnCheckPtop()
{
// TODO: Add your control notification handler code here
this->GetDlgItem(IDC_IPADDR1)->EnableWindow(TRUE);
this->GetDlgItem(IDC_IPADDR2)->EnableWindow(TRUE);
m_isAll=FALSE;
return;
}
bool CF_WinSniffDlg::getLocalInfor()
{
if(gethostname(m_hostName,20)==SOCKET_ERROR)
{
CString cs;
cs.Format("%d",WSAGetLastError());
MessageBox("Can't get host name!");
MessageBox(cs);
return FALSE;
}
ULONG sizeOfAdapterInfo=0;
char mac[20]={0};
GetAdaptersInfo(NULL,&sizeOfAdapterInfo); //取得网卡信息
if(sizeOfAdapterInfo!=0)
{
IP_ADAPTER_INFO *pAdapterInfo=(IP_ADAPTER_INFO *)malloc(sizeOfAdapterInfo);
memset(pAdapterInfo,0,sizeOfAdapterInfo);
GetAdaptersInfo(pAdapterInfo,&sizeOfAdapterInfo);
for(int i=0;pAdapterInfo!=NULL;i++)
{
sprintf(m_localMac[i],"%02x-%02x-%02x-%02x-%02x-%02x",pAdapterInfo->Address[0],pAdapterInfo->Address[1],pAdapterInfo->Address[2],
pAdapterInfo->Address[3],pAdapterInfo->Address[4],pAdapterInfo->Address[5]);
memcpy( m_localIP[i],pAdapterInfo->IpAddressList.IpAddress.String,strlen(pAdapterInfo->IpAddressList.IpAddress.String) );
pAdapterInfo=pAdapterInfo->Next;
}
m_localAdapterNum=sizeOfAdapterInfo/sizeof(IP_ADAPTER_INFO);
return TRUE;
}
MessageBox("Can't get adapter information!");
return FALSE;
}
void CF_WinSniffDlg::displayLocalInfor()
{
char bufOfLine[30]={0};
strcpy(bufOfLine,"Hostname: ");
strcat(bufOfLine,m_hostName);
m_localInfor.AddString(bufOfLine);
for(int i=0;i<m_localAdapterNum;i++)
{
memset(bufOfLine,0,30);
m_localInfor.AddString(bufOfLine);
memset(bufOfLine,0,30);
strcpy(bufOfLine,"******************************");
m_localInfor.AddString(bufOfLine);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -