keepcalculator

06 Numbers and Powers

Protect AI from Model Extraction & Distillation

Card No. 06·64105 · Updated · Standard formula, shown below

Model extraction and distillation attacks let an attacker recreate a close copy of your AI model by sending many queries and training a substitute on the responses. This guide explains how to detect and slow these attacks while keeping your model useful. You will need access to your model’s serving configuration, logging, and rate-limiting tools; no special hardware is required.

What You’ll Need

  • Admin access to your model’s inference endpoint (API gateway, cloud console, or web server)
  • Logging system that records request metadata (IP, API key, timestamp, payload size)
  • Rate-limiting or API management tool (most cloud providers include this)
  • Ability to modify model responses (e.g., add noise, round outputs, or reject certain queries)
  • Basic understanding of your model’s training data distribution

Step-by-Step

  1. Identify your attack surface. Determine which endpoints are public and which are internal. Model extraction usually targets public APIs that return detailed predictions (logits, confidence scores, or full text). If you expose a chat or classification API, assume it can be probed.
  2. Add authentication and quotas. Require an API key for every request. Set per-key daily quotas and per-minute rate limits. This doesn’t stop a determined attacker but forces them to rotate keys and slows the extraction.
  3. Monitor for anomalous query patterns. Log and review requests for signs of extraction: high volume from a single IP or key, requests with many different inputs in a short time, queries that cover a suspiciously wide range of the input space, or a high ratio of requests to unique users. Set alerts for these patterns.
  4. Limit output precision. The most effective defense is to reduce the information per response. For classification models, return only the top-1 label instead of full probability vectors. For regression or text models, round outputs to fewer decimal places or truncate responses. This makes it harder for the attacker to train a high-fidelity copy.
  5. Add noise to outputs. Inject small random noise into scores or predictions. The noise should be small enough not to harm legitimate users but large enough to degrade the attacker’s training signal. For example, add ±0.01 to confidence scores.
  6. Detect and block suspicious IPs. Use your rate-limiting tool to temporarily block IPs that exceed thresholds or show extraction-like behavior. Combine with CAPTCHA challenges for high-volume anonymous requests.
  7. Use differential privacy during training (if you can). If you control the training pipeline, train with differential privacy (DP-SGD). This bounds how much any single query can reveal about the model, making extraction inherently less accurate. This is a long-term fix, not a quick patch.
  8. Watermark your model. Embed a unique pattern in outputs that is invisible to normal users but detectable if the attacker’s copy is later used. For example, add a specific bias to certain rare inputs. This helps you prove extraction but does not prevent it.
  9. Rotate models periodically. If you update your model every few weeks, an extracted copy becomes stale. This is especially effective for models that are retrained on new data.
  10. Set up a honeypot endpoint. Create a decoy API that returns deliberately wrong or noisy answers. If you see heavy traffic to it, you know you’re under attack and can trace the attacker’s methods.

Common Mistakes

  • Over-restricting legitimate users. Aggressive rate limits or heavy noise can frustrate paying customers. Balance security with usability.
  • Only monitoring volume. Extraction attacks can be slow and distributed across many IPs. Look for patterns like high entropy in inputs or many requests for rare classes.
  • Ignoring the API response format. Even if you hide probabilities, returning the top-5 labels still leaks information. The more you return, the easier extraction is.
  • Assuming on-premise models are safe. If your model is embedded in a client app, the attacker can extract it directly from memory. This guide focuses on server-side APIs; for on-device models, consider obfuscation or server-side validation.
  • Not testing the impact of noise. Adding too much noise can break your product’s functionality. Always evaluate on your real use case.

Tips

  • Start with the cheapest defenses (rate limits, output rounding) and add more complex ones only if you see actual attacks.
  • Log every request for at least 90 days so you can retroactively analyze an attack.
  • Use anomaly detection tools that learn your normal traffic baseline; they can catch subtle extraction attempts.
  • For deep learning models, consider using model distillation itself to create a smaller, less informative public version while keeping the full model internal.
  • Coordinate with your security team to run a red-team exercise: try to extract your own model to see how hard it is.

Troubleshooting

  • Legitimate users complain about accuracy. Reduce the noise or rounding. If you must keep defenses, consider offering a premium tier with higher precision.
  • Attackers are still getting good copies. They may be using many IPs or rotating keys. Increase quotas per key, add IP-based limits, and require more human verification (CAPTCHA) for anonymous access.
  • You see a spike in traffic but can’t tell if it’s an attack. Look at the content of the queries. Extraction often uses a systematic grid of inputs (e.g., all combinations of values). Normal users have a more natural distribution.
  • Rate limiting is breaking your own integration. Make sure your own services use a separate, higher quota. Use API keys for internal services.
  • You suspect a specific customer is extracting. Review their usage pattern and contact them. If it’s a business relationship, a warning may suffice.

Frequently Asked Questions

Q: Can I completely prevent model extraction?
A: No. Any model that is accessible via an API can be approximated by enough queries. The goal is to make extraction so expensive that the attacker gives up.

Q: Does adding noise hurt my model’s accuracy for everyone?
A: It can, but you can tune the noise level. Often a tiny amount (e.g., 0.01 on a 0–1 scale) has negligible impact on real users but significantly degrades the attacker’s copy.

Q: Is model extraction illegal?
A: It may violate your terms of service or intellectual property laws, but laws vary by jurisdiction. From a technical standpoint, treat it as a security risk regardless of legality.

Q: How do I know if my model has been extracted?
A: If you see a suspiciously similar model elsewhere, you can test with watermark inputs. Without a watermark, it’s hard to prove. That’s why watermarking is recommended.

Q: Do these defenses work for large language models (LLMs)?
A: Partially. LLMs are harder to extract because their output space is huge, but attackers can still fine-tune a small model on your responses. Truncating responses and rate limiting help, but you may need to monitor for prompt patterns that try to elicit training data.

Q: What is the difference between model extraction and model stealing?
A: They are often used interchangeably. Extraction usually refers to creating a functional copy via queries; stealing can also include exfiltrating weights directly (e.g., through a vulnerability).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →