Sign in to follow this  
Followers 0
landetls

Quick Firewall Add Function

1 post in this topic

#1 ·  Posted (edited)

i thought i would share a script i wrote based on the firewall udf by jlogan3o13, the major difference is that this uses the "HNetCfg.FwPolicy2" native to vista, win 7 and win 8, this means it can set the firewall policy as both private and public profile. i wrote this for an udp streaming deployement script.

its a simple function, hopefully its self explanatory enough, based off both jlogan3o13's work and documentation from msdn.

#cs
----------------------------------------------------------------------------

AutoIt Version: 3.3.9.2 (beta)
Author:      LandeTLS

Script Function:
_AddFirewallProfile

Related:
Firewall UDF By jlogan3o13
----------------------------------------------------------------------------

example usage
#ce
$programfilesdir = @HomeDrive & "Program Files (x86)"

_AddFirewallProfile(1, "VLC media player", $programfilesdir & "VideoLANVLCvlc.exe")

Func _AddFirewallProfile($_intEnableDisable, $_appName, $_applicationFullPath, $_protocol = 17, $_port = 1234, $_direction = 1) ;Add/Enable/Disable Firewall Exception
If @OSVersion = "WIN_7" Or "WIN_8" Or "WIN_2008" Or "WIN_2008R2" Or "WIN_VISTA" Then
$Policy = ObjCreate("HNetCfg.FwPolicy2")
If Not @error Then
$RulesObject = $Policy.Rules
For $Rule In $RulesObject
If $Rule.name = $_appName Then $RulesObject.Remove($_appName)
Next
$newApplication = ObjCreate("HNetCfg.FWRule")
If Not @error Then
$newApplication.Name = $_appName
$newApplication.Description = $_appName
$newApplication.Applicationname = $_applicationFullPath
$newApplication.Protocol = $_protocol ; 17 = udp, 6 = tcp ; all = 0
If Not $_protocol = 0 Then $newApplication.LocalPorts = $_port
$newApplication.Direction = $_direction ; 1 = in; 2 = out
$newApplication.InterfaceTypes = "All"
$newApplication.Enabled = $_intEnableDisable
$newApplication.Profiles = 2147483647 ; 2147483647 = all
$newApplication.Action = 1 ; 1 = allow
$RulesObject.Add($newApplication)
Return 1
Else
Return SetError(2, 0, 0)
EndIf
Else
Return SetError(1, 0, 0)
EndIf
Else ; legacy
$Firewall = ObjCreate("HNetCfg.FwMgr")
If Not @error Then
$Policy = $Firewall.LocalPolicy
$Profile = $Policy.GetProfileByType(1)
$colApplications = $Profile.AuthorizedApplications
$newApplication = ObjCreate("HNetCfg.FwAuthorizedApplication")
If Not @error Then
$newApplication.Name = $_appName
$newApplication.IpVersion = 2
$newApplication.ProcessImageFileName = $_applicationFullPath
$newApplication.RemoteAddresses = "*"
$newApplication.Scope = 0
$newApplication.Enabled = $_intEnableDisable
$colApplications.Add($newApplication)
Return 1
Else
Return SetError(2, 0, 0)
EndIf
Else
Return SetError(1, 0, 0)
EndIf
EndIf
EndFunc ;==>_AddFirewallProfile
Edited by landetls

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0