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

Added Ability to Kill Modules During Scan #751

Merged
merged 1 commit into from
Sep 22, 2023
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
14 changes: 13 additions & 1 deletion bbot/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os
import re
import sys
import asyncio
import logging
Expand Down Expand Up @@ -307,14 +308,25 @@ async def _main():

def keyboard_listen():
allowed_errors = 10
kill_regex = re.compile(r"kill (?P<module>[a-z0-9_]+)")
while 1:
keyboard_input = "a"
try:
keyboard_input = input()
allowed_errors = 10
except Exception:
allowed_errors -= 1
if not keyboard_input:
if keyboard_input:
log.verbose(f'Got keyboard input: "{keyboard_input}"')
kill_match = kill_regex.match(keyboard_input)
if kill_match:
module = kill_match.group("module")
if module in scanner.modules:
log.hugewarning(f'Killing module: "{module}"')
scanner.manager.kill_module(module, message="killed by user")
else:
log.warning(f'Invalid module: "{module}"')
else:
toggle_log_level(logger=log)
scanner.manager.modules_status(_log=True)
if allowed_errors <= 0:
Expand Down
9 changes: 7 additions & 2 deletions bbot/modules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ async def _setup(self):
async def _worker(self):
async with self.scan.acatch(context=self._worker):
try:
while not self.scan.stopping:
while not self.scan.stopping and not self.errored:
# hold the reigns if our outgoing queue is full
if self._qsize > 0 and self.outgoing_event_queue.qsize() >= self._qsize:
await asyncio.sleep(0.1)
Expand Down Expand Up @@ -505,7 +505,7 @@ def queue_outgoing_event(self, event, **kwargs):
except AttributeError:
self.debug(f"Not in an acceptable state to queue outgoing event")

def set_error_state(self, message=None):
def set_error_state(self, message=None, clear_outgoing_queue=False):
if not self.errored:
log_msg = f"Setting error state for module {self.name}"
if message is not None:
Expand All @@ -522,6 +522,11 @@ def set_error_state(self, message=None):
# if there are leftover objects in the queue, the scan will hang.
self._incoming_event_queue = False

if clear_outgoing_queue:
with suppress(asyncio.queues.QueueEmpty):
while 1:
self.outgoing_event_queue.get_nowait()

# override in the module to define different values to comprise the hash
def get_per_host_hash(self, event):
parsed = getattr(event, "parsed", None)
Expand Down
4 changes: 4 additions & 0 deletions bbot/scanner/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ async def _worker_loop(self):
except Exception:
log.critical(traceback.format_exc())

def kill_module(self, module_name, message=None):
module = self.scan.modules[module_name]
module.set_error_state(message=message, clear_outgoing_queue=True)

@property
def modules_by_priority(self):
if not self._modules_by_priority:
Expand Down