Python code to read and print the folder permissions on regkeys

Hi,

Can someone please help to write python code that fetches values from registry keys and also prints what permissions different users of system have on those keys?

Cheers,

Dev
searchsanjaysharmaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
Accidentally posted in your perl question on the same topic (http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_28508128.html).  I follow both categories and wasn't paying a whole lot of attention.  Here's a python example.
import win32api
import win32security
import _winreg

class Ace(object):
    ace_flags = {win32security.CONTAINER_INHERIT_ACE: 'Container Inherit',
                 win32security.FAILED_ACCESS_ACE_FLAG: 'Failed Access',
                 win32security.INHERIT_ONLY_ACE: 'Inherit only',
                 win32security.INHERITED_ACE: 'Inherited ACE',
                 win32security.NO_PROPAGATE_INHERIT_ACE: 'No propagate',
                 win32security.OBJECT_INHERIT_ACE: 'Object inherit',
                 win32security.SUCCESSFUL_ACCESS_ACE_FLAG: 'Successful access'}
                     
    def __init__(self, ace):
        self.ace = ace
        ( (self.access_type, self.flags), self.mask, self.pysid) = ace

    def getType(self):
        if self.access_type == win32security.ACCESS_ALLOWED_ACE_TYPE:
            return "Allow"
        if self.access_type == win32security.ACCESS_DENIED_ACE_TYPE:
            return "Deny"

    def getFlags(self):
        readable_flags = list()
        for (f, v) in self.ace_flags.items():
            if f & self.flags == f:
                readable_flags.append(v)
        return readable_flags
    
class RegKey(object):

    registry_rights = {_winreg.KEY_ALL_ACCESS: 'All Access',
                      _winreg.KEY_WRITE: 'Write',
                      _winreg.KEY_READ: 'Read',
                      _winreg.KEY_EXECUTE: 'Read',
                      _winreg.KEY_QUERY_VALUE: 'Query value',
                      _winreg.KEY_SET_VALUE: 'Set value',
                      _winreg.KEY_CREATE_SUB_KEY: 'Create subkey',
                      _winreg.KEY_ENUMERATE_SUB_KEYS: 'Enum subkeys',
                      _winreg.KEY_NOTIFY: 'Request notification',
                      _winreg.KEY_CREATE_LINK: 'Link (reserved)' }
    
    def __init__(self, key_path, machine=None, root=_winreg.HKEY_LOCAL_MACHINE):
        self.registry = _winreg.ConnectRegistry(None, root)
        self.key_path = key_path
        self.key = _winreg.OpenKey(self.registry, key_path)
        self.security = win32api.RegGetKeySecurity(self.key.handle, win32security.DACL_SECURITY_INFORMATION)
        self.dacl = self.security.GetSecurityDescriptorDacl()
        self.aces = list()
        for i in range(self.dacl.GetAceCount()):
            self.aces.append(Ace(self.dacl.GetAce(i)))

    def dump_values(self):
        (subkey_count, value_count, last_modified) = _winreg.QueryInfoKey(self.key)
        print "Values:"
        for i in range(value_count):
            (name, data, value_type) = _winreg.EnumValue(self.key, i)
            print "\t{0}: {1}".format(name, data)

    def dump_subkeys(self):
        (subkey_count, value_count, last_modified) = _winreg.QueryInfoKey(self.key)
        print "Subkeys:"
        for i in range(subkey_count):
           print "\t{0}".format(_winreg.EnumKey(self.key, i))

    def dump_ace(self, ace):
        print "Ace: {0}".format(ace.ace)
        print " Type: {0}".format(ace.getType())
        print " Flags: {0}".format(", ".join(ace.getFlags()))  
        account = win32security.LookupAccountSid(None, ace.pysid);
        print " Account: {0}\{1}".format(account[1], account[0])
        print " Access:"
        if ace.mask & _winreg.KEY_ALL_ACCESS == _winreg.KEY_ALL_ACCESS:
            print "\t{0}".format(self.registry_rights[_winreg.KEY_ALL_ACCESS])
        else:
            for (p, v) in self.registry_rights.items():
                if p & ace.mask == p:
                   print "\t{0}".format(v)
        
    def dump_dacl(self):
        for ace in self.aces:
            self.dump_ace(ace)
            print
            
    
if __name__ == '__main__':
    r = RegKey(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
               root=_winreg.HKEY_CURRENT_USER)
    r.dump_subkeys()
    r.dump_values()
    r.dump_dacl()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.