Beispiel #1
0
def test_shutdown(tmpworkdir, httpserver):  # pylint: disable=unused-argument,redefined-outer-name
    """test no-work, continuous job assignment and shutdown signal handling"""

    sserver = SimpleServer(httpserver)

    proc_agent = multiprocessing.Process(
        target=agent_main,
        args=(['--server', sserver.url, '--apikey', 'dummy', '--debug'], ))
    proc_agent.start()
    sleep(1)
    assert proc_agent.is_alive()

    agent_main(['--shutdown', str(proc_agent.pid)])
    proc_agent.join(1)
    assert not proc_agent.is_alive()
Beispiel #2
0
def test_basic(tmpworkdir, test_dummy_a):  # pylint: disable=unused-argument
    """dummy module execution test"""

    result = agent_main(['--assignment', json.dumps(test_dummy_a), '--debug'])
    assert result == 0
    assert test_dummy_a['targets'][0] in file_from_zip(
        '%s.zip' % test_dummy_a['id'], 'assignment.json').decode('utf-8')
Beispiel #3
0
def test_fail_server_communication(tmpworkdir, httpserver):  # pylint: disable=unused-argument,redefined-outer-name
    """tests failure handling while retrieving assignment or uploading output"""

    # oneshot will abort upon first get_assignment error, coverage for external
    # processes is not working yet. to test the agent's communication error
    # handling: the agent will run in-thread, will be breaked by
    # signal/exception and fail_server internals will be checked manualy

    sserver = FailServer(httpserver)

    with terminate_after(1):
        agent_main([
            '--server', sserver.url, '--apikey', 'dummy', '--debug',
            '--backofftime', '0.1'
        ])

    assert sserver.cnt_assign > 1
    assert sserver.cnt_output > 1
Beispiel #4
0
def test_terminate_with_assignment(tmpworkdir, cleanup_markedprocess,
                                   test_longrun_a):  # pylint: disable=unused-argument
    """
    Agent external process handling test. Even thou the test uses nmap module, the point is to test sner.agent.modules.Base _terminate helper.
    """

    proc_agent = multiprocessing.Process(
        target=agent_main,
        args=(['--assignment',
               json.dumps(test_longrun_a), '--debug'], ))
    proc_agent.start()
    sleep(1)
    assert proc_agent.pid
    assert proc_agent.is_alive()

    agent_main(['--terminate', str(proc_agent.pid)])
    proc_agent.join(3)
    assert not proc_agent.is_alive()
    assert 'MARKEDPROCESS' not in os.popen('ps -f').read()
Beispiel #5
0
def test_empty_server_communication(tmpworkdir, live_server, apikey):  # pylint: disable=unused-argument,redefined-outer-name
    """tests oneshot vs wait on assignment on empty server"""

    result = agent_main([
        '--server',
        live_server.url(), '--apikey', apikey, '--debug', '--oneshot'
    ])
    assert result == 0

    proc_agent = multiprocessing.Process(
        target=agent_main,
        args=(['--server',
               live_server.url(), '--apikey', apikey, '--debug'], ))
    proc_agent.start()
    sleep(2)
    assert proc_agent.pid
    assert proc_agent.is_alive()
    agent_main(['--terminate', str(proc_agent.pid)])
    proc_agent.join(4)
    assert not proc_agent.is_alive()
Beispiel #6
0
def test_exception_in_module(tmpworkdir):  # pylint: disable=unused-argument
    """test exception handling during agent module execution"""

    test_a = {
        'id': str(uuid4()),
        'module': 'notexist',
        'params': '',
        'targets': []
    }

    result = agent_main(['--assignment', json.dumps(test_a)])
    assert result == 1
    assert os.path.exists('%s.zip' % test_a['id'])
Beispiel #7
0
def test_run_with_liveserver(tmpworkdir, live_server, apikey,
                             test_dummy_target):  # pylint: disable=unused-argument
    """test basic agent's networking codepath; fetch, execute, pack and upload assignment"""

    result = agent_main([
        '--server',
        live_server.url(), '--apikey', apikey, '--debug', '--queue',
        str(test_dummy_target.queue_id), '--oneshot'
    ])
    assert result == 0

    job = Job.query.filter(Job.queue_id == test_dummy_target.queue_id).one()
    assert test_dummy_target.target in file_from_zip(
        job.output_abspath, 'assignment.json').decode('utf-8')
Beispiel #8
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """nmap module execution test"""

    test_a = {
        'id': str(uuid4()),
        'module': 'nmap',
        'params': '-sL',
        'targets': ['127.0.0.1']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert 'Host: 127.0.0.1 (localhost)' in file_from_zip(
        '%s.zip' % test_a['id'], 'output.gnmap').decode('utf-8')
Beispiel #9
0
def test_terminate_with_liveserver(tmpworkdir, live_server, apikey,
                                   cleanup_markedprocess, test_longrun_target):  # noqa: ignore=E501  pylint: disable=unused-argument,redefined-outer-name
    """
    Agent external process handling test. Even thou the test uses nmap module, the point is to test sner.agent.modules.Base _terminate helper.
    """

    proc_agent = multiprocessing.Process(target=agent_main,
                                         args=([
                                             '--server',
                                             live_server.url(), '--apikey',
                                             apikey, '--debug', '--queue',
                                             str(test_longrun_target.queue_id),
                                             '--oneshot'
                                         ], ))
    proc_agent.start()
    sleep(1)
    assert proc_agent.pid
    assert proc_agent.is_alive()

    agent_main(['--terminate', str(proc_agent.pid)])
    proc_agent.join(3)
    assert not proc_agent.is_alive()
    assert 'MARKEDPROCESS' not in os.popen('ps -f').read()
Beispiel #10
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """manymap module execution test"""

    test_a = {
        'id': str(uuid4()),
        'module': 'manymap',
        'params': '-sV',
        'targets': ['invalid', 'tcp://127.0.0.1:1', 'udp://[::1]:2']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert 'Host: 127.0.0.1 (localhost)' in file_from_zip(
        '%s.zip' % test_a['id'], 'output-1.gnmap').decode('utf-8')
    assert '# Nmap done at' in file_from_zip('%s.zip' % test_a['id'],
                                             'output-2.gnmap').decode('utf-8')
Beispiel #11
0
def test_commandline_assignment(tmpworkdir, test_dummy_a):  # pylint: disable=unused-argument
    """test custom assignment passed from command line"""

    result = agent_main(['--assignment', json.dumps(test_dummy_a)])
    assert result == 0
    assert os.path.exists('%s.zip' % test_dummy_a['id'])
Beispiel #12
0
def test_version(tmpworkdir):  # pylint: disable=unused-argument
    """test print version"""

    result = agent_main(['--version'])
    assert result == 0
Beispiel #13
0
def test_invalid_server_oneshot(tmpworkdir):  # pylint: disable=unused-argument,redefined-outer-name
    """test to raise exception in oneshot"""

    result = agent_main(
        ['--server', 'http://localhost:0', '--debug', '--oneshot'])
    assert result == 1