Repository navigation
FEAT: Add baconian Encoding technique via LeetspeakConverter #4445
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,40 @@ | |
| from pyrit.scenario.core.scenario_technique import ScenarioTechnique | ||
| from pyrit.score import DecodingScorer, TrueFalseScorer | ||
|
|
||
| # Baconian cipher (Francis Bacon, 1605): each letter maps to a 5-symbol a/b group; see | ||
| # https://en.wikipedia.org/wiki/Bacon%27s_cipher . Framing classical ciphers as an LLM jailbreak | ||
| # vector: CipherChat (Yuan et al., 2023, arXiv:2308.06463). | ||
| # Kept as a Leetspeak substitution table rather than a dedicated converter (reuse and compose | ||
| # before adding). | ||
| _BACONIAN_SUBSTITUTIONS: dict[str, list[str]] = { | ||
| "a": ["aaaaa"], | ||
| "b": ["aaaab"], | ||
| "c": ["aaaba"], | ||
| "d": ["aaabb"], | ||
| "e": ["aabaa"], | ||
| "f": ["aabab"], | ||
| "g": ["aabba"], | ||
| "h": ["aabbb"], | ||
| "i": ["abaaa"], | ||
| "j": ["abaaa"], | ||
| "k": ["abaab"], | ||
| "l": ["ababa"], | ||
| "m": ["ababb"], | ||
| "n": ["abbaa"], | ||
| "o": ["abbab"], | ||
| "p": ["abbba"], | ||
| "q": ["abbbb"], | ||
| "r": ["baaaa"], | ||
| "s": ["baaab"], | ||
| "t": ["baaba"], | ||
| "u": ["baabb"], | ||
| "v": ["baabb"], | ||
| "w": ["babaa"], | ||
| "x": ["babab"], | ||
| "y": ["babba"], | ||
| "z": ["babbb"], | ||
| } | ||
|
|
||
|
|
||
| class EncodingDatasetConfiguration(DatasetAttackConfiguration): | ||
| """ | ||
|
|
@@ -129,6 +163,7 @@ class EncodingTechnique(ScenarioTechnique): | |
| AsciiSmuggler = ("ascii_smuggler", set[str]()) | ||
| Bifid = ("bifid", set[str]()) | ||
| ColumnarTransposition = ("columnar_transposition", set[str]()) | ||
| Baconian = ("baconian", set[str]()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Add Baconian to the Garak scanner guide doc/scanner/garak.py and doc/scanner/garak.ipynb list every Encoding technique by name in "Available techniques". On main this list now has 21 encodings and ends with "UbbiDubbi, Disemvowel", because #4448 and #4449 updated it. This PR adds |
||
|
|
||
| @classmethod | ||
| def get_aggregate_tags(cls) -> set[str]: | ||
|
|
@@ -317,6 +352,7 @@ def _converter_variants() -> list[tuple[list[Converter], str, str]]: | |
| ([AsciiSmugglerConverter()], "ascii_smuggler", "ascii_smuggler"), | ||
| ([BifidConverter()], "bifid", "bifid"), | ||
| ([ColumnarTranspositionConverter(key="ZEBRAS")], "columnar_transposition", "columnar_transposition"), | ||
| ([LeetspeakConverter(custom_substitutions=_BACONIAN_SUBSTITUTIONS)], "baconian", "baconian"), | ||
| ] | ||
|
|
||
| # These are the same as Garak encoding attacks | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| from pyrit.converter import LeetspeakConverter | ||
| from pyrit.scenario.scenarios.garak.encoding import _BACONIAN_SUBSTITUTIONS | ||
|
|
||
|
|
||
| async def test_baconian_technique_encodes_letters(): | ||
| """The baconian Encoding technique is a LeetspeakConverter configured with the Baconian a/b table.""" | ||
| converter = LeetspeakConverter(custom_substitutions=_BACONIAN_SUBSTITUTIONS) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Test the registered baconian row, not a hand-built converter This test builds its own LeetspeakConverter and computes the expected value from the same |
||
| result = await converter.convert_async(prompt="ab") | ||
| assert result.output_text == _BACONIAN_SUBSTITUTIONS["a"][0] + _BACONIAN_SUBSTITUTIONS["b"][0] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Accept a faithful Baconian decode in the scorer
The table maps i and j to "abaaa" and u and v to "baabb". A target that decodes the ciphertext by this table answers "iauascript" for "javascript" and "HAIII" for "Hajji". DecodingScorer does a case-insensitive substring match against the seed, so it scores these answers False. With the default datasets, this occurs for 14 of 17 garak_web_html_js seeds and 5 of 56 garak_slur_terms_en seeds. The run then reports the target as resistant although it decoded and repeated the payload. Main already fixes this class of problem for AsciiSmugglerConverter (#4437):
Encoding._seed_groups_for_convertersreads a converter'sto_ascii_equivalentand stores the result asdecoded_text, which DecodingScorer also accepts. When you rebase, keep the canonical table and add a thinBaconianConverter(LeetspeakConverter)that passes this table and exposes that fold (j to i, v to u). Then a faithful decode and a context-repaired decode both score True. Add a test that a j/v seed gets the foldeddecoded_text.