|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# text_representation: |
| 5 | +# extension: .py |
| 6 | +# format_name: percent |
| 7 | +# format_version: '1.3' |
| 8 | +# jupytext_version: 1.17.3 |
| 9 | +# --- |
| 10 | + |
| 11 | +# %% [markdown] |
| 12 | +# # The PyRIT CLI |
| 13 | +# |
| 14 | +# The PyRIT cli tool that allows you to run automated security testing and red teaming attacks against AI systems using [scenarios](../scenarios/scenarios.ipynb) for strategies and [configuration](../setup/0_configuration.ipynb). |
| 15 | +# |
| 16 | +# Note in this doc the ! prefaces all commands in the terminal so we can run in a Jupyter Notebook. |
| 17 | +# |
| 18 | +# ## Quick Start |
| 19 | +# |
| 20 | +# For help: |
| 21 | + |
| 22 | +# %% |
| 23 | +# !pyrit_scan --help |
| 24 | + |
| 25 | +# %% [markdown] |
| 26 | +# ### Discovery |
| 27 | +# |
| 28 | +# List all available scenarios: |
| 29 | + |
| 30 | +# %% |
| 31 | +# !pyrit_scan --list-scenarios |
| 32 | + |
| 33 | +# %% [markdown] |
| 34 | +# **Tip**: You can also discover user-defined scenarios by providing initialization scripts: |
| 35 | +# |
| 36 | +# ```shell |
| 37 | +# pyrit_scan --list-scenarios --initialization-scripts ./my_custom_initializer.py |
| 38 | +# ``` |
| 39 | +# |
| 40 | +# This will load your custom scenario definitions and include them in the list. |
| 41 | +# |
| 42 | +# ## Initializers |
| 43 | +# |
| 44 | +# PyRITInitializers are how you can configure the CLI scanner. PyRIT includes several built-in initializers you can use with the `--initializers` flag. |
| 45 | +# |
| 46 | +# The `--list-initializers` command shows all available initializers. Initializers are referenced by their filename (e.g., `objective_target`, `objective_list`, `simple`) regardless of which subdirectory they're in. |
| 47 | +# |
| 48 | +# List the available initializers using the --list-initializers flag. |
| 49 | + |
| 50 | +# %% |
| 51 | +# !pyrit_scan --list-initializers |
| 52 | + |
| 53 | +# %% [markdown] |
| 54 | +# ### Running Scenarios |
| 55 | +# |
| 56 | +# You need a single scenario to run, you need two things: |
| 57 | +# |
| 58 | +# 1. A Scenario. Many are defined in `pyrit.scenarios.scenarios`. But you can also define your own in initialization_scripts. |
| 59 | +# 2. Initializers (which can be supplied via `--initializers` or `--initialization-scripts`). Scenarios often don't need many arguments, but they can be configured in different ways. And at the very least, most need an `objective_target` (the thing you're running a scan against). |
| 60 | +# |
| 61 | +# Basic usage will look something like: |
| 62 | +# |
| 63 | +# ```shell |
| 64 | +# pyrit_scan <scenario> --initializers <initializer1> <initializer2> |
| 65 | +# ``` |
| 66 | +# |
| 67 | +# Or concretely: |
| 68 | +# |
| 69 | +# ```shell |
| 70 | +# !pyrit_scan foundry_scenario --initializers simple openai_objective_target |
| 71 | +# ``` |
| 72 | +# |
| 73 | +# Example with a basic configuration that runs the Foundry scenario against the objective target defined in `openai_objective_target` (which just is an OpenAIChatTarget with `DEFAULT_OPENAI_FRONTEND_ENDPOINT` and `DEFAULT_OPENAI_FRONTEND_KEY`). |
| 74 | + |
| 75 | +# %% |
| 76 | +# !pyrit_scan foundry_scenario --initializers openai_objective_target |
| 77 | + |
| 78 | +# %% [markdown] |
| 79 | +# Or with all options and multiple initializers: |
| 80 | +# |
| 81 | +# ```shell |
| 82 | +# pyrit_scan foundry_scenario --database InMemory --initializers simple objective_target objective_list |
| 83 | +# ``` |
| 84 | +# |
| 85 | +# You can also use custom initialization scripts by passing file paths. It is relative to your current working directory, but to avoid confusion, full paths are always better: |
| 86 | +# |
| 87 | +# ```shell |
| 88 | +# pyrit_scan encoding_scenario --initialization-scripts ./my_custom_config.py |
| 89 | +# ``` |
| 90 | +# |
| 91 | +# #### Using Custom Scenarios |
| 92 | +# |
| 93 | +# You can define your own scenarios in initialization scripts. The CLI will automatically discover any `Scenario` subclasses and make them available: |
| 94 | +# |
| 95 | +# ```python |
| 96 | +# # my_custom_scenarios.py |
| 97 | +# from pyrit.scenarios import Scenario |
| 98 | +# from pyrit.common.apply_defaults import apply_defaults |
| 99 | +# |
| 100 | +# @apply_defaults |
| 101 | +# class MyCustomScenario(Scenario): |
| 102 | +# """My custom scenario that does XYZ.""" |
| 103 | +# |
| 104 | +# def __init__(self, objective_target=None): |
| 105 | +# super().__init__(name="My Custom Scenario", version="1.0") |
| 106 | +# self.objective_target = objective_target |
| 107 | +# # ... your initialization code |
| 108 | +# |
| 109 | +# async def initialize_async(self): |
| 110 | +# # Load your atomic attacks |
| 111 | +# pass |
| 112 | +# |
| 113 | +# # ... implement other required methods |
| 114 | +# ``` |
| 115 | +# |
| 116 | +# Then discover and run it: |
| 117 | +# |
| 118 | +# ```shell |
| 119 | +# # List to see it's available |
| 120 | +# pyrit_scan --list-scenarios --initialization-scripts ./my_custom_scenarios.py |
| 121 | +# |
| 122 | +# # Run it |
| 123 | +# pyrit_scan my_custom_scenario --initialization-scripts ./my_custom_scenarios.py |
| 124 | +# ``` |
| 125 | +# |
| 126 | +# The scenario name is automatically converted from the class name (e.g., `MyCustomScenario` becomes `my_custom_scenario`). |
| 127 | +# |
| 128 | +# |
| 129 | +# ## When to Use the Scanner |
| 130 | +# |
| 131 | +# The scanner is ideal for: |
| 132 | +# |
| 133 | +# - **Automated testing pipelines**: CI/CD integration for continuous security testing |
| 134 | +# - **Batch testing**: Running multiple attack scenarios against various targets |
| 135 | +# - **Repeatable tests**: Standardized testing with consistent configurations |
| 136 | +# - **Team collaboration**: Shareable configuration files for consistent testing approaches |
| 137 | +# - **Quick testing**: Fast execution without writing Python code |
| 138 | +# |
| 139 | +# |
| 140 | +# ## Complete Documentation |
| 141 | +# |
| 142 | +# For comprehensive documentation about initialization files and setting defaults see: |
| 143 | +# |
| 144 | +# - **Configuration**: See [configuration](../setup/0_configuration.ipynb) |
| 145 | +# - **Setting Default Values**: See [default values](../setup/default_values.md) |
| 146 | +# - **Writing Initializers**: See [Initializers](../setup/pyrit_initializer.ipynb) |
| 147 | +# |
| 148 | +# Or visit the [PyRIT documentation website](https://azure.github.io/PyRIT/) |
0 commit comments