About_DAS_2023

Misc

坐牢8小时,在希望和绝望之间挣扎,唯一能做出来的题被一个5位数字密码拦住了,真是惭愧。在这里复现一下题目,以及解题思路。

Justpaint

下载下来是一个压缩包,被加密了,通过爆破软件得到密码:
FPAhm.png

打开压缩包后发现两个文件,一个jbn.pth,一个train.py。将train.py里面的代码拿去拷打ChatGPT,理解这段代码就是训练一个模型让图片能逼近flag,继续拷打GPT,得到完整的flag生成脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2


class JBN(nn.Module):
def __init__(self):
super(JBN, self).__init__()
self.main = nn.Sequential(
nn.Linear(100, 256),
nn.ReLU(),
nn.Linear(256, 512),
nn.ReLU(),
nn.Linear(512, 452 * 280),
nn.Tanh()
)

def forward(self, x):
img = self.main(x)
img = img.view(-1, 452, 280)
return img


def watch_flag(img):
flag = cv2.imread('./data/data/flag.png')
gray_image = cv2.cvtColor(flag, cv2.COLOR_BGR2GRAY)
flag_tensor = torch.from_numpy(np.array(gray_image))
flag_tensor = flag_tensor.unsqueeze(0).transpose(1, 2)
img_tensor = img
flag_tensor = flag_tensor.unsqueeze(0)
img_tensor = img_tensor.unsqueeze(0)
loss_fn = torch.nn.MSELoss()
loss = loss_fn(flag_tensor.float(), img_tensor)
return loss


# jbn = JBN()
# g_optimizer = torch.optim.Adam(jbn.parameters(), lr=0.001)
# min_loss = float('inf')

# for epoch in range(10):
# random_noise = torch.randn(1, 100)
# jbn_img = jbn(random_noise)
# g_optimizer.zero_grad()
# g_loss = watch_flag(jbn_img)
# g_loss.backward()
# g_optimizer.step()
# with torch.no_grad():
# if g_loss < min_loss:
# min_loss = g_loss
# torch.save(jbn.state_dict(), 'jbn.pth')

import torch
import cv2

# 创建模型实例
jbn = JBN()

# 加载模型参数
jbn.load_state_dict(torch.load(r"C:\Users\ASUSROG\Desktop\jbn.pth"))

# 将模型设置为评估模式(不进行梯度计算)
jbn.eval()

# 生成随机噪声向量
random_noise = torch.randn(1, 100)

# 使用生成器生成图像
generated_image = jbn(random_noise)

# 将生成的图像从张量转换为NumPy数组
generated_image = generated_image.squeeze().detach().numpy()

# 指定保存图像的完整文件路径
image_path = r"C:\Users\ASUSROG\Desktop\flag.png"

# 生成的图像保存为 'flag.png' 文件
cv2.imwrite(image_path, generated_image * 255) # 请确保生成的图像是[0, 255]范围的像素值

print(f"生成的图像已保存到 {image_path}。")

得到flag.png:
FPmdK.png