commit 1b1e14b85660fc5b072fe36fd0c15b76f74d445f Author: xamidev Date: Wed Sep 3 15:23:52 2025 +0200 ELF header read diff --git a/.gdb_history b/.gdb_history new file mode 100644 index 0000000..f19f752 --- /dev/null +++ b/.gdb_history @@ -0,0 +1,3 @@ +file helpelf +r +q diff --git a/.helper.c.swp b/.helper.c.swp new file mode 100644 index 0000000..b7b1969 Binary files /dev/null and b/.helper.c.swp differ diff --git a/helpelf b/helpelf new file mode 100755 index 0000000..bd1d713 Binary files /dev/null and b/helpelf differ diff --git a/helper.c b/helper.c new file mode 100644 index 0000000..f26f951 --- /dev/null +++ b/helper.c @@ -0,0 +1,90 @@ +// goal: +// replace "file" + "checksec" + +#include +#include +#include +#include +#include + +#define MAX_FILENAME 256 +#define EI_NIDENT 16 +#define MAX_HEADER_LEN 2048 + +#define Elf32_Half uint16_t +#define Elf32_Word uint32_t +#define Elf32_Addr uint32_t +#define Elf32_Off uint32_t + +struct Elf32_Ehdr +{ + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; + Elf32_Half e_phentsize; + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; +}; + +int parse_elf_header32(struct Elf32_Ehdr* header, FILE* fp) +{ + fread(header, sizeof(struct Elf32_Ehdr), 1, fp); + return EXIT_SUCCESS; +} + +int main(int argc, char* argv[]) +{ + // Accessing file arg + if (argc < 2) + { + printf("Usage: %s \n", argv[0]); + return -EINVAL; + } + + char* filename = argv[1]; + + // 0. is ELF32 or ELF64 ??? + // 1. create ELF header struct + // 2. populate ELF header struct + // 3. display ELF header struct + + if (!filename) + { + printf("Error parsing filename.\n"); + return -EINVAL; + } + + FILE* elf_file = fopen(filename, "rb"); + + if (!elf_file) + { + printf("Couldn't open file '%s'\n", filename); + return -ENOENT; + } + + struct Elf32_Ehdr elf_header = {0}; + int res = parse_elf_header32(&elf_header, elf_file); + if (res) + { + printf("Error parsing ELF header.\n"); + return -EINVAL; + } + + if (!(memcmp(elf_header.e_ident, "\177ELF", 4) == 0)) + { + printf("Not an ELF file..\n"); + return -EINVAL; + } + + printf("ELF file\n"); + + return EXIT_SUCCESS; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..700483f --- /dev/null +++ b/makefile @@ -0,0 +1,8 @@ +all: + gcc -Wall -Wextra helper.c -g -o helpelf + +install: + sudo cp helpelf /usr/bin/helpelf + +clean: + rm helpelf