Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove aioconsole dependency #1136

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions bbot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import traceback
from omegaconf import OmegaConf
from contextlib import suppress
from aioconsole import stream

# from aioconsole import stream

# fix tee buffering
sys.stdout.reconfigure(line_buffering=True)
Expand All @@ -20,12 +21,10 @@
from bbot import __version__
from bbot.modules import module_loader
from bbot.core.configurator.args import parser
from bbot.core.helpers.misc import smart_decode
from bbot.core.helpers.logger import log_to_stderr
from bbot.core.configurator import ensure_config_files, check_cli_args, environ

log = logging.getLogger("bbot.cli")
sys.stdout.reconfigure(line_buffering=True)


log_level = get_log_level()
Expand Down Expand Up @@ -303,12 +302,14 @@ async def _main():
if not options.dry_run:
log.trace(f"Command: {' '.join(sys.argv)}")

# if we're on the terminal, enable keyboard interaction
if sys.stdin.isatty():
if not options.agent_mode and not options.yes:
log.hugesuccess(f"Scan ready. Press enter to execute {scanner.name}")
input()

def handle_keyboard_input(keyboard_input):
"""Enable toggling log level, killing individual bbot modules during scan"""
kill_regex = re.compile(r"kill (?P<module>[a-z0-9_]+)")
if keyboard_input:
log.verbose(f'Got keyboard input: "{keyboard_input}"')
Expand All @@ -324,30 +325,23 @@ def handle_keyboard_input(keyboard_input):
toggle_log_level(logger=log)
scanner.manager.modules_status(_log=True)

# Reader
reader = stream.StandardStreamReader()
protocol = stream.StandardStreamReaderProtocol(reader)
await asyncio.get_event_loop().connect_read_pipe(lambda: protocol, sys.stdin)
def stdin_reader(queue):
"""Reads from stdin and puts lines into a queue."""
for line in sys.stdin:
queue.put_nowait(line)

from threading import Thread

input_queue = asyncio.Queue()

# Start the stdin reader thread
reader_thread = Thread(target=stdin_reader, args=(input_queue,), daemon=True)
reader_thread.start()

async def akeyboard_listen():
try:
allowed_errors = 10
while 1:
keyboard_input = None
try:
keyboard_input = smart_decode((await reader.readline()).strip())
allowed_errors = 10
except Exception as e:
log_to_stderr(f"Error in keyboard listen loop: {e}", level="TRACE")
log_to_stderr(traceback.format_exc(), level="TRACE")
allowed_errors -= 1
if keyboard_input is not None:
handle_keyboard_input(keyboard_input)
if allowed_errors <= 0:
break
except Exception as e:
log_to_stderr(f"Error in keyboard listen task: {e}", level="ERROR")
log_to_stderr(traceback.format_exc(), level="TRACE")
while True:
line = (await input_queue.get()).strip()
handle_keyboard_input(line)

asyncio.create_task(akeyboard_listen())

Expand Down
14 changes: 5 additions & 9 deletions bbot/core/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
split_host_port,
tagify,
validators,
truncate_string,
)


Expand Down Expand Up @@ -490,7 +489,10 @@ def data_human(self):
return self._data_human()

def _data_human(self):
return truncate_string(str(self.data), n=2000)
if isinstance(self.data, (dict, list)):
with suppress(Exception):
return json.dumps(self.data, sort_keys=True)
return smart_decode(self.data)

def _data_load(self, data):
"""
Expand Down Expand Up @@ -524,10 +526,7 @@ def pretty_string(self):
return self._pretty_string()

def _pretty_string(self):
if isinstance(self.data, dict):
with suppress(Exception):
return json.dumps(self.data, sort_keys=True)
return smart_decode(self.data)
return self._data_human()

@property
def data_graph(self):
Expand Down Expand Up @@ -753,9 +752,6 @@ def sanitize_data(self, data):
self.parsed = validators.validate_url_parsed(url)
return data

def _data_human(self):
return json.dumps(self.data, sort_keys=True)

def _data_load(self, data):
if isinstance(data, str):
return json.loads(data)
Expand Down
9 changes: 7 additions & 2 deletions bbot/test/test_step_2/module_tests/test_module_dastardly.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def new_filter_event(event):
module_test.monkeypatch.setattr(module_test.module, "filter_event", new_filter_event)

def check(self, module_test, events):
assert 1 == len([e for e in events if e.type == "VULNERABILITY"])
assert 1 == len(
[e for e in events if e.type == "VULNERABILITY" and f"{self.new_url}test" in e.data["description"]]
[
e
for e in events
if e.type == "VULNERABILITY"
and f"{self.new_url}test" in e.data["description"]
and "Cross-site scripting".lower() in e.data["description"].lower()
]
)
Loading
Loading