windows下c++设置防火墙
1、判断系统版本号:
string m_osDescribe; SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器 GetSystemInfo(&info); //调用GetSystemInfo函数填充结构 OSVERSIONINFOEX os; os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (!GetVersionEx((OSVERSIONINFO*)&os)) { return "false init"; } //下面根据版本信息判断操作系统名称 switch (os.dwMajorVersion) { //判断主版本号 case 4: switch (os.dwMinorVersion) { //判断次版本号 case 0: if (os.dwPlatformId == VER_PLATFORM_WIN32_NT) m_osDescribe = "Microsoft Windows NT 4.0"; //1996年7月发布 else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) m_osDescribe = "Microsoft Windows 95"; break; case 10: m_osDescribe = "Microsoft Windows 98"; break; case 90: m_osDescribe = "Microsoft Windows Me"; break; } break; case 5: switch (os.dwMinorVersion) { //再比较dwMinorVersion的值 case 0: m_osDescribe = "Microsoft Windows 2000"; //1999年12月发布 break; case 1: m_osDescribe = "Microsoft Windows XP"; //2001年8月发布 break; case 2: if (os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) m_osDescribe = "Microsoft Windows XP Professional x64 Edition"; else if (GetSystemMetrics(SM_SERVERR2) == 0) m_osDescribe = "Microsoft Windows Server 2003"; //2003年3月发布 else if (GetSystemMetrics(SM_SERVERR2) != 0) m_osDescribe = "Microsoft Windows Server 2003 R2"; break; } break; case 6: switch (os.dwMinorVersion) { case 0: if (os.wProductType == VER_NT_WORKSTATION) m_osDescribe = "Microsoft Windows Vista"; else m_osDescribe = "Microsoft Windows Server 2008"; //服务器版本 break; case 1: if (os.wProductType == VER_NT_WORKSTATION) m_osDescribe = "Microsoft Windows 7"; else m_osDescribe = "Microsoft Windows Server 2008 R2"; break; case 2: m_osDescribe = "Microsoft Windows 7"; break; } break; default: m_osDescribe = "Unknown System"; }
转载于:https://blog.csdn.net/qq_41719595/article/details/115030609
2、各系统设置防火墙:
//主版本号为5的操作系统:win2000,xp,Server 2003,Server 2003 R2 netsh firewall add allowedprogram "程序路径\程序名称.exe" "程序名称.exe" ENABLE //主版本号为6的操作系统:win vista,Server 2008,Server 2008 R2,win7,Server 2012,win8,Server 2012 R2,win8.1 //主版本号为10的操作系统:win10,Server2016 netsh advfirewall firewall del rule program="程序路径\程序名称.exe" name="自定义的规则名称" dir=in netsh advfirewall firewall add rule program="程序路径\程序名称.exe" name="自定义的规则名称" dir=in action=allow
3、资料:
这里的netsh命令操作防火墙主要是在Windows 7及以上版本,以示例说明:
1、防火墙入方向添加一条规则,规则名字:RDP,协议tcp,动作为允许,端口号3389;简而言之,就是允许其他机器远程桌面连接本机:
netsh advfirewall firewall add rule name=RDP dir=in action=allow protocol=tcp localport=3389
2、承上,我们删除这条规则
netsh advfirewall firewall delete rule name=RDP dir=in
那么这里为什么不直接引用上面的添加策略的命令,把add改成delete就行了呢?如下:
netsh advfirewall firewall delete rule name=RDP dir=in action=allow protocol=tcp localport=3389
Windows防火墙策略,图上,我们可以看到2个参数可以定位到你要删除哪条规则,第一个参数就是策略名字name=RDP,第二个参数是告诉命令行这个RDP是放在进站还在放在出站方向的,因此,以图中,2个参数即可定位,如果你非要再指定protocol=tcp localport=3389,那么也行。
常用参数解释:
必备参数,指定进站方向还是出站方向
dir=in|out
必备参数,设定这个规则是允许还是阻断或者是跳过
action=allow|block|bypass
可选参数,为某应用程序设定规则
program=<program path>]
可选参数,为某系统服务设定规则
[service=<service short name>|any]
可选参数,为这个规则加一个说明描述
[description=<string>]
可选参数,指定本端IP地址
[localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
可选参数,指定远端IP地址
[remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
可选参数,指定本端端口号,默认为所有端口
[localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|IPHTTPS|any (default=any)]
可选参数,指定远端端口号,默认为所有端口
[remoteport=0-65535|<port range>[,...]|any (default=any)]
可选参数,指定协议类型,默认为所有协议
[protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|tcp|udp|any (default=any)]
可选参数,指定接口类型
[interfacetype=wireless|lan|ras|any]
可选参数,指定加密访问方式
[security=authenticate|authenc|authdynenc|authnoencap|notrequired(default=notrequired)]
转载于:https://www.cnblogs.com/boltkiller/articles/5732497.html
4、其他设置:
/* 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, 80, 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; }
转载于:https://learn.microsoft.com/en-us/previous-versions//aa364726(v=vs.85)?redirectedfrom=MSDN
资料:https://blog.csdn.net/byxdaz/article/details/80382426