Bandit Level 12 → Level 13

장일영·2024년 5월 10일

Bandit

목록 보기
13/33

Level

Goal

The password for the next level is stored in the file data.txt, which is a hexdump of a file that has been repeatedly compressed. For this level it may be useful to create a directory under /tmp in which you can work. Use mkdir with a hard to guess directory name. Or better, use the command “mktemp -d”. Then copy the datafile using cp, and rename it using mv (read the manpages!)

다음 레벨의 패스워드는 data.txt 파일에 있다. 이 파일은 반복 압축된 16진수 덤프 파일이다. /tmp 하위에 작업 디렉토리를 생성하면 레벨에 도움이 될 것이다. 하드코딩 하기 어려운 이름으로 mkdir을 사용하거나, mktemp -d 커맨드를 사용하는 더 나은 방법도 있다. 그리고 cp를 사용해 데이터 파일을 복사하고 mv를 사용해 이름을 변경해라(man 페이지를 읽어라).

Commands you may need to solve this level

grep, sort, uniq, strings, base64, tr, tar, gzip, bzip2, xxd, mkdir, cp, mv, file

Helpful Reading Material

Hex dump on Wikipedia

Exploit

Hex Dump는 데이터를 16진법으로 표현한 것이다. 각 바이트(8bits)는 두 자리 16진수 숫자로 표현된다. 일반적으로 8 또는 16 바이트의 행과 공백으로 구성된다. 16진수 메모리 주소가 시작 부분에 위치할 수 있다.

bandit12@bandit:~$ ls -al
total 24
drwxr-xr-x  2 root     root     4096 Oct  5  2023 .
drwxr-xr-x 70 root     root     4096 Oct  5  2023 ..
-rw-r--r--  1 root     root      220 Jan  6  2022 .bash_logout
-rw-r--r--  1 root     root     3771 Jan  6  2022 .bashrc
-rw-r-----  1 bandit13 bandit12 2582 Oct  5  2023 data.txt
-rw-r--r--  1 root     root      807 Jan  6  2022 .profile

bandit12@bandit:~$ file data.txt
data.txt: ASCII text

bandit12@bandit:~$ head data.txt
00000000: 1f8b 0808 6855 1e65 0203 6461 7461 322e  ....hU.e..data2.
00000010: 6269 6e00 013d 02c2 fd42 5a68 3931 4159  bin..=...BZh91AY
00000020: 2653 5948 1b32 0200 0019 ffff faee cff7  &SYH.2..........
00000030: f6ff e4f7 bfbc ffff bff7 ffb9 39ff 7ffb  ............9...
00000040: bd31 eeff b9fb fbbb b9bf f77f b001 3b2c  .1............;,
00000050: d100 0d03 d200 6868 0d00 0069 a00d 0340  ......hh...i...@
00000060: 1a68 00d0 0d01 a1a0 0001 a680 0003 46d4  .h............F.
00000070: 6434 3234 611a 340d 07a4 c351 068f 5000  d424a.4....Q..P.
00000080: 069a 0680 0000 0006 8006 8da4 681a 6868  ............h.hh
00000090: 0d06 8d00 6834 3400 d07a 9a00 01a0 0341  ....h44..z.....A

좌측이 메모리 주소, 중간이 16진수, 우측이 이를 ASCII 값으로 변경한 결과다. 아주 익숙한 형태다... 우선 xxd 커맨드를 이용해 덤프 파일을 원래의 파일로 복원해야 한다.

우선 복원된 파일이 저장될 공간을 생성한다. bandit12 계정은 현 위치에 디렉토리나 파일을 생성할 수 있는 권한이 없다. mktemp 커맨드를 사용하면 /tmp 디렉토리 하위에 무작위로 이름을 정하고, 해당 사용자에게만 접근 권한이 있는 임시 파일이나 디렉토리를 만들 수 있다.

bandit12@bandit:~$ cd $(mktemp -d) && cp /home/bandit12/data.txt $(pwd)

xxd 커맨드를 이용해 덤프 파일을 복원한다.

 -r | -revert
              Reverse operation: convert (or patch) hexdump into binary.  If not writing to stdout, xxd writes into its output  file
              without  truncating  it. Use the combination -r -p to read plain hexadecimal dumps without line number information and
              without a particular column layout. Additional Whitespace and line-breaks are allowed anywhere.
bandit12@bandit:/tmp/tmp.mDzDzvOchP$ xxd -r data.txt > ./revert
bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file revert
revert: gzip compressed data, was "data2.bin", last modified: Thu Oct  5 06:19:20 2023, max compression, from Unix, original size modulo 2^32 573

revert 파일은 gzip compressed data 파일이다. 따라서 gzip 커맨드로 압축을 해제한다.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ gzip -d ./revert
gzip: ./revert: unknown suffix -- ignored

.gz 확장자가 없는 경우 압축을 풀 수 없는 경우가 있다. 파일 이름을 변경해 확장자를 명시하고 압축을 해제한다.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ mv revert revert.gz
bandit12@bandit:/tmp/tmp.mDzDzvOchP$ gzip -d ./revert.gz

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file revert
revert: bzip2 compressed data, block size = 900k

gzip으로 압축한 파일을 압축 해제 하면 bzip2로 압축한 파일이 생긴다. 여러번 압축했다는 것이 이런 의미였다.

gzip(.gz): Lempel-Ziv 방식의 인코딩을 사용해 압축. 60-70% 압축률.
bzip2(.bz2): 블록 정렬 텍스트 압축 알고리즘과 허프만 코딩 방식의 인코딩을 사용해 압축. gzip 보다 높은 압축률.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ bzip2 -d ./revert
bzip2: Can't guess original name for ./revert -- using ./revert.out

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file revert.out
revert.out: gzip compressed data, was "data4.bin", last modified: Thu Oct  5 06:19:20 2023, max compression, from Unix, original size modulo 2^32 20480

다시 gzip 파일이 나왔다.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ mv ./revert.out ./revert.out.gz
bandit12@bandit:/tmp/tmp.mDzDzvOchP$ gzip -d revert.out.gz

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file revert.out
revert.out: POSIX tar archive (GNU)

POSIX tar archive 파일은 tar로 아카이브한 파일이다. 리눅스는 파일이나 폴더를 archive 하는 것과 compress 하는 것이 나뉘어 있다. 여러 파일을 하나의 파일로 묶는 것이 archive다. 보통은 tar 파일을 gzip이나 bzip2로 압축해서 사용하는데 .tar.gz, .tar.bz2와 같은 확장자를 가지는 파일이 그 결과물이다.

tar을 이용해 아카이브 하면 용량이 줄지 않는다. 압축 작업이 아니라 파일이나 폴더를 하나로 묶는 작업이기 때문이다. 단순 아카이빙 기능 뿐만 아니라 tar로 묶이기 전 파일들의 속성과 심볼릭 링크, 디렉토리 구조를 그대로 가져갈 수 있으므로 가장 많이 사용된다.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ tar -xvf ./revert.out
data5.bin
bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file data5.bin
data5.bin: POSIX tar archive (GNU)

gzip, bzip2, tar 커맨드로 계속해서 압축을 해제하면 최종적으로 ASCII text 파일을 얻을 수 있다.

bandit12@bandit:/tmp/tmp.mDzDzvOchP$ file data8.bin
data8.bin: ASCII text

0개의 댓글