Skip to content

Instantly share code, notes, and snippets.

@newball2814
Created June 24, 2024 02:52
Show Gist options
  • Save newball2814/bf0c78d9a41c1dd293cbc503c3a0d268 to your computer and use it in GitHub Desktop.
Save newball2814/bf0c78d9a41c1dd293cbc503c3a0d268 to your computer and use it in GitHub Desktop.
Brute force jpeg's width and height
import struct
import sys
def find_sof0_marker(data):
for i in range(len(data) - 9):
if data[i] == 0xFF and data[i+1] == 0xC0:
return i
return None
def modify_jpeg_dimensions(data, width, height):
sof0_pos = find_sof0_marker(data)
if sof0_pos is None:
raise ValueError("SOF0 marker not found in the file.")
width_pos = sof0_pos + 7
height_pos = sof0_pos + 5
new_data = bytearray(data)
new_data[width_pos:width_pos + 2] = struct.pack('>H', width)
new_data[height_pos:height_pos + 2] = struct.pack('>H', height)
return new_data
if len(sys.argv) != 2:
print("Usage: python script_name.py <filename>")
sys.exit(1)
filename = sys.argv[1]
try:
with open(filename, 'rb') as f:
original_data = f.read()
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
sys.exit(1)
# Change size here
for size in range(11, 501):
try:
modified_data = modify_jpeg_dimensions(original_data, size, size)
output_filename = f'flag_{size}x{size}.jpg'
with open(output_filename, 'wb') as f:
f.write(modified_data)
print(f"Saved modified image with size {size}x{size} as {output_filename}")
except Exception as e:
print(f"Error processing size {size}x{size}: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment