1. 程式人生 > 其它 >利用Python編寫C&C

利用Python編寫C&C

  本程式碼可以實現C&C即command and control center的功能,可以新增被控端,然後同時發出相關的指令,並獲得相應的結果:

  

 1 import paramiko
 2 import sys
 3 
 4 class SSHClient:                                        #該類用於產生各個被控端,可以執行相應的Shell指令
 5     def __init__(self, host, username, password) -> None:
 6         self.host = host
 7
self.username = username 8 self.password = password 9 try: 10 self.sshclient = paramiko.SSHClient() 11 self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 12 self.sshclient.connect(hostname=self.host, username=self.username, password=self.password)
13 except paramiko.AuthenticationException: 14 print("[-] Failed to connect %s" % self.host) 15 16 17 def execute_command(self, command): 18 try: 19 stdin, stdout,stderr = self.sshclient.exec_command(command) 20 if stdout:
21 command_result = stdout.read().decode('utf-8') 22 return command_result 23 except Exception as e: 24 print(e) 25 return "Failed to execute command on the host: %s" % self.host 26 27 28 class CommandControlCenter: #該類為控制端,可以新增被控端 29 def __init__(self) -> None: 30 self.client_list = [] 31 self.banner() 32 33 34 def banner(self): 35 banner= """ 36 ************************************************** 37 38 ****Command & Control Center by Jason Wong******** 39 40 ************************************************** 41 42 """ 43 print(banner) 44 45 def add(self, host, username, password): 46 client = SSHClient(host, username, password) 47 self.client_list.append(client) 48 49 50 def run(self): 51 try: 52 while True: 53 command = input("Send command: ") 54 if command == 'q': 55 break 56 for client in self.client_list: 57 output = client.execute_command(command) 58 print("[+] Output from the host: %s " % client.host) 59 print(output, '\n\n') 60 except KeyboardInterrupt: 61 print("Exit the program") 62 sys.exit(0) 63 64 65 if __name__ == "__main__": 66 command = CommandControlCenter() 67 command.add('192.168.140.137', 'msfadmin', 'msfadmin') 68 command.add('192.168.140.248', 'root', '762326') 69 command.run() 70

  執行效果如下圖所示: