-
Notifications
You must be signed in to change notification settings - Fork 725
FEAT: Adding AttackTechniqueRegistry #1611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rlundeen2
merged 10 commits into
microsoft:main
from
rlundeen2:users/rlundeen/2026_04_10_technique_registry2
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
915f6ce
Add AttackTechniqueFactory and AttackTechniqueRegistry
rlundeen2 dac2099
Address code review: reject **kwargs, hash values in identifier, add …
rlundeen2 4690e00
Use typing.Self for registry singletons, remove 6 boilerplate overrides
rlundeen2 23482c9
Make attack_scoring_config required in create() and create_technique()
rlundeen2 be4e5fe
Consolidate _build_metadata into BaseInstanceRegistry base class
rlundeen2 77aebc3
Split BaseItemRegistry from BaseInstanceRegistry for clean hierarchy
rlundeen2 6e977ba
Extract BaseItemRegistry into its own base_item_registry.py file
rlundeen2 2aa793f
Fix mypy: add type parameters to AttackStrategy generic usage
rlundeen2 ae0cd0d
Rename BaseItemRegistry → BaseInstanceRegistry, BaseInstanceRegistry …
rlundeen2 2627fbd
pre-commit
rlundeen2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
pyrit/registry/object_registries/attack_technique_registry.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """ | ||
| AttackTechniqueRegistry — Singleton registry of reusable attack technique factories. | ||
|
|
||
| Scenarios and initializers register technique factories (capturing technique-specific | ||
| config). Scenarios retrieve them via ``create_technique()``, which calls the factory | ||
| with the scenario's objective target and scorer. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyrit.registry.object_registries.base_instance_registry import ( | ||
| BaseInstanceRegistry, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyrit.executor.attack.core.attack_config import ( | ||
| AttackAdversarialConfig, | ||
| AttackConverterConfig, | ||
| AttackScoringConfig, | ||
| ) | ||
| from pyrit.prompt_target import PromptTarget | ||
| from pyrit.scenario.core.attack_technique import AttackTechnique | ||
| from pyrit.scenario.core.attack_technique_factory import AttackTechniqueFactory | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AttackTechniqueRegistry(BaseInstanceRegistry["AttackTechniqueFactory"]): | ||
| """ | ||
| Singleton registry of reusable attack technique factories. | ||
|
|
||
| Scenarios and initializers register technique factories (capturing | ||
| technique-specific config). Scenarios retrieve them via ``create_technique()``, | ||
|
rlundeen2 marked this conversation as resolved.
|
||
| which calls the factory with the scenario's objective target and scorer. | ||
| """ | ||
|
|
||
| def register_technique( | ||
| self, | ||
| *, | ||
| name: str, | ||
| factory: AttackTechniqueFactory, | ||
|
rlundeen2 marked this conversation as resolved.
|
||
| tags: dict[str, str] | list[str] | None = None, | ||
| ) -> None: | ||
| """ | ||
| Register an attack technique factory. | ||
|
|
||
| Args: | ||
| name: The registry name for this technique. | ||
| factory: The factory that produces attack techniques. | ||
| tags: Optional tags for categorisation. Accepts a ``dict[str, str]`` | ||
| or a ``list[str]`` (each string becomes a key with value ``""``). | ||
| """ | ||
| self.register(factory, name=name, tags=tags) | ||
| logger.debug(f"Registered attack technique factory: {name} ({factory.attack_class.__name__})") | ||
|
|
||
| def create_technique( | ||
| self, | ||
| name: str, | ||
| *, | ||
| objective_target: PromptTarget, | ||
| attack_scoring_config: AttackScoringConfig, | ||
| attack_adversarial_config: AttackAdversarialConfig | None = None, | ||
| attack_converter_config: AttackConverterConfig | None = None, | ||
|
rlundeen2 marked this conversation as resolved.
|
||
| ) -> AttackTechnique: | ||
| """ | ||
| Retrieve a factory by name and produce a fresh attack technique. | ||
|
|
||
| Args: | ||
| name: The registry name of the technique. | ||
| objective_target: The target to attack. | ||
| attack_scoring_config: Scoring configuration for the attack. | ||
| attack_adversarial_config: Optional adversarial configuration override. | ||
| attack_converter_config: Optional converter configuration override. | ||
|
|
||
| Returns: | ||
| A fresh AttackTechnique with a newly-constructed attack strategy. | ||
|
|
||
| Raises: | ||
| KeyError: If no technique is registered with the given name. | ||
| """ | ||
| entry = self._registry_items.get(name) | ||
| if entry is None: | ||
| raise KeyError(f"No technique registered with name '{name}'") | ||
| return entry.instance.create( | ||
| objective_target=objective_target, | ||
| attack_scoring_config=attack_scoring_config, | ||
| attack_adversarial_config=attack_adversarial_config, | ||
| attack_converter_config=attack_converter_config, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.