Level 23 → Level 24

옥영진·2020년 5월 19일
0

Bandit - OverTheWire

목록 보기
24/33

Level 23 → Level 24

목표

Level Goal
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…

Commands you may need to solve this level
cron, crontab, crontab(5) (use “man 5 crontab” to access this)

이전 레벨과 동일하게 /etc/cron.d/ 디렉토리에 있는 설정 파일을 확인하여 어떤 명령어가 실행되는지 알아야 한다. 이 레벨에서는 쉘스크립트 파일을 하나 작성할 필요가 있다.

해결

cd /etc/cron.d/
cat ./cronjob_bandit24
cat /usr/bin/cronjob_bandit24.sh

이전과 마찬가지로 하나의 쉘 스크립트 실행하는데 그 내용을 확인해보면 다음과 같다.

#!/bin/bash

myname=$(whoami)

cd /var/spool/$myname
echo "Executing and deleting all scripts in /var/spool/$myname:"
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

/var/spool/$myname/ 디렉토리에 있는 스크립트들을 실행한 후 삭제하는 스크립트임을 알 수 있다. 즉, 다음 레벨인 bandit24의 패스워드를 알기 위해서 /etc/bandit_pass/bandit24 파일의 내용을 저장하는 스크립트를 작성하여 /var/spool/bandit24/ 디렉토리에 저장한다면 해당 스크립트가 cron에 의해 실행되어 패스워드를 얻을 수 있을 것이다.
스크립트를 실행하고 나면 바로 지워지므로 스크립트 코드를 /tmp/bandityj/script.sh 라는 파일에 복사해두기로 한다. 아래는 그 스크립트 코드이다.

#!/bin/bash

cat /etc/bandit_pass/bandit24 > /tmp/bandityj/pass

생성한 쉘 스크립트에 실행 권한을 부여하고 나서 /var/spool/bandit24/ 디렉토리 내에 복사해둔다. 1분마다 작업이 실행되므로 기다렸다가 /tmp/bandityj/ 디렉토리를 보면 다음 레벨로 가는 패스워드가 저장된 파일을 확인할 수 있다. (디렉토리에 pass 파일 쓰기 작업을 해야 하므로 /tmp/bandityj/ 디렉토리에도 쓰기 권한을 부여해야 한다.)

profile
안녕하세요 함께 공부합시다

0개의 댓글