OpenSecurity/bin/cygwin.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Tue, 13 May 2014 10:24:57 +0200
changeset 145 758031cf192a
parent 143 36948a118f71
child 152 028c3055147f
permissions -rwxr-xr-x
tray icon mailing done
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # cygwin command
     6 # 
     7 # executes a cygwin command inside the opensecurity project
     8 #
     9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
    10 #        Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    11 #
    12 # Copyright (C) 2013 AIT Austrian Institute of Technology
    13 # AIT Austrian Institute of Technology GmbH
    14 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    15 # http://www.ait.ac.at
    16 #
    17 # This program is free software; you can redistribute it and/or
    18 # modify it under the terms of the GNU General Public License
    19 # as published by the Free Software Foundation version 2.
    20 # 
    21 # This program is distributed in the hope that it will be useful,
    22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    24 # GNU General Public License for more details.
    25 # 
    26 # You should have received a copy of the GNU General Public License
    27 # along with this program; if not, write to the Free Software
    28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    29 # Boston, MA  02110-1301, USA.
    30 # ------------------------------------------------------------
    31 
    32 
    33 # ------------------------------------------------------------
    34 # imports
    35 
    36 import os
    37 import subprocess
    38 import sys
    39 import _winreg
    40 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
    41 import threading
    42 
    43 # local
    44 from environment import Environment
    45 from opensecurity_util import logger, setupLogger, OpenSecurityException
    46 import time
    47 
    48 
    49 # ------------------------------------------------------------
    50 # code
    51 
    52 def once(theClass):
    53     """get the path to our local cygwin installment"""
    54     home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
    55     path_hint = [ 
    56         os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin')), 
    57         os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin64')), 
    58         os.path.abspath(os.path.join(home_drive, 'cygwin')),
    59         os.path.abspath(os.path.join(home_drive, 'cygwin64'))
    60     ]
    61     path_valid = [ p for p in path_hint if os.path.exists(p) ]
    62         
    63     theClass.cygwin_root = path_valid[0]
    64     theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
    65     theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
    66     theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
    67     theClass.cygwin_scp = os.path.join(theClass.cygwin_bin, 'scp.exe')
    68     theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
    69     theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe") 
    70     """get the path to the VirtualBox installation on this system"""
    71     theClass.vbox_root = theClass.getRegEntry('SOFTWARE\Oracle\VirtualBox', 'InstallDir')[0]  
    72     theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
    73     #theClass.user_home = os.path.expanduser("~")
    74     theClass.user_home = os.environ['APPDATA']#os.path.expandvars("%APPDATA%")
    75     return theClass
    76 
    77 class XRunner(threading.Thread): 
    78     #running = True
    79     def __init__(self): 
    80         threading.Thread.__init__(self)
    81  
    82     def stop(self):
    83         self.running = False
    84         
    85     def run(self):
    86         #while self.running:
    87         logger.info('X starting')
    88         if not Cygwin.is_X11_running():
    89             #os.system('"'+Cygwin.cygwin_x11+'" :0 -multiwindow -resize')
    90             sts = call('"'+Cygwin.cygwin_x11+'" :0 -multiwindow -resize', shell=True)
    91         else:
    92             logger.info('X already started')
    93                 
    94             
    95             
    96 @once
    97 class Cygwin(object):
    98     cygwin_root = ''
    99     cygwin_bin = ''
   100     cygwin_bash = ''
   101     cygwin_ssh = ''
   102     cygwin_x11 = ''
   103     cygwin_scp = ''
   104     vbox_root = ''
   105     vbox_man = ''
   106     win_cmd = ''
   107     user_home = ''
   108     """Some nifty methods working with Cygwin"""
   109     
   110     def __call__(self, command, arguments, wait_return=True, window = False):
   111         """make an instance of this object act as a function"""
   112         return self.execute(command, arguments, wait_return, window)
   113 
   114     @staticmethod
   115     def getRegEntry(key, value):
   116         try:
   117             k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
   118             value = _winreg.QueryValueEx(k, value)
   119             _winreg.CloseKey(k)
   120             return value
   121         except:
   122             pass
   123     
   124             
   125     @staticmethod
   126     def root():
   127         return Cygwin.cygwin_root
   128 
   129     @staticmethod
   130     def bin():
   131         return Cygwin.cygwin_bin
   132     
   133     @staticmethod
   134     def bash():
   135         return Cygwin.cygwin_bash
   136     
   137     @staticmethod    
   138     def ssh():
   139         return Cygwin.cygwin_ssh
   140     
   141     @staticmethod    
   142     def scp():
   143         return Cygwin.cygwin_scp
   144 
   145     @staticmethod    
   146     def x11():
   147         return Cygwin.cygwin_x11
   148     
   149     @staticmethod
   150     def vboxman():
   151         return Cygwin.vbox_man
   152     
   153     @staticmethod
   154     def cmd():
   155         return Cygwin.win_cmd
   156     
   157     @staticmethod
   158     def home():
   159         return Cygwin.user_home
   160     
   161     executeLock = threading.Lock()
   162     #executes command on host system
   163     @staticmethod
   164     def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE):
   165         _startupinfo = STARTUPINFO()
   166         if not window:
   167             _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
   168             _startupinfo.wShowWindow = _subprocess.SW_HIDE
   169 
   170             #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
   171         res_stderr = None
   172         try:
   173             # quote the executable otherwise we run into troubles
   174             # when the path contains spaces and additonal arguments
   175             # are presented as well.
   176             # special: invoking bash as login shell here with
   177             # an unquoted command does not execute /etc/profile
   178             args = '"' + program + '" ' + arguments
   179             process = Popen(args, startupinfo = _startupinfo, stdin = stdin, stdout = stdout, stderr = stderr, shell = False)
   180             logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
   181             if not wait_return:
   182                 return [0, 'working in background', '']
   183             result = process.wait()
   184             res_stdout = process.stdout.read();
   185             res_stderr = process.stderr.read();
   186 
   187         except Exception as ex:
   188             res_stderr = ''.join(str(ex.args))
   189             result = 1 
   190             
   191         return result, res_stdout, res_stderr
   192     
   193     @staticmethod
   194     def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
   195         retry = 0
   196         result = None
   197         while retry < 3:
   198             if Cygwin.executeLock.acquire(True):
   199                 result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
   200                 Cygwin.executeLock.release()
   201                 if result[0] == 0:
   202                     return result
   203                 retry+=1
   204         return result
   205 
   206 
   207     @staticmethod
   208     def bashExecute(command, wait_return=True, window = False, bash_opts='', stdin = PIPE, stdout = PIPE, stderr = PIPE):
   209         # for some reason, the '-l' is ignored when started via python
   210         # so the same behavior is triggered by calling /etc/profile 
   211         # directly
   212         command = bash_opts + ' -l -c "'  + command + '"'
   213         return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window, stdin = stdin, stdout = stdout, stderr = stderr)
   214     
   215     @staticmethod
   216     def cmdExecute(command, wait_return=True, window = False):
   217         command = ' /c ' + command 
   218         return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
   219 
   220     # executes command over ssh on guest vm
   221     @staticmethod
   222     def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
   223         command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
   224         return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
   225     
   226     #machineFolder + '/' + vm_name + '/dvm_key
   227     #address = self.getHostOnlyIP(vm_name)
   228     #machineFolder = self.getDefaultMachineFolder()
   229     #machineFolder = Cygwin.cygwinPath(machineFolder)
   230     
   231     # executes command over ssh on guest vm with X forwarding
   232     @staticmethod
   233     def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
   234         #return call('"'+ Cygwin.cygwin_bash +'" -l -c "' + 'DISPLAY=:0.0 ssh -Y -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '"', shell=True)
   235         return Cygwin.bashExecute('DISPLAY=:0.0 ssh -Y -o StrictHostKeyChecking=no -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '')
   236 
   237     @staticmethod
   238     def is_X11_running():
   239         """check if we can connect to a X11 running instance"""
   240         p = Cygwin.bashExecute('xset -display :0.0 q', wait_return = True, window = False) 
   241         return p[0] == 0
   242         
   243     @staticmethod
   244     def start_X11():
   245         """start X11 in the background (if not already running) on DISPLAY=:0"""
   246         runner = XRunner()
   247         runner.start()
   248         return (0, None, None)
   249 
   250         # launch X11
   251         #return Cygwin.execute(Cygwin.cygwin_x11, ':0 -multiwindow', wait_return = True, window = False)
   252         #return Cygwin.bashExecute('XWin :0 -multiwindow', wait_return = True, window = False)
   253         #return Cygwin.bashExecute('DISPLAY=:0.0 xhost +', wait_return = True, window = False)
   254         #return os.system('"'+Cygwin.cygwin_x11+'" :0 -multiwindow -resize')
   255     
   256     @staticmethod    
   257     def cygPath(path):
   258         cmd = 'cygpath -u \'' + path + '\''
   259         return Cygwin.bashExecute(cmd)[1].rstrip('\n')
   260                 
   261 # start
   262 import os
   263 import win32api
   264 import win32con
   265 import win32security
   266 
   267 if __name__ == "__main__":
   268     logger = setupLogger('Cygwin')
   269     c = Cygwin()
   270     #logger.info(c.root())
   271     #logger.info(c.bin())
   272     #logger.info(c.bash())
   273     #logger.info(c.ssh())
   274     #logger.info(c.x11())
   275     #logger.info(c.home())   
   276     
   277     #PSEXEC -i -s -d CMD
   278     #tasklist /v /fo list /fi "IMAGENAME eq explorer.exe"
   279     
   280     #runner = XRunner()
   281     #runner.start()
   282     
   283     #Cygwin.start_X11()
   284     
   285     
   286             
   287     #time.sleep(500)
   288     
   289     #Cygwin.start_X11()
   290     #print (Cygwin.is_X11_running())
   291     #print (Cygwin.is_X11_running())
   292     #new_sdvm = 'SecurityDVM0'
   293     #new_ip = Cygwin.vboxExecute('guestproperty get ' + new_sdvm + ' /VirtualBox/GuestInfo/Net/0/V4/IP')[1]
   294     #new_ip = new_ip[new_ip.index(':')+1:].strip()
   295     #new_ip = '+'
   296     #result = Cygwin.bashExecute('DISPLAY=:0.0 xhost '+new_ip)
   297     #browser = '/usr/bin/midori '
   298     #print(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', '/cygdrive/c/Users/BarthaM/VirtualBox VMs' + '/' + new_sdvm + '/dvm_key'))
   299             
   300     #print(Cygwin.bashExecute('echo $PATH')[1])
   301     #print(Cygwin.cygPath('C:'))
   302     #print('C:\\Program Files\\OpenSecurity: ' + c.cygPath('C:\\Program Files\\OpenSecurity'))
   303     
   304     sys.exit(0)
   305