repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
paths.nixNix189 lines10.7 KiBraw download
# modules/features/pentest/paths.nix — ~/pentesting, the variables that point
# into it, and `htbpaths` to find your way around.
#
#   ~/pentesting/            the arsenal, one directory per job
#     privesc/ creds/ ad/ pivoting/ recon/ shells/ webshells/ scripts/
#     exploits/ sharpcollection/ wordlists/     -> read-only, from the Nix store
#     local/                                    -> yours, writable, never touched
#
#   htbpaths                 every variable, where it points, how full it is
#   htbpaths potatoes        just the path:  cd "$(htbpaths potatoes)"
#   htbpaths find godpotato  where a file is, and the URL payload-serve gives it
#   htbpaths tree [name]     a two-level tree
#   htbpaths env             `export` lines, for a shell started before login
#
# The variables are lowercase because that is how they are typed all day
# ($privesc, $potatoes, $ligolo). None of them is a name zsh or bash treats
# specially ($path, $status, $prompt… are avoided on purpose). $PAYLOADS and
# $WORDLISTS stay as uppercase aliases for scripts that already use them.
#
# Why symlinks into the store and not a download script: every file is pinned
# by hash in _pkgs/ and payloads.nix, so ~/pentesting rolls back with the
# system generation and is identical on a reinstall. The category links are
# re-pointed by systemd-tmpfiles on every switch; `local/` is a real directory
# and is only ever created, never cleaned.
#
# ONE table below feeds the variables, the tmpfiles links, htbpaths and its
# completion, so a new category is one line.
{ lib, ... }:
let
  # name -> { rel = path under ~/pentesting; desc = what it is for }
  # Order is the order `htbpaths` prints in.
  table = [
    { name = "pentesting";      rel = "";                                desc = "the arsenal root"; }
    { name = "wordlists";       rel = "wordlists";                       desc = "seclists, rockyou, usernames"; }
    { name = "seclists";        rel = "wordlists/seclists";              desc = "SecLists"; }
    { name = "rockyou";         rel = "wordlists/rockyou.txt";           desc = "rockyou.txt (plain text)"; }
    { name = "usernames";       rel = "wordlists/seclists/Usernames";    desc = "SecLists username lists"; }
    { name = "privesc";         rel = "privesc";                         desc = "privilege escalation, per OS"; }
    { name = "winprivesc";      rel = "privesc/windows";                 desc = "winPEAS, PrivescCheck, FullPowers, PowerUp"; }
    { name = "potatoes";        rel = "privesc/windows/potatoes";        desc = "God/Sigma/Juicy/Rogue/PrintSpoofer… (SeImpersonate)"; }
    { name = "linprivesc";      rel = "privesc/linux";                   desc = "linpeas, lse, pspy, traitor…"; }
    { name = "creds";           rel = "creds";                           desc = "credential dumping"; }
    { name = "mimikatz";        rel = "creds/mimikatz";                  desc = "mimikatz, every build and arch"; }
    { name = "ad";              rel = "ad";                              desc = "SharpHound CE, Rubeus, Certify, Certipy, bloodyAD…"; }
    { name = "sharpcollection"; rel = "sharpcollection";                 desc = "the full SharpCollection, every framework"; }
    { name = "sharp";           rel = "sharpcollection/NetFramework_4.7_x64"; desc = "SharpCollection, .NET 4.7 x64"; }
    { name = "pivoting";        rel = "pivoting";                        desc = "tunnels and port forwards, every OS/arch"; }
    { name = "ligolo";          rel = "pivoting/ligolo-ng";              desc = "ligolo-ng agent + proxy, every OS/arch"; }
    { name = "chisel";          rel = "pivoting/chisel";                 desc = "chisel, every OS/arch"; }
    { name = "recon";           rel = "recon";                           desc = "scanners to run FROM a foothold"; }
    { name = "nmapbin";         rel = "recon/nmap";                      desc = "static nmap (linux) + portable nmap (windows)"; }
    { name = "fscan";           rel = "recon/fscan";                     desc = "fscan, every OS/arch"; }
    { name = "shells";          rel = "shells";                          desc = "nc, RunasCs, nishang, reverse shells"; }
    { name = "webshells";       rel = "webshells";                       desc = "php/asp/aspx/jsp webshells"; }
    { name = "web";             rel = "web";                             desc = "web exploitation binaries (ysoserial.net)"; }
    { name = "ysoserial";       rel = "web/ysoserial.net/Release";       desc = "ysoserial.net — ViewState/.NET gadgets (needs wine)"; }
    { name = "scripts";         rel = "scripts";                         desc = "PowerView, PowerSploit, printer bug, nishang"; }
    { name = "exploits";        rel = "exploits";                        desc = "public exploits staged for targets"; }
    { name = "mytools";         rel = "local";                           desc = "YOUR tools — writable, never touched by Nix"; }
  ];

  # The store-backed categories linked into ~/pentesting (wordlists is linked
  # from its own tree; local is a real directory).
  linked = [ "privesc" "creds" "ad" "sharpcollection" "pivoting" "recon"
             "shells" "webshells" "web" "scripts" "exploits" "INVENTORY.txt" ];
