[GH-ISSUE #2624] SSH-ng doesn't correctly handle non-default iteration count for bcrypt_pbkdf #1710

Closed
opened 2026-10-15 04:22:59 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @Apingis on GitHub (Jul 10, 2017).
Original GitHub issue: https://github.com/openwall/john/issues/2624

Originally assigned to: @kholia on GitHub.

sshng2john.py doesn't correctly handle non-default iteration count for keys encrypted with bcrypt_pbkdf (created with ssh-keygen -o)
It always stores the default iteration count.

$ ssh-keygen.exe -o -N test -f ssh-test1
[...]

$ ssh-keygen.exe -o -N test -f ssh-test2 -a 64
[...]

$ ./sshng2john.py ssh-test1 >pw-ssh1

$ ./sshng2john.py ssh-test2 >pw-ssh2

$ ./john.exe --mask=test pw-ssh1
Using default input encoding: UTF-8
Loaded 1 password hash (SSH-ng [RSA/DSA/EC/OPENSSH (SSH private keys) 32/32])
Warning: OpenMP is disabled; a non-OpenMP build may be faster
Note: This format may emit false positives, so it will keep trying even after
finding a possible candidate.
Press 'q' or Ctrl-C to abort, almost any other key for status
test             (ssh-test1)
1g 0:00:00:00  1.510g/s 1.510p/s 1.510c/s 1.510C/s test
Session completed

$ ./john.exe --mask=test pw-ssh2
Using default input encoding: UTF-8
Loaded 1 password hash (SSH-ng [RSA/DSA/EC/OPENSSH (SSH private keys) 32/32])
Warning: OpenMP is disabled; a non-OpenMP build may be faster
Note: This format may emit false positives, so it will keep trying even after
finding a possible candidate.
Press 'q' or Ctrl-C to abort, almost any other key for status
0g 0:00:00:00  0g/s 2.518p/s 2.518c/s 2.518C/s test
Session completed

If iteration count in password file is edited, then john can't load the hash:

$ perl -pne 's/sshng\$2\$16/sshng\$2\$64/' <pw-ssh2 >pw-ssh3

$ ./john.exe --mask=test pw-ssh3
Using default input encoding: UTF-8
No password hashes loaded (see FAQ)

It's not clear if bcrypt_pbkdf can emit false positives - even if it can then the probability must be extremely low. The message on false positives seems to be related to other algorithm handled by the same format.

Originally created by @Apingis on GitHub (Jul 10, 2017). Original GitHub issue: https://github.com/openwall/john/issues/2624 Originally assigned to: @kholia on GitHub. sshng2john.py doesn't correctly handle non-default iteration count for keys encrypted with bcrypt_pbkdf (created with `ssh-keygen -o`) It always stores the default iteration count. ``` $ ssh-keygen.exe -o -N test -f ssh-test1 [...] $ ssh-keygen.exe -o -N test -f ssh-test2 -a 64 [...] $ ./sshng2john.py ssh-test1 >pw-ssh1 $ ./sshng2john.py ssh-test2 >pw-ssh2 $ ./john.exe --mask=test pw-ssh1 Using default input encoding: UTF-8 Loaded 1 password hash (SSH-ng [RSA/DSA/EC/OPENSSH (SSH private keys) 32/32]) Warning: OpenMP is disabled; a non-OpenMP build may be faster Note: This format may emit false positives, so it will keep trying even after finding a possible candidate. Press 'q' or Ctrl-C to abort, almost any other key for status test (ssh-test1) 1g 0:00:00:00 1.510g/s 1.510p/s 1.510c/s 1.510C/s test Session completed $ ./john.exe --mask=test pw-ssh2 Using default input encoding: UTF-8 Loaded 1 password hash (SSH-ng [RSA/DSA/EC/OPENSSH (SSH private keys) 32/32]) Warning: OpenMP is disabled; a non-OpenMP build may be faster Note: This format may emit false positives, so it will keep trying even after finding a possible candidate. Press 'q' or Ctrl-C to abort, almost any other key for status 0g 0:00:00:00 0g/s 2.518p/s 2.518c/s 2.518C/s test Session completed ``` If iteration count in password file is edited, then john can't load the hash: ``` $ perl -pne 's/sshng\$2\$16/sshng\$2\$64/' <pw-ssh2 >pw-ssh3 $ ./john.exe --mask=test pw-ssh3 Using default input encoding: UTF-8 No password hashes loaded (see FAQ) ``` It's not clear if bcrypt_pbkdf can emit false positives - even if it can then the probability must be extremely low. The message on false positives seems to be related to other algorithm handled by the same format.
bsakaguchi 2026-10-15 04:22:59 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@kholia commented on GitHub (Jul 11, 2017):

I think there are multiple problems with cracking SSH keys which use bcrypt_pbkdf.

I am running ssh-keygen from openssh-7.2p2, and JtR jumbo can't crack the hashes for the following files, even when the password is in the dictionary.

$ ssh-keygen -o -N test -f ssh-test1

$ ssh-keygen -o -N test -f ssh-test2 -a 64

Correction: The ssh-test1 file is cracked but ssh-test2 isn't.

FWIW, JtR jumbo loaded the second file (ssh-test2) just fine for me.

@Apingis Which versions of OpenSSH are you running? Can you please attach both the generated test SSH keys here?

I vaguely remember bcrypt_pbkdf being changed in the past. Usage of two different variants of bcrypt_pbkdf might be causing some of the problems we are observing.

I also see an old commit message of mine, which indicates that the quality, and correctness of this code is questionable at best.

$ git log -p ssh_ng_fmt_plug.c
...
commit b685dffbc8244dcfd542c53ae0b5b97364eeac15
Author: Dhiru Kholia <kholia@kth.se>
Date:   Thu Oct 29 14:39:14 2015 +0100

    [ssh-ng] advertise support for new "OPENSSH" format private keys
    
    Weirdly enough, this part is broken for no known reason!

I never got around to fixing this code.

Finally, I am seeing lots of false positives while cracking such keys. This is because only the CBC padding check is currently implemented for such keys. This needs to be fixed as well.

<!-- gh-comment-id:314308635 --> @kholia commented on GitHub (Jul 11, 2017): I think there are multiple problems with cracking SSH keys which use bcrypt_pbkdf. I am running ssh-keygen from `openssh-7.2p2`, and JtR jumbo can't crack the hashes for the following files, even when the password is in the dictionary. ``` $ ssh-keygen -o -N test -f ssh-test1 $ ssh-keygen -o -N test -f ssh-test2 -a 64 ``` Correction: The ssh-test1 file is cracked but ssh-test2 isn't. FWIW, JtR jumbo loaded the second file (ssh-test2) just fine for me. @Apingis Which versions of OpenSSH are you running? Can you please attach both the generated test SSH keys here? I vaguely remember `bcrypt_pbkdf` being changed in the past. Usage of two different variants of bcrypt_pbkdf might be causing some of the problems we are observing. I also see an old commit message of mine, which indicates that the quality, and correctness of this code is questionable at best. ``` $ git log -p ssh_ng_fmt_plug.c ... commit b685dffbc8244dcfd542c53ae0b5b97364eeac15 Author: Dhiru Kholia <kholia@kth.se> Date: Thu Oct 29 14:39:14 2015 +0100 [ssh-ng] advertise support for new "OPENSSH" format private keys Weirdly enough, this part is broken for no known reason! ``` I never got around to fixing this code. Finally, I am seeing lots of false positives while cracking such keys. This is because only the CBC padding check is currently implemented for such keys. This needs to be fixed as well.
Author
Owner

@kholia commented on GitHub (Jul 11, 2017):

PR #2626 fixes the iteration count problem.

The CBC padding check is not correct for all cases (keys). For some keys, the padding length can be zero bytes, which results in false negatives with our current code.

<!-- gh-comment-id:314321329 --> @kholia commented on GitHub (Jul 11, 2017): PR #2626 fixes the iteration count problem. The CBC padding check is not correct for all cases (keys). For some keys, the padding length can be zero bytes, which results in false negatives with our current code.
Author
Owner

@kholia commented on GitHub (Jul 11, 2017):

@Apingis Thanks for bringing this issue to my attention! It should be mostly fixed in PR #2627.

One of the reasons that your Perl hack wasn't working is that the iteration count is not the field adjacent to the $sshng$2 string. The iteration count is the last field in the hash. Actually, with the new PR, it the second last field in the hash.

<!-- gh-comment-id:314357590 --> @kholia commented on GitHub (Jul 11, 2017): @Apingis Thanks for bringing this issue to my attention! It should be mostly fixed in PR #2627. One of the reasons that your Perl hack wasn't working is that the `iteration count` is not the field adjacent to the `$sshng$2` string. The iteration count is the last field in the hash. Actually, with the new PR, it the second last field in the hash.
Author
Owner

@Apingis commented on GitHub (Jul 11, 2017):

OpenSSH version:
OpenSSH_7.5p1, OpenSSL 1.0.2k

@kholia I've updated from PR #2627, both previously mentioned keys were cracked.
As for John unable to load edited hash, that was other record referenced (with the same value), there's no issue.

<!-- gh-comment-id:314369021 --> @Apingis commented on GitHub (Jul 11, 2017): OpenSSH version: OpenSSH_7.5p1, OpenSSL 1.0.2k @kholia I've updated from PR #2627, both previously mentioned keys were cracked. As for John unable to load edited hash, that was other record referenced (with the same value), there's no issue.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1710
No description provided.