Bandit Level 12 → Level 13

Minchae Kim·2023년 5월 17일
0

📌 Level 12 → Level 13


https://overthewire.org/wargames/bandit/bandit13.html

🚩 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 using mkdir. For example: mkdir /tmp/myname123. Then copy the datafile using cp, and rename it using mv. (read the manpages!)

🚩 Problem Solving

password가 data.txt 파일에 존재하나, 반복적으로 compressed되어 있다고 한다. 이 압축을 풀기 전에, 자신만의 폴더를 생성하여 푸는 것을 권고하고 있다.

Level Goal에서 요구한대로 /tmp/cozy 폴더를 생성한 후, cp 명령어를 활용하여 data.txt 파일을 복사할 수 있다. /tmp/cozy라는 directory가 존재함으로, data.txt 파일을 해당 directory 안에 복사할 수 있다.

cp data.txt /tmp/cozy

bandit12@bandit:~$ mkdir /tmp/cozy
bandit12@bandit:~$ cp data.txt /tmp/cozy

cd 명령어로 /tmp/cozy directory로 이동한 후, ls 명령어를 이용하여 출력했더니 cp로 복사한 data.txt 파일이 있는 모습이다.

bandit12@bandit:~$ cd /tmp/cozy
bandit12@bandit:/tmp/cozy$ ls
data.txt

cat 명령어로 내부를 확인해보면, 상당한 양의 Hex Dump를 관찰할 수 있다.

