Skip to content

Commit

Permalink
Merge pull request #442 from anarkiwi/proc
Browse files Browse the repository at this point in the history
run_proc() did not wait, for the subprocess to exit. that could cause…
  • Loading branch information
cglewis authored Jul 30, 2021
2 parents b597a5e + b50196b commit 440e9e0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
7 changes: 1 addition & 6 deletions mercury/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import os
import shutil
import subprocess
import sys
import tempfile

Expand All @@ -27,16 +26,12 @@ def send_rabbit_msg(msg, channel, exchange='', routing_key='task_queue'):
print(" [X] %s UTC %r %r" % (str(datetime.datetime.utcnow()),
str(msg['id']), str(msg['file_path'])))

def run_proc(args, output=subprocess.DEVNULL):
proc = subprocess.Popen(args, stdout=output)
return proc.communicate()

def run_mercury(path):
with tempfile.TemporaryDirectory() as tempdir:
mercury = shutil.which('pmercury')
mercury_output = os.path.join(tempdir, 'mercury_output.txt')
args = [mercury, '-awxg', '-r', path, '-f', mercury_output]
run_proc(args)
network_tools_lib.run_proc(args)
with open(mercury_output, 'r') as f:
return f.read()

Expand Down
7 changes: 7 additions & 0 deletions network_tools_lib/network_tools_lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess


def mod_path(filename, file_for_dir=None):
Expand All @@ -11,3 +12,9 @@ def get_version():
ver_path = os.path.join(mod_path('VERSION', __file__))
with open(ver_path, 'r') as f:
return f.read().strip()


def run_proc(args, output=subprocess.DEVNULL):
with subprocess.Popen(args, stdout=output) as proc:
proc_output = proc.communicate()
return proc_output
7 changes: 1 addition & 6 deletions p0f/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import shutil
import subprocess
import sys
import tempfile

Expand All @@ -13,10 +12,6 @@
VERSION = network_tools_lib.get_version()


def run_proc(args, output=subprocess.DEVNULL):
proc = subprocess.Popen(args, stdout=output)
return proc.communicate()

def run_p0f(path):
with tempfile.TemporaryDirectory() as tempdir:
p0f = shutil.which('p0f')
Expand All @@ -25,7 +20,7 @@ def run_p0f(path):
p0f = '/usr/sbin/p0f'
p0f_output = os.path.join(tempdir, 'p0f_output.txt')
args = [p0f, '-r', path, '-o', p0f_output]
run_proc(args)
network_tools_lib.run_proc(args)
with open(p0f_output, 'r') as f:
return f.read()

Expand Down
7 changes: 7 additions & 0 deletions p0f/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@author: Charlie Lewis
"""
import os
import shutil
import sys

from .app import VERSION
Expand All @@ -14,11 +15,17 @@
from .app import run_tshark
from .app import run_p0f
from .app import build_result_json
import network_tools_lib


TEST_LO_CAP = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_lo.cap')


def test_runproc():
# returned filehandles both none as process has exited.
assert network_tools_lib.run_proc([shutil.which('ls')]) == (None, None)


def test_ispcap():
assert ispcap('afile.pcap') # nosec
assert not ispcap('notapcap.txt') # nosec
Expand Down

0 comments on commit 440e9e0

Please sign in to comment.