1.1 --- a/OpenSecurity/bin/cygwin.py Fri Mar 07 14:32:12 2014 +0100
1.2 +++ b/OpenSecurity/bin/cygwin.py Mon Mar 10 13:01:08 2014 +0100
1.3 @@ -1,222 +1,236 @@
1.4 -#!/bin/env python
1.5 -# -*- coding: utf-8 -*-
1.6 -
1.7 -# ------------------------------------------------------------
1.8 -# cygwin command
1.9 -#
1.10 -# executes a cygwin command inside the opensecurity project
1.11 -#
1.12 -# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
1.13 -# Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.14 -#
1.15 -# Copyright (C) 2013 AIT Austrian Institute of Technology
1.16 -# AIT Austrian Institute of Technology GmbH
1.17 -# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.18 -# http://www.ait.ac.at
1.19 -#
1.20 -# This program is free software; you can redistribute it and/or
1.21 -# modify it under the terms of the GNU General Public License
1.22 -# as published by the Free Software Foundation version 2.
1.23 -#
1.24 -# This program is distributed in the hope that it will be useful,
1.25 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.26 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.27 -# GNU General Public License for more details.
1.28 -#
1.29 -# You should have received a copy of the GNU General Public License
1.30 -# along with this program; if not, write to the Free Software
1.31 -# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.32 -# Boston, MA 02110-1301, USA.
1.33 -# ------------------------------------------------------------
1.34 -
1.35 -
1.36 -# ------------------------------------------------------------
1.37 -# imports
1.38 -
1.39 -import os
1.40 -import subprocess
1.41 -import sys
1.42 -import _winreg
1.43 -from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
1.44 -import threading
1.45 -# local
1.46 -from environment import Environment
1.47 -from opensecurity_util import logger, setupLogger, OpenSecurityException
1.48 -
1.49 -# ------------------------------------------------------------
1.50 -# code
1.51 -
1.52 -def once(theClass):
1.53 - """get the path to our local cygwin installment"""
1.54 - home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
1.55 - path_hint = [
1.56 - os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')),
1.57 - os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')),
1.58 - os.path.abspath(os.path.join(home_drive, 'cygwin')),
1.59 - os.path.abspath(os.path.join(home_drive, 'cygwin64'))
1.60 - ]
1.61 - path_valid = [ p for p in path_hint if os.path.exists(p) ]
1.62 -
1.63 - theClass.cygwin_root = path_valid[0]
1.64 - theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
1.65 - theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
1.66 - theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
1.67 - theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
1.68 - theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe")
1.69 - """get the path to the VirtualBox installation on this system"""
1.70 - theClass.vbox_root = theClass.getRegEntry('SOFTWARE\Oracle\VirtualBox', 'InstallDir')[0]
1.71 - theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
1.72 -
1.73 - return theClass
1.74 -
1.75 -@once
1.76 -class Cygwin(object):
1.77 - cygwin_root = ''
1.78 - cygwin_bin = ''
1.79 - cygwin_bash = ''
1.80 - cygwin_ssh = ''
1.81 - cygwin_x11 = ''
1.82 - vbox_root = ''
1.83 - vbox_man = ''
1.84 - win_cmd = ''
1.85 - """Some nifty methods working with Cygwin"""
1.86 -
1.87 - def __call__(self, command, arguments, wait_return=True, window = False):
1.88 - """make an instance of this object act as a function"""
1.89 - return self.execute(command, arguments, wait_return, window)
1.90 -
1.91 - @staticmethod
1.92 - def getRegEntry(key, value):
1.93 - try:
1.94 - k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
1.95 - value = _winreg.QueryValueEx(k, value)
1.96 - _winreg.CloseKey(k)
1.97 - return value
1.98 - except:
1.99 - pass
1.100 -
1.101 - @staticmethod
1.102 - def root():
1.103 - return Cygwin.cygwin_root
1.104 -
1.105 - @staticmethod
1.106 - def bin():
1.107 - return Cygwin.cygwin_bin
1.108 -
1.109 - @staticmethod
1.110 - def bash():
1.111 - return Cygwin.cygwin_bash
1.112 -
1.113 - @staticmethod
1.114 - def ssh():
1.115 - return Cygwin.cygwin_ssh
1.116 -
1.117 - @staticmethod
1.118 - def x11():
1.119 - return Cygwin.cygwin_x11
1.120 -
1.121 - @staticmethod
1.122 - def vboxman():
1.123 - return Cygwin.vbox_man
1.124 -
1.125 - @staticmethod
1.126 - def cmd():
1.127 - return Cygwin.win_cmd
1.128 -
1.129 - executeLock = threading.Lock()
1.130 - #executes command on host system
1.131 - @staticmethod
1.132 - def execute(program, arguments, wait_return=True, window = False):
1.133 - _startupinfo = STARTUPINFO()
1.134 - if not window:
1.135 - _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
1.136 - _startupinfo.wShowWindow = _subprocess.SW_HIDE
1.137 -
1.138 - #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
1.139 - res_stderr = None
1.140 - try:
1.141 - process = Popen(executable=program, args=' ' + arguments, startupinfo = _startupinfo, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = False)
1.142 - logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
1.143 - if not wait_return:
1.144 - return [0, 'working in background', '']
1.145 - result = process.wait()
1.146 - res_stdout = process.stdout.read();
1.147 - res_stderr = process.stderr.read();
1.148 -
1.149 - except Exception as ex:
1.150 - res_stderr.join(ex.args)
1.151 - result = 1
1.152 -
1.153 - return result, res_stdout, res_stderr
1.154 -
1.155 - @staticmethod
1.156 - def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
1.157 - retry = 0
1.158 - result = None
1.159 - while retry < 3:
1.160 - if Cygwin.executeLock.acquire(True):
1.161 - result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
1.162 - Cygwin.executeLock.release()
1.163 - if result[0] == 0:
1.164 - return result
1.165 - retry+=1
1.166 - return result
1.167 -
1.168 -
1.169 - @staticmethod
1.170 - def bashExecute(command, wait_return=True, window = False, bash_opts=''):
1.171 - command = bash_opts + ' -l -c ' + command
1.172 - return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window)
1.173 -
1.174 - @staticmethod
1.175 - def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
1.176 - command = ' /c ' + command
1.177 - return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
1.178 -
1.179 - # executes command over ssh on guest vm
1.180 - @staticmethod
1.181 - def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
1.182 - command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command
1.183 - return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
1.184 -
1.185 - #machineFolder + '/' + vm_name + '/dvm_key
1.186 - #address = self.getHostOnlyIP(vm_name)
1.187 - #machineFolder = self.getDefaultMachineFolder()
1.188 - #machineFolder = Cygwin.cygwinPath(machineFolder)
1.189 -
1.190 - # executes command over ssh on guest vm with X forwarding
1.191 - @staticmethod
1.192 - def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
1.193 - return Cygwin.bashExecute('"DISPLAY=:0.0 /usr/bin/ssh -v -Y -i \\"' + certificate +'\\" ' + user_name + '@' + address + ' ' + command + '\"')
1.194 -
1.195 - @staticmethod
1.196 - def is_X11_running():
1.197 - """check if we can connect to a X11 running instance"""
1.198 - p = Cygwin.bashExecute('"DISPLAY=:0 /usr/bin/xset -q"')
1.199 - return p[0] == 0
1.200 -
1.201 -
1.202 - @staticmethod
1.203 - def start_X11():
1.204 - """start X11 in the background (if not already running) on DISPLAY=:0"""
1.205 - # do not start if already running
1.206 - if Cygwin.is_X11_running():
1.207 - return
1.208 - # launch X11 (forget output and return immediately)
1.209 - return Cygwin.execute(Cygwin.cygwin_x11, ':0 -multiwindow', wait_return = False, window = False)
1.210 -
1.211 - @staticmethod
1.212 - def cygPath(path):
1.213 - return Cygwin.bashExecute('"/usr/bin/cygpath -u \\"' + path + '\\""')[1].rstrip('\n')
1.214 -
1.215 -# start
1.216 -if __name__ == "__main__":
1.217 - logger = setupLogger('Cygwin')
1.218 - c = Cygwin()
1.219 - logger.info(c.root())
1.220 - logger.info(c.bin())
1.221 - logger.info(c.bash())
1.222 - logger.info(c.ssh())
1.223 -
1.224 - c.cygPath('C:')
1.225 - c.start_X11()
1.226 +#!/bin/env python
1.227 +# -*- coding: utf-8 -*-
1.228 +
1.229 +# ------------------------------------------------------------
1.230 +# cygwin command
1.231 +#
1.232 +# executes a cygwin command inside the opensecurity project
1.233 +#
1.234 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
1.235 +# Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.236 +#
1.237 +# Copyright (C) 2013 AIT Austrian Institute of Technology
1.238 +# AIT Austrian Institute of Technology GmbH
1.239 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.240 +# http://www.ait.ac.at
1.241 +#
1.242 +# This program is free software; you can redistribute it and/or
1.243 +# modify it under the terms of the GNU General Public License
1.244 +# as published by the Free Software Foundation version 2.
1.245 +#
1.246 +# This program is distributed in the hope that it will be useful,
1.247 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.248 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.249 +# GNU General Public License for more details.
1.250 +#
1.251 +# You should have received a copy of the GNU General Public License
1.252 +# along with this program; if not, write to the Free Software
1.253 +# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.254 +# Boston, MA 02110-1301, USA.
1.255 +# ------------------------------------------------------------
1.256 +
1.257 +
1.258 +# ------------------------------------------------------------
1.259 +# imports
1.260 +
1.261 +import os
1.262 +import subprocess
1.263 +import sys
1.264 +import _winreg
1.265 +from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
1.266 +import threading
1.267 +# local
1.268 +from environment import Environment
1.269 +from opensecurity_util import logger, setupLogger, OpenSecurityException
1.270 +
1.271 +# ------------------------------------------------------------
1.272 +# code
1.273 +
1.274 +def once(theClass):
1.275 + """get the path to our local cygwin installment"""
1.276 + home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
1.277 + path_hint = [
1.278 + os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin')),
1.279 + os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin64')),
1.280 + os.path.abspath(os.path.join(home_drive, 'cygwin')),
1.281 + os.path.abspath(os.path.join(home_drive, 'cygwin64'))
1.282 + ]
1.283 + path_valid = [ p for p in path_hint if os.path.exists(p) ]
1.284 +
1.285 + theClass.cygwin_root = path_valid[0]
1.286 + theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
1.287 + theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
1.288 + theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
1.289 + theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
1.290 + theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe")
1.291 + """get the path to the VirtualBox installation on this system"""
1.292 + theClass.vbox_root = theClass.getRegEntry('SOFTWARE\Oracle\VirtualBox', 'InstallDir')[0]
1.293 + theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
1.294 +
1.295 + return theClass
1.296 +
1.297 +@once
1.298 +class Cygwin(object):
1.299 + cygwin_root = ''
1.300 + cygwin_bin = ''
1.301 + cygwin_bash = ''
1.302 + cygwin_ssh = ''
1.303 + cygwin_x11 = ''
1.304 + vbox_root = ''
1.305 + vbox_man = ''
1.306 + win_cmd = ''
1.307 + """Some nifty methods working with Cygwin"""
1.308 +
1.309 + def __call__(self, command, arguments, wait_return=True, window = False):
1.310 + """make an instance of this object act as a function"""
1.311 + return self.execute(command, arguments, wait_return, window)
1.312 +
1.313 + @staticmethod
1.314 + def getRegEntry(key, value):
1.315 + try:
1.316 + k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
1.317 + value = _winreg.QueryValueEx(k, value)
1.318 + _winreg.CloseKey(k)
1.319 + return value
1.320 + except:
1.321 + pass
1.322 +
1.323 +
1.324 + @staticmethod
1.325 + def root():
1.326 + return Cygwin.cygwin_root
1.327 +
1.328 + @staticmethod
1.329 + def bin():
1.330 + return Cygwin.cygwin_bin
1.331 +
1.332 + @staticmethod
1.333 + def bash():
1.334 + return Cygwin.cygwin_bash
1.335 +
1.336 + @staticmethod
1.337 + def ssh():
1.338 + return Cygwin.cygwin_ssh
1.339 +
1.340 + @staticmethod
1.341 + def x11():
1.342 + return Cygwin.cygwin_x11
1.343 +
1.344 + @staticmethod
1.345 + def vboxman():
1.346 + return Cygwin.vbox_man
1.347 +
1.348 + @staticmethod
1.349 + def cmd():
1.350 + return Cygwin.win_cmd
1.351 +
1.352 + executeLock = threading.Lock()
1.353 + #executes command on host system
1.354 + @staticmethod
1.355 + def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE):
1.356 + _startupinfo = STARTUPINFO()
1.357 + if not window:
1.358 + _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
1.359 + _startupinfo.wShowWindow = _subprocess.SW_HIDE
1.360 +
1.361 + #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
1.362 + res_stderr = None
1.363 + try:
1.364 + # quote the executable otherwise we run into troubles
1.365 + # when the path contains spaces and additonal arguments
1.366 + # are presented as well.
1.367 + # special: invoking bash as login shell here with
1.368 + # an unquoted command does not execute /etc/profile
1.369 + args = '"' + program + '" ' + arguments
1.370 + process = Popen(args, startupinfo = _startupinfo, stdin = stdin, stdout = stdout, stderr = stderr, shell = False)
1.371 + logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
1.372 + if not wait_return:
1.373 + return [0, 'working in background', '']
1.374 + result = process.wait()
1.375 + res_stdout = process.stdout.read();
1.376 + res_stderr = process.stderr.read();
1.377 +
1.378 + except Exception as ex:
1.379 + res_stderr = ''.join(str(ex.args))
1.380 + result = 1
1.381 +
1.382 + return result, res_stdout, res_stderr
1.383 +
1.384 + @staticmethod
1.385 + def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
1.386 + retry = 0
1.387 + result = None
1.388 + while retry < 3:
1.389 + if Cygwin.executeLock.acquire(True):
1.390 + result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
1.391 + Cygwin.executeLock.release()
1.392 + if result[0] == 0:
1.393 + return result
1.394 + retry+=1
1.395 + return result
1.396 +
1.397 +
1.398 + @staticmethod
1.399 + def bashExecute(command, wait_return=True, window = False, bash_opts='', stdin = PIPE, stdout = PIPE, stderr = PIPE):
1.400 + # for some reason, the '-l' is ignored when started via python
1.401 + # so the same behavior is triggered by calling /etc/profile
1.402 + # directly
1.403 + command = bash_opts + ' -l -c "' + command + '"'
1.404 + return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window, stdin = stdin, stdout = stdout, stderr = stderr)
1.405 +
1.406 + @staticmethod
1.407 + def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
1.408 + command = ' /c ' + command
1.409 + return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
1.410 +
1.411 + # executes command over ssh on guest vm
1.412 + @staticmethod
1.413 + def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
1.414 + command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command
1.415 + return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
1.416 +
1.417 + #machineFolder + '/' + vm_name + '/dvm_key
1.418 + #address = self.getHostOnlyIP(vm_name)
1.419 + #machineFolder = self.getDefaultMachineFolder()
1.420 + #machineFolder = Cygwin.cygwinPath(machineFolder)
1.421 +
1.422 + # executes command over ssh on guest vm with X forwarding
1.423 + @staticmethod
1.424 + def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
1.425 + return Cygwin.bashExecute('"DISPLAY=:0.0 /usr/bin/ssh -v -Y -i \\"' + certificate +'\\" ' + user_name + '@' + address + ' ' + command + '\"')
1.426 +
1.427 + @staticmethod
1.428 + def is_X11_running():
1.429 + """check if we can connect to a X11 running instance"""
1.430 + p = Cygwin.bashExecute('DISPLAY=:0 /usr/bin/xset -q')
1.431 + return p[0] == 0
1.432 +
1.433 +
1.434 + @staticmethod
1.435 + def start_X11():
1.436 + """start X11 in the background (if not already running) on DISPLAY=:0"""
1.437 + # do not start if already running
1.438 + if Cygwin.is_X11_running():
1.439 + return
1.440 + # launch X11 (forget output and return immediately)
1.441 + return Cygwin.execute(Cygwin.cygwin_x11, ':0 -multiwindow', wait_return = False, window = False)
1.442 +
1.443 + @staticmethod
1.444 + def cygPath(path):
1.445 + cmd = 'cygpath -u \'' + path + '\''
1.446 + return Cygwin.bashExecute(cmd)[1].rstrip('\n')
1.447 +
1.448 +# start
1.449 +if __name__ == "__main__":
1.450 + logger = setupLogger('Cygwin')
1.451 + c = Cygwin()
1.452 + logger.info(c.root())
1.453 + logger.info(c.bin())
1.454 + logger.info(c.bash())
1.455 + logger.info(c.ssh())
1.456 +
1.457 + print(c.bashExecute('echo $PATH')[1])
1.458 + print(c.cygPath('C:'))
1.459 + print('C:\\Program Files\\OpenSecurity: ' + c.cygPath('C:\\Program Files\\OpenSecurity'))
1.460 + c.start_X11()
1.461 +