Extracción de Secciones ELF

1. ¿Qué hace este código?

Este código analiza un archivo ELF y extrae información sobre sus secciones, incluyendo:

  • .text → Contiene el código ejecutable.
  • .data → Contiene datos inicializados.
  • .bss → Contiene datos no inicializados.
  • .rodata → Contiene datos de solo lectura.
  • .plt → Tabla de enlace de procedimientos (para llamadas a funciones externas).

2. Código fuente en C

Archivo de encabezado: elf_sections.h

#ifndef ELF_SECTIONS_H #define ELF_SECTIONS_H #include <stdio.h> #include <stdlib.h> #include <elf.h> void extraer_secciones_elf(const char *archivo); #endif // ELF_SECTIONS_H

Archivo fuente: elf_sections.c

#include "elf_sections.h" #include <fcntl.h> #include <unistd.h> #include <string.h> #include <libelf.h> void extraer_secciones_elf(const char *archivo) { int fd = open(archivo, O_RDONLY); if (fd < 0) { perror("Error al abrir el archivo ELF"); return; } if (elf_version(EV_CURRENT) == EV_NONE) { fprintf(stderr, "Error: versión ELF no soportada.\\n"); close(fd); return; } Elf *elf = elf_begin(fd, ELF_C_READ, NULL); if (!elf) { fprintf(stderr, "Error al leer el archivo ELF.\\n"); close(fd); return; } Elf64_Shdr *shdr; Elf_Scn *scn = NULL; size_t shstrndx; if (elf_getshdrstrndx(elf, &shstrndx) != 0) { fprintf(stderr, "Error al obtener índice de nombres de secciones.\\n"); elf_end(elf); close(fd); return; } printf("\\n--- Secciones ELF encontradas ---\\n"); while ((scn = elf_nextscn(elf, scn)) != NULL) { shdr = elf64_getshdr(scn); const char *nombre_seccion = elf_strptr(elf, shstrndx, shdr->sh_name); if (nombre_seccion) { if (strcmp(nombre_seccion, ".text") == 0 || strcmp(nombre_seccion, ".data") == 0 || strcmp(nombre_seccion, ".bss") == 0 || strcmp(nombre_seccion, ".rodata") == 0 || strcmp(nombre_seccion, ".plt") == 0) { printf("Sección: %s\\n", nombre_seccion); printf("Dirección en memoria: 0x%lx\\n", shdr->sh_addr); printf("Tamaño: %lu bytes\\n", shdr->sh_size); printf("Tipo de sección: %u\\n", shdr->sh_type); printf("-----------------------------------\\n"); } } } elf_end(elf); close(fd); }

Código de ejecución: main.c

#include "elf_sections.h" int main(int argc, char *argv[]) { if (argc < 2) { printf("Uso: %s <archivo ELF>\\n", argv[0]); return 1; } extraer_secciones_elf(argv[1]); return 0; }

3. Pasos para compilar y ejecutar

  1. Instalar libelf si no está instalada: sudo apt-get install libelf-dev
  2. Compilar el programa: gcc -o elf_sections main.c elf_sections.c -lelf
  3. Ejecutar el análisis ELF: ./elf_sections ejecutable.elf

4. Interpretación de las secciones ELF

Sección Propósito
.text Contiene el código ejecutable del programa.
.data Contiene datos inicializados.
.bss Contiene datos no inicializados.
.rodata Contiene datos de solo lectura.
.plt Tabla de enlace de procedimientos (para llamadas a funciones externas).

Este código ahora extrae información detallada de las secciones ELF, mostrando nombres, direcciones, tamaños y tipos.

Comentarios

Entradas populares de este blog

UTF-8 con y sin BOM

Edición y Estructura de Archivos ELF

Análisis de Archivos ELF y Llamadas al Sistema