1.1 --- a/OpenSecurity/bin/cygwin.py Wed Jan 29 15:38:12 2014 +0100
1.2 +++ b/OpenSecurity/bin/cygwin.py Fri Feb 14 14:08:24 2014 +0100
1.3 @@ -6,7 +6,8 @@
1.4 #
1.5 # executes a cygwin command inside the opensecurity project
1.6 #
1.7 -# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.8 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
1.9 +# Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.10 #
1.11 # Copyright (C) 2013 AIT Austrian Institute of Technology
1.12 # AIT Austrian Institute of Technology GmbH
1.13 @@ -35,94 +36,170 @@
1.14 import os
1.15 import subprocess
1.16 import sys
1.17 -
1.18 +import _winreg
1.19 +from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
1.20 # local
1.21 from environment import Environment
1.22 -
1.23 -
1.24 +from opensecurity_util import logger, setupLogger, OpenSecurityException
1.25 # ------------------------------------------------------------
1.26 # code
1.27
1.28 +def once(theClass):
1.29 + """get the path to our local cygwin installment"""
1.30 + home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
1.31 + path_hint = [
1.32 + os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')),
1.33 + os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')),
1.34 + os.path.abspath(os.path.join(home_drive, 'cygwin')),
1.35 + os.path.abspath(os.path.join(home_drive, 'cygwin64'))
1.36 + ]
1.37 + path_valid = [ p for p in path_hint if os.path.exists(p) ]
1.38 +
1.39 + theClass.cygwin_root = path_valid[0]
1.40 + theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
1.41 + theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
1.42 + theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
1.43 + theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe")
1.44 +
1.45 + """get the path to the VirtualBox installation on this system"""
1.46 + try:
1.47 + k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Oracle\VirtualBox')
1.48 + theClass.vbox_root = _winreg.QueryValueEx(k, 'InstallDir')[0]
1.49 + theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
1.50 + _winreg.CloseKey(k)
1.51 + except:
1.52 + pass
1.53 + return theClass
1.54
1.55 +@once
1.56 class Cygwin(object):
1.57 -
1.58 + cygwin_root = ''
1.59 + cygwin_bin = ''
1.60 + cygwin_bash = ''
1.61 + cygwin_ssh = ''
1.62 + vbox_root = ''
1.63 + vbox_man = ''
1.64 + win_cmd = ''
1.65 """Some nifty methods working with Cygwin"""
1.66
1.67 - def __call__(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
1.68 + def __call__(self, command, arguments, wait_return=True, window = False):
1.69 """make an instance of this object act as a function"""
1.70 - return self.execute(command, stdin, stdout, stderr)
1.71 + return self.execute(command, arguments, wait_return, window)
1.72
1.73
1.74 @staticmethod
1.75 def root():
1.76 - """get the path to our local cygwin installment"""
1.77 - home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
1.78 - path_hint = [
1.79 - os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')),
1.80 - os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')),
1.81 - os.path.abspath(os.path.join(home_drive, 'cygwin')),
1.82 - os.path.abspath(os.path.join(home_drive, 'cygwin64'))
1.83 - ]
1.84 - path_valid = [ p for p in path_hint if os.path.exists(p) ]
1.85 - return path_valid[0]
1.86 + return Cygwin.cygwin_root
1.87
1.88 + @staticmethod
1.89 + def bin():
1.90 + return Cygwin.cygwin_bin
1.91 +
1.92 + @staticmethod
1.93 + def bash():
1.94 + return Cygwin.cygwin_bash
1.95 +
1.96 + @staticmethod
1.97 + def ssh():
1.98 + return Cygwin.cygwin_ssh
1.99 +
1.100 + @staticmethod
1.101 + def vboxman():
1.102 + return Cygwin.vbox_man
1.103 +
1.104 + @staticmethod
1.105 + def cmd():
1.106 + return Cygwin.win_cmd
1.107 +
1.108 + #executes command on host system
1.109 + @staticmethod
1.110 + def execute(program, arguments, wait_return=True, window = False):
1.111 + _startupinfo = STARTUPINFO()
1.112 + if not window:
1.113 + _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
1.114 + _startupinfo.wShowWindow = _subprocess.SW_HIDE
1.115 +
1.116 + logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
1.117 + try:
1.118 + process = Popen(executable=program, args=' ' + arguments, startupinfo = _startupinfo, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = False)
1.119 + logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
1.120 + if not wait_return:
1.121 + return [0, 'working in background', '']
1.122 + result = process.wait()
1.123 + res_stdout = process.stdout.read();
1.124 + res_stderr = process.stderr.read();
1.125 + if res_stdout != "":
1.126 + logger.debug(res_stdout)
1.127 + if res_stderr != "":
1.128 + logger.debug(res_stderr)
1.129 + except:
1.130 + logger.error('Failed to execute cygwin command.\n\tcommand=' + program + ' ' + ''.join(arguments) + '\n')
1.131 + #TODO: throw exception
1.132 + return result, res_stdout, res_stderr
1.133 +
1.134 + @staticmethod
1.135 + def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
1.136 + return Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
1.137 +
1.138 + @staticmethod
1.139 + def bashExecute(command, wait_return=True, window = False, bash_opts=''):
1.140 + command = bash_opts + ' --login -c ' + command
1.141 + return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window)
1.142 +
1.143 + @staticmethod
1.144 + def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
1.145 + command = ' /c ' + command
1.146 + return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
1.147
1.148 - def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, window = False):
1.149 - """execute a cygwin shell command
1.150 -
1.151 - command is list of arguments like ['/bin/ls', '-al', '-h']
1.152 -
1.153 - a Popen object is returned"""
1.154 - command_path = os.sep.join([Cygwin.root()] + command[0].split('/')[1:])
1.155 - command = [command_path] + command[1:]
1.156 + # executes command over ssh on guest vm
1.157 + @staticmethod
1.158 + def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
1.159 + command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command
1.160 + return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
1.161 +
1.162 + #machineFolder + '/' + vm_name + '/dvm_key
1.163 + #address = self.getHostOnlyIP(vm_name)
1.164 + #machineFolder = self.getDefaultMachineFolder()
1.165 + #machineFolder = Cygwin.cygwinPath(machineFolder)
1.166 +
1.167 + # executes command over ssh on guest vm with X forwarding
1.168 + @staticmethod
1.169 + def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
1.170 + return Cygwin.bashExecute('"DISPLAY=:0.0 /usr/bin/ssh -v -Y -i \\"' + certificate +'\\" ' + user_name + '@' + address + ' ' + command + '\"')
1.171
1.172 - if not window:
1.173 - # hide any window
1.174 - shadowrun_path = os.path.join(Cygwin.root(), 'bin', 'shadowrun.exe')
1.175 - if os.access(shadowrun_path, os.X_OK):
1.176 - command = [shadowrun_path] + command
1.177 - else:
1.178 - print("EPIC FAIL!")
1.179 -
1.180 - try:
1.181 - print('cygwin: ' + ' '.join(command))
1.182 - return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
1.183 - except:
1.184 - sys.stderr.write('Failed to execute cygwin command.\n\tcommand="' + ' '.join(command) + '"\n')
1.185 -
1.186 -
1.187 @staticmethod
1.188 def is_X11_running():
1.189 """check if we can connect to a X11 running instance"""
1.190 - p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
1.191 - stdout, stderr = p.communicate()
1.192 - return p.returncode == 0
1.193 + p = Cygwin.bashExecute('"DISPLAY=:0 /usr/bin/xset -q"')
1.194 + return p[0] == 0
1.195
1.196
1.197 @staticmethod
1.198 def start_X11():
1.199 """start X11 in the background (if not already running) on DISPLAY=:0"""
1.200 -
1.201 # do not start if already running
1.202 if Cygwin.is_X11_running():
1.203 - return
1.204 -
1.205 + return
1.206 # launch X11 (forget output and return immediately)
1.207 - p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
1.208 -
1.209 + return Cygwin.bashExecute('"/usr/bin/X :0 -multiwindow"', bash_opts=' -i ', wait_return = False)
1.210 +
1.211 + @staticmethod
1.212 + def cygPath(path):
1.213 + cmd = '"/usr/bin/cygpath -u \\"' + path + '\\""'
1.214 + return Cygwin.bashExecute(cmd)[1].rstrip('\n')
1.215
1.216 # start
1.217 if __name__ == "__main__":
1.218 + logger = setupLogger('Cygwin')
1.219 + c = Cygwin()
1.220 + logger.info(c.root())
1.221 + logger.info(c.bin())
1.222 + logger.info(c.bash())
1.223 + logger.info(c.ssh())
1.224 +
1.225 + c.cygPath('C:')
1.226 + c.start_X11()
1.227
1.228 - print(Cygwin.root())
1.229 - sys.exit(1)
1.230 -
1.231 - # execute what is given on the command line
1.232 - c = Cygwin()
1.233 - p = c(sys.argv[1:])
1.234 +
1.235
1.236 - # wait until the process finished and grab the output
1.237 - stdout, stderr = p.communicate()
1.238 - print('=== call result on stdout: ===\n' + stdout)
1.239 - print('=== call result on stderr: ===\n' + stderr)
1.240
2.1 --- a/OpenSecurity/bin/launch.pyw Wed Jan 29 15:38:12 2014 +0100
2.2 +++ b/OpenSecurity/bin/launch.pyw Fri Feb 14 14:08:24 2014 +0100
2.3 @@ -238,9 +238,6 @@
2.4
2.5 # ensure we have our X11 running
2.6 Cygwin.start_X11()
2.7 -
2.8 - sys.exit(0)
2.9 -
2.10
2.11 # call the OpenSecurity Admin to launch our progie =)
2.12 # TODO: hard coded PORT
3.1 --- a/OpenSecurity/bin/opensecurity_tray.pyw Wed Jan 29 15:38:12 2014 +0100
3.2 +++ b/OpenSecurity/bin/opensecurity_tray.pyw Fri Feb 14 14:08:24 2014 +0100
3.3 @@ -105,7 +105,8 @@
3.4 # get a proper browsing VM
3.5 browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
3.6 dlg_launch_image = os.path.join(sys.path[0], 'launch.pyw')
3.7 - process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
3.8 + #process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/iceweasel']
3.9 + process_command = [sys.executable, dlg_launch_image, browsing_vm, '/usr/bin/midori']
3.10 print(process_command)
3.11 process = subprocess.Popen(process_command, shell = False)
3.12
4.1 --- a/OpenSecurity/bin/os-admind.bat Wed Jan 29 15:38:12 2014 +0100
4.2 +++ b/OpenSecurity/bin/os-admind.bat Fri Feb 14 14:08:24 2014 +0100
4.3 @@ -1,4 +1,4 @@
4.4 @echo off
4.5 cd %0%\..
4.6 -C:\Python27\python opensecurityd.py 8080
4.7 +C:\Python27\python opensecurityd.pyw 8080
4.8