Skip to content

Custom SpamAssassin rules for invoice-impersonation attempts

How to write SpamAssassin rules on a cPanel server that catch invoice-themed phishing with attachments named like "INV-2026-04.pdf" without flagging real supplier invoices.

Invoice-themed phishing is one of the highest-yield attacks against small businesses, and the default SpamAssassin ruleset misses much of it because the messages look procedurally identical to real invoices. Real fixes here are not lowering the threshold or buying another filter — they’re writing rules that match the specific shape of the attacks you’re seeing while leaving real invoices alone. The trick is precise pattern matching and conservative scoring.

What’s happening

Invoice phishing uses three patterns: a From: that spoofs a vendor your bookkeeping uses (display name only — actual address is unrelated), a subject like Invoice INV-2026-04 or Receipt Attached, and a PDF or DOCX attachment that contains either a link to credential theft or a macro. The default rules catch the most obvious cases but miss spoofed display names if the SPF for the spoofed domain happens to pass on a forwarder.

How to verify

Inspect a recent phishing example for distinctive headers:

grep -A2 "X-Spam-Status" /home/<user>/mail/.../<msg> | head

Extract the From header, the subject pattern, and the attachment names. The phishing campaigns reuse these patterns across days; a real invoice from a real vendor doesn’t.

The fix

  1. Create a custom rule file at /etc/mail/spamassassin/local_invoices.cf. cPanel will pick up files in that directory on the next SpamAssassin restart.

  2. Write a header rule for spoofed display names:

    header LOCAL_DISPLAY_SPOOF_VENDOR From =~ /\b(YourVendorName)\b/i
    tflags LOCAL_DISPLAY_SPOOF_VENDOR multiple
    describe LOCAL_DISPLAY_SPOOF_VENDOR From appears to claim to be a known vendor
    score LOCAL_DISPLAY_SPOOF_VENDOR 0.1

    Pair it with a meta rule that fires only when the address domain is not the vendor’s:

    meta LOCAL_VENDOR_IMPERSONATION (LOCAL_DISPLAY_SPOOF_VENDOR && !(From =~ /@yourvendor\.com>?$/i))
    score LOCAL_VENDOR_IMPERSONATION 4.0
  3. Match attachment naming patterns with a body or mimeheader rule:

    mimeheader LOCAL_INVOICE_ATTACH Content-Type =~ /name="?(?i)invoice|inv-|receipt/
    describe LOCAL_INVOICE_ATTACH Invoice-themed attachment name
    score LOCAL_INVOICE_ATTACH 0.5

    On its own this is too noisy — combine in a meta with other invoice-phishing indicators.

  4. Restart SpamAssassin with systemctl restart spamd and verify the rule loads:

    spamassassin --lint
  5. Review hit rates weekly for the first month. False positives should be near zero; false negatives drop the meta rules’ score. Iterate based on what real campaigns look like, not what you think they should look like.

For environments where finance email is regularly targeted and the rule set has to evolve weekly, Stack Harbor maintains the custom rule files as part of managed cPanel operations.