Before answering how to put an executable in 128 bytes, we need to understand a few things first.
- A dicom file must have the characters
DICM
(File extension) on the bytes 121-124 (Prefix section) to be recognized as a dicom file
- A windows executable file must have the DOS Header in the first 64 bytes of the file to be able to be executable as per the PE(Portable Executable) File Format.
- Combining the above 2 points a new file format is created called PEDICOM which is both a dicom as well as an executable. The PEDICOM has the architecture as shown in the image above.
- The PEDICOM contains both the header and content of the executable file in different sections because an entire executable can’t be fit inside 128 bytes.
- Windows provides a list of structures and Win32 APIs to read/write these PE files section by section in winnt.h header.
Creating a PEDICOM file:
- DOS header (
IMAGE_DOS_HEADER
) has 1 field named e_lfanew
which contains the offset of the actual PE content. This allows to keep an entire executable code in at least 2 memory locations.
- The PE Header (
IMAGE_NT_HEADER
) has the number of sections and the pointes to the sections (Code, Data, Stack etc.)
Now to answer the original question, an entire executable can't be kept in 128 bytes. However 128 bytes of data are sufficient to declare a file as executable i.e. the dos header
and the dos stub
can be kept in the 128 bytes while the rest of the executable can be kept somewhere else, in this case in a private dicom tag and a field in the header can point to this. Make the containing file a valid and legitimate executable.