例题ctfhub/隐写v2.0
打开是一张图片
文件分离得到zip,爆破密码得到7878
打开得到0和1,
!!!不是二进制转图片,直接是二维码
缩小能看到
000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0011111111111110110000111110000001001111110000000110000011111111111100
0011111111111110110000111110000001001111110000000110000011111111111100
0011000000000110110000001111100110111111001111111001110011000000000100
0011000000000110110000001111100110111111001111111001110011000000000100
0011001111100110111111110001100111000011110111100111110011011111100100
0011001111100110001111001000011111001111111000011111110011011111100100
0011001111100110001111001000011111001111111000011111110011011111100100
0011001111100110000011000000011001110011000111111000000011011111100100
0011001111100110000011000000011001110011000111111000000011011111100100
0011000000000110110000000110011111000011110001111111110011000000000100
0011000000000110110000000110011111000011110001111111110011000000000100
0011111111111110110011001001100110110011001001100110110011111111111100
0011111111111110110011001001100110110011001001100110110011111111111100
0000000000000000111100000110000110111100000111111000000000000000000000
0000001111100110110000001110000111110011111000000111111111100001111100
0000001111100110110000001110000111110011111000000111111111100001111100
0000110111100001110011110000000000000011000110000001001100111110011000
0000110111100001110011110000000000000011000110000001001100111110011000
0011000000000110000011111110011000110000001000011111111111100111100100
0011000000000110000011111110011000110000001000011111111111100111100100
0000000001111001000011110110011111000000001001100001001100100000000000
0000000001111001000011110110011111000000001001100001001100100000000000
0000001110011110000011110111100000110000000110011001111111111001100100
0000000001111001000011110000011000001111000000011110001100000000011000
0000000001111001000011110000011000001111000000011110001100000000011000
0011111110011110111111001001100111111111001111100001110011011111100100
0011111110011110111111001001100111111111001111100001110011011111100100
0011001000011000001100000001111111111111110001100001111111100000000100
0011001000011000001100000001111111111111110001100001111111100000000100
0011001001100111111100111110011111001100000111111111110011011111100100
0011001001100111111100111110011111001100000111111111110011011111100100
0000110110011001111111000111100001110011001001100000001100100110000000
0011001110000111110011001111100000000011110000000111111111011111111100
0011001110000111110011001111100000000011110000000111111111011111111100
0011000001100000111100000001111000111100110110000000000000100110011000
0011000001100000111100000001111000111100110110000000000000100110011000
0000001000011110111100000111111000110000000000011111110011011001111000
0000001000011110111100000111111000110000000000011111110011011001111000
0000111111111001000000111110011111001111110001100000001100100111111100
0000111111111001000000111110011111001111110001100000001100100111111100
0000111001111110110000111110000001000011000111100001111111100000011100
0011001111111001000011000001111000111111000111100111000011000000011000
0011001111111001000011000001111000111111000111100111000011000000011000
0000110000000110001100110000011110001111111000000001000011000111100000
0000110000000110001100110000011110001111111000000001000011000111100000
0011000001100001110011001110011000001100111001100111110011000110000000
0011000001100001110011001110011000001100111001100111110011000110000000
0011001000000111000011001110011110110011110000000110000000000001111100
0011001000000111000011001110011110110011110000000110000000000001111100
0011001111111001111100111111100111001111001001100110111100000110011100
0011001000011111110011110111111001001111001000011110111111111000000100
0011001000011111110011110111111001001111001000011110111111111000000100
0000000000000000111111110000000111110011110001100001110000011001111000
0000000000000000111111110000000111110011110001100001110000011001111000
0011111111111110000011001000011000001111000001111000110011011110000100
0011111111111110000011001000011000001111000001111000110011011110000100
0011000000000110000011110000000110111111111001100000110000011000000000
0011001111100110111111110001100001111111001001111111111111111111100100
0011001111100110111111110001100001111111001001111111111111111111100100
0011001111100110110011001001111110110011001000000001001111011000011100
0011001111100110110011001001111110110011001000000001001111011000011100
0011001111100110110000000110011001000011001111100001110000100000000100
0011001111100110110000000110011001000011001111100001110000100000000100
0011000000000110001111001111100110110011111110000111110011100000000100
0011000000000110001111001111100110110011111110000111110011100000000100
0011111111111110001111110110000000000011001110000000001100000111111100
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
exp1:0为黑色像素,1为白色像素,70行,70列
from PIL import Image
# 二进制字符串
binary_nums = """0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0011111111111110110000111110000001001111110000000110000011111111111100
0011111111111110110000111110000001001111110000000110000011111111111100
0011000000000110110000001111100110111111001111111001110011000000000100
0011000000000110110000001111100110111111001111111001110011000000000100
0011001111100110111111110001100111000011110111100111110011011111100100
0011001111100110001111001000011111001111111000011111110011011111100100
0011001111100110001111001000011111001111111000011111110011011111100100
0011001111100110000011000000011001110011000111111000000011011111100100
0011001111100110000011000000011001110011000111111000000011011111100100
0011000000000110110000000110011111000011110001111111110011000000000100
0011000000000110110000000110011111000011110001111111110011000000000100
0011111111111110110011001001100110110011001001100110110011111111111100
0011111111111110110011001001100110110011001001100110110011111111111100
0000000000000000111100000110000110111100000111111000000000000000000000
0000001111100110110000001110000111110011111000000111111111100001111100
0000001111100110110000001110000111110011111000000111111111100001111100
0000110111100001110011110000000000000011000110000001001100111110011000
0000110111100001110011110000000000000011000110000001001100111110011000
0011000000000110000011111110011000110000001000011111111111100111100100
0011000000000110000011111110011000110000001000011111111111100111100100
0000000001111001000011110110011111000000001001100001001100100000000000
0000000001111001000011110110011111000000001001100001001100100000000000
0000001110011110000011110111100000110000000110011001111111111001100100
0000000001111001000011110000011000001111000000011110001100000000011000
0000000001111001000011110000011000001111000000011110001100000000011000
0011111110011110111111001001100111111111001111100001110011011111100100
0011111110011110111111001001100111111111001111100001110011011111100100
0011001000011000001100000001111111111111110001100001111111100000000100
0011001000011000001100000001111111111111110001100001111111100000000100
0011001001100111111100111110011111001100000111111111110011011111100100
0011001001100111111100111110011111001100000111111111110011011111100100
0000110110011001111111000111100001110011001001100000001100100110000000
0011001110000111110011001111100000000011110000000111111111011111111100
0011001110000111110011001111100000000011110000000111111111011111111100
0011000001100000111100000001111000111100110110000000000000100110011000
0011000001100000111100000001111000111100110110000000000000100110011000
0000001000011110111100000111111000110000000000011111110011011001111000
0000001000011110111100000111111000110000000000011111110011011001111000
0000111111111001000000111110011111001111110001100000001100100111111100
0000111111111001000000111110011111001111110001100000001100100111111100
0000111001111110110000111110000001000011000111100001111111100000011100
0011001111111001000011000001111000111111000111100111000011000000011000
0011001111111001000011000001111000111111000111100111000011000000011000
0000110000000110001100110000011110001111111000000001000011000111100000
0000110000000110001100110000011110001111111000000001000011000111100000
0011000001100001110011001110011000001100111001100111110011000110000000
0011000001100001110011001110011000001100111001100111110011000110000000
0011001000000111000011001110011110110011110000000110000000000001111100
0011001000000111000011001110011110110011110000000110000000000001111100
0011001111111001111100111111100111001111001001100110111100000110011100
0011001000011111110011110111111001001111001000011110111111111000000100
0011001000011111110011110111111001001111001000011110111111111000000100
0000000000000000111111110000000111110011110001100001110000011001111000
0000000000000000111111110000000111110011110001100001110000011001111000
0011111111111110000011001000011000001111000001111000110011011110000100
0011111111111110000011001000011000001111000001111000110011011110000100
0011000000000110000011110000000110111111111001100000110000011000000000
0011001111100110111111110001100001111111001001111111111111111111100100
0011001111100110111111110001100001111111001001111111111111111111100100
0011001111100110110011001001111110110011001000000001001111011000011100
0011001111100110110011001001111110110011001000000001001111011000011100
0011001111100110110000000110011001000011001111100001110000100000000100
0011001111100110110000000110011001000011001111100001110000100000000100
0011000000000110001111001111100110110011111110000111110011100000000100
0011000000000110001111001111100110110011111110000111110011100000000100
0011111111111110001111110110000000000011001110000000001100000111111100
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000""".replace('\n', '')
# 图像尺寸
MAX = 70
pic = Image.new("RGB", (MAX, MAX))
# 填充像素
i = 0
for y in range(MAX):
for x in range(MAX):
if binary_nums[i] == '0':
pic.putpixel((x, y), (0, 0, 0)) # 黑色
else:
pic.putpixel((x, y), (255, 255, 255)) # 白色
i += 1
# 保存图像
pic.save("flag.png")
exp2:简化1
from PIL import Image
# 从 flag.txt 文件中读取二进制字符串
with open('flag.txt', 'r') as file:
binary_nums = file.read().strip().replace('\n', '')
# 图像尺寸
MAX = 70
pic = Image.new("RGB", (MAX, MAX))
# 填充像素
i = 0
for y in range(MAX):
for x in range(MAX):
if binary_nums[i] == '0':
pic.putpixel((x, y), (0, 0, 0)) # 黑色
else:
pic.putpixel((x, y), (255, 255, 255)) # 白色
i += 1
# 保存图像
pic.save("flag.png")
print("图像已保存为 flag.png")
代码解释
1. PIL
(Python Imaging Library)
PIL
是 Python Imaging Library 的缩写,是一个用于处理图像的库。它提供了许多图像处理功能,如读取、修改和保存各种图像格式。PIL
已经不再维护,但它的 fork 版本 Pillow
仍在积极开发和维护中。因此,通常我们会使用 Pillow
而不是原始的 PIL
。
2. Image
类
Image
类是 PIL
库中的一个核心类,用于表示和操作图像。你可以通过 Image
类创建新的图像、打开现有的图像文件、修改图像内容以及保存图像。
3. pic
变量
在你的代码中,pic
是一个 Image
对象,表示一个新创建的空白图像。具体来说:
pic = Image.new("RGB", (MAX, MAX))
-
"RGB"
:指定了图像的颜色模式。RGB
表示图像由红、绿、蓝三种颜色通道组成。 -
(MAX, MAX)
:指定了图像的尺寸,宽度和高度都是MAX
像素。
@4. putpixel
方法
putpixel
是 Image
类的一个方法,用于设置图像中某个特定像素的颜色。其语法如下:
image.putpixel((x, y), color)
-
(x, y)
:表示图像中的一个像素位置,x
和y
分别是水平和垂直坐标。 -
color
:表示要设置的颜色。对于RGB
图像,color
是一个包含三个整数的元组,分别表示红、绿、蓝三个颜色通道的值。例如,(0, 0, 0)
表示黑色,(255, 255, 255)
表示白色。 - 越接近0解释越接近黑色,越接近255解释越接近白色。
代码示例中的 putpixel
使用
在你的代码中,putpixel
方法用于根据 binary_nums
字符串中的值设置图像的像素颜色:
i = 0
for y in range(MAX):
for x in range(MAX):
if binary_nums[i] == '0':
pic.putpixel((x, y), (0, 0, 0)) # 黑色
else:
pic.putpixel((x, y), (255, 255, 255)) # 白色
i += 1
-
i
是一个索引,用于遍历binary_nums
字符串。 -
for y in range(MAX)
和for x in range(MAX)
是嵌套的循环,用于遍历图像的每一个像素位置(x, y)
。 -
if binary_nums[i] == '0'
:如果当前字符是0
,则将该位置的像素设置为黑色(0, 0, 0)
。 -
else
:如果当前字符是1
,则将该位置的像素设置为白色(255, 255, 255)
。 -
i += 1
:每次处理完一个像素后,i
自增 1,以便处理下一个字符。
总结
-
PIL
:Python Imaging Library,用于处理图像的库。 -
Image
类:PIL
中的核心类,用于表示和操作图像。 -
pic
变量:一个Image
对象,表示一个新创建的空白图像。 -
putpixel
方法:用于设置图像中某个特定像素的颜色。
通过这些工具,你可以创建和操作图像,生成二维码图像并保存为文件。希望这些解释对你有帮助!