winpcap局域网抓包工具http

来源:互联网 发布:360主机卫士 apache 编辑:程序博客网 时间:2024/06/05 06:56

局域网抓包工具可以抓取局域网所有http响应,需要安装winpcap驱动内有,内有源码,可以合并同一个ack的数据包,还原成一个html明文,下面附上源码mfc

// pcapcatchDlg.h : 头文件// #pragma once#include "afxcmn.h"#include <pcap.h>#include "stdafx.h" struct ether_header { u_int8_t  ether_dhost[6];      /* destination eth addr */ u_int8_t  ether_shost[6];      /* source ether addr    */ u_int16_t ether_type;          /* packet type ID field */ };   /* 4 bytes IP address */ typedef struct ip_address{ u_char byte1; u_char byte2; u_char byte3; u_char byte4; };   /* IPv4 header */ typedef struct ip_header{ u_char  ver_ihl;        // Version (4 bits) + Internet header length (4 bits) u_char  tos;            // Type of service u_short tlen;           // Total length u_short identification// Identification u_short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits) u_char  ttl;            // Time to live u_char  proto;          // Protocol u_short crc;            // Header checksum ip_address  saddr;      // Source address ip_address  daddr;      // Destination address u_int   op_pad;         // Option + Padding };   /* UDP header*/ typedef struct udp_header{ u_short sport;          // Source port u_short dport;          // Destination port u_short len;            // Datagram length u_short crc;            // Checksum }; struct outstr{CString sip;CString dip;CString otxt;u_int32_t th_seq;             /* sequence number */ u_int32_t th_ack;             /* acknowledgement number */}; /*TCP Header*/ struct tcp_header { u_int16_t th_sport;         /* source port */ u_int16_t th_dport;         /* destination port */ u_int32_t th_seq;             /* sequence number */ u_int32_t th_ack;             /* acknowledgement number */ u_int16_t th_len_resv_code//   Datagram   length and reserved code u_int16_t th_win;           /* window */ u_int16_t th_sum;           /* checksum */ u_int16_t th_urp;           /* urgent pointer */ }; bool is_readable(char c);CString iptostr(ip_address pa); // CpcapcatchDlg 对话框class CpcapcatchDlg : public CDialogEx{// 构造public:CpcapcatchDlg(CWndpParent = NULL);// 标准构造函数CListCtrlMouleList;// 对话框数据enum { IDD = IDD_PCAPCATCH_DIALOG };protected://virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV 支持  // 实现protected:HICON m_hIcon;virtual void DoDataExchange(CDataExchangepDX); // 生成的消息映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nIDLPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:afx_msg void OnBnClickedOk(); void lst(void);afx_msg void OnBnClickedButton1();afx_msg void OnBnClickedCancel();afx_msg void OnNMRClickList1(NMHDR *pNMHDRLRESULT *pResult);afx_msg void OnNMDblclkList1(NMHDR *pNMHDRLRESULT *pResult);afx_msg void OnTimer(UINT_PTR nIDEvent);afx_msg void OnBnClickedButton2();};
// pcapcatchDlg.cpp : 实现文件// #include "stdafx.h"#include "pcapcatch.h"#include "pcapcatchDlg.h"#include "afxdialogex.h"#include "resource.h"#include <pcap.h>#ifdef _DEBUG#define new DEBUG_NEW#endifusing namespace std;  extern outstr osr[500]={0};extern int si =0;// 完成列表使用了的长度extern int complete_data_last_print=0;pcap_if_t *alldevs;pcap_t *adhandle;// 用于应用程序“关于”菜单项的 CAboutDlg 对话框UINT Ctd(LPVOID pParam){struct pcap_pkthdr *header; const u_char *pkt_data; int rst=0;si=0;while((rst=pcap_next_ex(adhandle,&header,&pkt_data))>=0){  if(rst==0){ //time out and not packet captured continue; }   ether_header *eh = (ether_header*)pkt_data; if(ntohs(eh->ether_type)==0x0800){ // ip packet only ip_header *ih = (ip_header*)(pkt_data+14);   if(ntohs(ih->proto) == 0x0600){ // tcp packet only int ip_len = ntohs(ih->tlen);//ip_len = ip_body + ip_header bool find_http = false; CString http_txt = "";    size_t ip_header_len = (ih->ver_ihl & 0xf) * 4; tcp_headerth = (tcp_header *) ((u_char*)ih + ip_header_len);ip_len=ip_len-ip_header_len;charip_pkt_data = (char*)th;for(int im=0;im<ip_len;++im){if (ntohs(th->th_dport)==80||ntohs(th->th_sport)==80){   //check the http request if(!find_http && (im+3<ip_len && strncmp(ip_pkt_data+im,"GET ",strlen("GET ")) ==0 ) || (im+4<ip_len && strncmp(ip_pkt_data+im,"POST ",strlen("POST ")) == 0) ){ //find_http = true;int s=0; }   ////check the http response //if(!find_http && i+8<ip_len && strncmp(ip_pkt_data+i,"HTTP/1.1 ",strlen("HTTP/1.1 "))==0){ //find_http = true; //}else if (find_http|((im+strlen("text/html")<ip_len) &&strncmp(ip_pkt_data+im,"text/html",strlen("text/html"))==0&& is_readable(ip_pkt_data[im]))){find_http = true;bool fd=false;for (int i=0;i<si;i++){if (osr[i].th_ack==th->th_ack){osr[i].otxt+=ip_pkt_data[im];fd=true;}}if (!fd){ http_txt += ip_pkt_data[im];outstr o ;o.sip=iptostr(ih->saddr);o.dip=iptostr(ih->daddr);o.th_ack=th->th_ack;o.otxt=http_txt;osr[si]=o;si++;}}else{if(!find_http && is_readable(ip_pkt_data[im])){ //http_txt += ip_pkt_data[i];//find_http = true;int p=0;for (int i=0;i<si;i++){if (osr[i].th_ack==th->th_ack){osr[i].otxt+=ip_pkt_data[im];}}}//dotata()} }//collect the http text /*if(find_http && is_readable(ip_pkt_data[i])){ http_txt += ip_pkt_data[i]; }*/ }   //print the http request or response //if(http_txt != ""&&strlen(http_txt)>5){//outstr o ; //o.sip=iptostr(ih->saddr);//o.dip=iptostr(ih->daddr);//o.otxt=http_txt;//osr[si]=o;////AfxMessageBox(http_txt); ////MouleList.SetItemText(si,3,TEXT(stt));//// system("pause");//si++;////UpdateWindow(); //} } }   } return 0;}class CAboutDlg : public CDialogEx{public:CAboutDlg(); // 对话框数据enum { IDD = IDD_ABOUTBOX }; protected:virtual void DoDataExchange(CDataExchangepDX);    // DDX/DDV 支持 // 实现protected:DECLARE_MESSAGE_MAP()}; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){} void CAboutDlg::DoDataExchange(CDataExchangepDX){CDialogEx::DoDataExchange(pDX);} BEGIN_MESSAGE_MAP(CAboutDlgCDialogEx)END_MESSAGE_MAP()  // CpcapcatchDlg 对话框   CpcapcatchDlg::CpcapcatchDlg(CWndpParent /*=NULL*/): CDialogEx(CpcapcatchDlg::IDDpParent){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);} void CpcapcatchDlg::DoDataExchange(CDataExchangepDX){CDialogEx::DoDataExchange(pDX);DDX_Control(pDXIDC_LIST1MouleList);} BEGIN_MESSAGE_MAP(CpcapcatchDlgCDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDOK, &CpcapcatchDlg::OnBnClickedOk)ON_BN_CLICKED(IDC_BUTTON1, &CpcapcatchDlg::OnBnClickedButton1)ON_BN_CLICKED(IDCANCEL, &CpcapcatchDlg::OnBnClickedCancel)ON_NOTIFY(NM_RCLICKIDC_LIST1, &CpcapcatchDlg::OnNMRClickList1)ON_NOTIFY(NM_DBLCLKIDC_LIST1, &CpcapcatchDlg::OnNMDblclkList1)ON_WM_TIMER()ON_BN_CLICKED(IDC_BUTTON2, &CpcapcatchDlg::OnBnClickedButton2)END_MESSAGE_MAP()  // CpcapcatchDlg 消息处理程序 BOOL CpcapcatchDlg::OnInitDialog(){CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000); CMenupSysMenu = 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_STRINGIDM_ABOUTBOXstrAboutMenu);}} // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//  执行此操作SetIcon(m_hIconTRUE);// 设置大图标SetIcon(m_hIconFALSE);// 设置小图标complete_data_last_print = 0; // TODO: 在此添加额外的初始化代码MouleList.InsertColumn(0,TEXT("ipsrc"));MouleList.InsertColumn(1,TEXT("ipdes"));MouleList.InsertColumn(2,TEXT("ctx"));MouleList.SetExtendedStyle(MouleList.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);lst();MouleList.SetColumnWidth(0,120);MouleList.SetColumnWidth(1,120);MouleList.SetColumnWidth(2,120);return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE} void CpcapcatchDlg::OnSysCommand(UINT nIDLPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nIDlParam);}}void CpcapcatchDlg::lst(void){/*MouleList.InsertItem(0,"ModuleName");MouleList.SetItemText(0,1,"Module22Name");MouleList.SetItemText(0,2,"Mod33uleName");MouleList.SetItemText(0,3,"Modu44leName");*/} // 如果向对话框添加最小化按钮,则需要下面的代码//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,//  这将由框架自动完成。 void CpcapcatchDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGNDreinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中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(xym_hIcon);}else{CDialogEx::OnPaint();}} //当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CpcapcatchDlg::OnQueryDragIcon(){return static_cast<HCURSOR>(m_hIcon);}   void CpcapcatchDlg::OnBnClickedOk(){// TODO: 在此添加控件通知处理程序代码//CDialogEx::OnOK();int mi=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();CString sr;((CComboBox*)GetDlgItem(IDC_COMBO1))->GetLBText(mi,sr);//AfxMessageBox(sr);pcap_if_t *d;int i;char errbuf[PCAP_ERRBUF_SIZE]; for(d=alldevsi=0; imi-1 ;d=d->nexti++);if ((adhandlepcap_open_lived->name,// name of the device65536,// portion of the packet to capture. // 65536 grants that the whole packet will be captured on all the MACs.1,// promiscuous mode (nonzero means promiscuous)1000,// read timeouterrbuf// error buffer)) == NULL){//fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);AfxMessageBox("\nUnable to open the adapter. %s is not supported by WinPcap\n");/* Free the device list */pcap_freealldevs(alldevs);//return -1;}//AfxMessageBox("ok");pcap_freealldevs(alldevs);AfxBeginThread(Ctd,GetSafeHwnd());//OnTimer(1);SetTimer(1,10000,NULL);}bool is_readable(char c){   return isalnum(c) || ispunct(c) || isspace(c) || isprint(c); }void CpcapcatchDlg::OnBnClickedButton1(){CComboBoxnet_interface = (CComboBox*)GetDlgItem(IDC_COMBO1);CString interface_name;pcap_if_t *d;int i = 0;char errbuf[PCAP_ERRBUF_SIZE];//alldevs = get_dev_list();if(pcap_findalldevs(&alldevserrbuf) == -1){fprintf(stderr,"Error in pcap_findalldevs: %s\n"errbuf);exit(1);}if (alldevs == NULL){//return false; AfxMessageBox(_T("alldverr"));}for(d = alldevsd != NULL;d = d->next){if (d->description){interface_name = d->description;}else{interface_name = d->name;}net_interface->AddString(interface_name);}// TODO: 在此添加控件通知处理程序代码}CString iptostr(ip_address pa){CString sa="";sa.Format("%d.%d.%d.%d",pa.byte1,pa.byte2,pa.byte3,pa.byte4); return sa;}   void CpcapcatchDlg::OnBnClickedCancel(){// TODO: 在此添加控件通知处理程序代码CDialogEx::OnCancel();/*MouleList.InsertItem(0,"ModuleName");MouleList.SetItemText(0,1,"iptostr(ih->saddr)");MouleList.SetItemText(0,2,"iptostr(ih->daddr)");MouleList.SetItemText(0,3,"http_txt");*/}  void CpcapcatchDlg::OnNMRClickList1(NMHDR *pNMHDRLRESULT *pResult){LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);*pResult = 0;// TODO: 在此添加控件通知处理程序代码POSITION ps=MouleList.GetFirstSelectedItemPosition();//如果没得到位置就不做任何处理if (ps==NULL){return;}int nIndex=MouleList.GetNextSelectedItem(ps);//得到PIDCString szText=MouleList.GetItemText(nIndex,2);CEditpBoxOne; pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1); //付值 pBoxOne->SetWindowText(szText ); }  void CpcapcatchDlg::OnNMDblclkList1(NMHDR *pNMHDRLRESULT *pResult){LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);// TODO: 在此添加控件通知处理程序代码 POSITION ps=MouleList.GetFirstSelectedItemPosition();//如果没得到位置就不做任何处理if (ps==NULL){return;}int nIndex=MouleList.GetNextSelectedItem(ps);//得到PIDCString szText=MouleList.GetItemText(nIndex,2);CEditpBoxOne; pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1); //付值 pBoxOne->SetWindowText(szText );*pResult = 0;}  void CpcapcatchDlg::OnTimer(UINT_PTR nIDEvent){// TODO: 在此添加消息处理程序代码和/或调用默认值 CDialogEx::OnTimer(nIDEvent);while (si>complete_data_last_print){MouleList.InsertItem(complete_data_last_print,osr[complete_data_last_print].dip);MouleList.SetItemText(complete_data_last_print,1,osr[complete_data_last_print].sip);MouleList.SetItemText(complete_data_last_print,2,osr[complete_data_last_print].otxt);complete_data_last_print++;}}  void CpcapcatchDlg::OnBnClickedButton2(){// TODO: 在此添加控件通知处理程序代码complete_data_last_print=0;MouleList.DeleteAllItems();while (si>complete_data_last_print){MouleList.InsertItem(complete_data_last_print,osr[complete_data_last_print].dip);MouleList.SetItemText(complete_data_last_print,1,osr[complete_data_last_print].sip);MouleList.SetItemText(complete_data_last_print,2,osr[complete_data_last_print].otxt);complete_data_last_print++;}}
0 0
原创粉丝点击