For obvious reasons, the stage2*.bin file will be created as root when launching the script with sudo. In this case, the shellcode (that will run as non privileged user) won't be able to delete the stage2*.bin file (EPERM).
I guess that the file should be chowned so the owner becomes the non-root user.
A quick fix is adding this snippet as soon as the file is created.
if os.getuid() == 0:
uid = int(os.environ.get("SUDO_UID"))
guid = int(os.environ.get("SUDO_GID"))
os.chown(stage2_path, uid, guid)
For obvious reasons, the
stage2*.binfile will be created as root when launching the script withsudo. In this case, the shellcode (that will run as non privileged user) won't be able to delete thestage2*.binfile (EPERM).I guess that the file should be
chowned so the owner becomes the non-root user.A quick fix is adding this snippet as soon as the file is created.