import sys
def filter_blocks(input_path, output_path, target_hex):
"""按32KB块过滤包含目标十六进制模式的块
Args:
input_path: 输入文件路径
output_path: 输出文件路径
target_hex: 要查找的十六进制字符串(带0x前缀)
"""
# 移除0x前缀并转换为字节
target_hex = target_hex.lower().replace('0x', '')
target_bytes = bytes.fromhex(target_hex)
block_size = 32768 # 32KB块大小
with open(input_path, 'rb') as infile, open(output_path, 'wb') as outfile:
while True:
block = infile.read(block_size)
if not block:
break
if target_bytes not in block:
outfile.write(block)
if __name__ == '__main__':
if len(sys.argv) != 4:
print("用法: python hex_block_filter.py 输入文件 输出文件 0x目标十六进制")
print("示例: python hex_block_filter.py input.bin output.bin 0x9c351d")
sys.exit(1)
filter_blocks(sys.argv[1], sys.argv[2], sys.argv[3])
-
功能:按32KB块处理二进制文件,过滤掉包含指定十六进制模式的块
-
特点:自动处理0x前缀,支持大文件流式处理
-
使用:命令行参数输入输出文件和目标十六进制值(带0x前缀)
-
示例:python hex_block_filter.py input.bin output.bin 0x9c351d
-
输出:生成的新文件将排除所有包含目标模式的32KB块