roaris / ctf-log

0 stars 0 forks source link

picoCTF: Matryoshka doll (Forensics) #25

Open roaris opened 3 months ago

roaris commented 3 months ago

https://play.picoctf.org/practice/challenge/129

roaris commented 3 months ago

dolls.jpgが手に入るが、JPG画像ではなく、PNG画像らしい

$ file dolls.jpg
dolls.jpg: PNG image data, 594 x 1104, 8-bit/color RGBA, non-interlaced

PNG画像の先頭8バイトは0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0Aと決まっていて、それも確認出来る(fileコマンドもここを見て判定していそう) https://qiita.com/spc_ehara/items/c748ec636283df805926

$ hexdump -C dolls.jpg | head
00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
00000010  00 00 02 52 00 00 04 50  08 06 00 00 00 26 c7 bf  |...R...P.....&..|
00000020  42 00 00 0c 65 69 43 43  50 49 43 43 20 50 72 6f  |B...eiCCPICC Pro|
00000030  66 69 6c 65 00 00 48 89  95 97 07 58 53 c9 16 80  |file..H....XS...|
00000040  e7 96 54 12 5a 20 02 52  42 6f a2 48 0d 20 25 84  |..T.Z .RBo.H. %.|
00000050  16 41 40 aa 20 2a 21 09  24 94 18 13 82 88 9d 75  |.A@. *!.$......u|
00000060  51 c1 b5 8b 28 56 74 55  44 d1 d5 15 90 b5 20 e2  |Q...(VtUD..... .|
00000070  5a 17 c5 ee 5a 16 0b 2a  2b eb e2 2a 36 54 de a4  |Z...Z..*+..*6T..|
00000080  80 ae fb ca f7 e6 fb e6  ce 9f 33 67 ce 9c 73 32  |..........3g..s2|
00000090  73 ef 0c 00 7a 9d 7c 99  2c 1f d5 07 a0 40 5a 28  |s...z.|.,....@Z(|
roaris commented 3 months ago

タイトルにMatryoshkaとあり、再帰的に画像が埋め込まれている可能性があるため、binwalkコマンドを使うらしい

binwalkコマンドとは、与えられたファイルを先頭から走査し、特徴的なシグネチャを見つけると、先頭からのオフセットを出力するというコマンド

https://hana-shin.hatenablog.com/entry/2022/05/09/222555 この記事でbinwalkの使い方を勉強する

実行ファイル

$ cat test.c
#include <stdio.h>

int main() {
    printf("Hello\n");
    return 0;
}
$ gcc test.c
$ ls
a.out  test.c
$ binwalk a.out

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             ELF, 64-bit LSB shared object, AMD x86-64, version 1 (SYSV)

圧縮ファイル

$ fallocate -l 1M test.dat
$ gzip test.dat
$ ls
a.out  test.c  test.dat.gz
$ binwalk test.dat.gz

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             gzip compressed data, has original file name: "test.dat", from Unix, last modified: 2024-03-12 14:28:26

(fallocateについて https://monaski.hatenablog.com/entry/2016/02/21/225849)

アーカイブファイル

$ touch a
$ touch b
$ tar cvf test.tar a b
a
b
$ binwalk test.tar

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             POSIX tar archive (GNU)

ここまでは別にbinwalkを使わなくても、fileコマンドでも確認出来る

$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=857d8f7be01785da17332c03b8ef5cb4abd53b07, for GNU/Linux 3.2.0, not stripped
$ file test.dat.gz
test.dat.gz: gzip compressed data, was "test.dat", last modified: Tue Mar 12 14:28:26 2024, from Unix, original size modulo 2^32 1048576
$ file test.tar
test.tar: POSIX tar archive (GNU)

これら3つのファイルをddコマンドで結合したファイルを作る (ddのオプション: https://atmarkit.itmedia.co.jp/ait/articles/1711/30/news027.html) 0バイト\~15951バイトがa.out 15952バイト\~17011バイトがtest.dat.gz 17012バイト\~27251バイトがtest.tar

$ ls -l a.out test.dat.gz test.tar
-rwxr-xr-x 1 roaris roaris 15952 Mar 12 23:26 a.out
-rw-r--r-- 1 roaris roaris  1060 Mar 12 23:28 test.dat.gz
-rw-r--r-- 1 roaris roaris 10240 Mar 12 23:33 test.tar
$ dd if=a.out of=binwalk.dat seek=0 obs=15952 oflag=seek_bytes
31+1 records in
1+0 records out
15952 bytes (16 kB, 16 KiB) copied, 0.0005062 s, 31.5 MB/s
$ dd if=test.dat.gz of=binwalk.dat seek=15952 obs=1060 oflag=seek_bytes
2+1 records in
1+0 records out
1060 bytes (1.1 kB, 1.0 KiB) copied, 6.58e-05 s, 16.1 MB/s
$ dd if=test.tar of=binwalk.dat seek=17012 obs=10240 oflag=seek_bytes
20+0 records in
1+0 records out
10240 bytes (10 kB, 10 KiB) copied, 8.06e-05 s, 127 MB/s
$ ls -l binwalk.dat
-rw-r--r-- 1 roaris roaris 27252 Mar 12 23:39 binwalk.dat

fileコマンドだと先頭だけ見て実行ファイルだと解釈されている

$ file binwalk.dat
binwalk.dat: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=857d8f7be01785da17332c03b8ef5cb4abd53b07, for GNU/Linux 3.2.0, not stripped

binwalkだと実行ファイルの他に圧縮ファイルとアーカイブファイルが埋め込まれていることが分かる

$ binwalk binwalk.dat

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             ELF, 64-bit LSB shared object, AMD x86-64, version 1 (SYSV)
15952         0x3E50          gzip compressed data, has original file name: "test.dat", from Unix, last modified: 2024-03-12 14:28:26
17012         0x4274          POSIX tar archive (GNU)

-eオプションをつけると、圧縮ファイルを解凍したり、アーカイブファイルを展開してくれる

-e, --extract                Automatically extract known file types
$ ls
a  a.out  b  binwalk.dat  test.c  test.dat.gz  test.tar
$ binwalk -e binwalk.dat

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             ELF, 64-bit LSB shared object, AMD x86-64, version 1 (SYSV)
15952         0x3E50          gzip compressed data, has original file name: "test.dat", from Unix, last modified: 2024-03-12 14:28:26
17012         0x4274          POSIX tar archive (GNU)
$ ls
a  a.out  b  binwalk.dat  _binwalk.dat.extracted  test.c  test.dat.gz  test.tar
$ ls _binwalk.dat.extracted
4274.tar  a  b  test.dat
roaris commented 3 months ago

binwalk -eを繰り返していくと、flag.txtが得られる

$ tree
├── dolls.jpg
└── _dolls.jpg.extracted
    ├── 4286C.zip
    └── base_images
        ├── 2_c.jpg
        └── _2_c.jpg.extracted
            ├── 2DD3B.zip
            └── base_images
                ├── 3_c.jpg
                └── _3_c.jpg.extracted
                    ├── 1E2D6.zip
                    └── base_images
                        ├── 4_c.jpg
                        └── _4_c.jpg.extracted
                            ├── 136DA.zip
                            └── flag.txt

画像は全てマトリョーシカの画像で、zipアーカイブが埋め込まれていた

$ binwalk -e dolls.jpg

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 594 x 1104, 8-bit/color RGBA, non-interlaced
3226          0xC9A           TIFF image data, big-endian, offset of first image directory: 8
272492        0x4286C         Zip archive data, at least v2.0 to extract, compressed size: 378955, uncompressed size: 383936, name: base_images/2_c.jpg
651613        0x9F15D         End of Zip archive, footer length: 22
$ binwalk -e 2_c.jpg

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 526 x 1106, 8-bit/color RGBA, non-interlaced
3226          0xC9A           TIFF image data, big-endian, offset of first image directory: 8
187707        0x2DD3B         Zip archive data, at least v2.0 to extract, compressed size: 196041, uncompressed size: 201443, name: base_images/3_c.jpg
383803        0x5DB3B         End of Zip archive, footer length: 22
383914        0x5DBAA         End of Zip archive, footer length: 22
$ binwalk -e 3_c.jpg

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 428 x 1104, 8-bit/color RGBA, non-interlaced
3226          0xC9A           TIFF image data, big-endian, offset of first image directory: 8
123606        0x1E2D6         Zip archive data, at least v2.0 to extract, compressed size: 77649, uncompressed size: 79806, name: base_images/4_c.jpg
201421        0x312CD         End of Zip archive, footer length: 22
$ binwalk -e 4_c.jpg

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 320 x 768, 8-bit/color RGBA, non-interlaced
3226          0xC9A           TIFF image data, big-endian, offset of first image directory: 8
79578         0x136DA         Zip archive data, at least v2.0 to extract, compressed size: 62, uncompressed size: 81, name: flag.txt
79784         0x137A8         End of Zip archive, footer length: 22