Cybersecurity Topics: Threats, Defences and Careers · Course book
Chapter 15: AI and Machine Learning in Security
A medium-sized organisation can generate millions of security events every day: logins, network connections, emails, file changes and alerts from dozens of tools. No team of people can read them all, and attackers know that their activity can hide in the noise. Artificial intelligence (AI) and especially machine learning (ML) have become central to how defenders cope with this volume, finding patterns and anomalies far faster than humans could. At the same time, attackers use the same technologies to make their attacks more convincing and more automated. This chapter explains what AI and machine learning are, how they are applied in cybersecurity, how to measure whether a security model is actually any good using a worked confusion matrix, why there is always a trade-off between catching attacks and raising false alarms, and the new risks that AI brings, including attacks on AI systems themselves.
AI and machine learning in brief
Artificial intelligence is the broad goal of making computers perform tasks that normally need human intelligence, such as recognising patterns, understanding language and making decisions. Machine learning is the most important approach to AI today: instead of following rules written by a programmer, an ML system learns patterns from examples. Given thousands of emails labelled "phishing" or "legitimate", for instance, a learning algorithm adjusts a model until it can label new emails it has never seen.
Two styles of learning matter most in security. In supervised learning, the model learns from labelled examples, as in the email case; it is well suited to recognising known kinds of threat, such as families of malware or styles of phishing. In unsupervised learning, the model receives unlabelled data and finds structure on its own, for example by learning what normal behaviour looks like for each user and device and flagging what does not fit. This anomaly detection can spot attacks that have never been seen before, but it also flags unusual behaviour that is perfectly innocent, such as an employee working late before a deadline. More recently, large language models, trained on vast amounts of text, have added the ability to summarise, explain and generate language and code.
How defenders use AI
Machine learning now runs quietly inside many everyday security tools.
- Email filtering classifies messages as spam, phishing or legitimate, using the wording, links, attachments, sender history and technical headers.
- Malware detection classifies files and programs by their characteristics and behaviour, catching new variants that differ from known samples.
- User and entity behaviour analytics builds a baseline for each account and device, then flags anomalies such as a login from an unusual country, access to files a user has never touched or a sudden large download.
- Network traffic analysis spots patterns such as regular "beaconing" to an unfamiliar server, typical of malware contacting its controller.
- Fraud detection in banking and online shopping scores each transaction for risk in real time.
- Alert triage groups related alerts into incidents and ranks them, so analysts see the most serious first; language models can draft summaries and suggest next investigative steps.
The common thread is scale and speed. ML does the first, broad sift through enormous volumes of data, leaving human analysts to investigate the smaller set of events that need judgement, context and decisions about how to respond.
Models are only as good as the data they learn from. A behaviour model trained during a quiet holiday month may flag normal busy-season activity as suspicious, and one trained on a network that was already compromised may learn to treat the intruder's activity as normal. Data also goes out of date as staff, systems and attackers change, a problem called drift, so security models must be retrained and re-evaluated regularly. Analysts also need to understand why a model raised an alert. An alert that says only "risk score 0.93" is hard to investigate, while one that explains "first login from this country, followed by a download fifty times larger than usual" lets an analyst act quickly. For this reason many security products now show the main factors behind each score, and teams keep a record of how alerts were resolved, which becomes fresh labelled data for improving the model.
Measuring a security model
Claims that a product is "99% accurate" can be badly misleading, because in security the events of interest are rare. A better picture comes from a confusion matrix, which counts the four possible outcomes of every decision.
Accuracy, the share of all decisions that are correct, is (450 + 9,405) ÷ 10,000 = 98.55%. Recall (also called detection rate), the share of phishing emails caught, is 450 ÷ 500 = 90%. Precision, the share of flagged emails that really are phishing, is 450 ÷ (450 + 95) = 450 ÷ 545 ≈ 82.6%. The false positive rate is 95 ÷ 9,500 = 1.0%.
Now compare a useless filter that never flags anything. It is right about all 9,500 legitimate emails, so its accuracy is 95%, yet its recall is 0%: it catches no phishing at all. Accuracy alone hides this, which is why security teams look at recall and precision.
| Actually phishing | Actually legitimate | |
|---|---|---|
| Flagged as phishing | 450 (true positives) | 95 (false positives) |
| Passed as legitimate | 50 (false negatives) | 9,405 (true negatives) |
Each kind of error has a cost. A false negative lets an attack through. A false positive blocks a genuine email, perhaps an important order from a customer, and wastes analysts' time; too many of them and people start ignoring alerts altogether. Which error matters more depends on the situation, and that leads to an unavoidable trade-off.
The threshold trade-off
Most classifiers do not simply say yes or no. They produce a score, such as a probability that an email is phishing, and the organisation chooses a threshold above which an email is flagged. A low threshold catches almost every attack (high recall) but flags many innocent messages (low precision). A high threshold flags only the most obvious attacks (high precision) but lets more through (low recall). No threshold gives perfect results on both measures, so the choice reflects what the organisation can tolerate.
A bank's fraud system might choose a low threshold for large international transfers, accepting more false alarms because a missed fraud is expensive, while using a higher threshold for small everyday purchases to avoid annoying customers. Good security design often uses several thresholds: block automatically when the score is very high, send to a human analyst when it is moderate, and allow when it is low.
New risks: attackers and AI
AI cuts both ways. Attackers use language models to write fluent, personalised phishing messages in any language, removing the spelling mistakes that once gave scams away. Deepfake audio and video can imitate the voice or face of a manager on a call asking for an urgent payment. Automation lets attackers scan for and exploit vulnerabilities faster. These developments make verification procedures, such as confirming payment requests through a separate channel, more important than ever.
AI systems can also be attacked directly. In evasion attacks, attackers make small changes to malware or messages so that a classifier misjudges them, while keeping the harmful behaviour. In data poisoning, they tamper with the training data so that the model learns the wrong lessons. Applications built on language models face prompt injection, in which instructions hidden in a document, email or web page cause the model to ignore its intended rules or reveal information. And models can leak sensitive information that was included in their training data or that users typed into them.
Defending AI systems therefore requires the same security thinking as any other system: protect the training data, test models against deliberately tricky inputs, limit what actions an AI tool is allowed to take on its own, keep humans in charge of important decisions, and monitor for unusual behaviour. Organisations adopting AI security tools should also ask suppliers how models are tested, how often they are updated and what data they collect. AI is a powerful assistant for defenders, but it does not remove the need for skilled people, sound processes and layered controls.
Check your understanding
- Explain the difference between supervised learning and anomaly detection, with a security example of each.
- List four security tasks in which machine learning is commonly used.
- A malware detector examines 2,000 files, 100 of which are malicious. It flags 80 malicious files and 38 harmless ones. Calculate its recall, precision and false positive rate.
- Why can a model with very high accuracy still be useless for security? Use an example.
- Explain the threshold trade-off and describe how an organisation might use more than one threshold.
- Describe two ways attackers use AI and two ways they attack AI systems.
This course book is available to read online only at chrysolites.org.