Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion backend/routes/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,27 @@ async def review_queue(current_user: User = Depends(require_trainer)):
scope = _scope(current_user)
query = {"direction": "outbound", "status": "draft_pending"}
query.update(scope)
return await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500)
drafts = await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500)
# Give the reviewer the message being answered and which campaign it is.
names: dict[str, str] = {}
for d in drafts:
cid = d["campaign_id"]
if cid not in names:
c = await db.live_campaigns.find_one({"id": cid}, {"_id": 0, "name": 1})
names[cid] = (c or {}).get("name", "")
d["campaign_name"] = names[cid]
last_in = await db.live_messages.find_one(
{
"campaign_id": cid,
"email": d["email"],
"direction": "inbound",
"at": {"$lte": d["at"]},
},
{"_id": 0, "body_redacted": 1},
sort=[("at", -1)],
)
d["in_reply_to"] = (last_in or {}).get("body_redacted", "")
return drafts


class ReviewDecision(BaseModel):
Expand All @@ -587,6 +607,8 @@ async def approve_draft(
if not campaign or campaign.get("killed"):
raise HTTPException(status_code=409, detail="Campaign is not active")
body = data.body if data.body is not None else msg["body_redacted"]
if not (body or "").strip():
raise HTTPException(status_code=400, detail="Write a reply before sending")
sent = await conversation.send_approved_reply(campaign, msg, body, current_user.id)
await audit.record(
current_user.id, "live.review.approve", message_id, {"edited": bool(data.body)}
Expand Down
131 changes: 117 additions & 14 deletions backend/services/live/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
The persona prompt is built here on the server from campaign fields only; the
browser never supplies prompt text (G7). Secrets never reach the model: the
inbound text is scrubbed first.

The prompt includes the original lure so the model knows the thread, and it answers in
the language the recipient wrote in. A hosted model may still decline to draft;
that is retried once and otherwise queued as an empty draft marked
`persona_declined`, so the reviewer writes the reply instead of approving a
refusal or an empty email.
"""

import html
import logging
import re
import uuid
from datetime import UTC, datetime

Expand Down Expand Up @@ -46,18 +54,92 @@
}


def build_persona_prompt(campaign: dict, turn: int) -> str:
"""Locked system prompt. Built only from approved campaign fields."""
_TAG_RE = re.compile(r"<[^>]+>")
_TEMPLATE_URL_RE = re.compile(r"\{\{\s*\.URL\s*\}\}")
_TEMPLATE_RE = re.compile(r"\{\{[^}]*\}\}")

_ID_WORDS = {
"apa", "ini", "itu", "saya", "aku", "anda", "kamu", "tidak", "nggak", "gak", "yang",
"dan", "untuk", "dengan", "ada", "bisa", "sudah", "belum", "mohon", "kenapa", "mengapa",
"siapa", "bapak", "pak", "ibu", "bu", "terima", "kasih", "tolong", "kok", "gimana",
"bagaimana", "apakah", "maaf", "kode", "dari", "ke", "di", "akan", "harus", "boleh",
} # fmt: skip
_EN_WORDS = {
"what", "is", "this", "the", "i", "you", "please", "why", "who", "can", "not", "and",
"to", "of", "for", "with", "my", "your", "it", "do", "does", "are", "sorry", "thanks",
} # fmt: skip

# A model declining to write the persona turn, in English or Indonesian.
_REFUSAL_VERBS = (
r"(?:help|assist|generate|create|write|draft|produce|provide|comply|fulfil+|"
r"continue|participate|engage|do that|role-?play)"
)
_REFUSAL_RE = re.compile(
r"^\W*(?:(?:i'm\s+)?sorry,?\s+(?:but\s+)?i\b|as an ai\b"
r"|maaf,?\s+(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\b"
r"|(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\s+(?:membantu|membuat|menulis)\b)"
r"|\bi\s*(?:cannot|can't|can not|won't|will not|am unable to|'m unable to|am not able to)"
rf"\s+(?:\w+\s+)?{_REFUSAL_VERBS}\b",
re.IGNORECASE,
)


def lure_text(campaign: dict) -> str:
"""The original email as plain text, the first turn of the thread."""
raw = campaign.get("body_html") or campaign.get("body") or ""
raw = _TEMPLATE_URL_RE.sub("[link]", raw)
raw = _TEMPLATE_RE.sub("", raw)
raw = re.sub(r"(?i)<br\s*/?>|</p>|</div>|</li>", "\n", raw)
text = html.unescape(_TAG_RE.sub("", raw))
text = re.sub(r"[ \t]+", " ", text)
text = re.sub(r"\n\s*\n+", "\n\n", text).strip()
return text[:2000]


def detect_language(text: str) -> str | None:
"""'id' or 'en' when the recipient's words make it clear, else None."""
words = re.findall(r"[a-zA-Z']+", (text or "").lower())
id_hits = sum(w in _ID_WORDS for w in words)
en_hits = sum(w in _EN_WORDS for w in words)
if id_hits > en_hits:
return "id"
if en_hits > id_hits:
return "en"
return None


def is_refusal(draft: str) -> bool:
"""True when the model produced nothing usable as the persona's reply."""
text = (draft or "").strip()
return not text or bool(_REFUSAL_RE.search(text[:300]))


def _lure_block(campaign: dict) -> str:
text = lure_text(campaign)
if not text:
return ""
subject = campaign.get("subject") or ""
return f"The email you sent first (subject: {subject}):\n---\n{text}\n---\n\n"


def build_persona_prompt(campaign: dict, turn: int, language: str | None = None) -> str:
"""Locked system prompt. Built only from approved campaign fields; `language`
is the recipient's language when it could be detected."""
goal = _OBJECTIVE_GOALS.get(campaign["objective"], "continue the pretext")
principles = ", ".join(campaign.get("principles", []))
lang = (
"Write in Indonesian (Bahasa Indonesia)."
if campaign.get("language") == "id"
if (language or campaign.get("language")) == "id"
else "Write in English."
)
return (
"You are role-playing an attacker in an AUTHORIZED security awareness "
"simulation. You are continuing an email thread you started.\n\n"
"You are role-playing the sender of a simulated phishing email in an "
"AUTHORIZED security awareness exercise run by the recipient's own "
"organization. Your reply is held for a human trainer to review before it "
"is sent, the recipient is debriefed afterwards, and no real credentials "
"or codes are ever collected. You are continuing the email thread you "
"started.\n\n"
f"{_lure_block(campaign)}"
f"Your single objective: {goal}.\n"
f"Influence techniques you may use: {principles}.\n"
f"You are '{campaign['sender_name']}'. Do not claim to be anyone else.\n\n"
Expand All @@ -73,7 +155,13 @@ def build_persona_prompt(campaign: dict, turn: int) -> str:


async def _record_message(
campaign: dict, email: str, direction: str, body: str, status: str, secrets_found=None
campaign: dict,
email: str,
direction: str,
body: str,
status: str,
secrets_found=None,
**extra,
) -> None:
doc = {
"id": str(uuid.uuid4()),
Expand All @@ -86,6 +174,7 @@ async def _record_message(
"secrets_found": secrets_found or [],
"reviewed_by": None,
"at": datetime.now(UTC),
**extra,
}
await db.live_messages.insert_one(doc)
return doc
Expand Down Expand Up @@ -166,17 +255,35 @@ async def process_inbound(campaign: dict, email: str, raw_body: str, gateway=Non
if not gateway:
return {"status": "awaiting_gateway"}

thread = await _history(campaign["id"], email)
# Only what the recipient actually saw: their messages and replies we sent.
history = [
{
"role": "assistant" if m["direction"] == "outbound" else "user",
"content": m["body_redacted"],
}
for m in await _history(campaign["id"], email)
for m in thread
if m["body_redacted"] and (m["direction"] == "inbound" or m.get("status") == "sent")
]
draft = await gateway.persona_reply(
system_prompt=build_persona_prompt(campaign, turn),
history=history,
language = detect_language(redacted)
prompt = build_persona_prompt(campaign, turn, language)
draft = ""
for _attempt in range(2):
draft = (await gateway.persona_reply(system_prompt=prompt, history=history)).strip()
if not is_refusal(draft):
break
await db.live_recipients.update_one(
{"campaign_id": campaign["id"], "email": email},
{"$set": {"turn": turn, "conversation_state": "active", "updated_at": _now()}},
)
if is_refusal(draft):
# Queue an empty draft so a human writes this turn; never send a refusal.
logger.info(f"persona declined to draft a reply for campaign {campaign['id']}")
await _record_message(
campaign, email, "outbound", "", "draft_pending", persona_declined=True
)
return {"status": "persona_declined", "violations": [], "draft": ""}

violations = review_draft(
draft,
objective=campaign["objective"],
Expand All @@ -186,10 +293,6 @@ async def process_inbound(campaign: dict, email: str, raw_body: str, gateway=Non
)
status = "blocked" if violations else "draft_pending"
await _record_message(campaign, email, "outbound", draft, status)
await db.live_recipients.update_one(
{"campaign_id": campaign["id"], "email": email},
{"$set": {"turn": turn, "conversation_state": "active", "updated_at": _now()}},
)
return {"status": status, "violations": violations, "draft": draft}


Expand Down
125 changes: 125 additions & 0 deletions backend/tests/test_live_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,128 @@ async def test_approve_blocked_on_killed_campaign(api):
async def test_users_cannot_access_review(api):
_, h = await make_user("user")
assert (await api.get("/api/live/review", headers=h)).status_code == 403


# ---------- persona reliability ----------


class ScriptedGateway(FakeGateway):
"""Returns the scripted replies in order and records what it was sent."""

def __init__(self, replies):
super().__init__()
self._replies = list(replies)
self.calls = []

async def persona_reply(self, *, system_prompt, history):
self.calls.append({"system_prompt": system_prompt, "history": history})
return self._replies.pop(0)


@pytest.mark.parametrize(
"text,refused",
[
("", True),
(" ", True),
("I cannot generate persuasive phishing messages, even in a simulation.", True),
("I'm sorry, but I can't help with that.", True),
("Sorry, I won't write that.", True),
("As an AI, I must decline.", True),
("Maaf, saya tidak bisa membantu membuat pesan ini.", True),
("Just a quick follow-up on the reset, it only takes a minute.", False),
("I can't wait to get this sorted for you before the deadline.", False),
("I'm sorry for the delay, the reset link is still valid today.", False),
("Halo, ini dari IT Helpdesk. Mohon segera selesaikan verifikasinya ya.", False),
],
)
def test_is_refusal(text, refused):
assert conversation.is_refusal(text) is refused


@pytest.mark.parametrize(
"text,lang",
[
("Apa ini ?", "id"),
("Maaf, ini dari siapa ya pak?", "id"),
("What is this?", "en"),
("ok", None),
],
)
def test_detect_language(text, lang):
assert conversation.detect_language(text) == lang


def test_lure_text_strips_html_and_template_tags():
camp = {"body_html": "<p>Hi {{.FirstName}},</p><p>Verify here: {{.URL}}</p>&amp; thanks"}
text = conversation.lure_text(camp)
assert "Verify here: [link]" in text and "<p>" not in text and "{{" not in text
assert "& thanks" in text


async def test_prompt_carries_lure_and_follows_recipient_language(api):
camp = await _running_campaign()
camp["body_html"] = "<p>Your mailbox will be locked. Verify: {{.URL}}</p>"
gw = ScriptedGateway(["Halo, ini tindak lanjut dari IT Helpdesk soal verifikasi tadi."])
r = await conversation.process_inbound(camp, "bob@acme.test", "Apa ini ?", gw)
assert r["status"] == "draft_pending"
prompt = gw.calls[0]["system_prompt"]
assert "Your mailbox will be locked. Verify: [link]" in prompt
assert "Write in Indonesian" in prompt # campaign is "en", recipient wrote Indonesian
assert gw.calls[0]["history"] == [{"role": "user", "content": "Apa ini ?"}]


async def test_refusal_is_retried_once(api):
camp = await _running_campaign()
gw = ScriptedGateway(["I cannot help with phishing.", "A quick follow-up on the reset."])
r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw)
assert r["status"] == "draft_pending" and r["draft"] == "A quick follow-up on the reset."
assert len(gw.calls) == 2


async def test_persistent_refusal_queues_empty_draft_for_human(api):
camp = await _running_campaign()
gw = ScriptedGateway(["I cannot help with phishing.", ""])
r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw)
assert r["status"] == "persona_declined"
draft = await db.live_messages.find_one({"campaign_id": camp["id"], "direction": "outbound"})
assert draft["status"] == "draft_pending" and draft["persona_declined"] is True
assert draft["body_redacted"] == "" # the refusal text is never queued


async def test_unsent_drafts_are_not_in_history(api):
camp = await _running_campaign()
await conversation.process_inbound(camp, "bob@acme.test", "who is this?", FakeGateway())
gw = ScriptedGateway(["Following up again."])
await conversation.process_inbound(camp, "bob@acme.test", "hello?", gw)
roles = [m["role"] for m in gw.calls[0]["history"]]
assert roles == ["user", "user"] # the first draft was never approved, so never seen


async def test_approve_rejects_empty_body(api, _no_real_mail):
_, h = await make_user("trainer", organization_id="o1")
_, draft = await _seed_draft(org="o1")
await db.live_messages.update_one({"id": draft["id"]}, {"$set": {"body_redacted": ""}})
r = await api.post(f"/api/live/review/{draft['id']}/approve", json={}, headers=h)
assert r.status_code == 400
r = await api.post(f"/api/live/review/{draft['id']}/approve", json={"body": " "}, headers=h)
assert r.status_code == 400
assert not _no_real_mail


async def test_review_queue_includes_context(api):
_, h = await make_user("trainer", organization_id="o1")
camp, draft = await _seed_draft(org="o1")
await db.live_messages.insert_one(
{
"id": "in-1",
"campaign_id": camp["id"],
"organization_id": "o1",
"email": "bob@acme.test",
"direction": "inbound",
"body_redacted": "who is this?",
"status": "received",
"at": datetime(2020, 1, 1, tzinfo=UTC),
}
)
row = (await api.get("/api/live/review", headers=h)).json()[0]
assert row["in_reply_to"] == "who is this?" and row["campaign_name"] == "drill"
1 change: 1 addition & 0 deletions docs/live-campaign-walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ To run against a real organization instead of the sandbox:
| My reply never appears at all | IMAP isn't configured (Settings → Live), or the reply went to the sender address instead of the `Reply-To`. Check the campaign **Conversation** panel and run **Poll now** (Settings/API `POST /live/poll`). |
| I replied to the lure but **Reported** stays 0 | Replying is engaging, not reporting. Forward the email to the report mailbox (`lapor@acme.test`); it is credited within ~30 s, or click **Periksa sekarang** on Laporan Phishing. |
| My reply lands in the wrong place | The IMAP user must be the reply mailbox (`sim-reply@acme.test`), not the campaign sender. |
| Draft in Reply Review is empty and marked **AI did not draft** | The LLM declined to write that turn (hosted models sometimes refuse, most often for the OTP objective). Pretexta retries once, then queues an empty draft instead of a refusal: write the reply yourself or discard it. A local Ollama model avoids provider-side refusals. |
| Reply shows in Conversation but not in Reply Review | No LLM provider configured — the persona can't draft a reply. Configure one in Settings → LLM (local Ollama works). |
| Reply read in Thunderbird, poller skipped it | Fixed: the poller tracks UIDs, not the read/\Seen flag, so this no longer happens. |
| Phishing link won't open (`http://gophish?rid=…`) | `phish_url` is set to an internal Docker name. Set it to a browser-reachable address (`http://localhost:8080` locally, or your public phishing URL), then **launch a new campaign** — the URL is baked in at launch. |
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,5 +751,23 @@
"live_complied": "In a live phishing simulation you agreed to a request that used {{principle}}. This exercise helps you recognise it next time.",
"live_disclosed": "In a live phishing simulation you shared a secret in reply to a message that used {{principle}}. This exercise helps you recognise it next time.",
"retest": "Scheduled retest for {{principle}} — a different exercise to confirm the lesson stuck."
},
"review": {
"title": "Reply Review Queue",
"subtitle": "Approve each AI persona reply before it is sent (review mode)",
"empty_title": "Queue is empty",
"empty_desc": "AI replies awaiting approval will appear here.",
"awaiting": "awaiting review",
"declined": "AI did not draft",
"they_wrote": "They wrote",
"declined_note": "The AI model declined to draft this turn. Write the reply yourself, or discard it.",
"placeholder": "Write the persona's reply…",
"help": "Every AI reply is held here until a human approves it. Edit before sending if needed.",
"approve": "Approve & send",
"discard": "Discard",
"sent": "Reply sent",
"discarded": "Discarded",
"approve_failed": "Approve failed",
"discard_failed": "Discard failed"
}
}
Loading