Last active
November 26, 2021 15:00
-
-
Save mina86/3880154 to your computer and use it in GitHub Desktop.
Code reading auxiliary vector present in executable binary.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <linux/auxvec.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
static unsigned long *getauxv(void) { | |
char **env = environ; | |
while (*env++) { | |
/* nop */ | |
} | |
return (void*)env; | |
} | |
struct aux_handler { | |
unsigned long key; | |
const char *name; | |
void (*handler)(const struct aux_handler *h, unsigned long *aux); | |
const char *desc, *arg; | |
}; | |
static void handle_print(const struct aux_handler *h, unsigned long *aux) { | |
printf(h->arg, h->name, aux[0], aux[1], h->desc); | |
} | |
static void handle_random(const struct aux_handler *h, unsigned long *aux) { | |
const unsigned char *ch = (void *)aux[1], *end = ch + 16; | |
printf("%-16s %2lu ", h->name, aux[0]); | |
for (; ch != end; ++ch) { | |
printf("%02x", (unsigned)*ch); | |
} | |
printf(" // %s\n", h->desc); | |
} | |
#define AUX_HANDLER(key, handler, desc, arg) { key, #key, handler, desc, arg } | |
#define AUX_NUM(key, desc) { key, #key, handle_print, desc, "%-16s %2lu %-15lu // %s\n" } | |
#define AUX_HEX(key, desc) { key, #key, handle_print, desc, "%-16s %2lu 0x%-13lx // %s\n" } | |
#define AUX_STR(key, desc) { key, #key, handle_print, desc, "%-16s %2lu %-15s // %s\n" } | |
static const struct aux_handler aux_handlers[] = { | |
AUX_HANDLER(AT_IGNORE, NULL, NULL, NULL), | |
AUX_HANDLER(AT_RANDOM, handle_random, "16 random bytes", NULL), | |
AUX_NUM(AT_EXECFD, "file descriptor of program"), | |
AUX_HEX(AT_PHDR, "program headers for program"), | |
AUX_NUM(AT_PHENT, "size of program header entry"), | |
AUX_NUM(AT_PHNUM, "number of program headers"), | |
AUX_NUM(AT_PAGESZ, "system page size"), | |
AUX_HEX(AT_BASE, "base address of interpreter"), | |
AUX_HEX(AT_FLAGS, "flags"), | |
AUX_HEX(AT_ENTRY, "entry point of program"), | |
AUX_NUM(AT_NOTELF, "program is not ELF"), | |
AUX_NUM(AT_UID, "real uid"), | |
AUX_NUM(AT_EUID, "effective uid"), | |
AUX_NUM(AT_GID, "real gid"), | |
AUX_NUM(AT_EGID, "effective gid"), | |
AUX_STR(AT_PLATFORM, "CPU identity for optimizations"), | |
AUX_HEX(AT_HWCAP, "hints at CPU capabilities"), | |
AUX_HEX(AT_HWCAP2, "further hints at CPU capabilities"), | |
AUX_NUM(AT_CLKTCK, "frequency at which times() increments"), | |
AUX_NUM(AT_SECURE, "secure mode boolean"), | |
AUX_STR(AT_BASE_PLATFORM, "string identifying real platform"), | |
AUX_STR(AT_EXECFN, "filename of program"), | |
#ifdef AT_DCACHEBSIZE | |
AUX_NUM(AT_DCACHEBSIZE, "data cache block size"), | |
#endif | |
#ifdef AT_ICACHEBSIZE | |
AUX_NUM(AT_ICACHEBSIZE, "instruction cache block size"), | |
#endif | |
#ifdef AT_UCACHEBSIZE | |
AUX_NUM(AT_UCACHEBSIZE, "unified cache block size"), | |
#endif | |
#ifdef AT_L1D_CACHEGEOMETRY | |
AUX_HEX(AT_L1D_CACHEGEOMETRY, "L1 data cache geometry"), | |
#endif | |
#ifdef AT_L1D_CACHESIZE | |
AUX_NUM(AT_L1D_CACHESIZE, "L1 data cache size"), | |
#endif | |
#ifdef AT_L1I_CACHEGEOMETRY | |
AUX_HEX(AT_L1I_CACHEGEOMETRY, "L1 inst. cache geometry"), | |
#endif | |
#ifdef AT_L1I_CACHESIZE | |
AUX_NUM(AT_L1I_CACHESIZE, "L1 inst. cache size"), | |
#endif | |
#ifdef AT_L2_CACHEGEOMETRY | |
AUX_HEX(AT_L2_CACHEGEOMETRY, "L2 cache geometry"), | |
#endif | |
#ifdef AT_L2_CACHESIZE | |
AUX_NUM(AT_L2_CACHESIZE, "L2 cache size"), | |
#endif | |
#ifdef AT_L3_CACHEGEOMETRY | |
AUX_HEX(AT_L3_CACHEGEOMETRY, "L3 cache geometry"), | |
#endif | |
#ifdef AT_L3_CACHESIZE | |
AUX_NUM(AT_L3_CACHESIZE, "L3 cache size"), | |
#endif | |
#ifdef AT_FPUCW | |
AUX_NUM(AT_FPUCW, "used FPU control word"), | |
#endif | |
#ifdef AT_SYSINFO | |
AUX_HEX(AT_SYSINFO, "entry point to syscall in the vDSO"), | |
#endif | |
#ifdef AT_SYSINFO_EHDR | |
AUX_HEX(AT_SYSINFO_EHDR, "address of vDSO page"), | |
#endif | |
{ AT_NULL, "(unknown)", handle_print, | |
"unknown field", "%-16s %2lu %-15lu\n" } | |
}; | |
#undef AUX_HANDLER | |
#undef AUX_NUM | |
#undef AUX_STR | |
int main() { | |
unsigned long *auxv = getauxv(), key; | |
const struct aux_handler *h; | |
while (*auxv != AT_NULL) { | |
h = aux_handlers; | |
key = *auxv; | |
while (h->key != key && h->key != AT_NULL) { | |
++h; | |
} | |
if (h->handler) { | |
h->handler(h, auxv); | |
} | |
auxv += 2; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment