Skip to content

Commit

Permalink
🗓 May 7, 2022 11:01:10 AM
Browse files Browse the repository at this point in the history
✨ xor_key_from_file extra function
  • Loading branch information
securisec committed May 7, 2022
1 parent 5b3825a commit 2dff4a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions chepy/extras/crypto_extras.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import binascii
import json
from typing import Iterator, Dict, List, Union
from binascii import hexlify, unhexlify
Expand Down Expand Up @@ -82,6 +83,38 @@ def xor_two_files(file1: str, file2: str) -> bytes: # pragma: no cover
pass


def xor_key_from_files(
f1_path: str, f2_path: str, min: int = 1, max: int = 257
) -> Union[bytes, None]: # pragma: no cover
"""XOR two files and get the key.
Args:
f1_path (str): File 1 path
f2_path (str): File 2 path
min (int, optional): Min chars to test. Defaults to 1.
max (int, optional): Max chars to test. Defaults to 257.
Returns:
Union[bytes, None]: Key as hex bytes or None if no key found
"""

def find_same(s: bytes):
i = (s + s).find(s, 1, -1)
return None if i == -1 else s[:i]

for i in range(min, max):
with open(f1_path, "rb") as f:
d1 = f.read(i)

with open(f2_path, "rb") as f:
d2 = f.read(i)

x = bytes(a ^ b for a, b in zip(d1, d2))
o = find_same(x)
if o is not None:
return binascii.hexlify(o)


def xor(data: bytes, key: bytes) -> bytes: # pragma: no cover
"""XOR data with a hex key
Expand Down
3 changes: 2 additions & 1 deletion chepy/extras/crypto_extras.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, Iterator
from typing import Dict, Iterator, Union

def factordb(n: int) -> dict: ...
def construct_private_key(n: int, e: int, d: int, format: str=..., passphrase: str=...) -> str: ...
def xor_bruteforce_multi(data: str, min: int=..., max: int=..., errors: str=...) -> Iterator[Dict[str, str]]: ...
def xor_key_from_files(f1_path:str, f2_path: str, min: int =..., max: int = ...) -> Union[bytes, None]: ...

0 comments on commit 2dff4a1

Please sign in to comment.