1
1
import asyncio
2
2
from functools import wraps
3
3
import logging
4
- import types
5
- from nebula .addons .attacks .attacks import Attack
4
+ from nebula .addons .attacks .communications .communicationattack import CommunicationAttack
6
5
7
6
8
- class DelayerAttack (Attack ):
7
+ class DelayerAttack (CommunicationAttack ):
9
8
"""
10
9
Implements an attack that delays the execution of a target method by a specified amount of time.
11
-
12
- This attack dynamically modifies the `propagate` method of the propagator to
13
- introduce a delay during its execution.
14
10
"""
15
- def __init__ (self , engine , attack_params ):
11
+
12
+ def __init__ (self , engine , attack_params : dict ):
16
13
"""
17
14
Initializes the DelayerAttack with the engine and attack parameters.
18
15
19
16
Args:
20
17
engine: The engine managing the attack context.
21
18
attack_params (dict): Parameters for the attack, including the delay duration.
22
19
"""
23
- super ().__init__ ()
24
- self .engine = engine
25
- self .propagator = self .engine ._cm ._propagator
26
- self .original_propagate = self .propagator .propagate
27
- self .delay = int (attack_params ["delay" ])
28
- self .round_start_attack = int (attack_params ["round_start_attack" ])
29
- self .round_stop_attack = int (attack_params ["round_stop_attack" ])
30
-
31
- def delay_decorator (self , delay ):
20
+ try :
21
+ self .delay = int (attack_params ["delay" ])
22
+ round_start = int (attack_params ["round_start_attack" ])
23
+ round_stop = int (attack_params ["round_stop_attack" ])
24
+ except KeyError as e :
25
+ raise ValueError (f"Missing required attack parameter: { e } " )
26
+ except ValueError :
27
+ raise ValueError ("Invalid value in attack_params. Ensure all values are integers." )
28
+
29
+ super ().__init__ (
30
+ engine ,
31
+ engine ._cm ._propagator ,
32
+ "propagate" ,
33
+ round_start ,
34
+ round_stop ,
35
+ self .delay ,
36
+ )
37
+
38
+ def decorator (self , delay : int ):
32
39
"""
33
40
Decorator that adds a delay to the execution of the original method.
34
41
35
42
Args:
36
- delay (int or float ): The time in seconds to delay the method execution.
43
+ delay (int): The time in seconds to delay the method execution.
37
44
38
45
Returns:
39
46
function: A decorator function that wraps the target method with the delay logic.
40
47
"""
41
- # The actual decorator function that will be applied to the target method
42
48
def decorator (func ):
43
- @wraps (func ) # Preserves the metadata of the original function
44
- async def wrapper (* args ):
45
- logging .info (f"[DelayerAttack] Adding delay of { delay } seconds" )
46
-
49
+ @wraps (func )
50
+ async def wrapper (* args , ** kwargs ):
51
+ logging .info (f"[DelayerAttack] Adding delay of { delay } seconds to { func .__name__ } " )
47
52
await asyncio .sleep (delay )
48
53
_ , * new_args = args # Exclude self argument
49
54
return await func (* new_args )
50
55
return wrapper
51
- return decorator
52
-
53
- async def _inject_malicious_behaviour (self ):
54
- """
55
- Modifies the `propagate` method of the propagator to include a delay.
56
- """
57
- decorated_propagate = self .delay_decorator (self .delay )(self .propagator .propagate )
58
-
59
- self .propagator .propagate = types .MethodType (decorated_propagate , self .propagator )
60
-
61
- async def _restore_original_behaviour (self ):
62
- """
63
- Restores the original behaviour of the `propagate` method.
64
- """
65
- self .propagator .propagate = self .original_propagate
66
-
67
- async def attack (self ):
68
- """
69
- Starts the attack by injecting the malicious behaviour.
70
-
71
- If the current round matches the attack start round, the malicious behavior
72
- is injected. If it matches the stop round, the original behavior is restored.
73
- """
74
- if self .engine .round == self .round_stop_attack :
75
- logging .info (f"[DelayerAttack] Stopping Delayer attack" )
76
- await self ._restore_original_behaviour ()
77
- elif self .engine .round == self .round_start_attack :
78
- logging .info ("[DelayerAttack] Injecting malicious behaviour" )
79
- await self ._inject_malicious_behaviour ()
56
+ return decorator
0 commit comments