Coder Social home page Coder Social logo

threat9-test-bed's Introduction

threat9-test-bed

Installation

$ pip install git+https://github.com/threat9/threat9-test-bed.git

Test utilities

HttpServiceMock

HttpServiceMock is a flask application that allows for adding unittests.mock as view functions. This gives us ability to setup dummy http services on demand for testing purposes.

from threat9_test_bed.service_mocks import HttpServiceMock

from foo import ExploitUnderTest


def test_exploit():
    with HttpServiceMock("localhost", 8080) as target: 
        cgi_mock = target.get_route_mock("/cgi-bin/cgiSrv.cgi",
                                         methods=["POST"])
        cgi_mock.return_value = 'foo status="doing" bar'
        check_mock = target.get_route_mock("/routersploit.check",
                                           methods=["GET", "POST"])
        check_mock.return_value = 'root'
    
        exploit = ExploitUnderTest(f'http://{target.host}', target.port)
        assert exploit.check() is True
        cgi_mock.assert_called_once()
        assert check_mock.call_count == 2

It is very convenient to use py.test library and it's fixture abilities. Such fixture will perform setup and teardown automatically before each test. All we have to do is to pass target as the test argument.

import pytest
from threat9_test_bed.service_mocks import HttpServiceMock

from foo import ExploitUnderTest


@pytest.fixture
def target():
    with HttpServiceMock("localhost", 8080) as target_:
        yield target_


def test_exploit(target):
    cgi_mock = target.get_route_mock("/cgi-bin/cgiSrv.cgi",
                                     methods=["POST"])
    cgi_mock.return_value = 'foo status="doing" bar'
    check_mock = target.get_route_mock("/routersploit.check",
                                       methods=["GET", "POST"])
    check_mock.return_value = 'root'

    exploit = ExploitUnderTest(f'http://{target.host}', target.port)
    assert exploit.check() is True
    cgi_mock.assert_called_once()
    assert check_mock.call_count == 2

Adhoc SSL support

You can serve HttpScenarioService using adhoc SSL certificate by setting ssl keyword argument to True:

from threat9_test_bed.service_mocks import HttpServiceMock

@pytest.fixture
def trash_target():
    with HttpServiceMock("127.0.0.1", 0, ssl=True) as http_service:
        yield http_service

HttpScenarioService

HttpScenarioService allows for creating test utilities using pre-defined scenarios

import pytest

from threat9_test_bed.scenarios import HttpScenario
from threat9_test_bed.service_mocks import HttpScenarioService


@pytest.fixture(scope="session")
def empty_target():
    with HttpScenarioService("127.0.0.1", 8081,
                             HttpScenario.EMPTY_RESPONSE) as http_service:
        yield http_service


@pytest.fixture(scope="session")
def trash_target():
    with HttpScenarioService("127.0.0.1", 8082,
                             HttpScenario.TRASH) as http_service:
        yield http_service

Adhoc SSL support

You can serve HttpScenarioService using adhoc SSL certificate by setting ssl keyword argument to True:

from threat9_test_bed.service_mocks import HttpScenarioService

@pytest.fixture(scope="session")
def trash_target():
    with HttpScenarioService("127.0.0.1", 8443, HttpScenario.TRASH, 
                             ssl=True) as http_service:
        yield http_service

TelnetServiceMock

TelnetServiceMock allows for creating test utilities using pre-defined scenarios as well as attaching unittests.mock as command handlers. This gives us ability to setup dummy telnet service on demand for testing purposes.

from telnetlib import Telnet

import pytest

from threat9_test_bed.service_mocks.telnet_service_mock import TelnetServiceMock
from threat9_test_bed.scenarios import TelnetScenarios


@pytest.fixture
def generic_target():
    with TelnetServiceMock("127.0.0.1", 8023,
                           TelnetScenarios.AUTHORIZED) as telnet_service:
        yield telnet_service


def test_telnet(generic_target):
    command_mock = target.get_command_mock("scoobeedoobeedoo")
    command_mock.return_value = "Where are you?"

    tn = Telnet(target.host, target.port, timeout=5)
    tn.expect([b"Login: ", b"login: "], 5)
    tn.write(b"admin" + b"\r\n")

    tn.expect([b"Password: ", b"password"], 5)
    tn.write(b"admin" + b"\r\n")

    tn.expect([b"admin@target:~$"], 5)
    tn.write(b"scoobeedoobeedoo" + b"\r\n")
    _, match_object, _ = tn.expect([b"Where are you?"], 5)

    tn.close()

    assert match_object

Random port

To avoid port collison during tests you can tell test utilities to set it for you by passing 0

@pytest.fixture(scope="session")
def trash_target():
    with HttpScenarioService("127.0.0.1", 0,
                             HttpScenario.TRASH) as http_service:
        yield http_service

Services

http

$ test-bed http

http scenarios

Scenario Behavior
EMPTY_RESPONSE returns empty response with 200 status code
TRASH returns 100 characters long gibberish with 200 status code
NOT_FOUND returns 404 status code
FOUND returns OK with 200 status code
REDIRECT redirects you with 302 status code
TIMEOUT sleep the server for 1 hour which effectively times out the request
ERROR returns 500 status code
$ test-bed http --scenario TRASH

https

$ test-bed https

https scenarios

Scenario Behavior
EMPTY_RESPONSE returns empty response with 200 status code
TRASH returns 100 characters long gibberish with 200 status code
NOT_FOUND returns 404 status code
FOUND returns OK with 200 status code
REDIRECT redirects you with 302 status code
TIMEOUT sleep the server for 1 hour which effectively times out the request
ERROR returns 500 status code
$ test-bed https --scenario FOUND

telnet

After successful authorization elnet service responds with random Lorem ipsum... for every command

$ test-bed telnet

telnet scenarios

Scenario Behavior
AUTHORIZED Any authorization attempt ends with success
NOT_AUTHORIZED Every authorization attempt ends with failure
GENERIC Authorization using admin/admin credentials
TIMEOUT Server hangs as soon as client has been connected
$ test-bed telnet --scenario GENERIC

Troubleshooting

I can't start my https service on port 443 due to PermissionError

Running services on it's default port may need extra privileges thus prepending command with sudo should do the trick e.g.

$ sudo test-bed https --scenario TRASH --port 443
[2017-09-16 12:51:18,137: INFO/werkzeug]  * Running on https://127.0.0.1:443/ (Press CTRL+C to quit)

This solution can be applied to other services and it's default ports as well.

threat9-test-bed's People

Contributors

fwkz avatar lucyoa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

threat9-test-bed's Issues

Termux

Install Python3.6
command: |
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3.6-dev
sudo apt-get install python3.6-venv
python3.6 -m venv ~/venv

Python

pkg update && pkg upgradeCopy
pkg install gitCopy
pkg install python2Copy
pip2 install tqdmCopy
pip2 install requestsCopy
pip2 install mechanizeCopy
cd SenseiCopy
python2 main.py

License and description

Hello, could it be possible to have a description and a license on this project that routersploit depends on (and thus may be packaged by some maintainers)

Add support for banner.

Trying 192.168.1.1...
Connected to broadcom.home.
Escape character is '^]'.
BCM96318 Broadband Router
Login: ^C

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.