Model and data securityAdvancedLesson 1716 min read

Stealing models and training data

A model behind an API can leak itself and the data it learned from. Learn about model extraction, membership inference and memorisation, and the controls that reduce them.

Lesson in motion

In 60 seconds

Stealing models and training data

A model behind an API can leak itself and the data it learned from. Learn about model extraction, membership inference and memorisation, and the controls that reduce them.

1/4
In simple words
If you ask a shopkeeper the price of every item, one by one, you can copy his whole price list without ever seeing it. Asking a model many questions can let someone copy what it knows in a similar way.
Models are valuable. They cost money to train, and they may have learned from private data. Even without breaking into your servers, an attacker who can use the model may be able to copy it or learn about its training data.

Three research-established risks

AttackWhat the attacker wantsHow, in simple terms
Model extractionA copy of your model's behaviourSend many queries, collect the answers, and train their own model to imitate them
Membership inferenceTo know if a specific record was in your training dataModels are often more confident on examples they trained on; compare how the model reacts
Training-data extractionThe actual text or data the model memorisedPrompt the model in ways that make it repeat rare training content, such as a phone number that appeared many times
All three are well documented in academic research. How easy they are in practice depends on the model, how much it overfits, what the API returns, and how many queries are allowed.

Why memorisation happens

  • Content repeated many times in training data is more likely to be memorised word for word.
  • Larger models can store more rare details.
  • Training too long on a small dataset (overfitting) makes the model remember examples instead of general patterns.
  • Secrets and personal data that were never meant to be in the data, such as keys in code or IDs in chat logs, can come back out.
Danger
If a secret, password or personal record is in the training data, assume it might come out of the model. Removing it from the model later is hard. Keep it out of the data in the first place.

Defences

ControlHelps againstNotes
Rate limits and quotas per user and per keyExtractionMakes collecting large volumes slow and costly
Monitor query patternsExtraction, membership inferenceWatch for unusual volume, systematic coverage of inputs, or many near-identical queries
Limit outputsExtraction, membership inferenceReturn a label or a rounded score instead of full probabilities, where the product allows
Deduplicate training dataData extractionRepeated content is more likely to be memorised
Scrub secrets and personal dataData extractionScan for keys, IDs, phone numbers and emails before training
Differential privacy in trainingMembership inference, data extractionGives a mathematical limit on how much one record can affect the model, at some cost in accuracy
Terms of use and legal controlsExtractionForbid using outputs to train competing models; enforce with monitoring
Note
Output filters that block known secrets or personal data patterns are a useful last layer, but they only catch what they know to look for.
Try this. For one model API you run, find out: what does it return (label, score, full probabilities, text)? What is the per-key rate limit? Who looks at usage patterns?

Watch and read more

Lab

A monitoring rule and output policy for a model API, plus a secret scan of a training sample.

~25 min

The problem

Work on paper or with your own sample data.

  1. For a model API you run or design, write an output policy: exactly what is returned to callers, and why.
  2. Write two monitoring rules in plain language, such as "alert when one key sends more than N queries in an hour" or "alert when one key's queries cover an unusually wide range of inputs". Choose N from your normal traffic, not a guess.
  3. Use the starter script on a sample of text you might train on. It flags lines that look like secrets or phone numbers.
  4. Note what it caught, what it missed, and what you would add.
Starter codepython
import re, sys

PATTERNS = {
    "email": re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+"),
    "indian_mobile": re.compile(r"(?:\+91[\s-]?)?[6-9]\d{9}\b"),
    "possible_key": re.compile(r"(?i)(api[_-]?key|secret|token)\s*[:=]\s*\S{12,}"),
}

for n, line in enumerate(open(sys.argv[1], encoding="utf-8"), 1):
    for name, rx in PATTERNS.items():
        if rx.search(line):
            print(f"line {n}: {name}")
Open Google Colab ↗This code needs your own setup (a model, a GPU or extra packages). Run it in Google Colab: File → Upload notebook.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1Why do rate limits alone not stop model extraction?Reveal

A patient attacker can spread queries over many accounts, keys or weeks. Rate limits raise the cost, but you also need monitoring that links accounts, looks at query patterns over time, and legal terms you can enforce. Combine layers.

Q2Why might a model trained with differential privacy be less accurate on rare groups?Reveal

Differential privacy limits how much any single record, or small group of records, can shape the model. Rare groups have few records, so their patterns are the ones most weakened. This is a fairness trade-off to measure, not ignore: check accuracy per group when you use it.

Please sign in to continue.

Questions people ask

Can someone copy a large language model this way?

They cannot copy the exact weights by asking questions. But they can use many outputs to train a smaller model that imitates some of its behaviour, often called distillation. This is why many providers limit usage and forbid it in their terms.

Why does membership inference matter if the attacker learns only yes or no?

Sometimes the yes is the secret. If a model was trained only on patients with a certain illness, or customers who defaulted on a loan, learning that a person's record was in the training set reveals something sensitive about them.

Does fine-tuning on our own data create this risk?

Yes. A model fine-tuned on support tickets, emails or documents may repeat parts of them. Clean the data before fine-tuning, test by prompting for known sensitive strings, and restrict who can use the fine-tuned model.

Is returning confidence scores always bad?

No. Scores can be useful to customers. It is a trade-off. Return what the product really needs, round where you can, and watch for abuse.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress