Skip to content

Commit 5f4fafe

Browse files
committed
Iterated-sha1: Don't accept raw ciphertexts except XSHA
Bare XSHA need to 48 chars of uppercase hex. These will end up in john.pot as-is (like the old XSHA format).
1 parent 23bcda7 commit 5f4fafe

File tree

4 files changed

+37
-46
lines changed

4 files changed

+37
-46
lines changed

src/iterated_sha1_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ typedef struct {
3737

3838
extern struct fmt_tests iterated_sha1_tests[];
3939

40-
extern char *iterated_sha1_prepare(char *fields[10], struct fmt_main *self);
4140
extern int iterated_sha1_valid(char *ciphertext, struct fmt_main *self);
4241
extern void* iterated_sha1_get_binary(char* ciphertext);
4342
extern void* iterated_sha1_get_salt(char* ciphertext);

src/iterated_sha1_common_plug.c

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct fmt_tests iterated_sha1_tests[] = {
2525
// 8 bytes salt, 1024 iterations
2626
{"$sisha1$1024$6f77746f6f77746f5fa823ad3c2dc9b58893df73d52b2108b2efce45", "magnum"},
2727
{"$sisha1$1024$6a6f686e72697070a3a2baadacf154dca88a9ea31400481748e253bb", "password"},
28+
{"$sisha1$1024$6a6f686e72697070a48cd538757a88deaf12b93f4758e27017852ba3", "John the Ripper"},
2829
// 6 bytes salt, 512 iterations
2930
{"$sisha1$512$cafe80babe000cd885f153e249671f703039a5dce8a4ad771175", "ripper"},
3031
// 3 bytes salt, 2 iterations
@@ -33,46 +34,29 @@ struct fmt_tests iterated_sha1_tests[] = {
3334
{"$sisha1$1$73616c74d46dd115de9a2f3bf32d42b38d1b437e5f8b92a7", "clear"},
3435
// Raw SHA-1 (just for testing)
3536
{"$sisha1$1$2fbf0eba37de1d1d633bc1ed943b907f9b360d4c", "azertyuiop1"},
36-
// Raw ciphertexts:
37-
// 1024 iterations (default)
38-
{"6a6f686e72697070a48cd538757a88deaf12b93f4758e27017852ba3", "John the Ripper"},
39-
// XSHA: 4 bytes salt, 1 iteration (implicit for length 48 only)
40-
{"474379622bd7b9f84bd6e4bb52abf9d01705efb0a2426655", "passWOrd"},
37+
38+
// XSHA: uppercase, 4 bytes salt, 1 iteration (implicit for length 48 only)
39+
{"474379622BD7B9F84BD6E4BB52ABF9D01705EFB0A2426655", "passWOrd"},
4140
{NULL}
4241
};
4342

4443
/*
45-
* Convert raw ciphertext to canonical ciphertext with inferred salt length
46-
* and 1024 iterations (with exception for XSHA).
44+
* $sisha1$<iter>$<hex_salt><hex_hash>
45+
*
46+
* No separator between salt and hash.
4747
*/
48-
char *iterated_sha1_prepare(char *fields[10], struct fmt_main *self)
48+
int iterated_sha1_valid(char *ciphertext, struct fmt_main *self)
4949
{
50-
static char out[FORMAT_TAG_LEN + 4 + 1 + 2 * 8 + 40 + 1];
51-
52-
if (!strncasecmp(fields[1], FORMAT_TAG, FORMAT_TAG_LEN))
53-
return fields[1];
50+
if (strncasecmp(ciphertext, FORMAT_TAG, FORMAT_TAG_LEN)) {
51+
/* Handle XSHA, untagged 48 chars uc hex */
52+
int extra;
5453

55-
int len = strnlen(fields[1], 56 + 1);
56-
int iter, extra;
57-
58-
if (len < MIN_CIPHERTEXT_LEN || len > MAX_CIPHERTEXT_LEN)
59-
return fields[1];
60-
if (hexlenl(fields[1], &extra) != len || extra)
61-
return fields[1];
62-
63-
if (len == 48) // XSHA
64-
iter = 1;
65-
else
66-
iter = 1024;
67-
68-
sprintf(out, "%s%d$%s", FORMAT_TAG, iter, fields[1]);
69-
return out;
70-
}
54+
if (strnlen(ciphertext, 48 + 1) == 48 && hexlenu(ciphertext, &extra) == 48 && !extra)
55+
return 1;
7156

72-
int iterated_sha1_valid(char *ciphertext, struct fmt_main *self)
73-
{
74-
if (strncasecmp(ciphertext, FORMAT_TAG, FORMAT_TAG_LEN))
7557
return 0;
58+
}
59+
7660
ciphertext += FORMAT_TAG_LEN;
7761

7862
int iter = getdec(ciphertext, '$');
@@ -82,11 +66,11 @@ int iterated_sha1_valid(char *ciphertext, struct fmt_main *self)
8266
ciphertext = strchr(ciphertext, '$') + 1;
8367

8468
int len = strnlen(ciphertext, MAX_CIPHERTEXT_LEN + 1);
85-
if (len & 1 || len > MAX_CIPHERTEXT_LEN)
69+
if (len & 1 || len < MIN_CIPHERTEXT_LEN || len > MAX_CIPHERTEXT_LEN)
8670
return 0;
8771

8872
int extra;
89-
if (hexlenl(ciphertext, &extra) < MIN_CIPHERTEXT_LEN || extra)
73+
if (hexlenl(ciphertext, &extra) != len || extra)
9074
return 0;
9175

9276
return 1;
@@ -95,8 +79,10 @@ int iterated_sha1_valid(char *ciphertext, struct fmt_main *self)
9579
void* iterated_sha1_get_binary(char* ciphertext)
9680
{
9781
static uint8_t binary[BINARY_SIZE];
82+
int len = strlen(ciphertext);
83+
84+
ciphertext += len - 2 * BINARY_SIZE;
9885

99-
ciphertext += strlen(ciphertext) - 2 * BINARY_SIZE;
10086
base64_convert(ciphertext, e_b64_hex, 2 * BINARY_SIZE, binary, e_b64_raw, BINARY_SIZE, flg_Base64_DONOT_NULL_TERMINATE, 0);
10187

10288
#if defined(SIMD_COEF_32) && ARCH_LITTLE_ENDIAN
@@ -112,10 +98,12 @@ void* iterated_sha1_get_salt(char* ciphertext)
11298

11399
memset(&salt_blob, 0, sizeof(salt_blob));
114100

115-
ciphertext += FORMAT_TAG_LEN;
116-
117-
salt_blob.iter = atoi(ciphertext);
118-
ciphertext = strchr(ciphertext, '$') + 1;
101+
if (!strncasecmp(ciphertext, FORMAT_TAG, FORMAT_TAG_LEN)) {
102+
ciphertext += FORMAT_TAG_LEN;
103+
salt_blob.iter = atoi(ciphertext);
104+
ciphertext = strchr(ciphertext, '$') + 1;
105+
} else /* XSHA */
106+
salt_blob.iter = 1;
119107

120108
char* bin = ciphertext + strlen(ciphertext) - 40;
121109
salt_blob.len = (bin - ciphertext) / 2;

src/iterated_sha1_fmt_plug.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,16 @@ struct fmt_main FMT_STRUCT = {
231231
MAX_KEYS_PER_CRYPT,
232232
FMT_CASE | FMT_8_BIT | FMT_OMP | FMT_OMP_BAD,
233233
{ "iterations" },
234-
{ FORMAT_TAG },
234+
{
235+
FORMAT_TAG,
236+
""
237+
},
235238
iterated_sha1_tests
236239
}, {
237240
init,
238241
done,
239242
fmt_default_reset,
240-
iterated_sha1_prepare,
243+
fmt_default_prepare,
241244
iterated_sha1_valid,
242245
fmt_default_split,
243246
iterated_sha1_get_binary,

src/opencl_iterated_sha1_fmt_plug.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,7 @@ static void auto_tune(struct db_main *db, long double kernel_run_ms)
783783
tune_gws = 0;
784784

785785
/* Auto tune start.*/
786-
char *fields[10];
787-
fields[1]= fmt_opencl_sha1_iterated.params.tests[0].ciphertext;
788-
char *ciphertext = iterated_sha1_prepare(fields, &fmt_opencl_sha1_iterated);
786+
char *ciphertext = fmt_opencl_sha1_iterated.params.tests[0].ciphertext;
789787
set_salt(iterated_sha1_get_salt(ciphertext));
790788
pcount = gws_init;
791789
count = 0;
@@ -937,13 +935,16 @@ struct fmt_main FMT_STRUCT = {
937935
MAX_KEYS_PER_CRYPT,
938936
FMT_CASE | FMT_8_BIT | FMT_REMOVE | FMT_MASK,
939937
{ "iterations" },
940-
{ FORMAT_TAG },
938+
{
939+
FORMAT_TAG,
940+
""
941+
},
941942
iterated_sha1_tests
942943
}, {
943944
init,
944945
done,
945946
reset,
946-
iterated_sha1_prepare,
947+
fmt_default_prepare,
947948
iterated_sha1_valid,
948949
fmt_default_split,
949950
iterated_sha1_get_binary,

0 commit comments

Comments
 (0)