| 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 |
nino.borges |
744 |
print("Right now I'm %s" % win32api.GetUserName())
|
| 26 |
ninoborges |
8 |
a = Impersonate("wsburrou", "/.,zxc';lasdkf")
|
| 27 |
|
|
try:
|
| 28 |
|
|
a.logon() #become the user
|
| 29 |
|
|
try:
|
| 30 |
|
|
# Do whatever you need to do
|
| 31 |
nino.borges |
744 |
print("Now I'm %s"% win32api.GetUserName()) # show that I'm someone else
|
| 32 |
ninoborges |
8 |
finally:
|
| 33 |
|
|
a.logoff() # Ensure return-to-normal no matter what
|
| 34 |
|
|
except:
|
| 35 |
nino.borges |
744 |
print('Exception:', sys.exc_info()[0], sys.exc_info()[1])
|
| 36 |
|
|
print("I'm now exiting as %s" % win32api.GetUserName()) |