ARPRulePage.cpp
上传用户:zhuzhu0204
上传日期:2020-07-13
资源大小:13165k
文件大小:6k
- // ARPRulePage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "myfirewall.h"
- #include "ARPRulePage.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- int nThreadCounts = 0;
- int nMaxThreadCounts = 200;
- BOOL bIsARPRuleOpened = FALSE;
- ARPRule *pARPRuleList, *pARPRuleElem;
- DWORD WINAPI GetArpTableSubThread(LPVOID lpParam)
- {
- ULONG *p = (ULONG *)lpParam;
- ULONG ulIP = htonl(*p);
-
- // 发送ARP请求
- unsigned char arDestMac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
- ULONG ulLen = 6;
- ULONG ulRet = ::SendARP(ulIP, 0, (ULONG *)arDestMac, &ulLen);
- if (ulRet == NO_ERROR)
- {
- ARPRule *pNew = new ARPRule;
- in_addr in;
- in.S_un.S_addr = ulIP;
- pNew->ucIP[0] = in.S_un.S_un_b.s_b1;
- pNew->ucIP[1] = in.S_un.S_un_b.s_b2;
- pNew->ucIP[2] = in.S_un.S_un_b.s_b3;
- pNew->ucIP[3] = in.S_un.S_un_b.s_b4;
- memcpy(pNew->ucMac, arDestMac, sizeof(arDestMac));
- pNew->pNext = NULL;
- /* if (pARPRuleList == NULL)
- {
- pARPRuleList = pNew;
- pARPRuleElem = pNew;
- }
- else
- */ {
- pARPRuleElem->pNext = pNew;
- pARPRuleElem = pNew;
- }
- }
-
- nThreadCounts --;
-
- return ERROR_SUCCESS;
- }
- DWORD WINAPI GetArpTableThread(LPVOID lpParam)
- {
- CARPRulePage *pARPRulePage = (CARPRulePage *)lpParam;
-
- ULONG ulLocalIP; // 本机IP地址
- ULONG ulMask; // 子网掩码
- PIP_ADAPTER_INFO pAdapterInfo = NULL; // 适配器相关信息
- ULONG ulLen = 0;
-
- // 为适配器结构申请内存
- GetAdaptersInfo(pAdapterInfo, &ulLen);
- pAdapterInfo = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, ulLen);
-
- // 取得本机适配器结构信息
- if (GetAdaptersInfo(pAdapterInfo, &ulLen) == ERROR_SUCCESS)
- {
- if (pAdapterInfo != NULL)
- {
- ulLocalIP = inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
- ulMask = inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
- }
- }
- ULONG ulStartIP = ntohl(ulLocalIP) & ntohl(ulMask); // 局域网起始IP
- ULONG ulEndIP = ntohl(ulLocalIP) | (ntohl(ulMask) ^ 0xFFFFFFFF); // 局域网终止IP
-
- ULONG *pIP = new ULONG[ulEndIP - ulStartIP];
- for (ULONG i=ulStartIP; i<=ulEndIP; i++)
- {
- pIP[i-ulStartIP] = i;
- }
- // 保存本机IP地址和MAC到规则链表头节点处
- // IP地址
- in_addr in;
- in.S_un.S_addr = ulLocalIP;
- pARPRuleList->ucIP[0] = in.S_un.S_un_b.s_b1;
- pARPRuleList->ucIP[1] = in.S_un.S_un_b.s_b2;
- pARPRuleList->ucIP[2] = in.S_un.S_un_b.s_b3;
- pARPRuleList->ucIP[3] = in.S_un.S_un_b.s_b4;
- // MAC
- pARPRuleList->ucMac[0] = pAdapterInfo->Address[0];
- pARPRuleList->ucMac[1] = pAdapterInfo->Address[1];
- pARPRuleList->ucMac[2] = pAdapterInfo->Address[2];
- pARPRuleList->ucMac[3] = pAdapterInfo->Address[3];
- pARPRuleList->ucMac[4] = pAdapterInfo->Address[4];
- pARPRuleList->ucMac[5] = pAdapterInfo->Address[5];
-
- pARPRuleList->pNext = NULL;
-
- for (i=0; i<ulEndIP-ulStartIP; i++)
- {
- while (nThreadCounts > nMaxThreadCounts)
- {
- Sleep(10);
- }
- DWORD dwThreadId;
- if (CreateThread(NULL, 0, GetArpTableSubThread, &pIP[i], 0, &dwThreadId) != NULL)
- {
- nThreadCounts ++;
- }
- }
- // 确保所有的线程都已经结束
- while (nThreadCounts > 0);
- int nIndex = 0;
- for (ARPRule *p=pARPRuleList->pNext; p!=NULL; p=p->pNext)
- {
- CString strTemp;
- USES_CONVERSION;
- strTemp.Format(L"%d.%d.%d.%d", (p->ucIP[0]), (p->ucIP[1]), (p->ucIP[2]), (p->ucIP[3]));
- int nItem = pARPRulePage->m_ARPRuleList.InsertItem(nIndex, strTemp);
- UCHAR arDestMask[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
- UCHAR *s = p->ucMac;
- strTemp.Format(L"%02X-%02X-%02X-%02X-%02X-%02X", s[0],s[1],s[2],s[3],s[4],s[5]);
- pARPRulePage->m_ARPRuleList.SetItemText(nItem, 1, strTemp);
- nIndex++;
- }
- // 将ARP规则应用到核心层
- PtSetARPRules(bIsARPRuleOpened);
-
- return ERROR_SUCCESS;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CARPRulePage property page
- IMPLEMENT_DYNCREATE(CARPRulePage, CPropertyPage)
- CARPRulePage::CARPRulePage() : CPropertyPage(CARPRulePage::IDD)
- {
- //{{AFX_DATA_INIT(CARPRulePage)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- CARPRulePage::~CARPRulePage()
- {
- }
- void CARPRulePage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CARPRulePage)
- DDX_Control(pDX, IDC_ARP_RULE_LIST, m_ARPRuleList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CARPRulePage, CPropertyPage)
- //{{AFX_MSG_MAP(CARPRulePage)
- ON_BN_CLICKED(IDC_RADIO_OPEN, OnRadioOpen)
- ON_BN_CLICKED(IDC_RADIO_CLOSE, OnRadioClose)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CARPRulePage message handlers
- BOOL CARPRulePage::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
-
- // TODO: Add extra initialization here
- m_ARPRuleList.InsertColumn(0,L"IP地址",LVCFMT_LEFT,200,-1);
- m_ARPRuleList.InsertColumn(1,L"MAC地址", LVCFMT_LEFT, 200, -1);
- m_ARPRuleList.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
- m_ARPRuleList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
- bIsARPRuleOpened = TRUE;
- CheckRadioButton(IDC_RADIO_CLOSE, IDC_RADIO_OPEN, IDC_RADIO_OPEN);
- pARPRuleList = new ARPRule;
- pARPRuleElem = pARPRuleList;
- DWORD dwThreadId;
- m_hThread = CreateThread(NULL, 0, GetArpTableThread, this, 0, &dwThreadId);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CARPRulePage::OnRadioOpen()
- {
- // TODO: Add your control notification handler code here
- bIsARPRuleOpened = TRUE;
- PtSetARPRules(bIsARPRuleOpened);
- // 保存日志文件
- SaveLogFile("开启ARP攻击防范。");
- }
- void CARPRulePage::OnRadioClose()
- {
- // TODO: Add your control notification handler code here
- bIsARPRuleOpened = FALSE;
- PtSetARPRules(bIsARPRuleOpened);
- // 保存日志文件
- SaveLogFile("关闭ARP攻击防范。");
- }