T05 · Unauthorized Access and Privilege Escalation
Error
- Location
- examples/ssh-key-sharing.md:167
- Finding
- Remote Account Creation and Passwordless Sudo Delegation<![CDATA[ ## Vulnerability Details **File Location**: `examples/ssh-key-sharing.md:167-187` **Vulnerability Type**: Excessive remote privilege modification **Risk Level**: High ### Vulnerable Code ```bash # Configuration USERNAME="deploy" SERVER="production.example.com" TEMP_DIR="/tmp" print_status() { echo "[INFO] $1" } print_success() { echo "[SUCCESS] $1" } # 1. Create user on server (requires root access) print_status "Creating user $USERNAME on server..." ssh root@$SERVER "useradd -m -s /bin/bash $USERNAME" # 2. Generate SSH key print_status "Generating SSH key pair..." ssh-keygen -t ed25519 -f "$TEMP_DIR/$USERNAME-key" -C "$USERNAME@$SERVER" -N "" # 3. Send private key to human print_status "Sending private key via wormhole..." CODE=$(wormhole send --text "$(cat $TEMP_DIR/$USERNAME-key)" 2>&1 | grep "Wormhole code is:" | cut -d' ' -f4) # 4. Add public key to server print_status "Adding public key to server..." ssh root@$SERVER "mkdir -p /home/$USERNAME/.ssh && chmod 700 /home/$USERNAME/.ssh && echo '$(cat $TEMP_DIR/$USERNAME-key.pub)' > /home/$USERNAME/.ssh/authorized_keys && chmod 600 /home/$USERNAME/.ssh/authorized_keys && chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh" # 5. Grant sudo access (optional) print_status "Granting sudo access..." ssh root@$SERVER "echo '$USERNAME ALL=(ALL) NOPASSWD:/usr/bin/apt-get' >> /etc/sudoers.d/$USERNAME" ``` ### Technical Analysis The workflow does substantially more than transfer an SSH key. It connects to a remote server as `root`, creates a persistent login account, replaces that account's `authorized_keys`, and unconditionally installs a passwordless sudo rule. Although the comment calls sudo access optional, the command is executed without an option, confirmation prompt, or policy check. Allowing passwordless execution of `apt-get` as root is especially dangerous because package-manager invocation can commonly be adapted to execute arbitrary commands or package installation hooks with root p ...[truncated 1100 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Remove remote account creation and sudo-policy changes from the key-transfer example. - Require separate, explicit, human-approved provisioning steps for every remote mutation. - Never grant `NOPASSWD` access to a general-purpose package manager. - Use a pre-created, least-privileged service account with narrowly scoped authorization. - If sudo is genuinely required, permit only a purpose-built, validated command with fixed arguments. - Validate usernames and hostnames against strict allowlists before using them in remote commands. - Use `sudo visudo -cf` to validate any proposed sudo policy before installation. - Prefer constrained `authorized_keys` entries using options such as `from=`, `command=`, `no-port-forwarding`, `no-agent-forwarding`, `no-X11-forwarding`, and `no-pty`. - Require a confirmation step identifying the target host, account, key fingerprint, and exact access policy. ]]>
