[GH-ISSUE #2138] Byte Order Mark for UTF8 dictionary files fouls up first word #1428

Closed
opened 2026-10-15 04:16:49 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @crioux on GitHub (May 18, 2016).
Original GitHub issue: https://github.com/openwall/john/issues/2138

We aren't stripping off the byte order mark on utf-8 dictionary files, resulting in the first word of a wordlist attack not working right.

Here's a patch that fixes that from my jtrdll fork.

diff --git a/src/wordlist.c b/src/wordlist.c
index 7ee427d..d1e7fe1 100644
--- a/src/wordlist.c
+++ b/src/wordlist.c
@@ -117,6 +117,24 @@ static char *mem_map, *map_pos, *map_end, *map_scan_end;
 static char *word_file_str, **words;
 static int64_t nWordFileLines;

+static inline clean_bom(char *line)
+{
+       if (options.input_enc == UTF_8)
+       {
+               static int checkbomfirst = 1;
+
+               /* Remove UTF-8 BOM from first word */
+               if (checkbomfirst)
+               {
+                       if (line_number == 0 && strlen(line) > 3 && ((unsigned char *)line)[0] == 0xEF && ((unsigned char *)line)[1] == 0xBB && ((unsigned char *)line)[2] == 0xBF)
+                       {
+                               memmove(line, line + 3, strlen(line) - 2);
+                       }
+                       checkbomfirst = 0;
+               }
+       }
+}
+
 static void save_state(FILE *file)
 {
        fprintf(file, "%d\n" LLd "\n" LLd "\n",
@@ -1240,6 +1258,8 @@ REDO_AFTER_LMLOOP:
 #else
                        strcpy(line, words[line_number]);
 #endif
+                       clean_bom(line);
+
                        line_number++;

                        if ((word = apply(line, rule, -1, last))) {
@@ -1291,6 +1311,9 @@ REDO_AFTER_LMLOOP:
                else if (rule)
                while (mem_map ? mgetl(line) :
                       fgetl(line, LINE_BUFFER_SIZE, word_file)) {
+
+                       clean_bom(line);
+
                        line_number++;

                        if (line[0] != '#') {
Originally created by @crioux on GitHub (May 18, 2016). Original GitHub issue: https://github.com/openwall/john/issues/2138 We aren't stripping off the byte order mark on utf-8 dictionary files, resulting in the first word of a wordlist attack not working right. Here's a patch that fixes that from my jtrdll fork. ``` diff --git a/src/wordlist.c b/src/wordlist.c index 7ee427d..d1e7fe1 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -117,6 +117,24 @@ static char *mem_map, *map_pos, *map_end, *map_scan_end; static char *word_file_str, **words; static int64_t nWordFileLines; +static inline clean_bom(char *line) +{ + if (options.input_enc == UTF_8) + { + static int checkbomfirst = 1; + + /* Remove UTF-8 BOM from first word */ + if (checkbomfirst) + { + if (line_number == 0 && strlen(line) > 3 && ((unsigned char *)line)[0] == 0xEF && ((unsigned char *)line)[1] == 0xBB && ((unsigned char *)line)[2] == 0xBF) + { + memmove(line, line + 3, strlen(line) - 2); + } + checkbomfirst = 0; + } + } +} + static void save_state(FILE *file) { fprintf(file, "%d\n" LLd "\n" LLd "\n", @@ -1240,6 +1258,8 @@ REDO_AFTER_LMLOOP: #else strcpy(line, words[line_number]); #endif + clean_bom(line); + line_number++; if ((word = apply(line, rule, -1, last))) { @@ -1291,6 +1311,9 @@ REDO_AFTER_LMLOOP: else if (rule) while (mem_map ? mgetl(line) : fgetl(line, LINE_BUFFER_SIZE, word_file)) { + + clean_bom(line); + line_number++; if (line[0] != '#') { ```
Author
Owner

@magnumripper commented on GitHub (May 18, 2016):

Good point. I really hate UTF-8 BOMs, they are the worst idea ever. If a user concatenates several such files into one larger, there will be BOMs scattered all over so it wont suffice to just check the first line. But then it will harm performance even more 😢

<!-- gh-comment-id:220169396 --> @magnumripper commented on GitHub (May 18, 2016): Good point. I really hate UTF-8 BOMs, they are the worst idea ever. If a user concatenates several such files into one larger, there will be BOMs scattered all over so it wont suffice to just check the first line. But then it will harm performance even more 😢
Author
Owner

@jfoug commented on GitHub (May 24, 2016):

The BOM logic should only check the first line (default), and then have a flag to check each line.

Yes, I know you hate BOM's, but for real files, they ARE the best thing there is, especially when you get to unicode/UTF8 (or other binaries) encoding, and BE vs LE systems. It is the user the concats these files that is the problem. WOrst thing would be to concat files generated by BE and LE into 1 file. In that case, we really would need to read the BOM's, and handle the endianity ourselves within each section of the input file. Yup, ugly, but it is the user doing this (or whatever fool simply cat'd the files in the first place).

<!-- gh-comment-id:221329865 --> @jfoug commented on GitHub (May 24, 2016): The BOM logic should only check the first line (default), and then have a flag to check each line. Yes, I know you hate BOM's, but for real files, they ARE the best thing there is, especially when you get to unicode/UTF8 (or other binaries) encoding, and BE vs LE systems. It is the user the concats these files that is the problem. WOrst thing would be to concat files generated by BE and LE into 1 file. In that case, we really would need to read the BOM's, and handle the endianity ourselves within each section of the input file. Yup, ugly, but it is the user doing this (or whatever fool simply cat'd the files in the first place).
Author
Owner

@crioux commented on GitHub (May 24, 2016):

Yea, the only thing i'm handling with this right now is utf-8, with which there is no BE or LE issues, only one BOM. I don't think our wordlist code could deal with utf-16 or 32 anyway at this point, BOM or not.

And yea, concatenating the wrong way without respecting the BOM is bad form, but to a certain extent, not worrying about that is reasonable, as that would be an 'invalid' file format anyway. JtR should be able to handle the 'valid' utf-8 files for certain, which is what this patch is about.

<!-- gh-comment-id:221353738 --> @crioux commented on GitHub (May 24, 2016): Yea, the only thing i'm handling with this right now is utf-8, with which there is no BE or LE issues, only one BOM. I don't think our wordlist code could deal with utf-16 or 32 anyway at this point, BOM or not. And yea, concatenating the wrong way without respecting the BOM is bad form, but to a certain extent, not worrying about that is reasonable, as that would be an 'invalid' file format anyway. JtR should be able to handle the 'valid' utf-8 files for certain, which is what this patch is about.
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#1428
No description provided.