Tiny ELF Files: Revisited in 2021
nathanotterness.com
nathanotterness.com
Reducing the size of the code allows embedding it in less of the header, giving more options for code layout.
[bits 64]
file_load_va: equ 4096 * 40
db 0x7f, 'E', 'L', 'F'
db 2
db 1
db 1
entry_point:
mov al, 1
mov esi, file_load_va + message
jmp code_chunk_2
dw 2
dw 0x3e
dd 1
dq entry_point + file_load_va
dq program_headers_start
code_chunk_2:
mov edi, eax
mov dl, message_length
syscall
mov al, 60
xor edi, edi
syscall
db 0 ; usable
db 0 ; usable
dw 0x38
dw 1
; We simply deleted the three two-byte fields that used to be here. The only
; one that mattered, the number of section headers, will still be zero due to
; the upper two bytes of the field at the start of the program header being
; zero.
program_headers_start:
; These next two fields also serve as the final six bytes of the ELF header.
dd 1 ; Program header type: must be 1 (loadable segment)
dd 5 ; Program header flags: must be 5 (readable and executable)
dq 0 ; Offset of loadable segment in the file
dq file_load_va ; Address in memory to load the segment into ; could change
message_length: equ 14
message:
db `Hello, w`
; size in file then size in memory; can be anything non-zero and equal
last_bytes: equ `orld!\n`
dq last_bytes
dq last_bytes
dq 0 ; alignment; usable
This compiles and runs, and it's 114 bytes: $ nasm -f bin hello.asm -o hello && chmod a+x hello && ./hello
Hello, world!
$ ls -l hello
-rwxr-xr-x 1 josh josh 114 Oct 13 10:28 hello
Getting the file any smaller would require finding a way to overlap the program header further inside the ELF header. As the article observes, that seems challenging given the validation the kernel does.I'll need to update this article. The only annoying part will be scribbling over the hexdump output again.
For some really beautiful hand-made annotated binary format hexdump, see e.g.: https://github.com/corkami/pics/blob/master/binary/DalvikEXe...
If someone knows of tools fitting more or less what I described above, I'd be super grateful for some recommendations!!
[bits 64]
file_load_va: equ 4096 * 40
db 0x7f, 'E', 'L', 'F'
entry_point:
inc al
mov esi, file_load_va + message
pass2:
xor edi, 1
jmp code_chunk_2
dw 2
dw 0x3e
code_chunk_2:
mov dl, message_length
jmp code_chunk_3
dq entry_point + file_load_va
dq program_headers_start
code_chunk_3:
syscall
mov al, 60
jmp pass2
db 0 ; usable
db 0 ; usable
db 0 ; usable
program_headers_start:
dd 1 ; Program header type: must be 1 (loadable segment)
db 0x5 ; Program header flags: low bits must be 5 (readable and executable); high bytes don't matter
dw 0x38
dw 1
; High 7 bytes of offset of loadable segment
db 0
db 0
db 0
db 0
db 0
db 0
db 0
dq file_load_va ; Address in memory to load the segment into ; could change
message_length: equ 14
message:
db `Hello, w`
; size in file then size in memory; can be anything non-zero and equal
last_bytes: equ `orld!\n`
dq last_bytes
dq last_bytes
dq 0 ; alignment; usableThere are many OS kernels out there that use the ELF format for executable binaries. Many of them are not one particular Linux kernel. The fact that there is a series of articles based on the changing Linux kernel no longer accepting the old "tiny ELF" files is important.
I'll give kudos to the authors of these articles because they're doing something clever and fun. I just feel the urge to clarify to the internet that they're poking at exploits in a Linux kernel loader by offering it non-standard binary files and not creating tiny ELF files at all.
It's entirely on the Linux kernel to not verify these fields. However, its failure to verify these doesn't make the file not an ELF. It just makes it an ELF with a stupid alignment requirement that Linux happens to ignore.
The title:
> A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux
as opposed to ELF Executables for non-Linux systems, or a.out executables for Linux. In the middle of the article, when the illegal shenanigans start:
> Unless, that is, we could change the contents of the structures to make them match even further....
> How many of these fields is Linux actually looking at, anyway? For example, does Linux actually check to see if the e_machine field contains 3 (indicating an Intel 386 target), or is it just assuming that it does?
At the end of the article:
> Of course, half of the values in this file violate some part of the ELF standard, and it's a wonder that Linux will even consent to sneeze on it, much less give it a process ID. This is not the sort of program to which one would normally be willing to confess authorship.
I don't see how the author could possibly be any clearer that this is Linux-specific and will probably not work (unmodified) on non-Linux systems.
Nasm should perform this optimization automatically.
$ cat t.s
bits 64
mov rsi, 0xffffff
$ nasm -o t.o t.s
$ ndisasm -b 64 t.o
00000000 BEFFFFFF00 mov esi,0xffffff
$ nasm --version
NASM version 2.15.05 compiled on Sep 24 2020I ended up at 728 bytes without any self-extracting techniques. It played a nice animation though.
I have not tested it recently, I expect it won't run any more as it used "bad things", like relying on ecx having a specific value when the program started, but the ideas should still be relevant.
I have a couple of responses to specific points brought up in the article.
The author suggests that the original 45-byte executable no longer works on modern systems. If so, this is news to me. Admittedly my current machine is a bit behind the cutting edge (4.15), but what's there should still work. If people are finding the current version to fail for them, I'd appreciate some details on their setup.
* I respectfully disagree that 32-bit executables are "less relevant" today; I suspect they will continue to be supported for many, many years to come. Of course for a new explorer 64-bit executables are far more interesting, but when you're shaving bytes at a time, you can't beat a 32-bit executable.
* Many people are unaware that my original essay is only the first of a series that I wrote. (All of the essays are linked at the bottom of the original.) I note that my smallest 64-bit ELF executable without introducing invalid fields is also 120 bytes, so that's cool.
* However, by taking advantage of unvalidated fields, I was able to produce a working 64-bit ELF executable that is 84 bytes in size. The overlapping is a bit tricky, but I've verified that it continues to work on my box. See http://www.muppetlabs.com/~breadbox/software/tiny/return42.h... -- all variations of my return-42 executables are collected there.
* My smallest 64-bit ELF executable that prints "hello, world\n" (no punctuation: I always use the string from K&R) is 98 bytes. I don't have the assembly for that one posted on my site, but it uses the same layout as the 86-byte executable.
Naturally, the files from that challenge were bigger than the minimal ELF file presented here, because dynamic linking requires more sections.
I suppose I have no choice but to spend a few hours to try and shave off a couple more bytes now.
When I've worked with loading ELFs, in embedded contexts, the loader just used the headers for itself, but they never ended up in the final code being run so a trick like this would just crash. Interesting.
There's a field in the header for the offset of the loadable segment in the file. This binary sets that offset to zero, so the kernel loads the entire file into memory, headers and all.
Edit: That sounds like either an exploit or a murky corner case, interesting.