mirror of
https://github.com/openwall/john.git
synced 2026-12-03 18:45:21 +07:00
[GH-ISSUE #1108] Format valid problem by strtok() #689
Labels
No labels
bug
core branch
documentation
enhancement
enhancement
fixed - pending verify
fuzz bug
good first issue
invalid
invalid
maintenance/cleanup
new cracking mode
new format
new tool
non-trivial
notes/external issues
portability
question
RFC / discussion
testing
WONTFIX
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openwall/john#689
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @loverszhaokai on GitHub (Mar 16, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1108
Some of the formats valid by strtok(), such as src/agilekeychain_fmt_plug.c
Here is a valid format:
Here is another valid format:
(This case can pass the valid(), but I think it's not valid, for there are too many '*')
The problem is caused by strtok(), some formats use it in valid(). Does it matter?
@jfoug commented on GitHub (Mar 16, 2015):
This has been a bug IMHO in strtok since the 70's. Write a correct one, which returns "" (not null) for multiple.
Here is code that we 'could' use to replace strtok. This one does NOT merge adjacent. It is 100% same interface as strtok (with ALL of the bugs of that function, such as corruption of input string, having a static pointer inside the function, etc). But it is a replacement that 'works'.
It would be easy to put strtokm() into misc.[ch], and then modify all the code in jtr to use this function.
@jfoug commented on GitHub (Mar 16, 2015):
Here is the output from the above program:
@loverszhaokai commented on GitHub (Mar 16, 2015):
@jfoug Thanks. strtokm() can help to avoid this issue. Should we change strtok() -> strtokm() ?
@jfoug commented on GitHub (Mar 16, 2015):
Let me check this in. Should be a trivial change.
@jfoug commented on GitHub (Mar 16, 2015):
Note, code (and output changed). I was stripping off leading empty tokens. That code is removed, the check strings chagned, and output redone.
@magnumripper commented on GitHub (Mar 16, 2015):
There's a lot of formats using strtok but we could change them with some ninja one-liner (and obviously carefully review - as well as test - the changes before committing).
@jfoug commented on GitHub (Mar 16, 2015):
I will be putting this into misc.[ch] In misc.h I will simply add this define (for now)
#define strtok(a,b) strtokm(a,b)
This allows us to do testing, to make sure things are 'happy', prior to changing a couple hundred lines of code.
@jfoug commented on GitHub (Mar 16, 2015):
I have not tested yet, I wrote this code outside of JtR, so it needs testing. The 1 liner will localize changes until we are 'happy', also, simply comment that line out and all changes are gone, and we are back to the CRTL strtok.
@jfoug commented on GitHub (Mar 16, 2015):
Very early testing (just -test=0 stuff for non GPU)
The promiscuous valids are almost certainly bugs that changing the strtok has flushed out. The others may also be bugs. Note, opencl/cuda formats were not checked.
@magnumripper commented on GitHub (Mar 16, 2015):
Actually these might be false positives from the "promiscuous valid" test. We might need to re-write it.
@jfoug commented on GitHub (Mar 16, 2015):
It is the one that adds $$$
Check out
343b1b3The !len is the promiscuous valid. I also added a ishex(p) to validate the hex string.@jfoug commented on GitHub (Mar 16, 2015):
django-scrypt is because in salt, we move pointer from scrypt$..... to $..... and the new code does not skip the leading delimiters. We just need to skip that delimiter also.
I am 90% sure that ALL issues can easily be corrected. They will be items like this.
@jfoug commented on GitHub (Mar 16, 2015):
@magnumripper Question:
I changed code to return empty for any leading delims, and any trailing delims. But we are seeing problems, because there was code written to work with this 'buggy' behavior.
example is gpg (in valid and get_salt). Here is example)
So cp starts out pointing at the '*' char. Since I changed strtokm to tokenize leading items, the way to 'fix' this is to either skip leading delims (causing strange behavior IMHO in the strtokm), OR to fix the code to skip the delim.
My vote is to change the format. BUT this is a change in interface between strtokm and strtok.
I am open to suggestions.
@jfoug commented on GitHub (Mar 16, 2015):
the problem in skey is a little more involved. it "MAY" need original strtok. There is a 1 space gap, then a 2 space gap. Now, this is just in the test strings, BUT it may be that way for real!!
@jfoug commented on GitHub (Mar 16, 2015):
I have done tiny 'patches' to all formats listed above. For the SKEY format, I am going to use strtok, that combines the delimiters. I do not know the 'real' layout of the format, so we should use the original function here, as that buggy behavior 'may' be right in this case, since we are dealing with white space.
@jfoug commented on GitHub (Mar 16, 2015):
NOTE, all the tiny changes I have made (and will be checking in), work 100% fine with native strtok. But they were needed for the small behavior changes done within strtokm. The biggest 'nit' happens to be that skip leading delimiters. We have a lot of code that depended upon that 'buggy' (IMHO) behavior.
@jfoug commented on GitHub (Mar 16, 2015):
@magnumripper could there be any files that do not include 'misc.h' that do a strtok??
Almost all are _fmt_plug.c but there are other usage outside of those. Is there any 'easy' way to check to see if misc.h gets included in all of these files? I think I am ready to check in the strtokm() AND since it is just a #define, and we have changed no source code to use it (outside of the define), then it should be fully safe. If we see any issues, comment out one header line, and rebuild.
But finding a way to easily tell if all usages are using strtokm() it would be helpful.
@magnumripper commented on GitHub (Mar 16, 2015):
Easily answered with a one-liner[tm]
@magnumripper commented on GitHub (Mar 16, 2015):
A good number of @kholia's early formats had this bug (an unneeded first * after the
tag) and Solar had to nag about it for quite some time before Dhiru stopped doing that. As we probably do not want to change the format syntax, me thinks we should just consider it as the tag being an actual$gpg$*. So the code snippet you refer to would just have +6 instead of +5. Would this not solve it without any ill effects?@jfoug commented on GitHub (Mar 16, 2015):
I did exactly that. Only a handful. I really hate to suck up the leading delims. I see that as a side affect of the poor design of merging the delims (same for trailing). I have left my code as it is, and it will give you a token (possibly empty) for EVERY delim in the string.
I have checked in all files that 'required' to be changed. I have not checked in misc.[ch] which actually have the strtokm() function in it (yet).
@jfoug commented on GitHub (Mar 16, 2015):
But that does not account for other things that include misc.h But it is a good start. I can put an #error in there, and if I compile these files, if they do not error, then I know that they are using the native strtok() until we change code to actually 'force' use strtokm() i.e. they would not see the macro.
@magnumripper commented on GitHub (Mar 16, 2015):
True. The main thing that in turn includes misc.h, is formats.h.
More one-liner-fu:
Still not recursive though 😄 but I think this is very close to the true list. For some reason it's totally different from the original list though?!
@magnumripper commented on GitHub (Mar 16, 2015):
This one should be safe. No format needs a fix.
@loverszhaokai commented on GitHub (Mar 17, 2015):
@jfoug thank you very much for your reply and commits on the strtok() problem. You have made some work on use strtokm() instead of strtok(). I think it's great for there will be less bugs. I will pay attention to the changes of strtok().
@magnumripper commented on GitHub (Mar 18, 2015):
@jfoug you never committed the strtokm(), did you?
@jfoug commented on GitHub (Mar 18, 2015):
2f7c5e4adds it@jfoug commented on GitHub (Mar 18, 2015):
NOTE, this is all done with a #define. We should probably go into code and switch the strtok calls to strtokm at some time. There was ONE location where original strtok IS being used. That is in the SKEY format. There is a pair of spaces at one point. We 'could' write code to handle this, BUT I am not 100% sure that it is always 2 bytes. It may be 1, 2, or X. Not having native input strings makes it sort of hard to do. But this IS the case where original strtok shines. It does not care how many delims are there, it will merge any arbitrary amount > 0
@jfoug commented on GitHub (Mar 18, 2015):
However, I do feel that the strtokm function IS the function that strtok should have been from the start. Or it might have been good to have a param that controlled how strtok handled leading, trailing and adjacent tokens. I know that strtok is one of my favorite functions, but it is UNUSABLE for many very common and useful file formats, such as CSV and that really is what many of the JTR input strings are (CSV but using $ or * instead of a tab or comma).
@loverszhaokai commented on GitHub (Mar 18, 2015):
@jfoug I think there are two valid() functions should be changed to work with strtokm()
opencl_strip_fmt_plug.c
opencl_gpg_fmt_plug.c
@magnumripper commented on GitHub (Mar 18, 2015):
Those and blockchain-opencl. I have fixed them, committing soon.
@magnumripper commented on GitHub (Mar 18, 2015):
So can we close this now?
@jfoug commented on GitHub (Mar 18, 2015):
It would be nice to convert all strtok() into strtokm() except for the few instances where we WANT strtok() behavior. But as far as this but, we now 'have' a correct strtok function that properly tokenizes 'optional' fields, ie adjacent delimiters. Also, if there are a block of multiple delimiters, it will not pass valid with the new strtok, if there are not supposed to be multiples.
@magnumripper commented on GitHub (Mar 18, 2015):
I forgot that! Fixed in
743f3d0. It was only SKEY, right? That was the only "undef" I found.@jfoug commented on GitHub (Mar 18, 2015):
SKEY was the only format I found that I 'think' may need the strtok logic. It also could be that the hashes we had were typed in by hand by someone, and the 2 space is a fat finger problem. I really do NOT know. If I KNEW that it was 2 char spaces, then it is easy. But if it is 1, 2 or X spaces, then it is a little more complex, but can work either way, it is just simpler with the 'lossy' method of strtok. strtok was actually written TO parse arbitrary text into tokens, with variable spacing and stuff. It is great for that (and does ltrim and rtrim also). But for truely tokenized data like CSV type stuff, it is no good.
But I saw no other formats where the dupe delimiters being smashed was correct logic, so yes, for all other strtok's, I 'think' strtokm is the correct function
@jfoug commented on GitHub (Mar 18, 2015):
NOTE, if we are using strtok for reading things like word lists, or other input files, that are NOT input hashes, then we might want to look at that code, instead of simply doing a global search/replace. However, since I used the #define to change them all, it should have caught anything, OTHER than the fact that we really do not have coverage QA testing, other than running the TS, which misses a LOT of code. So, I think the formats are solid (with SKEY still wanting old strtok), but we should make sure strtokm() IS the correct thing for the other parts of JtR.
@magnumripper commented on GitHub (Mar 18, 2015):
I only replace "all" strtok's in formats. We need to carefully review the rest, or leave them alone.
@magnumripper commented on GitHub (Mar 18, 2015):
Hmm the PDF format segfaults now, on the build bots. I wonder why. They did not segfault with that #define in place!
@magnumripper commented on GitHub (Mar 18, 2015):
Oh, it segfaults on my gear too now.
Problem was a
strtok (that did not get replaced due to the space, and since it used NULL but the preceeding one was nowstrtokm(, well, no wonder it crashed and burned.I checked for other instances of that, nothing more.