wifi握手包自动跑包

众所周知握手包跑包的时候密码字典的生成是个非常头疼的问题,因为10位数的纯数字密码字典就已经很大了,这里我使用的穷举法,根据所给出的字符串中的字符串,穷举出所有密码组合。

为此使用python来进行自动化跑包,而用电脑跑包又非常的浪费资源,跑包的时候完全没法使用电脑了,所以准备在树莓派上进行跑包。可是无论PC还是树莓派跑包最怕的事情就是死机或者停电,so,程序的要求如下:

首先,能够把将要生成的字典分解成小字典来生成,当每个小字典生成结束并跑完的时候,再生成另一个小字典来跑包,如此反复进行;

其次,为了防止树莓派断电,或者突然想自己的电脑,需要程序能够保存已经完成的工作,当下次再启动程序的时候继续上次没有完成的任务;

最后,考虑到多核心cpu的利用,需要生成子进程来跑包,这样程序占用的资源就足够多了,才能充分利用设备来跑包。

函数分布如下:

父进程:

  1. 将密码字符串转换为数字字符串的函数
  2. 将数字字符串转换为密码字符串的函数
  3. 根据开始密码、结束密码、任务数量将所有密码组合分割成符合任务数量的字符串列表的函数,由这个列表来分配给各个子进程需要制作的字典容量
  4. 检测子进程是否找到密码并输出密码文件的函数
  5. 集合调用上述所有函数的主函数

子进程,只有一个函数和调用,大致流程如下:

  1. 利用传入的开始字符串和停止字符串来生成小字典
  2. 字典生成后跑包,调用subprocess.check_out(),生成跑包子进程
  3. 跑包子进程结束时检查是否得到密码,得到密码后写入文件,以便上级进程能够通过文件来检测到密码已找到
  4. 若未得到密码则写入当前字典最后的一个字符串,作为可以中断的存档

好了,父子进程的关系和功能大致都已经叙述了,下面就是开源的内容了:

需要注意

这个代码需要安装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'

结束!

发布了13 篇原创文章 · 获赞 0 · 访问量 5729
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章