学逆向论坛

找回密码
立即注册

只需一步,快速开始

发新帖

633

积分

0

好友

9

主题
发表于 2021-5-11 16:46:23 | 查看: 1790| 回复: 1
首先题目名称提示:colorful code is beautiful,这点当时第一时间想到了前段时间安恒赛misc有一题colorful porgramming

colorful code is beautiful

colorful code is beautiful


colorful porgramming详情见:https://www.bertnase.de/npiet/
附件中data1是文本文件,data2是数据文件,用hexdump查看如下

colorful code is beautiful

colorful code is beautiful

data1中是0-19的数字,用空格分开。也看不出什么别的(当时在这浪费了比较多的时间)。

colorful code is beautiful

colorful code is beautiful

data1暂时也看不出来和图片有什么关系,所以图片的线索在data2

colorful code is beautiful

colorful code is beautiful

咋一看也和图片没什关系,但是当我们将每一个字节的十六进制转换成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[i:i+2]
        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的数字,猜测data10-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()))

colorful code is beautiful

colorful code is beautiful

这里需要注意,因为data1最后有两个空格,所以会切多一个元素出来,去掉即可。所以这里总像素是:7067

7067看起来不像是一个比较常见的图片总像素数,不太好计算,直接在线分解质因数得到宽高

分解质因数:http://tools.jb51.net/jisuanqi/factor_calc

colorful code is beautiful

colorful code is beautiful

就先推测宽为: 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[i:i+2]
            color_list.append(int(i,16))
            n += 1
            if n == 3:
                color_dic[idx] = 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[int(pix)])
    idx = 0
    for w in range(width):
        for h in range(height):
            img.putpixel([w,h], pixlist[idx])
            idx += 1
    img.save('flag.png')


if __name__ == '__main__':
    # print(len(str2list()))
    # print(num2color())
    genimg()

colorful code is beautiful

colorful code is beautiful

npiet onlinehttps://www.bertnase.de/npiet/npiet-execute.php

colorful code is beautiful

colorful code is beautiful

得到flag






温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
已有 1 人评分学币 理由
roger + 1 饮水思源,吃水不忘挖井人!

总评分: 学币 + 1   查看全部评分

    发表于 2021-12-14 16:37:51
    感谢,学到了很多

    小黑屋|手机版|站务邮箱|学逆向论坛 ( 粤ICP备2021023307号 )|网站地图

    GMT+8, 2024-4-27 07:44 , Processed in 0.150583 second(s), 45 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表