colorful code is beautiful
首先题目名称提示:colorful code is beautiful,这点当时第一时间想到了前段时间安恒赛misc有一题colorful porgrammingcolorful porgramming详情见:https://www.bertnase.de/npiet/
附件中data1是文本文件,data2是数据文件,用hexdump查看如下
data1中是0-19的数字,用空格分开。也看不出什么别的(当时在这浪费了比较多的时间)。
data1暂时也看不出来和图片有什么关系,所以图片的线索在data2
咋一看也和图片没什关系,但是当我们将每一个字节的十六进制转换成RGB十进制,三个一组
from binascii import *
with open('data2','rb') as f:
f = hexlify(f.read()).decode()
n = 0
color_list = []
for i in range(0,len(f),2):
i = f
color_list.append(int(i,16))
n += 1
if n == 3:
print(tuple(color_list))
color_list = []
n = 0
else:
continue运行结果
PS C:\Users\Administrator\Downloads\colorful_code-1> python .\code.py
(0, 0, 0)
(0, 0, 192)
(0, 255, 255)
(0, 255, 0)
(255, 192, 255)
(255, 192, 192)
(192, 192, 255)
(192, 192, 0)
(255, 0, 255)
(255, 0, 0)
(192, 0, 0)
(192, 0, 192)
(255, 255, 255)
(255, 255, 0)
(255, 255, 192)
(0, 192, 0)
(0, 192, 192)
(192, 255, 255)
(192, 255, 192)
(0, 0, 255)
(20, 20, 20)
(21, 21, 21)
(22, 22, 22)
(23, 23, 23)
(24, 24, 24)
(25, 25, 25)
.......
(250, 250, 250)
(251, 251, 251)
(252, 252, 252)
(253, 253, 253)
(254, 254, 254)
(255, 255, 255)很明显,前20组数据和后面的数据不太一样。然后联想到前面data1中只有0-19的数字,猜测data1的0-19应该是对应data2种这二十组像素数据的下标。
OK,那么思路到这里就很清楚了。我们将这二十组RGB像素,按照data1中的顺序,将这些像素putpixel()即可。
思考到这里的时候还有最后一个问题,那就是生成的图片的宽高。要知道宽高,我们首先要知道图片的总像素,总像素,直接计算下data1中有多少个0-19数字。
Python简单处理
def str2list():
with open('data1.txt') as f:
f = f.read()
index_list = f.split(' ')
return index_list
print(str2list())
print(len(str2list()))
这里需要注意,因为data1最后有两个空格,所以会切多一个元素出来,去掉即可。所以这里总像素是:7067
7067看起来不像是一个比较常见的图片总像素数,不太好计算,直接在线分解质因数得到宽高
分解质因数:http://tools.jb51.net/jisuanqi/factor_calc
就先推测宽为: 37,高为: 191 OK,接下来直接Python简单处理下即可得到flag.png
# -*- coding:utf-8 -*-
# Author: mochu7
import PIL
from PIL import Image
from binascii import *
def str2list():
with open('data1','r') as f:
f = f.read()
index_list = f.split(' ')[:-1]
return index_list
def num2color():
with open('data2','rb') as f:
f = hexlify(f.read()).decode()
n = 0
idx = 0
color_dic = {}
color_list = []
for i in range(0,len(f),2):
i = f
color_list.append(int(i,16))
n += 1
if n == 3:
color_dic = tuple(color_list)
color_list = []
n = 0
idx += 1
elif idx == 20:
break
return color_dic
def genimg():
width, height = 37, 191
img = Image.new("RGB",(width,height))
imgpixels = str2list()
colorlist = num2color()
pixlist = []
for pix in imgpixels:
pixlist.append(colorlist)
idx = 0
for w in range(width):
for h in range(height):
img.putpixel(, pixlist)
idx += 1
img.save('flag.png')
if __name__ == '__main__':
# print(len(str2list()))
# print(num2color())
genimg()
npiet online:https://www.bertnase.de/npiet/npiet-execute.php
得到flag
感谢,学到了很多
页:
[1]