electron / rcedit

Command line tool to edit resources of exe
MIT License
1.68k stars 185 forks source link

When using rcedit on 7-Zip created archive, file gets corrupted #69

Open ghost opened 6 years ago

ghost commented 6 years ago

I am trying to change the resources of an sfx exe archive created with 7-Zip. When I do so however, the content of the archive is replaced with the files for the resources. Yes I said replaced. If I do this with a large archive, the file size of the file is reduced dramatically. See below for details.

  1. With 7-Zip installed, create an sfx archive with the GUI or the following command. "C:\Program Files\7-Zip\7z.exe" a -sfx7z.sfx "<path of archive to create ending in .exe>" "<path of file or folder to archive>"
  2. Use the 7-Zip GUI to open the archive and view its contents which should be as expected. Then close the GUI.
  3. Run the following command. rcedit-x64.exe "<path of archive>" --set-version-string "LegalCopyright" "Copyright (c) 2017 <name>"
  4. Again use the 7-Zip GUI to open the archive. Where is the original file or folder that was archived?
thomasmfields commented 6 years ago

I'm also seeing issues with rcedit corrupting my executable after setting the version string. It could be this issue. Is it possible to get a fix?

Mgamerz commented 6 years ago

Same exact issue.

romjacket commented 6 years ago

yup. executables created with 7zsd.sfx will be corrupted randomly.

Draykee commented 5 years ago

I use 7sfx files from: https://github.com/chrislake/7zsfxmm and the following approach:

  1. rcedit.exe 7zsd.sfx <options>
  2. copy /b 7zsd.sfx + sfx_config.txt + MyPackage.7z MyPackage.exe

You'r changing the icon of the SFX module which is in front of the archive.

romjacket commented 5 years ago

You'r changing the icon of the SFX module which is in front of the archive.

yup. fixed. thanks.

ghost commented 4 years ago

Oh, I get it! I was doing the commands Draykee posted in the opposite order and it was not working.

So maybe rcedit could be made to work even when the commands are completed the other way around so that other people would not have this problem?

si458 commented 3 years ago

im having a similar issue with https://github.com/leafac/caxa when i build an exe then try using rcedit to say add an icon, its just Fatal error: Unable to commit changes then when you run the command again, it does apply the icon but the exe size has shrunk by 90% ???

rversteegen commented 3 years ago

Since rcedit relies on the BeginUpdateResource/UpdateResource/EndUpdateResource winapi functions to do all the actual work of modifying an exe, it appears those functions trim off anything past the "official" end of the .exe file (as defined by the file size in the header), which is a popular place to put extra program data, which will be ignored by the OS. So appears that the fix for this bug (and very likely also bug #91 and bug #51) would be to copy any such data out of the .exe and then paste it back on after calling EndUpdateResource.

caesay commented 2 years ago

it appears those functions trim off anything past the "official" end of the .exe file

Indeed that's correct. dotnet5/6 binaries also store the application code (as IL) after the "official end" of the native binary, but it's worth noting that, at least in the dotnet case, simply copying the data away beforehand and putting it back at the end afterwards will not help - you'll still end up with a corrupt exe.

The "offset" pointing to this extra code stored at the end is written within the real executable code, so unless you know how to update this offset to match the new executable length, the exe will still be garbage.

I don't know for sure that other approaches will suffer the same problem (eg. 7z) but I suspect so, and it will likely be impossible to find a universal solution that works for every exe using this approach.

I work-around this problem in dotnet exe's specifically by extracting all of the embedded code at the end and then compiling a whole new binary bundle just like the SDK does it, setting the icon before re-appending the extracted code.

rversteegen commented 2 years ago

Oh, yes, if there's an offset somewhere to the appended data then rcedit can't hope to support it. But that's why appending zip files to an exe is so popular, it works because the header is at the end of the file without needing to hardcode the offset. And so I expect that what I described will work in quite a lot of cases. Don't know about 7zip.

fatherofinvention commented 2 years ago

I use 7sfx files from: https://github.com/chrislake/7zsfxmm and the following approach:

  1. rcedit.exe 7zsd.sfx <options>
  2. copy /b 7zsd.sfx + sfx_config.txt + MyPackage.7z MyPackage.exe

You'r changing the icon of the SFX module which is in front of the archive.

Legend

tpimh commented 2 months ago

Also trying to modify 7z SFX, and rcedit never finishes execution stuck in some kind of infinite loop.

I am using 7z SFX from this repo: https://github.com/garyzyg/7zs-windows

Executing like this: .\rcedit-x64.exe .\7zS.sfx --set-requested-execution-level 'asInvoker'