What this change means
Source files changed. The change could not be classified.
| @@ -2,9 +2,11 @@ package privatekey | ||
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "bytes" |
| 5 | added: "context" | |
| 5 | 6 | "crypto/x509" |
| 6 | 7 | _ "embed" |
| 7 | 8 | "errors" |
| 9 | added: "fmt" | |
| 8 | 10 | |
| 9 | 11 | "golang.org/x/crypto/ssh" |
| 10 | 12 | ) |
| @@ -19,10 +21,34 @@ func init() { | ||
| 19 | 21 | |
| 20 | 22 | var ( |
| 21 | 23 | ErrUncrackable = errors.New("unable to crack encryption") |
| 24 | added: // ErrKDFTooExpensive is returned without attempting any passphrase when a | |
| 25 | added: // key's KDF is more expensive than the crack budget allows. | |
| 26 | added: ErrKDFTooExpensive = errors.New("key KDF is too expensive to crack") | |
| 22 | 27 | ) |
| 23 | 28 | |
| 24 | removed: func Crack(in []byte) (any, string, error) { | |
| 25 | removed: for _, passphrase := range passphrases { | |
| 29 | added: // maxCrackRounds caps the bcrypt KDF round count this detector will attempt | |
| 30 | added: // to crack. The round count is chosen by whoever created the key and the KDF | |
| 31 | added: // cost is linear in rounds; OpenSSH's default is 16. 256 is 16x that default: | |
| 32 | added: // a single wrong guess on a key at the cap costs on the order of a second of | |
| 33 | added: // CPU, so a wordlist crack is hopeless within any bounded budget. x/crypto | |
| 34 | added: // refuses round counts above 2048 on its own; this lower cap keeps | |
| 35 | added: // hardened-but-legal keys from making each attempt that expensive. Division | |
| 36 | added: // of labor: the cap bounds what one attempt can cost, and the context check | |
| 37 | added: // below bounds the total, where the caller sets a deadline at all. | |
| 38 | added: const maxCrackRounds = 256 | |
| 39 | added: | |
| 40 | added: func Crack(ctx context.Context, in []byte) (any, string, error) { | |
| 41 | added: if rounds, ok := opensshBcryptRounds(in); ok && rounds > maxCrackRounds { | |
| 42 | added: return nil, "", fmt.Errorf("%w: %d rounds exceeds cap of %d", ErrKDFTooExpensive, rounds, maxCrackRounds) | |
| 43 | added: } | |
| 44 | added: for i, passphrase := range passphrases { | |
| 45 | added: // A wrong passphrase costs a full KDF run whose cost is set by the | |
| 46 | added: // key's author, so stop between attempts once the caller's context is | |
| 47 | added: // done. The in-flight attempt cannot be interrupted: | |
| 48 | added: // ssh.ParseRawPrivateKeyWithPassphrase takes no context. | |
| 49 | added: if err := ctx.Err(); err != nil { | |
| 50 | added: return nil, "", fmt.Errorf("stopped cracking after %d of %d passphrase attempts: %w", i, len(passphrases), err) | |
| 51 | added: } | |
| 26 | 52 | parsed, err := ssh.ParseRawPrivateKeyWithPassphrase(in, passphrase) |
| 27 | 53 | if err != nil { |
| 28 | 54 | if errors.Is(err, x509.IncorrectPasswordError) { |
| @@ -0,0 +1,83 @@ | ||
| 1 | added: package privatekey | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "encoding/pem" | |
| 5 | added: "math" | |
| 6 | added: | |
| 7 | added: "golang.org/x/crypto/ssh" | |
| 8 | added: ) | |
| 9 | added: | |
| 10 | added: // privateKeyAuthMagic starts the decoded body of every key in the OpenSSH | |
| 11 | added: // private key format (see PROTOCOL.key in the OpenSSH source distribution). | |
| 12 | added: const privateKeyAuthMagic = "openssh-key-v1\x00" | |
| 13 | added: | |
| 14 | added: // openSSHEncryptedPrivateKey and kdfOpts mirror the wire structs x/crypto | |
| 15 | added: // parses the same bytes with in ssh/keys.go, so the round count read here is | |
| 16 | added: // the round count the KDF will actually run at. Keep them in sync with | |
| 17 | added: // x/crypto's private structs: if the parses ever diverge, the count read | |
| 18 | added: // here silently stops matching the KDF's cost and keys are over- or | |
| 19 | added: // under-refused. x/crypto exports no way to read the KDF options, so the | |
| 20 | added: // header is read in the detector. | |
| 21 | added: type openSSHEncryptedPrivateKey struct { | |
| 22 | added: CipherName string | |
| 23 | added: KdfName string | |
| 24 | added: KdfOpts string | |
| 25 | added: NumKeys uint32 | |
| 26 | added: PubKey []byte | |
| 27 | added: PrivKeyBlock []byte | |
| 28 | added: // Rest swallows trailing bytes the way x/crypto's copy of this struct | |
| 29 | added: // does. Without the tag, a key with bytes after PrivKeyBlock would fail | |
| 30 | added: // to parse here, read as ok=false, and skip the cap check that | |
| 31 | added: // x/crypto would still apply. | |
| 32 | added: Rest []byte `ssh:"rest"` | |
| 33 | added: } | |
| 34 | added: | |
| 35 | added: // kdfOpts deliberately has no ssh:"rest" field: that is what makes | |
| 36 | added: // ssh.Unmarshal reject options blobs carrying trailing bytes, which is the | |
| 37 | added: // fail-closed path below. Adding one for forward compatibility would | |
| 38 | added: // silently disarm it. | |
| 39 | added: type kdfOpts struct { | |
| 40 | added: Salt string | |
| 41 | added: Rounds uint32 | |
| 42 | added: } | |
| 43 | added: | |
| 44 | added: // opensshBcryptRounds reads the bcrypt round count from an OpenSSH key's | |
| 45 | added: // header. The decoded key stores the cipher name, KDF name, and KDF options | |
| 46 | added: // before the encrypted section, so the round count is readable without the | |
| 47 | added: // passphrase; Crack uses it to refuse keys whose KDF exceeds maxCrackRounds | |
| 48 | added: // before spending any passphrase attempt. | |
| 49 | added: // | |
| 50 | added: // ok=false means the key is not an OpenSSH bcrypt key, and it should be | |
| 51 | added: // cracked exactly as before. ok=true means the round count is binding, | |
| 52 | added: // including fail-closed: a bcrypt key whose options blob does not parse | |
| 53 | added: // returns the maximum round count rather than ok=false, because the key is | |
| 54 | added: // a bcrypt key and should be refused, not silently cracked. ssh.Unmarshal | |
| 55 | added: // rejects such a blob as well, so the key could not have been decrypted at | |
| 56 | added: // any round count. | |
| 57 | added: func opensshBcryptRounds(pemBytes []byte) (rounds uint32, ok bool) { | |
| 58 | added: block, _ := pem.Decode(pemBytes) | |
| 59 | added: if block == nil || block.Type != "OPENSSH PRIVATE KEY" || | |
| 60 | added: len(block.Bytes) < len(privateKeyAuthMagic) || | |
| 61 | added: string(block.Bytes[:len(privateKeyAuthMagic)]) != privateKeyAuthMagic { | |
| 62 | added: return 0, false | |
| 63 | added: } | |
| 64 | added: | |
| 65 | added: var w openSSHEncryptedPrivateKey | |
| 66 | added: if err := ssh.Unmarshal(block.Bytes[len(privateKeyAuthMagic):], &w); err != nil { | |
| 67 | added: return 0, false | |
| 68 | added: } | |
| 69 | added: if w.KdfName != "bcrypt" { | |
| 70 | added: return 0, false | |
| 71 | added: } | |
| 72 | added: | |
| 73 | added: ok = true | |
| 74 | added: | |
| 75 | added: var opts kdfOpts | |
| 76 | added: if err := ssh.Unmarshal([]byte(w.KdfOpts), &opts); err != nil { | |
| 77 | added: rounds = math.MaxUint32 | |
| 78 | added: } else { | |
| 79 | added: rounds = opts.Rounds | |
| 80 | added: } | |
| 81 | added: | |
| 82 | added: return rounds, ok | |
| 83 | added: } | |
| @@ -79,13 +79,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 79 | 79 | parsedKey, err := ssh.ParseRawPrivateKey([]byte(token)) |
| 80 | 80 | if err != nil && strings.Contains(err.Error(), "private key is passphrase protected") { |
| 81 | 81 | s1.ExtraData["encrypted"] = "true" |
| 82 | removed: parsedKey, passphrase, err = Crack([]byte(token)) | |
| 82 | added: parsedKey, passphrase, err = Crack(ctx, []byte(token)) | |
| 83 | 83 | if err != nil { |
| 84 | removed: // The key is encrypted and the passphrase could not be | |
| 85 | removed: // recovered from the wordlist. It still represents a real | |
| 86 | removed: // exposure (offline-crackable, weak/legacy KDFs, passphrase | |
| 87 | removed: // often reused or committed nearby), so surface it as an | |
| 88 | removed: // unverified finding instead of dropping it silently. | |
| 84 | added: // The key is encrypted but could not be cracked: the | |
| 85 | added: // passphrase is not in the wordlist, the key's KDF exceeded | |
| 86 | added: // the crack budget, or the scan context ended first. It still | |
| 87 | added: // represents a real exposure (offline-crackable, weak/legacy | |
| 88 | added: // KDFs, passphrase often reused or committed nearby), so | |
| 89 | added: // surface it as an unverified finding instead of dropping it | |
| 90 | added: // silently. | |
| 89 | 91 | s1.SetVerificationError(err, token) |
| 90 | 92 | results = append(results, s1) |
| 91 | 93 | continue |