You can steal a machine learning model by repeatedly querying its API and observing the outputs.

Let’s say you have a service that offers a machine learning model via an API. You send it an input, and it sends back a prediction. Seems straightforward, right? But what if someone could use just those input-output pairs to reconstruct your model, or at least a very good approximation of it? That’s the core idea behind model extraction attacks.

Imagine a black box. You can poke it with different things (inputs) and see what comes out (outputs). If you poke it enough times with carefully chosen inputs, you can start to map out its internal workings, effectively "stealing" the logic it uses.

Here’s a simplified example. Suppose we have a model that classifies images of cats and dogs.

# Example API endpoint (simulated)
def predict(image_data):
    # This is our secret model, but the attacker only sees the output
    if "cat_features" in image_data:
        return "cat"
    else:
        return "dog"

# Attacker's perspective:
# They don't know the 'predict' function's logic.
# They can only send 'image_data' and get back "cat" or "dog".

# Attacker's queries:
query1_input = {"image_data": "some_image_with_cat_features"}
output1 = predict(query1_input) # attacker sees "cat"

query2_input = {"image_data": "some_other_image_with_dog_features"}
output2 = predict(query2_input) # attacker sees "dog"

# ... millions of such queries ...

The attacker’s goal is to build their own model that behaves identically, or nearly identically, to yours. They don’t need the weights or architecture of your neural network; they just need the function it represents.

The problem this solves is protecting your intellectual property. If you’ve spent months training a state-of-the-art model, you don’t want someone to simply query your API a million times and then replicate your model for free. This is especially critical for proprietary models used in competitive domains like finance, personalized recommendations, or specialized medical diagnostics.

Internally, the attacker is essentially performing supervised learning on your model. They are generating their own training dataset where the "labels" are the predictions from your API. If your API is the oracle, they are using it to label their synthetic data.

The levers you control are primarily:

  • Rate Limiting: How many queries can be made in a given time frame?
  • Output Perturbation: Can you subtly alter the output to make it harder to learn?
  • Input Validation/Sanitization: Can you detect and block suspicious query patterns?
  • Model Complexity/Output Granularity: Does your API reveal too much information?

Let’s look at a more concrete scenario. Imagine a sentiment analysis API.

// Attacker's Query
{
  "text": "This movie was absolutely fantastic!"
}

// API Response
{
  "sentiment": "positive",
  "confidence": 0.98
}

The attacker might start with generic phrases:

  • "This is good." -> positive
  • "This is bad." -> negative
  • "It’s okay." -> neutral

Then, they get more sophisticated, trying to probe the model’s decision boundaries:

  • "This movie was good, but the acting was terrible." -> mixed/neutral (depending on your model)
  • "I didn’t hate it." -> positive (a trickier case)

By collecting thousands or millions of such input-output pairs, the attacker can train their own model. If your model is a deep neural network, they might train a simpler model (like a logistic regression or a smaller neural net) on the extracted data, or they might try to train a network with a similar architecture. The more diverse and representative their queries are, the better their replica will be.

The key insight is that even a highly complex model can be approximated by a simpler one if you have enough data points. Think of it like fitting a curve to a set of points. The more points you have, the better you can define the curve.

The most surprising aspect is how little information the attacker actually needs. They don’t need to know how your model works, only what it outputs for given inputs. This means that even if your model is incredibly complex and your training data is secret, the API itself can become the vulnerability. The output of the model, even if it’s just a class label or a probability, is a form of information leakage.

When attackers want to extract a model, they often don’t just send random inputs. They use techniques like gradient-based inference if they can get gradient information (though this is less common with simple API predictions) or genetic algorithms to evolve queries that are most likely to reveal discriminative features. They might also train a surrogate model – a simpler model that tries to mimic the behavior of the target model.

If you’re running an ML API, don’t just think about the prediction itself. Consider what information that prediction reveals. Is it a binary "yes/no," a probability score, or a full probability distribution over classes? The more granular the output, the easier it is to extract. For instance, a model that returns {"class": "cat", "confidence": 0.99} is more vulnerable than one that just returns {"class": "cat"}.

The next problem you’ll encounter is defending against query-flooding attacks, where attackers try to overwhelm your API with legitimate-looking queries to extract information.

Want structured learning?

Take the full AI Security course →