Exemplo n.º 1
0
def test_terminate_with_assignment(tmpworkdir, cleanup_markedprocess, 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(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()
Exemplo n.º 2
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()
Exemplo n.º 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
Exemplo n.º 4
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()
Exemplo n.º 5
0
def test_exception_in_module(tmpworkdir):  # pylint: disable=unused-argument
    """test exception handling during agent module execution"""

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

    result = agent_main(['--assignment', json.dumps(test_a)])
    assert result == 1
    assert Path(f'{test_a["id"]}.zip').exists()
Exemplo n.º 6
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """dix_dns_discover test"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'six_dns_discover',
            'delay': 1
        },
        'targets': ['127.0.0.1', '0.0.0.0']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert '::1' in json.loads(file_from_zip(f'{test_a["id"]}.zip', 'output.json').decode('utf-8'))
Exemplo n.º 7
0
def test_terminate_with_liveserver(tmpworkdir, live_server, apikey, cleanup_markedprocess, 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', url_for('index_route', _external=True),
            '--apikey', apikey,
            '--queue', Queue.query.get(longrun_target.queue_id).name,
            '--oneshot',
            '--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()
Exemplo n.º 8
0
def test_commandline_assignment(tmpworkdir):  # pylint: disable=unused-argument
    """test custom assignment passed from command line"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'dummy',
            'args': '--arg1'
        },
        'targets': []
    }

    result = agent_main(['--assignment', json.dumps(test_a)])
    assert result == 0
    assert Path(f'{test_a["id"]}.zip').exists()
Exemplo n.º 9
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """dummy module execution test"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'dummy',
            'args': '--static_assignment'
        },
        'targets': ['target1']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert test_a['targets'][0] in file_from_zip(
        f'{test_a["id"]}.zip', 'assignment.json').decode('utf-8')
Exemplo n.º 10
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """jarm module execution test"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'jarm',
            'delay': 0
        },
        'targets': ['tcp://127.0.0.1:1', 'udp://127.0.0.1:1']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert \
        'JARM: 00000000000000000000000000000000000000000000000000000000000000' \
        in file_from_zip(f'{test_a["id"]}.zip', 'output-0.out').decode('utf-8')
Exemplo n.º 11
0
def test_run_with_liveserver(tmpworkdir, live_server, apikey, 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,
        '--queue',
        Queue.query.get(dummy_target.queue_id).name,
        '--oneshot',
        '--debug',
    ])
    assert result == 0

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

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'nmap',
            'args': '-sL',
            'timing_perhost': 1
        },
        'targets': ['127.0.0.1', '::1', '[ip6-localhost]']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])
    assert result == 0
    assert 'Host: 127.0.0.1 (localhost)' in file_from_zip(
        f'{test_a["id"]}.zip', 'output.gnmap').decode('utf-8')
    assert 'Host: ::1 (localhost)' in file_from_zip(
        f'{test_a["id"]}.zip', 'output6.gnmap').decode('utf-8')
Exemplo n.º 13
0
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """manymap module execution test"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'manymap',
            'args': '-sV',
            'delay': 1
        },
        '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')
def test_basic(tmpworkdir):  # pylint: disable=unused-argument
    """six_enum_discover test"""

    test_a = {
        'id': str(uuid4()),
        'config': {
            'module': 'six_enum_discover',
            'rate': 100
        },
        'targets': ['::1']
    }

    result = agent_main(['--assignment', json.dumps(test_a), '--debug'])

    # travis does not support ipv6 on bionic
    if 'TRAVIS' in os.environ:
        assert result == 1
        return

    assert result == 0
    assert '::1' in file_from_zip(f'{test_a["id"]}.zip',
                                  'output-0.txt').decode('utf-8')
Exemplo n.º 15
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
Exemplo n.º 16
0
def test_commandline_assignment(tmpworkdir, dummy_a):  # pylint: disable=unused-argument
    """test custom assignment passed from command line"""

    result = agent_main(['--assignment', json.dumps(dummy_a)])
    assert result == 0
    assert Path(f'{dummy_a["id"]}.zip').exists()
Exemplo n.º 17
0
def test_version(tmpworkdir):  # pylint: disable=unused-argument
    """test print version"""

    result = agent_main(['--version'])
    assert result == 0