/
pass_gen.pl
executable file
·2179 lines (2084 loc) · 92.5 KB
/
pass_gen.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict;
#############################################################################
# For the version information list and copyright statement,
# see doc/pass_gen.Manifest
#############################################################################
use Authen::Passphrase::DESCrypt;
use Authen::Passphrase::BigCrypt;
use Authen::Passphrase::MD5Crypt;
use Authen::Passphrase::BlowfishCrypt;
use Authen::Passphrase::EggdropBlowfish;
use Authen::Passphrase::LANManager;
use Authen::Passphrase::NTHash;
use Authen::Passphrase::PHPass;
use Digest::MD4 qw(md4 md4_hex md4_base64);
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Digest; # Whirlpool is gotten from Digest->new('Whirlpool')
use Digest::SHA qw(sha1 sha1_hex sha1_base64 sha224 sha224_hex sha224_base64 sha256 sha256_hex sha256_base64 sha384 sha384_hex sha384_base64 sha512 sha512_hex sha512_base64 );
use Digest::GOST qw(gost gost_hex gost_base64);
use Encode;
use Switch 'Perl5', 'Perl6';
use POSIX;
use Getopt::Long;
use Math::BigInt;
use Crypt::RC4;
use Crypt::CBC;
use Crypt::DES;
use Crypt::ECB qw(encrypt PADDING_AUTO PADDING_NONE);
use Crypt::PBKDF2;
use Crypt::OpenSSL::PBKDF2;
use String::CRC32;
use MIME::Base64;
#############################################################################
#
# Here is how to add a new hash subroutine to this script file:
#
# 1. add a new element to the @funcs array. The case of this string does
# not matter. The only time it is shown is on the usage screen, so make
# it something recognizable to the user wanting to know what this script
# can do.
# 2. add a new sub to the bottom of this program. The sub MUST be same
# spelling as what is added here, but MUST be lower case. Thus, we see
# DES here in funcs array, but the sub is: sub des($pass) This
# subroutine will be passed a candidate password, and should should output
# the proper hash. All salts are randomly selected, either from the perl
# function doing the script, or by using the randstr() subroutine.
# 3. Test to make sure it works properly. Make sure john can find ALL values
# your subroutine returns.
# 4. Update the version of this file (at the top of it)
# 5. Publish it to the john wiki for others to also use.
#
# These john jumbo formats are not done 'yet':
# AFS/KRB5/dominosec/sapG/sapB/DMD5/trip/keychain/pfx/racf/sip/vnc/wpapsk
#
# these are decrypt images, which we may not be able to do in perl. We will take these case by case.
# odf office pdf pkzip zip rar ssh
#
# lotus5 is done in some custom C code. If someone wants to take a crack at it here, be my guest :)
#############################################################################
my @funcs = (qw(DES BigCrypt BSDI MD5_1 MD5_a BF BFx BFegg RawMD5 RawMD5u
RawSHA1 RawSHA1u msCash LM NT pwdump RawMD4 PHPass PO hmacMD5
IPB2 PHPS MD4p MD4s SHA1p SHA1s mysqlSHA1 pixMD5 MSSql05 MSSql12 nsldap
nsldaps ns XSHA mskrb5 mysql mssql_no_upcase_change mssql oracle
oracle_no_upcase_change oracle11 hdaa netntlm_ess openssha
l0phtcrack netlmv2 netntlmv2 mschapv2 mscash2 mediawiki crc_32
Dynamic dummy rawsha224 rawsha256 rawsha384 rawsha512 dragonfly3_32
dragonfly4_32 dragonfly3_64 dragonfly4_64 saltedsha1 raw_gost
raw_gost_cp hmac_sha1 hmac_sha224 hmac_sha256 hmac_sha384 hmac_sha512
sha256crypt sha512crypt XSHA512 dynamic_27 dynamic_28 pwsafe django
drupal7 epi episerver_sha1 episerver_sha256 hmailserver ike keepass
keychain nukedclan pfx racf radmin rawsha0 sip SybaseASE vnc wbb3 wpapsk
sunmd5 wow_srp));
# todo: ike keychain pfx racf sip vnc wpapsk
my $i; my $h; my $u; my $salt;
my @chrAsciiText=('a'..'z','A'..'Z');
my @chrAsciiTextLo=('a'..'z');
my @chrAsciiTextHi=('A'..'Z');
my @chrAsciiTextNum=('a'..'z','A'..'Z','0'..'9');
my @chrAsciiTextNumUnder=('a'..'z','A'..'Z','0'..'9','_');
my @chrHexHiLo=('0'..'9','a'..'f','A'..'F');
my @chrHexLo=('0'..'9','a'..'f');
my @chrHexHi=('0'..'9','A'..'F');
my @i64 = ('.','/','0'..'9','A'..'Z','a'..'z');
my @ns_i64 = ('A'..'Z', 'a'..'z','0'..'9','+','/',);
my @userNames = (
"admin", "root", "bin", "Joe", "fi15_characters", "Babeface", "Herman", "lexi Conrad", "jack", "John", "sz110",
"fR14characters", "Thirteenchars", "Twelve_chars", "elev__chars", "teN__chars", "six16_characters",
# "Bãrtin",
"ninechars", "eightchr", "sevench", "barney", "C0ffee", "deadcafe", "user", "01234", "nineteen_characters",
"eight18_characters", "seven17characters", "u1", "harvey", "john", "ripper", "a", "Hank", "1", "u2", "u3",
"2", "3", "usr", "usrx", "usry", "skippy", "Bing", "Johnson", "addams", "anicocls", "twentyXXX_characters",
"twentyoneX_characters", "twentytwoXX_characters");
#########################################################
# These global vars are used by the Dynamic parsing engine
# to deal with unknown formats.
#########################################################
my $gen_u; my $gen_s; my $gen_soutput, my $gen_stype; my $gen_s2; my $gen_pw; my @gen_c; my @gen_toks; my $gen_num;
my $gen_lastTokIsFunc; my $gen_u_do; my $dynamic_usernameType; my $dynamic_passType; my $salt2len; my $saltlen; my $gen_PWCase="";
# pcode, and stack needed for pcode.
my @gen_pCode; my @gen_Stack; my @gen_Flags;
my $debug_pcode=0; my $gen_needs; my $gen_needs2; my $gen_needu; my $gen_singlesalt;
my $hash_format; my $arg_utf8 = 0; my $arg_codepage = ""; my $arg_minlen = 0; my $arg_maxlen = 128; my $arg_dictfile = "unknown";
my $arg_count = 1500, my $argsalt, my $arg_nocomment = 0; my $arg_hidden_cp;
GetOptions(
'codepage=s' => \$arg_codepage,
'hiddencp=s' => \$arg_hidden_cp,
'utf8!' => \$arg_utf8,
'nocomment!' => \$arg_nocomment,
'minlength=n' => \$arg_minlen,
'maxlength=n' => \$arg_maxlen,
'salt=s' => \$argsalt,
'count=n' => \$arg_count,
'dictfile=s' => \$arg_dictfile
) || usage();
sub usage {
die <<"UsageHelp";
usage: $0 [-h|-?] [codepage=CP|-utf8] [-option[s]] HashType [HashType2 [...]] [ < wordfile ]
Options can be abbreviated!
HashType is one or more (space separated) from the following list:
[ @funcs ]
Multiple hashtypes are done one after the other. All sample words
are read from stdin or redirection of a wordfile
Default is to read and write files as binary, no conversions
-utf8 shortcut to -codepage=UTF-8.
-codepage=CP Read and write files in CP encoding.
Options are:
-minlen <n> Discard lines shorter than <n> characters (0)
-maxlen <n> Discard lines longer than <n> characters (128)
-count <n> Stop when we have produced <n> hashes (1320)
-salt <s> Force a single salt (only supported in a few formats)
-dictfile <s> Put name of dict file into the first line comment
-nocomment eliminate the first line comment
-help shows this help screen.
UsageHelp
}
if (@ARGV == 0) {
die usage();
}
if ($arg_utf8) { $arg_codepage="UTF-8"; }
#if not a redirected file, prompt the user
if (-t STDIN) {
print STDERR "\nEnter words to hash, one per line.\n";
print STDERR "When all entered ^D starts the processing.\n\n";
$arg_nocomment = 1; # we do not output 'comment' line if writing to stdout.
}
###############################################################################################
# modifications to character set used. This is to get pass_gen.pl working correctly
# with john's -utf8 switch. Also added is code to do max length of passwords.
###############################################################################################
if (defined $arg_codepage and length($arg_codepage)) {
binmode(STDIN,"encoding(:$arg_codepage)");
binmode(STDOUT,"encoding(:$arg_codepage)");
if (!$arg_nocomment) { printf("#!comment: Built with pass_gen.pl using -codepage-$arg_codepage mode, $arg_minlen to $arg_maxlen characters. dict file=$arg_dictfile\n"); }
} else {
binmode(STDIN,":raw");
binmode(STDOUT,":raw");
if (!$arg_nocomment) { printf("#!comment: Built with pass_gen.pl using RAW mode, $arg_minlen to $arg_maxlen characters dict file=$arg_dictfile\n"); }
}
###############################################################################################
###############################################################################################
#### Data Processing Loop. We read all candidates here, and send them to the proper hashing
#### function(s) to build into john valid input lines.
###############################################################################################
###############################################################################################
if (@ARGV == 1) {
# if only one format (how this script SHOULD be used), then we do not slurp the file, but we
# read STDIN line by line. Cuts down on memory usage GREATLY within the running of the script.
$u = 0;
my $orig_arg = lc (defined($_) ? $_ : '');
my $arg = lc $ARGV[0];
if ($arg eq "dynamic") { $arg = "dynamic="; }
if (substr($arg,0,8) eq "dynamic=") {
@funcs = ();
push(@funcs, $arg = dynamic_compile(substr($arg,8)));
}
foreach (@funcs) {
if ($arg eq lc $_) {
if (-t STDOUT) { print "\n ** Here are the hashes for format $orig_arg **\n"; }
while (<STDIN>) {
next if (/^#!comment/);
chomp;
s/\r$//; # strip CR for non-Windows
my $line_len = length($_);
next if $line_len > $arg_maxlen || $line_len < $arg_minlen;
no strict 'refs';
&$arg($_);
use strict;
++$u;
if ($u >= $arg_count) {
print STDERR "Got $arg_count, not processing more. Use -count to bump limit.\n";
last;
}
}
last;
}
}
} else {
#slurp the wordlist words from stdin. We have to, to be able to run the same words multiple
# times, and not interleave the format 'types' in the file. Doing this allows us to group them.
my @lines = <STDIN>;
foreach (@ARGV) {
$u = 0;
my $orig_arg = lc $_;
my $arg = lc $_;
if (substr($arg,0,8) eq "dynamic=") {
push(@funcs, $arg = dynamic_compile(substr($ARGV[0],8)));
}
foreach (@funcs) {
if ($arg eq lc $_) {
if (-t STDOUT) { print "\n ** Here are the hashes for format $orig_arg **\n"; }
foreach (@lines) {
next if (/^#!comment/);
chomp;
s/\r$//; # strip CR for non-Windows
my $line_len = length($_);
next if $line_len > $arg_maxlen || $line_len < $arg_minlen;
no strict 'refs';
&$arg($_);
use strict;
++$u;
last if $u >= $arg_count;
}
last;
}
}
}
}
#############################################################################
# used to get salts. Call with randstr(count[,array of valid chars] ); array is 'optional' Default is AsciiText (UPloCase, nums, _ )
#############################################################################
sub randstr
{
my @chr = defined($_[1]) ? @{$_[1]} : @chrAsciiTextNum;
my $s;
foreach (1..$_[0]) {
$s.=$chr[rand @chr];
}
return $s;
}
sub randbytes {
my $ret = "";
foreach(1 .. $_[0]) {
$ret .= chr(rand(256));
}
return $ret;
}
sub randusername {
my $num = shift;
my $user = $userNames[rand @userNames];
if (defined($num) && $num > 0) {
while (length($user) > $num) {
$user = $userNames[rand @userNames];
}
}
return $user;
}
# helper function needed by md5_a (or md5_1 if we were doing that one)
sub to64 #unsigned long v, int n)
{
my $str, my $n = $_[1], my $v = $_[0];
while (--$n >= 0) {
$str .= $i64[$v & 0x3F];
$v >>= 6;
}
return $str;
}
# helper function for nsldap and nsldaps
sub base64 {
my $ret = encode_base64($_[0]);
chomp $ret;
return $ret;
}
sub _crypt_to64 {
my $itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
my ($v, $n) = ($_[1], $_[2]);
while (--$n >= 0) {
$_[0] .= substr($itoa64, $v & 0x3f, 1);
$v >>= 6;
}
}
# used by drupal. Would also probably be used for phpass. dragonfly also uses something similar, but 'mixes'
sub base64i {
my $final = $_[0];
my $len = length $final;
my $mod = $len%3;
my $cnt = ($len-$mod)/3;
my $out = "";
my ($l, $p);
for ($i = 0; $i < $cnt; $i++) {
$l = (ord(substr($final, $i*3, 1))) | (ord(substr($final, $i*3+1, 1)) << 8) | (ord(substr($final, $i*3+2, 1))<<16);
_crypt_to64($out, $l, 4); $p += 4;
}
if ($mod == 2) { $l = ord(substr($final, $i*3, 1)) | (ord(substr($final, $i*3+1, 1)) << 8); _crypt_to64($out, $l, 4); }
if ($mod == 1) { $l = ord(substr($final, $i*3, 1)); _crypt_to64($out, $l, 4); }
return $out;
}
#sub ns_base64 {
# my $ret = "";
# my $n; my @ha = split(//,$h);
# for ($i = 0; $i <= $_[0]; ++$i) {
# # the first one gets some unitialized at times.
# #$n = ord($ha[$i*3+2]) | (ord($ha[$i*3+1])<<8) | (ord($ha[$i*3])<<16);
# $n = ord($ha[$i*3])<<16;
# if (@ha > $i*3+1) {$n |= (ord($ha[$i*3+1])<<8);}
# if (@ha > $i*3+2) {$n |= ord($ha[$i*3+2]);}
# $ret .= "$ns_i64[($n>>18)&0x3F]";
# if ($_[1] == 3 && $i == $_[0]) { $ret .= "="; }
# else {$ret .= "$ns_i64[($n>>12)&0x3F]"; }
# if ($_[1] > 1 && $i == $_[0]) { $ret .= "="; }
# else {$ret .= "$ns_i64[($n>>6)&0x3F]"; }
# if ($_[1] > 0 && $i == $_[0]) { $ret .= "="; }
# else {$ret .= "$ns_i64[$n&0x3F]"; }
# }
# return $ret;
#}
##helper function for ns
#sub ns_base64_2 {
# my $ret = "";
# my $n; my @ha = split(//,$h);
# for ($i = 0; $i < $_[0]; ++$i) {
# # the first one gets some unitialized at times.. Same as the fix in ns_base64
# #$n = ord($ha[$i*2+1]) | (ord($ha[$i*2])<<8);
# $n = ord($ha[$i*2])<<8;
# if (@ha > $i*2+1) { $n |= ord($ha[$i*2+1]); }
# $ret .= "$ns_i64[($n>>12)&0xF]";
# $ret .= "$ns_i64[($n>>6)&0x3F]";
# $ret .= "$ns_i64[$n&0x3F]";
# }
# return $ret;
#}
# helper function to convert binary to hex. Many formats store salts and such in hex
sub saltToHex {
my $ret = "";
my @sa = split(//,$salt);
for ($i = 0; $i < $_[0]; ++$i) {
$ret .= $chrHexLo[ord($sa[$i])>>4];
$ret .= $chrHexLo[ord($sa[$i])&0xF];
}
return $ret;
}
sub whirlpool_hex {
my $whirlpool = Digest->new('Whirlpool');
$whirlpool->add( $_[0] );
return $whirlpool->hexdigest;
}
sub whirlpool_base64 {
my $whirlpool = Digest->new('Whirlpool');
$whirlpool->add( $_[0] );
return $whirlpool->b64digest;
}
sub whirlpool {
my $ret;
my $i;
my $h = whirlpool_hex($_[0]);
for($i = 0; $i < 128; $i += 2) {
$ret .= chr(substr($h,$i,1)*16 + substr($h,$i,1)*1);
}
return $ret;
}
#############################################################################
# Here are the encryption subroutines.
# the format of ALL of these is: function(password)
# all salted formats choose 'random' salts, in one way or another.
#############################################################################
sub des {
if ($argsalt && length($argsalt)==2) {
$h = Authen::Passphrase::DESCrypt->new(passphrase => $_[0], salt_base64 => $argsalt);
} else {
$h = Authen::Passphrase::DESCrypt->new(passphrase => $_[0], salt_random => 12);
}
print "u$u-DES:", $h->as_crypt, ":$u:0:$_[0]::\n";
}
sub bigcrypt {
if (length($_[0]) > 8) {
$h = Authen::Passphrase::BigCrypt->new(passphrase => $_[0], salt_random => 12);
print "u$u-DES_BigCrypt:", $h->salt_base64_2, $h->hash_base64, ":$u:0:$_[0]::\n";
}
}
sub bsdi {
$h = Authen::Passphrase::DESCrypt->new(passphrase => $_[0], fold => 1, nrounds => 725, salt_random => 24);
print "u$u-BSDI:", $h->as_crypt, ":$u:0:$_[0]::\n";
}
sub md5_1 {
# if (length($_[0]) > 15) { print "Warning, john can only handle 15 byte passwords for this format!\n"; }
# $h = Authen::Passphrase::MD5Crypt->new(passphrase => $_[0], salt_random => 1);
# print "u$u-MD5:", $h->as_crypt, ":$u:0:$_[0]::\n";
if (length($_[0]) > 15) { print "Warning, john can only handle 15 byte passwords for this format!\n"; }
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(8); }
$h = md5_a_hash($_[0], $salt, "\$1\$");
print "u$u-MD5:$h:$u:0:$_[0]::\n";
}
sub bfx_fix_pass {
my $pass = $_[0];
my $i;
for ($i = 0; $i < length($pass); $i++) {
my $s = substr($pass, $i, 1);
last if (ord($s) >= 0x80);
}
if ($i == length($pass)) { return $pass; } # if no high bits set, then the error would NOT show up.
my $pass_ret = "";
# Ok, now do the logic from 'broken' BF_std_set_key().
# When we get to a 4 byte limb, that has (limb&0xFF) == 0, we return the accumlated string, minus that last null.
my $BF_word; my $ptr=0;
for ($i = 0; $i < 18; $i++) { # BF_Rounds is 16, so 16+2 is 18
$BF_word = 0;
for (my $j = 0; $j < 4; $j++) {
$BF_word <<= 8;
my $c;
if ($ptr < length($pass)) {
$c = substr($pass, $ptr, 1);
if (ord($c) > 0x80) {
$BF_word = 0xFFFFFF00;
}
$BF_word |= ord($c);
}
if ($ptr < length($pass)) { $ptr++; }
else { $ptr = 0; }
}
$pass_ret .= chr(($BF_word&0xFF000000)>>24);
$pass_ret .= chr(($BF_word&0x00FF0000)>>16);
$pass_ret .= chr(($BF_word&0x0000FF00)>>8);
if ( ($BF_word & 0xFF) == 0) {
# done (uncomment to see just 'what' the password is. i.e. the hex string of the password)
#print unpack("H*", $pass_ret) . "\n";
return $pass_ret;
}
$pass_ret .= chr($BF_word&0xFF);
}
}
sub bfx {
my $fixed_pass = bfx_fix_pass($_[0]);
if ($argsalt && length($argsalt)==16) {
$h = Authen::Passphrase::BlowfishCrypt->new(passphrase => $fixed_pass, cost => 5, salt => $argsalt);
}
else {
$h = Authen::Passphrase::BlowfishCrypt->new(passphrase => $fixed_pass, cost => 5, salt_random => 1);
}
my $hash_str = $h->as_crypt;
$hash_str =~ s/\$2a\$/\$2x\$/;
print "u$u-BF:", $hash_str, ":$u:0:$_[0]::\n";
}
sub bf {
if ($argsalt && length($argsalt)==16) {
$h = Authen::Passphrase::BlowfishCrypt->new(passphrase => $_[0], cost => 5, salt => $argsalt);
}
else {
$h = Authen::Passphrase::BlowfishCrypt->new(passphrase => $_[0], cost => 5, salt_random => 1);
}
print "u$u-BF:", $h->as_crypt, ":$u:0:$_[0]::\n";
}
sub bfegg {
if (length($_[0]) > 0) {
$h = Authen::Passphrase::EggdropBlowfish->new(passphrase => $_[0] );
print "u$u-BFegg:+", $h->hash_base64, ":$u:0:$_[0]::\n";
}
}
sub rawmd5 {
print "u$u-RawMD5:", md5_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub rawmd5u {
print "u$u-RawMD5-unicode:", md5_hex(encode("UTF-16LE",$_[0])), ":$u:0:$_[0]::\n";
}
sub rawsha1 {
print "u$u-RawSHA1:", sha1_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub rawsha1u {
print "u$u-RawSHA1-unicode:", sha1_hex(encode("UTF-16LE",$_[0])), ":$u:0:$_[0]::\n";
}
sub rawsha256 {
print "u$u-RawSHA256:", sha256_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub rawsha224 {
print "u$u-RawSHA224:", sha224_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub rawsha384 {
print "u$u-RawSHA384:", sha384_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub rawsha512 {
print "u$u-RawSHA512:", sha512_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub dragonfly3_32 {
$salt = randstr(rand(8)+1);
my $final = sha256($_[0]."\$3\$\0".$salt);
my $out = "";
my ($l, $p);
for ($i = 0; $i < 10; $i++) {
$l = ord(substr($final, $i, 1)) << 16 | ord(substr($final, $i + 11, 1)) << 8 | ord(substr($final, $i + 21, 1));
_crypt_to64($out, $l, 4); $p += 4;
}
$l = ord(substr($final, 10, 1)) << 16 | ord(substr($final, 31, 1)) << 8;
_crypt_to64($out, $l, 4);
print "u$u-dragonfly3_32:", "\$3\$$salt\$" . $out, ":$u:0:$_[0]::\n";
}
sub dragonfly4_32 {
$salt = randstr(rand(8)+1);
my $final = sha512($_[0]."\$4\$\0".$salt);
my $out = "";
my ($l, $p);
for ($i = 0; $i < 20; $i++) {
$l = ord(substr($final, $i, 1)) << 16 | ord(substr($final, $i + 21, 1)) << 8 | ord(substr($final, $i + 42, 1));
_crypt_to64($out, $l, 4); $p += 4;
}
$l = ord(substr($final, 20, 1)) << 16 | ord(substr($final, 41, 1)) << 8;
_crypt_to64($out, $l, 4);
print "u$u-dragonfly4_32:", "\$4\$$salt\$" . $out, ":$u:0:$_[0]::\n";
}
sub dragonfly3_64 {
$salt = randstr(rand(8)+1);
my $final = sha256($_[0]."\$3\$\0sha5".$salt);
my $out = "";
my ($l, $p);
for ($i = 0; $i < 10; $i++) {
$l = ord(substr($final, $i, 1)) << 16 | ord(substr($final, $i + 11, 1)) << 8 | ord(substr($final, $i + 21, 1));
_crypt_to64($out, $l, 4); $p += 4;
}
$l = ord(substr($final, 10, 1)) << 16 | ord(substr($final, 31, 1)) << 8;
_crypt_to64($out, $l, 4);
print "u$u-dragonfly3_64:", "\$3\$$salt\$" . $out, ":$u:0:$_[0]::\n";
}
sub dragonfly4_64 {
$salt = randstr(rand(8)+1);
my $final = sha512($_[0]."\$4\$\0/etc".$salt);
my $out = "";
my ($l, $p);
for ($i = 0; $i < 20; $i++) {
$l = ord(substr($final, $i, 1)) << 16 | ord(substr($final, $i + 21, 1)) << 8 | ord(substr($final, $i + 42, 1));
_crypt_to64($out, $l, 4); $p += 4;
}
$l = ord(substr($final, 20, 1)) << 16 | ord(substr($final, 41, 1)) << 8;
_crypt_to64($out, $l, 4);
print "u$u-dragonfly4_64:", "\$4\$$salt\$" . $out, ":$u:0:$_[0]::\n";
}
sub mscash {
if (defined $argsalt) { $salt = $argsalt; } else { $salt = randusername(19); }
print "$salt:", md4_hex(md4(encode("UTF-16LE",$_[0])).encode("UTF-16LE", lc($salt))),
":$u:0:$_[0]:mscash (uname is salt):\n";
}
sub mscash2 {
# max username (salt) length is supposed to be 19 characters (in John)
# max password length is 27 characters (in John)
# the algorithm lowercases the salt
my $user;
if (defined $argsalt) { $user = $argsalt; } else { $user = randusername(22); }
$salt = encode("UTF-16LE", lc($user));
my $pbkdf2 = Crypt::PBKDF2->new(
hash_class => 'HMACSHA1',
iterations => 10240,
output_len => 16,
salt_len => length($salt),
);
# Crypt::PBKDF2 hex output is buggy, we do it ourselves!
print "$user:", unpack("H*", $pbkdf2->PBKDF2($salt,md4(md4(encode("UTF-16LE",$_[0])).$salt))),
":$u:0:$_[0]:mscash2:\n";
}
sub lm {
my $s = $_[0];
if (length($s)>14) { $s = substr($s,14);}
$h = Authen::Passphrase::LANManager->new(passphrase => length($s) <= 14 ? $s : "");
print "u$u-LM:$u:", $h->hash_hex, ":$u:0:", uc $s, "::\n";
}
sub nt { #$utf8mode=0, $utf8_pass;
$h = Authen::Passphrase::NTHash->new(passphrase => $_[0]);
print "u$u-NT:\$NT\$", $h->hash_hex, ":$u:0:$_[0]::\n";
}
sub pwdump {
my $lm = Authen::Passphrase::LANManager->new(passphrase => length($_[0]) <= 14 ? $_[0] : "");
my $nt = Authen::Passphrase::NTHash->new(passphrase => $_[0]);
print "u$u-pwdump:$u:", $lm->hash_hex, ":", $nt->hash_hex, ":$_[0]::\n";
}
sub rawmd4 {
print "u$u-RawMD4:", md4_hex($_[0]), ":$u:0:$_[0]::\n";
}
sub mediawiki {
if (defined $argsalt) { $salt = $argsalt; } else { $salt = randstr(8); }
print "u$u-mediawiki:\$B\$" . $salt . "\$" . md5_hex($salt . "-" . md5_hex($_[0])) . ":$u:0:$_[0]::\n";
}
sub phpass {
$h = Authen::Passphrase::PHPass->new(cost => 11, salt_random => 1, passphrase => $_[0]);
print "u$u-PHPass:", $h->as_crypt, ":$u:0:$_[0]::\n";
}
sub po {
if (defined $argsalt) {
if ($argsalt.length() == 32) { $salt = $argsalt; }
else { $salt = md5_hex($argsalt); }
} else {
$salt=randstr(32, \@chrHexLo);
}
print "u$u-PO:", md5_hex($salt . "Y" . $_[0] . "\xF7" . $salt), "$salt:$u:0:$_[0]::\n";
}
sub _md5_crypt_to_64 {
my $c = $_[0];
my $i;
# MD5-a (or MD5-BSD and sunmd5), do a strange
# transposition and base-64 conversion. We do the same here, to get the same hash
$i = (ord(substr($c,0,1))<<16) | (ord(substr($c,6,1))<<8) | ord(substr($c,12,1));
my $tmp = to64($i,4);
$i = (ord(substr($c,1,1))<<16) | (ord(substr($c,7,1))<<8) | ord(substr($c,13,1));
$tmp .= to64($i,4);
$i = (ord(substr($c,2,1))<<16) | (ord(substr($c,8,1))<<8) | ord(substr($c,14,1));
$tmp .= to64($i,4);
$i = (ord(substr($c,3,1))<<16) | (ord(substr($c,9,1))<<8) | ord(substr($c,15,1));
$tmp .= to64($i,4);
$i = (ord(substr($c,4,1))<<16) | (ord(substr($c,10,1))<<8) | ord(substr($c,5,1));
$tmp .= to64($i,4);
$i = ord(substr($c,11,1));
$tmp .= to64($i,2);
return $tmp;
}
sub md5_a_hash {
# not 'native' in the Authen::MD5Crypt (but should be!!!)
# NOTE, this function is about 2.5x FASTER than Authen::MD5Crypt !!!!!
# have to use md5() function to get the 'raw' md5s, and do our 1000 loops.
# md5("a","b","c") == md5("abc");
my $b, my $c, my $tmp;
my $type = $_[2];
my $salt = $_[1];
#create $b
$b = md5($_[0],$salt,$_[0]);
#create $a
$tmp = $_[0] . $type . $salt; # if this is $1$ then we have 'normal' BSD MD5
for ($i = length($_[0]); $i > 0; $i -= 16) {
if ($i > 16) { $tmp .= $b; }
else { $tmp .= substr($b,0,$i); }
}
for ($i = length($_[0]); $i > 0; $i >>= 1) {
if ($i & 1) { $tmp .= "\x0"; }
else { $tmp .= substr($_[0],0,1); }
}
$c = md5($tmp);
# now we do 1000 iterations of md5.
for ($i = 0; $i < 1000; ++$i) {
if ($i&1) { $tmp = $_[0]; }
else { $tmp = $c; }
if ($i%3) { $tmp .= $salt; }
if ($i%7) { $tmp .= $_[0]; }
if ($i&1) { $tmp .= $c; }
else { $tmp .= $_[0]; }
$c = md5($tmp);
}
$tmp = _md5_crypt_to_64($c);
my $ret = "$type$salt\$$tmp";
return $ret;
}
sub md5_a {
if (length($_[0]) > 15) { print "Warning, john can only handle 15 byte passwords for this format!\n"; }
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(8); }
$h = md5_a_hash($_[0], $salt, "\$apr1\$");
print "u$u-md5a:$h:$u:0:$_[0]::\n";
}
#int md5bit(unsigned char *digest, int bit_num)
sub md5bit {
my $digest = $_[0]; my $bit_num=$_[1];
my $byte_off;
my $bit_off;
$bit_num %= 128;
$byte_off = $bit_num / 8;
$bit_off = $bit_num % 8;
my $b = ord(substr($digest, $byte_off, 1));
if ($b&(1<<$bit_off)) { return 1; }
return 0;
}
# F'n ugly function, but pretty much straight port from sunmd5 C code.
sub moffet_coinflip {
my $c = $_[0];
my $round = $_[1];
my $i;
my @shift_4; my @shift_7; my @indirect_4; my @indirect_7;
my $shift_a; my $shift_b;
my $indirect_a; my $indirect_b;
my $bit_a; my $bit_b;
for ($i = 0; $i < 16; $i++) {
my $j;
$j = ($i + 3) & 0xF;
$shift_4[$i] = ord(substr($c,$j,1)) % 5;
$shift_7[$i] = (ord(substr($c,$j,1)) >> (ord(substr($c,$i,1)) & 7)) & 0x01;
}
$shift_a = md5bit($c, $round);
$shift_b = md5bit($c, $round + 64);
for ($i = 0; $i < 16; $i++) {
$indirect_4[$i] = (ord(substr($c,$i,1)) >> $shift_4[$i]) & 0x0f;
}
for ($i = 0; $i < 16; $i++) {
$indirect_7[$i] = (ord(substr($c,$indirect_4[$i],1)) >> $shift_7[$i]) & 0x7f;
}
$indirect_a = $indirect_b = 0;
for ($i = 0; $i < 8; $i++) {
$indirect_a |= (md5bit($c, $indirect_7[$i]) << $i);
$indirect_b |= (md5bit($c, $indirect_7[$i + 8]) << $i);
}
$indirect_a = ($indirect_a >> $shift_a) & 0x7f;
$indirect_b = ($indirect_b >> $shift_b) & 0x7f;
$bit_a = md5bit($c, $indirect_a);
$bit_b = md5bit($c, $indirect_b);
return $bit_a ^ $bit_b;
}
sub _sunmd5_hash {
my $pw=$_[0];
my $salt = $_[1];
my $c = md5($pw,$salt);
my $i = 0;
while ($i < 5000) {
# compute coin flip
my $round = sprintf("%d", $i);
# now do md5 on this round
if (moffet_coinflip($c, $i)) {
$c = md5(
$c,
# this long constant string (AND the null trailing),
# need to be added, then the round's text number
"To be, or not to be,--that is the question:--\n",
"Whether 'tis nobler in the mind to suffer\n",
"The slings and arrows of outrageous fortune\n",
"Or to take arms against a sea of troubles,\n",
"And by opposing end them?--To die,--to sleep,--\n",
"No more; and by a sleep to say we end\n",
"The heartache, and the thousand natural shocks\n",
"That flesh is heir to,--'tis a consummation\n",
"Devoutly to be wish'd. To die,--to sleep;--\n",
"To sleep! perchance to dream:--ay, there's the rub;\n",
"For in that sleep of death what dreams may come,\n",
"When we have shuffled off this mortal coil,\n",
"Must give us pause: there's the respect\n",
"That makes calamity of so long life;\n",
"For who would bear the whips and scorns of time,\n",
"The oppressor's wrong, the proud man's contumely,\n",
"The pangs of despis'd love, the law's delay,\n",
"The insolence of office, and the spurns\n",
"That patient merit of the unworthy takes,\n",
"When he himself might his quietus make\n",
"With a bare bodkin? who would these fardels bear,\n",
"To grunt and sweat under a weary life,\n",
"But that the dread of something after death,--\n",
"The undiscover'd country, from whose bourn\n",
"No traveller returns,--puzzles the will,\n",
"And makes us rather bear those ills we have\n",
"Than fly to others that we know not of?\n",
"Thus conscience does make cowards of us all;\n",
"And thus the native hue of resolution\n",
"Is sicklied o'er with the pale cast of thought;\n",
"And enterprises of great pith and moment,\n",
"With this regard, their currents turn awry,\n",
"And lose the name of action.--Soft you now!\n",
"The fair Ophelia!--Nymph, in thy orisons\n",
"Be all my sins remember'd.\n\x0", # the NULL must be included.
$round);
} else {
$c = md5($c,$round);
}
$i++;
}
return $c;
}
sub sunmd5 {
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(16); }
$salt = "\$md5\$rounds=904\$".$salt;
my $c = _sunmd5_hash($_[0], $salt);
my $h = _md5_crypt_to_64($c);
print "u$u-sunmd5:$salt\$$h:$u:0:$_[0]::\n";
}
sub wow_srp {
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(16); }
my $usr = uc randusername();
my $h = sha1($salt, sha1($usr,":",uc $_[0]));
# turn $h into a hex, so we can load it into a BigInt
$h = "0x" . unpack("H*", $h);
# perform exponentation.
my $base = Math::BigInt->new(47);
my $exp = Math::BigInt->new($h);
my $mod = Math::BigInt->new("112624315653284427036559548610503669920632123929604336254260115573677366691719");
$h = $base->bmodpow($exp, $mod);
# convert h into upper cased hex (also salt gets converted into upcased hex)
$h = uc substr($h->as_hex(), 2);
print "u$u-wow_srp:\$WoWSRP\$$h\$", uc unpack("H*", $salt), "*$usr:$u:0:", uc $_[0], "::\n";
}
sub binToHex {
my $bin = shift;
my $ret = "";
my @sa = split(//,$bin);
for ($i = 0; $i < length($bin); ++$i) {
$ret .= $chrHexLo[ord($sa[$i])>>4];
$ret .= $chrHexLo[ord($sa[$i])&0xF];
}
return $ret;
}
sub _hmacmd5 {
my ($key, $data) = @_;
my $ipad; my $opad;
for ($i = 0; $i < length($key); ++$i) {
$ipad .= chr(ord(substr($key, $i, 1)) ^ 0x36);
$opad .= chr(ord(substr($key, $i, 1)) ^ 0x5C);
}
while ($i++ < 64) {
$ipad .= chr(0x36);
$opad .= chr(0x5C);
}
return md5($opad,md5($ipad,$data));
}
sub hmacmd5 {
# now uses _hmacmd5 instead of being done inline.
$salt = randstr(32);
my $bin = _hmacmd5($_[0], $salt);
print "u$u-hmacMD5:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub _hmac_shas {
my ($func, $pad_sz, $key, $data) = @_;
my $ipad; my $opad;
for ($i = 0; $i < length($key); ++$i) {
$ipad .= chr(ord(substr($key, $i, 1)) ^ 0x36);
$opad .= chr(ord(substr($key, $i, 1)) ^ 0x5C);
}
while ($i++ < $pad_sz) {
$ipad .= chr(0x36);
$opad .= chr(0x5C);
}
return $func->($opad,$func->($ipad,$data));
}
sub hmac_sha1 {
$salt = randstr(24);
my $bin = _hmac_shas(\&sha1, 64, $_[0], $salt);
print "u$u-hmacSHA1:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub hmac_sha224 {
$salt = randstr(32);
my $bin = _hmac_shas(\&sha224, 64, $_[0], $salt);
print "u$u-hmacSHA224:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub hmac_sha256 {
$salt = randstr(32);
my $bin = _hmac_shas(\&sha256, 64, $_[0], $salt);
print "u$u-hmacSHA256:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub hmac_sha384 {
$salt = randstr(32);
my $bin = _hmac_shas(\&sha384, 128, $_[0], $salt);
print "u$u-hmacSHA384:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub hmac_sha512 {
$salt = randstr(32);
my $bin = _hmac_shas(\&sha512, 128, $_[0], $salt);
print "u$u-hmacSHA512:$salt#", binToHex($bin), ":$u:0:$_[0]::\n";
}
sub _sha_crypts {
my $a; my $b, my $c, my $tmp; my $i; my $ds; my $dp; my $p; my $s;
my ($func, $bits, $key, $salt) = @_;
my $bytes = $bits/8;
$b = $func->($key.$salt.$key);
# Add for any character in the key one byte of the alternate sum.
$tmp = $key . $salt;
for ($i = length($key); $i > 0; $i -= $bytes) {
if ($i > $bytes) { $tmp .= $b; }
else { $tmp .= substr($b,0,$i); }
}
# Take the binary representation of the length of the key and for every 1 add the alternate sum, for every 0 the key.
for ($i = length($key); $i > 0; $i >>= 1) {
if (($i & 1) != 0) { $tmp .= $b; }
else { $tmp .= $key; }
}
$a = $func->($tmp);
# NOTE, this will be the 'initial' $c value in the inner loop.
# For every character in the password add the entire password. produces DP
$tmp = "";
for ($i = 0; $i < length($key); ++$i) {
$tmp .= $key;
}
$dp = $func->($tmp);
# Create byte sequence P.
$p = "";
for ($i = length($key); $i > 0; $i -= $bytes) {
if ($i > $bytes) { $p .= $dp; }
else { $p .= substr($dp,0,$i); }
}
# produce ds
$tmp = "";
my $til = 16 + ord(substr($a,0,1));
for ($i = 0; $i < $til; ++$i) {
$tmp .= $salt;
}
$ds = $func->($tmp);
# Create byte sequence S.
for ($i = length($salt); $i > 0; $i -= $bytes) {
if ($i > $bytes) { $s .= $ds; }
else { $s .= substr($ds,0,$i); }
}
$c = $a; # Ok, we saved this, which will 'seed' our crypt value here in the loop.
# now we do 5000 iterations of md5.
for ($i = 0; $i < 5000; ++$i) {
if ($i&1) { $tmp = $p; }
else { $tmp = $c; }
if ($i%3) { $tmp .= $s; }
if ($i%7) { $tmp .= $p; }
if ($i&1) { $tmp .= $c; }
else { $tmp .= $p; }
# printf ("%02d=" . unpack("H*", $tmp) . "\n", $i); # for debugging.
$c = $func->($tmp);
}
# printf ("F =" . unpack("H*", $c) . "\n"); # final value.
# $c now contains the 'proper' sha_X_crypt hash. However, a strange transposition and
# base-64 conversion. We do the same here, to get the same hash. sha256 and sha512 use
# a different key schedule. I have come up with a way to do this, that is not using a
# table, but using modular walking of the data, 3 values at a time.
# seel http://www.akkadia.org/drepper/SHA-crypt.txt for information
my $inc1; my $inc2; my $mod; my $end;
if ($bits==256) { $inc1=10;$inc2=21;$mod=30;$end=0; }
else { $inc1=21;$inc2=22;$mod=63;$end=21; }
$i = 0;
$tmp = "";
do {
$tmp .= to64((ord(substr($c,$i,1))<<16) | (ord(substr($c,($i+$inc1)%$mod,1))<<8) | ord(substr($c,($i+$inc1*2)%$mod,1)),4);
$i = ($i + $inc2) % $mod;
} while ($i != $end);
if ($bits==256) { $tmp .= to64((ord(substr($c,31,1))<<8) | ord(substr($c,30,1)),3); }
else { $tmp .= to64(ord(substr($c,63,1)),2); }
return $tmp;
}
sub sha256crypt {
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(16); }
my $bin = _sha_crypts(\&sha256, 256, $_[0], $salt);
print "u$u-sha256crypt:\$5\$$salt\$$bin:$u:0:$_[0]::\n";
}
sub sha512crypt {
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(16); }
my $bin = _sha_crypts(\&sha512, 512, $_[0], $salt);
print "u$u-sha512crypt:\$6\$$salt\$$bin:$u:0:$_[0]::\n";
}
sub xsha512 {
# simple 4 byte salted crypt. No seperator char, just raw hash. Also 'may' have $LION$. We altenate, and every other
# hash get $LION$ (all even ones)
if (defined $argsalt) { $salt = $argsalt; } else { $salt=randstr(4); }
print "u$u-XSHA512:";
if ($u&1) { print ("\$LION\$"); }
print "" . unpack("H*", $salt) . sha512_hex($salt . $_[0]) . ":$u:0:$_[0]::\n";
}
sub mskrb5 {
my $password = shift;
my $datestring = sprintf('20%02u%02u%02u%02u%02u%02uZ', rand(100), rand(12)+1, rand(31)+1, rand(24), rand(60), rand(60));
my $timestamp = randbytes(14) . $datestring . randbytes(7);
my $K = Authen::Passphrase::NTHash->new(passphrase => $password)->hash;
my $K1 = _hmacmd5($K, pack('N', 0x01000000));
my $K2 = _hmacmd5($K1, $timestamp);
my $K3 = _hmacmd5($K1, $K2);
my $encrypted = RC4($K3, $timestamp);
printf("%s:\$mskrb5\$\$\$%s\$%s:::%s:%s\n", "u$u-mskrb5", binToHex($K2), binToHex($encrypted), $password, $datestring);
}
sub ipb2 {
if (defined $argsalt) { $salt = $argsalt; } else { $salt = randstr(5); }
print "u$u-IPB2:\$IPB2\$", saltToHex(5);
print "\$", md5_hex(md5_hex($salt), md5_hex($_[0])), ":$u:0:$_[0]::\n";
}
sub phps {
if (defined $argsalt) { $salt = $argsalt; } else { $salt = randstr(3); }
print "u$u-PHPS:\$PHPS\$", saltToHex(3);