mirror of
https://github.com/openwall/john.git
synced 2026-12-03 18:45:21 +07:00
[GH-ISSUE #752] Add support for dynamic salt [compare] size #387
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#387
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 @magnumripper on GitHub (Sep 23, 2014).
Original GitHub issue: https://github.com/openwall/john/issues/752
Originally assigned to: @jfoug on GitHub.
http://www.openwall.com/lists/john-users/2014/07/06/1
See also #736. The pending functionality in OldOffice (as well as in RAR and ZIP, which currently have hacks in cracker.c) need a salt compare size different from the salt copy size.
Solar's suggestion fixes that as well as opens up for dynamically allocated salts (that can also be freed):
Have a flag like FMT_DYNAMIC_SALT that would tell the rest of JtR that salt() returns a dynamically allocated salt of arbitrary size and encodes the actual size in first size_t of the returned buffer. (...) Since we intend to use this for cases where salts are large, I think the dynamic allocation should be made in a way that can be free()d, and the rest of JtR should actually be free()ing these as appropriate
@magnumripper commented on GitHub (Sep 23, 2014):
We'd need to encode the salt compare size separately too, in a second size_t.
@magnumripper commented on GitHub (Sep 23, 2014):
For RAR and ZIP, we could do without this, and use SALT_SIZE as salt compare size. But for OldOffice, we'll need to be able to change the compare size on the fly, for any given salt.
@magnumripper commented on GitHub (Sep 23, 2014):
No, wait. We can do that anyway. We just change self->params.salt_size! So Solar's suggestion is good as-is.
@magnumripper commented on GitHub (Sep 23, 2014):
Hmm but that is not per salt. I need to think this over.
@jfoug commented on GitHub (Oct 2, 2014):
typedef struct dyna_salt_t {
size_t salt_cmp_size;
unsigned char buffer[1];
} dyna_salt;
#define DYNA_SALT_SIZE sizeof(dyna_salt*)
then simply set the salt_size in the format structure to be DYNA_SALT_SIZE
End of problem.
For a custom salt, all you need is a structure that has the first element be a size_t. allocate one of these structures, whatever 'shape' they are. They can be ANYTHING, as long as they have a size_t item FIRST in the structure, that is filled in. The size_t can be unique to each salt ITEM, not fixed for the class.
Then in the salt() function, allocate your struct, fill it in, and return a pointer to that structure. John core will treat it as a pointer of sizeof(pointer) copy the data. It can dereference that pointer to get the size, which will be the first item of a structure. Easy-peasy.
@jfoug commented on GitHub (Oct 2, 2014):
Actually, all salts could be done this way, BUT that would take work on each salt. For formats that have a fixed size salt, this is not a gain. jtr core takes same amount of code to do it the way it is today, or this new way. But for some formats (old-office, dynamic) it makes a LOT of sense. I went to a LOT of trouble with salts in dyna, doing about the same thing. I always return a pointer. But I did add other logic, and got some very useful gain from it. The code i did certainly made things like the regen-lost-salts code easier to do. That is why it is ONLY in dyna formats at this time. But I could have done it if dyna was using a 'dyna_salt' structure method also. I doubt I will change dyna to use a dyna_salt structure pointer, but I could 'look' at it. There is about 1000 to 1500 lines of code in the salt handing. I have to deal with everything. salt hashing, uniqueing, etc, etc. A lot of tricky ugly code that I do not understand all that well any more, since it has been about 2 to 3 years since I have looked at it. But to undo it and change to another method is not super trivial either, and the re-gen salts code would have to be pretty much redone.
@jfoug commented on GitHub (Oct 2, 2014):
Note, this is very similar to how MS does API functions that use binary structures and expect to be changed over time. You build a struct where the first item is a sizeof, then allocation a structure and set that element to sizeof the structure. Then when the API function is called, the sizeof value which is a fixed item in the structure can tell the API code what structure signature to use when processing. So there might be 4 versions of the structure, but the newest API dll can handle all versions. Some program that was built in 2009 that uses interface #1 still works, even though there are 3 updates to that structure since then. It is a very slick way to do things, keeping old code working, while extending functionality. This is a little different in usage, BUT can achieve same end result.
I ccan take on this task ,if you like. Hmm, I see that you can not have a issue assigned to 2 ppl. Too bad. I can take it, or you can keep it. Your call.
@magnumripper commented on GitHub (Oct 2, 2014):
We need to support dynamic copy size, and dynamic compare size, one or both of them may be dynamic for a given format (or the simplest case: they are actually static but differ). So how will core know what size to copy? If
fmt->params.salt_sizethen this is still static. I think we need to haveFirst candidates to implement this might be RAR and ZIP. I did start a local topic branch but ran out of time, so it's dusty now and probably nothing to use. Feel free to take over.
@jfoug commented on GitHub (Oct 6, 2014):
So fmt->params.salt_size = sizeof(dyna_salt_); ALWAYS, and fmt->params->salt_align = sizeof(dyna_salt_); since we have a pointer.
The salt is a pointer. When the flag is set this will trigger code in the salt compare deep in john to deref the pointer, find the cmp size and compare
There SHOULD be a 2nd size_t value, but not for sizeof salt. I think this is the right way to do it:
The 'offset' will be how many bytes into buffer to skip ahead, before compare. So if there were several pointers to data needed, but not part of the salt, or some other ints, floats, etc, that were not part of the salt, they could be put at the start of the buffer[1] field (as john core would see it), and would not be part of the compare.
I am actively working on this. It really should not be too hard to get this working, and working well.
Here is a 'first' version. It is not used by salt_hash or any salt compare stuff yet. Also, it should be updated in the salt smashing code, to also handle dyna salts. However, the .zip format has fully used this method now. John core simply copies the pointer and passes it back in. The core will need updated to properly access this pointer to do the comparisons.
811d9c2@magnumripper commented on GitHub (Oct 6, 2014):
I had some totally different thing in mind but (now that I get it) your solution is probably what Solar meant too. Hmm so revisiting his suggestion, we can still free() this buffer in crk_remove_salt() if we want to (perhaps in the future).
salt_hash() is format-local, so you should probably fix that right away in zip_fmt, no?
@jfoug commented on GitHub (Oct 6, 2014):
It is similar to how I do it in dyna already. I think solar dug into that one, and saw what was done, and simply realized it could be generalized and used in other locations. The dyna also has a stub header that must be the same, then the real 'salt-contents' are later in the structure. But the method of simply passing the 'contents' of a pointer back to john's core, and then later doing a dereference of a pointer to a pointer is the easy way to go. Then john thinks salts are simply a fixed length of the size of a pointer (4 or 8 bytes).
We can ONLY call free if we do not use mem_alloc_tiny There are reasons to use the mem_alloc_tiny, since it does put salts close together. HOWEVER, for these type salts (large things), packing the salts close enough together may not be needed. But if we have formats that may have variable sized salts, but they are not huge, then using mem_alloc_tiny is really the way to go (such as the current dynamic format).
So yes, it would be pretty easy to remove the salt in crk_remove_salt. I might add another bit (set one of the sizes to *= -1 if the salt was allocated with mem_alloc_tiny. That way crk_remove_salt could be a little smarter.
Ok what parts of JtR need hooked to get this working?
salt_hash
crk_remove_salt
salt-smashing code in fmt_self_test
I already have it in the show options.
what else?
@magnumripper commented on GitHub (Oct 6, 2014):
I'd have a look at these:
And also search for all mentions of potcheck_salt_size in cracker.c - that variable should eventually not be needed.
@magnumripper commented on GitHub (Oct 6, 2014):
There is no salt_hash() in core, it's format-local. Any format using FMT_DYNA_SALT should handle it properly by themselves.
@jfoug commented on GitHub (Oct 7, 2014):
Here is one reason for cmp_offset:
In this salt, you have 2 variable sized buffers. To do this, you need to first know how big both are. Then allocate a buffer large enough for the base structure, + the size of both buffers (and null's, or whatever). Then set p1 to point to start of buffer (or wherever the data is, and p2 to point to its data. Then set the comp_size/comp_offset to exclude these 2 pointer, since they completely vary, and are dependent upon the location of the salt in memory BUT not on the salt.
This was what I meant, by putting all of the non-salt related stuff first, followed by the 'real' salt data. To do that, we need an offset, along with size. I am pretty sure with this design, that we can handle all variable sized salts, keying off the unique data to filter out dupes properly.
Also, we should delete memory during load time, when we detect a dupe, not just at salt crack remove.
Well, I am getting the other core code in right now. I am sure we will expand upon this over time, getting it to drop out memory from dupes, and drop memory when salts are cracked. The first time around may not be that important. Getting it working right, fast and solid are what needs done now. Later we will try to save some memory.
If my code works, I should have a checking soon that hooks into loader and cracker as you listed, and try to get it into the salt smashing code also.
@jfoug commented on GitHub (Oct 7, 2014):
Got it working, but still a few things to complete.
b979611Fixed a few bugs in zip_fmt (overwrite and flag not being set). Now added code to loader, and cracker to properly deal with FMT_DYNA_SALT types. The salt_hash in zip_fmt grown to handle entire blob (not just the salt item).
Tested by concatenating the 2 zip hashes in the file 9 times. When run on 'older' .zip format (or the 1st gen of this patch), it said 44 unique salts. The old zip format found them all. The new one found all but the first (due to the memory overwrite. The fixed version only sees 2 salts and finds them both.
NOTE, I need to get salt smashing code working for this. Also, salt dupe removal should free salts. But first I have to use mem_alloc, an not mem_alloc_tiny, and somehow pass back the information that they are free-able. Then we need to also carry that free code over into fmt_remove_salt function.
@magnumripper please peer review.
@jfoug commented on GitHub (Oct 7, 2014):
Found issue. Need to check that sizes/offsets match before checking buffer
866c48a@jfoug commented on GitHub (Oct 7, 2014):
Added a new default format function for salt_hash for dyna_salt types. Now all of their salt_hash functions can simply use that one.
271911f@jfoug commented on GitHub (Oct 7, 2014):
Added format self test salt smashing logic for dyna salts. Before the code was simply smashing the data holding the pointer. Now it smashes the actual salt data.
7040cbcI am not sure this is that big of a deal, since by definition, dyna_salts have to be allocated. But the code was put in for completeness.@magnumripper commented on GitHub (Oct 7, 2014):
So when deallocating a salt, you'd still just free(salt) and all will be fine, right?
We could opt to always have them freeable by definition, never use tiny. FMT_DYNA_SALT would mean dynamic size and dynamically allocated. I believe most cases this is needed will be large salts anyway.
@jfoug commented on GitHub (Oct 7, 2014):
I have code to list the salt can be freed. I stole 1 bit from offset using an anon union. I also start the myself with a dynasalt struct and not tell same members. I think doing that is safe, but I need to test more. I now have a bunch of memdbg found leaks, so I can make sure I track down all palaces that need frees. I will get it in morning. I'm crashing now
@magnumripper commented on GitHub (Oct 7, 2014):
Please note that ZIP, RAR, Office and likely most other formats needing this has OpenCL variants. All changes should be ported to the OpenCL sources too. I assume this will be trivial, not really changing any GPU stuff.
@jfoug commented on GitHub (Oct 11, 2014):
Code has now been put out there, which fully cleans up the salts, and which will free a salt when all hashes have been cracked for it (dyna_salts). I have also reduced some complexity (such as the salt compare), making a single function that does either dyna_salt or non-dyna_salt
Getting the memory release correct was not easy. Lots of scattered places in the code, which simply did not care about salts. All salt stuff was done with mem_alloc_tiny, so a great deal of code simply zipped right over it. I am pretty sure I have caught it all.
f606329Now after others sign off on this, I will get the other formats we things should be dyna_salt done. I would assume it is much more than zip-rar-office, but I can easily start there.
@jfoug commented on GitHub (Oct 11, 2014):
rar (cpu) added as dyna_salt. NOTE, I removed to vars from the salt structure. They were (or should have been) local temp vars in the get_salt function, even though they are part of the format line. Once those 2 were gone, all other salt data is now used in salt compare/hash.
619c6deSmall bug fix:
419e767@magnumripper commented on GitHub (Oct 11, 2014):
What vars is that? How could we be that stupid? I implemented the (exact) same changes in rar-opencl now, and best of all that kludge in cracker.c is now history.
@jfoug commented on GitHub (Oct 11, 2014):
The vars are in the hash string, but have no place in the runtime salt.
@magnumripper commented on GitHub (Oct 11, 2014):
I got that part but which was it? Are you forcing me to read the diff and find out? ;-)
@magnumripper commented on GitHub (Oct 11, 2014):
Oh, I see. They are needed in the hash, but only for building our runtime salt. It's archive name and pos, right?
@jfoug commented on GitHub (Oct 11, 2014):
U took them both out. Fname and pos
@magnumripper commented on GitHub (Oct 11, 2014):
On a side note I've been contemplating to add mmap() support to RAR. Does this fit worse or better now with dynamic salt? Hmm I think it's better still. Instead of loading filename:offset into our allocated buffer, we'd just mmap() it. It's just the difference we must not free it but munmap() it. Maybe we should sacrifice another bit in the offset field for saying it's mmap'ed?
@jfoug commented on GitHub (Oct 12, 2014):
We could take another bit from our off, or use a int32 for flags. Can u think of other flags ?
@jfoug commented on GitHub (Oct 12, 2014):
Is mmap more or less efficient than a normal allocation ?
@magnumripper commented on GitHub (Oct 12, 2014):
It's extremely effiecient in case we have a huge file but never actually need much of the data at all. We can almost always do early rejection in the first fraction of it. As far as I know we have a really low number of false positives bad enough to end up in a full CRC32 of the whole data. An mmap() will not actually read ANY data until actually accessed. I bet only the first 4K page will actually be used in almost all uses!
It's also very efficient if you start eight separate sessions against a 4GB RAR file. It will only use one copy of the data (if that!).
@jfoug commented on GitHub (Oct 12, 2014):
Sounds much better than allocation.for many unfound items it uses little men.
@jfoug commented on GitHub (Oct 12, 2014):
For mmap() to work, would we not need another pointer in the main dyna_salt struct, that points to the part of the struct that is actually mapped? If so, we still could use the salt_alloc_needs_free==1 check for calling MEM_FREE, we would just check this pointer first and if not null we call munmap() first. This does require another pointer of space. I just do not see how we can do it otherwise generically, since the core will not have knowledge of salt record structure.
Thinking a bit more, I think we can do it without having to add a pointer to formats not needing it. Heres how:
then in rar (or other formats needing mmap allocated items, they add a dyna_salt_mmap structure as their first item, setting the right bits from the bitmap structure. the dyna_salt_remove_fp function detects the mmap bit set, re-interpret the structure as a type dyna_salt_mmap type, calls the munmap() and then optionally calls the MEM_FREE on the whole structure. But it is possible that the structure would be better allocated using the mem_alloc_tiny, due to the additional memory overhead of allocation. Yes, a mem_alloc_tiny does not allow us to remove the memory later, but it does often speed up runtime and can reduce memory. Especially if the main large buffer is not being allocated. The way it is setup, a format can alternate between allocation, and mem_free, and can also alternate between mmap and non-mmap. So rar inlines could be allocated non-mmap, and files could be mmap.
One other thing I thought about, is we have to be very careful about cmp_size (possibly offset) if using mmap. Right now, rar has entire blob in the cmp_size range. If we did that for mmap() version, it would allocate the whole thing, correct just to build the hash, and then to later compare. Not good. OR are we dealing with JUST a pointer (within our memory block)? If so, then that pointer certainly would NOT be used during comparison, it has nothing to do with the salt. The reason I ask, is I really do not know much about mmap, so I am not 100% sure how it is implemented. Guess I need to read up some, lol.
@magnumripper commented on GitHub (Oct 12, 2014):
I'm not 100% sure about anything. Just thinking loud below.
In the mmap() case we'd need psalt->raw_data to be just a pointer not included in the compare. This is not a problem for RAR, it's actually better than comparing all data during a salt compare. The rest of the salt "can't" be dupe, we still have real salt and CRC in there.
Current code reads part of the file into our data blob:
An mmap() version would be something like this (we only map that same data, not the whole file):
No other code in rar_fmt should need any change. It's still a pointer to the same data. I'm not sure about mmap() and offsets though, when it comes to offsets not being a multiple of page size.
To unmap it, we need the pointer and the length:
@magnumripper commented on GitHub (Oct 12, 2014):
This is the uncertain part
So what happens if we have a page size of 4K and an offset of 1234? I'm not sure if we'd get what we actually wanted, or if we'd get 1,233 zeros to skip over.
@magnumripper commented on GitHub (Oct 12, 2014):
BTW if I got it right, it's OK to use fclose() after mapping. It will still be mapped.
@jfoug commented on GitHub (Oct 12, 2014):
I will look at this later tonight. I still think this is a no-go
for 2 reasons (one is EASY to fix). The syntax rar_data[1] will make an inlined array and not a pointer. No big deal. Just change to unsigned char *raw_data; then we can mmap that pointer.
The other issue is that functions in dyna_salt.c have to unmap that, and have NO idea what a rarfile structure is. They know what the dyna_salt struct is (and if you read my message about 5 up, would also know what a dyna_salt_mmap struct is). But they have NO idea how to deal with a raw_data pointer.
I have been thinking more about this, and it MAY be prudent to much with format struct again, adding a free_salt function. Then a format can be in charge of its salt, from beginning to end. But this additional functionality CAN be done with a 'generic' dyna_salt.c function, if we make the base structures smart enough.. Then the format would actually USE the pointer contained in the dyna_salt_mmap structure, IF it is used. For rar, you already have to deal with both types, both mmap and static allocated, since I think we can only mmap a file, not one of the inline objects.
I have also changed rar2john, to allow myself to get some test data for mmap work if we go down that path. I am not sure if you have been following along there or not.
Anyway, it sounds like if we go the struct dyna_salt_mmap method, we will also have to add the length of the mapping along with the pointer to that struct.
@jfoug commented on GitHub (Oct 12, 2014):
Can we simply map from start of file, and simply access data += pos ? So ar[pos] would be the first byte? If we do that, will mmap pull the entire first part of the file in memory, when we hit that first byte??? I would hope not, but do not know the implementation. It would not be overly hard to change this to work that way. Then for non-mmap hashes (inlines), we just set pos to 0, so the same code still works.
@magnumripper commented on GitHub (Oct 12, 2014):
Yes, I overlooked this. So we really need to add some kind of mmap-specific struct like the dyna_salt_mmap_t you suggested.
Actually it can be done like this:
But the unmapping would obviously need to be
Alternatively (EDITED):
And then the unmapping would be
@magnumripper commented on GitHub (Oct 12, 2014):
No matter how we do it, nothing will be read from disk unless we actually read data from that page. This is all ran by the usual virtual memory machinery!
@magnumripper commented on GitHub (Oct 12, 2014):
I tried this
If I give an offset of 0 or eg. 4096, it works fine. If I use an offset that is not a multiple of 4K, the mmap() fails.
@magnumripper commented on GitHub (Oct 12, 2014):
This version works with any offset:
In the last invocation, we read across a page boundary