PWN::Plugins::YubiKey
Use the native Ruby wrapper around the official ykman executable. Never
provision, overwrite, repair or reset a slot as part of routine inventory,
authentication or vault access. See documentation/YubiKey.md for the device
contract and documentation/YubiKey-Vaults.md for storage/recovery integration.
When to use
- Inventory and application status, or explicit YubiKey management requests.
- Binary HMAC-SHA1 challenge-response from an already provisioned OTP slot.
- OATH account listing and retrieval with consent for authentication, remembered-key
cleanup, counter and credential-repair effects.
- Do not use device management as a substitute for browser WebAuthn ceremonies.
Prerequisites
Install the official yubikey-manager separately. required_bins returns
['ykman']; available? probes --version without opening a device. Use a trusted
explicit ykman: path when PATH selection is unsuitable. The reviewed management
grammar is 5.9.1; automatic provisioning intentionally accepts only that version.
Use decimal String serials, integer slots 1 or 2, and a finite positive timeout.
How to call
PWN::Plugins::YubiKey.help
PWN::Plugins::YubiKey.available?
PWN::Plugins::YubiKey.devices
PWN::Plugins::YubiKey.info(serial: '12345678')
PWN::Plugins::YubiKey.otp(args: ['info'], serial: '12345678')
PWN::Plugins::YubiKey.cli_help(path: %w[piv keys generate])
Serials above are placeholders. Family methods take an args: argv Array after
the family name; omission returns official help. Use long options as separate
array elements, not a shell string or --option=value. command takes the full
management argv. All return raw output without logging it; successful output can
still contain secrets and must not be echoed into a transcript.
Public methods
required_bins, available?, devices, info, cli_help, help, authors
command, interact, config, otp, oath, fido, piv, openpgp, hsmauth,
security_domain (official sd family), apdu
challenge_response, provision_challenge_response, oath_code
Procedure
- Identify the intended device and application with read-only inventory. Pin a
serial when selecting a device; never choose the first key automatically.
- Read the specific command's help. The wrapper guards the complete reviewed
management tree, but model/firmware/interface support still belongs to ykman.
- Obtain explicit
confirm: true for every non-allowlisted operation, including
resets, provisioning, authentication attempts, exports, OATH listing/calculation and
all raw APDUs. Unknown commands stay rejected even after consent. Do not add
--force merely to make a command succeed.
- Keep credentials out of argv, environment variables, history and logs. Generic
calls close stdin. Use
interact(args: ..., confirm: true) only in a private
operator terminal outside curses/recorded tool sessions. It bridges a native
PTY, returns only :completed, restores terminal mode and descriptor status flags,
and bounds total traffic and elapsed time, including human input. Its terminal
must be readable and writable, including the Ruby wrapper mode; validation
consumes no input and never closes caller-owned IO. Some upstream secret prompts
are not masked. Never guess a PIN. Key imports and SCP11 use protected existing files;
custom SCP03 import KIDs are refused before filename checks, even when the
inline key string names an existing file. SCP11 file-import KIDs accept unsigned
decimal, 0x hex or SCP11a/SCP11b/SCP11c names, without coercion.
- Read the exact result or device state after a mutation. A timeout is uncertain
device state, not proof that nothing changed. Do not automatically retry.
Existing HMAC slot
challenge = SecureRandom.random_bytes(32)
response = PWN::Plugins::YubiKey.challenge_response(
challenge: challenge, slot: 2, serial: '12345678', timeout: 30
); nil
Pass raw 1–64 bytes, not pre-encoded hex. The wrapper sends otp calculate SLOT HEX_CHALLENGE and returns exactly 20 binary bytes. Slot 2 is only a
selector default, never implicit provisioning. Touch the device if required;
the wrapper suppresses the CLI's stderr prompt. The challenge is public in argv;
never use a password as the challenge. Keep the response out of transcripts.
Explicit provisioning
Only after inspecting the slot and obtaining consent, use
provision_challenge_response(key: key_bytes, serial: serial, slot: 2, confirm: true). key_bytes must be exactly 20 raw bytes acquired in local memory,
not a literal pasted into a console. The method checks ykman 5.9.1, supplies a
validated key/consent sequence through stdin without --force, enables touch,
and verifies a fresh random challenge with Ruby OpenSSL before returning
:verified. It overwrites the slot. Plan and test independent recovery before
discarding the key. Protected slots and other CLI versions require the separately
reviewed private-terminal workflow described in documentation/YubiKey.md.
No Ruby zeroization guarantee is made.
Exact OATH
oath_code(account: exact_identifier, serial: serial, confirm: true) returns
6–8 digits, retaining leading zeros, or raises. It verifies a unique full
inventory identifier and the returned account label; substring-only, ambiguous,
changed-identity and nonnumeric output is refused. Official 5.9.1 has no
--exact option; --single alone is not exact matching. Confirmation is mandatory
for listing too: upstream may authenticate using a remembered key and delete/persist
that key if authentication fails, even without --remember. HOTP advances counters
and upstream code can delete corrupted credentials automatically. Do not treat
this as a read-only operation or use it for SMS/passkey
challenges. Password-protected OATH may require the private terminal workflow.
Pitfalls
- Missing executable, unsupported device capability or malformed output is an
error, not permission to reset, provision or fall back to a software key.
ykman script, debug/traffic logging, secret argv fields, arbitrary stdin and
unreviewed future commands are intentionally unavailable.
- Raw APDU bytes are visible in argv. Never put credentials in arbitrary payloads;
the wrapper cannot infer their confidentiality.
- Noninteractive mutations needing prompts can abort/time out. Confirmation in
Ruby does not bypass the CLI's own confirmation except the fixed provisioning
stdin flow; no force flag is inserted.
timeout defaults to 30 seconds and combined output to 1 MiB. Timeout/output
failures kill the Unix child process group and reap the direct child. This is
not a sandbox against a malicious executable escaping its process group.
- FIDO credential/PIN/fingerprint management is not website authentication.
Browser and relying party must perform genuine security-key ceremonies.
Verification
Run the paired offline suites:
bundle exec rspec spec/lib/pwn/plugins/yubikey_spec.rb spec/lib/pwn/plugins/yubikey_security_spec.rb spec/lib/pwn/plugins/yubikey_terminal_spec.rb
bundle exec rubocop lib/pwn/plugins/yubikey.rb spec/lib/pwn/plugins/yubikey_spec.rb spec/lib/pwn/plugins/yubikey_security_spec.rb spec/lib/pwn/plugins/yubikey_terminal_spec.rb
Specs exercise real child processes with fake device executables, argument guards,
binary HMAC vectors, exact OATH parsing, validated provisioning, private PTY
prompts, descriptor/terminal restoration on success and failure, pre-spawn duplex
validation without consuming input, and cleanup. The
real 5.9.1 CLI's management help tree and Click stdin contract were separately
checked offline. None of this proves USB/NFC hardware, physical touches, PIN
protection, every firmware variant, or live website login. Report those boundaries.
1---2name: pwn-plugins-yubikey3description: Manage YubiKeys with explicit mutation guards.4license: MIT5allowed-tools: [pwn, pwn_eval]6metadata:7 bundled: true8 preserve: true9 module: PWN::Plugins::YubiKey10 source: https://skillmd.com/skills/0dayinc/pwn-plugins-yubikey11 upstream: pwn/plugins/yubikey.rb12---1314# PWN::Plugins::YubiKey1516Use the native Ruby wrapper around the official `ykman` executable. Never17provision, overwrite, repair or reset a slot as part of routine inventory,18authentication or vault access. See `documentation/YubiKey.md` for the device19contract and `documentation/YubiKey-Vaults.md` for storage/recovery integration.2021## When to use2223- Inventory and application status, or explicit YubiKey management requests.24- Binary HMAC-SHA1 challenge-response from an **already provisioned** OTP slot.25- OATH account listing and retrieval with consent for authentication, remembered-key26 cleanup, counter and credential-repair effects.27- Do not use device management as a substitute for browser WebAuthn ceremonies.2829## Prerequisites3031Install the official `yubikey-manager` separately. `required_bins` returns32`['ykman']`; `available?` probes `--version` without opening a device. Use a trusted33explicit `ykman:` path when PATH selection is unsuitable. The reviewed management34grammar is 5.9.1; automatic provisioning intentionally accepts only that version.35Use decimal String serials, integer slots 1 or 2, and a finite positive timeout.3637## How to call3839```ruby40PWN::Plugins::YubiKey.help41PWN::Plugins::YubiKey.available?42PWN::Plugins::YubiKey.devices43PWN::Plugins::YubiKey.info(serial: '12345678')44PWN::Plugins::YubiKey.otp(args: ['info'], serial: '12345678')45PWN::Plugins::YubiKey.cli_help(path: %w[piv keys generate])46```4748Serials above are placeholders. Family methods take an `args:` argv Array after49the family name; omission returns official help. Use long options as separate50array elements, not a shell string or `--option=value`. `command` takes the full51management argv. All return raw output without logging it; successful output can52still contain secrets and must not be echoed into a transcript.5354## Public methods5556- `required_bins`, `available?`, `devices`, `info`, `cli_help`, `help`, `authors`57- `command`, `interact`, `config`, `otp`, `oath`, `fido`, `piv`, `openpgp`, `hsmauth`,58 `security_domain` (official **sd** family), `apdu`59- `challenge_response`, `provision_challenge_response`, `oath_code`6061## Procedure62631. Identify the intended device and application with read-only inventory. Pin a64 serial when selecting a device; never choose the first key automatically.652. Read the specific command's help. The wrapper guards the complete reviewed66 management tree, but model/firmware/interface support still belongs to ykman.673. Obtain explicit `confirm: true` for every non-allowlisted operation, including68 resets, provisioning, authentication attempts, exports, OATH listing/calculation and69 all raw APDUs. Unknown commands stay rejected even after consent. Do not add70 `--force` merely to make a command succeed.714. Keep credentials out of argv, environment variables, history and logs. Generic72 calls close stdin. Use `interact(args: ..., confirm: true)` only in a private73 operator terminal outside curses/recorded tool sessions. It bridges a native74 PTY, returns only `:completed`, restores terminal mode and descriptor status flags,75 and bounds total traffic and elapsed time, including human input. Its terminal76 must be readable and writable, including the Ruby wrapper mode; validation77 consumes no input and never closes caller-owned IO. Some upstream secret prompts78 are not masked. Never guess a PIN. Key imports and SCP11 use protected existing files;79 custom SCP03 import KIDs are refused before filename checks, even when the80 inline key string names an existing file. SCP11 file-import KIDs accept unsigned81 decimal, `0x` hex or `SCP11a`/`SCP11b`/`SCP11c` names, without coercion.825. Read the exact result or device state after a mutation. A timeout is uncertain83 device state, not proof that nothing changed. Do not automatically retry.8485### Existing HMAC slot8687```ruby88challenge = SecureRandom.random_bytes(32)89response = PWN::Plugins::YubiKey.challenge_response(90 challenge: challenge, slot: 2, serial: '12345678', timeout: 3091); nil92```9394Pass **raw 1–64 bytes**, not pre-encoded hex. The wrapper sends `otp calculate95SLOT HEX_CHALLENGE` and returns exactly **20 binary bytes**. Slot 2 is only a96selector default, never implicit provisioning. Touch the device if required;97the wrapper suppresses the CLI's stderr prompt. The challenge is public in argv;98never use a password as the challenge. Keep the response out of transcripts.99100### Explicit provisioning101102Only after inspecting the slot and obtaining consent, use103`provision_challenge_response(key: key_bytes, serial: serial, slot: 2,104confirm: true)`. `key_bytes` must be exactly 20 raw bytes acquired in local memory,105not a literal pasted into a console. The method checks ykman 5.9.1, supplies a106validated key/consent sequence through stdin without `--force`, enables touch,107and verifies a fresh random challenge with Ruby OpenSSL before returning108`:verified`. It overwrites the slot. Plan and test independent recovery before109discarding the key. Protected slots and other CLI versions require the separately110reviewed private-terminal workflow described in `documentation/YubiKey.md`.111No Ruby zeroization guarantee is made.112113### Exact OATH114115`oath_code(account: exact_identifier, serial: serial, confirm: true)` returns1166–8 digits, retaining leading zeros, or raises. It verifies a unique full117inventory identifier and the returned account label; substring-only, ambiguous,118changed-identity and nonnumeric output is refused. Official 5.9.1 has **no119--exact** option; `--single` alone is not exact matching. Confirmation is mandatory120for listing too: upstream may authenticate using a remembered key and delete/persist121that key if authentication fails, even without `--remember`. HOTP advances counters122and upstream code can delete corrupted credentials automatically. Do not treat123this as a read-only operation or use it for SMS/passkey124challenges. Password-protected OATH may require the private terminal workflow.125126## Pitfalls127128- Missing executable, unsupported device capability or malformed output is an129 error, not permission to reset, provision or fall back to a software key.130- `ykman script`, debug/traffic logging, secret argv fields, arbitrary stdin and131 unreviewed future commands are intentionally unavailable.132- Raw APDU bytes are visible in argv. Never put credentials in arbitrary payloads;133 the wrapper cannot infer their confidentiality.134- Noninteractive mutations needing prompts can abort/time out. Confirmation in135 Ruby does not bypass the CLI's own confirmation except the fixed provisioning136 stdin flow; no force flag is inserted.137- `timeout` defaults to 30 seconds and combined output to 1 MiB. Timeout/output138 failures kill the Unix child process group and reap the direct child. This is139 not a sandbox against a malicious executable escaping its process group.140- FIDO credential/PIN/fingerprint management is not website authentication.141 Browser and relying party must perform genuine security-key ceremonies.142143## Verification144145Run the paired offline suites:146147```text148bundle exec rspec spec/lib/pwn/plugins/yubikey_spec.rb spec/lib/pwn/plugins/yubikey_security_spec.rb spec/lib/pwn/plugins/yubikey_terminal_spec.rb149bundle exec rubocop lib/pwn/plugins/yubikey.rb spec/lib/pwn/plugins/yubikey_spec.rb spec/lib/pwn/plugins/yubikey_security_spec.rb spec/lib/pwn/plugins/yubikey_terminal_spec.rb150```151152Specs exercise real child processes with fake device executables, argument guards,153binary HMAC vectors, exact OATH parsing, validated provisioning, private PTY154prompts, descriptor/terminal restoration on success and failure, pre-spawn duplex155validation without consuming input, and cleanup. The156real 5.9.1 CLI's management help tree and Click stdin contract were separately157checked offline. None of this proves USB/NFC hardware, physical touches, PIN158protection, every firmware variant, or live website login. Report those boundaries.