earliest=-24h ((index=web (sourcetype="access_combined" OR sourcetype="iis" OR sourcetype="ms:iis:auto" OR sourcetype="nginx:plus:access")) OR (index=azure (sourcetype="azure:aad:signin" OR (sourcetype="azure:monitor:aad" category IN ("SignInLogs", "AuditLogs", "ServicePrincipalSignInLogs")) OR sourcetype="azure:aad:audit")) OR (index=okta sourcetype="OktaIM2:log" eventType="user.session.start") OR (index=aws sourcetype="aws:cloudtrail" (eventName="List*" OR eventName="Describe*" OR eventName="Get*" OR eventName="BatchGetSecretValue" OR errorCode="Client.DryRunOperation")) OR (index=linux sourcetype="sysmon:linux" (EventCode IN (1, 11) OR EventID IN (1, 11))) OR (index=sysmon EventCode IN (1, 11, 22) (source="XmlWinEventLog:Microsoft-Windows-Sysmon/Operational" OR source="WinEventLog:Microsoft-Windows-Sysmon/Operational")) OR (index=database (sourcetype="mysql:generalQueryLog" OR sourcetype="mysql:queryLog") general_log_file)) | eval ev=tostring(coalesce(EventCode, EventID, "-")), stype=lower(sourcetype), cat=if(stype="azure:aad:audit", "AuditLogs", coalesce(category, "-")), kind=case(in(stype, "access_combined", "iis", "ms:iis:auto", "nginx:plus:access"), "web", in(stype, "mysql:generalquerylog", "mysql:querylog"), "db", stype="aws:cloudtrail", "aws", cat="ServicePrincipalSignInLogs", "sp", cat="AuditLogs", "audit", in(stype, "azure:aad:signin", "azure:monitor:aad", "oktaim2:log"), "signin", ev="1", "proc", ev="11", "file", ev="22", "dns") | eval st=tostring(coalesce(status, sc_status, "")), ec=coalesce(errorCode, ""), path=coalesce(uri_path, cs_uri_stem, uri, ""), err4=if(match(st, "^[45]\\d\\d$"), 1, 0), is_obj=if(kind="web" AND match(st, "^2\\d\\d$") AND match(path, "/[A-Za-z_-]+/[0-9]{3,12}(/|$)") AND NOT match(lower(path), "\\.(jpe?g|png|gif|webp|svg|ico|css|js|map|woff2?|ttf)$") AND NOT match(path, "/(19|20)[0-9][0-9]/[01][0-9]/"), 1, 0), obj=if(is_obj=1, path, null()), err=tostring(coalesce('properties.status.errorCode', 'status.errorCode', resultType, 'outcome.result', "-")), fail=if(kind="signin" AND in(err, "50126", "50053", "50034", "FAILURE"), 1, 0), ok=if(in(kind, "signin", "sp") AND in(err, "0", "SUCCESS", "Success"), 1, 0), ok_t=if(ok=1, _time, null()), fuser=if(fail=1, coalesce('properties.userPrincipalName', userPrincipalName, 'actor.alternateId'), null()), sfail=if(kind="sp" AND in(err, "7000215", "7000222"), 1, 0), sfail_t=if(sfail=1, _time, null()), sp=if(kind="sp", coalesce('properties.servicePrincipalId', servicePrincipalId), null()), papi=if(kind="aws" AND (match(coalesce(eventName, ""), "^(List|Describe|Get)") OR match(ec, "DryRunOperation")), eventName, null()), deny=if(isnotnull(papi) AND match(ec, "AccessDenied|UnauthorizedOperation|DryRunOperation"), 1, 0), secret=if(kind="aws" AND ec="" AND match(coalesce(eventName, ""), "^((Batch)?GetSecretValue|GetParameters?(ByPath)?)$"), coalesce('requestParameters.secretId', 'requestParameters.name', 'requestParameters.path', eventName), null()) | eval kind=if(kind="aws" AND isnotnull(secret), "awssec", kind), op=if(kind="audit" AND lower(coalesce('properties.result', result, "success"))="success", coalesce('properties.activityDisplayName', activityDisplayName, operationName, ""), ""), cred=if(match(op, "Add service principal credentials|Certificates and secrets management"), 1, 0), role=if(match(op, "^Add (eligible )?member to role"), 1, 0), is_op=if(cred=1 OR role=1 OR match(op, "^(Add app role assignment to service principal|Add delegated permission grant|Consent to application)") OR match(op, "^Add owner to (service principal|application)"), 1, 0), aop=if(is_op=1, op, null()), prole=if(role=1 AND match(_raw, "Global Administrator|Privileged Role Administrator|Application Administrator"), 1, 0), aipr=coalesce('properties.initiatedBy.user.ipAddress', 'initiatedBy.user.ipAddress', ""), aip=if(match(aipr, "^[0-9A-Fa-f:.]*[.:][0-9A-Fa-f:.]*$"), aipr, null()), pimg=lower(replace(coalesce(ParentImage, ""), "^.*[\\\\/]", "")), img=lower(replace(coalesce(Image, ""), "^.*[\\\\/]", "")), tf=lower(coalesce(TargetFilename, "")), q=lower(coalesce(QueryName, "")), dbw=if(match(img, "^(mysqld|mariadbd)(\\.exe)?$"), 1, 0) | eval b7=case(kind="file" AND (dbw=1 ```B7 a web or database server process writes a script, or the MySQL general log points at one``` OR match(img, "^(w3wp|httpd|nginx|apache2|php-(fpm|cgi)[0-9.]*|lsphp[0-9]*|frankenphp|tomcat[0-9]*)(\\.exe)?$")) AND match(tf, "\\.(aspx|ashx|asmx|asp|jspx?|php[0-9]?|phtml|phar|war)$") AND NOT match(tf, "temporary asp\\.net files|[\\\\/](cache|templates_c|storage[\\\\/]framework[\\\\/]views|wp-content[\\\\/]upgrade)([\\\\/]|$)"), 1, kind="db" AND match(lower(_raw), "general_log_file\\s*=\\s*['\\x22]?[^'\\x22\\s;]*\\.(aspx|ashx|asmx|asp|jspx?|php[0-9]?|phtml|phar)(['\\x22\\s;]|$)"), 1, true(), 0), b7d=if(b7=1 AND (dbw=1 OR kind="db"), 1, 0) | eval b8=if(kind="proc" ```B8 a web, app or database server process spawns a shell, downloader, admin or recon tool``` AND (match(pimg, "^(w3wp|httpd|nginx|apache2|lshttpd|php-(fpm|cgi)[0-9.]*|lsphp[0-9]*|frankenphp|tomcat[0-9]*|sqlservr)(\\.exe)?$") OR (match(pimg, "^javaw?(\\.exe)?$") AND match(lower(coalesce(ParentCommandLine, "")), "catalina|weblogic"))) AND (match(img, "^(cmd|powershell|pwsh|whoami|certutil|bitsadmin|net1?|nltest|wmic|schtasks)(\\.exe)?$") OR match(img, "^(sh|bash|dash|zsh|curl|wget|nc|ncat|socat|nmap|masscan|python[0-9.]*|perl|ruby[0-9.]*)(\\.exe)?$")) AND NOT match(lower(coalesce(CommandLine, "")), "^(/bin/)?sh -c (-- )?/usr/sbin/sendmail -t -i$"), 1, 0) | eval b9=if(kind="dns" AND NOT match(img, "^(chrome|msedge|firefox|brave|opera|vivaldi|iexplore)\\.exe$") AND (match(q, "^(api\\.openai\\.com|api\\.anthropic\\.com|api\\.deepseek\\.com|api\\.x\\.ai|open\\.bigmodel\\.cn|openrouter\\.ai)$") OR match(q, "(^|\\.)(webhook\\.site|httpbin\\.org|httpbun\\.com|interact\\.sh)$")), 1, 0) ```B9 LLM or capture host, extend lists``` | eval entity_type=case(in(kind, "web", "proc", "file", "dns", "db"), "host", kind="audit" AND isnull(aip), "app", true(), "source_ip"), awsip=if(match(coalesce(sourceIPAddress, ""), "^[0-9A-Fa-f:.]*[.:][0-9A-Fa-f:.]*$"), sourceIPAddress, null()), hraw=coalesce(Computer, host), entity=case(entity_type="host" AND match(hraw, "^\\d{1,3}(\\.\\d{1,3}){3}$"), hraw, entity_type="host", lower(mvindex(split(hraw, "."), 0)), in(kind, "aws", "awssec"), awsip, kind="audit", coalesce(aip, 'properties.initiatedBy.app.servicePrincipalId', 'initiatedBy.app.servicePrincipalId'), true(), coalesce('properties.ipAddress', ipAddress, callerIpAddress, 'client.ipAddress')), span=case(kind="web", 600, kind="signin", 60, kind="aws", 300, kind="awssec", 1800, kind="sp" OR kind="audit", 3600, true(), 86400), tb=floor(_time/span)*span, peer=if(kind="web", coalesce(clientip, c_ip, src_ip, src, "-"), "-"), det=case(kind="proc", pimg." > ".img, kind="file", img." wrote ".tf, kind="dns", img." > ".q, kind="audit", op, kind="sp", sp, in(kind, "aws", "awssec"), coalesce('userIdentity.arn', "-"), kind="db", "general_log_file set to a script path") | where isnotnull(kind) AND isnotnull(entity) AND (in(kind, "web", "aws", "awssec", "sp") OR fail+ok+is_op+b7+b8+b9>0) AND NOT (entity_type="source_ip" AND false()) ```egress allow-list: replace the false call with cidrmatch on entity, one per range``` | stats count AS n dc(path) AS paths sum(err4) AS errs dc(obj) AS objs sum(is_obj) AS objn sum(fail) AS fails dc(fuser) AS fusers sum(ok) AS oks max(ok_t) AS last_ok dc(sp) AS sps sum(sfail) AS sfails min(sfail_t) AS first_sfail sum(deny) AS denies dc(papi) AS papis dc(secret) AS secrets sum(is_op) AS ops dc(aop) AS kinds max(cred) AS cred max(prole) AS prole max(b7) AS b7 max(b7d) AS b7d max(b8) AS b8 max(b9) AS b9 values(det) AS det min(_time) AS t0 max(_time) AS t1 BY kind entity_type entity peer tb | eval f1=if(kind="web" AND n>=300 AND paths>=100 AND errs>=0.3*n, 1, 0) ```B1 web burst: 10 min, 300+ requests, 100+ paths, 30%+ errors``` | eval f2=if(kind="web" AND objs>=50 AND objs>=0.8*objn, 1, 0) ```B2 harvest: 10 min, 50+ distinct object-ID paths answered 2xx, 80% new``` | eval spray_t=if(kind="signin" AND fails>=30 AND fusers>=10, tb, null()), f3sp=if(kind="sp" AND sps>=3 AND sfails>=1 AND isnotnull(last_ok) AND isnotnull(first_sfail), if(last_ok>=first_sfail, 1, 0), 0) | eventstats min(spray_t) AS spray_from BY entity_type entity ```B3 spray: 1 min 30+ fails 10+ accounts; or 1 h 3+ apps fail then ok``` | eval f3=if(f3sp=1 OR isnotnull(spray_t), 1, 0), d3=if(f3sp=1, 1, if(kind="signin" AND oks>0 AND isnotnull(spray_from), if(tb>=spray_from, 1, 0), 0)) | eval f4=if(kind="aws" AND papis>=25 AND denies>=10, 1, 0) ```B4 probing: 5 min, 25+ read or dry-run APIs, 10+ denied or dry-run``` | eval f5=if(kind="awssec" AND secrets>=10, 1, 0) ```B5 bulk secret reads: 30 min, 10+ distinct secrets or parameters read``` | eval f6=if(kind="audit" AND (ops>=3 OR kinds>=2), 1, 0), ```B6 app credential, consent, owner or role changes: 1 h, 3+ ops or 2+ kinds``` d6=if(f6=1 AND entity_type="app" AND cred=1 AND prole=1, 1, 0) ```B6 decisive: an app adds a credential and a privileged role``` | where f1+f2+f3+d3+f4+f5+f6+b7+b8+b9>0 | eventstats max(f1) AS pf1 max(f2) AS pf2 BY entity_type entity peer ```B1 and B2 count as two only when one web client did both``` | eval same12=if(kind="web" AND pf1=1 AND pf2=1, 1, 0), evid=case(kind="web", "client ".peer.": ".n." req, ".paths." paths, ".errs." errors, ".objs." ids", kind="signin" AND f3=1, "spray: ".fails." fails ".fusers." users", kind="signin", "ok after spray", kind="sp", sps." apps, ".sfails." bad secrets, ".oks." ok", kind="aws", papis." apis, ".denies." denied/dry-run: ".mvjoin(det, ", "), kind="awssec", secrets." secrets: ".mvjoin(det, ", "), true(), mvjoin(det, "; ")) | stats max(f1) AS b1 max(f2) AS b2 max(f3) AS b3 max(f4) AS b4 max(f5) AS b5 max(f6) AS b6 max(b7) AS b7 max(b8) AS b8 max(b9) AS b9 max(d3) AS d3 max(d6) AS d6 max(b7d) AS d7 max(same12) AS same12 min(t0) AS first_seen max(t1) AS last_seen values(evid) AS evidence BY entity_type entity | eval decisive=max(d3, d6, d7), split12=if(b1=1 AND b2=1 AND same12=0, 1, 0), behaviour_count=b1+b2+b3+b4+b5+b6+b7+b8+b9-split12, stage_count=b1+b2+b3+b4+b5+max(b6, b7)+b8+b9-split12, score=10*behaviour_count+5*stage_count+20*decisive, first_seen=strftime(first_seen, "%F %T"), last_seen=strftime(last_seen, "%F %T"), behaviours=if(b1=1, "B1 web burst; ", "").if(b2=1, "B2 object harvest; ", "").if(split12=1, "(B1 and B2 from different clients); ", "") .if(b3=1, "B3 spray or secret validation; ", "").if(b4=1, "B4 cloud probing; ", "").if(b5=1, "B5 secret reads; ", "") .if(b6=1, "B6 app credential or role; ", "").if(b7=1, "B7 script write; ", "").if(b8=1, "B8 shell spawn; ", "") .if(b9=1, "B9 LLM or request-capture egress", ""), stages=if(b1=1, "reconnaissance ", "").if(b2=1, "collection ", "").if(b3=1, "initial-access ", "").if(b4=1, "discovery ", "") .if(b5=1, "credential-access ", "").if(b6+b7>0, "persistence ", "").if(b8=1, "execution ", "").if(b9=1, "command-and-control", "") | where behaviour_count>=2 OR decisive=1 | table entity entity_type behaviours stages score behaviour_count stage_count decisive first_seen last_seen evidence | sort 0 - score