Skip to content

Commit

Permalink
Merge pull request #1136 from blacklanternsecurity/fix-logging-error
Browse files Browse the repository at this point in the history
Remove aioconsole dependency
  • Loading branch information
TheTechromancer authored Feb 28, 2024
2 parents 99cdc44 + 14a24cb commit 522a5c1
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 266 deletions.
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

0 comments on commit 522a5c1

Please sign in to comment.