[python-win32] PythonCOM & Windows Firewall

Metz, Bobby W, WWCS bwmetz at att.com
Mon May 22 18:42:20 CEST 2006


	Again, thanks to Roger to the perfect tip.  As I feared,
non-admins don't get prompted to "allow" an app that's requesting access
or I'm doing something wrong.  But, this did lead me to write a couple
of quick routines for detecting the FW condition for the users and
providing a nice little notification so they know to contact the Help
Desk for assistance.  
	Also thought I'd share these to save someone else the time down
the road.  Note, the first function should theoretically detect Windows
XP of any flavor running SP2 or later.  Only time will tell...it's based
on VB code to detect XP Prof with SP2.  Modify as needed.  I use them in
tandem since SP2 is the only XP with FW enabled by default.  I won't
vouch for the FW detection in pre-SP2 or any other version using the ICF
as I don't have a test machine at the moment to try it.

import win32com.client
import pythoncom

# Using Python to detect if XP Firewall is enabled.
def is_WinXP_FW_Enabled():
    try:
	  XPFW =
win32com.client.gencache.EnsureDispatch('HNetCfg.FwMgr',0)
	  XPFW_Policy = XPFW.LocalPolicy.CurrentProfile
    except pythoncom.com_error:
        # Can't dispatch or access the FW COM instance.
        return False
    else:
	  return XPFW_policy.FirewallEnabled

# Using Python to detect if XP SP 2 is installed.
def is_WinXP_SP2():
    try:
	  objWMIService = win32com.client.GetObject("winmgmts:")
	  objOS = objWMIService.ExecQuery ("Select * from
Win32_OperatingSystem WHERE Caption LIKE 'Microsoft Windows XP%' AND
ServicePackMajorVersion >= 2")
	  if objOS.Count != 0:                            # objOS.Count
on Win2000 is invalid (raises com_error)
            return True
    except pythoncom.com_error:
        # Can't access WMI so default to False.
        # Maybe add some fancier error handling later.
        return False
    else:
        # No COM errors but not Win XP
        return False

Bobby

-----Original Message-----
From: Metz, Bobby W, WWCS 
Sent: Thursday, May 18, 2006 12:46 AM
To: 'Roger Upole'; python-win32 at python.org
Subject: RE: [python-win32] Re: PythonCOM & Windows Firewall


Roger, thanks for the code.  I'll give it a try.  Having read a bunch of
the documentation though, I'm concerned it may not work as some of our
users won't have admin rights.  I was hoping this would provide them the
Zone Alarm type prompt so that non-admin could approve the listening app
upon execution.  If all else fails it gives me a starter for checking
for existing exceptions.

Here's to hoping!!!

Bobby

-----Original Message-----
From: python-win32-bounces at python.org
[mailto:python-win32-bounces at python.org]On Behalf Of Roger Upole
Sent: Wednesday, May 17, 2006 7:07 PM
To: python-win32 at python.org
Subject: [python-win32] Re: PythonCOM & Windows Firewall


I've used it to list firewall exceptions, and it wasn't too much
more work to add an application: 

import win32com.client
fw=win32com.client.gencache.EnsureDispatch('HNetCfg.FwMgr',0)
apps=fw.LocalPolicy.CurrentProfile.AuthorizedApplications
for app in apps:
    print app.Name, app.ProcessImageFileName
    
newapp=win32com.client.Dispatch('HNetCfg.FwAuthorizedApplication')
newapp.Name='python_d.exe'
newapp.ProcessImageFileName='j:\\python24\\python_d.exe'
newapp.Enabled=True
apps.Add(newapp)


    hth
         Roger

_______________________________________________
Python-win32 mailing list
Python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32


More information about the Python-win32 mailing list