File size: 5,576 Bytes
d0af6b9
 
 
 
 
 
 
 
 
 
 
 
a1fb9a0
d0af6b9
 
a1fb9a0
d0af6b9
 
a1fb9a0
d0af6b9
 
 
 
 
a1fb9a0
d0af6b9
 
 
 
 
 
 
 
 
a1fb9a0
 
 
 
 
 
 
 
 
d0af6b9
a1fb9a0
 
d0af6b9
a1fb9a0
 
d0af6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1fb9a0
d0af6b9
 
 
a1fb9a0
d0af6b9
 
 
 
a1fb9a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
license: apache-2.0
base_model: distilbert/distilroberta-base
pipeline_tag: text-classification
library_name: transformers
language: [en]
tags: [phishing, email-security, phishing-detection, security, classification, onnx, transformers.js, spam, social-engineering]
model-index:
- name: phishing-email-classifier
  results:
  - task: {type: text-classification, name: Phishing email detection}
    dataset: {type: ceas-08, name: CEAS_08 test split (3392 emails)}
    metrics: [{type: auroc, value: 0.9999, name: AUROC}]
  - task: {type: text-classification, name: Phishing email detection}
    dataset: {type: trec-07, name: TREC_07 test split (5235 emails)}
    metrics: [{type: auroc, value: 0.9996, name: AUROC}]
  - task: {type: text-classification, name: Phishing email detection}
    dataset: {type: llmgen-phishing, name: LLMGen-Phishing-Email-Dataset test split (676 emails)}
    metrics: [{type: auroc, value: 0.9968, name: AUROC}]
inference: false
---

# phishing-email-classifier

Flags phishing emails from their text (subject + body). An 82M-parameter DistilRoBERTa classifier that scores above the two most-downloaded open phishing-email models on nine of twelve public phishing and spam corpora (CEAS-08, TREC-05/06/07, Nazario, Nigerian fraud, Enron, locuoco, LLM-written phishing) and within 0.004 AUROC of the best on the other three, with far fewer false alarms at the default threshold. Runs on CPU or in your browser with no server.

**[Try it in your browser](https://huggingface.co/spaces/Project-Bastion/phishing-email-classifier-demo)**: a static page that runs the model locally (transformers.js, ONNX int8, about 86 MB, downloaded once). The email text you paste never leaves your device.

## Results

Test splits of public phishing-email benchmarks (AUROC; recall at 1% false-positive rate in brackets). All models scored with identical code on identical emails. For cybersectony's 4-class model the phishing-email logit is compared with the legitimate-email logit.

| Benchmark (emails) | **phishing-email-classifier** | cybersectony/phishing-email-detection-distilbert_v2.4.1 (312k downloads) | ptouch/phishing-distilbert-cyber207 (248k downloads) |
|---|---|---|---|
| CEAS_08 (3,392) | **0.9999** (0.998) | 0.9847 (0.610) | 0.9669 (0.499) |
| TREC_05 (5,450) | **0.9995** (0.990) | 0.9856 (0.837) | 0.9553 (0.666) |
| TREC_06 (1,607) | **0.9990** (0.980) | 0.9860 (0.812) | 0.9445 (0.576) |
| TREC_07 (5,235) | **0.9996** (0.998) | 0.9630 (0.608) | 0.8906 (0.470) |
| Nazario (299) | **0.9995** (0.987) | 0.9317 (0.081) | 0.9079 (0.169) |
| Nigerian fraud (649) | **1.0000** (1.000) | 0.9897 (0.879) | 0.9940 (0.968) |
| SpamAssassin (565) | 0.9987 (0.963) | 0.9976 (0.951) | **0.9996** (0.982) |
| Enron (3,004) | **0.9997** (0.996) | 0.9995 (0.994) | 0.9914 (0.866) |
| Ling (284) | 0.9967 (0.957) | 0.9980 (0.978) | **1.0000** (1.000) |
| locuoco spam-ham-phish, phish vs ham (16,529) | **1.0000** (1.000) | 0.9686 (0.568) | 0.9457 (0.057) |
| LLMGen-Phishing-Email-Dataset (676) | **0.9968** (0.996) | 0.6948 (0.089) | 0.5147 (0.052) |
| zefang-liu/phishing-email-dataset (17,236) | 0.9991 (0.979) | **0.9998** (0.998) | 0.9884 (0.866) |

At the default threshold (margin 0) the false-positive rate on legitimate mail is 0.2% (CEAS_08), 0.3% (TREC_07) and 0.1% (locuoco), against 10.2%, 10.2% and 18.5% for cybersectony.
Corpus labels are used as published: CEAS_08, TREC_05/06/07 and Enron mark spam as phishing.

## Usage

```python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
repo = "Project-Bastion/phishing-email-classifier"
tok = AutoTokenizer.from_pretrained(repo); model = AutoModelForSequenceClassification.from_pretrained(repo).eval()

def phishing_margin(subject, body):
    enc = tok(f"{subject}\n{body}", truncation=True, max_length=256, return_tensors="pt")
    with torch.no_grad(): logits = model(**enc).logits[0]
    return float(logits[1] - logits[0])            # label 0 = legitimate, 1 = phishing

m = phishing_margin("Urgent: verify your account within 24 hours",
                    "We detected unusual activity on your bank account. Confirm your password and card details now at http://secure-bank-verify.example.com/login")
print(m, "PHISHING" if m > 0 else "ok")            # raise the threshold for fewer false alarms
```

The score is a logit margin (phishing minus legitimate); 0 corresponds to p = 0.5.

## Files

`onnx/model_quantized.onnx` (weight-only int8, 86 MB, the demo default) and `onnx/model.onnx` (fp32, 329 MB) for transformers.js / onnxruntime. fp32 gives the same decisions as PyTorch on 315 checked emails; int8 gives the same decisions with a largest margin difference of 0.63.

## Limits

* Slightly behind cybersectony on zefang-liu and Ling (and behind ptouch on SpamAssassin and Ling): test on your own mail and tune the threshold before relying on it.
* English email text only, first 256 tokens; it does not look at URLs, headers or attachments.

## License and credits

Apache-2.0. Base model: distilroberta-base (Apache-2.0). Training data: locuoco/the-biggest-spam-ham-phish-email-dataset-300000 (MIT); Dizzzy0x00/LLMGen-Phishing-Email-Dataset (Apache-2.0); the *Phishing Email Curated Datasets* of Champa, Rabbi and Zibran (2023), Zenodo, doi:10.5281/zenodo.8339691 (CC-BY-4.0; CEAS_08, TREC_05/06/07, Nazario, Nigerian fraud, SpamAssassin, Enron, Ling), part via kudzaiprichard/aura-phishing-email-corpus. zefang-liu/phishing-email-dataset (LGPL-3.0) is used for evaluation only. Revision `v1` of this repo holds the earlier weights.