Re: a.out Magic Number
twiz <[email protected]> Fri, 29 Oct 2004 11:03:07 +0200 (CEST)
| Newsgroups | gmane.comp.security.italian.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============1999798045==
Content-Type: TEXT/PLAIN; charset=US-ASCII
On Tue, 26 Oct 2004, Lonely Wolf wrote:
> Salve a tutti.
>
> Mi trovo alle prese con un file a.out generato su una piattaforma che
> momentaneamente ignoro...voglio dire non conosco la piattaforma sul
> quale e'stato compilato.
> Credo si tratti di un Bsd, ma potrei sbagliarmi :)
> Su "Linkers and Loaders" leggo:
>
> "The magic number a_magic indicates what kind of executable file this is.
> on the original PDP-11 was octal 407, which was a branch instruction that would
> jump over the next seven words of the header to the beginning of the text
> segment.... Different magic numbers tell the operating system program
> loader to load the file in to memory differently..."
Ho guardato un po' /usr/src/linux/fs/binfmt_aout.c e
/usr/src/linux/include/[linux|asm-i386]/a.out.h
L'header che trovi all'inizio dell' a.out e' mappato da questa struct :
struct exec
{
unsigned long a_info; /* Use macros N_MAGIC, etc for access */
unsigned a_text; /* length of text, in bytes */
unsigned a_data; /* length of data, in bytes */
unsigned a_bss; /* length of uninitialized data area for
file, in bytes */
unsigned a_syms; /* length of symbol table data in file, in
bytes */
unsigned a_entry; /* start address */
unsigned a_trsize; /* length of relocation info for text, in
bytes */
unsigned a_drsize; /* length of relocation info for data, in
bytes */
};
L'header fa poco piu' che dare la size (in bytes) delle varie sezioni,
l'entry point e, all'inizio dell'header, alcune informazioni (tra cui i
magic number di cui chiedevi) :
#if !defined (N_MAGIC)
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
#endif
#define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) &
0xff))
#define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
Da a_info puoi dunque ricavare MAGIC, MACHTYPE e FLAGS.
I MAGIC numbers sono definiti poco sotto :
/* Code indicating object file or impure executable. */
#define OMAGIC 0407
/* Code indicating pure executable. */
#define NMAGIC 0410
/* Code indicating demand-paged executable. */
#define ZMAGIC 0413
/* This indicates a demand-paged executable with the header in the text.
The first page is unmapped to help trap NULL pointer references */
#define QMAGIC 0314
/* Code indicating core file. */
#define CMAGIC 0421
Sono abbastanza "self-explanatory", guardando load_aout_binary, dopo il
check di validita' :
ex = *((struct exec *) bprm->buf); /* exec-header */
if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
N_TRSIZE(ex) || N_DRSIZE(ex) ||
bprm->file->f_dentry->d_inode->i_size <
ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
return -ENOEXEC;
}
Se MAGIC e' != da OMAGIC
if (N_MAGIC(ex) == OMAGIC) {
[...]
} else {
[...]
error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
PROT_READ | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
MAP_EXECUTABLE,
fd_offset);
[...]
error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
MAP_EXECUTABLE,
fd_offset + ex.a_text);
up_write(¤t->mm->mmap_sem);
[...]
.text e .data hanno mapping diverso, con .text mappata su pagine
read-only (e, logicamente, sono mappate su pagine diverse, quindi ci sara'
presumibilmente una sorta di padding al fondo, spazio potenzialmente
utilizzabile per inserire codice "comodamente", se e' questo il fine
ultimo :) )
Per quanto riguarda invece FLAGS, non ho trovato traccia di #define negli
header, tuttavia il check su FLAGS avviene solo in load_aout_library :
if (N_FLAGS(ex))
goto out;
A buttarla li' penso che le flags semplicemente contraddistinguano una
libreria dall'eseguibile. La libreria e' mappata senza "distinzioni" :
error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
N_TXTOFF(ex));
Per quanto riguarda N_MACHTYPE, beh sai gia' che l'arch e' Intel, quindi
non penso possa darti informazioni vitali (per la lista, controlla
l'enum machine_type in linux/a.out.h ) :)
Questo e' quanto a un primo sguardo, soprattutto visto che nelle prime due
pagine della mia ricerca su google non ho trovato una specifica o qualcosa
di simile :/
Non so quanto questo ti possa essere utile e prendilo comunque con
beneficio di inventario :)
Ciaps
twiz
--===============1999798045==
Content-Type: text/plain; charset="iso-8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
________________________________________________________
http://www.sikurezza.org - Italian Security Mailing List
--===============1999798045==--