Official repository for the paper ICL-Evader: Zero-Query Black-Box Evasion Attacks on In-Context Learning and Their Defenses (WWW 2026).
This repository includes both notebook-based and modular CLI-based pipelines.
- Notebook workflow:
blackbox_adv_examples_against_ICL_with_LLMs_defense.ipynbis the original end-to-end experiment notebook.data_preprocessing.ipynbhandles data preprocessing and train/test preparation.
- Modular workflow:
src/icl_evader/contains reusable modules for attacks, defenses, ICL, metrics, and experiments.scripts/run_modular.pyruns baseline, attack, and defense experiment suites.scripts/run_joint_defense.pyhardens a single ICL prompt with the joint defense strategy.
original_data/: raw and intermediate task data.final_data/: processed datasets (typicallytrain.csvandtest.csv).configs/: runnable templates for baseline, attack, defense, and joint defense hardening.
Python version: 3.12
# install uv if needed: https://docs.astral.sh/uv/getting-started/installation/
uv venv --python 3.12
source .venv/bin/activate
uv pip install -r requirements.txtpython3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtUse and customize:
configs/baseline.example.jsonconfigs/attack.example.jsonconfigs/defense.example.json
Recommended setup:
model_pathpoints to your local model checkpoint.task_data_dirspoints to processed datasets underfinal_data/.output_dirpoints to your experiment output location.
python scripts/run_modular.py baseline --config configs/baseline.example.jsonpython scripts/run_modular.py attack --config configs/attack.example.jsonpython scripts/run_modular.py defense --config configs/defense.example.jsonEach run writes results under your configured output_dir, including:
- per-run raw outputs (
*_outputs.pkl) - confidence tables (
*_confidence.csv) - cumulative run log (
results_all.json)
This section is independent from modular reproducibility runs.
Use this flow when you want the CLI to infer missing prompt attributes from an OpenAI-compatible API.
- Export your API key
export OPENAI_API_KEY="<your_api_key>"- Configure endpoint and model in
configs/joint_defense.llm.example.yaml
# OpenAI default (change to your provider if needed)
endpoint: "https://api.openai.com/v1"
model: "gpt-4o-mini"
api_key_env: "OPENAI_API_KEY"
timeout: 30Notes:
endpointcan be either a full.../chat/completionsURL or a base URL (for examplehttps://api.openai.com/v1).- For non-OpenAI providers, set
endpointto your provider's OpenAI-compatible base URL. - Keep secrets in environment variables; avoid committing literal keys.
- Joint defense with explicit defense config only
mkdir -p defense_test/outputs
python scripts/run_joint_defense.py \
--prompt-file defense_test/prompts/toxic_prompt.txt \
--config defense_test/configs/toxic_config.yaml \
--output defense_test/outputs/toxic_hardened.txt \
--output-meta defense_test/outputs/toxic_meta.json- Joint defense with LLM-assisted attribute extraction
mkdir -p defense_test/outputs
python scripts/run_joint_defense.py \
--prompt-file defense_test/prompts/toxic_prompt.txt \
--llm-config configs/joint_defense.llm.example.yaml \
--output defense_test/outputs/toxic_hardened.txt \
--output-meta defense_test/outputs/toxic_meta.jsonEquivalent via modular entrypoint:
python scripts/run_modular.py joint-defense \
--prompt-file defense_test/prompts/toxic_prompt.txt \
--config defense_test/configs/toxic_config.yaml \
--output defense_test/outputs/toxic_hardened.txtIf required attributes are missing and no LLM config is provided, the script exits with explicit missing-field guidance.
Run sample prompts/configs and print hardened prompts directly in terminal:
bash defense_test/run_render_examples.shGenerated files are written to defense_test/outputs/.
For deeper modular documentation, see MODULAR_README.md.