File size: 4,044 Bytes
c624cb8 | 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 | """
Base Attack Strategy
Abstract base class for all attack strategies.
"""
from abc import ABC, abstractmethod
from typing import Any, Dict
class BaseAttackStrategy(ABC):
"""
Abstract base class for attack strategies.
All attack strategies must implement the `apply` method.
This provides a plugin architecture for extensible attacks.
"""
def __init__(self, name: str = "base"):
"""
Initialize the attack strategy.
Args:
name: Name of the attack strategy
"""
self.name = name
@abstractmethod
def apply(self, prompt: str, **kwargs: Any) -> str:
"""
Apply the attack strategy to a prompt.
Args:
prompt: The original prompt to mutate
**kwargs: Additional strategy-specific parameters
Returns:
The mutated adversarial prompt
"""
raise NotImplementedError
def get_metadata(self) -> Dict[str, Any]:
"""
Get metadata about this attack strategy.
Returns:
Dictionary containing strategy metadata
"""
return {
"strategy_name": self.name,
"strategy_type": self.__class__.__name__,
}
def validate_prompt(self, prompt: str) -> bool:
"""
Validate that the prompt is suitable for this attack.
Args:
prompt: The prompt to validate
Returns:
True if valid, False otherwise
"""
return isinstance(prompt, str) and len(prompt.strip()) > 0
class PromptInjectionStrategy(BaseAttackStrategy):
"""Base class for prompt injection attacks."""
def __init__(self):
super().__init__(name="prompt_injection")
def get_injection_markers(self) -> list[str]:
"""
Get common injection markers for detection.
Returns:
List of injection marker strings
"""
return [
"ignore previous instructions",
"ignore all previous instructions",
"you are now operating under new rules",
"disregard your previous instructions",
"forget your previous instructions",
"new system instructions",
"override system",
]
class JailbreakStrategy(BaseAttackStrategy):
"""Base class for jailbreak attacks."""
def __init__(self):
super().__init__(name="jailbreak")
class BiasTriggerStrategy(BaseAttackStrategy):
"""Base class for bias trigger attacks."""
def __init__(self, protected_attributes: list[str] | None = None):
super().__init__(name="bias_trigger")
self.protected_attributes = protected_attributes or [
"race",
"gender",
"religion",
"nationality",
"age",
"disability",
"sexual orientation",
]
class ContextPoisoningStrategy(BaseAttackStrategy):
"""Base class for context poisoning attacks."""
def __init__(self):
super().__init__(name="context_poisoning")
class RoleConfusionStrategy(BaseAttackStrategy):
"""Base class for role confusion attacks."""
def __init__(self):
super().__init__(name="role_confusion")
class ChainingStrategy(BaseAttackStrategy):
"""Base class for attack chaining."""
def __init__(self, chain_depth: int = 3):
super().__init__(name="chaining")
self.chain_depth = chain_depth
def get_chain_steps(self) -> list[str]:
"""
Get the chain steps for multi-turn attacks.
Returns:
List of chain step descriptions
"""
return [
"soften_context",
"introduce_hypothetical",
"escalate_topic",
]
|