bandit12@bandit:/tmp/cozy$ cat data.txt
00000000: 1f8b 0808 d7d2 c55b 0203 6461 7461 322e  .......[..data2.
00000010: 6269 6e00 013c 02c3 fd42 5a68 3931 4159  bin..<...BZh91AY
00000020: 2653 591d aae5 9800 001b ffff de7f 7fff  &SY.............
00000030: bfb7 dfcf 9fff febf f5ad efbf bbdf 7fdb  ................
00000040: f2fd ffdf effa 7fff fbd7 bdff b001 398c  ..............9.
00000050: 1006 8000 0000 0d06 9900 0000 6834 000d  ............h4..
00000060: 01a1 a000 007a 8000 0d00 0006 9a00 d034  .....z.........4
00000070: 0d1a 3234 68d1 e536 a6d4 4000 341a 6200  ..24h..6..@.4.b.
00000080: 0069 a000 0000 0000 d003 d200 681a 0d00  .i..........h...
00000090: 0001 b51a 1a0c 201e a000 6d46 8068 069a  ...... ...mF.h..
000000a0: 6834 340c a7a8 3406 4000 0680 0001 ea06  h44...4.@.......
000000b0: 8190 03f5 4032 1a00 0343 4068 0000 0686  ....@2...C@h....
000000c0: 8000 0320 00d0 0d00 0610 0014 1844 0308  ... .........D..
000000d0: 04e1 c542 9ab8 2c30 f1be 0b93 763b fb13  ...B..,0....v;..
000000e0: 50c4 c101 e008 3b7a 92a7 9eba 8a73 8d21  P.....;z.....s.!
000000f0: 9219 9c17 052b fb66 a2c2 fccc 9719 b330  .....+.f.......0
00000100: 6068 8c65 e504 5ec0 ae02 fa6d 16bc 904b  `h.e..^....m...K
00000110: ba6c f692 356e c02b 0374 c394 6859 f5bb  .l..5n.+.t..hY..
00000120: 0f9f 528e 4272 22bb 103c 2848 d8aa 2409  ..R.Br"..<(H..$.
00000130: 24d0 d4c8 4b42 7388 ce25 6c1a 7ec1 5f17  $...KBs..%l.~._.
00000140: cc18 ddbf edc1 e3a4 67f1 7a4d 8277 c823  ........g.zM.w.#
00000150: 0450 2232 40e0 07f1 ca16 c6d6 ef0d ecc9  .P"2@...........
00000160: 8bc0 5e2d 4b12 8586 088e 8ca0 e67d a55c  ..^-K........}.\
00000170: 2ca0 18c7 bfb7 7d45 9346 ea5f 2172 01e4  ,.....}E.F._!r..
00000180: 5598 673f 45af 69b7 a739 7814 8706 04ed  U.g?E.i..9x.....
00000190: 5442 1240 0796 6cc8 b2f6 1ef9 8d13 421d  TB.@..l.......B.
000001a0: 461f 2e68 4d91 5343 34b5 56e7 46d0 0a0a  F..hM.SC4.V.F...
000001b0: 72b7 d873 71d9 6f09 c326 402d dbc0 7cef  r..sq.o..&@-..|.
000001c0: 53b1 df60 9ec7 f318 00df 3907 2e85 d85b  S..`......9....[
000001d0: 6a1a e105 0207 c580 e31d 82d5 8646 183c  j............F.<
000001e0: 6a04 4911 101a 5427 087c 1f94 47a2 270d  j.I...T'.|..G.'.
000001f0: ad12 fc5c 9ad2 5714 514f 34ba 701d fb69  ...\..W.QO4.p..i
00000200: 8eed 0183 e2a1 53ea 2300 26bb bd2f 13df  ......S.#.&../..
00000210: b703 08a3 2309 e43c 44bf 75d4 905e 5f96  ....#..<D.u..^_.
00000220: 481b 362e e82d 9093 7741 740c e65b c7f1  H.6..-..wAt..[..
00000230: 5550 f247 9043 5097 d626 3a16 da32 c213  UP.G.CP..&:..2..
00000240: 2acd 298a 5c8a f0c1 b99f e2ee 48a7 0a12  *.).\.......H...
00000250: 03b5 5cb3 0037 cece 773c 0200 00         ..\..7..w<...

🔹 Process

난잡한 Hex Dump file을 Binary file로 바꾸어주는 xxd 명령어와 앞서 언급한 기능을 수행하는 option인 -r을 활용해야 한다. (본래 xxd 명령어는 shell 상에서 Binary file을 Hex Dump로 바꾸어주는 역할을 수행한다.) Binary file로 바꾼 내용은 'password' file에 넣어주었다.

xxd -r data.txt > password

bandit12@bandit:/tmp/cozy$ xxd -r data.txt > password
bandit12@bandit:/tmp/cozy$ ls
data.txt  password

file 명령어로 해당 파일을 읽어보면, data2.bin이라는 파일이 gzip으로 compressed되어 있다고 한다.

bandit12@bandit:/tmp/cozy$ file password
password: gzip compressed data, was "data2.bin", last modified: Sun Apr 23 18:04:23 2023, max compression, from Unix

gzip으로 압축된 파일을 풀기 위해서는 gzip 명령어와 option -d를 활용해야 한다. 이때 주의할 점은, 압축을 풀 때 file name 뒤에는 확장자명을 붙여줘야 한다. gzip확장자명.gz이다.

gzip -d password.gz
→ (ls) data.txt password

압축을 푼 후 다시 file 명령어로 password file을 읽어보면, 이번에는 gzip이 아닌 bzip2로 압축되어 있음을 확인할 수 있다.

bandit12@bandit:/tmp/cozy$ file password
password: bzip2 compressed data, block size = 900k

mv 명령어로, password file의 이름을 'password.bz'로 바꾸어 이동해준다.

mv password password.bz

이후 앞서 gzip으로 압축된 파일을 풀었던 것처럼, bzip2와 압축을 풀어주는 -d option으로 해당 파일의 압축을 풀어주면 된다. bzip2확장자명.bz이다. 이후 다시 file 명령어로 해당 파일을 읽으면, 또 다시 gzip으로 압축되어 있음을 파악할 수 있다. 위의 과정을 다시 반복하면 된다.

bandit12@bandit:/tmp/cozy$ bzip2 -d password.bz
bandit12@bandit:/tmp/cozy$ ls
data.txt  password
bandit12@bandit:/tmp/cozy$ file password
password: gzip compressed data, was "data4.bin", last modified: Sun Apr 23 18:04:23 2023, max compression, from Unix

위에서 했던 시행을 반복하여 압축을 풀어주면, 이번에는 tar로 압축되어 있음을 알 수 있다.

bandit12@bandit:/tmp/cozy$ mv password password.gz
bandit12@bandit:/tmp/cozy$ gzip -d password.gz
bandit12@bandit:/tmp/cozy$ ls
data.txt  password
bandit12@bandit:/tmp/cozy$ file password
password: POSIX tar archive (GNU)

tar로 압축되어 있는 경우, tar 명령어와 압축 푸는 option인 -xvf를 사용하여 동일한 시행을 반복하면 된다. tar확장자명.tar이다.

tar -xvf password.tar

bandit12@bandit:/tmp/cozy$ mv password password.tar
bandit12@bandit:/tmp/cozy$ ls
data.txt  password.tar
bandit12@bandit:/tmp/cozy$ tar -xvf password.tar
data5.bin
bandit12@bandit:/tmp/cozy$ file data5.bin
data5.bin: POSIX tar archive (GNU)

그 이후에도 gzip, bzip2, tar로 압축되어 있으니, 각각의 압축 풀기 option과 그때그때 mv 명령어를 적절히 사용하여 풀어주면 된다. 'ASCII text'가 뜨고, cat으로 온전히 password를 읽어낼 수 있을 때까지 압축을 풀어야 한다.

🔹 Password

Bandit13 Passsword : wbWdlBxEir4CaE8LaPhauuOo6pwRmrDw

📌 Linux Commands


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

🚩 mkdir

mkdir은 'make directory'의 약자로, directory를 생성할 때 사용하는 명령어이다.

mkdir [option][directory name]

경로를 지정하지 않으면 현 위치를 defualt로 하며, 특정한 디렉터리에 폴더를 형성하고자 한다면 위치를 나열하면 된다. 밑의 예시는, /home/user 위치에 디렉터리를 생성한다.

mkdir /home/user/folder

이외에 생성할 디렉터리를 여러 개 나열하여 한 번의 명령어로 복수의 디렉터리를 생성할 수 있다.

mkdir folder1 folder2 folder3

🔹 option

  • -m : 디렉터리를 생성할 때 권한을 설정한다.
  • -p : 상위 경로도 함께 생성한다. 즉 계층형 디렉터리를 한 번에 만들 수 있다.
  • -v : 디렉터리를 생성하고, 생성된 디렉터리에 대한 메세지를 출력한다.

directory 안에 새로운 directory가 있는 계층형 directorymkdir로 만든다면, 'No such file or directory'라는 error가 발생한다. 이때 앞서 언급한 option 중 하나인 -p를 사용하면 된다. 해당 option을 사용하면, 존재하지 않는 폴더를 자동으로 생성해주어 계층형 directory를 형성할 수 있다.

🚩 cp

cp는 'copy'의 약자로, 복사하는 명령어이다. 대체로 아래와 같이 사용한다면, file1.txt 파일을 file2.txt로 이름을 바꾸어 복사한다. file2.txt 자리에 directory 이름을 적는다면, 해당 디렉터리 안에 file1.txt 파일을 복사할 수 있다.

cp file1.txt file2.txt(or directory name)

🚩 mv

mv 명령어는 'move'의 약자로, 파일을 이동하게 해준다. 파일 이름을 바꾸는 것과 결과가 동일하다. cp 명령어와 마찬가지로 file2.txt 자리에 directory name을 입력할 수 있다.

mv file1.txt file2.txt(or directory name)

🚩 Hex Dump

Hex Dump는 램 또는 파일이나 저장 장치에 있는 컴퓨터 데이터의 16진법적인 보임새이다. 주로 8 또는 16 바이트의 행으로 조직된다. C와 큰 연관이 있는 것으로 보여진다.

🚩 xxd

xxd 명령어는 주어진 파일이나 standard input으로 들어온 문자들에 대해서 Hex Dump를 만들어준다. 즉 xxd로 파일 내용을 16진수로 변환해준다.

xxd [option][file name]

🔹 option

  • -r : 'Reverse option'이다. Convert hexdump into binary. xxd 명령어와 반대로, 16진수로 이루어진 hexdump를 binary 파일로 변환해준다.

📌 Linux Concept


🚩 압축 파일 관리 commands

윈도우 운영 체제에서 알집과 같은 압축 유틸리티를 사용했던 경험이 있을 건데, 이때 여러 확장자들(압축형식)을 접했을 것이다. 마찬가지로, Linux에서도 여러 종류의 압축 형식을 지원한다. 당연히 commmand에는 여러 option이 존재함으로, option을 사용해 압축률을 조절할 수 있다.

🔹 gzip

gzip은 GNU zip의 약자로, GNU에서 만든 압축 프로그램이라고 한다. gzip으로 합축하면 파일명에 .gz가 붙는다. 기본적인 명령어로 압축할 때 명령어는, gzip 뒤에 압축하고자 하는 파일명을 적어주면 된다. 압축을 하게 되면 신규 파일이 생성되는 것이 아니라, 기존의 파일 자체가 압축 파일로 대체된다는 것에 유의해야 한다.

gzip newfile.tar

🔸 압축 풀기

압축한 파일을 다시 풀고자 할 때는 gunzip이라는 명령어에 파일명만 지정할 수 있고, 위에서 사용한 바와 같이 gzip 명령어에 -d option을 활용할 수도 있다.

gunzip newfile.tar.gz
gzip -d newfile.tar.gz

🔸 option

  • -d : 압축 풀기
  • -l : 이미 압축된 파일의 정보를 확인할 수 있다.

🔹 bzip2

bzip2는 버로우즈-휠러 변환이라는 블록 정렬 알고리즘과 허브만 부호화를 사용한 압축 프로그램이다. 참고로, gzip보다 압축률은 좋으나 압축하는 데 더 많은 시간이 소요된다. bzip2로 압축하면 파일명 뒤에 .bz2가 붙는다.

압축, 압축 효율 지정, 압축 결과를 표준 출력으로 보낼 때의 방법이 gzipgunzip과 동일하다. 단, 생성되는 확장자에 차이가 있을 뿐이다.

🔸 압축 풀기

bunzip2 명령어를 사용하여 풀거나, -d option를 사용하여 풀 수 있다. gzip과 동일함으로 자세한 사용법은 생략했다.

🔸 option

  • -d : 압축 풀기
  • -l : 파일의 압축 시간을 줄인다. 압축은 빨라지지만, 압축률은 떨어진다.

🚩 tar

tar은 TapeARchive(tar)의 약자로 여러 파일이나 디렉터리들을 하나의 새로운 파일로 묶을 때 사용하는 명령어이다.

🔹 압축/해제

option들은 아래 정리하고, 자주 사용하는 명령어 패턴 먼저 정리해보자.

folder1을 folder2.tar로 압축하고자 한다면, 아래와 같이 -cvf를 사용하면 된다.

tar -cvf folder2.tar folder1

folder2.tar의 압축을 풀고자 한다면, 아래와 같이 -xvf를 사용하면 된다.

tar -xvf folder.tar

🔹 option

  • -x : 압축 해제하기.
  • -v : 압축/해제 하는 과정을 화면에 표시해준다.
  • -f : 파일 이름을 지정할 수 있다.
  • -c : 파일을 묶어준다.

해당 level에서는 앞서 제시한 option들을 모두 합쳐서 -xvf로 사용했다.

profile
It's time to fire on up !

0개의 댓글