- 浏览: 300027 次
- 性别:
- 来自: 广州
文章分类
Sample code for the Windows Firewall COM interface
/* Copyright (c) Microsoft Corporation SYNOPSIS Sample code for the Windows Firewall COM interface. */ #include <windows.h> #include <crtdbg.h> #include <netfw.h> #include <objbase.h> #include <oleauto.h> #include <stdio.h> #pragma comment( lib, "ole32.lib" ) #pragma comment( lib, "oleaut32.lib" ) HRESULT WindowsFirewallInitialize(OUT INetFwProfile** fwProfile) { HRESULT hr = S_OK; INetFwMgr* fwMgr = NULL; INetFwPolicy* fwPolicy = NULL; _ASSERT(fwProfile != NULL); *fwProfile = NULL; // Create an instance of the firewall settings manager. hr = CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwMgr), (void**)&fwMgr ); if (FAILED(hr)) { printf("CoCreateInstance failed: 0x%08lx\n", hr); goto error; } // Retrieve the local firewall policy. hr = fwMgr->get_LocalPolicy(&fwPolicy); if (FAILED(hr)) { printf("get_LocalPolicy failed: 0x%08lx\n", hr); goto error; } // Retrieve the firewall profile currently in effect. hr = fwPolicy->get_CurrentProfile(fwProfile); if (FAILED(hr)) { printf("get_CurrentProfile failed: 0x%08lx\n", hr); goto error; } error: // Release the local firewall policy. if (fwPolicy != NULL) { fwPolicy->Release(); } // Release the firewall settings manager. if (fwMgr != NULL) { fwMgr->Release(); } return hr; } void WindowsFirewallCleanup(IN INetFwProfile* fwProfile) { // Release the firewall profile. if (fwProfile != NULL) { fwProfile->Release(); } } HRESULT WindowsFirewallIsOn(IN INetFwProfile* fwProfile, OUT BOOL* fwOn) { HRESULT hr = S_OK; VARIANT_BOOL fwEnabled; _ASSERT(fwProfile != NULL); _ASSERT(fwOn != NULL); *fwOn = FALSE; // Get the current state of the firewall. hr = fwProfile->get_FirewallEnabled(&fwEnabled); if (FAILED(hr)) { printf("get_FirewallEnabled failed: 0x%08lx\n", hr); goto error; } // Check to see if the firewall is on. if (fwEnabled != VARIANT_FALSE) { *fwOn = TRUE; printf("The firewall is on.\n"); } else { printf("The firewall is off.\n"); } error: return hr; } HRESULT WindowsFirewallTurnOn(IN INetFwProfile* fwProfile) { HRESULT hr = S_OK; BOOL fwOn; _ASSERT(fwProfile != NULL); // Check to see if the firewall is off. hr = WindowsFirewallIsOn(fwProfile, &fwOn); if (FAILED(hr)) { printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr); goto error; } // If it is, turn it on. if (!fwOn) { // Turn the firewall on. hr = fwProfile->put_FirewallEnabled(VARIANT_TRUE); if (FAILED(hr)) { printf("put_FirewallEnabled failed: 0x%08lx\n", hr); goto error; } printf("The firewall is now on.\n"); } error: return hr; } HRESULT WindowsFirewallTurnOff(IN INetFwProfile* fwProfile) { HRESULT hr = S_OK; BOOL fwOn; _ASSERT(fwProfile != NULL); // Check to see if the firewall is on. hr = WindowsFirewallIsOn(fwProfile, &fwOn); if (FAILED(hr)) { printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr); goto error; } // If it is, turn it off. if (fwOn) { // Turn the firewall off. hr = fwProfile->put_FirewallEnabled(VARIANT_FALSE); if (FAILED(hr)) { printf("put_FirewallEnabled failed: 0x%08lx\n", hr); goto error; } printf("The firewall is now off.\n"); } error: return hr; } HRESULT WindowsFirewallAppIsEnabled( IN INetFwProfile* fwProfile, IN const wchar_t* fwProcessImageFileName, OUT BOOL* fwAppEnabled ) { HRESULT hr = S_OK; BSTR fwBstrProcessImageFileName = NULL; VARIANT_BOOL fwEnabled; INetFwAuthorizedApplication* fwApp = NULL; INetFwAuthorizedApplications* fwApps = NULL; _ASSERT(fwProfile != NULL); _ASSERT(fwProcessImageFileName != NULL); _ASSERT(fwAppEnabled != NULL); *fwAppEnabled = FALSE; // Retrieve the authorized application collection. hr = fwProfile->get_AuthorizedApplications(&fwApps); if (FAILED(hr)) { printf("get_AuthorizedApplications failed: 0x%08lx\n", hr); goto error; } // Allocate a BSTR for the process image file name. fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName); if (fwBstrProcessImageFileName == NULL) { hr = E_OUTOFMEMORY; printf("SysAllocString failed: 0x%08lx\n", hr); goto error; } // Attempt to retrieve the authorized application. hr = fwApps->Item(fwBstrProcessImageFileName, &fwApp); if (SUCCEEDED(hr)) { // Find out if the authorized application is enabled. hr = fwApp->get_Enabled(&fwEnabled); if (FAILED(hr)) { printf("get_Enabled failed: 0x%08lx\n", hr); goto error; } if (fwEnabled != VARIANT_FALSE) { // The authorized application is enabled. *fwAppEnabled = TRUE; printf( "Authorized application %lS is enabled in the firewall.\n", fwProcessImageFileName ); } else { printf( "Authorized application %lS is disabled in the firewall.\n", fwProcessImageFileName ); } } else { // The authorized application was not in the collection. hr = S_OK; printf( "Authorized application %lS is disabled in the firewall.\n", fwProcessImageFileName ); } error: // Free the BSTR. SysFreeString(fwBstrProcessImageFileName); // Release the authorized application instance. if (fwApp != NULL) { fwApp->Release(); } // Release the authorized application collection. if (fwApps != NULL) { fwApps->Release(); } return hr; } HRESULT WindowsFirewallAddApp( IN INetFwProfile* fwProfile, IN const wchar_t* fwProcessImageFileName, IN const wchar_t* fwName ) { HRESULT hr = S_OK; BOOL fwAppEnabled; BSTR fwBstrName = NULL; BSTR fwBstrProcessImageFileName = NULL; INetFwAuthorizedApplication* fwApp = NULL; INetFwAuthorizedApplications* fwApps = NULL; _ASSERT(fwProfile != NULL); _ASSERT(fwProcessImageFileName != NULL); _ASSERT(fwName != NULL); // First check to see if the application is already authorized. hr = WindowsFirewallAppIsEnabled( fwProfile, fwProcessImageFileName, &fwAppEnabled ); if (FAILED(hr)) { printf("WindowsFirewallAppIsEnabled failed: 0x%08lx\n", hr); goto error; } // Only add the application if it isn't already authorized. if (!fwAppEnabled) { // Retrieve the authorized application collection. hr = fwProfile->get_AuthorizedApplications(&fwApps); if (FAILED(hr)) { printf("get_AuthorizedApplications failed: 0x%08lx\n", hr); goto error; } // Create an instance of an authorized application. hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&fwApp ); if (FAILED(hr)) { printf("CoCreateInstance failed: 0x%08lx\n", hr); goto error; } // Allocate a BSTR for the process image file name. fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName); if (fwBstrProcessImageFileName == NULL) { hr = E_OUTOFMEMORY; printf("SysAllocString failed: 0x%08lx\n", hr); goto error; } // Set the process image file name. hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName); if (FAILED(hr)) { printf("put_ProcessImageFileName failed: 0x%08lx\n", hr); goto error; } // Allocate a BSTR for the application friendly name. fwBstrName = SysAllocString(fwName); if (SysStringLen(fwBstrName) == 0) { hr = E_OUTOFMEMORY; printf("SysAllocString failed: 0x%08lx\n", hr); goto error; } // Set the application friendly name. hr = fwApp->put_Name(fwBstrName); if (FAILED(hr)) { printf("put_Name failed: 0x%08lx\n", hr); goto error; } // Add the application to the collection. hr = fwApps->Add(fwApp); if (FAILED(hr)) { printf("Add failed: 0x%08lx\n", hr); goto error; } printf( "Authorized application %lS is now enabled in the firewall.\n", fwProcessImageFileName ); } error: // Free the BSTRs. SysFreeString(fwBstrName); SysFreeString(fwBstrProcessImageFileName); // Release the authorized application instance. if (fwApp != NULL) { fwApp->Release(); } // Release the authorized application collection. if (fwApps != NULL) { fwApps->Release(); } return hr; } HRESULT WindowsFirewallPortIsEnabled( IN INetFwProfile* fwProfile, IN LONG portNumber, IN NET_FW_IP_PROTOCOL ipProtocol, OUT BOOL* fwPortEnabled ) { HRESULT hr = S_OK; VARIANT_BOOL fwEnabled; INetFwOpenPort* fwOpenPort = NULL; INetFwOpenPorts* fwOpenPorts = NULL; _ASSERT(fwProfile != NULL); _ASSERT(fwPortEnabled != NULL); *fwPortEnabled = FALSE; // Retrieve the globally open ports collection. hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts); if (FAILED(hr)) { printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr); goto error; } // Attempt to retrieve the globally open port. hr = fwOpenPorts->Item(portNumber, ipProtocol, &fwOpenPort); if (SUCCEEDED(hr)) { // Find out if the globally open port is enabled. hr = fwOpenPort->get_Enabled(&fwEnabled); if (FAILED(hr)) { printf("get_Enabled failed: 0x%08lx\n", hr); goto error; } if (fwEnabled != VARIANT_FALSE) { // The globally open port is enabled. *fwPortEnabled = TRUE; printf("Port %ld is open in the firewall.\n", portNumber); } else { printf("Port %ld is not open in the firewall.\n", portNumber); } } else { // The globally open port was not in the collection. hr = S_OK; printf("Port %ld is not open in the firewall.\n", portNumber); } error: // Release the globally open port. if (fwOpenPort != NULL) { fwOpenPort->Release(); } // Release the globally open ports collection. if (fwOpenPorts != NULL) { fwOpenPorts->Release(); } return hr; } HRESULT WindowsFirewallPortAdd( IN INetFwProfile* fwProfile, IN LONG portNumber, IN NET_FW_IP_PROTOCOL ipProtocol, IN const wchar_t* name ) { HRESULT hr = S_OK; BOOL fwPortEnabled; BSTR fwBstrName = NULL; INetFwOpenPort* fwOpenPort = NULL; INetFwOpenPorts* fwOpenPorts = NULL; _ASSERT(fwProfile != NULL); _ASSERT(name != NULL); // First check to see if the port is already added. hr = WindowsFirewallPortIsEnabled( fwProfile, portNumber, ipProtocol, &fwPortEnabled ); if (FAILED(hr)) { printf("WindowsFirewallPortIsEnabled failed: 0x%08lx\n", hr); goto error; } // Only add the port if it isn't already added. if (!fwPortEnabled) { // Retrieve the collection of globally open ports. hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts); if (FAILED(hr)) { printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr); goto error; } // Create an instance of an open port. hr = CoCreateInstance( __uuidof(NetFwOpenPort), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwOpenPort), (void**)&fwOpenPort ); if (FAILED(hr)) { printf("CoCreateInstance failed: 0x%08lx\n", hr); goto error; } // Set the port number. hr = fwOpenPort->put_Port(portNumber); if (FAILED(hr)) { printf("put_Port failed: 0x%08lx\n", hr); goto error; } // Set the IP protocol. hr = fwOpenPort->put_Protocol(ipProtocol); if (FAILED(hr)) { printf("put_Protocol failed: 0x%08lx\n", hr); goto error; } // Allocate a BSTR for the friendly name of the port. fwBstrName = SysAllocString(name); if (SysStringLen(fwBstrName) == 0) { hr = E_OUTOFMEMORY; printf("SysAllocString failed: 0x%08lx\n", hr); goto error; } // Set the friendly name of the port. hr = fwOpenPort->put_Name(fwBstrName); if (FAILED(hr)) { printf("put_Name failed: 0x%08lx\n", hr); goto error; } // Opens the port and adds it to the collection. hr = fwOpenPorts->Add(fwOpenPort); if (FAILED(hr)) { printf("Add failed: 0x%08lx\n", hr); goto error; } printf("Port %ld is now open in the firewall.\n", portNumber); } error: // Free the BSTR. SysFreeString(fwBstrName); // Release the open port instance. if (fwOpenPort != NULL) { fwOpenPort->Release(); } // Release the globally open ports collection. if (fwOpenPorts != NULL) { fwOpenPorts->Release(); } return hr; } int __cdecl wmain(int argc, wchar_t* argv[]) { HRESULT hr = S_OK; HRESULT comInit = E_FAIL; INetFwProfile* fwProfile = NULL; // Initialize COM. comInit = CoInitializeEx( 0, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE ); // 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 (comInit != RPC_E_CHANGED_MODE) { hr = comInit; if (FAILED(hr)) { printf("CoInitializeEx failed: 0x%08lx\n", hr); goto error; } } // Retrieve the firewall profile currently in effect. hr = WindowsFirewallInitialize(&fwProfile); if (FAILED(hr)) { printf("WindowsFirewallInitialize failed: 0x%08lx\n", hr); goto error; } // Turn off the firewall. hr = WindowsFirewallTurnOff(fwProfile); if (FAILED(hr)) { printf("WindowsFirewallTurnOff failed: 0x%08lx\n", hr); goto error; } // Turn on the firewall. hr = WindowsFirewallTurnOn(fwProfile); if (FAILED(hr)) { printf("WindowsFirewallTurnOn failed: 0x%08lx\n", hr); goto error; } // Add Windows Messenger to the authorized application collection. hr = WindowsFirewallAddApp( fwProfile, L"%ProgramFiles%\\Messenger\\msmsgs.exe", L"Windows Messenger" ); if (FAILED(hr)) { printf("WindowsFirewallAddApp failed: 0x%08lx\n", hr); goto error; } // Add TCP::80 to list of globally open ports. hr = WindowsFirewallPortAdd(fwProfile, 880, NET_FW_IP_PROTOCOL_TCP, L"WWW"); if (FAILED(hr)) { printf("WindowsFirewallPortAdd failed: 0x%08lx\n", hr); goto error; } error: // Release the firewall profile. WindowsFirewallCleanup(fwProfile); // Uninitialize COM. if (SUCCEEDED(comInit)) { CoUninitialize(); } return 0; }
发表评论
-
ScreenCapture
2012-08-02 11:59 1061import java.awt.Rectangle; impo ... -
dos 命令 批处理bat 建立Windows快捷方式
2012-06-15 11:56 1372useage: shortcut.exe -f -t ... -
WMIC Command-Line Components
2012-02-05 09:03 840http://technet.microsoft.com/en ... -
java run dos command
2012-01-19 16:44 761Runtime.getRuntime().exec(" ... -
DOS set ipaddress
2012-01-13 21:18 785netsh interface ip set address ... -
JavaService把java程序发布为windows服务
2011-12-09 14:45 1030http://forge.ow2.org/projects/j ... -
WireShark找不到网卡的解决方法
2011-11-17 16:45 44311.将本机的c:设置为共享2.使用其他pc来访问该共享文件,找 ... -
ASCII Table
2011-11-14 20:15 1061ASCII Table and Description ... -
GB2312简体中文编码表
2011-11-14 15:25 2642code +0 +1 +2 +3 +4 +5 +6 + ... -
GBK 代码集字符定义表
2011-11-14 15:22 74517中文 Windows 95 GBK 代码 ... -
UNICODE汉字编码表
2011-11-14 11:21 82521 Unicode编码表 Uni ... -
AV Voice Changer
2011-06-28 16:58 1081AV Voice Changer -
zabbix客户端for windwos配置
2011-04-23 15:17 1585zabbix客户端for windwos配置 Zab ... -
windows xp 系统CMD命令大全
2011-04-19 17:29 999appwiz.cpl------------添加删除程序 ... -
简单实用 Windows 7系统上帝模式探秘
2011-04-19 14:14 898为私密文件穿件隐身 ... -
让Windows XP只运行某个应用程序
2011-04-15 17:29 1111如果Windows XP在启动之后自动进入某个应用程序,而不出 ...
相关推荐
Take advantage of the Windows Firewall and customize startup options Set up social media accounts, manage contacts, and use the Remote Desktop app Use OneDrive to save and share files Download and ...
该库为Go提供了使用Windows COM界面管理Windows防火墙的界面。 用法 请参阅的或C中的。 内部文件 C API 通常,Microsoft会期望您使用C ++ API。 从C对其进行访问的文献资料不多,但受到支持。 为了访问netfw.h C...
including English, Dutch, French, German, Spanish, Persian, Finnish, Japanese and Chinese and other.[1] The software uses the Windows API for its interface. It is available for both 32-bit x86 and 64...
Windows Server Core 是微软的一种精简版服务器操作系统,它专注于服务器的核心功能,如网络服务、文件服务和应用程序服务器等,而不包含图形用户界面。这个版本旨在提高安全性、减少维护成本并优化资源利用。以下是...
Windows Firewall Yes Yes Yes Yes Yes IE 8 Protected Mode and DEP support Yes Yes Yes Yes Yes Windows Update (can access Microsoft Update) Yes Yes Yes Yes Yes Fast User Switching Yes Yes Yes Yes ...
在Windows操作系统中,我们通常使用`.bat`或`.cmd`文件来编写批处理脚本。 二、开启端口 1. 使用`netsh`命令: `netsh interface portproxy add v4tov4 listenport=端口号 listenaddress=IP地址 connectport=端口...
Blocking Hackers with Windows Firewall Chapter : Conquering Malicious Software Chapter : Automatic Updates as Security Chapter : Troubleshooting Security Part III: Personalizing Windows...
17. **Internet Connection Firewall (ICF)**:互联网连接防火墙 18. **IPSec Services**:IP安全服务 19. **Logical Disk Manager Administrative Service**:逻辑磁盘管理器管理服务 20. **Messenger**:消息传递...
Burning COM Service`、`Indexing Service`、`Internet Connection Firewall (ICF)`、`IPSec Services`、`Logical Disk Manager Administrative Service`、`Messenger`、`MS Software Shadow Copy Provider`、`...
firewall-cmd –permanent –zone=trusted –change-interface=docker0 firewall-cmd –reload 补充知识:docker 启动mysql 容器出错Ports are not available: listen tcp 0.0.0.0:3306 错误截图如下 该错误是由于...
The company’s application for the Windows operating system is called Winbox, which provides a graphical interface to configure the router and monitor its function. At the same time, RouterOS ...
One of the tests is designed to check if the Windows firewall service is blocking the traffic between your router and the system, thus preventing UPnP from working. The results can be copied to your ...
在详细探讨NDIS中间层和过滤驱动开发的过程中,首先需要明确NDIS(Network Driver Interface Specification)网络驱动接口规范的角色与作用。NDIS定义了一组标准接口,它允许开发者能够编写能够在Windows平台上运行...
Softros LAN Messenger 是一款用于WAN, LAN 或INTRANET网络环境的消息工具,可以发送或接收信息。软件不需要服务器,使用简单,支持...Our program provides you with ergonomic, full Windows OS interface support.
- **Human Interface Device Access**:人机接口设备访问。除非有特殊需要,否则可禁用。 - **IMAPI CD-Burning COM Service**:光盘刻录服务。若不使用光驱刻录功能,则可禁用。 - **Indexing Service**:索引服务...
这通常通过控制面板的网络设置完成,包括选择"无线网络连接"属性,启用Windows配置,添加无线网络,输入SSID、认证方式、加密方式和密钥。 7. **结果验证** - 成功配置后,Station应能通过DHCP自动获取IP地址,并...
1. 防火墙规则:`iptables`在Linux中设置防火墙规则,`netsh advfirewall firewall`在Windows中管理防火墙。 2. SSH配置:`ssh-keygen`生成密钥对,`sshd_config`配置SSH服务器。 六、网络性能监控 1. `top`或`...
端口proxy命令(`netsh interface portproxy add v4tov4`)则配置了端口转发,将本地流量重定向到IP地址1.1.1.1的53端口,这是一个公共DNS服务器地址,这种异常行为可能用于数据窃取或中间人攻击。 此外,样本还...
- 超级终端程序(Windows XP系统自带,如果使用的是Windows 7或更高版本的操作系统,则需要从XP系统中复制相关文件)。 #### 三、连接路由器 1. **连接步骤**: - 使用Console线将路由器的Console口与电脑的串口...