Command Line Compromise
Command Line Compromise
Craig H. Rowland
@CraigHRowland @SandflySecurity
www.sandflysecurity.com
Introduction
www.sandflysecurity.com
Simple First
“Common problems are surprisingly common.”
www.sandflysecurity.com
1000:1 Rule
Defenders need to know thousands of ways for a
system to get compromised. Attackers need to be
right just once.
www.sandflysecurity.com
The Big Five
Processes
Directories
Files
Users
Logs
www.sandflysecurity.com
Suspicious Processes
www.sandflysecurity.com
Suspicious Processes
• Named to look legit or very odd.
• Network activity you don’t recognize.
• High CPU/RAM.
• Deleted binary still running.
• Combination of the above.
www.sandflysecurity.com
Suspicious Processes
Unusual Ports
root@ubuntu18-dirty:/lib# netstat -nalp TCP port 22222
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
tcp 0 0 0.0.0.0:22222 0.0.0.0:* LISTEN 10580/cron
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1293/sshd
tcp 0 332 192.168.1.122 120.136.1.1:56639 ESTABLISHED 11022/2
tcp6 0 0 :::22 :::* LISTEN 1293/sshd
udp 0 0 0.0.0.0:555 0.0.0.0:* 32481/t
raw 0 0 0.0.0.0:1 0.0.0.0:* 7 10580/cron
www.sandflysecurity.com
Suspicious Processes
Investigating a PID
root@ubuntu-18-dirty:/# ls -alR /proc/*/cwd 2> /dev/null | grep tmp
lrwxrwxrwx 1 root root 0 Nov 14 02:07 /proc/10580/cwd -> /tmp
PID of Running
interest from /tmp
root@ubuntu-18-dirty:/tmp# ls -alR /proc/*/exe 2> /dev/null | grep deleted
lrwxrwxrwx 1 root root 0 Nov 13 07:39 /proc/10580/exe -> /usr/bin/perl (deleted)
www.sandflysecurity.com
Suspicious Processes
Investigating a PID
PID of interest
root@ubuntu-18-dirty:~# ls -al /proc/10580/cwd Running
lrwxrwxrwx 1 root root 0 Nov 14 02:07 /proc/10580/cwd -> /tmp
root@ubuntu-18-dirty:/tmp# cat /proc/10580/comm from /tmp
/usr/sbin/cron Says it’s cron.
root@ubuntu-18-dirty:/tmp# cat /proc/10580/cmdline
/usr/sbin/cron
root@ubuntu-18-dirty:/tmp# ls -al /proc/10580/exe
Really, I’m cron!
lrwxrwxrwx 1 root root 0 Nov 13 07:39 /proc/10580/exe -> '/usr/bin/perl (deleted)'
www.sandflysecurity.com
Suspicious Directories
www.sandflysecurity.com
Suspicious Directories
• Used to hide malicious binaries.
www.sandflysecurity.com
Suspicious Directories
Commonly Targeted
/tmp, /var/tmp /lib*, /usr/lib*
/dev /etc
/dev/shm /var/log
/bin /var/spool
/sbin public_html
www.sandflysecurity.com
Suspicious Directories
Weird Names
root@ubuntu18-dirty:~# ls -al /bin
total 17120
drwxr-xr-x 2 root root 4096 Jul 25 21:45
drwxr-xr-x 2 root root 4096 Sep 7 09:52 .
drwxr-xr-x 10 root root 12288 Sep 7 09:52 .
drwxr-xr-x 2 root root 4096 Mar 25 2017 .
drwxr-xr-x 2 root root 4096 Mar 25 2017 . .
drwxr-xr-x 24 root root 4096 Oct 11 04:01 ..
drwxr-xr-x 2 root root 4096 Jun 4 01:56 ..
drwxr-xr-x 2 root root 4096 Jun 4 02:25 ...
drwxr-xr-x 2 root root 4096 Jun 7 00:46 ..%
-rwxr-xr-x 1 root root 1037528 May 16 12:49 bash
-rwxr-xr-x 1 root root 520992 Jun 15 23:46 btrfs
-rwxr-xr-x 1 root root 249464 Jun 15 23:46 btrfs-calc-size
www.sandflysecurity.com
Suspicious Directories
Weird Names
}
root@ubuntu18-dirty:~# ls -al /bin
total 17120
drwxr-xr-x 2 root root 4096 Jul 25 21:45
drwxr-xr-x 2 root root 4096 Sep 7 09:52 .
drwxr-xr-x 10 root root 12288 Sep 7 09:52 .
drwxr-xr-x 2 root root 4096 Mar 25 2017 .
drwxr-xr-x 2 root root 4096 Mar 25 2017 . . What is this?
drwxr-xr-x 24 root root 4096 Oct 11 04:01 ..
drwxr-xr-x 2 root root 4096 Jun 4 01:56 ..
drwxr-xr-x 2 root root 4096 Jun 4 02:25 ...
drwxr-xr-x 2 root root 4096 Jun 7 00:46 ..%
-rwxr-xr-x 1 root root 1037528 May 16 12:49 bash
-rwxr-xr-x 1 root root 520992 Jun 15 23:46 btrfs
-rwxr-xr-x 1 root root 249464 Jun 15 23:46 btrfs-calc-size
www.sandflysecurity.com
Suspicious Directories
Weird Names
root@ubuntu18-dirty:~# ls -lap /bin “space”
total 17120 “space” dot
drwxr-xr-x 2 root root 4096 Jul 25 21:45 /
drwxr-xr-x 2 root root 4096 Sep 7 09:52 ./ dot “space”
drwxr-xr-x 10 root root 12288 Sep 7 09:52 ./
drwxr-xr-x 2 root root 4096 Mar 25 2017 . / dot dot “space”
drwxr-xr-x 24 root root 4096 Oct 11 04:01 ../ Trying to look
drwxr-xr-x 2 root root 4096 Jun 4 01:56 .. / legit
drwxr-xr-x 2 root root 4096 Jun 4 02:25 .../
drwxr-xr-x 2 root root 4096 Jun 7 00:46 ..%/ Special
-rwxr-xr-x 1 root root 1037528 May 16 12:49 bash characters
-rwxr-xr-x 1 root root 520992 Jun 15 23:46 btrfs
-rwxr-xr-x 1 root root 249464 Jun 15 23:46 btrfs-calc-siz
www.sandflysecurity.com
Suspicious Directories
Hidden Directories
root@ubuntu-18:/# find / -type d -name ".*”
/root/.local
/root/.ssh
/lib/modules/4.15.0-34-generic/vdso/.build-id
/bin/. .
/dev/.blKb
/dev/shm/. . All suspicious!
www.sandflysecurity.com
Suspicious Files
www.sandflysecurity.com
Suspicious Files
• Exploit traces left behind from attack.
• Files that are not what they claim to be or are out of place.
www.sandflysecurity.com
Suspicious Files
Exploit Traces Left Behind
root@ubuntu-18-dirty:~# ls -al /tmp Left by poorly
total 44 written or
rwxrwxrwt 8 root root 12288 Sep 5 00:12 .
drwxr-xr-x 23 root root 4096 Sep 5 00:03 .. crashed
drwxrwxrwt 2 root root 4096 Sep 5 00:03 .font-unix
drwxrwxrwt 2 root root 4096 Sep 5 00:03 .ICE-unix
log cleaner.
drwxrwxrwt 2 root root 4096 Sep 5 00:03 .Test-unix
-rw-r--r-- 1 root root 2304 Sep 5 00:12 utmp.bak
drwxrwxrwt 2 root root 4096 Sep 5 00:03 .X11-unix
drwxrwxrwt 2 root root 4096 Sep 5 00:03 .XIM-unix
www.sandflysecurity.com
Suspicious Files
Immutable Files
www.sandflysecurity.com
Suspicious Files
File Masquerading
root@ubuntu-18-dirty:~/public_html# file * -p
1.jpg: ELF 32-bit LSB executable, ARM, …statically linked, stripped
2.jpg: ELF 32-bit LSB executable, ARM, …statically linked, stripped
3.jpg: ELF 32-bit MSB executable, MIPS, …statically linked, stripped
4.jpg: ELF 32-bit LSB executable, MIPS, …statically linked, stripped
index.html: data
logo.jpg: PHP script, ASCII text
logo.png: PNG image data, 32 x 32, 8-bit/color RGBA, non-interlaced
www.sandflysecurity.com
Suspicious Files
Hidden Binaries
www.sandflysecurity.com
Suspicious Files
Named Pipes
root@ubuntu-18:/# find / -type p
/run/dmeventd-client
/run/dmeventd-server
…
/tmp/f
www.sandflysecurity.com
Suspicious Files
System Files Modified
[root@centos-6-2 ~]# rpm -Va | grep ^..5.
SM5....T. c /etc/ssh/sshd_config
S.5....T. c /etc/ssh/ssh_config Manually
S.5....T. c /root/.bashrc
inspect
these.
root@ubuntu16-dirty:/bin# debsums -c
/usr/sbin/nologin
www.sandflysecurity.com
Suspicious Users
www.sandflysecurity.com
Suspicious Users
History File Weirdness
Should www user have Creation
a history file? Date Anti-forensics
www.sandflysecurity.com
Suspicious Users
SSH Keys
www.sandflysecurity.com
Suspicious Users
Scheduler Persistence
root@ubuntu-18:/tmp# crontab -l
* * * * * /tmp/.d >/dev/null 2>&1
Weird name.
Cron script in tmp?
www.sandflysecurity.com
Suspicious Logs
www.sandflysecurity.com
Suspicious Logs
Audit Log Tampering
High value and frequently targeted files:
www.sandflysecurity.com
Suspicious Logs
Zero Byte Logs
root@ubuntu16-dirty:~# ls -al /var/log
total 104
drwxrwxr-x 8 root syslog 4096 Oct 24 06:25 . Zero byte
drwxr-xr-x 17 root root 4096 Jul 25 23:18 .. audit logs?
-rw-r----- 1 syslog adm 0 Oct 25 00:55 auth.log
-rw-r----- 1 syslog adm 0 Oct 25 00:55 auth.log.1
-rw-r----- 1 syslog adm 0 Oct 25 00:55 auth.log.2.gz No bad logins?
-rw-rw---- 1 root utmp 0 Oct 25 00:55 btmp
-rw------- 1 root utmp 0 Oct 25 00:55 btmp.1 No kernel
… messages?
-rw-r—— 1 syslog adm 0 Oct 25 00:55 kern.log
Log rotate
-rw-r----- 1 syslog adm 0 Oct 25 00:55 kern.log.1
compressed a
-rw-r----- 1 syslog adm 0 Oct 25 00:55 kern.log.2.gz
zero byte file?
-rw-r--r-- 1 root root 292292 Oct 24 21:09 lastlog
-rw-r----- 1 syslog adm 0 Oct 25 00:55 syslog
-rw-r----- 1 syslog adm 0 Oct 25 00:55 syslog.1 Date/time all
-rw-r----- 1 syslog adm 0 Oct 25 00:55 syslog.2.gz identical?
…
www.sandflysecurity.com
Suspicious Logs
Null Erased Logins
root@ubuntu16-dirty:~# utmpdump /var/run/utmp
Utmp dump of /dev/stdin
[2] [00000] [~~ ] [reboot ] [~ ] [4.4.0-93-generic ] [0.0.0.0 ] [Tue Sep 05 00:03:17 2017 UTC]
[1] [00053] [~~ ] [runlevel] [~ ] [4.4.0-93-generic ] [0.0.0.0 ] [Tue Sep 05 00:03:22 2017 UTC]
[6] [01391] [tyS0] [LOGIN ] [ttyS0 ][ ] [0.0.0.0 ] [Tue Sep 05 00:03:23 2017 UTC]
[6] [01388] [tty1] [LOGIN ] [tty1 ][ ] [0.0.0.0 ] [Tue Sep 05 00:03:23 2017 UTC]
[7] [01488] [ts/0] [root ] [pts/0 ] [120.136.1.1 ] [120.136.1.1 ] [Tue Sep 05 00:03:57 2017 UTC]
[0] [00000] [ ] [ ][ ][ ] [0.0.0.0 ][ ]
www.sandflysecurity.com
Conclusions
• Focus on simple first.
• Remember the 1000:1 rule works in your
advantage once a host is compromised.
www.sandflysecurity.com
Linux Compromise Detection Command Cheat Sheet
The Big Five Directories Users
Processes • Directories • Files • Users • Logs Commonly targeted directories: Find all ssh authorized_keys files:
/tmp, /var/tmp, /dev/shm, /var/run,
find / -name authorized_keys
Haste makes waste: /var/spool, user home directories Find history files for all uses:
echo "Don't Panic." List and delimit spaces, etc. in names: find / -name .*history
ls -lap History files linked to /dev/null:
Processes List all hidden directories: ls -alR / 2> /dev/null | grep .*history
Large amounts of CPU/RAM: find / -type d -name ".*" | grep null
top List UID 0/GID 0 users:
Process tree: Files grep ":0:" /etc/passwd
ps -auxwf Show all immutable files and directories: Check sudoers file:
Open network ports or raw sockets: lsattr / -R 2> /dev/null | grep "\----i" cat /etc/sudoers and /etc/group
netstat -nalp Find SUID/SGID files: Check scheduled tasks:
netstat -plant find / -type f \( -perm -04000 -o -perm crontab -l
-02000 \) -exec ls -lg {} \;
ss -a -e -i atq
lsof [many options] Files/dirs with no user/group name: systemctl list-timers --all
find / \( -nouser -o -nogroup \) -exec
Deleted binaries still running:
ls -lg {} \; Logs
ls -alR /proc/*/exe 2> /dev/null | grep
deleted List all file types in current dir: Check for zero size logs:
file * -p ls -al /var/log/*
Process command name/cmdline:
cat /proc/<PID>/comm Find executables anywhere, /tmp, /dev, etc.: Dump audit logs:
cat /proc/<PID>/cmdline find / -type f -exec file -p '{}' \; | utmpdump /var/log/wtmp
grep ELF
Real process path: utmpdump /var/run/utmp
Find all named pipes: utmpdump /var/log/btmp
ls -al /proc/<PID>/exe
find / -type p
Process environment: last
Find files modified/created within last day: lastb
cat /proc/<PID>/environ
find / -mtime -1
Process working directory: Logs with binary in them:
Persistence areas: grep [[:cntrl:]] /var/log/*.log
ls -alR /proc/*/cwd
/etc/rc.local, /etc/initd, /etc/rc*.d, /etc/
ls -alR /proc/*/cwd 2> /dev/null | grep modules, /etc/cron*, /var/spool/cron/*
tmp
Package commands to find changed files:
ls -alR /proc/*/cwd 2> /dev/null | grep
rpm -Va | grep ^..5.
dev
debsums -c
www.sandflysecurity.com
@SandflySecurity
2018-11-14
Links
www.sandflysecurity.com/blog/compromised-linux-cheat-sheet/
www.sandflysecurity.com
www.sandflysecurity.com