|
1 | | -import logging |
2 | | -from typing import Dict, Any, List |
3 | | -from adware_dashboard.core.payload_manager import PayloadManager |
4 | | -from adware_dashboard.core.deployment_manager import DeploymentManager |
5 | | -from adware_dashboard.models import Adware, Payload, DeploymentMethod |
6 | | - |
7 | | -class AdwareManager: |
8 | | - def __init__(self, logger: logging.Logger, payload_manager: PayloadManager, deployment_manager: DeploymentManager): |
9 | | - """ |
10 | | - Initializes the AdwareManager with a logger, payload manager, and deployment manager. |
11 | | -
|
12 | | - Args: |
13 | | - logger (logging.Logger): The logger instance to use. |
14 | | - payload_manager (PayloadManager): The payload manager instance. |
15 | | - deployment_manager (DeploymentManager): The deployment manager instance. |
16 | | - """ |
17 | | - self.logger = logger |
18 | | - self.payload_manager = payload_manager |
19 | | - self.deployment_manager = deployment_manager |
20 | | - |
21 | | - def create_adware(self, name: str, description: str, target_os: str, persistence_method: str, payload_id: int, deployment_method_id: int, config: Dict[str, Any]) -> Adware: |
22 | | - """ |
23 | | - Creates a new adware configuration. |
24 | | -
|
25 | | - Args: |
26 | | - name (str): The name of the adware. |
27 | | - description (str): A description of the adware. |
28 | | - target_os (str): The target operating system. |
29 | | - persistence_method (str): The persistence method. |
30 | | - payload_id (int): The ID of the payload to use. |
31 | | - deployment_method_id (int): The ID of the deployment method to use. |
32 | | - config (Dict[str, Any]): Additional configuration parameters. |
33 | | -
|
34 | | - Returns: |
35 | | - Adware: The created adware object. |
36 | | - """ |
37 | | - payload = self.payload_manager.get_payload(payload_id) |
38 | | - if not payload: |
39 | | - self.logger.error(f"Payload with ID {payload_id} not found.") |
40 | | - raise ValueError(f"Payload with ID {payload_id} not found.") |
41 | | - |
42 | | - deployment_method = self.deployment_manager.get_deployment_method(deployment_method_id) |
43 | | - if not deployment_method: |
44 | | - self.logger.error(f"Deployment method with ID {deployment_method_id} not found.") |
45 | | - raise ValueError(f"Deployment method with ID {deployment_method_id} not found.") |
46 | | - |
47 | | - adware = Adware( |
48 | | - name=name, |
49 | | - description=description, |
50 | | - target_os=target_os, |
51 | | - persistence_method=persistence_method, |
52 | | - payload=payload, |
53 | | - deployment_method=deployment_method, |
54 | | - config=config |
55 | | - ) |
56 | | - adware.save() |
57 | | - self.logger.info(f"Adware '{name}' created successfully.") |
58 | | - return adware |
59 | | - |
60 | | - def get_adware(self, adware_id: int) -> Adware: |
61 | | - """ |
62 | | - Retrieves an adware configuration by its ID. |
63 | | -
|
64 | | - Args: |
65 | | - adware_id (int): The ID of the adware to retrieve. |
66 | | -
|
67 | | - Returns: |
68 | | - Adware: The adware object, or None if not found. |
69 | | - """ |
70 | | - adware = Adware.get_or_none(Adware.id == adware_id) |
71 | | - if not adware: |
72 | | - self.logger.warning(f"Adware with ID {adware_id} not found.") |
73 | | - return adware |
74 | | - |
75 | | - def update_adware(self, adware_id: int, name: str = None, description: str = None, target_os: str = None, persistence_method: str = None, payload_id: int = None, deployment_method_id: int = None, config: Dict[str, Any] = None) -> Adware: |
76 | | - """ |
77 | | - Updates an existing adware configuration. |
78 | | -
|
79 | | - Args: |
80 | | - adware_id (int): The ID of the adware to update. |
81 | | - name (str, optional): The new name of the adware. |
82 | | - description (str, optional): The new description of the adware. |
83 | | - target_os (str, optional): The new target operating system. |
84 | | - persistence_method (str, optional): The new persistence method. |
85 | | - payload_id (int, optional): The new ID of the payload to use. |
86 | | - deployment_method_id (int, optional): The new ID of the deployment method to use. |
87 | | - config (Dict[str, Any], optional): Additional configuration parameters. |
88 | | -
|
89 | | - Returns: |
90 | | - Adware: The updated adware object, or None if not found. |
91 | | - """ |
92 | | - adware = self.get_adware(adware_id) |
93 | | - if not adware: |
94 | | - return None |
95 | | - |
96 | | - if name: |
97 | | - adware.name = name |
98 | | - if description: |
99 | | - adware.description = description |
100 | | - if target_os: |
101 | | - adware.target_os = target_os |
102 | | - if persistence_method: |
103 | | - adware.persistence_method = persistence_method |
104 | | - if payload_id: |
105 | | - payload = self.payload_manager.get_payload(payload_id) |
106 | | - if not payload: |
107 | | - self.logger.error(f"Payload with ID {payload_id} not found.") |
108 | | - raise ValueError(f"Payload with ID {payload_id} not found.") |
109 | | - adware.payload = payload |
110 | | - if deployment_method_id: |
111 | | - deployment_method = self.deployment_manager.get_deployment_method(deployment_method_id) |
112 | | - if not deployment_method: |
113 | | - self.logger.error(f"Deployment method with ID {deployment_method_id} not found.") |
114 | | - raise ValueError(f"Deployment method with ID {deployment_method_id} not found.") |
115 | | - adware.deployment_method = deployment_method |
116 | | - if config: |
117 | | - adware.config = config |
118 | | - |
119 | | - adware.save() |
120 | | - self.logger.info(f"Adware '{adware.name}' updated successfully.") |
121 | | - return adware |
122 | | - |
123 | | - def delete_adware(self, adware_id: int) -> bool: |
124 | | - """ |
125 | | - Deletes an adware configuration by its ID. |
126 | | -
|
127 | | - Args: |
128 | | - adware_id (int): The ID of the adware to delete. |
129 | | -
|
130 | | - Returns: |
131 | | - bool: True if the adware was deleted, False otherwise. |
132 | | - """ |
133 | | - adware = self.get_adware(adware_id) |
134 | | - if not adware: |
135 | | - return False |
136 | | - |
137 | | - adware.delete_instance() |
138 | | - self.logger.info(f"Adware '{adware.name}' deleted successfully.") |
139 | | - return True |
140 | | - |
141 | | - def list_adware(self) -> List[Adware]: |
142 | | - """ |
143 | | - Lists all adware configurations. |
144 | | -
|
145 | | - Returns: |
146 | | - List[Adware]: A list of all adware objects. |
147 | | - """ |
148 | | - adware_list = list(Adware.select()) |
149 | | - return adware_list |
150 | | - |
151 | | - def deploy_adware(self, adware_id: int) -> bool: |
152 | | - """ |
153 | | - Deploys an adware configuration. |
154 | | -
|
155 | | - Args: |
156 | | - adware_id (int): The ID of the adware to deploy. |
157 | | -
|
158 | | - Returns: |
159 | | - bool: True if the adware was deployed, False otherwise. |
160 | | - """ |
161 | | - adware = self.get_adware(adware_id) |
162 | | - if not adware: |
163 | | - return False |
164 | | - |
165 | | - try: |
166 | | - self.deployment_manager.deploy(adware.deployment_method, adware.payload, adware.config) |
167 | | - self.logger.info(f"Adware '{adware.name}' deployed successfully.") |
168 | | - return True |
169 | | - except Exception as e: |
170 | | - self.logger.error(f"Error deploying adware '{adware.name}': {str(e)}") |
171 | | - return False |
| 1 | +import logging |
| 2 | +from typing import Dict, Any, List |
| 3 | +from adware_dashboard.core.payload_manager import PayloadManager |
| 4 | +from adware_dashboard.core.deployment_manager import DeploymentManager |
| 5 | +from adware_dashboard.models import Adware, Payload, DeploymentMethod |
| 6 | + |
| 7 | +class AdwareManager: |
| 8 | + def __init__(self, logger: logging.Logger, payload_manager: PayloadManager, deployment_manager: DeploymentManager): |
| 9 | + """ |
| 10 | + Initializes the AdwareManager with a logger, payload manager, and deployment manager. |
| 11 | +
|
| 12 | + Args: |
| 13 | + logger (logging.Logger): The logger instance to use. |
| 14 | + payload_manager (PayloadManager): The payload manager instance. |
| 15 | + deployment_manager (DeploymentManager): The deployment manager instance. |
| 16 | + """ |
| 17 | + self.logger = logger |
| 18 | + self.payload_manager = payload_manager |
| 19 | + self.deployment_manager = deployment_manager |
| 20 | + |
| 21 | + def create_adware(self, name: str, description: str, target_os: str, persistence_method: str, payload_id: int, deployment_method_id: int, config: Dict[str, Any]) -> Adware: |
| 22 | + """ |
| 23 | + Creates a new adware configuration. |
| 24 | +
|
| 25 | + Args: |
| 26 | + name (str): The name of the adware. |
| 27 | + description (str): A description of the adware. |
| 28 | + target_os (str): The target operating system. |
| 29 | + persistence_method (str): The persistence method. |
| 30 | + payload_id (int): The ID of the payload to use. |
| 31 | + deployment_method_id (int): The ID of the deployment method to use. |
| 32 | + config (Dict[str, Any]): Additional configuration parameters. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + Adware: The created adware object. |
| 36 | + """ |
| 37 | + try: |
| 38 | + payload = self.payload_manager.get_payload(payload_id) |
| 39 | + if not payload: |
| 40 | + self.logger.error(f"Payload with ID {payload_id} not found.") |
| 41 | + raise ValueError(f"Payload with ID {payload_id} not found.") |
| 42 | + |
| 43 | + deployment_method = self.deployment_manager.get_deployment_method(deployment_method_id) |
| 44 | + if not deployment_method: |
| 45 | + self.logger.error(f"Deployment method with ID {deployment_method_id} not found.") |
| 46 | + raise ValueError(f"Deployment method with ID {deployment_method_id} not found.") |
| 47 | + |
| 48 | + adware = Adware( |
| 49 | + name=name, |
| 50 | + description=description, |
| 51 | + target_os=target_os, |
| 52 | + persistence_method=persistence_method, |
| 53 | + payload=payload, |
| 54 | + deployment_method=deployment_method, |
| 55 | + config=config |
| 56 | + ) |
| 57 | + adware.save() |
| 58 | + self.logger.info(f"Adware '{name}' created successfully.") |
| 59 | + return adware |
| 60 | + except ValueError as e: |
| 61 | + self.logger.error(f"Error creating adware: {str(e)}") |
| 62 | + raise |
| 63 | + |
| 64 | + def get_adware(self, adware_id: int) -> Adware: |
| 65 | + """ |
| 66 | + Retrieves an adware configuration by its ID. |
| 67 | +
|
| 68 | + Args: |
| 69 | + adware_id (int): The ID of the adware to retrieve. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Adware: The adware object, or None if not found. |
| 73 | + """ |
| 74 | + adware = Adware.get_or_none(Adware.id == adware_id) |
| 75 | + if not adware: |
| 76 | + self.logger.warning(f"Adware with ID {adware_id} not found.") |
| 77 | + return adware |
| 78 | + |
| 79 | + def update_adware(self, adware_id: int, name: str = None, description: str = None, target_os: str = None, persistence_method: str = None, payload_id: int = None, deployment_method_id: int = None, config: Dict[str, Any] = None) -> Adware: |
| 80 | + """ |
| 81 | + Updates an existing adware configuration. |
| 82 | +
|
| 83 | + Args: |
| 84 | + adware_id (int): The ID of the adware to update. |
| 85 | + name (str, optional): The new name of the adware. |
| 86 | + description (str, optional): The new description of the adware. |
| 87 | + target_os (str, optional): The new target operating system. |
| 88 | + persistence_method (str, optional): The new persistence method. |
| 89 | + payload_id (int, optional): The new ID of the payload to use. |
| 90 | + deployment_method_id (int, optional): The new ID of the deployment method to use. |
| 91 | + config (Dict[str, Any], optional): Additional configuration parameters. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + Adware: The updated adware object, or None if not found. |
| 95 | + """ |
| 96 | + adware = self.get_adware(adware_id) |
| 97 | + if not adware: |
| 98 | + return None |
| 99 | + |
| 100 | + if name: |
| 101 | + adware.name = name |
| 102 | + if description: |
| 103 | + adware.description = description |
| 104 | + if target_os: |
| 105 | + adware.target_os = target_os |
| 106 | + if persistence_method: |
| 107 | + adware.persistence_method = persistence_method |
| 108 | + if payload_id: |
| 109 | + payload = self.payload_manager.get_payload(payload_id) |
| 110 | + if not payload: |
| 111 | + self.logger.error(f"Payload with ID {payload_id} not found.") |
| 112 | + raise ValueError(f"Payload with ID {payload_id} not found.") |
| 113 | + adware.payload = payload |
| 114 | + if deployment_method_id: |
| 115 | + deployment_method = self.deployment_manager.get_deployment_method(deployment_method_id) |
| 116 | + if not deployment_method: |
| 117 | + self.logger.error(f"Deployment method with ID {deployment_method_id} not found.") |
| 118 | + raise ValueError(f"Deployment method with ID {deployment_method_id} not found.") |
| 119 | + adware.deployment_method = deployment_method |
| 120 | + if config: |
| 121 | + adware.config = config |
| 122 | + |
| 123 | + adware.save() |
| 124 | + self.logger.info(f"Adware '{adware.name}' updated successfully.") |
| 125 | + return adware |
| 126 | + |
| 127 | + def delete_adware(self, adware_id: int) -> bool: |
| 128 | + """ |
| 129 | + Deletes an adware configuration by its ID. |
| 130 | +
|
| 131 | + Args: |
| 132 | + adware_id (int): The ID of the adware to delete. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + bool: True if the adware was deleted, False otherwise. |
| 136 | + """ |
| 137 | + adware = self.get_adware(adware_id) |
| 138 | + if not adware: |
| 139 | + return False |
| 140 | + |
| 141 | + adware.delete_instance() |
| 142 | + self.logger.info(f"Adware '{adware.name}' deleted successfully.") |
| 143 | + return True |
| 144 | + |
| 145 | + def list_adware(self) -> List[Adware]: |
| 146 | + """ |
| 147 | + Lists all adware configurations. |
| 148 | +
|
| 149 | + Returns: |
| 150 | + List[Adware]: A list of all adware objects. |
| 151 | + """ |
| 152 | + adware_list = list(Adware.select()) |
| 153 | + return adware_list |
| 154 | + |
| 155 | + def deploy_adware(self, adware_id: int) -> bool: |
| 156 | + """ |
| 157 | + Deploys an adware configuration. |
| 158 | +
|
| 159 | + Args: |
| 160 | + adware_id (int): The ID of the adware to deploy. |
| 161 | +
|
| 162 | + Returns: |
| 163 | + bool: True if the adware was deployed, False otherwise. |
| 164 | + """ |
| 165 | + adware = self.get_adware(adware_id) |
| 166 | + if not adware: |
| 167 | + return False |
| 168 | + |
| 169 | + try: |
| 170 | + self.deployment_manager.deploy(adware.deployment_method, adware.payload, adware.config) |
| 171 | + self.logger.info(f"Adware '{adware.name}' deployed successfully.") |
| 172 | + return True |
| 173 | + except Exception as e: |
| 174 | + self.logger.error(f"Error deploying adware '{adware.name}': {str(e)}") |
| 175 | + return False |
0 commit comments