Saltar a contenido

apollon.attacks

attacks

Adversarial evaluation with the Adversarial Robustness Toolbox (ART).

Two settings are supported:

  • transfer: adversarial examples are crafted against a single classifier (:func:model_classifier) and then submitted to Apollon;
  • direct: the attack queries Apollon itself (:func:apollon_classifier), so every query goes through cluster assignment and Thompson-sampled selection, exactly as a request reaching the deployed IDS.

Both attacks are black-box and targeted towards the benign class: ZOO (score-based) and HopSkipJump (decision-based). After an attack, :func:restore_functional puts back the value of every functional feature, those an attacker cannot change without breaking the attack itself.

Requires the attacks extra: pip install "apollon-ids[attacks]".

functional_indexes

functional_indexes(columns, functional=FUNCTIONAL_FEATURES)

Indexes of functional (must not change) and modifiable features in columns.

columns are the feature columns in the order the models receive them. Returns (functional, modifiable, missing), where missing lists functional names that are not among the columns.

Source code in src/apollon/attacks.py
def functional_indexes(columns, functional=FUNCTIONAL_FEATURES):
    """Indexes of functional (must not change) and modifiable features in ``columns``.

    ``columns`` are the feature columns in the order the models receive them. Returns
    ``(functional, modifiable, missing)``, where ``missing`` lists functional names that
    are not among the columns.
    """
    columns = [str(c) for c in columns]
    names = set(functional)
    func = [i for i, c in enumerate(columns) if c in names]
    mod = [i for i, c in enumerate(columns) if c not in names]
    missing = [c for c in functional if c not in columns]
    return func, mod, missing

restore_functional

restore_functional(x_adv, x_orig, functional_idx)

Put back the original value of every functional feature.

Source code in src/apollon/attacks.py
def restore_functional(x_adv, x_orig, functional_idx):
    """Put back the original value of every functional feature."""
    x_adv = np.array(x_adv, dtype=float, copy=True)
    x_adv[:, functional_idx] = np.asarray(x_orig, dtype=float)[:, functional_idx]
    return x_adv

model_classifier

model_classifier(model, clip_values=(0, 1))

ART wrapper around a single scikit-learn model (transfer setting).

Source code in src/apollon/attacks.py
def model_classifier(model, clip_values=(0, 1)):
    """ART wrapper around a single scikit-learn model (transfer setting)."""
    _art()
    from art.estimators.classification import SklearnClassifier

    return SklearnClassifier(model=model, clip_values=clip_values)

apollon_classifier

apollon_classifier(model, n_features: int, clip_values=(0, 1))

ART wrapper that queries the whole Apollon system (direct setting).

Probabilities come from the arm selected for each query: ZOO uses the scores and HopSkipJump only the predicted label.

Source code in src/apollon/attacks.py
def apollon_classifier(model, n_features: int, clip_values=(0, 1)):
    """ART wrapper that queries the whole Apollon system (direct setting).

    Probabilities come from the arm selected for each query: ZOO uses the scores and
    HopSkipJump only the predicted label.
    """
    _art()
    from art.estimators.classification import BlackBoxClassifier

    return BlackBoxClassifier(
        predict_fn=lambda x: model.predict_proba(x),
        input_shape=(n_features,),
        nb_classes=2,
        clip_values=clip_values,
    )

run_attack

run_attack(name: str, classifier, x, max_iter: int = 100, nb_parallel: int = 60)

Run a targeted (towards benign) black-box attack and return the adversarial examples.

Parameters:

Name Type Description Default
name str

"zoo" or "hsja" (HopSkipJump).

required
classifier

An ART classifier, from :func:model_classifier or :func:apollon_classifier.

required
x

Attack samples to perturb.

required
max_iter int

Iterations of the attack (100 in the paper).

100
nb_parallel int

Coordinates updated in parallel by ZOO.

60
Source code in src/apollon/attacks.py
def run_attack(name: str, classifier, x, max_iter: int = 100, nb_parallel: int = 60):
    """Run a targeted (towards benign) black-box attack and return the adversarial examples.

    Parameters
    ----------
    name:
        ``"zoo"`` or ``"hsja"`` (HopSkipJump).
    classifier:
        An ART classifier, from :func:`model_classifier` or :func:`apollon_classifier`.
    x:
        Attack samples to perturb.
    max_iter:
        Iterations of the attack (100 in the paper).
    nb_parallel:
        Coordinates updated in parallel by ZOO.
    """
    _art()
    from art.attacks.evasion import HopSkipJump, ZooAttack

    y_target = np.zeros((len(x), 2))
    y_target[:, BENIGN] = 1
    if name == "zoo":
        attack = ZooAttack(
            classifier=classifier,
            targeted=True,
            max_iter=max_iter,
            nb_parallel=nb_parallel,
            verbose=False,
        )
    elif name == "hsja":
        attack = HopSkipJump(classifier=classifier, targeted=True, max_iter=max_iter, verbose=False)
    else:
        raise ValueError(f"unknown attack {name!r}; available: {', '.join(ATTACKS)}")
    return attack.generate(x=np.asarray(x, dtype=float), y=y_target)