衆所周知握手包跑包的時候密碼字典的生成是個非常頭疼的問題,因爲10位數的純數字密碼字典就已經很大了,這裏我使用的窮舉法,根據所給出的字符串中的字符串,窮舉出所有密碼組合。
爲此使用python來進行自動化跑包,而用電腦跑包又非常的浪費資源,跑包的時候完全沒法使用電腦了,所以準備在樹莓派上進行跑包。可是無論PC還是樹莓派跑包最怕的事情就是死機或者停電,so,程序的要求如下:
首先,能夠把將要生成的字典分解成小字典來生成,當每個小字典生成結束並跑完的時候,再生成另一個小字典來跑包,如此反覆進行;
其次,爲了防止樹莓派斷電,或者突然想自己的電腦,需要程序能夠保存已經完成的工作,當下次再啓動程序的時候繼續上次沒有完成的任務;
最後,考慮到多核心cpu的利用,需要生成子進程來跑包,這樣程序佔用的資源就足夠多了,才能充分利用設備來跑包。
函數分佈如下:
父進程:
- 將密碼字符串轉換爲數字字符串的函數
- 將數字字符串轉換爲密碼字符串的函數
- 根據開始密碼、結束密碼、任務數量將所有密碼組合分割成符合任務數量的字符串列表的函數,由這個列表來分配給各個子進程需要製作的字典容量
- 檢測子進程是否找到密碼並輸出密碼文件的函數
- 集合調用上述所有函數的主函數
子進程,只有一個函數和調用,大致流程如下:
- 利用傳入的開始字符串和停止字符串來生成小字典
- 字典生成後跑包,調用subprocess.check_out(),生成跑包子進程
- 跑包子進程結束時檢查是否得到密碼,得到密碼後寫入文件,以便上級進程能夠通過文件來檢測到密碼已找到
- 若未得到密碼則寫入當前字典最後的一個字符串,作爲可以中斷的存檔
好了,父子進程的關係和功能大致都已經敘述了,下面就是開源的內容了:
需要注意
這個代碼需要安裝aircrack-ng
運行的參數需要在hashcat_attack_tasks_malloc.py的最後修改
跑包的命令需要在hashcat_attack_task.py中修改,代碼中有註釋
第二次運行程序時需要輸入時候繼續
資源文件我已經上傳,想支持我的可以去下載一下,給我1個積分的支持
//download.csdn.net/download/wachm/12118290
還有,hashcat比這個跑的快,而且可以利用GPU加速,但是不像我這個可以中斷,保存任務的進度然後下次繼續進行,有高性能設備的小可愛們可以試試這個!
好了,找一找,改一改吧!
hashcat_attack_tasks_malloc.py
import subprocess
import time
import hashcat_attack_name as han
# 進制轉換
# 進制字符串, 待轉換至10進制的字符串
# 返回,10進制數字
def base_change_special_normal(_rule_str, _special):
_rule_str = "".join(_rule_str)
_special = "".join(_special)
t_base = _rule_str.__str__().__len__()
t_total = 0
t_length = _special.__len__()
t_s = _special.__getitem__(t_length - 1)
t_total = t_total + _rule_str.index(t_s)
t_unit_base = t_base
for i in range(t_length-2, -1, -1):
t_total = t_total + t_unit_base * _rule_str.index(_special.__getitem__(i))
t_unit_base = t_unit_base*t_base
return t_total
# 進制轉換
# 進制字符串,10進制數字, 填充長度
# 返回,給定進制字符串形式的進制
def base_change_normal_special(_rule_str, _decimal, _fill_length):
# 進制基數
t_base = _rule_str.__str__().__len__().__int__()
t_list = []
t_decimal = _decimal.__int__()
while t_decimal > 0:
t_mod = t_decimal % t_base
t_decimal = t_decimal // t_base
t_list.append(_rule_str.__getitem__(t_mod))
if _fill_length > 0 and t_list.__len__() < _fill_length:
for i in range(t_list.__len__(), _fill_length):
t_list.append(_rule_str.__getitem__(0))
t_list.reverse()
return "".join(t_list)
# dict_string表示所有組合的字符源,task_mount表示分割的任務數,task_length表示生成的長度
# task_starts指定開始的字符串,task_stops指定結束的字符串
# 返回一個列表,以[開始字符串,結束字符串,開始字符串,結束字符串,。。。]的形式返回
def str_task_split(_dict_str, _task_mount, _task_length, _task_starts, _task_stops):
_task_mount = int(_task_mount)
_task_length = int(_task_length)
if str(_task_starts).__len__() != str(_task_stops).__len__():
print("str_task_split 參數錯誤,4,5長度不等")
return 0
if _task_length != str(_task_starts).__len__():
print("str_task_split 參數錯誤,長度不匹配")
return 0
# 進制轉換
t_starts_d = base_change_special_normal(_dict_str, _task_starts)
t_stops_d = base_change_special_normal(_dict_str, _task_stops)
# 剩餘需要生成的總數
t_total = t_stops_d-t_starts_d
# 每個任務的數量
t_task_average = t_total // _task_mount
# 任務餘數,這個加在最後一個任務裏
t_task_mod = t_total % _task_mount
t_return_list = []
for i in range(0, _task_mount - 1):
t_return_list.append(base_change_normal_special(_dict_str, t_starts_d, _task_length))
print(t_starts_d, end="-->")
t_stops_d = t_starts_d + t_task_average
print(t_stops_d)
t_return_list.append(base_change_normal_special(_dict_str, t_stops_d, _task_length))
t_starts_d = t_stops_d+1
t_return_list.append(base_change_normal_special(_dict_str, t_starts_d, _task_length))
print(t_stops_d, end="-->")
t_stops_dt_stops_d = t_stops_d + t_task_average + t_task_mod
t_return_list.append(base_change_normal_special(_dict_str, t_stops_d, _task_length))
print(t_stops_d)
return t_return_list
# 檢測是否子進程是否找到密碼並輸出文件,文件內容是否包含密碼
# 文件名前綴,進程數量,文件擴展名
# 返回True表示找到密碼
def hashcat_attack_sub_has_passwd(_out_pre, _mount, _out_type):
_mount = int(_mount)
_has_passwd = False
for i in range(0,_mount):
_file_name = _out_pre+str(i)+_out_type
try:
f = open(_file_name, 'r')
if f.readline().find('KEY FOUND') >= 0:
_has_passwd = True
f.close()
break
except IOError:
pass
return _has_passwd
# 得到分配列表
# 分配進程
# 讀取子進程進度,記錄子進程已完成的密碼,顯示子進程已完成的密碼
def hashcat_attack_tasks_malloc(_dict_str, _task_mount, _task_length, _task_starts, _task_stops):
_task_mount = int(_task_mount)
_task_length = int(_task_length)
# 檢測是否存在上一次的任務,並且是否繼續上一次的任務
print(time.asctime(time.localtime(time.time())))
_has_load_task = False
try:
continue_file = open(han.continue_file_name, 'r')
__task_mount = continue_file.readline().strip()
_is_finish = continue_file.readline().strip()
continue_file.close()
if _is_finish == han.continue_flag:
ask = input('是否繼續未完成的任務(Y/n):')
print(ask)
if ask.lower() == 'yes' or ask.lower() == 'y' or ask == '\n':
task_str_list = list()
for i in range(0, _task_mount):
_task_load_file = open(han.split_load_file_pre+str(i)+han.split_load_file_type)
task_str_list.append(_task_load_file.readline().strip())
_has_load_task = True
_task_mount = int(__task_mount)
except IOError:
pass
# 分配列表
if not _has_load_task:
task_str_list = str_task_split(_dict_str, _task_mount, _task_length, _task_starts, _task_stops)
task_list = list()
# 創建任務列表
for i in range(0, _task_mount):
task_argument_list = ['python', 'hashcat_attack_task.py', _dict_str, task_str_list[i * 2], task_str_list[i * 2 + 1], str(i)]
task_list.append(subprocess.Popen(task_argument_list))
# 創建記錄文件
continue_file = open(han.continue_file_name, 'w')
continue_file.write(str(_task_mount)+'\n')
continue_file.write(han.continue_flag)
continue_file.close()
while True:
# 60秒查詢一次子進程的狀態,當有進程得到密碼時,停止所有的進程
time.sleep(60)
# 用於判斷所有進程是否結束
task_all_over = 0
# 檢測任務是否結束,檢測子進程是否輸出密碼文件
for i in range(0, _task_mount):
if task_list[i].poll().__str__().strip() == "None":
# 檢測是否輸出密碼文件的函數
if hashcat_attack_sub_has_passwd(han.output_pre, _task_mount, han.output_type):
# 結束所有進程
task_all_over = _task_mount
# 寫入任務完成
continue_file = open(han.continue_file_name, 'w')
continue_file.write(str(_task_mount) + '\n')
continue_file.write(han.continue_flag)
continue_file.close()
print('找到密碼請查看目錄下文件')
print(time.asctime(time.localtime(time.time())))
else:
task_all_over = task_all_over+1
if task_all_over == _task_mount:
for i in range(0,_task_mount):
task_list[i].terminate()
break
print('當前字符集所有組合已經全部嘗試')
dict_str = "1234567890"
task_length = 10
task_mount = 4
task_starts = "1234491111"
task_stops = "1234560000"
hashcat_attack_tasks_malloc(dict_str, task_mount, task_length, task_starts, task_stops)
hashcat_attack_task.py
# coding=utf-8
import sys
import subprocess
import shlex
import hashcat_attack_name as han
# number是當前進程被分配的字符串標記,用來防止寫文件的時候重名
# 參數:字典的源字符集,開始字符串,停止字符串,文件標記
def hashcat_attack_task(_dict_string, _starts, _stops, _number):
# limit限制字典條數
limit = 10000000
dict_file_name = han.dict_pre + _number + han.dict_type
dict_file = open(dict_file_name, 'w')
_dict_string = str(_dict_string)
_starts = str(_starts)
_stops = str(_stops)
dict_string_length = _dict_string.__len__()
tab_key = list()
tab_count = list()
tab_length = _starts.__len__()
# init
for i in range(0, tab_length):
tab_key.append(_dict_string.__getitem__(i))
tab_count.append(0)
# set from starts
for i in range(0, tab_length):
tab_count.__setitem__(i, _dict_string.index(_starts.__getitem__(i)))
tab_key.__setitem__(i, _dict_string.__getitem__(tab_count.__getitem__(i)))
print("starts:" + "".join(tab_key))
print(tab_key.__len__())
print(tab_count.__len__())
gcount = 0
_found_key = False
key = "none"
while "".join(tab_key) != _stops :
# global counter
gcount = gcount + 1
# last key ++
cur = tab_count.__getitem__(tab_length - 1)
tab_count.__setitem__(tab_length - 1, cur + 1)
# check length-1 to 1, the 1st can't be checked in here
for i in range(tab_length - 1, 0, -1):
if tab_count.__getitem__(i) == dict_string_length:
pre = tab_count.__getitem__(i - 1)
tab_count.__setitem__(i - 1, pre + 1)
tab_count.__setitem__(i, 0)
# check the 1st
if tab_count.__getitem__(0) == dict_string_length:
break
# reload tab_key
for i in range(0, tab_length):
tab_key.__setitem__(i, _dict_string.__getitem__(tab_count.__getitem__(i)))
# write
key = "".join(tab_key)
dict_file.write(key + "\n")
# 中斷,寫字典,跑包
if gcount >= limit or key == _stops:
dict_file.close()
# 使用aircrack跑包命令在此,請自行修改,可以刪去-b參數
argument = 'aircrack-ng -a2 -b 88:10:8f:36:6c:6c -w ./' + dict_file_name + ' ./-05.cap'
argument = shlex.split(argument)
result = subprocess.check_output(argument).decode(encoding="utf-8").split('\n')
# 記錄當前任務
_task_save = open(han.split_load_file_pre + _number + han.split_load_file_type, 'w')
_task_save.write(key+'\n')
_task_save.write(_stops)
_task_save.close()
# 如果找到密碼則寫入文件
for i in result:
if i.find('KEY FOUND') >= 0:
_output = open(han.output_pre + _number + han.output_type, 'w')
_output.write(i)
_output.close()
_found_key = True
break
dict_file = open(dict_file_name, 'w')
if _found_key:
break
dict_file.close()
# hashcat_attack_task('1234567890', '1234564444', '1234567890', str(5))
hashcat_attack_task(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
hashcat_attack_name.py
output_pre = 'hashcat_attack_passwd_'
output_type = '.txt'
dict_pre = 'hashcat_attack_dictionary_'
dict_type = '.txt'
continue_file_name = 'hashcat_attack_continue.txt'
continue_flag = 'unfinished!'
split_load_file_pre = 'hashcat_attack_task_'
split_load_file_type = '.txt'
結束!