ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/Inpersonator.py
Revision: 744
Committed: Tue Apr 13 21:40:41 2021 UTC (4 years, 11 months ago) by nino.borges
Content type: text/x-python
File size: 1350 byte(s)
Log Message:
Updated all tools libs to be python 3 compatible. 

File Contents

# Content
1 ## Impersonate.py
2 ## A program that will allow you to impersonate another user by attaching their security token to the
3 ## calling process.
4 ## EBorges
5 ## 04.08.03
6
7 import win32security, win32con, sys, win32api
8
9 class Impersonate:
10 def __init__(self, login, password):
11 self.domain = 'MWBB.DOM'
12 self.login = login
13 self.password = password
14
15 def logon(self):
16 self.handle = win32security.LogonUser(self.login, self.domain, self.password, win32con.LOGON32_LOGON_INTERACTIVE,
17 win32con.LOGON32_PROVIDER_DEFAULT)
18 win32security.ImpersonateLoggedOnUser(self.handle)
19
20 def logoff(self):
21 win32security.RevertToSelf() #Terminates impersonation
22 self.handle.Close() # Guarantees cleanup
23
24 if __name__=='__main__':
25 print("Right now I'm %s" % win32api.GetUserName())
26 a = Impersonate("wsburrou", "/.,zxc';lasdkf")
27 try:
28 a.logon() #become the user
29 try:
30 # Do whatever you need to do
31 print("Now I'm %s"% win32api.GetUserName()) # show that I'm someone else
32 finally:
33 a.logoff() # Ensure return-to-normal no matter what
34 except:
35 print('Exception:', sys.exc_info()[0], sys.exc_info()[1])
36 print("I'm now exiting as %s" % win32api.GetUserName())