123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- #include "CWindowsFireWall.h"
- #include <crtdbg.h>
- #include <objbase.h>
- #include <oleauto.h>
- #include <stdio.h>
- #include <tlhelp32.h>
- #include <Psapi.h>
- #include <strsafe.h>
- #include <QStringList>
- #include <QDebug>
- #include <QFileInfo>
- #include <QSettings>
- #include <comutil.h>
- #include <atlcomcli.h>
- #include<QDateTime>
- #include "logproc.h"
- #pragma comment( lib, "ole32.lib" )
- #pragma comment( lib, "oleaut32.lib" )
- std::shared_ptr<CWindowsFireWall> g_windowsFireWallPtr = nullptr;
- #define STRING_BUFFER_SIZE 500
- BOOL EnableDebugPrivilege()
- {
- HANDLE hToken;
- BOOL fOk = FALSE;
- if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
- {
- TOKEN_PRIVILEGES tp;
- tp.PrivilegeCount = 1;
- LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid);
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
- fOk = (GetLastError() == ERROR_SUCCESS);
- CloseHandle(hToken);
- }
- return fOk;
- }
- CWindowsFireWall::CWindowsFireWall(DWORD pid, bool bcheck) : m_pid(pid), m_bcheck(bcheck)
- {
- EnableDebugPrivilege();
- QFileInfo file("coe.cfgi");
- QString sFilePath = file.absoluteFilePath();
- QSettings set(sFilePath, QSettings::IniFormat);
- m_bModifyFireWall = set.value("wallConfig/ModifyWall", false).toBool();
- m_fw_profile2_domain_enabled = set.value("wallConfig/profile2_domain_enabled", VARIANT_TRUE).toInt();
- m_fw_profile2_private_enabled = set.value("wallConfig/profile2_private_enabled", VARIANT_TRUE).toInt();
- m_fw_profile2_public_enabled = set.value("wallConfig/profile2_public_enabled", VARIANT_TRUE).toInt();
- init();
- if(bcheck)
- {
- m_pThrd = new std::thread(std::bind(&CWindowsFireWall::threadProc, this));
- }
- }
- CWindowsFireWall::~CWindowsFireWall()
- {
- m_bIsRun = false;
- if(m_bcheck)
- {
- m_pThrd->join();
- cleanup();
- }
- }
- void CWindowsFireWall::threadProc()
- {
- setWallOn();
- removeLastRules();
- setAllRulesEnabled(VARIANT_FALSE);
- disableAllApp();
- __int64 nLastTime = QDateTime::currentDateTime().toSecsSinceEpoch();
- while(m_bIsRun)
- {
- __int64 nCurrentTime = QDateTime::currentDateTime().toSecsSinceEpoch();
- if(nCurrentTime - nLastTime > m_inprogressCheckSeconds)
- {
- nLastTime = nCurrentTime;
- checkInprogressFireWall();
- }
- Sleep(100);
- }
- }
- void CWindowsFireWall::checkInprogressFireWall()
- {
- //检测防火墙是否开启
- HRESULT hr;
- VARIANT_BOOL fw_profile2_domain_enabled = VARIANT_FALSE;
- VARIANT_BOOL fw_profile2_private_enabled = VARIANT_FALSE;
- VARIANT_BOOL fw_profile2_public_enabled = VARIANT_FALSE;
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, &fw_profile2_domain_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Domain: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- myServerLog()<<m_sErrMsg;
- }
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, &fw_profile2_private_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for private: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- myServerLog()<<m_sErrMsg;
- }
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, &fw_profile2_public_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for public: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- myServerLog()<<m_sErrMsg;
- }
- if(fw_profile2_domain_enabled != VARIANT_TRUE ||
- fw_profile2_private_enabled != VARIANT_TRUE ||
- fw_profile2_public_enabled != VARIANT_TRUE)
- {
- myServerLog()<<QString::fromLocal8Bit("考试中防火墙被关闭 ")<<",fw_profile2_domain_enabled:"<<fw_profile2_domain_enabled
- <<",fw_profile2_private_enabled:"<<fw_profile2_private_enabled
- <<",fw_profile2_public_enabled:"<<fw_profile2_public_enabled;
- setWallOn();
- }
- //遍历进程
- checkRulesIsEnabled();
- disableAllApp();
- }
- void CWindowsFireWall::checkRulesIsEnabled()
- {
- HRESULT hr = S_OK;
- CComVariant var;
- ULONG cFetched = 0;
- IUnknown *pEnumerator;
- IEnumVARIANT* pVariant = NULL;
- INetFwRules *pFwRules = NULL;
- INetFwRule *pFwRule = NULL;
- long fwRuleCount;
- // Retrieve INetFwRules
- hr = m_pNetFwPolicy2->get_Rules(&pFwRules);
- if (FAILED(hr))
- {
- wprintf(L"get_Rules failed: 0x%08lx\n", hr);
- }
- // Obtain the number of Firewall rules
- hr = pFwRules->get_Count(&fwRuleCount);
- if (FAILED(hr))
- {
- wprintf(L"get_Count failed: 0x%08lx\n", hr);
- }
- wprintf(L"The number of rules in the Windows Firewall are %d\n", fwRuleCount);
- // Iterate through all of the rules in pFwRules
- pFwRules->get__NewEnum(&pEnumerator);
- if(pEnumerator)
- {
- hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **) &pVariant);
- }
- while(SUCCEEDED(hr) && hr != S_FALSE)
- {
- var.Clear();
- hr = pVariant->Next(1, &var, &cFetched);
- if (S_FALSE != hr)
- {
- if (SUCCEEDED(hr))
- {
- hr = var.ChangeType(VT_DISPATCH);
- }
- if (SUCCEEDED(hr))
- {
- hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule), reinterpret_cast<void**>(&pFwRule));
- }
- if (SUCCEEDED(hr))
- {
- BSTR szRuleName;
- pFwRule->get_Name(&szRuleName);
- QString sRuleName = QString::fromStdWString(szRuleName);
- VARIANT_BOOL bEnable = VARIANT_FALSE;
- pFwRule->put_Enabled(bEnable);
- if(sRuleName.startsWith("disable_coe_"))
- {
- if(bEnable == VARIANT_FALSE)
- {
- pFwRule->put_Enabled(VARIANT_TRUE);
- }
- }
- else
- {
- if(bEnable == VARIANT_TRUE)
- {
- pFwRule->put_Enabled(VARIANT_FALSE);
- }
- }
- }
- }
- }
- // Release pFwRule
- if (pFwRule != NULL)
- {
- pFwRule->Release();
- }
- if(pFwRules != NULL)
- {
- pFwRules->Release();
- }
- }
- void CWindowsFireWall::uploadAppLog(std::vector<CProcessData> &vAppList)
- {
- QStringList appList;
- for(CProcessData data : vAppList)
- {
- appList << QString::fromStdWString(data.sExeName);
- }
- myServerLog()<<appList.join(",");
- }
- // Instantiate INetFwPolicy2
- HRESULT CWindowsFireWall::WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)
- {
- HRESULT hr = S_OK;
- hr = CoCreateInstance(
- __uuidof(NetFwPolicy2),
- NULL,
- CLSCTX_INPROC_SERVER,
- __uuidof(INetFwPolicy2),
- (void**)ppNetFwPolicy2);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("CoCreateInstance for INetFwPolicy2 failed: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- return hr;
- }
- bool CWindowsFireWall::init()
- {
- HRESULT hr = S_OK;
- // Initialize COM.
- m_comInit = CoInitializeEx(
- 0,
- COINIT_APARTMENTTHREADED
- );
- // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
- // initialized with a different mode. Since we don't care what the mode is,
- // we'll just use the existing mode.
- if (m_comInit != RPC_E_CHANGED_MODE)
- {
- if (FAILED(m_comInit))
- {
- m_sErrMsg = QString("CoInitializeEx for INetFwPolicy2 failed: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- }
- // Retrieve INetFwPolicy2
- hr = WFCOMInitialize(&m_pNetFwPolicy2);
- if (FAILED(hr))
- {
- return false;
- }
- return true;
- }
- void CWindowsFireWall::cleanup()
- {
- if(m_vDisableRuleNames.size() > 0)
- {
- INetFwRules *pNetFwRules = NULL;
- HRESULT hr = m_pNetFwPolicy2->get_Rules(&pNetFwRules);
- if (SUCCEEDED(hr))
- {
- for(std::wstring sRuleName: m_vDisableRuleNames)
- {
- pNetFwRules->Remove(const_cast<BSTR>(sRuleName.c_str()));
- }
- }
- }
- setAllRulesEnabled(VARIANT_TRUE);
- if(m_bModifyFireWall)
- {
- HRESULT hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, m_fw_profile2_domain_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Domain: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, m_fw_profile2_private_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Private: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, m_fw_profile2_public_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Public: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- m_bModifyFireWall = false;
- m_fw_profile2_domain_enabled = VARIANT_TRUE;
- m_fw_profile2_private_enabled = VARIANT_TRUE;
- m_fw_profile2_public_enabled = VARIANT_TRUE;
- QFileInfo file("coe.cfgi");
- QString sFilePath = file.absoluteFilePath();
- QSettings set(sFilePath, QSettings::IniFormat);
- set.setValue("wallConfig/ModifyWall", m_bModifyFireWall);
- set.setValue("wallConfig/profile2_domain_enabled", m_fw_profile2_domain_enabled);
- set.setValue("wallConfig/profile2_private_enabled", m_fw_profile2_private_enabled);
- set.setValue("wallConfig/profile2_public_enabled", m_fw_profile2_public_enabled);
- }
- if (m_pNetFwPolicy2 != NULL)
- {
- m_pNetFwPolicy2->Release();
- }
- if (SUCCEEDED(m_comInit))
- {
- CoUninitialize();
- }
- }
- bool CWindowsFireWall::setWallOn()
- {
- HRESULT hr;
- if(!m_bModifyFireWall)
- {
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, &m_fw_profile2_domain_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Domain: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, &m_fw_profile2_private_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for private: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- hr = m_pNetFwPolicy2->get_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, &m_fw_profile2_public_enabled);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for public: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- }
- hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, TRUE);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Domain: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, TRUE);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Private: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- hr = m_pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, TRUE);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("put_FirewallEnabled failed for Public: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- return false;
- }
- if(m_fw_profile2_domain_enabled != VARIANT_TRUE ||
- m_fw_profile2_private_enabled != VARIANT_TRUE ||
- m_fw_profile2_public_enabled != VARIANT_TRUE )
- {
- m_bModifyFireWall = true;
- QFileInfo file("coe.cfgi");
- QString sFilePath = file.absoluteFilePath();
- QSettings set(sFilePath, QSettings::IniFormat);
- set.setValue("wallConfig/ModifyWall", m_bModifyFireWall);
- set.setValue("wallConfig/profile2_domain_enabled", m_fw_profile2_domain_enabled);
- set.setValue("wallConfig/profile2_private_enabled", m_fw_profile2_private_enabled);
- set.setValue("wallConfig/profile2_public_enabled", m_fw_profile2_public_enabled);
- }
- return true;
- }
- void CWindowsFireWall::setAllRulesEnabled(VARIANT_BOOL bEnable)
- {
- HRESULT hr = S_OK;
- CComVariant var;
- ULONG cFetched = 0;
- IUnknown *pEnumerator;
- IEnumVARIANT* pVariant = NULL;
- INetFwRules *pFwRules = NULL;
- INetFwRule *pFwRule = NULL;
- long fwRuleCount;
- // Retrieve INetFwRules
- hr = m_pNetFwPolicy2->get_Rules(&pFwRules);
- if (FAILED(hr))
- {
- wprintf(L"get_Rules failed: 0x%08lx\n", hr);
- }
- // Obtain the number of Firewall rules
- hr = pFwRules->get_Count(&fwRuleCount);
- if (FAILED(hr))
- {
- wprintf(L"get_Count failed: 0x%08lx\n", hr);
- }
- wprintf(L"The number of rules in the Windows Firewall are %d\n", fwRuleCount);
- // Iterate through all of the rules in pFwRules
- pFwRules->get__NewEnum(&pEnumerator);
- if(pEnumerator)
- {
- hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **) &pVariant);
- }
- while(SUCCEEDED(hr) && hr != S_FALSE)
- {
- var.Clear();
- hr = pVariant->Next(1, &var, &cFetched);
- if (S_FALSE != hr)
- {
- if (SUCCEEDED(hr))
- {
- hr = var.ChangeType(VT_DISPATCH);
- }
- if (SUCCEEDED(hr))
- {
- hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule), reinterpret_cast<void**>(&pFwRule));
- }
- if (SUCCEEDED(hr))
- {
- // Output the properties of this rule
- // DumpFWRulesInCollection(pFwRule);
- pFwRule->put_Enabled(bEnable);
- }
- }
- }
- // Release pFwRule
- if (pFwRule != NULL)
- {
- pFwRule->Release();
- }
- if(pFwRules != NULL)
- {
- pFwRules->Release();
- }
- }
- void CWindowsFireWall::removeLastRules()
- {
- HRESULT hr = S_OK;
- CComVariant var;
- ULONG cFetched = 0;
- IUnknown *pEnumerator;
- IEnumVARIANT* pVariant = NULL;
- INetFwRules *pFwRules = NULL;
- INetFwRule *pFwRule = NULL;
- long fwRuleCount;
- // Retrieve INetFwRules
- hr = m_pNetFwPolicy2->get_Rules(&pFwRules);
- if (FAILED(hr))
- {
- wprintf(L"get_Rules failed: 0x%08lx\n", hr);
- }
- // Obtain the number of Firewall rules
- hr = pFwRules->get_Count(&fwRuleCount);
- if (FAILED(hr))
- {
- wprintf(L"get_Count failed: 0x%08lx\n", hr);
- }
- wprintf(L"The number of rules in the Windows Firewall are %d\n", fwRuleCount);
- // Iterate through all of the rules in pFwRules
- pFwRules->get__NewEnum(&pEnumerator);
- if(pEnumerator)
- {
- hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **) &pVariant);
- }
- QStringList RuleNamelist;
- while(SUCCEEDED(hr) && hr != S_FALSE)
- {
- var.Clear();
- hr = pVariant->Next(1, &var, &cFetched);
- if (S_FALSE != hr)
- {
- if (SUCCEEDED(hr))
- {
- hr = var.ChangeType(VT_DISPATCH);
- }
- if (SUCCEEDED(hr))
- {
- hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule), reinterpret_cast<void**>(&pFwRule));
- }
- if (SUCCEEDED(hr))
- {
- // Output the properties of this rule
- // DumpFWRulesInCollection(pFwRule);
- BSTR szRuleName;
- pFwRule->get_Name(&szRuleName);
- QString sRuleName = QString::fromStdWString(szRuleName);
- if(sRuleName.startsWith("disable_coe_"))
- {
- RuleNamelist<<sRuleName;
- }
- }
- }
-
- }
- for(QString &sRuleName : RuleNamelist)
- {
- std::wstring sName = sRuleName.toStdWString();
- pFwRules->Remove(const_cast<BSTR>(sName.c_str()));
- }
- // Release pFwRule
- if (pFwRule != NULL)
- {
- pFwRule->Release();
- }
- if(pFwRules != NULL)
- {
- pFwRules->Release();
- }
- }
- bool CWindowsFireWall::disableAllApp()
- {
- HRESULT hr = S_OK;
- std::vector<CProcessData> vAppList;
- if(getAllAppNameList(vAppList))
- {
- uploadAppLog(vAppList);
- }
- INetFwRules *pNetFwRules = NULL;
- hr = m_pNetFwPolicy2->get_Rules(&pNetFwRules);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("Failed to retrieve firewall rules collection : 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- for(CProcessData &data : vAppList)
- {
- if(data.bDisabled)
- {
- continue;
- }
- bool bSucceed = true;
- INetFwRule *pNetFwRule = NULL;
- INetFwRule2 *pNetFwRule2 = NULL;
- WCHAR pwszTemp[STRING_BUFFER_SIZE] = L"";
- BSTR RuleName = NULL;
- BSTR RuleGroupName = NULL;
- BSTR RuleDescription = NULL;
- BSTR RuleAppPath = NULL;
- hr = StringCchPrintfW(pwszTemp, STRING_BUFFER_SIZE, (L"disable_coe_" + data.sExeName).c_str());
- if (FAILED(hr))
- {
- m_sErrMsg = QString("Failed to compose a resource identifier string: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- RuleName = SysAllocString(pwszTemp);
- if (NULL == RuleName)
- {
- m_sErrMsg = QString("ERROR: Insufficient memory");
- bSucceed = false;
- }
- hr = StringCchPrintfW(pwszTemp, STRING_BUFFER_SIZE, L"coe");
- if (FAILED(hr))
- {
- m_sErrMsg = QString("Failed to compose a resource identifier string: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- RuleGroupName = SysAllocString(pwszTemp); // Used for grouping together multiple rules
- if (NULL == RuleGroupName)
- {
- m_sErrMsg = QString("ERROR: Insufficient memory");
- bSucceed = false;
- }
- hr = StringCchPrintfW(pwszTemp, STRING_BUFFER_SIZE, (L"disable " + data.sExeName).c_str());
- if (FAILED(hr))
- {
- m_sErrMsg = QString("Failed to compose a resource identifier string: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- RuleDescription = SysAllocString(pwszTemp);
- if (NULL == RuleDescription)
- {
- m_sErrMsg = "ERROR: Insufficient memory";
- bSucceed = false;
- }
- RuleAppPath = SysAllocString(data.sExePath.c_str());
- if (NULL == RuleAppPath)
- {
- m_sErrMsg = "ERROR: Insufficient memory";
- bSucceed = false;
- }
- // hr = m_pNetFwPolicy2->get_Rules(&pNetFwRules);
- // if (FAILED(hr))
- // {
- // m_sErrMsg = QString("Failed to retrieve firewall rules collection : 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- // bSucceed = false;
- // }
- hr = CoCreateInstance(
- __uuidof(NetFwRule), //CLSID of the class whose object is to be created
- NULL,
- CLSCTX_INPROC_SERVER,
- __uuidof(INetFwRule), // Identifier of the Interface used for communicating with the object
- (void**)&pNetFwRule);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("CoCreateInstance for INetFwRule failed: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Name(RuleName);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Name failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Grouping(RuleGroupName);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Grouping failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Description(RuleDescription);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Description failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Direction(NET_FW_RULE_DIR_OUT);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Direction failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Action(NET_FW_ACTION_BLOCK);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Action failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_ApplicationName(RuleAppPath);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_ApplicationName failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Profiles(NET_FW_PROFILE2_DOMAIN | NET_FW_PROFILE2_PRIVATE | NET_FW_PROFILE2_PUBLIC);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Profiles failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- hr = pNetFwRule->put_Enabled(VARIANT_TRUE);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_Enabled failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- // Check if INetFwRule2 interface is available (i.e Windows7+)
- // If supported, then use EdgeTraversalOptions
- // Else use the EdgeTraversal boolean flag.
- if (SUCCEEDED(pNetFwRule->QueryInterface(__uuidof(INetFwRule2), (void**)&pNetFwRule2)))
- {
- hr = pNetFwRule2->put_EdgeTraversalOptions(NET_FW_EDGE_TRAVERSAL_TYPE_DENY);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_EdgeTraversalOptions failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- }
- }
- else
- {
- hr = pNetFwRule->put_EdgeTraversal(VARIANT_FALSE);
- if ( FAILED(hr) )
- {
- m_sErrMsg = QString("Failed INetFwRule::put_EdgeTraversal failed with error: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- }
- hr = pNetFwRules->Add(pNetFwRule);
- if (FAILED(hr))
- {
- m_sErrMsg = QString("Failed to add firewall rule to the firewall rules collection: 0x%1").arg(hr, 8, 16, QLatin1Char('0'));
- bSucceed = false;
- }
- if(bSucceed)
- {
- data.bDisabled = true;
- m_vDisableRuleNames.push_back(RuleName);
- qDebug()<<QString::fromStdWString(RuleName);
- }
- else
- {
- qDebug()<<RuleName<<":"<<m_sErrMsg;
- }
- SysFreeString(RuleName);
- SysFreeString(RuleGroupName);
- SysFreeString(RuleDescription);
- SysFreeString(RuleAppPath);
- if (pNetFwRule2 != NULL)
- {
- pNetFwRule2->Release();
- }
- if (pNetFwRule != NULL)
- {
- pNetFwRule->Release();
- }
- }
- if (pNetFwRules != NULL)
- {
- pNetFwRules->Release();
- }
- return hr;
- }
- std::wstring GetPathByProcessID(DWORD pid)
- {
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
- if (!hProcess)
- {
- return L"";
- }
- WCHAR filePath[MAX_PATH];
- DWORD ret = GetModuleFileNameEx(hProcess, NULL, filePath, MAX_PATH) ;
- CloseHandle(hProcess);
- return ret == 0 ? L"" : filePath;
- }
- // dos 文件路径转 windows 文件路径
- BOOL DosPathToNtPath(LPTSTR pszDosPath, LPTSTR pszNtPath)
- {
- TCHAR szDriveStr[500];
- TCHAR szDrive[3];
- TCHAR szDevName[100];
- INT cchDevName;
- INT i;
- //检查参数
- if(!pszDosPath || !pszNtPath )
- return FALSE;
- //获取本地磁盘字符串
- if(GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr))
- {
- for(i = 0; szDriveStr[i]; i += 4)
- {
- if(!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\"))){continue;}
- szDrive[0] = szDriveStr[i];
- szDrive[1] = szDriveStr[i + 1];
- szDrive[2] = '\0';
- // 查询 Dos 设备名
- if(!QueryDosDevice(szDrive, szDevName, 100)){return FALSE;}
- // 命中
- cchDevName = lstrlen(szDevName);
- if(_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0){
- // 复制驱动器
- lstrcpy(pszNtPath, szDrive);
- // 复制路径
- lstrcat(pszNtPath, pszDosPath + cchDevName);
- return TRUE;
- }
- }
- }
- lstrcpy(pszNtPath, pszDosPath);
- return FALSE;
- }
- // 获取进程全路径
- BOOL GetProcessFullPath(DWORD dwPID, std::wstring &fullPath){
- TCHAR szImagePath[MAX_PATH]={'\0'};
- TCHAR pszFullPath[MAX_PATH]={'\0'};
- HANDLE hProcess;
- // 获取进程句柄失败
- hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID);
- if(!hProcess){return FALSE;}
- // 获取进程完整路径失败
- if(!GetProcessImageFileName(
- hProcess, // 进程句柄
- szImagePath, // 接收进程所属文件全路径的指针
- MAX_PATH // 缓冲区大小
- )){
- CloseHandle(hProcess);
- return FALSE;
- }
- // 路径转换失败
- if(!DosPathToNtPath(szImagePath, pszFullPath)){
- CloseHandle(hProcess);
- return FALSE;
- }
- CloseHandle(hProcess);
- // 导出文件全路径
- fullPath = pszFullPath;
- return TRUE;
- }
- bool CWindowsFireWall::getAllAppNameList(std::vector<CProcessData> &vAppList)
- {
- bool bRet = false;
- PROCESSENTRY32 pe32;
- pe32.dwSize = sizeof(pe32);
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hProcessSnap == INVALID_HANDLE_VALUE)
- {
- return bRet;
- }
- std::wstring sThisExePath = L"";
- GetProcessFullPath(m_pid, sThisExePath);
- QString sThisPath = QString::fromStdWString(sThisExePath).toLower();
- BOOL bMore = Process32First(hProcessSnap,&pe32);
- static QStringList list;
- while(bMore)
- {
- std::wstring sExePath = L"";
- GetProcessFullPath( pe32.th32ProcessID, sExePath);
- QString sPath = QString::fromStdWString(sExePath).toLower();
- if(pe32.th32ProcessID != m_pid &&
- !sExePath.empty() &&
- sThisPath != sPath &&
- sPath.toLower().indexOf("svchost.exe") < 0 &&
- list.indexOf(QString::fromStdWString(pe32.szExeFile).toLower()) < 0)
- {
- list<<QString::fromStdWString(pe32.szExeFile).toLower();
- CProcessData data;
- data.pid = pe32.th32ProcessID;
- data.sExeName = pe32.szExeFile;
- data.sExePath = sExePath;
- vAppList.push_back(data);
- bRet = true;
- }
- bMore = Process32Next(hProcessSnap,&pe32);
- }
- CloseHandle(hProcessSnap);
- return bRet;
- }
|