r/PiratedGames drama. Is it malware? Yes. Is it cool malware? No
#!/usr/bin/env python3
import struct
import os
import sys
MZ_SIGNATURE = b"MZ"
PE_SIGNATURE = b"PE\x00\x00"
def read_u16(data, offset):
return struct.unpack_from("<H", data, offset)[0]
def read_u32(data, offset):
return struct.unpack_from("<I", data, offset)[0]
def is_valid_pe(data, base):
try:
if data[base:base+2] != MZ_SIGNATURE:
return False
e_lfanew = read_u32(data, base + 0x3C)
pe_offset = base + e_lfanew
if pe_offset + 4 > len(data):
return False
if data[pe_offset:pe_offset+4] != PE_SIGNATURE:
return False
machine = read_u16(data, pe_offset + 4)
number_of_sections = read_u16(data, pe_offset + 6)
size_of_optional_header = read_u16(data, pe_offset + 20)
optional_header_offset = pe_offset + 24
section_table_offset = optional_header_offset + size_of_optional_header
if number_of_sections == 0 or number_of_sections > 96:
return False
max_end = 0
for i in range(number_of_sections):
sec_offset = section_table_offset + i * 40
if sec_offset + 40 > len(data):
return False
raw_ptr = read_u32(data, sec_offset + 20)
raw_size = read_u32(data, sec_offset + 16)
end = raw_ptr + raw_size
if end > max_end:
max_end = end
if max_end == 0:
return False
return max_end
except Exception:
return False
def extract_pes(input_file, output_dir):
with open(input_file, "rb") as f:
data = f.read()
os.makedirs(output_dir, exist_ok=True)
found = 0
offset = 0
data_len = len(data)
while offset < data_len - 2:
if data[offset:offset+2] == MZ_SIGNATURE:
pe_size = is_valid_pe(data, offset)
if pe_size:
pe_data = data[offset:offset+pe_size]
name = f"pe_{found:04d}.exe_do_not_run"
out_path = os.path.join(output_dir, name)
with open(out_path, "wb") as out:
out.write(pe_data)
print(f"[+] Extracted PE at offset 0x{offset:X} -> {name}")
found += 1
offset += pe_size
continue
offset += 1
print(f"\nDone. Extracted {found} PE files.")
if __name__ == "__main__":
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <input_file> <output_dir>")
sys.exit(1)
extract_pes(sys.argv[1], sys.argv[2])
Last updated