Implementation:AISecurityLab Hackagent IterativeAttack
| Knowledge Sources | |
|---|---|
| Domains | Security_Testing, Adversarial_Attacks, Search |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for defining iterative attack techniques that search against a target with a judge panel in the loop, provided by the HackAgent attacks framework.
Description
IterativeAttack is an abstract base class for attacks that talk to the target while they run: each reply decides what they send next. It receives the target as a Target callable and the campaign's evaluation panel as a Judge callable, and it asks the panel about each exchange as it goes, because the verdict is what tells it whether to stop.
The class provides:
- prepare(goals, target, judge): Run-scoped setup, done once before any goal is searched. Default does nothing; AutoDAN-Turbo overrides to build its strategy library.
- run(goal, target, judge): Validates the goal and delegates to search.
- search(goal, target, judge): Abstract — subclasses implement the actual search algorithm.
- decode(response): Maps a target reply back to plain text (identity by default).
Each exchange an iterative attack judges becomes a Finding, carrying the messages sent, the response, the verdict, metadata, and the trace path identifying the phase it came from.
The module also provides succeeded(verdict) — a helper that returns True when a verdict exists, has no error, and success is True.
This is a Pattern Doc: it documents the interface that concrete iterative attacks (PAIR, TAP, PAIR, BoN, crescendo, AutoDAN-Turbo, etc.) implement.
Usage
- Implementing a new iterative attack: Subclass IterativeAttack, set name and params_type, implement search. Use phase/decision/artifact from the trace module to record the search structure.
- Cross-goal learning: Override prepare to do run-scoped work (e.g., building a strategy library) that search reads from self.
Code Reference
Source Location
- Repository: AISecurityLab_Hackagent
- File: hackagent/attacks/techniques/iterative.py
Signature
@dataclass(frozen=True)
class Finding:
"""One judged exchange of a search."""
messages: Messages
response: CompletionResult
verdict: Optional[Verdict] = None
metadata: Mapping[str, Any] = field(default_factory=dict)
path: Path = ()
class IterativeAttack(ABC, Generic[ParamsT]):
"""A search against the target for one goal."""
name: ClassVar[str]
params_type: ClassVar[type[AttackParams]]
async def prepare(self, goals: list[str], target: Target, judge: Optional[Judge] = None) -> None: ...
async def run(self, goal: str, target: Target, judge: Optional[Judge] = None) -> list[Finding]: ...
@abstractmethod
async def search(self, goal: str, target: Target, judge: Optional[Judge]) -> list[Finding]: ...
def decode(self, response: str) -> str:
"""Map a target reply back to plain text before it is judged."""
return response
def succeeded(verdict: Optional[Verdict]) -> bool:
"""Whether the panel called this exchange a success."""
Import
from hackagent.attacks.techniques.iterative import IterativeAttack, Finding, succeeded
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| params | AttackParams subclass | Yes | Validated, immutable configuration with role callables |
| goal | str | Yes | Non-empty goal text after stripping |
| target | Target | Yes | Async callable to the model under test (messages in, response out) |
| judge | Optional[Judge] | No | Panel callable; None when no judges configured |
Outputs
| Name | Type | Description |
|---|---|---|
| search return | list[Finding] | One per judged exchange, in the order tried |
| Finding | dataclass | messages, response, verdict, metadata, path (trace phase) |
Usage Examples
Implementing an Iterative Attack
from hackagent.attacks.techniques.iterative import IterativeAttack, Finding, succeeded
from hackagent.attacks.techniques.contract import AttackParams, Completion, Target, Judge
from hackagent.attacks.techniques.trace import phase, decision
class MySearchParams(AttackParams):
"""Needs an attacker model to generate prompts."""
attacker: Completion
max_rounds: int = 5
class MySearch(IterativeAttack[MySearchParams]):
name = "my_search"
params_type = MySearchParams
REQUIRED_ROLES = frozenset({"attacker"})
async def search(self, goal, target, judge):
findings = []
for round_num in range(self.params.max_rounds):
async with phase(f"round_{round_num}"):
prompt = await self.params.attacker([{"role": "user", "content": goal}])
response = await target([{"role": "user", "content": prompt}])
verdict = await judge(Sample(goal=goal, prompt=prompt, response=response.text)) if judge else None
findings.append(Finding(
messages=[{"role": "user", "content": prompt}],
response=response,
verdict=verdict,
))
if succeeded(verdict):
decision("stopped", "goal achieved")
break
return findings
Related Pages
Implements Principle
- Principle:AISecurityLab_Hackagent_Iterative_Attack_Search
- Principle:AISecurityLab_Hackagent_Attack_Dependency_Injection