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…
이번 문제도 etc/cron.d/로 접근하여 cron 파일을 살펴봐야 할 것 같습니다.
같이 풀어보실까요?
bandit23@bandit:~$ cd /etc/cron.d
bandit23@bandit:/etc/cron.d$ ls
cronjob_bandit22 cronjob_bandit23 cronjob_bandit24 e2scrub_all otw-tmp-dir sysstat
/etc/cron.d 디렉토리로 이동하여 안에 있는 파일들을 보니
cronjob_bandit24가 눈에 띄는 군요.
아마 저 파일안에 이번 비밀번호의 힌트가 있을 것 같습니다.
bandit23@bandit:/etc/cron.d$ cat cronjob_bandit24
@reboot bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
* * * * * bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
문제 패턴이 똑같군요!
/usr/bin/cronjob_bandit24.sh 파일을 읽어보죠!!
bandit23@bandit:/etc/cron.d$ 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
스크립트가 처음이라 쉽지 않네요 ㅠㅠ
그래도 하나씩 풀어서 이해해 보겠습니다.
myname=$(whoami)
이것은 저번에도 본 것처럼, 현재 로그인한 계정의 이름을 myname이라는 변수에 저장하는 것 입니다.
myname=bandit24 가 되겠지요!
for i in * .*;
파이썬을 공부하다 보니 살짝 헷갈렸는데요!
*는 현재 디렉토리에서 모든 파일과 폴더를 의미하구요 .*얘는 숨김 파일을 말해요!
그리고 bash에서 공백은 구분자입니다.
예시를 들어서 설명해 보겠습니다.
for i in a b c;
위는 a, b, c를 순서대로 i에 넣으면서 반복하겠다는 뜻입니다.
파이썬과 조금 달라서 헷갈리지요?
결국, 모든 파일(*)을 i에 넣어서 반복 돌리고 그 다음 숨김 파일(.*)을 i에 넣어서 반복을 수행하겠다! 라는 뜻입니다.
if [ "$i" != "." -a "$i" != ".." ];
# -a는 and를 말합니다 :)
$i는 위의 for 문으로 전달받은 값입니다.
현재 디렉토리에서의 파일, 폴더, 숨김 파일 중 하나일 텐데요!
저 조건문이 왜 필요한 걸까요?
예시를 들어 설명해 보겠습니다.
moonsookang@MoonSooui-noteubug ~ % mkdir testdir
moonsookang@MoonSooui-noteubug ~ % cd testdir
moonsookang@MoonSooui-noteubug testdir % touch file.txt
moonsookang@MoonSooui-noteubug testdir % touch .hidden
임의의 폴더를 만들어 텍스트 파일과 숨김 파일을 생성합니다.
moonsookang@MoonSooui-noteubug testdir % ls -al
total 0
drwxr-xr-x 4 moonsookang staff 128 3 23 12:58 .
drwxr-x---+ 24 moonsookang staff 768 3 23 12:58 ..
-rw-r--r-- 1 moonsookang staff 0 3 23 12:58 .hidden
-rw-r--r-- 1 moonsookang staff 0 3 23 12:58 file.txt
ls -al 명령어를 통하여 폴더 내부를 살펴보면 .과 ..이 보입니다.
.(현재 디렉토리)와 ..(부모 디렉토리)를 뜻해요!
얘네들은 시스템 폴더라서 건들면 안되기에 not 조건으로 걸러내는 겁니다!
owner="$(stat --format "%U" ./$i)"
stat은 파일이나 폴더에 대한 상세 정보를 보여주는 명령이입니다.
--format은 리눅스에서 흔히 쓰는 옵션으로 출력물을 내가 원하는 대로 바꾸는 것이에요! %U는 stat 명령어 전용 포맷 문자열입니다.
%U(파일 소유자), %G(파일 소유 그룹), %s(파일 크기), %n(파일 이름) 등이 있어요.
bandit23@bandit:/etc/cron.d$ stat --format "%U" cronjob_bandit24
root
위 실행 결과처럼, 파일의 소유자를 보여줍니다.
timeout -s 9 60 ./$i
timeout은 어떤 명령어를 정해진 시간 동안만 실행하고 종료하는 명령어에요.
여기에 -s 9라는 옵션을 추가하면 강제 종료를 뜻하구요!
풀어보면, ./$i 파일을 60초간 실행하고 강제 종료해! 입니다.
cronjob_bandit24.sh 이 스크립트는 bandit24가 주기적으로 실행합니다.
bandit24가 읽어서 비밀번호를 알아내게 하면 되겠지요?
근데, 여기서 조건이 있습니다!
파일 소유주가 bandit23이어야 한다는 것이에요.
bandit23 계정으로 스크립트를 짜서 bandit24가 읽게 하면 됩니다.
bandit23@bandit:~$ cd /var/spool/bandit24/foo
bandit23@bandit:/var/spool/bandit24/foo$
우선, 스크립트를 실행하는 폴더로 이동합니다.
bandit23@bandit:/var/spool/bandit24/foo$ vi bandit.sh
bandit23@bandit:/var/spool/bandit24/foo$ cat bandit.sh
#!/bin/bash
cat /etc/bandit_pass/bandit24 > /tmp/bandir24_password
그 후 vi 명령어를 이용하여 bandit24가 비밀번호 파일을 읽어서 /tmp 폴더로
파일을 보내게 만듭니다.
bandit24가 실행하게 하기 위해 권한도 변경해 줘야겠지요!
bandit23@bandit:/var/spool/bandit24/foo$ cat /tmp/bandir24_password
gb8KRRCsshuZXI0tUuR6ypOFjiZbf3G8
1분 지난 후 /tmp 경로의 파일을 읽어보면 비밀번호가 출력됩니다 :)