Skip to content

FEAT: Add ANSI Escape Code Attack #577

Closed

Description

Is your feature request related to a problem? Please describe.

LLMs can output ANSI escape codes that manipulate terminals, such as moving cursors, hiding text, or even executing commands. These codes can cause unintended actions when logged or displayed, posing risks in downstream systems. PyRIT currently lacks functionality to handle these scenarios.

Describe the solution you'd like

Introduce an ANSI escape code attack feature in PyRIT to:

  • Generate ANSI escape sequences (e.g., text coloring, cursor movements, or OSC8/OSC52 commands).
  • Allow users to embed these sequences into outputs for red teaming purposes, such as testing how systems handle or log such outputs.

References and Tools

Activity

  1. KutalVolkan commented on Nov 26, 2024

    @KutalVolkan
    ContributorAuthor

    Hello all,

    I'll handle the ANSI Escape Code Attack. Let me know if there are best practices or wishes to consider :)

  2. changed the title [-]FEAT: Add ANSI Escape Code Converter[/-] [+]FEAT: Add ANSI Escape Code Attack[/+] on Nov 26, 2024
  3. richlundeen commented on Nov 28, 2024

    @richlundeen
    Contributor

    This is fantastic; def something we want. I haven't dug too deep, but it might be good to comment here on what you're thinking.

    Would it just be datasets that contain the characters? And potentially we would have a scorer to check the output and see if a model includes these characters?

  4. KutalVolkan commented on Nov 29, 2024

    @KutalVolkan
    ContributorAuthor

    Hello @rlundeen2,

    Here’s the approach I’m planning:

    First, I’ll inject ANSI escape codes into prompts using converters. These codes will include actions like changing text color, moving the cursor, adding hyperlinks, or injecting clipboard data. To simulate real-world scenarios, I’ll test both raw ANSI codes (\033[31m) and escaped forms (e.g., r"\033[31m").

    Next, I’ll test the LLM’s behavior in two ways:

    1. Echoing Input: Does the LLM repeat the ANSI codes from the prompt without sanitizing them?
    2. Generating New Codes: Can the LLM create its own ANSI sequences when prompted, such as when asked how to format terminal text?

    I’ll also use a scorer to analyze the model’s output. The scorer will:

    • Detect the presence of raw or escaped ANSI codes in the output.
    • Differentiate between harmless and potentially harmful sequences, such as those that could manipulate logs or terminals.
    • Flag cases where the LLM generates new ANSI sequences, as this may indicate a vulnerability.

    To make testing robust, I’ll use a variety of prompts, such as asking the LLM to repeat a sequence, unescape a string, or explain how to achieve specific terminal effects. This ensures multiple scenarios are covered where the LLM might encounter or output these codes.

    For now, my focus is on testing the LLM’s behavior directly. Downstream system testing—like how logs or terminals process these outputs—is more challenging to automate, but if you have ideas on how to approach this effectively, I’d love to hear them!

    Let me know if this aligns with what you’re envisioning or if there’s anything else I should consider!

  5. leondz commented on Nov 29, 2024

    @leondz

    Worth checking out the OSC 8 manual to see what's in scope - likely broader than just the obvious \x1b \x9b \x9d

  6. richlundeen commented on Nov 29, 2024

    @richlundeen
    Contributor

    All this sounds great! Thanks again for tackling Volkan Kutal (@KutalVolkan) and for the pointers Leon Derczynski (@leondz)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions