A Python package for benchmarking adversarial attacks and defenses.
Project description
AttackBench: Evaluating Gradient-based Attacks for Adversarial Examples
Antonio Emanuele Cinà, Jérôme Rony, Maura Pintor, Luca Demetrio, Ambra Demontis, Battista Biggio, Ismail Ben Ayed, Fabio Roli, and Riccardo Trebiani
Leaderboard: https://attackbench.github.io/
Paper: https://arxiv.org/pdf/2404.19460
How it works
The AttackBench framework wants to fairly compare gradient-based attacks based on their security evaluation curves. To this end, we derive a process involving five distinct stages, as depicted below.
- In stage (1), we construct a list of diverse non-robust and robust models to assess the attacks' impact on various settings, thus testing their adaptability to diverse defensive strategies.
- In stage (2), we define an environment for testing gradient-based attacks under a systematic and reproducible protocol. This step provides common ground with shared assumptions, advantages, and limitations. We then run the attacks against the selected models individually and collect the performance metrics of interest in our analysis, which are perturbation size, execution time, and query usage.
- In stage (3), we gather all the previously-obtained results, comparing attacks with the novel
local optimalitymetric. - Finally, in stage (4), we aggregate the optimality results from all considered models, and in stage (5) we rank the attacks based on their average optimality, namely
global optimality.
Currently implemented
| Attack | Original | Advertorch | Adv_lib | ART | CleverHans | DeepRobust | Foolbox | Torchattacks |
|---|---|---|---|---|---|---|---|---|
| DDN | ☒ | ✓ | ☒ | ☒ | ☒ | ✓ | ☒ | |
| ALMA | ☒ | ☒ | ✓ | ☒ | ☒ | ☒ | ☒ | ☒ |
| FMN | ✓ | ☒ | ✓ | ☒ | ☒ | ☒ | ✓ | ☒ |
| PGD | ☒ | ✓ | ✓ | ✓ | ✓ | |||
| JSMA | ☒ | ☒ | ✓ | ☒ | ☒ | ☒ | ☒ | |
| CW-L2 | ☒ | ✓ | ✓ | ~ | ✓ | ✓ | ||
| CW-LINF | ☒ | ☒ | ✓ | ✓ | ☒ | ☒ | ☒ | ☒ |
| FGSM | ☒ | ☒ | ✓ | ✓ | ||||
| BB | ☒ | ☒ | ☒ | ✓ | ☒ | ☒ | ✓ | ☒ |
| DF | ✓ | ☒ | ☒ | ✓ | ☒ | ~ | ✓ | ✓ |
| SuperDF | ✓ | ☒ | ☒ | ☒ | ☒ | ☒ | ☒ | ☒ |
| APGD | ✓ | ☒ | ✓ | ✓ | ☒ | ☒ | ☒ | ✓ |
| BIM | ☒ | ☒ | ✓ | ☒ | ☒ | |||
| EAD | ☒ | ☒ | ✓ | ☒ | ☒ | ✓ | ☒ | |
| PDGD | ☒ | ☒ | ✓ | ☒ | ☒ | ☒ | ☒ | ☒ |
| PDPGD | ☒ | ☒ | ✓ | ☒ | ☒ | ☒ | ☒ | ☒ |
| TR | ✓ | ☒ | ✓ | ☒ | ☒ | ☒ | ☒ | ☒ |
| FAB | ✓ | ✓ | ☒ | ☒ | ☒ | ☒ | ✓ |
Legend:
- empty : not implemented yet
- ☒ : not available
- ✓ : implemented
- ~ : not functional yet
Requirements and Installation
- Python >= 3.9, < 3.13
- PyTorch >= 2.4
- TorchVision >= 0.19
- CUDA compatible GPU (recommended)
Install from PyPI
pip install attackbench
Optional dependencies
# Attack library wrappers (ART, Foolbox, Torchattacks, CleverHans, RobustBench)
pip install "attackbench[attacks]"
# Model loading utilities (RobustBench, timm, transformers)
pip install "attackbench[models]"
# Analysis and visualization tools (scikit-learn, seaborn, plotly)
pip install "attackbench[metrics]"
# Everything (attacks + models + metrics)
pip install "attackbench[all]"
Note:
adv-libis not on PyPI. Install it manually if needed:pip install git+https://github.com/jeromerony/adversarial-library
deeprobustrequiresscipy<1.8.0and only works on Python 3.9:pip install "attackbench[deeprobust]"
Install from source (development)
git clone https://github.com/attackbench/AttackBench.git
cd AttackBench
pip install -e ".[dev]"
Usage
import torch
import attackbench
from attackbench.attacks import apgd
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load model and dataset
model = attackbench.get_model('Standard')
model.to(device)
dataset = attackbench.get_loader(dataset='cifar10', batch_size=128, num_samples=1000)
# Run attack
results = attackbench.run_attack(
model=model,
dataset=dataset,
attack=apgd,
threat_model='linf',
device=device
)
# Analyze results (requires attackbench[metrics])
stats = attackbench.get_stats(results, 'linf')
print(f"ASR: {stats['asr']*100:.1f}%")
Preconfigured attacks available out of the box: pgd, fgsm, apgd, fab, fmn, deepfool, superdeepfool, trust_region.
To use attacks from external libraries (requires attackbench[attacks]):
# List available attacks
attacks = attackbench.list_attacks(threat_model='linf')
# Load a specific library attack
art_pgd = attackbench.get_attack(lib='art', attack='pgd', threat_model='linf')
results = attackbench.run_attack(model=model, dataset=dataset, attack=art_pgd, threat_model='linf', device=device)
Attack format
Tthe wrappers for all the implementations (including libraries) must have the following format:
- inputs:
model:nn.Moduletaking inputs in the [0, 1] range and returning logits in $\mathbb{R}^K$inputs:FloatTensorrepresenting the input samples in the [0, 1] rangelabels:LongTensorrepresenting the labels of the samplestargets:LongTensororNonerepresenting the targets associated to each samplestargeted:boolflag indicating if a targeted attack should be performed
- output:
adv_inputs:FloatTensorrepresenting the perturbed inputs in the [0, 1] range
Citation
If you use the AttackBench leaderboards or implementation, then consider citing our paper:
@inproceedings{cina2025attackbench,
title={Attackbench: Evaluating gradient-based attacks for adversarial examples},
author={Cin{\`a}, Antonio Emanuele and Rony, J{\'e}r{\^o}me and Pintor, Maura and Demetrio, Luca and Demontis, Ambra and Biggio, Battista and Ayed, Ismail Ben and Roli, Fabio},
booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
volume={39},
number={3},
pages={2600--2608},
year={2025},
DOI={10.1609/aaai.v39i3.32263}
}
Contact
Feel free to contact us about anything related to AttackBench by creating an issue, a pull request or
by email at antonio.cina@unige.it.
Acknowledgements
AttackBench has been partially developed with the support of European Union’s ELSA – European Lighthouse on Secure and Safe AI, Horizon Europe, grant agreement No. 101070617, and Sec4AI4Sec - Cybersecurity for AI-Augmented Systems, Horizon Europe, grant agreement No. 101120393.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file attackbenchlib-1.0.0a10.tar.gz.
File metadata
- Download URL: attackbenchlib-1.0.0a10.tar.gz
- Upload date:
- Size: 474.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7a49d09c5bab2b3d20ff1bad34bbb1410d6a68e8a3d033575824c471162a9c1
|
|
| MD5 |
c330732884be824911c81c0ee0f5526a
|
|
| BLAKE2b-256 |
274f43d184397155056d42cef9e018e49a9bc823a094b043251de206e6d297d4
|
File details
Details for the file attackbenchlib-1.0.0a10-py3-none-any.whl.
File metadata
- Download URL: attackbenchlib-1.0.0a10-py3-none-any.whl
- Upload date:
- Size: 144.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c67a24c4027958429f93847f7cf8e4ca2b7b4f43cdaf345178db5eef7f1f197
|
|
| MD5 |
8a8a65a4932a3def6b5b4ad9eedf1250
|
|
| BLAKE2b-256 |
cf899d5cf7985d676aace99296d1942afd7282d82d153326a8c94d475b328522
|