ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/Inpersonator.py
Revision: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 1337 byte(s)
Log Message:
Initial Import

File Contents

# User Rev Content
1 ninoborges 8 ## 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_type, sys.exc_value
36     print "I'm now exiting as %s" % win32api.GetUserName()