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