Home > Article > Backend Development > How to use Python to add or remove permissions on Windows
The following is an article about how to use Python to add or delete permissions on Windows. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
When using Python to develop on the Windows platform, sometimes we need to dynamically add or delete certain permissions of the user. At this time, we can achieve this through the AdjustTokenPrivileges API.
For example, we want to assign the SE_TCB_NAME permission to the user
flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY token = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags) id = win32security.LookupPrivilegeValue(None, win32security.SE_TCB_NAME) privilege = [(id, win32security.SE_PRIVILEGE_ENABLED)] print win32security.AdjustTokenPrivileges(token, False, privilege)
For example, we want to remove SE_TCB_NAME permissions from users
flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY token = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags) id = win32security.LookupPrivilegeValue(None, win32security.SE_TCB_NAME) privilege = [(id, 0)] print win32security.AdjustTokenPrivileges(token, False, privilege)
Related recommendations:
Introduction to how to add screen clearing function in Python
The above is the detailed content of How to use Python to add or remove permissions on Windows. For more information, please follow other related articles on the PHP Chinese website!