Skip to content

armgit / bbot

Public Imported from GitHub
0

The recursive internet scanner for hackers. 🧡

Include git commit hash in version output for git checkouts (#3530)

Paul Mueller055319d8b836
diff --git a/bbot/__init__.py b/bbot/__init__.py
index 1a7242313..16844023a 100644
--- a/bbot/__init__.py
+++ b/bbot/__init__.py
@@ -1,6 +1,31 @@
+import subprocess
+from pathlib import Path
from importlib.metadata import version, PackageNotFoundError
try:
__version__ = version("bbot")
except PackageNotFoundError:
__version__ = "0.0.0"
+
+
+def _git_commit():
+ # only git checkouts have .git next to the package (a file, not a dir, in worktrees)
+ repo_root = Path(__file__).resolve().parent.parent
+ if not (repo_root / ".git").exists():
+ return None
+ try:
+ result = subprocess.run(
+ ["git", "-C", str(repo_root), "describe", "--always", "--dirty", "--abbrev=9", "--exclude=*"],
+ capture_output=True,
+ text=True,
+ timeout=2,
+ )
+ except (OSError, subprocess.SubprocessError):
+ return None
+ if result.returncode != 0:
+ return None
+ return result.stdout.strip() or None
+
+
+_commit = _git_commit()
+__full_version__ = f"{__version__} (git:{_commit})" if _commit else __version__
diff --git a/bbot/cli.py b/bbot/cli.py
index 72a457d2b..b437c88c7 100755
--- a/bbot/cli.py
+++ b/bbot/cli.py
@@ -6,7 +6,7 @@ import sys
import logging
import multiprocessing
from bbot.errors import *
-from bbot import __version__
+from bbot import __full_version__
from bbot.logger import log_to_stderr
from bbot.core.helpers.misc import chain_lists
@@ -27,7 +27,7 @@ if multiprocessing.current_process().name == "MainProcess":
{o}| ___ <{e}| __ <| | | | | |
{o}| |___) {e}| |__) | |__| | | |
{o}|______/{e}|_____/ \____/ |_|
- {o}BIGHUGE{e} BLS OSINT TOOL {__version__}
+ {o}BIGHUGE{e} BLS OSINT TOOL {__full_version__}
www.blacklanternsecurity.com/bbot
"""
@@ -83,7 +83,7 @@ async def _main():
# --version
if options.version:
- print(__version__)
+ print(__full_version__)
sys.exit(0)
return
diff --git a/bbot/scanner/scanner.py b/bbot/scanner/scanner.py
index f5b8455b2..2b0f1f3cc 100644
--- a/bbot/scanner/scanner.py
+++ b/bbot/scanner/scanner.py
@@ -11,7 +11,7 @@ from datetime import datetime
from zoneinfo import ZoneInfo
from collections import OrderedDict
-from bbot import __version__
+from bbot import __full_version__
from bbot.core.event import make_event, update_event
from .manager import ScanIngress, ScanEgress
from bbot.core.helpers.misc import sha1, rand_string
@@ -450,7 +450,8 @@ class Scanner:
self.root_event.data["started_at"] = self.start_time.timestamp()
self._start_log_handlers()
- self.trace(f"Ran BBOT {__version__} at {self.start_time}, command: {' '.join(sys.argv)}")
+ self.info(f"BBOT version: {__full_version__}")
+ self.trace(f"Ran BBOT {__full_version__} at {self.start_time}, command: {' '.join(sys.argv)}")
self.trace(f"Target: {self.preset.target.json}")
self.trace(f"Preset: {self.preset.to_dict(redact_secrets=True)}")
diff --git a/bbot/test/test_step_1/test_cli.py b/bbot/test/test_step_1/test_cli.py
index 2db0037d1..9f528b9ef 100644
--- a/bbot/test/test_step_1/test_cli.py
+++ b/bbot/test/test_step_1/test_cli.py
@@ -1,8 +1,10 @@
import stat
+import subprocess
import yaml
from ..bbot_fixtures import *
+import bbot
from bbot import cli
@@ -134,6 +136,45 @@ async def test_cli_scan(monkeypatch):
assert "[INFO]" in open(scan_log).read()
+def test_full_version(monkeypatch, tmp_path):
+ # the real value depends on whether bbot is running from a git checkout
+ if bbot._commit:
+ assert bbot.__full_version__ == f"{bbot.__version__} (git:{bbot._commit})"
+ else:
+ assert bbot.__full_version__ == bbot.__version__
+
+ # fake git checkout
+ (tmp_path / ".git").mkdir()
+ monkeypatch.setattr(bbot, "__file__", str(tmp_path / "bbot" / "__init__.py"))
+
+ def fake_git(stdout, returncode=0):
+ def run(*args, **kwargs):
+ return subprocess.CompletedProcess(args[0], returncode, stdout=stdout, stderr="")
+
+ return run
+
+ monkeypatch.setattr(bbot.subprocess, "run", fake_git("012345678\n"))
+ assert bbot._git_commit() == "012345678"
+ monkeypatch.setattr(bbot.subprocess, "run", fake_git("012345678-dirty\n"))
+ assert bbot._git_commit() == "012345678-dirty"
+
+ # git fails
+ monkeypatch.setattr(bbot.subprocess, "run", fake_git("", returncode=128))
+ assert bbot._git_commit() is None
+
+ # no git binary
+ def no_git(*args, **kwargs):
+ raise FileNotFoundError("git")
+
+ monkeypatch.setattr(bbot.subprocess, "run", no_git)
+ assert bbot._git_commit() is None
+
+ # not a git checkout
+ (tmp_path / ".git").rmdir()
+ monkeypatch.setattr(bbot.subprocess, "run", fake_git("012345678\n"))
+ assert bbot._git_commit() is None
+
+
@pytest.mark.asyncio
async def test_cli_args(monkeypatch, caplog, capsys, clean_default_config):
caplog.set_level(logging.INFO)
@@ -148,6 +189,7 @@ async def test_cli_args(monkeypatch, caplog, capsys, clean_default_config):
assert result is None
assert len(out.splitlines()) == 1
assert out.count(".") > 1
+ assert out.strip() == bbot.__full_version__
# deps behavior
monkeypatch.setattr("sys.argv", ["bbot", "-n", "depstest", "--retry-deps", "--current-preset"])
diff --git a/bbot/test/test_step_1/test_scan.py b/bbot/test/test_step_1/test_scan.py
index 67167afb3..cb4dffeb6 100644
--- a/bbot/test/test_step_1/test_scan.py
+++ b/bbot/test/test_step_1/test_scan.py
@@ -426,6 +426,22 @@ async def test_scan_event_started_at_type(bbot_scanner):
)
+async def test_scan_log_version(bbot_scanner):
+ import bbot
+
+ scan = bbot_scanner("127.0.0.1")
+ # run at the default log level, since the test suite otherwise logs at DEBUG
+ old_log_level = scan.core.logger.log_level
+ scan.core.logger.log_level = logging.INFO
+ try:
+ await scan.async_start_without_generator()
+ finally:
+ scan.core.logger.log_level = old_log_level
+ scan_log = open(scan.home / "scan.log").read()
+ assert f"BBOT version: {bbot.__full_version__}" in scan_log
+
+
async def test_scan_name(bbot_scanner):
scan = bbot_scanner("evilcorp.com", name="test_scan_name")
await scan._prep()