{"id":13319,"date":"2016-09-06T16:06:21","date_gmt":"2016-09-06T14:06:21","guid":{"rendered":"http:\/\/www.codilime.com\/?p=13319"},"modified":"2017-11-07T19:04:35","modified_gmt":"2017-11-07T18:04:35","slug":"tw-mma-2-2016-ninth","status":"publish","type":"post","link":"https:\/\/codisec.com\/tw-mma-2-2016-ninth\/","title":{"rendered":"Ninth"},"content":{"rendered":"
Link: https:\/\/score.ctf.westerns.tokyo\/problems\/22 (only for logged in users)
\nPoints: 100
\nCategory: Misc<\/p>\n
Find the flag.
\n<\/a><\/p>\nThis problem is not image based on steganography.<\/p><\/blockquote>\n
tl;dr<\/h2>\n
Take data from IDAT chunk, decompress it and grep for
TWCTF<\/code>.<\/p>\n
Solution explanation<\/h2>\n
The first step in every image\u00a0based challenge is to look at its metadata:<\/p>\n
$ identify -verbose ninth.png \r\nImage: ninth.png\r\n Format: PNG (Portable Network Graphics)\r\n (...)\r\n Geometry: 1200x848+0+0\r\n (...)\r\nidentify: Extra compressed data. `ninth.png' @ warning\/png.c\/MagickPNGWarningHandler\/1671.\r\nidentify: Extra compression data. `ninth.png' @ warning\/png.c\/MagickPNGWarningHandler\/1671.<\/pre>\nWe can notice two important things while solving this task:<\/p>\n
\n
- image size is
1200x848px<\/code><\/li>\n
- identify notices some
Extra compressed\/compression data<\/code> – the flag?<\/li>\n<\/ul>\n
To understand what it means, basic knowledge of PNG “backstage” is required.<\/p>\n
PNG data is organized as chunks. A chunk is a set of bytes containing the following information:<\/p>\n
\n
- Length – 4 bytes<\/li>\n
- Chunk type – 4 bytes<\/li>\n
- Chunk data –\u00a0Length\u00a0<\/em>bytes<\/li>\n
- CRC – 4 bytes<\/li>\n<\/ul>\n
One of chunks type is
IDAT<\/code>. There can be many of them in one PNG file.
IDAT<\/code> chunks contain compressed information about pixels in the image.
\nWe can get this information with simplePython<\/code> script:<\/p>\n
import png\r\nimport zlib\r\n\r\ndef get_decompressed_data(filename):\r\n img = png.Reader(filename)\r\n for chunk in img.chunks():\r\n if chunk[0] == \"IDAT\":\r\n return zlib.decompress(chunk[1])<\/pre>\nThis function returns
4071274<\/code> bytes. They are organized in scanlines which represent information about pixels in each line of an image (3 bytes per pixel) with one additional byte representing filter applied to this line. Knowing that, we can calculate how many bytes we should have and remove them (we are only interested in that “additional compressed data”).<\/p>\n
data = get_decompressed_data(\"ninth.png\")\r\nimage_width = 1200\r\nimage_height = 848\r\nbytes_per_pixel = 3\r\nbytes_per_filter = 1\r\ndata = data[(image_width * bytes_per_pixel + bytes_per_filter) * image_height:]\r\nprint ''.join([x for x in data if x in string.printable])<\/pre>\n$ python solve.py \r\nTWCTF{WAMP_Are_You_Ready?}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Link: https:\/\/score.ctf.westerns.tokyo\/problems\/22 (only for logged in users) Points: 100 Category: Misc Description Find the flag. This problem is not image based on steganography. tl;dr Take data from IDAT chunk, decompress it and grep for TWCTF. Solution explanation The first step…<\/span> <\/p>\n
Read more ›<\/div>\n<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,6],"tags":[8,9],"yoast_head":"\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n