Der Herr Jonathan Salwan, seines Zeichens Herscher über
www.shell-storm.org hat eine recht weitere recht interessante Antidebug-Methode gefunden.
In dem von ihm verfassten Artikel
Linux-process-execution-and-the-useless-ELF-header-fields macht er recht übersichtlich klar, dass manche Teile des ELF-Header bei der Ausführung keine Beachtung finden.
According to this, we can say that the following ElfX_Ehdr's fields are kinda useless: e_shoff, e_shentsize, e_shnum, e_shstrndx.
|
Hierzu hat er auch ein Tool veröffentlicht, welches die entsprechenden Eigenschaften der Sections auf 0 setzt.
/*
** Copyright (C) 2013 - Jonathan Salwan - http://twitter.com/JonathanSalwan
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <elf.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
Elf32_Ehdr *pElf_Header;
Elf32_Phdr *pElf32_Phdr;
Elf32_Shdr *pElf32_Shdr;
char *pMapElf;
struct stat filestat;
static unsigned char *set_header(char *file)
{
int fd;
unsigned char *data;
fd = open(file, O_RDONLY, 0644);
stat(file, &filestat);
printf("[+] Binary size : %d octets\n", (int)filestat.st_size);
data = malloc(filestat.st_size * sizeof(char));
read(fd, data, filestat.st_size);
pMapElf = mmap(0, filestat.st_size, PROT_READ, MAP_SHARED, fd, 0);
pElf_Header = (Elf32_Ehdr *)data;
pElf32_Shdr = (Elf32_Shdr *)((char *)data + pElf_Header->e_shoff);
pElf32_Phdr = (Elf32_Phdr *)((char *)data + pElf_Header->e_phoff);
close(fd);
return (data);
}
int main(int argc, char **argv)
{
unsigned char *data;
unsigned nb_section;
Elf32_Shdr *current;
int i, fd;
if (argc < 2){
printf("Syntax: ./%s <bin>\n", argv[0]);
return -1;
}
data = set_header(argv[1]);
printf("--- Step 1 ---\n");
printf("[+] Clean sections...\n");
nb_section = pElf_Header->e_shnum;
for (i = 0 ; i < nb_section ; i++){
pElf32_Shdr->sh_name = 0;
pElf32_Shdr->sh_type = 0;
pElf32_Shdr->sh_flags = 0;
pElf32_Shdr->sh_addr = 0;
pElf32_Shdr->sh_offset = 0;
pElf32_Shdr->sh_size = 0;
pElf32_Shdr->sh_link = 0;
pElf32_Shdr->sh_info = 0;
pElf32_Shdr->sh_addralign = 0;
pElf32_Shdr->sh_entsize = 0;
pElf32_Shdr++;
}
printf("[+] Clean section [DONE]\n");
printf("--- Step 2 ---\n");
printf("[+] Clean elf header...\n");
pElf_Header->e_shnum = 0;
pElf_Header->e_shstrndx = 0;
pElf_Header->e_shentsize = 0;
pElf_Header->e_version = 0;
pElf_Header->e_ehsize = 0;
pElf_Header->e_shoff = 123;
printf("[+] Clean elf header [DONE]\n");
printf("--- Step 3 ---\n");
printf("[+] Writting binary...\n");
fd = open(argv[1], O_WRONLY, 0644);
write(fd, data, filestat.st_size);
close(fd);
printf("[+] Writting binary [DONE]\n");
free(data);
return 0;
}
Und die Programme sind danach immer noch ausführbar?
Kurze Antwort: Ja!
Beispiel an einer kleinen Hello_World Anwendung:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
./hello_world
Hello World
file hello_world
hello_world: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x76996acaebdeb479499ffdafe8dc7392c907a342, not stripped
readelf -t hello_world
There are 30 section headers, starting at offset 0x113c:
Section Headers:
[Nr] Name
Type Addr Off Size ES Lk Inf Al
Flags
[ 0]
NULL 00000000 000000 000000 00 0 0 0
[00000000]:
[ 1] .interp
PROGBITS 08048154 000154 000013 00 0 0 1
[00000002]: ALLOC
[ 2] .note.ABI-tag
NOTE 08048168 000168 000020 00 0 0 4
[00000002]: ALLOC
[ 3] .note.gnu.build-id
NOTE 08048188 000188 000024 00 0 0 4
[00000002]: ALLOC
[ 4] .gnu.hash
GNU_HASH 080481ac 0001ac 000020 04 5 0 4
[00000002]: ALLOC
...
ELF HEADER:
e_ident: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
e_ident[EI_CLASS]: ELFCLASS32
e_ident[EI_DATA]: ELFDATA2LSB
e_ident[EI_VERSION]: EV_CURRENT
e_ident[EI_OSABI]: ELFOSABI_SYSV
e_ident[EI_ABIVERSION]: 0x0
e_type: ET_EXEC
e_machine: EM_386
e_version: EV_CURRENT
e_entry: 0x08048320
e_phoff: 0x00000034 (52)
e_shoff: 0x0000113c (4412)
e_ehsize: 0x00000034 (52)
e_phentsize: 0x00000020 (32)
e_phnum: 0x00000009 (9)
e_shentsize: 0x00000028 (40)
e_shnum: 0x0000001e (30)
e_shstrndx: 0x0000001b (27)
nach der Bearbeitung mit dem Section-Cleaner-Tool:
./hello_world
Hello World
file hello_world
hello_world_2: ELF 32-bit LSB executable, Intel 80386, invalid version (SYSV), for GNU/Linux 2.6.24, dynamically linked (uses shared libs), corrupted section header size
readelf -t hello_world
There are no sections in this file.
ELF HEADER:
e_ident: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
e_ident[EI_CLASS]: ELFCLASS32
e_ident[EI_DATA]: ELFDATA2LSB
e_ident[EI_VERSION]: EV_CURRENT
e_ident[EI_OSABI]: ELFOSABI_SYSV
e_ident[EI_ABIVERSION]: 0x0
e_type: ET_EXEC
e_machine: EM_386
e_version: EV_NONE
e_entry: 0x08048320
e_phoff: 0x00000034 (52)
e_shoff: 0x00000000 (0)
e_ehsize: 0x00000000 (0)
e_phentsize: 0x00000020 (32)
e_phnum: 0x00000009 (9)
e_shentsize: 0x00000000 (0)
e_shnum: 0x00000000 (0)
e_shstrndx: 0x00000000 (0)
Fazit: ich finde das sehr interessant und ich denke mal, dass eine ELF ohne Sections schon den einen oder anderen Reverser stutzig machen würde...