repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
python.nixNix151 lines5.4 KiBraw download
# modules/features/pentest/python.nix — one interpreter with every offensive
# library importable, plus impacket's 70 example scripts reachable by name.
{ lib, ... }:
(import ./_sets.nix { inherit lib; }) {
  name = "python";
  description = "offensive python: impacket, certipy, pypykatz and friends";

  packages = pkgs:
    let
      inherit (import ./_impacket.nix { inherit lib pkgs; }) impacket aliases;

      # Libraries, for importing. Applications are NOT installed from this env:
      # python3.withPackages links every package's console scripts into the
      # env's bin/, so an app that is ALSO installed standalone (certipy in
      # ad.nix, bloodhound-py, pypykatz…) gives two store paths owning one
      # name, which is a profile collision that stops the system building.
      # checks.pentest-collisions found exactly that, twice.
      env = pkgs.python3.withPackages (ps: with ps; [
        impacket
        certipy
        dploot
        masky
        ldapdomaindump
        pypykatz
        bloodyad
        lsassy
        minikerberos
        aiowinreg
        dnspython
        scapy
        pwntools
        pycryptodomex
        requests
        rich
        aiosmtpd # Snoopy: pentest-python -m aiosmtpd
        paramiko # Snoopy's Python SSH honeypot
        pycryptodome # Crypto namespace, distinct from Cryptodome above
        pyasn1
        ldap3
      ]);

      # Spec C2's discovery command: `impacket` alone lists the 70 scripts
      # (through fzf when there is a terminal), `impacket <name>` runs one.
      # Tab-completing `impacket-` does most of this already, but this is the
      # entry point when you cannot remember whether it is GetUserSPNs or
      # getuserspns.
      impacket-cmd = pkgs.writeShellScriptBin "impacket" ''
        set -uo pipefail
        names() {
          ${pkgs.coreutils}/bin/ls -1 ${aliases}/bin \
            | ${pkgs.gnugrep}/bin/grep '^impacket-' \
            | ${pkgs.gnused}/bin/sed 's/^impacket-//' \
            | ${pkgs.coreutils}/bin/sort
        }
        case "''${1:-}" in
          -l|--list) names; exit 0 ;;
          -h|--help)
            echo "usage: impacket [<script>] [args…]   (no script: pick one)"
            echo "       impacket --list"
            echo "       every script is also a command: impacket-<script>"
            exit 0 ;;
        esac
        if [ "$#" -eq 0 ]; then
          if [ -t 0 ] && [ -t 1 ]; then
            sel=$(names | ${pkgs.fzf}/bin/fzf --prompt='impacket › ' \
                    --preview='${aliases}/bin/impacket-{} --help 2>&1 | head -40' \
                    --preview-window='right,65%,border-left,wrap') || exit 0
            [ -n "''${sel:-}" ] || exit 0
            exec ${aliases}/bin/impacket-"$sel"
          fi
          names
          exit 0
        fi
        script="$1"; shift
        if [ ! -x "${aliases}/bin/impacket-$script" ]; then
          echo "impacket: no script '$script'" >&2
          echo "try: impacket --list" >&2
          exit 2
        fi
        exec ${aliases}/bin/impacket-"$script" "$@"
      '';

      # So only ONE name is exported from the env: the interpreter. Any offensive script you
      # download runs with `pentest-python foo.py` and its imports resolve, with
      # no venv and no collisions.
      pentest-python = pkgs.runCommand "pentest-python"
        {
          meta = {
            description = "Python with the offensive library set importable";
            mainProgram = "pentest-python";
          };
        }
        ''
          mkdir -p $out/bin
          ln -s ${env}/bin/python3 $out/bin/pentest-python
        '';
    in
    [
      pentest-python
      impacket-cmd # `impacket` / `impacket --list`
      impacket # the 70 example scripts, as `secretsdump.py` etc.
      aliases # ...and as `secretsdump`, collision-guarded
    ];

  expectedBins = [
    "pentest-python"
    "secretsdump.py" # impacket's own name
    "secretsdump" # the suffix-free alias
    "GetUserSPNs"
    "impacket" # the discovery command
    "impacket-split" # held back from the bare name, reachable prefixed
  ];

  # Review Focus #1. environment.systemPackages merges every package into ONE
  # profile with buildEnv, so two packages owning bin/split is a collision, not
  # a PATH-order question. The probe below is that same merge: it fails to build
  # if an alias claims a name a real tool already owns, and once it builds we
  # assert the real tool is what the name resolves to.
  checkScript = { pkgs, lib }:
    let
      probe = pkgs.buildEnv {
        name = "pentest-python-profile-probe";
        paths = [
          (import ./_impacket.nix { inherit lib pkgs; }).aliases
          pkgs.coreutils
          pkgs.iputils
          pkgs.samba
        ];
      };
    in
    ''
      pentest-python -c 'import aiosmtpd, paramiko, Crypto, pyasn1, ldap3'
      # The bare names must still be the real tools.
      for n in split ping net smbclient; do
        t=$(readlink -f ${probe}/bin/$n)
        case "$t" in
          *impacket*)
            echo "pentest-python: '$n' resolves to impacket ($t)" >&2
            echo "  a bare '$n' must stay the real tool; impacket's is impacket-$n" >&2
            exit 1 ;;
        esac
      done
      # ...and impacket's versions must be reachable under the prefix.
      for n in split ping net smbclient mimikatz; do
        if [ ! -e ${probe}/bin/impacket-$n ]; then
          echo "pentest-python: impacket-$n is missing" >&2
          exit 1
        fi
      done
    '';
}