import sys

import pefile


def main():
    dump_path, out_path = sys.argv[1], sys.argv[2]
    data = open(dump_path, "rb").read()

    pe = pefile.PE(data=data, fast_load=True)
    pe.OPTIONAL_HEADER.FileAlignment = pe.OPTIONAL_HEADER.SectionAlignment
    for section in pe.sections:
        section.PointerToRawData = section.VirtualAddress
        section.SizeOfRawData = (section.Misc_VirtualSize + 0xFFF) & ~0xFFF
    pe.write(out_path)

    print(f"[+] {out_path}: {pe.FILE_HEADER.NumberOfSections} sections, "
          f"base {pe.OPTIONAL_HEADER.ImageBase:#x}, {len(data)} bytes")


if __name__ == "__main__":
    main()