in
{
  flake.nixosModules.pentest-paths =
    { config, pkgs, lib, user, ... }:
    let
      cfg = config.daemon.pentest;
      on = cfg.enable && cfg.payloads.enable;
      home = config.users.users.${user}.home;
      group = config.users.users.${user}.group;
      root = "${home}/pentesting";
      abs = rel: if rel == "" then root else "${root}/${rel}";

      # name|path|desc, one per line: the data htbpaths reads.
      tsv = pkgs.writeText "htbpaths.tsv"
        (lib.concatMapStrings (e: "${e.name}\t${abs e.rel}\t${e.desc}\n") table);

      htbpaths = pkgs.writeShellScriptBin "htbpaths" ''
        set -uo pipefail
        PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gawk pkgs.gnugrep pkgs.tree ]}:$PATH
        TABLE=${tsv}
        ROOT=${lib.escapeShellArg root}

        if [ -t 1 ] && [ -z "''${NO_COLOR:-}" ]; then
          B=$'\e[1m' D=$'\e[2m' M=$'\e[1;35m' R=$'\e[31m' Z=$'\e[0m'
        else B="" D="" M="" R="" Z=""; fi

        usage() {
          printf '%s\n' \
            'usage: htbpaths                 every variable and where it points' \
            '       htbpaths <name>          print one path   (cd "$(htbpaths potatoes)")' \
            '       htbpaths find <pattern>  locate a tool, with its payload-serve URL' \
            '       htbpaths tree [name]     two-level tree' \
            '       htbpaths env             export lines (eval "$(htbpaths env)")' \
            '       htbpaths names           just the names (completion)'
        }

        lookup() { awk -F'\t' -v n="$1" '$1==n {print $2; f=1} END {exit !f}' "$TABLE"; }

        # The whole output is built first and written once, then exit 0: a
        # reader like `htbpaths | grep -q x` closes the pipe early, and a
        # write after that would turn EPIPE into this script's exit status.
        case "''${1:-}" in
          "")
            out=$(awk -F'\t' -v B="$B" -v D="$D" -v M="$M" -v R="$R" -v Z="$Z" '
              { n=$1; p=$2; d=$3
                cmd = "test -e \"" p "\""; ok = (system(cmd) == 0)
                cnt = ""
                if (ok) { c = "find -L \"" p "\" -maxdepth 1 -mindepth 1 2>/dev/null | wc -l"; c | getline cnt; close(c); cnt = cnt+0 }
                printf "%s%-17s%s %-52s %s%s%s\n", M, "$" n, Z, p (ok ? "" : R " (missing)" Z), D, d (cnt != "" && cnt > 0 ? "  [" cnt "]" : ""), Z
              }' "$TABLE")
            printf '%s\n%s\n' "$out" "''${D}also: \$PAYLOADS / \$WORDLISTS (same files, store side)  ·  \$BOXDIR = current box  ·  htbpaths find <tool>''${Z}" || true
            exit 0 ;;
          names) cut -f1 "$TABLE" || true; exit 0 ;;
          env)
            awk -F'\t' '{printf "export %s=%s\n", $1, $2}' "$TABLE"
            printf 'export PAYLOADS=%s\nexport WORDLISTS=%s\n' "$ROOT" "$ROOT/wordlists"
            exit 0 ;;
          find|f)
            pat="''${2:-}"
            [ -n "$pat" ] || { echo "usage: htbpaths find <pattern>" >&2; exit 2; }
            # Wordlists are skipped unless asked for: seclists alone is tens of
            # thousands of files and would bury the tool you are looking for.
            res=$(find -L "$ROOT" \( -path "$ROOT/wordlists" -prune \) -o \
                    -iname "*$pat*" -print 2>/dev/null | sort)
            [ -n "$res" ] || { echo "htbpaths: nothing matching '$pat' (wordlists not searched — try: find -L \$wordlists -iname '*$pat*')" >&2; exit 1; }
            out=""
            while IFS= read -r f; do
              rel=''${f#"$ROOT"/}
              out="$out$f
  ''${D}payload-serve → http://\$LHOST:8000/$rel''${Z}
"
            done <<< "$res"
            printf '%s' "$out" || true
            exit 0 ;;
          tree|t)
            p=$ROOT
            if [ -n "''${2:-}" ]; then p=$(lookup "$2") || { echo "htbpaths: unknown name: $2" >&2; exit 2; }; fi
            tree -L 2 --noreport -l "$p" | head -200 || true
            exit 0 ;;
          -h|--help|help) usage; exit 0 ;;
          *)
            p=$(lookup "$1") || { echo "htbpaths: unknown name: $1 (htbpaths names)" >&2; exit 2; }
            echo "$p"; exit 0 ;;
        esac
      '';

      completion = pkgs.writeTextDir "share/zsh/site-functions/_htbpaths" ''
        #compdef htbpaths
        local -a names
        names=( ${lib.concatMapStringsSep " " (e: lib.escapeShellArg "${e.name}:${e.desc}") table} )
        if (( CURRENT == 2 )); then
          _describe -t names 'path' names
          _values 'command' find tree env names help
        elif [[ $words[2] == (tree|t) ]]; then
          _describe -t names 'path' names
        fi
      '';
    in
    {
      config = lib.mkIf on {
        environment.systemPackages = [ htbpaths completion ];

        # Permanent: set for every login session (and so every shell, editor
        # and script started from it). After the first switch, log out and in
        # once, or `eval "$(htbpaths env)"` in a shell that predates it.
        #
        # Only the lowercase per-category variables are set here. $PAYLOADS and
        # $WORDLISTS stay owned by payloads.nix and wordlists.nix (each pointing
        # at its store tree), so they still resolve when this category is off —
        # and so there is no mkForce-vs-mkForce conflict. The lowercase
        # $wordlists points at the ~/pentesting symlink of the same tree.
        environment.sessionVariables =
          lib.listToAttrs (map (e: lib.nameValuePair e.name (abs e.rel)) table);

        systemd.tmpfiles.rules =
          [
            "d ${root} 0755 ${user} ${group} -"
            "d ${root}/local 0755 ${user} ${group} -"
            "L+ ${root}/wordlists - - - - ${cfg.payloads.wordlistsTree}"
          ]
          ++ map (n: "L+ ${root}/${n} - - - - ${cfg.payloads.tree}/${n}") linked;
      };
    };
}