使用Python破解RAR文件密碼的代碼實(shí)例
1. 簡(jiǎn)介
rar 壓縮文件資源又不少是被加密的,密碼通常也比較簡(jiǎn)單,我們可以通過暴力破解的方式來獲取,通常耗時(shí)也比較小。
2. 使用說明
2.1 基本語(yǔ)法
rar-bruteforce-crack.py [--start START] [--stop STOP] [--verbose VERBOSE] [--alphabet ALPHABET] [--file FILE]
- –start START 設(shè)置密碼長(zhǎng)度位數(shù)的最小值;
- –stop STOP 設(shè)置密碼長(zhǎng)度位數(shù)的最大值;
- –verbose VERBOSE 設(shè)置是否顯示破解的過程;
- –alphabet ALPHABET 設(shè)置密碼的組成集合;
- – file FILE 設(shè)置待破解的rar壓縮文件
2.1 密碼長(zhǎng)度
可以大致估算一下密碼的位數(shù),一般不會(huì)很長(zhǎng),絕大部分都是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è)置文件
如要對(duì) 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)符號(hào)和空白號(hào),直接用string庫(kù)下的 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pandas DataFrame 篩選數(shù)據(jù)幾種方法實(shí)現(xiàn)
本文介紹了四種在DataFrame中篩選數(shù)據(jù)的方法:根據(jù)字段、標(biāo)簽、位置、布爾索引和通過query進(jìn)行篩選,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
python+django加載靜態(tài)網(wǎng)頁(yè)模板解析
這篇文章主要介紹了python+django加載靜態(tài)網(wǎng)頁(yè)模板解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
tensorflow建立一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)的方法
本篇文章主要介紹了tensorflow建立一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Django form表單與請(qǐng)求的生命周期步驟詳解
這篇文章主要介紹了Django-form表單與請(qǐng)求的生命周期,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算
按位取反是位運(yùn)算符,而位運(yùn)算符是應(yīng)用在兩個(gè)數(shù)的運(yùn)算上,會(huì)對(duì)數(shù)字的二進(jìn)制所有位數(shù)進(jìn)行從低到高的運(yùn)算,下面這篇文章主要給大家介紹了關(guān)于Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算的相關(guān)資料,需要的朋友可以參考下2023-02-02
python2和python3應(yīng)該學(xué)哪個(gè)(python3.6與python3.7的選擇)
許多剛?cè)腴T Python 的朋友都在糾結(jié)的的問題是:我應(yīng)該選擇學(xué)習(xí) python2 還是 python3,Python 3.7 已經(jīng)發(fā)布了,目前Python的用戶,主要使用的版本 應(yīng)該是 Python3.6 和 Python2.7 ,那么是不是該轉(zhuǎn)到 Python 3.7 呢2019-10-10
一文詳解Python中數(shù)據(jù)清洗與處理的常用方法
在數(shù)據(jù)處理與分析過程中,缺失值、重復(fù)值、異常值等問題是常見的挑戰(zhàn),本文總結(jié)了多種數(shù)據(jù)清洗與處理方法,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考下2025-01-01
python初步實(shí)現(xiàn)word2vec操作
這篇文章主要介紹了python初步實(shí)現(xiàn)word2vec操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
在Keras中利用np.random.shuffle()打亂數(shù)據(jù)集實(shí)例
這篇文章主要介紹了在Keras中利用np.random.shuffle()打亂數(shù)據(jù)集實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06

