BUUCTF_misc_大白

环境

大白,png宽高修改

flag{He1l0_d4_ba1}

wp

用python写一个修改png图片的脚本,运行即可

import binascii
import struct
import sys
import os

def is_png(file_data):
  """检查文件是否为 PNG 文件"""
  return file_data.startswith(b'\x89PNG\r\n\x1a\n')

def fix_png_dimensions(input_file, output_file=None):
  """
  修复 PNG 文件的宽度和高度信息
  :param input_file: 输入文件路径
  :param output_file: 输出文件路径(可选)
  """
  if not os.path.exists(input_file):
    print(f"错误:文件 '{input_file}' 不存在。")
    return

  with open(input_file, 'rb') as f:
    fr = f.read()

  # 检查文件是否为 PNG
  if not is_png(fr):
    print(f"错误:文件 '{input_file}' 不是有效的 PNG 文件。")
    return

  # 提取关键数据
  data = bytearray(fr[0x0c:0x1d])
  crc32key = int.from_bytes(fr[0x1d:0x21], byteorder='big')

  # 遍历可能的宽度和高度
  max_dimension = 4095  # 最大宽度和高度
  for w in range(max_dimension):
    width_bytes = struct.pack('>i', w)
    for h in range(max_dimension):
      height_bytes = struct.pack('>i', h)

      # 替换宽度和高度
      for x in range(4):
        data[x + 4] = width_bytes[x]
        data[x + 8] = height_bytes[x]

      # 计算 CRC32
      crc32result = binascii.crc32(data) & 0xffffffff

      # 如果 CRC32 匹配,修复文件
      if crc32result == crc32key:
        print(f"找到正确的宽度和高度:{w} x {h}")
        newpic = bytearray(fr)
        for x in range(4):
          newpic[x + 16] = width_bytes[x]  # 修复宽度
          newpic[x + 20] = height_bytes[x]  # 修复高度

        # 设置输出文件路径
        if not output_file:
          # 如果未指定输出文件路径,生成默认路径
          base_name = os.path.splitext(os.path.basename(input_file))[0]
          output_dir = os.path.dirname(input_file)
          output_file = os.path.join(output_dir, f"{base_name}_fixed.png")
        elif os.path.isdir(output_file):
          # 如果输出路径是目录,生成默认文件名
          base_name = os.path.splitext(os.path.basename(input_file))[0]
          output_file = os.path.join(output_file, f"{base_name}_fixed.png")

        # 保存修复后的文件
        with open(output_file, 'wb') as fw:
          fw.write(newpic)
        print(f"修复后的文件已保存为:{output_file}")
        return

  print("未找到正确的宽度和高度。")

def main():
  if len(sys.argv) < 2:
    print("使用方法:python fixpng.py <输入文件> [输出文件]")
    return

  input_file = sys.argv[1]
  output_file = sys.argv[2] if len(sys.argv) > 2 else None
  fix_png_dimensions(input_file, output_file)

if __name__ == "__main__":
  main()

image

posted @ 2025-01-14 10:32  ra1nbowsea  阅读(51)  评论(0)    收藏  举报