CTF-MISC-多文件处理

发布时间 2023-05-21 15:01:51作者: CierraRunnis

1.批量修改文件头

题目来源-NSSCTF Round #12

附件给了一个压缩包,里面有tex文件和markdown文件

 

用010editor查看tex文件,可以查到该文件是有标识信息的png文件

 删除8950前面的数据,保存为png文件后可以看到

 按照这个思路将所有tex文件全部删除前缀就能得到flag

我是个菜鸟是手动删的

这里附上探姬师傅的脚本和出题思路

import os
import glob

def find_and_remove_hex(input_file, hex_str):
    with open(input_file, 'rb') as f:
        file_content = f.read()
    
    hex_content = file_content.hex()
    start_index = hex_content.find(hex_str)

    if start_index == -1:
        return None
    
    cleaned_hex_content = hex_content[start_index:]
    cleaned_binary_content = bytes.fromhex(cleaned_hex_content)

    return cleaned_binary_content

def main():
    tex_files = glob.glob('*.tex')

    for tex_file in tex_files:
        png_file = os.path.splitext(tex_file)[0] + '.png'
        cleaned_content = find_and_remove_hex(tex_file, '89504e47')

        if cleaned_content is not None:
            with open(png_file, 'wb') as f:
                f.write(cleaned_content)
        else:
            print(f"ERROR")

if __name__ == "__main__":
    main()
View Code

https://dqgom7v7dl.feishu.cn/docx/QG3PdTU3Qoc8ZZxy6x2c8EXHn7c