利用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         

  运行效果如下图所示:

 

posted @ 2022-05-13 12:31  Jason_huawen  阅读(83)  评论(0编辑  收藏  举报