feat(handler): add upx decompression to elf executable #1189
+68
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
UPX is a free, secure, portable, extendable, high-performance
executable packer for several executable formats.
The UPX header is written after the elf header. The offset depends on how big the ELF is. Luckily UPX writes in the last 4 bytes of the compressed file the offet to the header.
Create UPX file :
Decompress UPX file:
Key notes:
UPX!
as magicstat
to get information about the compressed file, which is why the total compressed file size is no where to be foundHeader format :
Using python's
lief
library, it possible to get the offset of the last segment. Subtracting the loader size of it, returns the stub loader offset after alignment. Subtracting the alignment return the correct loader offset used for the checksum. Following the official UPX github repository, insrc/p_lx_elf.cpp
line385
to447
is where the alignment is done. Before calculating the total alignment,xct_off
checks for shared android library in the section names of the elf header. If there is one present, this will change the total alignment afterwards. To summarize, UPX aligns the loader size to be a multiple of 4, with additional steps in between. The alignment is the difference between the loader size in the header and the value after the alignment calculation. The checksum size the loader size minus the alignment.[Sources]
https://bbs.kanxue.com/thread-248779.htm
https://github.com/upx/upx/blob/devel/src/stub/src/include/linux.h
https://github.com/upx/upx/blob/devel/src/p_lx_elf.cpp
https://www.programmersought.com/article/58884806733/