Created
October 8, 2023 13:21
-
-
Save JujuDel/2304a46e3b7c1f1e346065502a450896 to your computer and use it in GitHub Desktop.
Open a password protected RAR file from a RegEx pattern. The RegEx serves as hint to provide a list of password to try
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Brute force a password protected rar file with a RegEx pattern hint. | |
This is not meant to be used with malicious intent. | |
I have made this script only because I had a protected rar file for which I forget | |
the pwd but remembered partially what it was. Writing a RegEx helped to reduce the | |
pwd candidates to something I could easily fastly recover :) | |
Pre-requisites: | |
- `pip install exrex rarfile` | |
- have unrar available in your PATH | |
Usage: | |
`python3 crackRAR.py -i INPUT_FILE -p PWD_PATTERN` | |
Example: | |
`python3 crackRAR.py -i my_file.rar -p "my_awesome_[Rr][3e]gEX\!?"` | |
Will try to open extract the file `my_file.rar` with the following passwords: | |
my_awesome_regEX | |
my_awesome_regEX! | |
my_awesome_RegEX | |
my_awesome_RegEX! | |
my_awesome_r3gEX | |
my_awesome_r3gEX! | |
my_awesome_R3gEX | |
my_awesome_R3gEX! | |
""" | |
import argparse | |
import os | |
import exrex | |
from rarfile import RarFile, BadRarFile | |
from tqdm import tqdm | |
from typing import Tuple | |
def parse_arguments() -> argparse.Namespace: | |
"""Parse the arguments.""" | |
parser = argparse.ArgumentParser(description="Parse command line arguments") | |
parser.add_argument( | |
"-i", "--input_file", type=str, required=True, help="Path to the input rar file" | |
) | |
parser.add_argument( | |
"-p", | |
"--pwd_pattern", | |
type=str, | |
required=True, | |
help="The regex pattern matching the pwd.", | |
) | |
return parser.parse_args() | |
def tryExtractRar(file_path: str, pattern: str) -> Tuple[bool, str]: | |
"""Try to open the rar file. | |
Args: | |
file_path: the path to the protected rar file. | |
pattern: the regex pattern to use to generate password candidates. | |
Returns: | |
ret: True on pwd found, False otherwise. | |
pwd: the pwd to open the rar file. Can be ignored if ret is False. | |
""" | |
# Generator pwd from the regex pattern | |
pwd_gen = exrex.generate(pattern) | |
# Number of password to try | |
pwd_count = exrex.count(pattern) | |
with RarFile(file_path, "r") as rar_file: | |
pbar = tqdm(pwd_gen, total=pwd_count) | |
for pwd in pbar: | |
pbar.set_description(f"Processing '{pwd}'") | |
try: | |
# Try to extract | |
rar_file.extractall(pwd=pwd) | |
return True, pwd | |
except BadRarFile: | |
# The error occurs on bad password | |
pass | |
return False, "" | |
def __main(): | |
# Parse the arguments | |
args = parse_arguments() | |
# Check if the file exists | |
if not os.path.exists(args.input_file): | |
raise FileNotFoundError(f"File {args.input_file} does not exit!") | |
# Try to find the password | |
print("Start trying passwords") | |
ret, pwd = tryExtractRar(args.input_file, args.pwd_pattern) | |
if ret: | |
print(f"The password is: {pwd}") | |
else: | |
print(f"Sorry, the password was not found from the provided RegEx: {args.pwd_pattern}") | |
if __name__ == "__main__": | |
__main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment