alerts.sql 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. $osquery_data = [
  2. <|dt:1688910000, host:"fqdn1", ev_type:"someEv", ev_status:"", user:"", vpn:false, |>,
  3. <|dt:1688910050, host:"fqdn2", ev_type:"login", ev_status:"success", user:"", vpn:true, |>,
  4. <|dt:1688910100, host:"fqdn1", ev_type:"login", ev_status:"success", user:"", vpn:true, |>,
  5. <|dt:1688910220, host:"fqdn1", ev_type:"login", ev_status:"success", user:"", vpn:false, |>,
  6. <|dt:1688910300, host:"fqdn1", ev_type:"delete_all", ev_status:"", user:"", vpn:false, |>,
  7. <|dt:1688910400, host:"fqdn2", ev_type:"delete_all", ev_status:"", user:"", vpn:false, |>,
  8. <|dt:1688910500, host:"fqdn1", ev_type:"login", ev_status:"failed", user:"user1", vpn:false, |>,
  9. <|dt:1688910500, host:"fqdn1", ev_type:"login", ev_status:"failed", user:"user2", vpn:false, |>,
  10. <|dt:1688910600, host:"fqdn", ev_type:"someEv", ev_status:"", user:"user1", vpn:false, |>,
  11. <|dt:1688910800, host:"fqdn2", ev_type:"login", ev_status:"failed", user:"user1", vpn:false, |>,
  12. <|dt:1688910900, host:"fqdn2", ev_type:"login", ev_status:"failed", user:"user2", vpn:false, |>,
  13. <|dt:1688911000, host:"fqdn2", ev_type:"login", ev_status:"success", user:"user1", vpn:false, |>,
  14. <|dt:1688911001, host:"fqdn2", ev_type:"login", ev_status:"success", user:"user1", vpn:false, |>,
  15. ];
  16. pragma FeatureR010="prototype";
  17. pragma config.flags("MatchRecognizeStream", "disable");
  18. SELECT *
  19. FROM AS_TABLE($osquery_data) MATCH_RECOGNIZE(
  20. ORDER BY CAST(dt as Timestamp)
  21. MEASURES
  22. LAST(LOGIN_SUCCESS_REMOTE.host) as remote_login_host,
  23. LAST(LOGIN_SUCCESS_REMOTE.user) as remote_login_user,
  24. LAST(LOGIN_SUCCESS_REMOTE.dt) as remote_login_dt,
  25. LAST(SUSPICIOUS_ACTION_SOON.dt) as suspicious_action_dt,
  26. FIRST(LOGIN_FAILED_SAME_USER.dt) as brutforce_begin,
  27. FIRST(LOGIN_SUCCESS_SAME_USER.dt) as brutforce_end,
  28. LAST(LOGIN_SUCCESS_SAME_USER.user) as brutforce_login
  29. ONE ROW PER MATCH
  30. AFTER MATCH SKIP TO NEXT ROW
  31. PATTERN (
  32. LOGIN_SUCCESS_REMOTE ANY_ROW1* SUSPICIOUS_ACTION_SOON |
  33. (LOGIN_FAILED_SAME_USER ANY_ROW2*){2,} LOGIN_SUCCESS_SAME_USER
  34. )
  35. DEFINE
  36. LOGIN_SUCCESS_REMOTE as
  37. LOGIN_SUCCESS_REMOTE.ev_type = "login" and
  38. LOGIN_SUCCESS_REMOTE.ev_status = "success" and
  39. LOGIN_SUCCESS_REMOTE.vpn = true and
  40. COALESCE(LOGIN_SUCCESS_REMOTE.dt - FIRST(LOGIN_FAILED_SAME_USER.dt) <= 500, TRUE),
  41. ANY_ROW1 as
  42. COALESCE(ANY_ROW1.dt - FIRST(LOGIN_SUCCESS_REMOTE.dt) <= 500, TRUE),
  43. SUSPICIOUS_ACTION_SOON as
  44. SUSPICIOUS_ACTION_SOON.host = LAST(LOGIN_SUCCESS_REMOTE.host) and
  45. SUSPICIOUS_ACTION_SOON.ev_type = "delete_all" and
  46. COALESCE(SUSPICIOUS_ACTION_SOON.dt - FIRST(LOGIN_SUCCESS_REMOTE.dt) <= 500, TRUE),
  47. LOGIN_FAILED_SAME_USER as
  48. LOGIN_FAILED_SAME_USER.ev_type = "login" and
  49. LOGIN_FAILED_SAME_USER.ev_status <> "success" and
  50. (LAST(LOGIN_FAILED_SAME_USER.user) IS NULL
  51. or LAST(LOGIN_FAILED_SAME_USER.user) = LOGIN_FAILED_SAME_USER.user
  52. ) and COALESCE(LOGIN_FAILED_SAME_USER.dt - FIRST(LOGIN_FAILED_SAME_USER.dt) <= 500, TRUE),
  53. ANY_ROW2 as
  54. COALESCE(ANY_ROW2.dt - FIRST(LOGIN_FAILED_SAME_USER.dt) <= 500, TRUE),
  55. LOGIN_SUCCESS_SAME_USER as
  56. LOGIN_SUCCESS_SAME_USER.ev_type = "login" and
  57. LOGIN_SUCCESS_SAME_USER.ev_status = "success" and
  58. LOGIN_SUCCESS_SAME_USER.user = LAST(LOGIN_FAILED_SAME_USER.user) and
  59. COALESCE(LOGIN_SUCCESS_SAME_USER.dt - FIRST(LOGIN_FAILED_SAME_USER.dt) <= 500, TRUE)
  60. ) AS MATCHED
  61. ;