• 【转】如何编程自动配置系统防火墙 (MSDN资料)


    2008年10月18日 星期六 09:57

    现在的网络程序都会在系统自带Windows 防火墙里面添加端口规则的了,其实这个使用“Windows Firewall” api接口就可以很简单的做到。

    http://msdn.microsoft.com/en-us/library/aa364695(VS.85).aspx

    option explicit
    
    Dim CurrentProfile
    
    ' Protocol
    Const NET_FW_IP_PROTOCOL_TCP = 6
    
    'Action
    Const NET_FW_ACTION_ALLOW = 1
    
    ' Create the FwPolicy2 object.
    Dim fwPolicy2
    Set fwPolicy2 = CreateObject("HNetCfg.FwPolicy2")
    
    ' Get the Rules object
    Dim RulesObject
    Set RulesObject = fwPolicy2.Rules
    
    CurrentProfile = fwPolicy2.CurrentProfileTypes
    
    'Create a Rule Object.
    Dim NewRule
    Set NewRule = CreateObject("HNetCfg.FWRule")
        
    NewRule.Name = "My Application Name"
    NewRule.Description = "Allow my application network traffic"
    NewRule.Applicationname = "%systemDrive%\\Program Files\\MyApplication.exe"
    NewRule.Protocol = NET_FW_IP_PROTOCOL_TCP
    NewRule.LocalPorts = 4000
    NewRule.Enabled = TRUE
    NewRule.Grouping = "@firewallapi.dll,-23255"
    NewRule.Profiles = CurrentProfile
    NewRule.Action = NET_FW_ACTION_ALLOW
        
    'Add a new rule
    RulesObject.Add NewRule
    option explicit
    
    Dim CurrentProfile
    
    ' Protocol
    Const NET_FW_IP_PROTOCOL_TCP = 6
    
    'Action
    Const NET_FW_ACTION_ALLOW = 1
    
    ' Create the FwPolicy2 object.
    Dim fwPolicy2
    Set fwPolicy2 = CreateObject("HNetCfg.FwPolicy2")
    
    ' Get the Rules object
    Dim RulesObject
    Set RulesObject = fwPolicy2.Rules
    
    CurrentProfile = fwPolicy2.CurrentProfileTypes
    
    'Create a Rule Object.
    Dim NewRule
    Set NewRule = CreateObject("HNetCfg.FWRule")
        
    NewRule.Name = "My Application Name"
    NewRule.Description = "Allow my application network traffic"
    NewRule.Applicationname = "%systemDrive%\\Program Files\\MyApplication.exe"
    NewRule.Protocol = NET_FW_IP_PROTOCOL_TCP
    NewRule.LocalPorts = 4000
    NewRule.Enabled = TRUE
    NewRule.Grouping = "@firewallapi.dll,-23255"
    NewRule.Profiles = CurrentProfile
    NewRule.Action = NET_FW_ACTION_ALLOW
        
    'Add a new rule
    RulesObject.Add NewRule


  • 相关阅读:
    Git学习笔记
    利用GitHub Pages和Hexo搭建个人博客
    dpkg的用法
    Ubuntu 16.04安装有道词典
    Linux 命令之删除命令
    Linux 命令之权限修改
    PHP学习记录第一篇:Ubuntu14.04下LAMP环境的搭建
    ubuntu时钟不显示的解决方法
    kali linux 2.0安装sublime text 2
    折腾kali linux2.0
  • 原文地址:https://www.cnblogs.com/SummerRain/p/1961340.html
Copyright © 2020-2023  润新知