Advertisement
Guest User

Win. service

a guest
Nov 19th, 2015
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. __author__ = 'Frenky'
  2.  
  3. import win32service
  4. import win32serviceutil
  5. import win32event
  6. import win32con
  7. import win32api
  8. import win32security
  9. import win32process
  10. import pywintypes
  11.  
  12.  
  13.  
  14. #############################################
  15. ############ CREATE PROCESS #################
  16. #############################################
  17.  
  18. def attempt_to_logon():
  19.     username = "abcdef"
  20.     password = "123456"
  21.     try:
  22.         hUser = win32security.LogonUser(username, None,
  23.                                         password, win32security.LOGON32_LOGON_INTERACTIVE,
  24.                                         win32security.LOGON32_PROVIDER_DEFAULT)
  25.     except win32security.error:
  26.         print "unable to logon"
  27.         return None
  28.     return hUser
  29.  
  30. def run_as_user(hUser):
  31.     startup = win32process.STARTUPINFO()
  32.     startup.dwFlags = win32process.STARTF_USESHOWWINDOW
  33.     startup.wShowWindow = win32con.SW_SHOW
  34.     startup.lpDesktop = 'winsta0\default'
  35.  
  36.  
  37.     try:
  38.         result = win32process.CreateProcessAsUser(hUser,
  39.                                                   None,  # appName
  40.                                                   "c:\\windows\\notepad.exe",  # notepad.exe
  41.                                                   None,  # process attrs
  42.                                                   None,  # thread attrs
  43.                                                   0,  # inherit handles
  44.                                                   0,  # create flags
  45.                                                   None,  # new environment dict
  46.                                                   None,  # current directory
  47.                                                   startup)  # startup info
  48.         print result
  49.     except pywintypes.error, (errcode, method, msg):
  50.         print errcode, method, msg
  51.  
  52.  
  53.  
  54. def AdjustPriv(priv, enable=1):
  55.     flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
  56.     htoken = win32security.OpenProcessToken(
  57.         win32api.GetCurrentProcess(), flags)
  58.     id = win32security.LookupPrivilegeValue(None, priv)
  59.     if enable:
  60.         newPriv = [(id, win32security.SE_PRIVILEGE_ENABLED)]
  61.     else:
  62.         newPriv = [(id, 0)]
  63.     win32security.AdjustTokenPrivileges(htoken, 0, newPriv)
  64.  
  65.  
  66.  
  67.  
  68.  
  69. #################################################################
  70. ###############  WINDOWS SERVICE  ###############################
  71. #################################################################
  72.  
  73. class aservice(win32serviceutil.ServiceFramework):
  74.     _svc_name_ = "Stratosphere_service"
  75.     _svc_display_name_ = "Stratosphere"
  76.     _svc_description_ = "Stratosphere is your shield!"
  77.  
  78.     def __init__(self, args):
  79.         win32serviceutil.ServiceFramework.__init__(self, args)
  80.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  81.  
  82.     def SvcStop(self):
  83.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  84.         win32event.SetEvent(self.hWaitStop)
  85.  
  86.     def SvcDoRun(self):
  87.         import servicemanager
  88.         servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED,
  89.                               (self._svc_name_, ''))
  90.  
  91.  
  92.         self.timeout = 1000  # 10 seconds
  93.         while 1:
  94.             # Wait for service stop signal, if I timeout, loop again
  95.             rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
  96.             # Check to see if self.hWaitStop happened
  97.             if rc == win32event.WAIT_OBJECT_0:
  98.                 break
  99.             else:
  100.                 try:
  101.                     AdjustPriv(win32security.SE_TCB_NAME)
  102.                     AdjustPriv(win32security.SE_ASSIGNPRIMARYTOKEN_NAME)
  103.                     AdjustPriv(win32security.SE_INCREASE_QUOTA_NAME)
  104.                     hUser = attempt_to_logon()
  105.                     run_as_user(hUser)
  106.                 except:
  107.                     pass
  108.  
  109.  
  110.  
  111. def ctrlHandler(ctrlType):
  112.     return True
  113.  
  114.  
  115. if __name__ == '__main__':
  116.     win32api.SetConsoleCtrlHandler(ctrlHandler, True)
  117.     win32serviceutil.HandleCommandLine(aservice)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement