ThisCalculator

Everyday

Detect Unusual API Traffic for Model Extraction

Updated · Standard formula, shown below

This guide helps you spot API traffic patterns that may indicate someone is trying to extract your machine learning model. You’ll learn what to monitor, which signals are red flags, and how to respond. You’ll need access to your API gateway or web server logs, a basic understanding of HTTP status codes, and the ability to run simple log queries.

What You’ll Need

  • Access to API access logs (e.g., from Nginx, AWS API Gateway, or a logging service like Splunk or ELK).
  • A tool to query and analyze logs (e.g., grep, awk, or a log analytics platform).
  • Knowledge of your API’s normal traffic patterns (baseline).
  • An alerting mechanism (optional but helpful) for real-time detection.

Step-by-Step

  1. Establish a baseline – Review your API logs over the past 30 days. Note the average requests per minute, typical request size, and common user agents. This gives you a reference point for what is “normal.”
  2. Monitor request volume spikes – Use your log query tool to count requests per IP address or per API key over time. A sudden, sustained spike (e.g., 10x normal) from a single source is a red flag. Set a threshold alert for when requests exceed, say, 5 standard deviations from the mean.
  3. Check for high-frequency, low-latency calls – Model extraction often involves sending many requests quickly to probe the model’s responses. Look for IPs that make hundreds or thousands of requests within a few minutes, especially if the response time is consistently under 100ms (indicating automated calls).
  4. Examine request payload patterns – For text models, look for requests that contain systematic variations of a base input (e.g., changing one word at a time). For image models, check for repeated requests with similar but slightly altered images. This can be done by hashing request bodies and grouping by similarity.
  5. Analyze response size and content – If your API returns probabilities or confidence scores, a scraper might request the same input multiple times to average out randomness. Look for repeated identical requests. Also, if responses are unusually large (e.g., full model weights), that’s a critical alert.
  6. Review user agent and IP reputation – Filter logs for user agents that are not typical browsers (e.g., Python-requests, curl, custom strings). Cross-reference IPs with known cloud provider ranges (AWS, GCP, Azure) – attackers often use cloud VMs. Free IP reputation services can flag suspicious sources.
  7. Check for abnormal API key usage – If you use API keys, look for a single key being used from multiple IPs simultaneously, or a key that suddenly starts making requests at all hours (if it previously was only active during business hours).
  8. Look for sequential probing of endpoints – An attacker might try to access undocumented endpoints or versioned endpoints (e.g., /v1/model, /v2/model) to see what’s available. Monitor 404 errors – a sudden spike in 404s from one IP could be a scan.
  9. Set up automated alerts – Use your log analytics tool to create alerts for the patterns above. For example, alert when any single IP exceeds 1000 requests per hour, or when the ratio of 4xx/5xx errors to successful requests jumps.
  10. Investigate and respond – When you see a red flag, manually inspect the logs. If the traffic looks like extraction (e.g., systematic probing), block the IP or revoke the API key, and consider rate-limiting. Document the incident for future reference.

Common Mistakes

  • Ignoring the baseline – Without a baseline, you’ll either miss real attacks or trigger false alarms on normal traffic.
  • Only looking at total volume – A slow, distributed attack (from many IPs) can fly under the radar if you only check per-IP counts.
  • Not logging request bodies – If you don’t log the payload, you can’t detect systematic input variations.
  • Overlooking legitimate high-volume users – Some customers might have a legitimate reason for high traffic (e.g., batch processing). Always verify before blocking.
  • Setting alerts too low – Alerts that trigger on every small spike will be ignored. Set thresholds based on your baseline.

Tips

  • Use rate limiting as a first line of defense – Even if you can’t detect extraction in real-time, rate limiting slows it down.
  • Add a CAPTCHA or challenge for suspicious patterns – This can deter automated scripts without affecting normal users.
  • Monitor for ‘distillation’ attacks – An attacker may use your model to train a cheaper substitute. Look for a high volume of requests with diverse inputs, not just repeated ones.
  • Keep logs for at least 90 days – Extraction attempts can be slow and spread over weeks. Long retention helps with post-incident analysis.
  • Rotate API keys regularly – Even if a key is compromised, rotation limits the damage.

Troubleshooting

  • You see a spike but it’s not an attack – Check if it coincides with a marketing campaign, a new feature release, or a scheduled data job. Verify with the team responsible.
  • Your log query is too slow – If you’re querying millions of rows, use pre-aggregated summaries or a log analytics service that indexes fields.
  • You can’t identify the source IP – If your API is behind a load balancer, ensure you’re logging the X-Forwarded-For header, not just the load balancer’s IP.
  • You block a legitimate user – Have a process to unblock quickly. Use a temporary block (e.g., 24 hours) and review.
  • Alerts are overwhelming – Tune thresholds or use severity levels. Start with high-precision alerts (e.g., >10,000 requests/hour from one IP) and add more granular ones later.

Frequently Asked Questions

  1. How can I tell if someone is extracting my model vs. just using my API normally? – Look for systematic patterns: many requests with slight input variations, high request rates, and responses that are used to train a substitute model. Normal usage is more random and lower volume.
  2. What’s the most common sign of model extraction? – A sudden, sustained spike in requests from a single IP or API key, especially if the requests are automated (low latency, no browser user agent).
  3. Can I detect extraction if the attacker uses multiple IPs? – Yes, but it’s harder. Look for coordinated patterns across IPs, such as the same input variations, or a high total volume from a cloud provider’s IP range.
  4. Should I block an IP immediately if I see suspicious traffic? – Not always. First, verify it’s not a legitimate customer. If it is malicious, block it, but also consider rate-limiting instead of a hard block to avoid collateral damage.
  5. What if my model is open-source? Should I still worry? – If your model is open-source, extraction is less of a concern because the weights are public. But you should still monitor for abuse (e.g., using your API to serve illegal content).
  6. How often should I review my API logs? – At least weekly for a small service, but daily if you have high traffic or a valuable model. Automated alerts are essential for real-time detection.

Related

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