I need to create process as user. However during creating the process some error appears. The error is: 1314 CreateProcessAsUser A required privilege is not held by the client.
import win32api
import win32security
import win32process
import pywintypes
def attempt_to_logon():
username = "abcdef"
password = "123456"
try:
hUser = win32security.LogonUser(username, None,
password, win32security.LOGON32_LOGON_INTERACTIVE,
win32security.LOGON32_PROVIDER_DEFAULT)
except win32security.error:
print "unable to logon"
return None
return hUser
def run_as_user(hUser):
startup = win32process.STARTUPINFO()
startup.dwFlags = win32process.STARTF_USESHOWWINDOW
startup.wShowWindow = win32con.SW_SHOW
startup.lpDesktop = 'winsta0\default'
try:
result = win32process.CreateProcessAsUser(hUser,
None, # appName
"c:\\windows\\notepad.exe", # commandLine
None, # process attrs
None, # thread attrs
0, # inherit handles
0, # create flags
None, # new environment dict
None, # current directory
startup) # startup info
except pywintypes.error, (errcode, method, msg):
print errcode, method, msg
def AdjustPriv(priv, enable=1):
flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
htoken = win32security.OpenProcessToken(
win32api.GetCurrentProcess(), flags)
id = win32security.LookupPrivilegeValue(None, priv)
if enable:
newPriv = [(id, win32security.SE_PRIVILEGE_ENABLED)]
else:
newPriv = [(id, 0)]
win32security.AdjustTokenPrivileges(htoken, 0, newPriv)
if __name__ == "__main__":
AdjustPriv(win32security.SE_TCB_NAME)
AdjustPriv(win32security.SE_ASSIGNPRIMARYTOKEN_NAME)
AdjustPriv(win32security.SE_INCREASE_QUOTA_NAME)
hUser = attempt_to_logon()
run_as_user(hUser)
The error is in function run_as_user
, when I try to create the process. I was able to read, that I have to set SE_TCB_NAME
, SE_ASSIGNPRIMARYTOKEN_NAME
and E_INCREASE_QUOTA_NAME
for using CreateProcessAsUser. So I have no idea where the problem can be.