# modules/features/pentest/bloodhound.nix — both BloodHound viewers, each with # a single command that brings up everything it needs. # # bloodhound ce postgres + neo4j + the CE API, then the URL to open # bloodhound legacy neo4j + the archived 4.3.1 GUI # bloodhound status what is up, and where # bloodhound down stop all of it # bloodhound creds the CE admin password (printed on first start) # # The two are NOT interchangeable, and this is the thing that wastes an hour: # # bloodhound-ce reads CE-format JSON. Collect with `rusthound-ce` or # SharpHound CE ($PAYLOADS/windows/amd64/ad). # bloodhound-legacy reads the OLD format, which is what # `bloodhound-python` 1.9 and SharpHound v1 produce. CE # refuses it, which is the only reason this is still here. # nixpkgs dropped it (archived upstream, Electron 11) — # lab data only. # # Nothing starts at boot: neo4j is a JVM that wants a gigabyte, and the API # follows it. `bloodhound ce` is how you pay for it, when you want it. # # Every credential is generated on first start and kept in the state # directory (0750 bloodhound, files 0600): the neo4j password, the postgres # role password, the JWT key and the CE admin password. Loopback-only is not # enough on its own: any local process, or a web page doing DNS rebinding # against 127.0.0.1:7474, could otherwise read or rewrite a client's AD graph. # `bloodhound creds` prints the ones you type. { lib, self, ... }: { imports = [ ((import ./_sets.nix { inherit lib; }) { name = "bloodhound"; description = "BloodHound CE and Legacy, with neo4j and postgresql"; packages = pkgs: let extra = import ./_pkgs/default.nix { inherit lib pkgs; }; in [ pkgs.bloodhound-ce # the CE API + web graph viewer pkgs.bloodhound-py # legacy-format collector pkgs.rusthound-ce # CE-format collector # Neo4j 4.4, NOT nixpkgs' 2026.x: BloodHound CE's migration calls # `db.indexes`, removed in Neo4j 5. See _pkgs/default.nix. extra.neo4j44 # `neo4j`, `cypher-shell`, `neo4j-admin` extra.bloodhoundLegacy ]; expectedBins = [ "bloodhound-ce" "bloodhound-legacy" "cypher-shell" "neo4j" "bloodhound-python" "rusthound-ce" ]; extraConfig = { pkgs, lib, config, ... }: let stateDir = "/var/lib/bloodhound"; configFile = "${stateDir}/bhapi.json"; port = 8080; systemctl = "/run/current-system/sw/bin/systemctl"; sudo = "/run/wrappers/bin/sudo"; dbUnits = "neo4j.service postgresql.service"; extra = import ./_pkgs/default.nix { inherit lib pkgs; }; neo4j44 = extra.neo4j44; # Neo4j's scripts expect a writable NEO4J_HOME. Build one per boot: # lib/ and bin/ come from the store, everything it writes is real. neo4jSetup = pkgs.writeShellScript "neo4j-setup" '' set -euo pipefail PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.openssl ]}:$PATH H=${stateDir}/neo4j mkdir -p $H/{conf,data,logs,run,import,plugins,certificates} ln -sfn ${neo4j44}/share/neo4j/lib $H/lib ln -sfn ${neo4j44}/share/neo4j/bin $H/bin # A generated password, set through neo4j-admin before the server # starts. On a graph that already exists, neo4j 4.4 applies it # while the built-in user still has the default password. [ -s ${stateDir}/neo4j.pw ] || { (umask 077; openssl rand -hex 24 > ${stateDir}/neo4j.pw); } if [ ! -e $H/.initial-password-set ]; then ${neo4j44}/bin/neo4j-admin set-initial-password "$(cat ${stateDir}/neo4j.pw)" >/dev/null touch $H/.initial-password-set fi cat > $H/conf/neo4j.conf <<'EOF' # One lab graph on loopback, password-protected (see the header). dbms.security.auth_enabled=true dbms.default_listen_address=127.0.0.1 dbms.connector.bolt.enabled=true dbms.connector.bolt.listen_address=127.0.0.1:7687 dbms.connector.http.enabled=true dbms.connector.http.listen_address=127.0.0.1:7474 dbms.connector.https.enabled=false dbms.memory.heap.initial_size=512m dbms.memory.heap.max_size=1G dbms.memory.pagecache.size=512m dbms.jvm.additional=-XX:+UseG1GC EOF ${pkgs.gnused}/bin/sed -i 's/^ //' $H/conf/neo4j.conf ''; # Root, before the API starts: give the postgres role a generated # password (ensureUsers sets none) so pg_hba can require it instead # of trusting every local process. SQL goes over stdin, never argv. pgPassword = pkgs.writeShellScript "bloodhound-pg-password" '' set -euo pipefail PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.openssl pkgs.util-linux ]} f=${stateDir}/postgres.pw if [ ! -s "$f" ]; then (umask 077; openssl rand -hex 24 > "$f") chown bloodhound:bloodhound "$f" fi printf "ALTER ROLE bloodhound WITH PASSWORD '%s';\n" "$(cat "$f")" \ | runuser -u postgres -- ${config.services.postgresql.package}/bin/psql -X -q -v ON_ERROR_STOP=1 -d postgres ''; # The config is written at START, not by Nix: it carries a generated # JWT signing key, and a secret in the Nix store is world-readable. mkConfig = pkgs.writeShellScript "bloodhound-ce-config" '' set -euo pipefail PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.openssl pkgs.jq ]}:$PATH mkdir -p ${stateDir}/work [ -s ${stateDir}/jwt.key ] || { openssl rand -base64 48 > ${stateDir}/jwt.key; chmod 600 ${stateDir}/jwt.key; } [ -s ${stateDir}/admin.pw ] || { openssl rand -base64 18 > ${stateDir}/admin.pw; chmod 600 ${stateDir}/admin.pw; } # --rawfile, not --arg "$(cat …)": argv is readable by every user in ps. jq -n \ --rawfile jwt ${stateDir}/jwt.key \ --rawfile pw ${stateDir}/admin.pw \ --rawfile neo ${stateDir}/neo4j.pw \ --rawfile pg ${stateDir}/postgres.pw \ '{ version: 1, bind_addr: "127.0.0.1:${toString port}", root_url: "http://127.0.0.1:${toString port}/", metrics_port: "127.0.0.1:2112", work_dir: "${stateDir}/work", log_level: "INFO", graph_driver: "neo4j", collectors_base_path: "${pkgs.bloodhound-ce.collectors}", database: { addr: "127.0.0.1:5432", database: "bloodhound", username: "bloodhound", secret: ($pg | rtrimstr("\n")) }, neo4j: { addr: "127.0.0.1:7687", database: "neo4j", username: "neo4j", secret: ($neo | rtrimstr("\n")) }, crypto: { jwt: { signing_key: ($jwt | rtrimstr("\n")) }, argon2: { memory_kibibytes: 1048576, num_iterations: 1, num_threads: 4 } }, default_admin: { principal_name: "admin", password: ($pw | rtrimstr("\n")), email_address: "admin@bloodhound.lab", first_name: "Admin", last_name: "User", expire_now: false }, enable_startup_wait_period: false, enable_api_logging: true, enable_cypher_mutations: true, disable_cypher_complexity_limit: true }' > ${configFile} chmod 600 ${configFile} ''; bloodhound = pkgs.writeShellScriptBin "bloodhound" '' set -uo pipefail PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.curl pkgs.systemd pkgs.gnugrep ]}:$PATH wait_for() { # wait_for