oliver@168
|
1 |
#!/usr/bin/env python
|
oliver@167
|
2 |
# -*- coding: utf-8 -*-
|
oliver@167
|
3 |
|
oliver@167
|
4 |
# ------------------------------------------------------------
|
oliver@167
|
5 |
# opensecurity_client_restful_server
|
oliver@167
|
6 |
#
|
oliver@167
|
7 |
# the OpenSecurity client RESTful server
|
oliver@167
|
8 |
#
|
oliver@167
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
oliver@167
|
10 |
#
|
oliver@167
|
11 |
# Copyright (C) 2013 AIT Austrian Institute of Technology
|
oliver@167
|
12 |
# AIT Austrian Institute of Technology GmbH
|
oliver@167
|
13 |
# Donau-City-Strasse 1 | 1220 Vienna | Austria
|
oliver@167
|
14 |
# http://www.ait.ac.at
|
oliver@167
|
15 |
#
|
oliver@167
|
16 |
# This program is free software; you can redistribute it and/or
|
oliver@167
|
17 |
# modify it under the terms of the GNU General Public License
|
oliver@167
|
18 |
# as published by the Free Software Foundation version 2.
|
oliver@167
|
19 |
#
|
oliver@167
|
20 |
# This program is distributed in the hope that it will be useful,
|
oliver@167
|
21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
oliver@167
|
22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
oliver@167
|
23 |
# GNU General Public License for more details.
|
oliver@167
|
24 |
#
|
oliver@167
|
25 |
# You should have received a copy of the GNU General Public License
|
oliver@167
|
26 |
# along with this program; if not, write to the Free Software
|
oliver@167
|
27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
oliver@167
|
28 |
# Boston, MA 02110-1301, USA.
|
oliver@167
|
29 |
# ------------------------------------------------------------
|
oliver@167
|
30 |
|
oliver@167
|
31 |
|
oliver@167
|
32 |
# ------------------------------------------------------------
|
oliver@167
|
33 |
# imports
|
oliver@167
|
34 |
|
oliver@167
|
35 |
import getpass
|
oliver@167
|
36 |
import glob
|
oliver@167
|
37 |
import json
|
oliver@167
|
38 |
import os
|
oliver@167
|
39 |
import os.path
|
oliver@167
|
40 |
import pickle
|
oliver@167
|
41 |
import platform
|
oliver@167
|
42 |
import socket
|
oliver@167
|
43 |
import subprocess
|
oliver@167
|
44 |
import sys
|
oliver@167
|
45 |
import threading
|
oliver@167
|
46 |
import time
|
oliver@167
|
47 |
import urllib
|
oliver@167
|
48 |
import urllib2
|
oliver@167
|
49 |
import web
|
oliver@167
|
50 |
import threading
|
oliver@167
|
51 |
import time
|
oliver@167
|
52 |
import string
|
oliver@167
|
53 |
import win32api
|
oliver@167
|
54 |
import win32con
|
BarthaM@176
|
55 |
import win32wnet
|
oliver@178
|
56 |
import win32netcon
|
BarthaM@176
|
57 |
import itertools
|
BarthaM@176
|
58 |
import ctypes
|
oliver@167
|
59 |
|
oliver@167
|
60 |
from opensecurity_util import logger, setupLogger, OpenSecurityException
|
oliver@167
|
61 |
if sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@167
|
62 |
from cygwin import Cygwin
|
oliver@167
|
63 |
|
oliver@167
|
64 |
# local
|
oliver@167
|
65 |
import __init__ as opensecurity
|
oliver@167
|
66 |
from environment import Environment
|
oliver@167
|
67 |
|
oliver@167
|
68 |
|
oliver@167
|
69 |
# ------------------------------------------------------------
|
oliver@167
|
70 |
# const
|
oliver@167
|
71 |
|
oliver@167
|
72 |
|
oliver@167
|
73 |
"""All the URLs we know mapping to class handler"""
|
oliver@167
|
74 |
opensecurity_urls = (
|
oliver@167
|
75 |
'/credentials', 'os_credentials',
|
oliver@167
|
76 |
'/keyfile', 'os_keyfile',
|
oliver@167
|
77 |
'/log', 'os_log',
|
oliver@167
|
78 |
'/notification', 'os_notification',
|
oliver@167
|
79 |
'/password', 'os_password',
|
oliver@167
|
80 |
'/netmount', 'os_netmount',
|
oliver@167
|
81 |
'/netumount', 'os_netumount',
|
BarthaM@176
|
82 |
'/netcleanup', 'os_netcleanup',
|
oliver@167
|
83 |
'/', 'os_root'
|
oliver@167
|
84 |
)
|
oliver@167
|
85 |
|
oliver@167
|
86 |
|
oliver@167
|
87 |
# ------------------------------------------------------------
|
oliver@167
|
88 |
# vars
|
oliver@167
|
89 |
|
oliver@167
|
90 |
|
oliver@167
|
91 |
"""lock for read/write log file"""
|
oliver@167
|
92 |
log_file_lock = threading.Lock()
|
oliver@167
|
93 |
|
oliver@167
|
94 |
"""timer for the log file bouncer"""
|
oliver@167
|
95 |
log_file_bouncer = None
|
oliver@167
|
96 |
|
oliver@167
|
97 |
|
oliver@167
|
98 |
"""The REST server object"""
|
oliver@167
|
99 |
server = None
|
oliver@167
|
100 |
|
oliver@167
|
101 |
|
oliver@167
|
102 |
# ------------------------------------------------------------
|
oliver@167
|
103 |
# code
|
oliver@167
|
104 |
|
oliver@167
|
105 |
|
oliver@167
|
106 |
class os_credentials:
|
oliver@167
|
107 |
|
oliver@167
|
108 |
"""OpenSecurity '/credentials' handler.
|
oliver@167
|
109 |
|
oliver@167
|
110 |
This is called on GET /credentials?text=TEXT.
|
oliver@167
|
111 |
Ideally this should pop up a user dialog to insert his
|
oliver@167
|
112 |
credentials based the given TEXT.
|
oliver@167
|
113 |
"""
|
oliver@167
|
114 |
|
oliver@167
|
115 |
def GET(self):
|
oliver@167
|
116 |
|
oliver@167
|
117 |
# pick the arguments
|
oliver@167
|
118 |
args = web.input()
|
oliver@167
|
119 |
|
oliver@167
|
120 |
# we _need_ a text
|
oliver@167
|
121 |
if not "text" in args:
|
oliver@167
|
122 |
raise web.badrequest('no text given')
|
oliver@167
|
123 |
|
oliver@167
|
124 |
# remember remote ip
|
oliver@167
|
125 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
oliver@167
|
126 |
|
oliver@167
|
127 |
# create the process which queries the user
|
oliver@167
|
128 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@167
|
129 |
process_command = [sys.executable, dlg_image, 'credentials', args.text]
|
oliver@167
|
130 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@167
|
131 |
|
oliver@167
|
132 |
# run process result handling in seprate thread (not to block main one)
|
oliver@167
|
133 |
bouncer = ProcessResultBouncer(process, remote_ip, '/credentials')
|
oliver@167
|
134 |
bouncer.start()
|
oliver@167
|
135 |
|
oliver@167
|
136 |
return 'user queried for credentials'
|
oliver@167
|
137 |
|
oliver@167
|
138 |
|
oliver@167
|
139 |
class os_keyfile:
|
oliver@167
|
140 |
|
oliver@167
|
141 |
"""OpenSecurity '/keyfile' handler.
|
oliver@167
|
142 |
|
oliver@167
|
143 |
This is called on GET /keyfile?text=TEXT.
|
oliver@167
|
144 |
Ideally this should pop up a user dialog to insert his
|
oliver@167
|
145 |
password along with a keyfile.
|
oliver@167
|
146 |
"""
|
oliver@167
|
147 |
|
oliver@167
|
148 |
def GET(self):
|
oliver@167
|
149 |
|
oliver@167
|
150 |
# pick the arguments
|
oliver@167
|
151 |
args = web.input()
|
oliver@167
|
152 |
|
oliver@167
|
153 |
# we _need_ a text
|
oliver@167
|
154 |
if not "text" in args:
|
oliver@167
|
155 |
raise web.badrequest('no text given')
|
oliver@167
|
156 |
|
oliver@167
|
157 |
# remember remote ip
|
oliver@167
|
158 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
oliver@167
|
159 |
|
oliver@167
|
160 |
# create the process which queries the user
|
oliver@167
|
161 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@167
|
162 |
process_command = [sys.executable, dlg_image, 'keyfile', args.text]
|
oliver@167
|
163 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@167
|
164 |
|
oliver@167
|
165 |
# run process result handling in seprate thread (not to block main one)
|
oliver@167
|
166 |
bouncer = ProcessResultBouncer(process, remote_ip, '/keyfile')
|
oliver@167
|
167 |
bouncer.start()
|
oliver@167
|
168 |
|
oliver@167
|
169 |
return 'user queried for password and keyfile'
|
oliver@167
|
170 |
|
oliver@167
|
171 |
|
oliver@167
|
172 |
class os_log:
|
oliver@167
|
173 |
|
oliver@167
|
174 |
"""OpenSecurity '/log' handler.
|
oliver@167
|
175 |
|
oliver@167
|
176 |
This is called on GET or POST on the log function /log
|
oliver@167
|
177 |
"""
|
oliver@167
|
178 |
|
oliver@167
|
179 |
def GET(self):
|
oliver@167
|
180 |
|
oliver@167
|
181 |
# pick the arguments
|
oliver@167
|
182 |
self.POST()
|
oliver@167
|
183 |
|
oliver@167
|
184 |
|
oliver@167
|
185 |
def POST(self):
|
oliver@167
|
186 |
|
oliver@167
|
187 |
# pick the arguments
|
oliver@167
|
188 |
args = web.input()
|
oliver@167
|
189 |
args['user'] = getpass.getuser()
|
oliver@167
|
190 |
args['system'] = platform.node() + " " + platform.system() + " " + platform.release()
|
oliver@167
|
191 |
|
oliver@167
|
192 |
# add these to new data to log
|
oliver@167
|
193 |
global log_file_lock
|
oliver@167
|
194 |
log_file_name = os.path.join(Environment('OpenSecurity').log_path, 'vm_new.log')
|
oliver@167
|
195 |
log_file_lock.acquire()
|
oliver@167
|
196 |
pickle.dump(args, open(log_file_name, 'ab'))
|
oliver@167
|
197 |
log_file_lock.release()
|
oliver@167
|
198 |
|
oliver@167
|
199 |
return "Ok"
|
oliver@167
|
200 |
|
oliver@167
|
201 |
|
oliver@167
|
202 |
class os_notification:
|
oliver@167
|
203 |
|
oliver@167
|
204 |
"""OpenSecurity '/notification' handler.
|
oliver@167
|
205 |
|
oliver@167
|
206 |
This is called on GET /notification?msgtype=TYPE&text=TEXT.
|
oliver@167
|
207 |
This will pop up an OpenSecurity notifcation window
|
oliver@167
|
208 |
"""
|
oliver@178
|
209 |
|
oliver@178
|
210 |
def POST(self):
|
oliver@178
|
211 |
return self.GET()
|
oliver@167
|
212 |
|
oliver@167
|
213 |
def GET(self):
|
oliver@167
|
214 |
|
oliver@167
|
215 |
# pick the arguments
|
oliver@167
|
216 |
args = web.input()
|
oliver@167
|
217 |
|
oliver@167
|
218 |
# we _need_ a type
|
oliver@167
|
219 |
if not "msgtype" in args:
|
oliver@167
|
220 |
raise web.badrequest('no msgtype given')
|
oliver@167
|
221 |
|
oliver@167
|
222 |
if not args.msgtype in ['information', 'warning', 'critical']:
|
oliver@167
|
223 |
raise web.badrequest('Unknown value for msgtype')
|
oliver@167
|
224 |
|
oliver@167
|
225 |
# we _need_ a text
|
oliver@167
|
226 |
if not "text" in args:
|
oliver@167
|
227 |
raise web.badrequest('no text given')
|
oliver@167
|
228 |
|
oliver@167
|
229 |
# invoke the user dialog as a subprocess
|
oliver@167
|
230 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
|
oliver@167
|
231 |
process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
|
oliver@167
|
232 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@167
|
233 |
|
oliver@167
|
234 |
return "Ok"
|
oliver@167
|
235 |
|
oliver@167
|
236 |
|
oliver@167
|
237 |
class os_password:
|
oliver@167
|
238 |
|
oliver@167
|
239 |
"""OpenSecurity '/password' handler.
|
oliver@167
|
240 |
|
oliver@167
|
241 |
This is called on GET /password?text=TEXT.
|
oliver@167
|
242 |
Ideally this should pop up a user dialog to insert his
|
oliver@167
|
243 |
password based device name.
|
oliver@167
|
244 |
"""
|
oliver@167
|
245 |
|
oliver@167
|
246 |
def GET(self):
|
oliver@167
|
247 |
|
oliver@167
|
248 |
# pick the arguments
|
oliver@167
|
249 |
args = web.input()
|
oliver@167
|
250 |
|
oliver@167
|
251 |
# we _need_ a text
|
oliver@167
|
252 |
if not "text" in args:
|
oliver@167
|
253 |
raise web.badrequest('no text given')
|
oliver@167
|
254 |
|
oliver@167
|
255 |
# remember remote ip
|
oliver@167
|
256 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
oliver@167
|
257 |
|
oliver@167
|
258 |
# create the process which queries the user
|
oliver@167
|
259 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@167
|
260 |
process_command = [sys.executable, dlg_image, 'password', args.text]
|
oliver@167
|
261 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@167
|
262 |
|
oliver@167
|
263 |
# run process result handling in seprate thread (not to block main one)
|
oliver@167
|
264 |
bouncer = ProcessResultBouncer(process, remote_ip, '/password')
|
oliver@167
|
265 |
bouncer.start()
|
oliver@167
|
266 |
|
oliver@167
|
267 |
return 'user queried for password'
|
oliver@167
|
268 |
|
BarthaM@176
|
269 |
def genNetworkDrive():
|
BarthaM@176
|
270 |
logical_drives = getLogicalDrives()
|
BarthaM@176
|
271 |
logger.info("Used logical drive letters: "+ str(logical_drives).strip('[]') )
|
BarthaM@176
|
272 |
drives = list(map(chr, range(68, 91)))
|
BarthaM@176
|
273 |
for drive in drives:
|
BarthaM@176
|
274 |
if drive not in logical_drives:
|
BarthaM@176
|
275 |
return drive
|
BarthaM@176
|
276 |
return None
|
BarthaM@176
|
277 |
|
BarthaM@176
|
278 |
def getLogicalDrives():
|
BarthaM@176
|
279 |
drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
|
BarthaM@176
|
280 |
drives = list(itertools.compress(string.ascii_uppercase, map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
|
BarthaM@176
|
281 |
return drives
|
BarthaM@176
|
282 |
|
BarthaM@176
|
283 |
def getNetworkPath(drive):
|
BarthaM@176
|
284 |
return win32wnet.WNetGetConnection(drive+':')
|
BarthaM@176
|
285 |
|
BarthaM@176
|
286 |
def getDriveType(drive):
|
BarthaM@176
|
287 |
return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
|
BarthaM@176
|
288 |
|
BarthaM@176
|
289 |
def getNetworkDrive(path):
|
BarthaM@176
|
290 |
for drive in getLogicalDrives():
|
BarthaM@176
|
291 |
#if is a network drive
|
BarthaM@176
|
292 |
if getDriveType(drive) == 4:
|
BarthaM@176
|
293 |
network_path = getNetworkPath(drive)
|
BarthaM@176
|
294 |
if path in network_path:
|
BarthaM@176
|
295 |
return drive
|
BarthaM@176
|
296 |
return None
|
oliver@178
|
297 |
|
oliver@178
|
298 |
def mapDrive(drive, networkPath, user, password):
|
oliver@178
|
299 |
print networkPath
|
oliver@178
|
300 |
if (os.path.exists(networkPath)):
|
oliver@178
|
301 |
print networkPath, " is found..."
|
oliver@178
|
302 |
print "Trying to map ", networkPath, " on to ", drive, " ....."
|
oliver@178
|
303 |
try:
|
oliver@178
|
304 |
win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive, networkPath, None, user, password)
|
oliver@178
|
305 |
except:
|
oliver@178
|
306 |
print "Unexpected error..."
|
oliver@178
|
307 |
return 1
|
oliver@178
|
308 |
print "Mapping successful"
|
oliver@178
|
309 |
return 0
|
oliver@178
|
310 |
else:
|
oliver@178
|
311 |
print "Network path unreachable..."
|
oliver@178
|
312 |
return 1
|
oliver@178
|
313 |
|
oliver@167
|
314 |
# handles netumount request
|
oliver@167
|
315 |
class MountNetworkDriveHandler(threading.Thread):
|
BarthaM@176
|
316 |
networkPath = None
|
BarthaM@176
|
317 |
def __init__(self, net_path):
|
oliver@167
|
318 |
threading.Thread.__init__(self)
|
oliver@167
|
319 |
self.networkPath = net_path
|
oliver@167
|
320 |
|
oliver@167
|
321 |
def run(self):
|
BarthaM@176
|
322 |
drive = genNetworkDrive()
|
BarthaM@176
|
323 |
if not drive:
|
BarthaM@176
|
324 |
logger.error("Failed to assign drive letter for: " + self.networkPath)
|
BarthaM@176
|
325 |
return 1
|
BarthaM@176
|
326 |
else:
|
BarthaM@176
|
327 |
logger.info("Assigned drive " + drive + " to " + self.networkPath)
|
BarthaM@176
|
328 |
|
oliver@167
|
329 |
#Check for drive availability
|
BarthaM@176
|
330 |
drive = drive+':'
|
BarthaM@176
|
331 |
if os.path.exists(drive):
|
BarthaM@176
|
332 |
logger.error("Drive letter is already in use: " + drive)
|
oliver@167
|
333 |
return 1
|
oliver@167
|
334 |
|
oliver@167
|
335 |
#Check for network resource availability
|
oliver@178
|
336 |
retry = 20
|
oliver@167
|
337 |
while not os.path.exists(self.networkPath):
|
oliver@178
|
338 |
if retry == 0:
|
oliver@178
|
339 |
break
|
oliver@178
|
340 |
logger.info("Path not accessible: " + self.networkPath + " retrying")
|
oliver@167
|
341 |
time.sleep(1)
|
oliver@167
|
342 |
retry-=1
|
oliver@178
|
343 |
|
oliver@178
|
344 |
return mapDrive(drive, self.networkPath, "", "")
|
oliver@167
|
345 |
|
oliver@167
|
346 |
class os_netmount:
|
oliver@167
|
347 |
|
oliver@167
|
348 |
"""OpenSecurity '/netmount' handler"""
|
oliver@167
|
349 |
|
oliver@167
|
350 |
def GET(self):
|
oliver@167
|
351 |
# pick the arguments
|
oliver@167
|
352 |
args = web.input()
|
oliver@167
|
353 |
|
oliver@167
|
354 |
# we _need_ a net_resource
|
oliver@167
|
355 |
if not "net_resource" in args:
|
oliver@167
|
356 |
raise web.badrequest('no net_resource given')
|
oliver@167
|
357 |
|
BarthaM@176
|
358 |
driveHandler = MountNetworkDriveHandler(args['net_resource'])
|
oliver@167
|
359 |
driveHandler.start()
|
oliver@167
|
360 |
driveHandler.join(None)
|
oliver@167
|
361 |
return 'Ok'
|
oliver@178
|
362 |
|
oliver@178
|
363 |
def unmapDrive(drive, force=0):
|
oliver@178
|
364 |
print "drive in use, trying to unmap..."
|
oliver@178
|
365 |
if force == 0:
|
oliver@178
|
366 |
print "Executing un-forced call..."
|
oliver@178
|
367 |
|
oliver@178
|
368 |
try:
|
oliver@178
|
369 |
win32wnet.WNetCancelConnection2(drive, 1, force)
|
oliver@178
|
370 |
print drive, "successfully unmapped..."
|
oliver@178
|
371 |
return 0
|
oliver@178
|
372 |
except:
|
oliver@178
|
373 |
print "Unmap failed, try again..."
|
oliver@178
|
374 |
return 1
|
oliver@178
|
375 |
|
oliver@167
|
376 |
# handles netumount request
|
oliver@167
|
377 |
class UmountNetworkDriveHandler(threading.Thread):
|
BarthaM@176
|
378 |
networkPath = None
|
oliver@167
|
379 |
running = True
|
oliver@167
|
380 |
|
BarthaM@176
|
381 |
def __init__(self, path):
|
oliver@167
|
382 |
threading.Thread.__init__(self)
|
BarthaM@176
|
383 |
self.networkPath = path
|
oliver@167
|
384 |
|
oliver@167
|
385 |
def run(self):
|
oliver@167
|
386 |
while self.running:
|
BarthaM@176
|
387 |
drive = getNetworkDrive(self.networkPath)
|
BarthaM@176
|
388 |
if not drive:
|
BarthaM@176
|
389 |
logger.info("Failed to retrieve drive letter for: " + self.networkPath + ". Successfully deleted or missing.")
|
oliver@167
|
390 |
self.running = False
|
oliver@167
|
391 |
else:
|
BarthaM@176
|
392 |
drive = drive+':'
|
BarthaM@176
|
393 |
logger.info("Unmounting drive " + drive + " for " + self.networkPath)
|
oliver@178
|
394 |
result = unmapDrive(drive, force=1)
|
oliver@178
|
395 |
if result != 0:
|
oliver@167
|
396 |
continue
|
oliver@167
|
397 |
|
oliver@167
|
398 |
|
oliver@167
|
399 |
class os_netumount:
|
oliver@167
|
400 |
|
oliver@167
|
401 |
"""OpenSecurity '/netumount' handler"""
|
oliver@167
|
402 |
|
oliver@167
|
403 |
def GET(self):
|
oliver@167
|
404 |
# pick the arguments
|
oliver@167
|
405 |
args = web.input()
|
oliver@167
|
406 |
|
BarthaM@176
|
407 |
# we _need_ a net_resource
|
BarthaM@176
|
408 |
if not "net_resource" in args:
|
BarthaM@176
|
409 |
raise web.badrequest('no net_resource given')
|
oliver@167
|
410 |
|
BarthaM@176
|
411 |
driveHandler = UmountNetworkDriveHandler(args['net_resource'])
|
oliver@167
|
412 |
driveHandler.start()
|
oliver@167
|
413 |
driveHandler.join(None)
|
oliver@167
|
414 |
return 'Ok'
|
BarthaM@176
|
415 |
|
BarthaM@176
|
416 |
class os_netcleanup:
|
oliver@167
|
417 |
|
BarthaM@176
|
418 |
"""OpenSecurity '/netcleanup' handler"""
|
BarthaM@176
|
419 |
|
BarthaM@176
|
420 |
def GET(self):
|
BarthaM@176
|
421 |
# pick the arguments
|
BarthaM@176
|
422 |
args = web.input()
|
BarthaM@176
|
423 |
|
BarthaM@176
|
424 |
# we _need_ a net_resource
|
BarthaM@176
|
425 |
if not "hostonly_ip" in args:
|
BarthaM@181
|
426 |
raise web.badrequest('no hostonly_ip given')
|
BarthaM@176
|
427 |
|
BarthaM@176
|
428 |
ip = args['hostonly_ip']
|
BarthaM@176
|
429 |
ip = ip[:ip.rindex('.')]
|
BarthaM@176
|
430 |
drives = getLogicalDrives()
|
BarthaM@176
|
431 |
for drive in drives:
|
BarthaM@176
|
432 |
# found network drive
|
BarthaM@176
|
433 |
if getDriveType(drive) == 4:
|
BarthaM@176
|
434 |
path = getNetworkPath(drive)
|
BarthaM@176
|
435 |
if ip in path:
|
BarthaM@176
|
436 |
driveHandler = UmountNetworkDriveHandler(path)
|
BarthaM@176
|
437 |
driveHandler.start()
|
BarthaM@176
|
438 |
driveHandler.join(None)
|
oliver@167
|
439 |
|
oliver@167
|
440 |
class os_root:
|
oliver@167
|
441 |
|
oliver@167
|
442 |
"""OpenSecurity '/' handler"""
|
oliver@167
|
443 |
|
oliver@167
|
444 |
def GET(self):
|
oliver@167
|
445 |
|
oliver@167
|
446 |
res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % opensecurity.__version__
|
oliver@167
|
447 |
|
oliver@167
|
448 |
# add some sample links
|
oliver@167
|
449 |
res = res + """
|
oliver@167
|
450 |
|
oliver@167
|
451 |
USAGE EXAMPLES:
|
oliver@167
|
452 |
|
oliver@167
|
453 |
Request a password:
|
oliver@167
|
454 |
(copy paste this into your browser's address field after the host:port)
|
oliver@167
|
455 |
|
oliver@167
|
456 |
/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
|
oliver@167
|
457 |
|
oliver@167
|
458 |
(eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
|
oliver@167
|
459 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
oliver@167
|
460 |
|
oliver@167
|
461 |
|
oliver@167
|
462 |
Request a combination of user and password:
|
oliver@167
|
463 |
(copy paste this into your browser's address field after the host:port)
|
oliver@167
|
464 |
|
oliver@167
|
465 |
/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
|
oliver@167
|
466 |
|
oliver@167
|
467 |
(eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
|
oliver@167
|
468 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
oliver@167
|
469 |
|
oliver@167
|
470 |
|
oliver@167
|
471 |
Request a combination of password and keyfile:
|
oliver@167
|
472 |
(copy paste this into your browser's address field after the host:port)
|
oliver@167
|
473 |
|
oliver@167
|
474 |
/keyfile?text=Your%20private%20RSA%20Keyfile%3A
|
oliver@167
|
475 |
|
oliver@167
|
476 |
(eg.: http://127.0.0.1:8090//keyfile?text=Your%20private%20RSA%20Keyfile%3A)
|
oliver@167
|
477 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
oliver@167
|
478 |
|
oliver@167
|
479 |
|
oliver@167
|
480 |
Start a Browser:
|
oliver@167
|
481 |
(copy paste this into your browser's address field after the host:port)
|
oliver@167
|
482 |
|
oliver@167
|
483 |
/application?vm=Debian+7&app=Browser
|
oliver@167
|
484 |
|
oliver@167
|
485 |
(e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
|
oliver@167
|
486 |
"""
|
oliver@167
|
487 |
|
oliver@167
|
488 |
return res
|
oliver@167
|
489 |
|
oliver@167
|
490 |
|
oliver@167
|
491 |
class ProcessResultBouncer(threading.Thread):
|
oliver@167
|
492 |
|
oliver@167
|
493 |
"""A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
|
oliver@167
|
494 |
|
oliver@167
|
495 |
def __init__(self, process, remote_ip, resource):
|
oliver@167
|
496 |
|
oliver@167
|
497 |
"""ctor"""
|
oliver@167
|
498 |
|
oliver@167
|
499 |
threading.Thread.__init__(self)
|
oliver@167
|
500 |
self._process = process
|
oliver@167
|
501 |
self._remote_ip = remote_ip
|
oliver@167
|
502 |
self._resource = resource
|
oliver@167
|
503 |
|
oliver@167
|
504 |
|
oliver@167
|
505 |
def stop(self):
|
oliver@167
|
506 |
|
oliver@167
|
507 |
"""stop thread"""
|
oliver@167
|
508 |
self.running = False
|
oliver@167
|
509 |
|
oliver@167
|
510 |
|
oliver@167
|
511 |
def run(self):
|
oliver@167
|
512 |
|
oliver@167
|
513 |
"""run the thread"""
|
oliver@167
|
514 |
|
oliver@167
|
515 |
# invoke the user dialog as a subprocess
|
oliver@167
|
516 |
result = self._process.communicate()[0]
|
oliver@167
|
517 |
if self._process.returncode != 0:
|
oliver@167
|
518 |
print 'user request has been aborted.'
|
oliver@167
|
519 |
return
|
oliver@167
|
520 |
|
oliver@167
|
521 |
# all ok, tell send request back appropriate destination
|
oliver@167
|
522 |
try:
|
oliver@167
|
523 |
j = json.loads(result)
|
oliver@167
|
524 |
except:
|
oliver@167
|
525 |
print 'error in password parsing'
|
oliver@167
|
526 |
return
|
oliver@167
|
527 |
|
oliver@167
|
528 |
# by provided a 'data' we turn this into a POST statement
|
oliver@167
|
529 |
url_addr = 'http://' + self._remote_ip + ':58080' + self._resource
|
oliver@167
|
530 |
req = urllib2.Request(url_addr, urllib.urlencode(j))
|
oliver@167
|
531 |
try:
|
oliver@167
|
532 |
res = urllib2.urlopen(req)
|
oliver@167
|
533 |
except:
|
oliver@167
|
534 |
print 'failed to contact: ' + url_addr
|
oliver@167
|
535 |
return
|
oliver@167
|
536 |
|
oliver@167
|
537 |
|
oliver@167
|
538 |
class RESTServerThread(threading.Thread):
|
oliver@167
|
539 |
|
oliver@167
|
540 |
"""Thread for serving the REST API."""
|
oliver@167
|
541 |
|
oliver@167
|
542 |
def __init__(self, port):
|
oliver@167
|
543 |
|
oliver@167
|
544 |
"""ctor"""
|
oliver@167
|
545 |
threading.Thread.__init__(self)
|
oliver@167
|
546 |
self._port = port
|
oliver@167
|
547 |
|
oliver@167
|
548 |
def stop(self):
|
oliver@167
|
549 |
|
oliver@167
|
550 |
"""stop thread"""
|
oliver@167
|
551 |
self.running = False
|
oliver@167
|
552 |
|
oliver@167
|
553 |
|
oliver@167
|
554 |
def run(self):
|
oliver@167
|
555 |
|
oliver@167
|
556 |
"""run the thread"""
|
oliver@167
|
557 |
_serve(self._port)
|
oliver@167
|
558 |
|
oliver@167
|
559 |
|
oliver@167
|
560 |
|
oliver@167
|
561 |
def is_already_running(port = 8090):
|
oliver@167
|
562 |
|
oliver@167
|
563 |
"""check if this is started twice"""
|
oliver@167
|
564 |
|
oliver@167
|
565 |
try:
|
oliver@167
|
566 |
s = socket.create_connection(('127.0.0.1', port), 0.5)
|
oliver@167
|
567 |
except:
|
oliver@167
|
568 |
return False
|
oliver@167
|
569 |
|
oliver@167
|
570 |
return True
|
oliver@167
|
571 |
|
oliver@167
|
572 |
|
oliver@167
|
573 |
def _bounce_vm_logs():
|
oliver@167
|
574 |
|
oliver@167
|
575 |
"""grab all logs from the VMs and push them to the log servers"""
|
oliver@167
|
576 |
|
oliver@167
|
577 |
global log_file_lock
|
oliver@167
|
578 |
|
oliver@167
|
579 |
# pick the highest current number
|
oliver@167
|
580 |
cur = 0
|
oliver@167
|
581 |
for f in glob.iglob(os.path.join(Environment('OpenSecurity').log_path, 'vm_cur.log.*')):
|
oliver@167
|
582 |
try:
|
oliver@167
|
583 |
n = f.split('.')[-1:][0]
|
oliver@167
|
584 |
if cur < int(n):
|
oliver@167
|
585 |
cur = int(n)
|
oliver@167
|
586 |
except:
|
oliver@167
|
587 |
pass
|
oliver@167
|
588 |
|
oliver@167
|
589 |
cur = cur + 1
|
oliver@167
|
590 |
|
oliver@167
|
591 |
# first add new vm logs to our existing one: rename the log file
|
oliver@167
|
592 |
log_file_name_new = os.path.join(Environment('OpenSecurity').log_path, 'vm_new.log')
|
oliver@167
|
593 |
log_file_name_cur = os.path.join(Environment('OpenSecurity').log_path, 'vm_cur.log.' + str(cur))
|
oliver@167
|
594 |
log_file_lock.acquire()
|
oliver@167
|
595 |
try:
|
oliver@167
|
596 |
os.rename(log_file_name_new, log_file_name_cur)
|
oliver@167
|
597 |
print('new log file: ' + log_file_name_cur)
|
oliver@167
|
598 |
except:
|
oliver@167
|
599 |
pass
|
oliver@167
|
600 |
log_file_lock.release()
|
oliver@167
|
601 |
|
oliver@167
|
602 |
# now we have a list of next log files to dump
|
oliver@167
|
603 |
log_files = glob.glob(os.path.join(Environment('OpenSecurity').log_path, 'vm_cur.log.*'))
|
oliver@167
|
604 |
log_files.sort()
|
oliver@167
|
605 |
for log_file in log_files:
|
oliver@167
|
606 |
|
oliver@167
|
607 |
try:
|
oliver@167
|
608 |
f = open(log_file, 'rb')
|
oliver@167
|
609 |
while True:
|
oliver@167
|
610 |
l = pickle.load(f)
|
oliver@167
|
611 |
_push_log(l)
|
oliver@167
|
612 |
|
oliver@167
|
613 |
except EOFError:
|
oliver@167
|
614 |
|
oliver@167
|
615 |
try:
|
oliver@167
|
616 |
os.remove(log_file)
|
oliver@167
|
617 |
except:
|
oliver@167
|
618 |
logger.warning('tried to delete log file (pushed to EOF) "' + log_file + '" but failed')
|
oliver@167
|
619 |
|
oliver@167
|
620 |
except:
|
oliver@167
|
621 |
logger.warning('encountered error while pushing log file "' + log_file + '"')
|
oliver@167
|
622 |
break
|
oliver@167
|
623 |
|
oliver@167
|
624 |
# start bouncer again ...
|
oliver@167
|
625 |
global log_file_bouncer
|
oliver@167
|
626 |
log_file_bouncer = threading.Timer(5.0, _bounce_vm_logs)
|
oliver@167
|
627 |
log_file_bouncer.start()
|
oliver@167
|
628 |
|
oliver@167
|
629 |
|
oliver@167
|
630 |
def _push_log(log):
|
oliver@167
|
631 |
"""POST a single log to log server
|
oliver@167
|
632 |
|
oliver@167
|
633 |
@param log the log POST param
|
oliver@167
|
634 |
"""
|
oliver@167
|
635 |
|
oliver@168
|
636 |
log_server_url = "http://extern.x-net.at/opensecurity/log"
|
oliver@167
|
637 |
try:
|
oliver@167
|
638 |
key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\OpenSecurity')
|
oliver@167
|
639 |
log_server_url = str(win32api.RegQueryValueEx(key, 'LogServerURL')[0])
|
oliver@167
|
640 |
win32api.RegCloseKey(key)
|
oliver@167
|
641 |
except:
|
oliver@168
|
642 |
logger.warning('Cannot open Registry HKEY_LOCAL_MACHINE\SOFTWARE\OpenSecurity and get "LogServerURL" value, using default instead')
|
oliver@167
|
643 |
|
oliver@167
|
644 |
# by provided a 'data' we turn this into a POST statement
|
oliver@167
|
645 |
d = urllib.urlencode(log)
|
oliver@167
|
646 |
req = urllib2.Request(log_server_url, d)
|
oliver@167
|
647 |
urllib2.urlopen(req)
|
oliver@167
|
648 |
logger.debug('pushed log to server: ' + str(log_server_url))
|
oliver@167
|
649 |
|
oliver@167
|
650 |
|
oliver@167
|
651 |
def _serve(port):
|
oliver@167
|
652 |
|
oliver@167
|
653 |
"""Start the REST server"""
|
oliver@167
|
654 |
|
oliver@167
|
655 |
global server
|
oliver@167
|
656 |
|
oliver@167
|
657 |
# start the VM-log bouncer timer
|
oliver@167
|
658 |
global log_file_bouncer
|
oliver@167
|
659 |
log_file_bouncer = threading.Timer(5.0, _bounce_vm_logs)
|
oliver@167
|
660 |
log_file_bouncer.start()
|
oliver@167
|
661 |
|
oliver@167
|
662 |
# trick the web.py server
|
oliver@167
|
663 |
sys.argv = [__file__, str(port)]
|
oliver@167
|
664 |
server = web.application(opensecurity_urls, globals())
|
oliver@167
|
665 |
server.run()
|
oliver@167
|
666 |
|
oliver@167
|
667 |
|
oliver@167
|
668 |
def serve(port = 8090, background = False):
|
oliver@167
|
669 |
|
oliver@167
|
670 |
"""Start serving the REST Api
|
oliver@167
|
671 |
port ... port number to listen on
|
oliver@167
|
672 |
background ... cease into background (spawn thread) and return immediately"""
|
oliver@167
|
673 |
|
oliver@167
|
674 |
# start threaded or direct version
|
oliver@167
|
675 |
if background == True:
|
oliver@167
|
676 |
t = RESTServerThread(port)
|
oliver@167
|
677 |
t.start()
|
oliver@167
|
678 |
else:
|
oliver@167
|
679 |
_serve(port)
|
oliver@167
|
680 |
|
oliver@167
|
681 |
def stop():
|
oliver@167
|
682 |
|
oliver@167
|
683 |
"""Stop serving the REST Api"""
|
oliver@167
|
684 |
|
oliver@167
|
685 |
global server
|
oliver@167
|
686 |
if server is None:
|
oliver@167
|
687 |
return
|
oliver@167
|
688 |
|
oliver@167
|
689 |
global log_file_bouncer
|
oliver@167
|
690 |
if log_file_bouncer is not None:
|
oliver@167
|
691 |
log_file_bouncer.cancel()
|
oliver@167
|
692 |
|
oliver@167
|
693 |
server.stop()
|
oliver@167
|
694 |
|
oliver@167
|
695 |
# start
|
oliver@167
|
696 |
if __name__ == "__main__":
|
oliver@167
|
697 |
serve()
|
oliver@167
|
698 |
|