Anamorpher

repository·main·Indexed 22 days ago

https://github.com/trailofbits/anamorpher

A research and development tool for generating adversarial image payloads that exploit image scaling vulnerabilities in multi-modal AI systems. Anamorpher allows users to craft images that hide multi-modal prompt injections which become visible upon downscaling. It supports bicubic, bilinear, and nearest neighbor algorithms with implementations from OpenCV, PyTorch, TensorFlow, and Pillow, and provides a modular Python API for custom downscaling implementations.

Tokens
1.6K
Snippets
10
Records
12
Agent score
27%

What's inside anamorpher

  1. Overview of Anamorpher

    main

    Anamorpher is a tool designed for crafting and visualizing image scaling attacks against multi-modal AI systems. It allows users to generate images that hide multi-modal prompt injections, which only become visible to the AI when the image is downscaled.

    Key capabilities include:

    • Generating payloads for specific downscaling algorithms (bicubic, bilinear, and nearest neighbor).
    • Comparing payload effectiveness via a frontend interface that supports implementations from OpenCV, PyTorch, TensorFlow, and Pillow.
    • Extending the tool with custom image downscaling implementations via a modular Python API.
  2. Install and run Anamorpher

    main

    Anamorpher requires Python 3.11+. It is recommended to use uv for dependency management.

    Note for Windows users: You must use WSL2 because of TensorFlow dependencies.

    # 1. Install dependencies
    uv sync
    
    # 2. Run the backend
    uv run python backend/app.py
    
    # 3. Open the frontend
    # Open 'frontend/index.html' in your web browser
  3. Important limitations and usage warnings

    main

    When using Anamorpher, keep the following in mind:

    • Probabilistic Results: Due to the nature of multi-modal AI systems, results can vary. For consistent evaluation, it is recommended to run each example 5 times.
    • Transformations: Applying additional image transformations may interfere with the effectiveness of the prompt injections.
    • Algorithm Variability: Payloads may not work across all implementations of bicubic and bilinear algorithms due to differences in anti-aliasing robustness, default library parameters, and implementation-specific optimizations.
    • System Changes: Production AI systems may change their scaling behavior, which can affect payload effectiveness.
  4. Downsample an image

    main

    Perform image downsampling using a specific engine and method.

    Requirements:

    • The input image must be square.
    • The image dimensions must be divisible by 4.
    • The target_resolution must be an integer between 64 and 2048.

    Request Body (JSON):

    • image: Base64 encoded string of the image.
    • downsampler: The engine name (e.g., opencv).
    • method: The interpolation method supported by the engine.
    • target_resolution: The desired side length of the square output image.

    Response (JSON): Returns the original image, the downsampled image (both as base64 data URLs), their respective sizes, and metadata about the operation.

    POST /api/downsample
    {
      "image": "data:image/png;base64,...",
      "downsampler": "opencv",
      "method": "bilinear",
      "target_resolution": 800
    }
  5. Generate a text image

    main

    Create a square image containing specified text. This is useful for creating target images for adversarial attacks.

    Request Body (JSON):

    • text: The string to render.
    • size: The side length of the square image (64 to 2048).
    • font_size: The font size (20 to 64).
    • alignment: Text alignment (center, top, bottom, left, right, topleft, topright, bottomleft, bottomright).

    Response (JSON): Returns the base64 image, its size, the rendered text, font size, alignment, and a text_overflowed boolean flag indicating if the text exceeded the image bounds.

    POST /api/generate-text-image
    {
      "text": "Sample Text",
      "size": 1092,
      "font_size": 32,
      "alignment": "center"
    }
  6. Generate an adversarial image

    main

    Generate an adversarial image by combining a decoy image with a target text image using specific interpolation-based methods (bicubic, bilinear, or nearest).

    Request Body (JSON):

    • method: bicubic, bilinear, or nearest.
    • text: The text to be used in the target image.
    • decoy_filename: The filename of the decoy image to use.
    • font_size: Font size for the target text.
    • alignment: Text alignment.
    • lam: (Optional) Lambda parameter.
    • eps: (Optional) Epsilon parameter.
    • gamma: (Optional) Gamma target parameter.
    • dark_frac: (Optional) Dark fraction parameter (for bicubic and bilinear).
    • offset: (Optional) Offset parameter (only for nearest).

    Note: The decoy image must be exactly 4x the size of the generated target image.

    Response (JSON): Returns the adversarial_image (base64), the target_image (base64), the original_decoy (base64), and the parameters used.

    POST /api/generate-adversarial
    {
      "method": "bicubic",
      "text": "Sample Text",
      "decoy_filename": "1024_*.png",
      "lam": 0.25,
      "eps": 0.0,
      "gamma": 1.0,
      "dark_frac": 0.3
    }