Score a suspicious email

Mail filters and careful readers weigh several warning signs together. Here you turn those signs into a score with reasons.

What to build

Score emails by their warning signs and give a verdict.

Requirements

  • Line 1: the organisation's own site. Each following email is a block of lines key: value ended by a line ---. Keys: from (an address), subject, link (zero or more), text.
  • Add points: +3 if the sender's site (after @, last two labels, lower case) is not the organisation's site but contains the organisation's first label; +2 for each link whose site (last two labels of the host) is not the organisation's site; +2 if subject or text contains (any case) urgent, immediately, suspended or within 24 hours; +3 if the text asks for a password, pin or otp (any case, as a whole word).
  • Print the score, then the verdict: phishing for 5 or more, suspicious for 2 to 4, looks fine for 0 or 1, then the reasons in the order above, as score N verdict: reason; reason. Reasons are look-alike sender, link to SITE (once per link), pressure words, asks for secrets.

Input

As described.

Output

One line per email.

Constraints

Up to 20 emails of up to 15 lines each.

Example

Input:

bank.example
from: alerts@bank-example-support.com
subject: URGENT: account suspended
link: https://bank-example-verify.net/login
text: Confirm your PIN within 24 hours.
---

Output:

score 10 phishing: look-alike sender; link to bank-example-verify.net; pressure words; asks for secrets

Why it works

No single sign proves phishing, but they add up: a sender that borrows the real name, links to another site, pressure and a request for secrets together are the classic shape, so a weighted score with reasons is both useful and explainable.

Examples

Example 1 · Input
bank.example
from: alerts@bank-example-support.com
subject: URGENT: account suspended
link: https://bank-example-verify.net/login
text: Confirm your PIN within 24 hours.
---
Expected output
score 10 phishing: look-alike sender; link to bank-example-verify.net; pressure words; asks for secrets
Example 2 · Input
bank.example
from: news@bank.example
subject: Your monthly statement
link: https://bank.example/statements
text: Your statement is ready.
---
Expected output
score 0 looks fine

Hints

Hint 1

Collect each email's fields until the --- line, then score it.

Hint 2

A whole-word test: /\b(password|pin|otp)\b/i.

Solution

Show one correct solution (try it yourself first)
const org = prompt().trim().toLowerCase();
const orgLabel = org.split(".")[0];
const site = (host) => host.toLowerCase().split(".").slice(-2).join(".");
const hostOf = (url) => (url.includes("://") ? url.split("://")[1] : url).split("/")[0].split("?")[0].split("@").pop().split(":")[0];
function score(e) {
  let points = 0;
  const reasons = [];
  const senderSite = site((e.from ?? "").split("@").pop() ?? "");
  if (senderSite !== org && senderSite.includes(orgLabel)) {
    points += 3;
    reasons.push("look-alike sender");
  }
  for (const link of e.links) {
    const s = site(hostOf(link));
    if (s !== org) {
      points += 2;
      reasons.push(`link to ${s}`);
    }
  }
  const words = `${e.subject ?? ""} ${e.text ?? ""}`.toLowerCase();
  if (["urgent", "immediately", "suspended", "within 24 hours"].some((w) => words.includes(w))) {
    points += 2;
    reasons.push("pressure words");
  }
  if (/\b(password|pin|otp)\b/i.test(e.text ?? "")) {
    points += 3;
    reasons.push("asks for secrets");
  }
  const verdict = points >= 5 ? "phishing" : points >= 2 ? "suspicious" : "looks fine";
  console.log(`score ${points} ${verdict}${reasons.length ? ": " + reasons.join("; ") : ""}`);
}
let email = { links: [] };
let line = prompt();
while (line !== null) {
  const t = line.trim();
  if (t === "---") {
    score(email);
    email = { links: [] };
  } else if (t) {
    const at = t.indexOf(":");
    const key = t.slice(0, at).trim().toLowerCase();
    const value = t.slice(at + 1).trim();
    if (key === "link") email.links.push(value);
    else email[key] = value;
  }
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Phishing and social engineering →