def adjust_privilege(self, priv): try: flags = win32.TOKEN_ADJUST_PRIVILEGES | win32.TOKEN_QUERY htoken = win32.OpenProcessToken(win32.GetCurrentProcess(), flags) priv_value = win32.LookupPrivilegeValue(None, priv) new_privs = [(priv_value, win32.SE_PRIVILEGE_ENABLED)] win32.AdjustTokenPrivileges(htoken, new_privs) self.logger.debug( 'Success - AdjustTokenPrivileges(%s)'.format(priv)) except win32.WindowsError as err: self.logger.warning('Exception - AdjustTokenPrivileges') self.logger.warning(err) except Exception as err: self.logger.critical('Exception - general error') self.logger.critical(err)
def adjust_privileges(state, privileges): """ Requests or drops privileges. @type state: bool @param state: C{True} to request, C{False} to drop. @type privileges: list(int) @param privileges: Privileges to request or drop. @raise WindowsError: Raises an exception on error. """ with win32.OpenProcessToken(win32.GetCurrentProcess(), win32.TOKEN_ADJUST_PRIVILEGES) as hToken: NewState = ( (priv, state) for priv in privileges ) win32.AdjustTokenPrivileges(hToken, NewState)
def request_privileges(self, privileges, state=True): with win32.OpenProcessToken(win32.GetCurrentProcess(), win32.TOKEN_ADJUST_PRIVILEGES) as hToken: NewState = ((priv, state) for priv in privileges) win32.AdjustTokenPrivileges(hToken, NewState)