Detect whether the firewall is turned on and determine whether the program is added to the firewall whitelist (source code attached)

Summary of the development of common functions of VC++ (list of column articles, welcome to subscribe, continuous updates...) icon-default.png?t=N7T8https://blog.csdn.net/chenlycly/article/details/124272585 C++ software anomaly troubleshooting series of tutorials from entry to proficiency (list of column articles) , welcome to subscribe and continue to update...) icon-default.png?t=N7T8https://blog.csdn.net/chenlycly/article/details/125529931 C++ software analysis tools from entry to mastery case collection (column article is being updated...) icon-default.png?t=N7T8https:/ /blog.csdn.net/chenlycly/article/details/131405795 C/C++ basics and advanced (column article, continuously updated...) icon-default.png?t=N7T8https://blog.csdn.net/chenlycly/category_11931267.html        This article is introduced through COM interfaces such as INetFwMgr, INetFwPolicy, and INetFwProfile are used to detect whether fire protection is enabled and whether the current process has been added to the firewall whitelist.

       For Windows 10 systems, the page to enable/disable the system firewall is as follows:

        The page to set up communication through Windows Firewall is as follows:

       The complete code for using the COM component to detect whether the firewall is turned on and whether the program is allowed to communicate through the firewall (add to the firewall whitelist) is as follows:

BOOL DetectFirewallSettings( BOOL* bExeEnableFW )
{
    BOOL status = FALSE;
    HRESULT hr = S_FALSE;
    
    INetFwMgr* fwMgr = NULL;
    INetFwPolicy* fwPolicy = NULL;
    INetFwProfile* fwProfile = NULL;
    INetFwAuthorizedApplications *apps = NULL;
    INetFwAuthorizedApplication  *app = NULL;
    FW_ERROR_CODE ret = FW_NOERROR;
    VARIANT_BOOL bFWEnabled;
    VARIANT_BOOL bDoNoteAllowExceptions;
    
    CoInitialize( NULL );
    
    try
    {
        // Create an instance of the firewall settings manager.
        hr = CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), (void**)&fwMgr );
        
        if( FAILED( hr ) )
        {
            throw FW_ERR_CREATE_SETTING_MANAGER;
        }
        
        // Retrieve the local firewall policy.
        hr = fwMgr->get_LocalPolicy( &fwPolicy );
        if( FAILED( hr ) )
        {
            throw FW_ERR_LOCAL_POLICY;
        }
        
        // Retrieve the firewall profile currently in effect
        hr = fwPolicy->get_CurrentProfile( &fwProfile );
        if( FAILED( hr ) )
        {
            throw FW_ERR_PROFILE;
        }
        
        // 1、是否开启了防火墙
        hr = fwProfile->get_FirewallEnabled( &bFWEnabled );
        if( FAILED( hr ) )
        {
            throw FW_ERR_FIREWALL_IS_ENABLED;
        }
 
        if( bFWEnabled )
        {
            // 开启了防火墙,再检测是否允许例外
            hr = fwProfile->get_ExceptionsNotAllowed( &bDoNoteAllowExceptions );
            if( FAILED(hr) )
            {
                throw FW_ERR_FIREWALL_IS_ENABLED;
            }
 
            if( bDoNoteAllowExceptions )
            {
                status = TRUE;
            }
            else
            {
                // 允许意外
                // 2、得到授权应用程序
                hr = fwProfile->get_AuthorizedApplications( &apps );
                if( FAILED(hr) )
                {
                    status = TRUE;
                    throw FW_ERR_FIREWALL_IS_ENABLED;
                }
 
                // 3、获取当前进程的进程名
                TCHAR szPEBuff[MAX_PATH]    = {0};
                GetModuleFileName( NULL, szPEBuff, MAX_PATH );
                BSTR bstrTemp = _bstr_t( szPEBuff );
 
                // 看当前进程在不在允许列表中
                hr = apps->Item( bstrTemp, &app );
                if( FAILED(hr) )
                {
                    // 不在允许列表中
                    status = TRUE;
                    throw FW_ERR_FIREWALL_IS_ENABLED;
                }
                else
                {
                    //在允许列表中,查看是否允许通过防火墙
                    hr = app->get_Enabled( &bFWEnabled );
                    if( FAILED(hr) )
                    {
                        status = TRUE;
                        throw FW_ERR_FIREWALL_IS_ENABLED;
                    }
 
                    if( bFWEnabled != VARIANT_FALSE )
                    {
                        status = FALSE;
                        if ( bExeEnableFW != NULL )
                        {
                            *bExeEnableFW = true;
                        }
                    }
                    else
                    {
                        status = TRUE;
                    }
                }
            }
        }
        
    }
    catch( FW_ERROR_CODE nError )
    {
        ret = nError;
    }
    
    if( fwPolicy )
    {
        fwPolicy->Release();
    }
    if( fwMgr )
    {
        fwMgr->Release();
    }
    
    CoUninitialize();
    
    return status;
}

       The return value of the above interface identifies whether the protection wall is enabled, and the outgoing parameter bExeEnableFW of the interface identifies whether the current program is allowed to communicate through the firewall. 

Guess you like

Origin blog.csdn.net/chenlycly/article/details/133493380