赞
踩
rar 压缩文件资源又不少是被加密的,密码通常也比较简单,我们可以通过暴力破解的方式来获取,通常耗时也比较小。
rar-bruteforce-crack.py [--start START] [--stop STOP] [--verbose VERBOSE] [--alphabet ALPHABET] [--file FILE]
可以大致估算一下密码的位数,一般不会很长,绝大部分都是4 ~ 10位的密码,则 --start 4 --stop 10
.
--verbose
设置为 True 时将显示破解的详细过程,默认是不显示的
--alphabet
用于设置密码的组成集合,默认的密码是由 0123456789
这些数字组成的。
可以根据需要设置,如设置成数字加小写字母,则 --alphabet 0123456789abcdefghijklmnopqrstuvwxyz
.
如要对 rartest.rar
这个rar压缩文件解密即解压需要设置 --file rartest.rar
.
my_directory 文件夹下有3个文件 file1.txt
、file2.txt
、file3.txt
,将 my_directory 文件夹下文件全部压缩成 rartest.rar
文件,并且使用密码:
rar a rartest.rar my_directory/* -p
两次输入压缩密码得到压缩文件 rartest.rar
.
python3 rar-bruteforce-crack.py --start 4 --stop 10 --alphabet 0123456789 --file rartest.rar --verbose True
开始暴力破解
... ...
可以看到得到了压缩密码 “2351”
unrar x rartest.rar
成功解压!
from argparse import ArgumentParser from itertools import chain, product from os.path import exists from string import digits, ascii_lowercase, ascii_uppercase, ascii_letters, printable from subprocess import PIPE, Popen from time import time chars = ( # 默认的密码只来自数字 "0123456789" digits # 若需要更多的组合可加上如下 # 若要加上小写英文字母 "abcdefghijklmnopqrstuvwxyz" 的排列组合 # digits + ascii_lowercase # 若要加上大小写英文字母 ascii_uppercase # digits + ascii_lowercase + ascii_uppercase # 若要加上标点符号和空白号,直接用string库下的 printable # printable ) special_chars = "();<>`|~\"&\'}]" parser = ArgumentParser(description='Python combination generator to unrar') parser.add_argument( '--start', help='Number of characters of the initial string [1 -> "a", 2 -> "aa"]', type=int, ) parser.add_argument( '--stop', help='Number of characters of the final string [3 -> "aaa"]', type=int, ) parser.add_argument( '--verbose', help='Show combintations', default=False, required=False ) parser.add_argument( '--alphabet', help='alternative chars to combinations', default=chars, required=False, ) parser.add_argument('--file', help='.rar file [file.rar]', type=str) args = parser.parse_args() def generate_combinations(alphabet, length, start=1): """Generate combinations using alphabet.""" yield from ( ''.join(string) for string in chain.from_iterable( product(alphabet, repeat=x) for x in range(start, length + 1) ) ) def format(string): """Format chars to write them in shell.""" formated = map( lambda char: char if char not in special_chars else f'\\{char}', string ) return ''.join(formated) if __name__ == '__main__': if not exists(args.file): raise FileNotFoundError(args.file) if args.stop < args.start: raise Exception('Stop number is less than start') start_time = time() for combination in generate_combinations(args.alphabet, args.stop, args.start): formated_combination = format(combination) if args.verbose: print(f'Trying: {combination}') cmd = Popen( f'unrar t -p{formated_combination} {args.file}'.split(), stdout=PIPE, stderr=PIPE, ) out, err = cmd.communicate() if 'All OK' in out.decode(): print(f'Password found: {combination}') print(f'Time: {time() - start_time}') exit()
[1] https://github.com/dunossauro/PyRarCrack
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。