WindowsFirewall.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. //#include <rpcsal.h> // MSDN的代码中这里没有包含这个头文件,导致编译不过
  4. #include <crtdbg.h>
  5. #include <objbase.h>
  6. #include <oleauto.h>
  7. #include <stdio.h>
  8. #pragma comment( lib, "ole32.lib" )
  9. #pragma comment( lib, "oleaut32.lib" )
  10. #include "WindowsFirewall.h"
  11. #include <strsafe.h>
  12. #include <comdef.h>
  13. #include <atlcomcli.h>
  14. //////////////////////////////////////////////////////////////////////////
  15. #define NET_FW_IP_PROTOCOL_TCP_NAME L"TCP"
  16. #define NET_FW_IP_PROTOCOL_UDP_NAME L"UDP"
  17. #define NET_FW_RULE_DIR_IN_NAME L"In"
  18. #define NET_FW_RULE_DIR_OUT_NAME L"Out"
  19. #define NET_FW_RULE_ACTION_BLOCK_NAME L"Block"
  20. #define NET_FW_RULE_ACTION_ALLOW_NAME L"Allow"
  21. #define NET_FW_RULE_ENABLE_IN_NAME L"TRUE"
  22. #define NET_FW_RULE_DISABLE_IN_NAME L"FALSE"
  23. #define STRING_BUFFER_SIZE 500
  24. //////////////////////////////////////////////////////////////////////////
  25. WinFireWallXP::WinFireWallXP() :fwProfile(NULL), m_hr(S_OK)
  26. {
  27. HRESULT comInit = CoInitializeEx(0,COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  28. if (comInit != RPC_E_CHANGED_MODE)
  29. {
  30. if (FAILED(comInit))
  31. {
  32. printf("CoInitializeEx failed: 0x%08lx\n", comInit);
  33. }
  34. }
  35. }
  36. WinFireWallXP::~WinFireWallXP()
  37. {
  38. FirewallCleanup();
  39. CoUninitialize();
  40. }
  41. BOOL WinFireWallXP::FirewallInit()
  42. {
  43. INetFwMgr *fwMgr = NULL;
  44. INetFwPolicy *fwPolicy = NULL;
  45. m_hr = CoCreateInstance(
  46. __uuidof(NetFwMgr),
  47. NULL,
  48. CLSCTX_INPROC_SERVER,
  49. __uuidof(INetFwMgr),
  50. (void **)&fwMgr);
  51. if (FAILED(m_hr))
  52. {
  53. printf("CoCreateInstance failed: 0x%08lx\n", m_hr);
  54. goto error;
  55. }
  56. m_hr = fwMgr->get_LocalPolicy(&fwPolicy);
  57. if (FAILED(m_hr))
  58. {
  59. printf("get_localPolicy failed: 0x%08lx\n", m_hr);
  60. goto error;
  61. }
  62. m_hr = fwPolicy->get_CurrentProfile(&fwProfile);
  63. if (FAILED(m_hr))
  64. {
  65. printf("get_CurrentProfile failed: 0x%08lx\n", m_hr);
  66. goto error;
  67. }
  68. error:
  69. if (fwPolicy != NULL)
  70. {
  71. fwPolicy->Release();
  72. }
  73. if (fwMgr != NULL)
  74. {
  75. fwMgr->Release();
  76. }
  77. return SUCCEEDED(m_hr) ? TRUE : FALSE;
  78. }
  79. void WinFireWallXP::FirewallCleanup()
  80. {
  81. if (fwProfile != NULL)
  82. {
  83. fwProfile->Release();
  84. }
  85. }
  86. BOOL WinFireWallXP::FirewallIsOn()
  87. {
  88. BOOL fwOn = FALSE;
  89. m_hr = S_OK;
  90. VARIANT_BOOL fwEnabled;
  91. _ASSERT(fwProfile != NULL);
  92. m_hr = fwProfile->get_FirewallEnabled(&fwEnabled);
  93. if (FAILED(m_hr))
  94. {
  95. printf("get_FirewallEnabled failed: 0x%08lx\n", m_hr);
  96. return FALSE;
  97. }
  98. if (fwEnabled != VARIANT_FALSE)
  99. {
  100. fwOn = TRUE;
  101. printf("The firewall is on.\n");
  102. }
  103. else
  104. {
  105. printf("The firewall is off.\n");
  106. }
  107. return fwOn;
  108. }
  109. BOOL WinFireWallXP::FirewallTurnOn()
  110. {
  111. _ASSERT(fwProfile != NULL);
  112. BOOL bRet = FALSE;
  113. if (FALSE == (bRet = FirewallIsOn()))
  114. {// 获取防火墙状态失败;
  115. if (FAILED(HGetLastError()))
  116. return FALSE;
  117. }
  118. m_hr = S_OK;
  119. if (!bRet)
  120. {
  121. m_hr = fwProfile->put_FirewallEnabled(VARIANT_TRUE);
  122. if (FAILED(m_hr))
  123. {
  124. printf("put_FirewallEnabled failed: 0x%08lx\n", m_hr);
  125. return FALSE;
  126. }
  127. printf("The firewall is now on.\n");
  128. }
  129. return TRUE;
  130. }
  131. BOOL WinFireWallXP::FirewallTurnOff()
  132. {
  133. _ASSERT(fwProfile != NULL);
  134. BOOL bRet = FALSE;
  135. if (FALSE == (bRet = FirewallIsOn()))
  136. {// 获取防火墙状态失败;
  137. if (FAILED(HGetLastError()))
  138. return FALSE;
  139. }
  140. m_hr = S_OK;
  141. if (bRet)
  142. {
  143. m_hr = fwProfile->put_FirewallEnabled(VARIANT_FALSE);
  144. if (FAILED(m_hr))
  145. {
  146. printf("put_FirewallEnabled failed: 0x%08lx\n", m_hr);
  147. return FALSE;
  148. }
  149. printf("The firewall is now on.\n");
  150. }
  151. return TRUE;
  152. }
  153. BOOL WinFireWallXP::FirewallAppIsEnable(IN LPCTSTR lpProcessFileName)
  154. {
  155. BSTR fwBstrProcessImageFileName = NULL;
  156. VARIANT_BOOL fwEnabled;
  157. INetFwAuthorizedApplication *fwApp = NULL;
  158. INetFwAuthorizedApplications *fwApps = NULL;
  159. _ASSERT(lpProcessFileName != NULL);
  160. BOOL fwAppEnabled = false;
  161. // 获取授权的程序集 ;
  162. m_hr = fwProfile->get_AuthorizedApplications(&fwApps);
  163. if (FAILED(m_hr))
  164. {
  165. printf("get_AuthorizedApplications failed: 0x%08lx\n", m_hr);
  166. goto error;
  167. }
  168. fwBstrProcessImageFileName = SysAllocString(lpProcessFileName);
  169. if (fwBstrProcessImageFileName == NULL)
  170. {
  171. m_hr = E_OUTOFMEMORY;
  172. printf("SysAllocString failed: 0x%08lx\n", m_hr);
  173. goto error;
  174. }
  175. m_hr = fwApps->Item(fwBstrProcessImageFileName, &fwApp);
  176. if (SUCCEEDED(m_hr))
  177. {
  178. m_hr = fwApp->get_Enabled(&fwEnabled);
  179. if (FAILED(m_hr))
  180. {
  181. printf("get_Enabled failed: 0x%08lx\n", m_hr);
  182. goto error;
  183. }
  184. if (fwEnabled != VARIANT_FALSE)
  185. {
  186. fwAppEnabled = TRUE;
  187. printf(
  188. "Authorized application %lS is enabled in the firewall.\n",
  189. lpProcessFileName
  190. );
  191. }
  192. else
  193. {
  194. printf(
  195. "Authorized application %lS is disabled in the firewall.\n",
  196. lpProcessFileName
  197. );
  198. }
  199. }
  200. else
  201. {
  202. m_hr = S_OK;
  203. printf(
  204. "Authorized application %lS is disabled in the firewall.\n",
  205. lpProcessFileName
  206. );
  207. }
  208. error:
  209. SysFreeString(fwBstrProcessImageFileName);
  210. if (fwApp != NULL)
  211. {
  212. fwApp->Release();
  213. }
  214. if (fwApps != NULL)
  215. {
  216. fwApps->Release();
  217. }
  218. return fwAppEnabled;
  219. }
  220. BOOL WinFireWallXP::FirewallAddApp(IN LPCTSTR lpProcessFileName, IN LPCTSTR lpName)
  221. {
  222. HRESULT hr = S_OK;
  223. BOOL fwAppEnabled;
  224. BSTR fwBstrName = NULL;
  225. BSTR fwBstrProcessImageFileName = NULL;
  226. INetFwAuthorizedApplication *fwApp = NULL;
  227. INetFwAuthorizedApplications *fwApps = NULL;
  228. _ASSERT(fwProfile != NULL);
  229. _ASSERT(lpProcessFileName != NULL);
  230. _ASSERT(lpName != NULL);
  231. fwAppEnabled = FirewallAppIsEnable(lpProcessFileName);
  232. if (!fwAppEnabled)
  233. {
  234. if (FAILED(HGetLastError()))
  235. return FALSE;
  236. m_hr = fwProfile->get_AuthorizedApplications(&fwApps);
  237. if (FAILED(m_hr))
  238. {
  239. printf("get_AuthorizedApplications failed: 0x%08lx\n", m_hr);
  240. goto error;
  241. }
  242. m_hr = CoCreateInstance(
  243. __uuidof(NetFwAuthorizedApplication),
  244. NULL,
  245. CLSCTX_INPROC_SERVER,
  246. __uuidof(INetFwAuthorizedApplication),
  247. (void**)&fwApp);
  248. if (FAILED(m_hr))
  249. {
  250. printf("CoCreateInstance failed: 0x%08lx\n", m_hr);
  251. goto error;
  252. }
  253. fwBstrProcessImageFileName = SysAllocString(lpProcessFileName);
  254. if (fwBstrProcessImageFileName == NULL)
  255. {
  256. m_hr = E_OUTOFMEMORY;
  257. printf("SysAllocString failed: 0x%08lx\n", m_hr);
  258. goto error;
  259. }
  260. m_hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName);
  261. if (FAILED(m_hr))
  262. {
  263. printf("put_ProcessImageFileName failed: 0x%08lx\n", m_hr);
  264. goto error;
  265. }
  266. fwBstrName = SysAllocString(lpName);
  267. if (SysStringLen(fwBstrName) == 0)
  268. {
  269. m_hr = E_OUTOFMEMORY;
  270. printf("SysAllocString failed: 0x%08lx\n", m_hr);
  271. goto error;
  272. }
  273. m_hr = fwApp->put_Name(fwBstrName);
  274. if (FAILED(hr))
  275. {
  276. printf("put_Name failed: 0x%08lx\n", m_hr);
  277. goto error;
  278. }
  279. m_hr = fwApps->Add(fwApp);
  280. if (FAILED(m_hr))
  281. {
  282. printf("Add failed: 0x%08lx\n", m_hr);
  283. goto error;
  284. }
  285. m_hr = S_OK;
  286. fwAppEnabled = TRUE;
  287. printf(
  288. "Authorized application %lS is now enabled in the firewall.\n",
  289. lpProcessFileName
  290. );
  291. }
  292. error:
  293. SysFreeString(fwBstrName);
  294. SysFreeString(fwBstrProcessImageFileName);
  295. if (fwApp != NULL)
  296. {
  297. fwApp->Release();
  298. }
  299. if (fwApps != NULL)
  300. {
  301. fwApps->Release();
  302. }
  303. return fwAppEnabled;
  304. }
  305. BOOL WinFireWallXP::FirewallPortIsEnable(IN LONG lPortNumber, IN NET_FW_IP_PROTOCOL netProtocol)
  306. {
  307. VARIANT_BOOL fwEnabled;
  308. INetFwOpenPort *fwOpenPort = NULL;
  309. INetFwOpenPorts *fwOpenPorts = NULL;
  310. _ASSERT(fwProfile != NULL);
  311. BOOL fwPortEnabled = FALSE;
  312. m_hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
  313. if (FAILED(m_hr))
  314. {
  315. printf("get_GloballyOpenPorts failed: 0x%08lx\n", m_hr);
  316. goto error;
  317. }
  318. // 获取端口的设备上下文;
  319. m_hr = fwOpenPorts->Item(lPortNumber, netProtocol, &fwOpenPort);
  320. if (SUCCEEDED(m_hr))
  321. {
  322. m_hr = fwOpenPort->get_Enabled(&fwEnabled);
  323. if (FAILED(m_hr))
  324. {
  325. printf("get_Enabled failed: 0x%08lx\n", m_hr);
  326. goto error;
  327. }
  328. if (fwEnabled != VARIANT_FALSE)
  329. {
  330. fwPortEnabled = TRUE;
  331. printf("Port %ld is open in the firewall.\n", lPortNumber);
  332. }
  333. else
  334. {
  335. printf("Port %ld is not open in the firewall.\n", lPortNumber);
  336. }
  337. }
  338. else
  339. {
  340. m_hr = S_OK;
  341. printf("Port %ld is not open in the firewall.\n", lPortNumber);
  342. }
  343. error:
  344. if (fwOpenPort != NULL)
  345. {
  346. fwOpenPort->Release();
  347. }
  348. if (fwOpenPorts != NULL)
  349. {
  350. fwOpenPorts->Release();
  351. }
  352. return fwPortEnabled;
  353. }
  354. BOOL WinFireWallXP::FirewallAddPort(IN LONG lPortNumber, IN NET_FW_IP_PROTOCOL netProtocol, IN LPCTSTR lpName)
  355. {
  356. BOOL fwPortEnabled;
  357. BSTR fwBstrName = NULL;
  358. INetFwOpenPort *fwOpenPort = NULL;
  359. INetFwOpenPorts *fwOpenPorts = NULL;
  360. _ASSERT(fwProfile != NULL);
  361. _ASSERT(lpName != NULL);
  362. fwPortEnabled = FirewallPortIsEnable(lPortNumber, netProtocol);
  363. if (!fwPortEnabled)
  364. {
  365. if (FAILED(HGetLastError()))
  366. return FALSE;
  367. m_hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
  368. if (FAILED(m_hr))
  369. {
  370. printf("get_GloballyOpenPorts failed: 0x%08lx\n", m_hr);
  371. goto error;
  372. }
  373. m_hr = CoCreateInstance(
  374. __uuidof(NetFwOpenPort),
  375. NULL,
  376. CLSCTX_INPROC_SERVER,
  377. __uuidof(INetFwOpenPort),
  378. (void**)&fwOpenPort);
  379. if (FAILED(m_hr))
  380. {
  381. printf("CoCreateInstance failed: 0x%08lx\n", m_hr);
  382. goto error;
  383. }
  384. m_hr = fwOpenPort->put_Port(lPortNumber);
  385. if (FAILED(m_hr))
  386. {
  387. printf("put_Port failed: 0x%08lx\n", m_hr);
  388. goto error;
  389. }
  390. m_hr = fwOpenPort->put_Protocol(netProtocol);
  391. if (FAILED(m_hr))
  392. {
  393. printf("put_Protocol failed: 0x%08lx\n", m_hr);
  394. goto error;
  395. }
  396. fwBstrName = SysAllocString(lpName);
  397. if (SysStringLen(fwBstrName) == 0)
  398. {
  399. m_hr = E_OUTOFMEMORY;
  400. printf("SysAllocString failed: 0x%08lx\n", m_hr);
  401. goto error;
  402. }
  403. m_hr = fwOpenPort->put_Name(fwBstrName);
  404. if (FAILED(m_hr))
  405. {
  406. printf("put_Name failed: 0x%08lx\n", m_hr);
  407. goto error;
  408. }
  409. m_hr = fwOpenPorts->Add(fwOpenPort);
  410. if (FAILED(m_hr))
  411. {
  412. printf("Add failed: 0x%08lx\n", m_hr);
  413. goto error;
  414. }
  415. m_hr = S_OK;
  416. fwPortEnabled = TRUE;
  417. printf("Port %ld is now open in the firewall.\n", lPortNumber);
  418. }
  419. error:
  420. SysFreeString(fwBstrName);
  421. if (fwOpenPort != NULL)
  422. {
  423. fwOpenPort->Release();
  424. }
  425. if (fwOpenPorts != NULL)
  426. {
  427. fwOpenPorts->Release();
  428. }
  429. return fwPortEnabled;
  430. }
  431. BOOL WinFireWallXP::FirewallAddPortVista(IN LPCTSTR lpRulePorts, IN LPCTSTR lpRuleInterfaceType, IN LPCTSTR lpRuleName, IN LPCTSTR lpRuleDescription, IN LPCTSTR lpRuleGroupName)
  432. {
  433. if (lpRulePorts == NULL || lpRulePorts[0] == '\0' || lpRuleName == NULL || lpRuleName[0] == '\0')
  434. {
  435. return FALSE;
  436. }
  437. LONG lPort = _tstol(lpRulePorts);
  438. // 不处理lpRuleInterfaceType信息,默认any(tcp+udp);
  439. NET_FW_IP_PROTOCOL netProctocl = NET_FW_IP_PROTOCOL_ANY;
  440. return FirewallAddPort(lPort, netProctocl, lpRuleName);
  441. }
  442. int WinFireWallXP::FireWallTest()
  443. {
  444. HRESULT hr = FirewallInit();
  445. if (FAILED(hr))
  446. {
  447. printf("WindowsFirewallInitialize failed: 0x%08lx\n", hr);
  448. goto error;
  449. }
  450. hr = FirewallTurnOff();
  451. if (FAILED(hr))
  452. {
  453. printf("WindowsFirewallTurnOff failed: 0x%08lx\n", hr);
  454. goto error;
  455. }
  456. hr = FirewallTurnOn();
  457. if (FAILED(hr))
  458. {
  459. printf("WindowsFirewallTurnOn failed: 0x%08lx\n", hr);
  460. goto error;
  461. }
  462. hr = FirewallAddApp(L"%ProgramFiles%\\Messenger\\msmsgs.exe", L"Windows Messenger");
  463. if (FAILED(hr))
  464. {
  465. printf("WindowsFirewallAddApp failed: 0x%08lx\n", hr);
  466. goto error;
  467. }
  468. hr = FirewallAddPort( 80, NET_FW_IP_PROTOCOL_TCP, L"WWW");
  469. if (FAILED(hr))
  470. {
  471. printf("WindowsFirewallPortAdd failed: 0x%08lx\n", hr);
  472. goto error;
  473. }
  474. error:
  475. FirewallCleanup();
  476. return 0;
  477. }
  478. #if _MSC_VER >= 1600
  479. //////////////////////////////////////////////////////////////////////////
  480. WinFireWallVista::WinFireWallVista() :pNetFwPolicy2(NULL)
  481. {
  482. // Initialize COM.
  483. HRESULT hrComInit = CoInitializeEx(0,COINIT_APARTMENTTHREADED);
  484. // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
  485. // initialized with a different mode. Since we don't care what the mode is,
  486. // we'll just use the existing mode.
  487. if (hrComInit != RPC_E_CHANGED_MODE)
  488. {
  489. if (FAILED(hrComInit))
  490. {
  491. printf("CoInitializeEx failed: 0x%08lx\n", hrComInit);
  492. }
  493. }
  494. }
  495. WinFireWallVista::~WinFireWallVista()
  496. {
  497. FirewallCleanup();
  498. CoUninitialize();
  499. }
  500. BOOL WinFireWallVista::FirewallInit()
  501. {
  502. m_hr = CoCreateInstance(
  503. __uuidof(NetFwPolicy2),
  504. NULL,
  505. CLSCTX_INPROC_SERVER,
  506. __uuidof(INetFwPolicy2),
  507. (void**)&pNetFwPolicy2);
  508. if (FAILED(m_hr))
  509. {
  510. printf("CoCreateInstance for INetFwPolicy2 failed: 0x%08lx\n", m_hr);
  511. return FALSE;
  512. }
  513. return TRUE;
  514. }
  515. void WinFireWallVista::FirewallCleanup()
  516. {
  517. if (pNetFwPolicy2 != NULL)
  518. {
  519. pNetFwPolicy2->Release();
  520. }
  521. }
  522. /************************************************************************/
  523. /* 函数:[5/8/2017 Jeff];
  524. /* 描述:;
  525. /* 参数:;
  526. /* [IN] :;
  527. /* [OUT] :;
  528. /* [IN/OUT] :;
  529. /* 返回:void;
  530. /* 注意:;
  531. /* 示例:;
  532. /*
  533. /* 修改:;
  534. /* 日期:;
  535. /* 内容:;
  536. /************************************************************************/
  537. BOOL WinFireWallVista::Get_FirewallSettings_PerProfileType(NET_FW_PROFILE_TYPE2 ProfileTypePassed)
  538. {
  539. VARIANT_BOOL bIsEnabled = FALSE;
  540. NET_FW_ACTION action;
  541. printf("******************************************\n");
  542. if (SUCCEEDED(pNetFwPolicy2->get_FirewallEnabled(ProfileTypePassed, &bIsEnabled)))
  543. {
  544. printf("Firewall is %s\n", bIsEnabled ? "enabled" : "disabled");
  545. if (!bIsEnabled)
  546. return FALSE;
  547. }
  548. if (SUCCEEDED(pNetFwPolicy2->get_BlockAllInboundTraffic(ProfileTypePassed, &bIsEnabled)))
  549. {
  550. printf("Block all inbound traffic is %s\n", bIsEnabled ? "enabled" : "disabled");
  551. if (!bIsEnabled)
  552. return FALSE;
  553. }
  554. if (SUCCEEDED(pNetFwPolicy2->get_NotificationsDisabled(ProfileTypePassed, &bIsEnabled)))
  555. {
  556. printf("Notifications are %s\n", bIsEnabled ? "disabled" : "enabled");
  557. if (!bIsEnabled)
  558. return FALSE;
  559. }
  560. if (SUCCEEDED(pNetFwPolicy2->get_UnicastResponsesToMulticastBroadcastDisabled(ProfileTypePassed, &bIsEnabled)))
  561. {
  562. printf("UnicastResponsesToMulticastBroadcast is %s\n", bIsEnabled ? "disabled" : "enabled");
  563. if (!bIsEnabled)
  564. return FALSE;
  565. }
  566. if (SUCCEEDED(pNetFwPolicy2->get_DefaultInboundAction(ProfileTypePassed, &action)))
  567. {
  568. printf("Default inbound action is %s\n", action != NET_FW_ACTION_BLOCK ? "Allow" : "Block");
  569. if (!bIsEnabled)
  570. return FALSE;
  571. }
  572. if (SUCCEEDED(pNetFwPolicy2->get_DefaultOutboundAction(ProfileTypePassed, &action)))
  573. {
  574. printf("Default outbound action is %s\n", action != NET_FW_ACTION_BLOCK ? "Allow" : "Block");
  575. if (!bIsEnabled)
  576. return FALSE;
  577. }
  578. printf("\n");
  579. return TRUE;
  580. }
  581. HRESULT WinFireWallVista::GetCurrentFirewallState()
  582. {
  583. HRESULT hr = S_FALSE;
  584. long CurrentProfilesBitMask = 0;
  585. VARIANT_BOOL bActualFirewallEnabled = VARIANT_FALSE;
  586. struct ProfileMapElement
  587. {
  588. NET_FW_PROFILE_TYPE2 Id;
  589. LPCWSTR Name;
  590. };
  591. ProfileMapElement ProfileMap[3];
  592. ProfileMap[0].Id = NET_FW_PROFILE2_DOMAIN;
  593. ProfileMap[0].Name = L"Domain";
  594. ProfileMap[1].Id = NET_FW_PROFILE2_PRIVATE;
  595. ProfileMap[1].Name = L"Private";
  596. ProfileMap[2].Id = NET_FW_PROFILE2_PUBLIC;
  597. ProfileMap[2].Name = L"Public";
  598. wprintf(L"\n\nCurrent Firewall State:\n");
  599. wprintf(L"-----------------------\n");
  600. hr = pNetFwPolicy2->get_CurrentProfileTypes(&CurrentProfilesBitMask);
  601. if (FAILED(hr))
  602. {
  603. wprintf(L"Failed to get CurrentProfileTypes. Error: %x.\n", hr);
  604. goto CLEANUP;
  605. }
  606. // The returned 'CurrentProfiles' bitmask can have more than 1 bit set if multiple profiles
  607. // are active or current at the same time
  608. for (int i = 0; i < 3; i++)
  609. {
  610. if (CurrentProfilesBitMask & ProfileMap[i].Id)
  611. {
  612. /*Is Firewall Enabled?*/
  613. hr = pNetFwPolicy2->get_FirewallEnabled(ProfileMap[i].Id, &bActualFirewallEnabled);
  614. if (FAILED(hr))
  615. {
  616. wprintf(L"Failed to get FirewallEnabled settings for %s profile. Error: %x.\n", ProfileMap[i].Name, hr);
  617. goto CLEANUP;
  618. }
  619. wprintf(L"On %s profile (Current) : Firewall state is %s\n", ProfileMap[i].Name, (bActualFirewallEnabled ? L"ON" : L"OFF"));
  620. }
  621. }
  622. CLEANUP:
  623. return hr;
  624. }
  625. // For the currently active firewall profiles display whether the rule group is enabled or not
  626. HRESULT WinFireWallVista::IsRuleGroupCurrentlyEnabled()
  627. {
  628. HRESULT hr = S_OK;
  629. VARIANT_BOOL bActualEnabled = VARIANT_FALSE;
  630. BSTR GroupName = SysAllocString(L"File and Printer Sharing");
  631. wprintf(L"\n\nIs 'File and Printer Sharing' rule group currently enabled ?\n");
  632. wprintf(L"------------------------------------------------------------\n");
  633. hr = pNetFwPolicy2->get_IsRuleGroupCurrentlyEnabled(GroupName, &bActualEnabled);
  634. if (SUCCEEDED(hr))
  635. {
  636. if (VARIANT_TRUE == bActualEnabled && S_OK == hr)
  637. {
  638. wprintf(L"Rule Group currently enabled on all the current profiles\n");
  639. }
  640. else if (VARIANT_TRUE == bActualEnabled && S_FALSE == hr)
  641. {
  642. wprintf(L"Rule Group currently enabled on some of the current profiles but not on all the current profiles\n");
  643. }
  644. else if (VARIANT_FALSE == bActualEnabled)
  645. {
  646. wprintf(L"Rule Group Currently not enabled on any of the current profiles\n");
  647. }
  648. }
  649. else
  650. {
  651. wprintf(L"Failed calling API IsRuleGroupCurrentlyEnabled. Error: 0x %x.\n", hr);
  652. goto Cleanup;
  653. }
  654. Cleanup:
  655. SysFreeString(GroupName);
  656. return hr;
  657. }
  658. // For the specified firewall profiles display whether the rule group is enabled or not
  659. HRESULT WinFireWallVista::IsRuleGroupEnabled()
  660. {
  661. HRESULT hr = S_OK;
  662. VARIANT_BOOL bActualEnabled = VARIANT_FALSE;
  663. BSTR GroupName = SysAllocString(L"File and Printer Sharing");
  664. wprintf(L"\n\nIs 'File and Printer Sharing' rule group enabled in public and private profiles ?\n");
  665. wprintf(L"---------------------------------------------------------------------------------\n");
  666. hr = pNetFwPolicy2->IsRuleGroupEnabled(NET_FW_PROFILE2_PRIVATE | NET_FW_PROFILE2_PUBLIC, GroupName, &bActualEnabled);
  667. if (SUCCEEDED(hr))
  668. {
  669. if (VARIANT_TRUE == bActualEnabled && S_OK == hr)
  670. {
  671. wprintf(L"Rule Group currently enabled on both public and private profiles\n");
  672. }
  673. else if (VARIANT_TRUE == bActualEnabled && S_FALSE == hr)
  674. {
  675. wprintf(L"Rule Group currently enabled on either public or private profile but not both\n");
  676. }
  677. else if (VARIANT_FALSE == bActualEnabled)
  678. {
  679. wprintf(L"Rule Group currently disabled on both public and private profiles\n");
  680. }
  681. }
  682. else
  683. {
  684. wprintf(L"Failed calling API IsRuleGroupCurrentlyEnabled. Error: 0x %x.\n", hr);
  685. goto Cleanup;
  686. }
  687. Cleanup:
  688. SysFreeString(GroupName);
  689. return hr;
  690. }
  691. // For the currently active firewall profiles display whether the changes to firewall rules will take effect or not
  692. HRESULT WinFireWallVista::GetLocalPolicyModifyState()
  693. {
  694. HRESULT hr;
  695. NET_FW_MODIFY_STATE modifystate;
  696. wprintf(L"\n\nWill changes to firewall rules take effect ?\n");
  697. wprintf(L"--------------------------------------------\n");
  698. hr = pNetFwPolicy2->get_LocalPolicyModifyState(&modifystate);
  699. if (FAILED(hr))
  700. {
  701. wprintf(L"Failed calling API get_LocalPolicyModifyState. Error: %x.\n", hr);
  702. return hr;
  703. }
  704. if (modifystate == NET_FW_MODIFY_STATE_OK)
  705. {
  706. if (hr == S_OK)
  707. {
  708. wprintf(L"Changing or adding firewall rule (or group) to the current profiles will take effect on all current profiles.\n");
  709. }
  710. else if (hr == S_FALSE)
  711. {
  712. wprintf(L"Changing or adding firewall rule (or group) to the current profiles will take effect on only some current profiles but not all.\n");
  713. }
  714. }
  715. else if (modifystate == NET_FW_MODIFY_STATE_GP_OVERRIDE)
  716. {
  717. if (hr == S_OK)
  718. {
  719. wprintf(L"Changing or adding a firewall rule (or group) to the current profiles will not take effect because group policy overrides it on all current profiles.\n");
  720. }
  721. else if (hr == S_FALSE)
  722. {
  723. wprintf(L"Changing or adding a firewall rule (or group) to the current profiles will not take effect because group policy overrides it on some of the current profiles.\n");
  724. }
  725. }
  726. else if (modifystate == NET_FW_MODIFY_STATE_INBOUND_BLOCKED)
  727. {
  728. if (hr == S_OK)
  729. {
  730. wprintf(L"Changing or adding firewall rule (or group) to the current profiles will not take effect because unsolicited inbound traffic is not allowed on all the current profiles.\n");
  731. }
  732. else if (hr == S_FALSE)
  733. {
  734. wprintf(L"Changing or adding firewall rule (or group) to the current profiles will not take effect because unsolicited inbound traffic is not allowed on some of the current profiles.\n");
  735. }
  736. }
  737. return hr;
  738. }
  739. // Output properties of a Firewall rule
  740. void WinFireWallVista::DumpFWRulesInCollection(INetFwRule* FwRule)
  741. {
  742. variant_t InterfaceArray;
  743. variant_t InterfaceString;
  744. VARIANT_BOOL bEnabled;
  745. BSTR bstrVal;
  746. long lVal = 0;
  747. long lProfileBitmask = 0;
  748. NET_FW_RULE_DIRECTION fwDirection;
  749. NET_FW_ACTION fwAction;
  750. struct ProfileMapElement
  751. {
  752. NET_FW_PROFILE_TYPE2 Id;
  753. LPCWSTR Name;
  754. };
  755. ProfileMapElement ProfileMap[3];
  756. ProfileMap[0].Id = NET_FW_PROFILE2_DOMAIN;
  757. ProfileMap[0].Name = L"Domain";
  758. ProfileMap[1].Id = NET_FW_PROFILE2_PRIVATE;
  759. ProfileMap[1].Name = L"Private";
  760. ProfileMap[2].Id = NET_FW_PROFILE2_PUBLIC;
  761. ProfileMap[2].Name = L"Public";
  762. wprintf(L"---------------------------------------------\n");
  763. if (SUCCEEDED(FwRule->get_Name(&bstrVal)))
  764. {
  765. wprintf(L"Name: %s\n", bstrVal);
  766. }
  767. if (SUCCEEDED(FwRule->get_Description(&bstrVal)))
  768. {
  769. wprintf(L"Description: %s\n", bstrVal);
  770. }
  771. if (SUCCEEDED(FwRule->get_ApplicationName(&bstrVal)))
  772. {
  773. wprintf(L"Application Name: %s\n", bstrVal);
  774. }
  775. if (SUCCEEDED(FwRule->get_ServiceName(&bstrVal)))
  776. {
  777. wprintf(L"Service Name: %s\n", bstrVal);
  778. }
  779. if (SUCCEEDED(FwRule->get_Protocol(&lVal)))
  780. {
  781. switch (lVal)
  782. {
  783. case NET_FW_IP_PROTOCOL_TCP:
  784. wprintf(L"IP Protocol: %s\n", NET_FW_IP_PROTOCOL_TCP_NAME);
  785. break;
  786. case NET_FW_IP_PROTOCOL_UDP:
  787. wprintf(L"IP Protocol: %s\n", NET_FW_IP_PROTOCOL_UDP_NAME);
  788. break;
  789. default:
  790. break;
  791. }
  792. if (lVal != NET_FW_IP_VERSION_V4 && lVal != NET_FW_IP_VERSION_V6)
  793. {
  794. if (SUCCEEDED(FwRule->get_LocalPorts(&bstrVal)))
  795. {
  796. wprintf(L"Local Ports: %s\n", bstrVal);
  797. }
  798. if (SUCCEEDED(FwRule->get_RemotePorts(&bstrVal)))
  799. {
  800. wprintf(L"Remote Ports: %s\n", bstrVal);
  801. }
  802. }
  803. else
  804. {
  805. if (SUCCEEDED(FwRule->get_IcmpTypesAndCodes(&bstrVal)))
  806. {
  807. wprintf(L"ICMP TypeCode: %s\n", bstrVal);
  808. }
  809. }
  810. }
  811. if (SUCCEEDED(FwRule->get_LocalAddresses(&bstrVal)))
  812. {
  813. wprintf(L"LocalAddresses: %s\n", bstrVal);
  814. }
  815. if (SUCCEEDED(FwRule->get_RemoteAddresses(&bstrVal)))
  816. {
  817. wprintf(L"RemoteAddresses: %s\n", bstrVal);
  818. }
  819. if (SUCCEEDED(FwRule->get_Profiles(&lProfileBitmask)))
  820. {
  821. // The returned bitmask can have more than 1 bit set if multiple profiles
  822. // are active or current at the same time
  823. for (int i = 0; i < 3; i++)
  824. {
  825. if (lProfileBitmask & ProfileMap[i].Id)
  826. {
  827. wprintf(L"Profile: %s\n", ProfileMap[i].Name);
  828. }
  829. }
  830. }
  831. if (SUCCEEDED(FwRule->get_Direction(&fwDirection)))
  832. {
  833. switch (fwDirection)
  834. {
  835. case NET_FW_RULE_DIR_IN:
  836. wprintf(L"Direction: %s\n", NET_FW_RULE_DIR_IN_NAME);
  837. break;
  838. case NET_FW_RULE_DIR_OUT:
  839. wprintf(L"Direction: %s\n", NET_FW_RULE_DIR_OUT_NAME);
  840. break;
  841. default:
  842. break;
  843. }
  844. }
  845. if (SUCCEEDED(FwRule->get_Action(&fwAction)))
  846. {
  847. switch (fwAction)
  848. {
  849. case NET_FW_ACTION_BLOCK:
  850. wprintf(L"Action: %s\n", NET_FW_RULE_ACTION_BLOCK_NAME);
  851. break;
  852. case NET_FW_ACTION_ALLOW:
  853. wprintf(L"Action: %s\n", NET_FW_RULE_ACTION_ALLOW_NAME);
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. if (SUCCEEDED(FwRule->get_Interfaces(&InterfaceArray)))
  860. {
  861. if (InterfaceArray.vt != VT_EMPTY)
  862. {
  863. SAFEARRAY *pSa = NULL;
  864. pSa = InterfaceArray.parray;
  865. for (long index = pSa->rgsabound->lLbound; index < (long)pSa->rgsabound->cElements; index++)
  866. {
  867. SafeArrayGetElement(pSa, &index, &InterfaceString);
  868. wprintf(L"Interfaces: %s\n", (BSTR)InterfaceString.bstrVal);
  869. }
  870. }
  871. }
  872. if (SUCCEEDED(FwRule->get_InterfaceTypes(&bstrVal)))
  873. {
  874. wprintf(L"Interface Types: %s\n", bstrVal);
  875. }
  876. if (SUCCEEDED(FwRule->get_Enabled(&bEnabled)))
  877. {
  878. if (bEnabled)
  879. {
  880. wprintf(L"Enabled: %s\n", NET_FW_RULE_ENABLE_IN_NAME);
  881. }
  882. else
  883. {
  884. wprintf(L"Enabled: %s\n", NET_FW_RULE_DISABLE_IN_NAME);
  885. }
  886. }
  887. if (SUCCEEDED(FwRule->get_Grouping(&bstrVal)))
  888. {
  889. wprintf(L"Grouping: %s\n", bstrVal);
  890. }
  891. if (SUCCEEDED(FwRule->get_EdgeTraversal(&bEnabled)))
  892. {
  893. if (bEnabled)
  894. {
  895. wprintf(L"Edge Traversal: %s\n", NET_FW_RULE_ENABLE_IN_NAME);
  896. }
  897. else
  898. {
  899. wprintf(L"Edge Traversal: %s\n", NET_FW_RULE_DISABLE_IN_NAME);
  900. }
  901. }
  902. }
  903. void WinFireWallVista::Enumerates()
  904. {
  905. HRESULT hrComInit = S_OK;
  906. HRESULT hr = S_OK;
  907. ULONG cFetched = 0;
  908. CComVariant var;
  909. IUnknown *pEnumerator;
  910. IEnumVARIANT* pVariant = NULL;
  911. INetFwPolicy2 *pNetFwPolicy2 = NULL;
  912. INetFwRules *pFwRules = NULL;
  913. INetFwRule *pFwRule = NULL;
  914. long fwRuleCount;
  915. // Retrieve INetFwRules
  916. hr = pNetFwPolicy2->get_Rules(&pFwRules);
  917. if (FAILED(hr))
  918. {
  919. wprintf(L"get_Rules failed: 0x%08lx\n", hr);
  920. goto Cleanup;
  921. }
  922. // Obtain the number of Firewall rules
  923. hr = pFwRules->get_Count(&fwRuleCount);
  924. if (FAILED(hr))
  925. {
  926. wprintf(L"get_Count failed: 0x%08lx\n", hr);
  927. goto Cleanup;
  928. }
  929. wprintf(L"The number of rules in the Windows Firewall are %d\n", fwRuleCount);
  930. // Iterate through all of the rules in pFwRules
  931. pFwRules->get__NewEnum(&pEnumerator);
  932. if (pEnumerator)
  933. {
  934. hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **)&pVariant);
  935. }
  936. while (SUCCEEDED(hr) && hr != S_FALSE)
  937. {
  938. var.Clear();
  939. hr = pVariant->Next(1, &var, &cFetched);
  940. if (S_FALSE != hr)
  941. {
  942. if (SUCCEEDED(hr))
  943. {
  944. hr = var.ChangeType(VT_DISPATCH);
  945. }
  946. if (SUCCEEDED(hr))
  947. {
  948. hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule), reinterpret_cast<void**>(&pFwRule));
  949. }
  950. if (SUCCEEDED(hr))
  951. {
  952. // Output the properties of this rule
  953. DumpFWRulesInCollection(pFwRule);
  954. }
  955. }
  956. }
  957. Cleanup:
  958. // Release pFwRule
  959. if (pFwRule != NULL)
  960. {
  961. pFwRule->Release();
  962. }
  963. // Release INetFwPolicy2
  964. if (pNetFwPolicy2 != NULL)
  965. {
  966. pNetFwPolicy2->Release();
  967. }
  968. // Uninitialize COM.
  969. if (SUCCEEDED(hrComInit))
  970. {
  971. CoUninitialize();
  972. }
  973. }
  974. BOOL WinFireWallVista::FirewallIsOn()
  975. {
  976. // 防火墙的三部分,只要有一部分未开启,认为未开启;
  977. if (Get_FirewallSettings_PerProfileType(NET_FW_PROFILE2_DOMAIN) &&
  978. Get_FirewallSettings_PerProfileType(NET_FW_PROFILE2_PRIVATE) &&
  979. Get_FirewallSettings_PerProfileType(NET_FW_PROFILE2_PUBLIC)
  980. )
  981. {
  982. return TRUE;
  983. }
  984. else
  985. {
  986. return FALSE;
  987. }
  988. }
  989. BOOL WinFireWallVista::FirewallTurnOn()
  990. {
  991. // Disable Windows Firewall for the Domain profile
  992. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, VARIANT_TRUE);
  993. if (FAILED(m_hr))
  994. {
  995. printf("put_FirewallEnabled failed for Domain: 0x%08lx\n", m_hr);
  996. return FALSE;
  997. }
  998. // Disable Windows Firewall for the Private profile
  999. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, VARIANT_TRUE);
  1000. if (FAILED(m_hr))
  1001. {
  1002. printf("put_FirewallEnabled failed for Private: 0x%08lx\n", m_hr);
  1003. return FALSE;
  1004. }
  1005. // Disable Windows Firewall for the Public profile
  1006. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, VARIANT_TRUE);
  1007. if (FAILED(m_hr))
  1008. {
  1009. printf("put_FirewallEnabled failed for Public: 0x%08lx\n", m_hr);
  1010. return FALSE;
  1011. }
  1012. return TRUE;
  1013. }
  1014. BOOL WinFireWallVista::FirewallTurnOff()
  1015. {
  1016. // Disable Windows Firewall for the Domain profile
  1017. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, VARIANT_FALSE);
  1018. if (FAILED(m_hr))
  1019. {
  1020. printf("put_FirewallEnabled failed for Domain: 0x%08lx\n", m_hr);
  1021. return FALSE;
  1022. }
  1023. // Disable Windows Firewall for the Private profile
  1024. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, VARIANT_FALSE);
  1025. if (FAILED(m_hr))
  1026. {
  1027. printf("put_FirewallEnabled failed for Private: 0x%08lx\n", m_hr);
  1028. return FALSE;
  1029. }
  1030. // Disable Windows Firewall for the Public profile
  1031. m_hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, VARIANT_FALSE);
  1032. if (FAILED(m_hr))
  1033. {
  1034. printf("put_FirewallEnabled failed for Public: 0x%08lx\n", m_hr);
  1035. return FALSE;
  1036. }
  1037. return TRUE;
  1038. }
  1039. BOOL WinFireWallVista::FirewallAppIsEnable(IN LPCTSTR lpProcessFileName)
  1040. {
  1041. return TRUE;
  1042. }
  1043. BOOL WinFireWallVista::FirewallAddApp(IN LPCTSTR lpRuleAppPath, IN LPCTSTR lpRuleName)
  1044. {
  1045. return FirewallAddAppVista(lpRuleAppPath, lpRuleName);
  1046. }
  1047. /************************************************************************/
  1048. /* 函数:[5/11/2017 IT];
  1049. /* 描述:;
  1050. /* 参数:;
  1051. /* [IN] lpRuleAppPath:应用程序路径;
  1052. /* [IN] lpRuleName:规则名称;
  1053. /* [IN] lpRuleDescription:规则描述;
  1054. /* [IN] lpRuleGroupName:规则组名;
  1055. /* 返回:void;
  1056. /* 注意:;
  1057. /* 示例:;
  1058. /*
  1059. /* 修改:;
  1060. /* 日期:;
  1061. /* 内容:;
  1062. /************************************************************************/
  1063. BOOL WinFireWallVista::FirewallAddAppVista(IN LPCTSTR lpRuleAppPath, IN LPCTSTR lpRuleName, IN LPCTSTR lpRuleDescription /* = NULL */, IN LPCTSTR lpRuleGroupName /* = NULL */)
  1064. {
  1065. if (lpRuleAppPath == NULL || lpRuleAppPath[0] == '\0' || lpRuleName == NULL || lpRuleName[0] == '\0' || _taccess(lpRuleAppPath, 0) == ENOENT)
  1066. {
  1067. m_hr = S_FALSE;
  1068. return FALSE;
  1069. }
  1070. m_hr = S_OK;
  1071. INetFwRules *pNetFwRules = NULL;
  1072. INetFwRule *pNetFwRule = NULL;
  1073. INetFwRule2 *pNetFwRule2 = NULL;
  1074. WCHAR pwszTemp[STRING_BUFFER_SIZE] = L"";
  1075. BSTR RuleName = NULL;
  1076. BSTR RuleGroupName = NULL;
  1077. BSTR RuleDescription = NULL;
  1078. BSTR RuleAppPath = NULL;
  1079. // 规则名称;
  1080. RuleName = SysAllocString(lpRuleName);
  1081. if (NULL == RuleName)
  1082. {
  1083. wprintf(L"\nERROR: Insufficient memory\n");
  1084. goto Cleanup;
  1085. }
  1086. // 规则组名称;
  1087. RuleGroupName = SysAllocString(lpRuleGroupName ? lpRuleGroupName : _T("")); // Used for grouping together multiple rules
  1088. if (NULL == RuleGroupName)
  1089. {
  1090. wprintf(L"\nERROR: Insufficient memory\n");
  1091. goto Cleanup;
  1092. }
  1093. // 规则描述;
  1094. RuleDescription = SysAllocString(lpRuleDescription ? lpRuleDescription : _T(""));
  1095. if (NULL == RuleDescription)
  1096. {
  1097. wprintf(L"\nERROR: Insufficient memory\n");
  1098. goto Cleanup;
  1099. }
  1100. // 规则应用程序路径;
  1101. RuleAppPath = SysAllocString(lpRuleAppPath);
  1102. if (NULL == RuleAppPath)
  1103. {
  1104. wprintf(L"\nERROR: Insufficient memory\n");
  1105. goto Cleanup;
  1106. }
  1107. m_hr = pNetFwPolicy2->get_Rules(&pNetFwRules);
  1108. if (FAILED(m_hr))
  1109. {
  1110. wprintf(L"Failed to retrieve firewall rules collection : 0x%08lx\n", m_hr);
  1111. goto Cleanup;
  1112. }
  1113. m_hr = CoCreateInstance(
  1114. __uuidof(NetFwRule), //CLSID of the class whose object is to be created
  1115. NULL,
  1116. CLSCTX_INPROC_SERVER,
  1117. __uuidof(INetFwRule), // Identifier of the Interface used for communicating with the object
  1118. (void**)&pNetFwRule);
  1119. if (FAILED(m_hr))
  1120. {
  1121. wprintf(L"CoCreateInstance for INetFwRule failed: 0x%08lx\n", m_hr);
  1122. goto Cleanup;
  1123. }
  1124. m_hr = pNetFwRule->put_Name(RuleName);
  1125. if (FAILED(m_hr))
  1126. {
  1127. wprintf(L"Failed INetFwRule::put_Name failed with error: 0x %x.\n", m_hr);
  1128. goto Cleanup;
  1129. }
  1130. m_hr = pNetFwRule->put_Grouping(RuleGroupName);
  1131. if (FAILED(m_hr))
  1132. {
  1133. wprintf(L"Failed INetFwRule::put_Grouping failed with error: 0x %x.\n", m_hr);
  1134. goto Cleanup;
  1135. }
  1136. m_hr = pNetFwRule->put_Description(RuleDescription);
  1137. if (FAILED(m_hr))
  1138. {
  1139. wprintf(L"Failed INetFwRule::put_Description failed with error: 0x %x.\n", m_hr);
  1140. goto Cleanup;
  1141. }
  1142. m_hr = pNetFwRule->put_Direction(NET_FW_RULE_DIR_IN);
  1143. if (FAILED(m_hr))
  1144. {
  1145. wprintf(L"Failed INetFwRule::put_Direction failed with error: 0x %x.\n", m_hr);
  1146. goto Cleanup;
  1147. }
  1148. m_hr = pNetFwRule->put_Action(NET_FW_ACTION_ALLOW);
  1149. if (FAILED(m_hr))
  1150. {
  1151. wprintf(L"Failed INetFwRule::put_Action failed with error: 0x %x.\n", m_hr);
  1152. goto Cleanup;
  1153. }
  1154. m_hr = pNetFwRule->put_ApplicationName(RuleAppPath);
  1155. if (FAILED(m_hr))
  1156. {
  1157. wprintf(L"Failed INetFwRule::put_ApplicationName failed with error: 0x %x.\n", m_hr);
  1158. goto Cleanup;
  1159. }
  1160. m_hr = pNetFwRule->put_Protocol(6); // TCP
  1161. if (FAILED(m_hr))
  1162. {
  1163. wprintf(L"Failed INetFwRule::put_Protocol failed with error: 0x %x.\n", m_hr);
  1164. goto Cleanup;
  1165. }
  1166. m_hr = pNetFwRule->put_Profiles(NET_FW_PROFILE2_ALL);
  1167. if (FAILED(m_hr))
  1168. {
  1169. wprintf(L"Failed INetFwRule::put_Profiles failed with error: 0x %x.\n", m_hr);
  1170. goto Cleanup;
  1171. }
  1172. m_hr = pNetFwRule->put_Enabled(VARIANT_TRUE);
  1173. if (FAILED(m_hr))
  1174. {
  1175. wprintf(L"Failed INetFwRule::put_Enabled failed with error: 0x %x.\n", m_hr);
  1176. goto Cleanup;
  1177. }
  1178. // Check if INetFwRule2 interface is available (i.e Windows7+)
  1179. // If supported, then use EdgeTraversalOptions
  1180. // Else use the EdgeTraversal boolean flag.
  1181. if (SUCCEEDED(pNetFwRule->QueryInterface(__uuidof(INetFwRule2), (void**)&pNetFwRule2)))
  1182. {
  1183. m_hr = pNetFwRule2->put_EdgeTraversalOptions(NET_FW_EDGE_TRAVERSAL_TYPE_DEFER_TO_APP);
  1184. if (FAILED(m_hr))
  1185. {
  1186. wprintf(L"Failed INetFwRule::put_EdgeTraversalOptions failed with error: 0x %x.\n", m_hr);
  1187. goto Cleanup;
  1188. }
  1189. }
  1190. else
  1191. {
  1192. m_hr = pNetFwRule->put_EdgeTraversal(VARIANT_TRUE);
  1193. if (FAILED(m_hr))
  1194. {
  1195. wprintf(L"Failed INetFwRule::put_EdgeTraversal failed with error: 0x %x.\n", m_hr);
  1196. goto Cleanup;
  1197. }
  1198. }
  1199. m_hr = pNetFwRules->Add(pNetFwRule);
  1200. if (FAILED(m_hr))
  1201. {
  1202. wprintf(L"Failed to add firewall rule to the firewall rules collection : 0x%08lx\n", m_hr);
  1203. goto Cleanup;
  1204. }
  1205. wprintf(L"Successfully added firewall rule !\n");
  1206. Cleanup:
  1207. SysFreeString(RuleName);
  1208. SysFreeString(RuleGroupName);
  1209. SysFreeString(RuleDescription);
  1210. SysFreeString(RuleAppPath);
  1211. if (pNetFwRule2 != NULL)
  1212. {
  1213. pNetFwRule2->Release();
  1214. }
  1215. if (pNetFwRule != NULL)
  1216. {
  1217. pNetFwRule->Release();
  1218. }
  1219. if (pNetFwRules != NULL)
  1220. {
  1221. pNetFwRules->Release();
  1222. }
  1223. return SUCCEEDED(m_hr);
  1224. }
  1225. BOOL WinFireWallVista::FirewallPortIsEnable(IN LONG lPortNumber, IN NET_FW_IP_PROTOCOL netProtocol)
  1226. {
  1227. return TRUE;
  1228. }
  1229. BOOL WinFireWallVista::FirewallAddPort(IN LONG lPortNumber, IN NET_FW_IP_PROTOCOL netProtocol, IN LPCTSTR lpName)
  1230. {
  1231. return TRUE;
  1232. }
  1233. /************************************************************************/
  1234. /* 函数:[5/11/2017 IT];
  1235. /* 描述:;
  1236. /* 参数:;
  1237. /* [IN] lpRulePorts:端口号,可以是连续的端口号"2015-2066",也可是多个独立的端口号"2015,2016,2017";
  1238. /* [IN] lpRuleInterfaceType:接口类型有4种:"RemoteAccess"远程访问, "Wireless"无线, "Lan"局域网, "All"全部 ;
  1239. /* [IN/OUT] :;
  1240. /* 返回:void;
  1241. /* 注意:;
  1242. /* 示例:;
  1243. /*
  1244. /* 修改:;
  1245. /* 日期:;
  1246. /* 内容:;
  1247. /************************************************************************/
  1248. BOOL WinFireWallVista::FirewallAddPortVista(IN LPCTSTR lpRulePorts, IN LPCTSTR lpRuleInterfaceType, IN LPCTSTR lpRuleName, IN LPCTSTR lpRuleDescription /* = NULL */, IN LPCTSTR lpRuleGroupName /* = NULL */)
  1249. {
  1250. if (lpRulePorts == NULL || lpRulePorts[0] == '\0' || lpRuleInterfaceType == NULL || lpRuleInterfaceType[0] == '\0')
  1251. {
  1252. m_hr = S_FALSE;
  1253. return FALSE;
  1254. }
  1255. m_hr = S_OK;
  1256. INetFwPolicy2 *pNetFwPolicy2 = NULL;
  1257. INetFwRules *pFwRules = NULL;
  1258. INetFwRule *pFwRule = NULL;
  1259. long CurrentProfilesBitMask = 0;
  1260. BSTR bstrRuleName = SysAllocString(lpRuleName);
  1261. BSTR bstrRuleDescription = SysAllocString(lpRuleDescription ? lpRuleDescription : _T(""));
  1262. BSTR bstrRuleGroup = SysAllocString(lpRuleGroupName ? lpRuleGroupName : _T(""));
  1263. BSTR bstrRuleLPorts = SysAllocString(lpRulePorts);
  1264. BSTR bstrRuleInterfaceType = NULL;
  1265. if ( lpRuleInterfaceType == NULL || lpRuleInterfaceType[0] == '\0')
  1266. bstrRuleInterfaceType = SysAllocString(_T("ALL"));
  1267. else
  1268. {
  1269. if (_tcsicmp(lpRuleInterfaceType, _T("RemoteAccess")) != 0 &&
  1270. _tcsicmp(lpRuleInterfaceType, _T("Wireless")) != 0 &&
  1271. _tcsicmp(lpRuleInterfaceType, _T("Lan")) != 0 &&
  1272. _tcsicmp(lpRuleInterfaceType, _T("all")) != 0
  1273. )
  1274. {
  1275. // 在输入错误的情况下,默认使用all;
  1276. bstrRuleInterfaceType = SysAllocString(_T("ALL"));
  1277. }
  1278. else
  1279. {
  1280. bstrRuleInterfaceType = SysAllocString(lpRuleInterfaceType);
  1281. }
  1282. }
  1283. // Retrieve INetFwRules
  1284. m_hr = pNetFwPolicy2->get_Rules(&pFwRules);
  1285. if (FAILED(m_hr))
  1286. {
  1287. printf("get_Rules failed: 0x%08lx\n", m_hr);
  1288. goto Cleanup;
  1289. }
  1290. // Retrieve Current Profiles bitmask
  1291. m_hr = pNetFwPolicy2->get_CurrentProfileTypes(&CurrentProfilesBitMask);
  1292. if (FAILED(m_hr))
  1293. {
  1294. printf("get_CurrentProfileTypes failed: 0x%08lx\n", m_hr);
  1295. goto Cleanup;
  1296. }
  1297. #if 0
  1298. // When possible we avoid adding firewall rules to the Public profile.
  1299. // If Public is currently active and it is not the only active profile, we remove it from the bitmask
  1300. if ((CurrentProfilesBitMask & NET_FW_PROFILE2_PUBLIC) &&
  1301. (CurrentProfilesBitMask != NET_FW_PROFILE2_PUBLIC))
  1302. {
  1303. CurrentProfilesBitMask ^= NET_FW_PROFILE2_PUBLIC;
  1304. }
  1305. #else
  1306. // 三个域都能通过;
  1307. CurrentProfilesBitMask = NET_FW_PROFILE2_ALL;
  1308. #endif
  1309. // Create a new Firewall Rule object.
  1310. m_hr = CoCreateInstance(
  1311. __uuidof(NetFwRule),
  1312. NULL,
  1313. CLSCTX_INPROC_SERVER,
  1314. __uuidof(INetFwRule),
  1315. (void**)&pFwRule);
  1316. if (FAILED(m_hr))
  1317. {
  1318. printf("CoCreateInstance for Firewall Rule failed: 0x%08lx\n", m_hr);
  1319. goto Cleanup;
  1320. }
  1321. // Populate the Firewall Rule object
  1322. pFwRule->put_Name(bstrRuleName);
  1323. pFwRule->put_Description(bstrRuleDescription);
  1324. pFwRule->put_Protocol(NET_FW_IP_PROTOCOL_TCP);
  1325. pFwRule->put_LocalPorts(bstrRuleLPorts);
  1326. pFwRule->put_Grouping(bstrRuleGroup);
  1327. pFwRule->put_InterfaceTypes(bstrRuleInterfaceType);
  1328. pFwRule->put_Profiles(CurrentProfilesBitMask);
  1329. pFwRule->put_Action(NET_FW_ACTION_ALLOW);
  1330. pFwRule->put_Enabled(VARIANT_TRUE);
  1331. // Add the Firewall Rule
  1332. m_hr = pFwRules->Add(pFwRule);
  1333. if (FAILED(m_hr))
  1334. {
  1335. printf("Firewall Rule Add failed: 0x%08lx\n", m_hr);
  1336. goto Cleanup;
  1337. }
  1338. Cleanup:
  1339. // Free BSTR's
  1340. SysFreeString(bstrRuleName);
  1341. SysFreeString(bstrRuleDescription);
  1342. SysFreeString(bstrRuleGroup);
  1343. SysFreeString(bstrRuleLPorts);
  1344. SysFreeString(bstrRuleInterfaceType);
  1345. // Release the INetFwRule object
  1346. if (pFwRule != NULL)
  1347. {
  1348. pFwRule->Release();
  1349. }
  1350. // Release the INetFwRules object
  1351. if (pFwRules != NULL)
  1352. {
  1353. pFwRules->Release();
  1354. }
  1355. // Release the INetFwPolicy2 object
  1356. if (pNetFwPolicy2 != NULL)
  1357. {
  1358. pNetFwPolicy2->Release();
  1359. }
  1360. return SUCCEEDED(m_hr);
  1361. }
  1362. #endif // #ifdef _MSC_VER >= 1500