Jump to content

Hello,

 

Was wondering if there is a easy way to control windows firewall from python?

Windows got this FirewallAPI.dll file that C# can use with this "using NetFwTypeLib;"

I did this in C#

INetFwRule blockout = (INetFwRule)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FWRule"));
            blockout.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
            blockout.Description = "BLOCKING THE PORT 8000 WHEN OUTBOUND";
            blockout.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
            blockout.Enabled = true;
            blockout.InterfaceTypes = "All";
            blockout.Name = "Port 8000";
            blockout.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
            blockout.LocalPorts = "8000";
            blockout.RemoteAddresses = range;
            firewallPolicy.Rules.Add(blockout);

So I saw someone do this for python:

os.popen('netsh advfirewall firewall add rule name="'+name+'" dir=in action=block program= "'+ b[i]+'" enable=yes profile=any')

Sure I can do that, I did that before I discovered the API for C# in C# anyways, but it's messy. Is there any package, framework etc for this for Python?

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/953160-python-control-windows-firewall/
Share on other sites

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

×