Notes AI Security LLM Attacks

LLM Attacks

garak, PyRIT and ART

Introduction

Manual prompting is enough to find a first weakness. Tools repeat hundreds of known attacks against a target, record which ones worked, and re-run the same test after the target changes. The three tools here cover different targets:

ToolMade byTargetsWhat it does
garakNVIDIALLMs and LLM applicationsRuns a large library of known attack prompts and reports which ones succeeded
PyRITMicrosoftLLMs and LLM applicationsFramework for building custom and multi-turn attack runs with automatic scoring
ARTLinux Foundation AI & Data (originally IBM)Classic ML models: classifiers, image models, tabular modelsLibrary of evasion, poisoning, extraction and inference attacks, plus defenses

garak and PyRIT send text to a model and judge the text that comes back. ART works on the model object itself and needs the model, or at least its predictions, in Python.

Only run these against systems you are authorized to test. They send many requests, which costs money on paid APIs and can trip rate limits or alerts.

garak

garak (Generative AI Red-teaming and Assessment Kit) is a command line scanner, similar in use to nmap or nikto for web servers. It has four kinds of component:

  • Generators: the connection to the target, such as OpenAI, Hugging Face, a local model, or a REST endpoint.
  • Probes: the attacks. Each probe sends a set of prompts that tries to produce one kind of failure, such as dan (DAN jailbreaks), promptinject, encoding (instructions hidden in Base64 and similar), leakreplay (training data leakage) or malwaregen.
  • Detectors: check each reply to decide whether the attack worked, for example whether the model produced the forbidden string or complied with the jailbreak.
  • Buffs: transform the prompts before they are sent, for example paraphrasing them or changing the encoding, to multiply the number of variants.

Install it and list what is available:

python -m pip install -U garak
python -m garak --list_probes

Run a probe family against a model:

export OPENAI_API_KEY="..."
python -m garak --target_type openai --target_name gpt-4o-mini --probes encoding

Use --probes dan,promptinject,encoding to run several families, and leave --probes out to run every probe, which takes a long time. Older versions use --model_type and --model_name for the two target options, so check python -m garak --help for the version you have. Models that run locally use --target_type huggingface with the Hugging Face model name. For a chatbot behind a web endpoint, the rest generator takes a JSON file that describes the request format and where the reply is in the response.

garak runs each prompt several times, since replies vary, and writes a report with a pass or fail rate per probe plus a log of every prompt and reply. Read the logged replies for the failures: detectors produce false positives and false negatives, and the counts alone do not show how serious a failure is.

PyRIT

PyRIT (Python Risk Identification Tool) is Microsoft’s framework for automating AI red teaming. garak is a scanner with a fixed library. PyRIT is a set of parts you combine, which suits attacks that need several turns or a custom target. The documentation describes four core parts:

  • Targets: what receives the prompts. Built-in targets cover OpenAI, Azure, Anthropic, Hugging Face and generic HTTP endpoints, and you can write your own.
  • Converters: transform a prompt before it is sent, and they can be stacked. Examples are Base64 encoding, translation, rewording by another LLM, or turning the text into an image or document.
  • Scorers: judge each reply, using a true or false check, a rating scale, a classifier, or another LLM that is asked whether the reply is harmful.
  • Attacks: the algorithm that ties the others together. It sends prompts to the target, applies converters, and uses the scorer’s result to choose the next prompt. Single-turn and multi-turn attacks are included, such as Crescendo (gradually escalating the conversation), TAP (a tree search over prompt variations) and Skeleton Key.

The multi-turn attacks use a second LLM as the attacker. It writes each new message based on the target’s last reply, which automates the manual multi-turn jailbreaking described in Jailbreaking.

python -m pip install pyrit

PyRIT also stores every prompt and reply in a database, so a run can be reviewed and compared with later runs. Its Python interface and class names have changed between releases, so use the examples in the documentation for the version you install. Older tutorials often use class names that no longer exist. It can also run as a command line scanner (pyrit_scan) over bundled scenarios.

Adversarial Robustness Toolbox (ART)

ART is a Python library for attacking and defending machine learning models, and it works with scikit-learn, PyTorch, TensorFlow, Keras and XGBoost among others. It covers four threat categories:

  • Evasion: modify an input so the model gets it wrong (art.attacks.evasion).
  • Poisoning: alter training data to change what the model learns (art.attacks.poisoning).
  • Extraction: copy a model through its answers (art.attacks.extraction).
  • Inference: recover private information, including whether a record was in the training data, and reconstruct training inputs (art.attacks.inference).

It also has defenses in art.defences, such as adversarial training and input preprocessing, so the same library measures how much a defense helps.

The example below runs an evasion attack: it changes test inputs slightly so that a trained model classifies them wrongly. The data is scikit-learn’s load_digits, small images of handwritten digits from 0 to 9. Each image is 8 by 8 pixels, so it is a row of 64 numbers, one per pixel, and its label is the digit it shows.

The model is a logistic regression trained to recognize the digits. The attack is the Fast Gradient Method. For every pixel of every test image, it checks whether making the pixel darker or lighter makes the model more wrong, and moves the pixel a small fixed amount in that direction.

Before attacking, the model is wrapped in an ART estimator. ART works with models from scikit-learn, PyTorch, TensorFlow and other libraries, and the wrapper gives every attack the same functions to call on the model, whichever library built it.

from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from art.estimators.classification import SklearnClassifier
from art.attacks.evasion import FastGradientMethod

# X: one row of 64 pixel values per image. y: the digit each image shows
X, y = load_digits(return_X_y=True)
# Pixels go from 0 to 16. Dividing by 16 puts them between 0 and 1,
# so a change of 0.2 means a fifth of the way from white to black
X = X / 16.0
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Train the model to be attacked. max_iter=1000 gives training enough steps to finish.
# score() predicts every test image and returns the share it got right
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
print("clean accuracy:", model.score(X_test, y_test))

# 1. Wrap the model for ART. clip_values keeps every pixel between 0 and 1,
#    so the attack cannot produce pixel values that real images never have
classifier = SklearnClassifier(model=model, clip_values=(0.0, 1.0))

# 2. Choose the attack. eps=0.2 is the most any single pixel may change
attack = FastGradientMethod(estimator=classifier, eps=0.2)

# 3. Attack the test images. X_adv holds the changed copies ("adv" for adversarial)
X_adv = attack.generate(x=X_test)

# The same model, the same correct answers, only the images changed
print("accuracy on adversarial inputs:", model.score(X_adv, y_test))

Each pixel changes by at most 0.2, but all 64 change at once, and each one moves in the direction that makes the model more wrong. The model’s score for a digit adds up every pixel times its weight, so 64 small changes in the worst direction add up to a large change in the score. That is enough to push many images across the decision boundary, while a person looking at them still sees the same digits.

eps is the largest change allowed to each feature (here, to each pixel). Raising it lowers accuracy further and makes the changes more visible. The accuracy drop from the clean set to the adversarial set is the measurement to report.

ART does not attack chat models through text prompts. For a chat application, use garak or PyRIT. For the classifiers and image models in the rest of this handbook, use ART to produce adversarial examples.