pierce403/keyhunter — pull request #5 by pilate (2014-01-08 23:16:00+00:00)
Title: Fixed version

This patch fixes a pretty big issue with large amounts of data being skipped.

The current script (incorrectly) assumes that the return value from str.find subtracts the offset position from the return value.

```
pos=data.find(magic,x)
```

Returns the offset of the magic string. The value returned is still the absolute offset, and does not take 'x' into account in any way. That would be fine, except for this line:

```
x+=(pos+1)
```

This increases the search offset by the position of the found string + 1.

Now lets say we have some private key at data offset 100, 150, and 200.

After the first discovery, pos would accurately be updated to '101' at the end.

```
# x is still 0
x += (100+1)
```

Now when the second discovery happens, at offset 150, this happens again:

```
# x is now 101
x += (150+1)
```

Resulting in the new offset being 252, instead of 152.

This ends up erroneously skipping our third key entirely.

The file open flags have been changed to "rb", as windows machines fail horribly with binary files unless this is done.

I also did a bit of cleanup on the code as a whole, implementing a 'main' function, and stealing tabbek's idea of using 'with' for the open file.