NetPage.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:16k
- // NetPage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AntiEvilTools.h"
- #include "NetPage.h"
- #include "mstcpip.h"
- #include "iphlpapi.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- SOCKET SockRaw;
- char TcpFlag[6]={'F','S','R','P','A','U'}; //定义TCP标志位
- BOOL ParamTcp =false; // 关注TCP 报文
- BOOL ParamUdp =false; // 关注UDP 报文
- BOOL ParamIcmp =false; // 关注ICMP报文
- char *strFromIpFilter=NULL; // 源IP地址过滤
- char *strDestIpFilter=NULL; // 目的地址过滤
- char *strSensitive=NULL; // 敏感字符串
- int iPortFilter=0; // 端口过滤
- int iProtocol, iTTL;
- unsigned char IP1[4],IP2[4];
- //char szProtocol[MAX_PROTO_TEXT_LEN];
- //char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
- int DecodeTcpPack(char *, int); //TCP解包函数
- int DecodeUdpPack(char *, int); //UDP解包函数
- int DecodeIcmpPack(char *, int); //ICMP解包函数
- void CheckSockError(int, char*); //出错处理函数
- CString Port1,Port2;
- long total_num1=1; //全局变量,用于计数监听到的数据包的数量
- PROTN2T aOfProto [ PROTO_NUM + 1] = //为子协议映射表赋值
- {
- { IPPROTO_IP , "IP" },
- { IPPROTO_ICMP , "ICMP" },
- { IPPROTO_IGMP , "IGMP" },
- { IPPROTO_GGP , "GGP" },
- { IPPROTO_TCP , "TCP" },
- { IPPROTO_PUP , "PUP" },
- { IPPROTO_UDP , "UDP" },
- { IPPROTO_IDP , "IDP" },
- { IPPROTO_ND , "NP" },
- { IPPROTO_RAW , "RAW" },
- { IPPROTO_MAX , "MAX" },
- { NULL , "" }
- } ;
- /////////////////////////////////////////////////////////////////////////////
- // CNetPage property page
- IMPLEMENT_DYNCREATE(CNetPage, CPropertyPage)
- CNetPage::CNetPage() : CPropertyPage(CNetPage::IDD)
- {
- //{{AFX_DATA_INIT(CNetPage)
- m_check1 = TRUE;
- m_check2 = FALSE;
- m_check3 = FALSE;
- //}}AFX_DATA_INIT
- m_threadID = 0 ;
- m_Multihomed = FALSE ;
- m_Local = TRUE ;
- }
- CNetPage::~CNetPage()
- {
- }
- void CNetPage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CNetPage)
- DDX_Control(pDX, IDC_LIST2, m_ctrList);
- DDX_Control(pDX, IDC_IPADDRESS2, m_ip2);
- DDX_Control(pDX, IDC_IPADDRESS1, m_ip1);
- DDX_Control(pDX, IDC_BUTTON1, m_start);
- DDX_Check(pDX, IDC_CHECK1, m_check1);
- DDX_Check(pDX, IDC_CHECK2, m_check2);
- DDX_Check(pDX, IDC_CHECK3, m_check3);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CNetPage, CPropertyPage)
- //{{AFX_MSG_MAP(CNetPage)
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CNetPage message handlers
- void CNetPage::OnButton1()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- // TODO: Add your control notification handler code here
- char szErr [ 50 ] , szHostName[MAX_PATH];
- DWORD dwErr ;
- SOCKADDR_IN sa;
- ParamTcp= m_check1;
- ParamUdp =m_check2; // 关注UDP 报文
- ParamIcmp =m_check3;
- // m_check1 = true;
- // m_check2 = true;
- // m_check3 = true;
- m_ip1.GetAddress(IP1[0],IP1[1],IP1[2],IP1[3]);
- m_ip2.GetAddress(IP2[0],IP2[1],IP2[2],IP2[3]);
-
- GetDlgItemText(IDC_EDIT1,Port1);
- GetDlgItemText(IDC_EDIT2,Port2);
- gethostname(szHostName, sizeof(szHostName)) ; //得到本机主机名
- m_iphostsource = m_ipsource ;
-
- m_ipcheckedhost = ntohl(m_iphost) ;
- {
- CString str ;
- if( 0 == m_threadID ) //判断是否有监听线程在执行
- {
- str.LoadString( IDS_STOP ) ;
- m_start.SetWindowText( str ) ;
- }
- else
- {
- if( m_threadID ) //如果有线程,则向存在的线程发送结束消息
- {
- PostThreadMessage(m_threadID,WM_CLOSE,0,0) ;
- str.LoadString( IDS_START ) ;
- m_start.SetWindowText( str ) ;
- m_start.EnableWindow(FALSE) ;
- }
- return ;
- }
- DWORD dwBufferLen[10] ;
- DWORD dwBufferInLen= 1 ;
- DWORD dwBytesReturned = 0 ;
- m_s = socket( AF_INET , SOCK_RAW , IPPROTO_IP ) ; //创建socket连接
- if( INVALID_SOCKET == m_s )
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error socket() = %ld " , dwErr ) ;
- AfxMessageBox( szErr ) ;
- closesocket( m_s ) ;
- return ;
- }
- int rcvtimeo = 5000 ; //设置套字节超时选项
- if( setsockopt( m_s , SOL_SOCKET , SO_RCVTIMEO , (const char *)&rcvtimeo , sizeof(rcvtimeo) ) == SOCKET_ERROR)
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error WSAIoctl = %ld " , dwErr ) ;
- AfxMessageBox( szErr ) ;
- closesocket( m_s ) ;
- return ;
- }
- sa.sin_family = AF_INET;
- sa.sin_port = htons(7000);
- sa.sin_addr.s_addr= m_iphostsource;
- if (bind(m_s,(PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR) //绑定本机ip
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error bind() = %ld " , dwErr ) ;
- AfxMessageBox( szErr ) ;
- closesocket( m_s ) ;
- return ;
- }
- if( SOCKET_ERROR != WSAIoctl( m_s, SIO_RCVALL , &dwBufferInLen, sizeof(dwBufferInLen),
- &dwBufferLen, sizeof(dwBufferLen),
- &dwBytesReturned , NULL , NULL ) ) //设置网卡为混杂模式
- AfxBeginThread( threadFunc , (LPVOID)this ); //开启监听线程
- else
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error WSAIoctl = %ld " , dwErr ) ;
- AfxMessageBox( szErr ) ;
- closesocket( m_s ) ;
- return ;
- }
- }
-
- }
- void CNetPage::AddData(CString s0, CString s1, CString s2, CString s3, CString s4, CString s5, CString s6)
- {
- int index;
- CString str1="";
- str1.Format("%d",total_num1);
- index = m_ctrList.InsertItem(0,str1);
- m_ctrList.SetItem(index,1,LVIF_TEXT,s0, 0, 0, 0, 0);
- m_ctrList.SetItem(index,2,LVIF_TEXT,s1, 0, 0, 0, 0);
- m_ctrList.SetItem(index,3,LVIF_TEXT,s2, 0, 0, 0, 0);
- m_ctrList.SetItem(index,4,LVIF_TEXT,s3, 0, 0, 0, 0);
- m_ctrList.SetItem(index,5,LVIF_TEXT,s4, 0, 0, 0, 0);
- m_ctrList.SetItem(index,6,LVIF_TEXT,s5, 0, 0, 0, 0);
- m_ctrList.SetItem(index,7,LVIF_TEXT,s6, 0, 0, 0, 0);
- // m_ctrList.SetItem(index,7,LVIF_TEXT,s7, 0, 0, 0, 0);
- // m_ctrList.SetItem(index,8,LVIF_TEXT,s8, 0, 0, 0, 0);
- total_num1++;
- }
- void CNetPage::close_thread()
- {
- if( NULL != m_threadID )
- PostThreadMessage(m_threadID,WM_CLOSE,0,0) ; //向线程发送WM_CLOSE消息
- if( m_IPArr.GetSize() )
- m_IPArr.RemoveAll() ;
- }
- BOOL CNetPage::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
-
- // TODO: Add extra initialization here
- // total_num1=0;
- // Get local IP addr.
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
- CHAR szHostName[128] = {0};
- HOSTENT* pHost = NULL;
- CHAR* pszIp = NULL;
- int iNum = 0;
- wVersionRequested = MAKEWORD( 2, 0 );
- err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 )
- {
- /* Tell the user that we could not find a usable */
- /* WinSock DLL. */
- AfxMessageBox("Load WinSock DLL failed!");
- exit(1);
- }
- if(gethostname(szHostName, 128)==0)
- {
-
- pHost = gethostbyname(szHostName);
- if(pHost != NULL)
- {
- pszIp = inet_ntoa(*(in_addr*)pHost->h_addr_list[iNum]);
- m_ipsource = inet_addr(pszIp);
- }
- else AfxMessageBox("pHost = NULL!");
- //AfxMessageBox(pszIp);
- }
- else AfxMessageBox("can't find host name!");
- // ListView initialize
- DWORD dwStyle=GetWindowLong(m_ctrList.GetSafeHwnd(),GWL_STYLE);
- dwStyle&=~LVS_TYPEMASK;
- dwStyle|=LVS_REPORT;
- SetWindowLong(m_ctrList.GetSafeHwnd(),GWL_STYLE,dwStyle);
- //SetRedraw(TRUE);
-
- //初始化列表
- m_ctrList.InsertColumn(0,"数据",LVCFMT_LEFT,250);
- m_ctrList.InsertColumn(0,"数据包大小",LVCFMT_LEFT,80);
- m_ctrList.InsertColumn(0,"目的端口",LVCFMT_LEFT,60);
- m_ctrList.InsertColumn(0,"目的地址IP",LVCFMT_LEFT,100);
- m_ctrList.InsertColumn(0,"源端口",LVCFMT_LEFT,60);
- m_ctrList.InsertColumn(0,"源地址IP",LVCFMT_LEFT,100);
- m_ctrList.InsertColumn(0,"协议类型",LVCFMT_LEFT,60);
- m_ctrList.InsertColumn(0,"序号",LVCFMT_LEFT,40);
-
- ::SendMessage(m_ctrList.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
- LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
- SetWindowText("网络监听");
- /*---------------------------------------------------------------------*/
- // 读取主机所有ip
- /* DWORD dwSize = 0 ;
- GetIpAddrTable( NULL , &dwSize, FALSE ) ;
- PMIB_IPADDRTABLE pIpAddrTable = (PMIB_IPADDRTABLE )new BYTE [ dwSize ] ;
- if( pIpAddrTable )
- {
- if( GetIpAddrTable( (PMIB_IPADDRTABLE)pIpAddrTable, // // buffer for IP table
- &dwSize, // size of buffer
- FALSE // sort by IP address
- ) == NO_ERROR )
- {
- if( pIpAddrTable->dwNumEntries > 2 ) // Second is MS TCP loopback IP ( 127.0.0.1 )
- {
- m_Multihomed = TRUE ;
- char szIP[16];
- for( int i = 0 ; i < (int)pIpAddrTable->dwNumEntries ; i++ )
- {
- in_addr ina ;
- ina.S_un.S_addr = pIpAddrTable->table[i].dwAddr ;
- char *pIP = inet_ntoa( ina ) ;
- strcpy( szIP , pIP ) ;
- if( stricmp( szIP , "127.0.0.1" ) )
- m_IPArr.Add(pIpAddrTable->table[i].dwAddr) ;
- }
- }
- }
- delete [] pIpAddrTable ;
- }
- */ //m_check.SetCheck(TRUE) ;
- return TRUE; // return TRUE unless you set the focus to a control
- }
- char *get_proto_name( unsigned char proto ) //获取协议名称
- {
- BOOL bFound = FALSE ;
- for( int i = 0 ; i < PROTO_NUM ; i++ )
- {
- if( aOfProto[i].proto == proto )
- {
- bFound = TRUE ;
- break ;
- }
- }
- if( bFound )
- return aOfProto[i].pprototext ;
- return aOfProto[PROTO_NUM].pprototext ;
- }
- UINT threadFunc ( LPVOID p )
- {
- CNetPage *pDlg = static_cast<CNetPage *>(p) ;
- char buf [1000] , *bufwork ;
- MSG msg ;
- int iRet ;
- DWORD dwErr ;
- char *pSource , *pDest ;
- IPHEADER *pIpHeader ;
- in_addr ina ;
- char szSource [16] , szDest[16] , szErr [ 50 ];
- char *pLastBuf = NULL ;
- int HdrLen, totallen;
- WORD sourport, destport;
- //struct IPPacketHead *pIPHead; //定义不同结构协议头变量
- struct TCPPacketHead *pTCPHead;
- //struct ARPPacket *pARPHead;
- struct ICMPPacketHead *pICMPHead;
- struct UDPPacketHead *pUDPHead;
- BYTE *pdata = NULL;
- char *pMac=NULL;
- //检测用户消息
- PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) ; // Force to make the queue
- pDlg->m_threadID = GetCurrentThreadId() ; //获取当前线程id
-
- while( TRUE ) //开始截获数据包和解析循环
- {
- if( PeekMessage( &msg , 0 , WM_CLOSE,WM_CLOSE,PM_NOREMOVE ) )//检测WM_CLOSE消息,有则结束线程
- {
- closesocket( pDlg->m_s ) ;
- pDlg->m_threadID = 0 ;
- // Only after you see Next message you can press on Start button
- pDlg->m_start.EnableWindow(TRUE) ;
- break ;
- }
- memset( buf , 0 , sizeof(buf) ) ;//recv函数截获数据包,放在buf缓存中
- iRet = recv( pDlg->m_s , buf , sizeof( buf ) , 0 ) ;
- if( iRet == SOCKET_ERROR )
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
- continue ;
- }
- else
- if( *buf )//如果缓存中有数据包,则开始进行分析
- {
- // check IP here
- // sprintf( szErr , "recv() = %ld " , iRet ) ;
- // AfxMessageBox( szErr ) ;
- bufwork = buf ;
- pIpHeader = (IPHEADER *)bufwork ;//获取ip包头
- WORD iLen = ntohs(pIpHeader->total_len) ;
- while( TRUE )
- {
- if( iLen <= iRet )
- {
- //if( pDlg->IsIpofHost( pIpHeader ) )
- { // Form the line for m_list
- ina.S_un.S_addr = pIpHeader->sourceIP ;
- pSource = inet_ntoa( ina ) ;
- strcpy( szSource , pSource ) ;//源地址
- ina.S_un.S_addr = pIpHeader->destIP ;
- pDest = inet_ntoa( ina ) ;
- strcpy( szDest , pDest ) ;//目标地址
- CString ip1,ip2;
- ip1.Format("%d.%d.%d.%d",IP1[0],IP1[1],IP1[2],IP1[3]);
- ip2.Format("%d.%d.%d.%d",IP2[0],IP2[1],IP2[2],IP2[3]);
-
- if(!strcmp(ip1,szSource))
- break;
- if(!strcmp(ip2,szDest))
- break;
- CString str, strProto, strSourPort, strDestPort, strData, strSize,strMAC,strHost;
- strProto = get_proto_name( pIpHeader->proto ); //协议
- HdrLen = pIpHeader->header_len&0xf;
- HdrLen *= 4;
- totallen = ntohs(pIpHeader->total_len);
- totallen-=HdrLen;
- iProtocol = pIpHeader->proto;
- if((iProtocol==IPPROTO_TCP) && (ParamTcp))
- {
- pTCPHead=(struct TCPPacketHead *)(buf+HdrLen);
- sourport = ntohs(pTCPHead->SourPort);
- destport = ntohs(pTCPHead->DestPort);
- strSourPort.Format("%d",sourport);
- strDestPort.Format("%d",destport);
- if(strSourPort==Port1)
- break;
- if(strDestPort==Port2)
- break;
- HdrLen = (pTCPHead->HLen)>>4;
- HdrLen *= 4;
- pdata=((BYTE *)pTCPHead)+HdrLen;
- totallen -= HdrLen;
- strData.Format(" %s",pdata);
- strSize.Format("%d",totallen);
- //str = strIP+strL4+strData;
- pDlg->AddData(strProto,szSource,strSourPort,szDest,strDestPort,strSize,strData);
- }
- if((iProtocol==IPPROTO_UDP) && (ParamUdp))
- {
- pUDPHead=(struct UDPPacketHead *)(buf+HdrLen);
- sourport = ntohs(pUDPHead->SourPort);
- destport = ntohs(pUDPHead->DestPort);
- strSourPort.Format("%d",sourport);
- strDestPort.Format("%d",destport);
- if(strSourPort==Port1)
- break;
- if(strDestPort==Port2)
- break;
- pdata=((BYTE *)pUDPHead)+UDP_HEAD_LEN;
- totallen -= UDP_HEAD_LEN;
- strData.Format(" %s",pdata);
- strSize.Format("%d",totallen);
- //str = strIP+strL4+strData;
- pDlg->AddData(strProto,szSource,strSourPort,szDest,strDestPort,strSize,strData);
-
- }
- if((iProtocol==IPPROTO_ICMP) && (ParamIcmp))
- {
- pICMPHead=(struct ICMPPacketHead *)(buf+HdrLen);
- strSourPort = "-";
- strDestPort = "-";
- pdata=((BYTE *)pICMPHead)+ICMP_HEAD_LEN;
- totallen -= ICMP_HEAD_LEN;
- strData.Format("type:%d code:%d data:%s",pICMPHead->Type,pICMPHead->Code,pdata);
- strSize.Format("%d",totallen);
- //str = strIP+strL4+strData;
- pDlg->AddData(strProto,szSource,strSourPort,szDest,strDestPort,strSize,strData);
-
- }
- }
- if( iLen < iRet )
- {
- iRet -= iLen ;
- bufwork += iLen ;
- pIpHeader = (IPHEADER *)bufwork ;
- }
- else
- break ;
- }
- else
- { // read last part of buf. I wrote it , but always recv() read exactly
- // the lenght of the packet
- int iLast = iLen - iRet ;
- pLastBuf = new char [ iLen ] ;
- int iReaden = iRet ;
- memcpy( pLastBuf , bufwork , iReaden ) ;
- iRet = recv( pDlg->m_s , pLastBuf + iReaden , iLast , 0 ) ;
- if( iRet == SOCKET_ERROR )
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
- break ;
- }
- else
- {
- bufwork = pLastBuf ;
- pIpHeader = (IPHEADER *)bufwork ;
- if( iRet == iLast )
- iRet = iLen ;
- else
- { // read all last data
- iReaden += iRet ;
- iLast -= iRet ;
- while( TRUE )
- {
- iRet = recv( pDlg->m_s , pLastBuf +iReaden , iLast , 0 ) ;
- if( iRet == SOCKET_ERROR )
- {
- dwErr = WSAGetLastError() ;
- sprintf( szErr , "Error recv() = %ld " , dwErr ) ;
- break ;
- }
- else
- {
- iReaden += iRet ;
- iLast -= iRet ;
- if( iLast <= 0 )
- break ;
- }
- } // while
- }
- }
- }
- } // while
- if( pLastBuf )
- delete [ ] pLastBuf ;
- }
- else
- {
- AfxMessageBox( "No data on network" ) ;
- continue ;
- }
- }
- return TRUE ;
- }