使用Python破解RAR文件密碼的代碼實(shí)例
1. 簡介
rar 壓縮文件資源又不少是被加密的,密碼通常也比較簡單,我們可以通過暴力破解的方式來獲取,通常耗時(shí)也比較小。
2. 使用說明
2.1 基本語法
rar-bruteforce-crack.py [--start START] [--stop STOP] [--verbose VERBOSE] [--alphabet ALPHABET] [--file FILE]
- –start START 設(shè)置密碼長度位數(shù)的最小值;
- –stop STOP 設(shè)置密碼長度位數(shù)的最大值;
- –verbose VERBOSE 設(shè)置是否顯示破解的過程;
- –alphabet ALPHABET 設(shè)置密碼的組成集合;
- – file FILE 設(shè)置待破解的rar壓縮文件
2.1 密碼長度
可以大致估算一下密碼的位數(shù),一般不會很長,絕大部分都是4 ~ 10位的密碼,則 --start 4 --stop 10 .
2.2 設(shè)置是否顯示破解過程
--verbose 設(shè)置為 True 時(shí)將顯示破解的詳細(xì)過程,默認(rèn)是不顯示的
2.3 設(shè)置密碼的組成集合
--alphabet 用于設(shè)置密碼的組成集合,默認(rèn)的密碼是由 0123456789 這些數(shù)字組成的。 可以根據(jù)需要設(shè)置,如設(shè)置成數(shù)字加小寫字母,則 --alphabet 0123456789abcdefghijklmnopqrstuvwxyz .
2.4 設(shè)置文件
如要對 rartest.rar 這個(gè)rar壓縮文件解密即解壓需要設(shè)置 --file rartest.rar .
3. 例子
加密壓縮
my_directory 文件夾下有3個(gè)文件 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

成功解壓!
4. 代碼
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 = (
# 默認(rèn)的密碼只來自數(shù)字 "0123456789"
digits
# 若需要更多的組合可加上如下
# 若要加上小寫英文字母 "abcdefghijklmnopqrstuvwxyz" 的排列組合
# digits + ascii_lowercase
# 若要加上大小寫英文字母 ascii_uppercase
# digits + ascii_lowercase + ascii_uppercase
# 若要加上標(biāo)點(diǎn)符號和空白號,直接用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()
到此這篇關(guān)于使用Python破解RAR文件密碼的代碼實(shí)例的文章就介紹到這了,更多相關(guān)Python破解RAR文件密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python學(xué)習(xí)之subprocess模塊詳解
這篇文章主要介紹了python學(xué)習(xí)之subprocess模塊的相關(guān)資料,該模塊用于創(chuàng)建子進(jìn)程并與其進(jìn)行交互,它提供了多個(gè)函數(shù)和類來執(zhí)行操作系統(tǒng)命令、獲取命令輸出以及管理子進(jìn)程的生命周期,需要的朋友可以參考下2024-12-12
pandas dataframe rolling移動計(jì)算方式
在Pandas中,rolling()方法用于執(zhí)行移動窗口計(jì)算,常用于時(shí)間序列數(shù)據(jù)分析,例如,計(jì)算某商品的7天或1個(gè)月銷售總量,可以通過rolling()輕松實(shí)現(xiàn),該方法的關(guān)鍵參數(shù)包括window(窗口大?。?min_periods(最小計(jì)算周期)2024-09-09
keras 讀取多標(biāo)簽圖像數(shù)據(jù)方式
這篇文章主要介紹了keras 讀取多標(biāo)簽圖像數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
教你用Python查看茅臺股票交易數(shù)據(jù)的詳細(xì)代碼
CSV是以逗號分隔數(shù)據(jù)項(xiàng)(也被稱為字段)的數(shù)據(jù)交換格式,主要應(yīng)用于電子表格和數(shù)據(jù)庫之間的數(shù)據(jù)交換,本文給大家介紹下用Python查看茅臺股票交易數(shù)據(jù)的詳細(xì)代碼,感興趣的朋友一起看看吧2022-03-03
python實(shí)現(xiàn)用戶登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)用戶登錄系統(tǒng)的相關(guān)資料,感興趣的朋友可以參考一下2016-05-05

