DEV Community

tktkkame
tktkkame

Posted on Originally published at zenn.dev

Detect Linux escalation: uid != euid, and a bug in my own one-liner

The scenario is fictional (RFC 5737 IPs). The awk and SUID diff were run on synthetic data; auditd rules come from the book and are untested live.

A web-service account (uid=33) running a shell with euid=0 is the signature of SUID/sudo escalation.

What you'll learn

  • The auditd rule: -a always,exit -F arch=b64 -S execve -C uid!=euid -F euid=0 -k suid_exec
  • Why my book's awk one-liner returned zero hits
  • A field-wise detector that works
  • SUID baseline diffing

The bug

The book used /uid=/ && /euid=0/ && !/uid=0/. But euid=0 itself contains uid=0, so the negation excludes every line. Verified on synthetic logs: 0 hits.

Fixed version, comparing fields:

awk '{u="";e="";c=""; for(i=1;i<=NF;i++){ if($i ~ /^uid=/) u=substr($i,5); if($i ~ /^euid=/) e=substr($i,6); if($i ~ /^comm=/) c=$i } if(e==0 && u!=0 && c ~ /"(sh|bash|dash)"/) print u, c}'
Enter fullscreen mode Exit fullscreen mode

It flags only the uid=33 euid=0 comm="sh" line.

SUID baseline

find / -xdev -perm -4000 -type f | sort into a baseline, then diff periodically. In the test, a chmod u+s showed up as exactly one diff line and chmod u-s cleared it.

Next: sudo misconfiguration and shell escapes.


Adapted from chapter 25 of my Japanese book on server defense.

Top comments (0)