Commit c662cba6 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Add option for nmap output directory

- Load new options into config variable
- Create output filename
- Spawn process using full nmap options
- Clean up code layout a bit
parent 7ae0438e
......@@ -32,5 +32,9 @@ datadir = spawner_data
# located in the datadir.
timingdb = timing.db
# This defines the location of the nmap binary
nmap_binary = '/usr/local/bin/nmap'
\ No newline at end of file
# This defines the location of the nmap binary, including the binary name
nmap_binary = '/usr/local/bin/nmap'
# The directory where output from scans should be placed. Include the trailing
# slash.
output_dir = '/opt/skynet/output/'
\ No newline at end of file
......@@ -120,10 +120,14 @@ def loadconfig():
cfg['configdir'] = config.get('spawner', 'configdir')
cfg['datadir'] = config.get('spawner', 'datadir')
cfg['timingdb'] = config.get('spawner', 'timingdb')
cfg['nmap_binary'] = config.get('spawner', 'nmap_binary')
cfg['output_dir'] = config.get('spawner', 'output_dir')
logger.debug('configdir = {0}'.format(cfg['configdir']))
logger.debug('datadir = {0}'.format(cfg['datadir']))
logger.debug('timingdb = {0}'.format(cfg['timingdb']))
logger.debug('nmap_binary = {0}'.format(cfg['nmap_binary']))
logger.debug('output_dir = {0}'.format(cfg['output_dir']))
def check_configdir():
logger.info('Check Config')
......@@ -169,13 +173,13 @@ def check_timing(loopstarttime, timingdb):
try:
timingcursor = timingdb.cursor()
timingcursor.execute('''SELECT t.server_id, td.override_flag FROM timers AS t, timer_details AS td WHERE (t.minute = ?
OR t.minute = "*") AND (t.hour = ? OR t.hour = "*") AND
(t.day = ? OR t.day = "*") AND (t.month = ? OR
t.month = "*") AND t.server_id = td.server_id''', (loopstarttime.minute,
loopstarttime.hour,
loopstarttime.day,
loopstarttime.month))
timingcursor.execute('''SELECT t.server_id, td.override_flag FROM timers
AS t, timer_details AS td WHERE (t.minute = ? OR
t.minute = "*") AND (t.hour = ? OR t.hour = "*")
AND (t.day = ? OR t.day = "*") AND (t.month = ? OR
t.month = "*") AND t.server_id = td.server_id''',
(loopstarttime.minute, loopstarttime.hour,
loopstarttime.day, loopstarttime.month))
rows = timingcursor.fetchall()
......@@ -216,21 +220,22 @@ def check_complete(spawnlist, timingdb):
# and is still running, then remove it from the spawnlist
# and set overrun = 1
spawnlist.remove((row[0],0))
logger.debug('ID ' + str(row[0]) + ' not allowed to override, removed from spawn list')
logger.debug('ID ' + str(row[0]) + ' not allowed to '
'override, removed from spawn list')
timingcursor.execute('UPDATE spawned SET overtime = 1 '
'WHERE server_id = ? AND pid = ?',
timingcursor.execute('''UPDATE spawned SET overtime = 1
WHERE server_id = ? AND pid = ?''',
(row[0], row[2],))
except:
pass
else:
logger.debug('PID ' + str(row[2]) + ' Completed')
end_time = int(time.mktime(time.gmtime()))
timingcursor.execute('DELETE FROM spawned WHERE server_id = ? '
'AND pid = ?', (row[0], row[2]))
timingcursor.execute('''DELETE FROM spawned WHERE server_id = ?
AND pid = ?''', (row[0], row[2]))
timingcursor.execute('INSERT INTO spawn_log (server_id, '
'start_time, end_time) VALUES (?, ?, ?)',
timingcursor.execute('''INSERT INTO spawn_log (server_id,
start_time, end_time) VALUES (?, ?, ?)''',
(row[0], row[1], end_time))
timingdb.commit()
......@@ -251,7 +256,8 @@ def spawn_process(spawnlist):
# The parent process should jump to the next loop iteration
continue
except OSError, e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.stderr.write("fork #1 failed: (%d) %s\n" %
(e.errno, e.strerror))
sys.exit(1)
# Set the parameters for the child
......@@ -266,7 +272,8 @@ def spawn_process(spawnlist):
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.stderr.write("fork #2 failed: (%d) %s\n" %
(e.errno, e.strerror))
sys.exit(1)
# Set the streams for the process
......@@ -284,6 +291,8 @@ def spawn_process(spawnlist):
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# Since we're in a different PID, if we passed a timingdb handle in,
# it wouldn't be valid. So, get a new one.
try:
timingdb = open_timing_database()
timingcursor = timingdb.cursor()
......@@ -293,31 +302,34 @@ def spawn_process(spawnlist):
logger.exception("Error creating cursor : %s", e)
logger.debug('Timer number ' + str(timer))
timingcursor.execute('SELECT ip_range, nmap_options FROM timer_details WHERE '
'server_id = ?', (timer,))
timingcursor.execute('''SELECT ip_range, nmap_options FROM timer_details
WHERE server_id = ?''', (timer,))
nmap_options = timingcursor.fetchone()
optionlist = shlex.split('nmap ' + nmap_options[1] + ' ' +
nmap_options[0])
nmap_filename = 'nmap_xml.' + str(timer) + '.' + str(start_time) + \
'.xml'
optionlist = shlex.split(cfg['nmap_binary'] + ' -oX ' +
cfg['output_dir'] + nmap_filename + ' ' +
nmap_options[1] + ' ' + nmap_options[0])
logger.debug('Will spawn with options ' + str(optionlist))
# TODO : We need to get the command line, split it, and call it
# split with shlex.split(commandline)
# then use the output in Popen
spawned = Popen(['./script-test.sh'])
spawned = Popen(optionlist)
logger.debug('Spawned - ' + str(spawned.pid))
timingcursor.execute('INSERT INTO spawned (server_id, start_time, '
'pid, overtime) VALUES (?, ?, ?, 0)',
(timer, start_time, spawned.pid))
timingcursor.execute('''INSERT INTO spawned (server_id, start_time, pid,
overtime) VALUES (?, ?, ?, 0)''', (timer,
start_time,
spawned.pid))
timingdb.commit()
spawned.wait()
sys.exit(0)
def open_timing_database():
logger.info('Opening timing database')
if (os.path.exists(cfg['datadir'])):
......
......@@ -11,6 +11,7 @@ This is a “living” document and is not feature complete. Check back often f
* NMAP
* Local timing and parameters data
* Local control daemon
* SQLite database for data storage
### Control system
* Control nmap options ?
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment