1:基于数据库的用户登录界面
在注册的时候,就将用户所注册的信息加载到数据库中(用户的密码为了保正数据的CIA特性,在加入数据库的时候我使用了MD5加密算法,在用户组成的时候,江永输入的密码进行MD5的加密,在数据库中进行遍历遍历操作,找到就能够进入下一个界面,不能进入时系统将根据匹配的结果进行系统提示)
2:进入用户的选择界面,此时只有一个选项(用于数据的截获界面的进入,当用户点击相应的按钮时,将进入项目的主界面---------数据包的捕获截面,此处设计的时候由于时间有限,界面显得比较单调,将用于后续的开发使用,从而实现更强大的更强大的可选功能)
3::进入主界面(数据的捕获截面时首先需要选择相应的网卡信息,如果在没有选择网卡的情况下进行数据的捕获卖系统将自动提示-------没有选择网卡信息;当用户选择网卡信息后,可根据捕获所有数据包还是有过滤的捕获一些包【TCP/UDP/IGMP.ICMP】);在信息选择完毕时,按下相应的按钮开始数据的捕获。
数据不活的结果如下:
1>:没有选择的进行数据包的捕获
2>:有限责的进行数据包的捕获(此处用TCP数据包的捕获为例)
在此处出现的小问题是在最开始会出现一些空白行这是实践中没有处理的
本次的课题是用socket与c开发一个网络嗅探器的程序设计,设计完成对流经本地网卡的所有数据包捕获,分析协议类型,并根据不同的协议类型对数据包进行进一步的分析,包括分析数据包的源IP地址,目的IP地址,源端口号,目的端口号,大小等等。设计完成后经测试能实现预期要求的功能。但是仍然存在不足之处,例如:
1.由于时间和所学知识有限,只对数据包进行了简单分析,就归类到日志文件中。这样会占用很大的内存空间去存储这些数据。并且只能在日志文件中对其进一步的分析,操作量大。
2.本文中的嗅探器使用于基于广播包的网络,而对于诸如交换机这类设备,由于它能阻止广播,所以就不能够对子网内其他的机器进行监听。
3 . 通过本次的课程设计,学会了数据包的抓取得相关环节以及一些设计大的系统函数的使用,这在一定条件下,为以后的编程留下了一定的印象和基础。
4 . 实现了在vs2012的开发的平台下进行界面化的编程设计,对一些常用的控件有了一定的了解。
5 . 本次课程设计的最大的锻炼自己的地方在于查阅文档的相关能力,与此同时,在查阅玄关的英语文档时,对自身的英语水平有了一定的提高(学到了很多的专业英语词汇)。
通过对该课题的研究,让我对嗅探器技术有了进一步的了解,对其在网络中产生的影响有了更深的认识。
1>:用户注册界面的实现
头文件:
#pragma once
#include "afxwin.h"
// user_register 对话框
class user_register : public CDialogEx
{
DECLARE_DYNAMIC(user_register)
public:
user_register(CWnd* pParent = NULL); // 标准构造函数
virtual ~user_register();
// 对话框数据
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
CEdit user_name;
CEdit user_password;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual BOOL OnInitDialog();
};
成员函数的实现:
// user_register.cpp : 实现文件
//
#include "stdafx.h"
#include "safe.h"
#include "user_register.h"
#include "afxdialogex.h"
#include "safeDlg.h"
#include "md5.h"
// user_register 对话框
IMPLEMENT_DYNAMIC(user_register, CDialogEx)
user_register::user_register(CWnd* pParent /*=NULL*/)
: CDialogEx(user_register::IDD, pParent)
{
}
user_register::~user_register()
{
}
void user_register::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, user_name);
DDX_Control(pDX, IDC_EDIT2, user_password);
}
BEGIN_MESSAGE_MAP(user_register, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON1, &user_register::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &user_register::OnBnClickedButton2)
END_MESSAGE_MAP()
// user_register 消息处理程序
void user_register::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString get_user_name;
user_name.GetWindowText(get_user_name);
if(get_user_name!="")
{
char sql[100] = {0};
sprintf_s(sql,"select user_name from register_user_information where user_name = '%s'",get_user_name);
list
theApp.my_sql.SelectMySql (sql,1,lststr);
if(lststr.size()>0)
{
MessageBox (_T("此用户已被使用"));
return;
}
CString get_user_password;
user_password.GetWindowText(get_user_password);
if(get_user_password==""){
MessageBox(_T("密码为空"));
}
else
{
CTime t = CTime::GetCurrentTime();
CString strTime = t.Format(_T("%Y-%m-%d %H:%M:%S"));
//***************************************使用MD5算法加密***********************************
sprintf_s(sql,"insert into register_user_information values('%s',md5('%s'),'%s')",get_user_name,get_user_password, strTime);
theApp.my_sql.UpdateMySql(sql);
char select_user_name[100]={0};
list
sprintf_s(select_user_name,"select user_name from register_user_information where user_name='%s'",get_user_name);
theApp.my_sql.SelectMySql(select_user_name,1,list_select_user_name);
if(list_select_user_name.size()>0)
{
//将数据清空
list_select_user_name.pop_front();
MessageBox (_T("注册成功!"));
}
}
}
else
{
MessageBox(_T("请输入用户名"));
}
}
void user_register::OnBnClickedButton2() //取消
{
// TODO: 在此添加控件通知处理程序代码
CsafeDlg safe;
theApp.m_pMainWnd=&safe;
EndDialog(IDOK);
safe.DoModal();
}
BOOL user_register::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13)
{
return true;
}
return CDialogEx::PreTranslateMessage(pMsg);
}
BOOL user_register::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetWindowText("用户注册");
return TRUE;
}
2>:选项界面
头文件:
#pragma once
#include "afxwin.h"
// user_select 对话框
class user_select : public CDialogEx
{
DECLARE_DYNAMIC(user_select)
public:
user_select(CWnd* pParent = NULL); // 标准构造函数
virtual ~user_select();
// 对话框数据
enum { IDD = IDD_DIALOG4 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
CBitmapButton m_button;
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedButton1();
};
成员函数的实现:
// user_select.cpp : 实现文件
//
#include "stdafx.h"
#include "safe.h"
#include "user_select.h"
#include "afxdialogex.h"
#include "user_main.h"
// user_select 对话框
IMPLEMENT_DYNAMIC(user_select, CDialogEx)
user_select::user_select(CWnd* pParent /*=NULL*/)
: CDialogEx(user_select::IDD, pParent)
{
}
user_select::~user_select()
{
}
void user_select::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON1, m_button);
}
BEGIN_MESSAGE_MAP(user_select, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON1, &user_select::OnBnClickedButton1)
END_MESSAGE_MAP()
// user_select 消息处理程序
BOOL user_select::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetWindowText("用户选择");
m_button.LoadBitmaps(IDB_user_get_date);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void user_select::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
user_main m_user_main;
theApp.m_pMainWnd=&m_user_main;
EndDialog(IDOK);
m_user_main.DoModal();
}
3>:数据捕获截面的实现
头文件:
#pragma once
#include "pcap.h"
#include
#include
#include "afxwin.h"
//相关的结构体函数(数据包的包头信息)
struct ether_header
{
u_int8_t ether_dhost[6]; //目的Mac地址
u_int8_t ether_shost[6]; //源Mac地址
u_int16_t ether_type; //协议类型
};
struct ip_header
{
#if defined(WORDS_BIENDIAN)
u_int8_t ip_version : 4,
ip_header_length : 4;
#else
u_int8_t ip_header_length : 4,
ip_version : 4;
#endif
u_int8_t ip_tos;
u_int16_t ip_length;
u_int16_t ip_id;
u_int16_t ip_off;
u_int8_t ip_ttl;
u_int8_t ip_protocol;
u_int16_t ip_checksum;
struct in_addr ip_souce_address;
struct in_addr ip_destination_address;
};
// user_main 对话框
class user_main : public CDialogEx
{
DECLARE_DYNAMIC(user_main)
public:
user_main(CWnd* pParent = NULL); // 标准构造函数
virtual ~user_main();
// 对话框数据
enum { IDD = IDD_DIALOG2 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
CListCtrl list_information;
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
int m_radio;
string m_user_chose;
HANDLE m_chose_event_tcp;
HANDLE m_chose_threed_tcp;
HANDLE m_totol_date_event;
HANDLE m_totol_date_threed;
HANDLE select_netmask;
HANDLE EVENT_select_netmask;
bool m_flag;
BOOL bool_select_netmask;
int argc;
char **argv;
int bj_protocol;
int protocol_date;
int protocol; //ip首部中的协议字段的数据
afx_msg void OnBnClickedButton3();
bool bool_start;
afx_msg void OnBnClickedButton4();
CEdit tcp_count;
CEdit udp_count;
CEdit icmp_count;
CEdit igmp_count;
int ip_date_len;
public:
CListBox selecet_equipment;
afx_msg void OnBnClickedButton5();
public:
FILE *log_file;
char * iptos(u_long in);
static void ip_protool_packet_callback(u_char *argument, const struct pcap_pkthdr* packet_header, const u_char* packet_content);
static void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr* packet_header, const u_char* packet_content);
void ifprint(pcap_if_t *d);
int i;
CString netmask_information;
afx_msg void OnLbnDblclkList3();
CListBox header_information;
ip_header *ip_h;
CEdit current_netmask_name;
pcap_if_t * alldevs;
pcap_if_t * d;
pcap_t * adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
int num;
u_char argument;
afx_msg void OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult);
pcap_pkthdr packet_header;
u_char packet_content;
int int_tcp_count;
int int_udp_count;
int int_icmp_count;
int int_igmp_count;
int int_arp_count;
int int_rarp_count;
CEdit arp_count;
CEdit rarp_count;
};
成员函数的实现:
// user_main.cpp : 实现文件
//
#include "stdafx.h"
#include "safe.h"
#include "user_main.h"
#include "afxdialogex.h"
#include "safeDlg.h"
#include "user_select.h"
#include
#include "pcap.h"
#include "stdlib.h"
#include
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"packet.lib")
#pragma comment(lib,"ws2_32.lib")
#define IPTOSBUFFERS 12
#define LINE_LEN 16
// user_main 对话框
IMPLEMENT_DYNAMIC(user_main, CDialogEx)
user_main::user_main(CWnd* pParent /*=NULL*/)
: CDialogEx(user_main::IDD ,pParent)
, m_radio(0)
{
m_flag=true;
bool_start=true;
i=0;
netmask_information="";
bool_select_netmask=TRUE;
num=0;
int_tcp_count=0;
int_udp_count=0;
int_icmp_count=0;
int_igmp_count=0;
int_arp_count=0;
int_rarp_count=0;
//argument = 0;
//packet_header = 0;
//packet_content = 0;
}
user_main::~user_main()
{
}
void user_main::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, list_information);
DDX_Radio(pDX, IDC_RADIO1, m_radio);
DDX_Control(pDX, IDC_EDIT3, tcp_count);
DDX_Control(pDX, IDC_EDIT2, udp_count);
DDX_Control(pDX, IDC_EDIT1, icmp_count);
DDX_Control(pDX, IDC_EDIT4, igmp_count);
DDX_Control(pDX, IDC_LIST3, selecet_equipment);
DDX_Control(pDX, IDC_LIST4, header_information);
DDX_Control(pDX, IDC_EDIT5, current_netmask_name);
DDX_Control(pDX, IDC_EDIT6, arp_count);
DDX_Control(pDX, IDC_EDIT7, rarp_count);
}
BEGIN_MESSAGE_MAP(user_main, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON1, &user_main::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &user_main::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &user_main::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &user_main::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &user_main::OnBnClickedButton5)
ON_LBN_DBLCLK(IDC_LIST3, &user_main::OnLbnDblclkList3)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &user_main::OnNMDblclkList1)
END_MESSAGE_MAP()
// user_main 消息处理程序
BOOL user_main::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetWindowText("数据抓包");
CRect m_rect;
list_information.GetClientRect(&m_rect);
int m_width=m_rect.Width()/4;
list_information.InsertColumn(0,"编号",0,m_width);
list_information.InsertColumn(1,"数据类型",0,m_width);
list_information.InsertColumn(2,"数据长度",0,m_width);
list_information.InsertColumn(3,"数据捕获数据的时间",m_width);
tcp_count.SetWindowText("0");
udp_count.SetWindowText("0");
icmp_count.SetWindowText("0");
igmp_count.SetWindowText("0");
arp_count.SetWindowText("0");
rarp_count.SetWindowText("0");
return TRUE; // return TRUE unless you set the focus to a control
}
int ip_date_count=0;
//过滤事件的线程函数
DWORD WINAPI ThreadProc_tcp( LPVOID lpParameter){
user_main *pthis=(user_main*)lpParameter;
CTime t;
CString strTime;
while(pthis->bool_start){
//等待100s 超时的时候 继续等待
if(WaitForSingleObject(pthis->m_chose_event_tcp,100) == WAIT_TIMEOUT)
{
continue;
}
//根据选项抓包 根据所选取的端口号进项监听 捕获流经相应端口的数据
//2:提取iP头中的协议字段的数值 获取与需要截获的数据的类型相同的数据
//pthis->protocol=(int)pthis->select_ip_date.ip_protocol->ip_protocol;
//3:当协议字段满足条件时,将截获数据的时间设置为有信号 将过滤事件设置为有信号
if(pthis->protocol==pthis->bj_protocol){
pthis->protocol_date=pthis->protocol;
}
//获取数据 根据数据包的包头中的字段进行分析
//1: 获取数据的长度
//pthis->ip_date_len=pthis->select_ip_date.ip_protocol->ip_length;
//2:获取系统时间
t = CTime::GetCurrentTime();
strTime = t.Format(_T("%Y-%m-%d %H:%M:%S"));
//插入数据
char a[100]={0};
CString str=itoa(ip_date_count,a,10);
pthis->list_information.InsertItem(0,str);
//跟据协议字段的类型 选择需要加载的信息的类型
if(pthis->protocol_date==1) pthis->list_information.SetItemText(0,1,"TCMP");
else if(pthis->protocol_date==2) pthis->list_information.SetItemText(0,1,"IGMP");
else if(pthis->protocol_date==6) pthis->list_information.SetItemText(0,1,"TCP");
else if(pthis->protocol_date==17) pthis->list_information.SetItemText(0,1,"UDP");
char l[100]={0};
CString ip_date_length=itoa(pthis->ip_date_len,l,10);
pthis->list_information.SetItemText(0,2,ip_date_length);
pthis->list_information.SetItemText(0,3,strTime);
CString current_equipment_info;
pthis->current_netmask_name.GetWindowText(current_equipment_info);
if ((pthis->adhandle = pcap_open_live(current_equipment_info, 65536, 1, 1000,pthis->errbuf)) == NULL)
{
::MessageBox(NULL,"\nUnable to open the adapter.%s is not supported by WinPcap","提示",NULL);
pcap_freealldevs(pthis->alldevs);
return -1;
}
//释放列表
//pcap_freealldevs(pthis_totol->alldevs);
//开始捕捉 ///*************************************************
//pthis_totol->ethernet_protocol_packet_callback(&(pthis_totol->argument),&(pthis_totol->packet_header),&(pthis_totol->packet_content));
pcap_loop(pthis->adhandle, -1,user_main::ip_protool_packet_callback, NULL);
}
return 0;
}
//截获用户需要过滤的数据
void user_main::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
//将事件设置为有信号
//1:根据需要过滤的选项选择需要的数据 进行处理
switch(m_radio)
{
case 0:
bj_protocol=6;
break;
case 1:
bj_protocol=17;
break;
case 2:
bj_protocol=1;
break;
case 3:
bj_protocol=2;
break;
default:
break;
}
//当用户没有选择网卡时 不能进入相应的线程函数
//当选项信息 不是空的时候 将截获数据的事件的信息号设置为有信号
m_chose_event_tcp = CreateEvent(NULL,FALSE,FALSE,NULL);
if(netmask_information!=""){
m_chose_threed_tcp = CreateThread(NULL,0,&ThreadProc_tcp,this,0,NULL);
SetEvent(m_chose_event_tcp); //将事件设置为有信号
}
else{
MessageBox("请先选择相应的网卡");
}
}
void user_main::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
user_select m_user_select;
theApp.m_pMainWnd=&m_user_select;
EndDialog(IDOK);
m_user_select.DoModal();
}
void user_main::ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr* packet_header, const u_char* packet_content)
{
u_short ethernet_type;
struct ether_header *ethernet_protocol;
u_char *mac_string;
static int packet_number = 1;
//对获取到的数据进行编号
CString str_packet_number;
char c_packet_number[100]={0};
str_packet_number=itoa(packet_number,c_packet_number,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("获取的数据包的编号:"+str_packet_number);
//获取当前的系统时间
CTime t = CTime::GetCurrentTime();
CString strTime = t.Format(_T("%Y-%m-%d %H:%M:%S"));
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获时间:"+strTime);
//获取数据包的长度
CString str_len;
char c_len[100]={0};
str_len=itoa(packet_header->len,c_len,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("数据包的长度:"+str_len);
//将数据加载到List_information上面
((user_main*)(theApp.m_pMainWnd))->list_information.InsertItem(0,str_packet_number);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,2,str_len);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,3,strTime);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("---------以太网协议---------");
ethernet_protocol = (struct ether_header*)packet_content;//获得数据包内容
ethernet_type = ntohs(ethernet_protocol->ether_type);//获得以太网类型
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("以太网的类型是:"+ethernet_type);
switch (ethernet_type)
{
case 0x0800: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是IP协议\n");
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"IP");
break;
case 0x0806: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是ARP协议\n");
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"ARP");
((user_main*)(theApp.m_pMainWnd))->int_arp_count++;
break;
case 0x8035: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是RARP协议\n");
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"RARP");
((user_main*)(theApp.m_pMainWnd))->int_rarp_count++;
break;
default:break;
}
mac_string = ethernet_protocol->ether_shost;
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("MAC的原地址是:"+*mac_string+':'+*(mac_string + 1)+':'+ *(mac_string + 2)+':'+ *(mac_string + 3)+':'+ *(mac_string + 4)+':'+ *(mac_string + 5));
mac_string = ethernet_protocol->ether_dhost;
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("MAC的目的地址是:"+*mac_string+':'+ *(mac_string + 1)+':'+ *(mac_string + 2)+':'+*(mac_string + 3)+':'+ *(mac_string + 4)+':'+ *(mac_string + 5));
packet_number++;
//return 0;
}
//用户截获数据的线程函数 不需要过滤数据 所有的数据都捕获
DWORD WINAPI ThreadProc_m_totol_date_event( LPVOID lpParameter){
user_main *pthis_totol=(user_main*)lpParameter;
while(pthis_totol->bool_start){
if(WaitForSingleObject(pthis_totol->m_totol_date_event,100)==WAIT_TIMEOUT){
continue;
}
//监听数据 并将数据显示到窗口上
//转到选择的设备
//打开失败
CString current_equipment_info;
pthis_totol->current_netmask_name.GetWindowText(current_equipment_info);
if ((pthis_totol->adhandle = pcap_open_live(current_equipment_info, 65536, 1, 1000,pthis_totol->errbuf)) == NULL)
{
::MessageBox(NULL,"\nUnable to open the adapter.%s is not supported by WinPcap","提示",NULL);
pcap_freealldevs(pthis_totol->alldevs);
return -1;
}
//释放列表
//pcap_freealldevs(pthis_totol->alldevs);
//开始捕捉 ///*************************************************
//pthis_totol->ethernet_protocol_packet_callback(&(pthis_totol->argument),&(pthis_totol->packet_header),&(pthis_totol->packet_content));
pcap_loop(pthis_totol->adhandle, -1,user_main::ethernet_protocol_packet_callback, NULL);
}
return 0;
}
//截获流经用户端口的所有的数据
void user_main::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
CString str_equipment_information;
current_netmask_name.GetWindowText(str_equipment_information);
if(str_equipment_information!=""){
m_totol_date_event = CreateEvent(NULL,FALSE,FALSE,NULL);
m_totol_date_threed = CreateThread(NULL,0,&ThreadProc_m_totol_date_event,this,0,NULL);
SetEvent(m_totol_date_event); //将事件设置为有信号
}
else{
MessageBox("请先选择所需捕获数据的网卡");
}
}
void user_main::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
bool_start=false;
char c_tcp[100]={0};
char c_udp[100]={0};
char c_icmp[100]={0};
char c_igmp[100]={0};
char c_arp[100]={0};
char c_rarp[100]={0};
CString str_tcp=itoa(int_tcp_count,c_tcp,10);
CString str_udp=itoa(int_udp_count,c_udp,10);
CString str_icmp=itoa(int_icmp_count,c_icmp,10);
CString str_igmp=itoa(int_igmp_count,c_igmp,10);
CString str_arp=itoa(int_arp_count,c_arp,10);
CString str_rarp=itoa(int_rarp_count,c_rarp,10);
tcp_count.SetWindowText(str_tcp);
udp_count.SetWindowText(str_udp);
icmp_count.SetWindowText(str_icmp);
igmp_count.SetWindowText(str_igmp);
arp_count.SetWindowText(str_arp);
rarp_count.SetWindowText(str_rarp);
}
char * user_main::iptos(u_long in)
{
static char output[IPTOSBUFFERS][3 * 4 + 3 + 1];
static short which;
u_char *p;
p = (u_char *)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
printf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
int protocol_count=1;
void user_main::ip_protool_packet_callback(u_char *argument, const struct pcap_pkthdr* packet_header, const u_char* packet_content)
{
struct ip_header *ip_protocol;
u_int header_length = 0;
u_int offset;
u_char tos;
u_int16_t checksum;
//MAC首部是14位的,加上14位得到IP协议首部
ip_protocol = (struct ip_header *) (packet_content + 14);
checksum = ntohs(ip_protocol->ip_checksum);
tos = ip_protocol->ip_tos;
offset = ntohs(ip_protocol->ip_off);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("#---------IP协议---------");
char num[100]={0};
CString str_num=itoa(protocol_count,num,10);
char c_ip_protocol_ip_version[100]={0};
CString str_ip_version=itoa(ip_protocol->ip_version,c_ip_protocol_ip_version,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("版本号:"+str_ip_version);
char c_header_length[100]={0};
CString str_header_length=itoa(header_length,c_header_length,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("首部长度:"+str_header_length);
char c_tos[100]={0};
CString str_tos=itoa(tos,c_tos,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("服务质量:"+str_tos);
char c_ntohs_ip_protocol_ip_length[100]={0};
CString str_ip_protocol_ip_length=itoa(ntohs(ip_protocol->ip_length),c_ntohs_ip_protocol_ip_length,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("总长度:"+str_ip_protocol_ip_length);
char c_ntohs_ip_protocol_ip_id[100]={0};
CString str_c_ntohs_ip_protocol_ip_id=itoa(ntohs(ip_protocol->ip_id),c_ntohs_ip_protocol_ip_id,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("标识:"+ str_c_ntohs_ip_protocol_ip_id);
//计算偏移量
char c_offset[100]={0};
CString str_offset=itoa((offset & 0x1fff) * 8,c_offset,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("偏移:"+str_offset);
//获取生存时间
char c_ttl[100]={0};
CString str_ttl=itoa(ip_protocol->ip_ttl,c_ttl,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("生存时间:"+str_ttl);
//获取协议的类型
char c_ip_protocol[100]={0};
CString str_ip_protocol=itoa(ip_protocol->ip_protocol,c_ip_protocol,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("协议类型:"+str_ip_protocol);
if(((user_main*)(theApp.m_pMainWnd))->bj_protocol==ip_protocol->ip_protocol){
//根据协议的类型加载相关的数据
((user_main*)(theApp.m_pMainWnd))->list_information.InsertItem(0,str_num);
if(ip_protocol->ip_protocol==1){
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获到的数据的编号是:"+str_num);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"ICMP");
((user_main*)(theApp.m_pMainWnd))->int_icmp_count++;
}else if(ip_protocol->ip_protocol==2){
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获到的数据的编号是:"+str_num);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"IGMP");
((user_main*)(theApp.m_pMainWnd))->int_igmp_count++;
}else if(ip_protocol->ip_protocol==6){
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获到的数据的编号是:"+str_num);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"TCP");
((user_main*)(theApp.m_pMainWnd))->int_tcp_count++;
}else if(ip_protocol->ip_protocol==17){
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获到的数据的编号是:"+str_num);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,1,"UDP");
((user_main*)(theApp.m_pMainWnd))->int_udp_count++;
}
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,2,str_ip_protocol_ip_length);
CTime t = CTime::GetCurrentTime();
CString strTime = t.Format(_T("%Y-%m-%d %H:%M:%S"));
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("捕获时间:"+strTime);
((user_main*)(theApp.m_pMainWnd))->list_information.SetItemText(0,3,strTime);
}
switch (ip_protocol->ip_protocol)
{
case 1: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是ICMP协议:ICMP"); break;
case 2: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是IGMP协议:IGMP"); break;
case 6: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是TCP协议:TCP"); break;
case 17: ((user_main*)(theApp.m_pMainWnd))->header_information.AddString("上层协议是UDP协议:UDP"); break;
default:break;
}
char c_checksum[100]={0};
CString str_checksum=itoa(checksum,c_checksum,10);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("检验和:"+str_checksum);
CString str_ip_souce_address=inet_ntoa(ip_protocol->ip_souce_address);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("源IP地址:%s\n"+str_ip_souce_address);
CString str_ip_destination_address=inet_ntoa(ip_protocol->ip_destination_address);
((user_main*)(theApp.m_pMainWnd))->header_information.AddString("目的地址:%s\n"+str_ip_destination_address);
protocol_count++;
}
void user_main::ifprint(pcap_if_t *d){
pcap_addr_t *a;
char c_i[100]={0};
CString str_i=itoa(++i,c_i,10);
CString str_1=d->name;
selecet_equipment.AddString(str_1);
if (d->description)
{
for (a = d->addresses; a != NULL; a = a->next)
{
char c_2[100]={0};
CString str_2=itoa(a->addr->sa_family,c_2,10);
//根据网卡信息列表中的数据的数目遍历整个列表上的数据 当没有相同的信息是 加载数据 有相同的则不加载数据
int a_count=selecet_equipment.GetCount();
int bj_0=0;
CString str_0_0;
for(int i=0;i selecet_equipment.GetText(i,str_0_0); if(str_2==str_0_0) bj_0=1; break; } if(bj_0==0){ selecet_equipment.AddString("\tAddress Family:#"+str_2); } switch (a->addr->sa_family) { case AF_INET: selecet_equipment.AddString("\tAddress Family Name:AF_INET"); if (a->addr) { CString str_3=iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr); int bj_0_1=0; int a_0_1=selecet_equipment.GetCount(); CString str_0_1; for(int i=0;i selecet_equipment.GetText(i,str_0_1); if(str_0_1==str_3) bj_0_1=1; break; } if(bj_0_1==0){ selecet_equipment.AddString("\tAddress:"+str_3); } } if (a->netmask) { CString str_4=iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr); selecet_equipment.AddString("\tNetmask:"+str_4); } if (a->broadaddr) { CString str_5=iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr); selecet_equipment.AddString("\tBroadcast Address:"+str_5); } if (a->dstaddr) { CString str_6= iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr); selecet_equipment.AddString("\tDestination Address:"+str_6); } break; default: selecet_equipment.AddString("\tAddressFamilyName:Unknown"); break; } } } else{ return; } } void user_main::OnBnClickedButton5() //准备 将网卡信息加载到select_equipment 的控件上 { // TODO: 在此添加控件通知处理程序代码 log_file=fopen("log.txt","w+"); //创建日志文件 /*取得列表*/ //pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf) if (pcap_findalldevs(&alldevs, errbuf) == -1) { exit(1); } /*输出列表*/ for (d = alldevs; d != NULL; d = d->next) { ifprint(d); } /*ifprint(d);*/ if (i == 0) { MessageBox("No interfaces found!Make sure WinPcap is installed."); return ; } } DWORD WINAPI select_netmask_ThreadProc( LPVOID lpParameter){ user_main *pthis_select_netmask=(user_main*)lpParameter; while(pthis_select_netmask->bool_select_netmask){ pthis_select_netmask->bool_select_netmask=FALSE; int index=pthis_select_netmask->selecet_equipment.GetCurSel(); if(index == -1){ return 0; } pthis_select_netmask->selecet_equipment.GetText(index,pthis_select_netmask->netmask_information); pthis_select_netmask->current_netmask_name.SetSel(0,-1); pthis_select_netmask->current_netmask_name.Clear(); pthis_select_netmask->current_netmask_name.SetWindowText(pthis_select_netmask->netmask_information); } pthis_select_netmask->bool_select_netmask=TRUE; return 0; } void user_main::OnLbnDblclkList3() //双击select_equipment中的网卡选项-------将网卡选中 { // TODO: 在此添加控件通知处理程序代码 select_netmask=CreateThread(NULL,0,&select_netmask_ThreadProc,this,0,0); SetEvent(EVENT_select_netmask); } void user_main::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult) { LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast *pResult = 0; // TODO: 在此添加控件通知处理程序代码 *pResult = 0; } 4>:APP 头文件: // safeDlg.h : 头文件 // #pragma once #include "afxwin.h" // CsafeDlg 对话框 class CsafeDlg : public CDialogEx { // 构造 public: CsafeDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_SAFE_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: afx_msg void OnBnClickedButton1(); CStatic m_picture; CEdit user_name; CEdit user_password; CBitmap m_bitmap; afx_msg void OnBnClickedButton2(); afx_msg void OnBnClickedButton3(); virtual BOOL PreTranslateMessage(MSG* pMsg); }; 成员函数的实现: // safeDlg.h : 头文件 // #pragma once #include "afxwin.h" // CsafeDlg 对话框 class CsafeDlg : public CDialogEx { // 构造 public: CsafeDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_SAFE_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() // safeDlg.cpp : 实现文件 #include "stdafx.h" #include "safe.h" #include "safeDlg.h" #include "afxdialogex.h" #include "user_register.h" #include "user_main.h" #include "user_select.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CsafeDlg 对话框 CsafeDlg::CsafeDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CsafeDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bitmap.LoadBitmap(IDB_power); } void CsafeDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_m_picture, m_picture); DDX_Control(pDX, IDC_EDIT1, user_name); DDX_Control(pDX, IDC_EDIT2, user_password); } BEGIN_MESSAGE_MAP(CsafeDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, &CsafeDlg::OnBnClickedButton1) ON_BN_CLICKED(IDC_BUTTON2, &CsafeDlg::OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, &CsafeDlg::OnBnClickedButton3) END_MESSAGE_MAP() // CsafeDlg 消息处理程序 BOOL CsafeDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 SetWindowText("用户登录"); m_picture.SetBitmap(m_bitmap); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CsafeDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CsafeDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CsafeDlg::OnQueryDragIcon() { return static_cast } void CsafeDlg::OnBnClickedButton1() //注册 { // TODO: 在此添加控件通知处理程序代码 user_register user_reg; theApp.m_pMainWnd=&user_reg; EndDialog(IDOK); user_reg.DoModal(); } void CsafeDlg::OnBnClickedButton2() //登录 { // TODO: 在此添加控件通知处理程序代码 char sql[1000] = {0}; CString get_user_name; CString get_user_password; user_name.GetWindowText(get_user_name); if(get_user_name==""){ MessageBox(_T("请输入用户名")); } else{ user_password.GetWindowText(get_user_password); if(get_user_password==""){ MessageBox(_T("请输入用对用户名的密码")); } else{ sprintf_s(sql,"select user_name from register_user_information where user_name = '%s' and user_password = md5('%s')",get_user_name,get_user_password); list theApp.my_sql.SelectMySql (sql,1,lststr); if(lststr.size()>0) { //当找到合适的匹配的用户信息的时候 就将链表中的数据清空 lststr.pop_front(); //当登陆成功是 跳转到数据抓包界面 /*user_main u_main; theApp.m_pMainWnd=&u_main; EndDialog(IDOK); u_main.DoModal();*/ user_select m_user_select; theApp.m_pMainWnd=&m_user_select; EndDialog(IDOK); m_user_select.DoModal(); } else { MessageBox(_T("密码错误,请重新输入!")); user_name.SetSel(0,-1); user_name.Clear(); user_password.SetSel(0,-1); user_password.Clear(); } } } } void CsafeDlg::OnBnClickedButton3() //取消 { // TODO: 在此添加控件通知处理程序代码 SendMessage(WM_CLOSE); //退出程序 } BOOL CsafeDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13) { return true; } return CDialogEx::PreTranslateMessage(pMsg); } afx_msg void OnBnClickedButton1(); CStatic m_picture; CEdit user_name; CEdit user_password; CBitmap m_bitmap; afx_msg void OnBnClickedButton2(); afx_msg void OnBnClickedButton3(); virtual BOOL PreTranslateMessage(MSG* pMsg); };