在 ELF 文件中读取 .dynstr (strtab) 时出现段错误
Segfault when reading .dynstr (strtab) in an ELF file
我正在尝试使用 C 语言读取二进制文件中使用的共享库的名称。到目前为止,我在 test.c
中有以下程序:
#include <string.h>
#include <sys/mman.h>
#include <elf.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv) {
const char *ls = NULL;
int fd = -1;
struct stat stat = {0};
if (argc != 2) {
printf("Missing arg\n");
return 0;
}
// open the file in readonly mode
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
goto cleanup;
}
// get the file size
if (fstat(fd, &stat) != 0) {
perror("stat");
goto cleanup;
}
// put the file in memory
ls = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (ls == MAP_FAILED) {
perror("mmap");
goto cleanup;
}
Elf64_Ehdr *eh = (Elf64_Ehdr *)ls;
// looking for the PT_DYNAMIC segment
for (int i = 0; i < eh->e_phnum; i++) {
Elf64_Phdr *ph = (Elf64_Phdr *)((char *)ls + (eh->e_phoff + eh->e_phentsize * i));
const char *strtab = NULL;
if (ph->p_type == PT_DYNAMIC) {
const Elf64_Dyn *dtag_table = (const Elf64_Dyn *)(ls + ph->p_offset);
// looking for the string table
for (int j = 0; 1; j++) {
// the end of the dtag table is marked by DT_NULL
if (dtag_table[j].d_tag == DT_NULL) {
break;
}
if (dtag_table[j].d_tag == DT_STRTAB) {
strtab = (const char *)dtag_table[j].d_un.d_ptr;
printf("string table addr: %p\n", strtab);
}
}
// no string table ? we're stuck, bail out
if (strtab == NULL) {
printf("no strtab, abort\n");
break;
}
// now, i print shared libraries
for (int j = 0; 1; j++) {
// the end of the dtag table is marked by DT_NULL
if (dtag_table[j].d_tag == DT_NULL) {
break;
}
if (dtag_table[j].d_tag == DT_NEEDED) {
printf("too long: %d\n", &strtab[dtag_table[j].d_un.d_val] >= ls + stat.st_size);
printf("string offset in strtab: %lu\n", dtag_table[j].d_un.d_val);
printf("string from strtab: %s\n", &strtab[dtag_table[j].d_un.d_val]);
}
}
// only go through the PT_DYNAMIC segment we found,
// other segments dont matter
break;
}
}
// cleanup memory
cleanup:
if (fd != -1) {
close(fd);
}
if (ls != MAP_FAILED) {
munmap((void *)ls, stat.st_size);
}
return 0;
}
我编译它:
gcc -g -Wall -Wextra test.c
当我 运行 ./a.out a.out
(因此读取它自己的共享库)时,它似乎工作正常,我得到以下输出:
string table addr: 0x4003d8
too long: 0
string offset in strtab: 1
string from strtab: libc.so.6
但是,当我 运行 它针对系统二进制文件时,例如 /bin/ls
,使用 ./a.out /bin/ls
,然后我在第 78 行遇到段错误。
string table addr: 0x401030
too long: 0
string offset in strtab: 1
Segmentation fault (core dumped)
我不知道为什么。用 readelf
读取 /bin/ls
,我使用的地址似乎是正确的,字符串偏移量似乎也是正确的:
$ readelf -a /bin/ls | grep dynstr
...
[ 6] .dynstr STRTAB 0000000000401030 00001030
...
$ readelf -p .dynstr /bin/ls
String dump of section '.dynstr':
[ 1] libselinux.so.1
...
我做错了什么?
What am I doing wrong?
对于非 PIE 二进制文件,这:
strtab = (const char *)dtag_table[j].d_un.d_ptr;
指向 .dynstr
如果该二进制文件在当前进程 运行 中实际上是 运行 的位置 。
如果二进制文件不是 运行,您需要通过$where_mmaped - $load_addr
重定位这个值。 $where_mmaped
是您的 ls
变量。 $load_addr
是二进制文件静态链接加载的地址(通常是第一个 PT_LOAD
段的 p_vaddr
;对于 x86_64 二进制文件,典型值为 0x400000
).
您会注意到,这巧妙地解释了为什么 ./a.out a.out
有效:您从自己的地址 space 读取 .dynstr
,同时使用 a.out
计算出正确的偏移量.
我正在尝试使用 C 语言读取二进制文件中使用的共享库的名称。到目前为止,我在 test.c
中有以下程序:
#include <string.h>
#include <sys/mman.h>
#include <elf.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv) {
const char *ls = NULL;
int fd = -1;
struct stat stat = {0};
if (argc != 2) {
printf("Missing arg\n");
return 0;
}
// open the file in readonly mode
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
goto cleanup;
}
// get the file size
if (fstat(fd, &stat) != 0) {
perror("stat");
goto cleanup;
}
// put the file in memory
ls = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (ls == MAP_FAILED) {
perror("mmap");
goto cleanup;
}
Elf64_Ehdr *eh = (Elf64_Ehdr *)ls;
// looking for the PT_DYNAMIC segment
for (int i = 0; i < eh->e_phnum; i++) {
Elf64_Phdr *ph = (Elf64_Phdr *)((char *)ls + (eh->e_phoff + eh->e_phentsize * i));
const char *strtab = NULL;
if (ph->p_type == PT_DYNAMIC) {
const Elf64_Dyn *dtag_table = (const Elf64_Dyn *)(ls + ph->p_offset);
// looking for the string table
for (int j = 0; 1; j++) {
// the end of the dtag table is marked by DT_NULL
if (dtag_table[j].d_tag == DT_NULL) {
break;
}
if (dtag_table[j].d_tag == DT_STRTAB) {
strtab = (const char *)dtag_table[j].d_un.d_ptr;
printf("string table addr: %p\n", strtab);
}
}
// no string table ? we're stuck, bail out
if (strtab == NULL) {
printf("no strtab, abort\n");
break;
}
// now, i print shared libraries
for (int j = 0; 1; j++) {
// the end of the dtag table is marked by DT_NULL
if (dtag_table[j].d_tag == DT_NULL) {
break;
}
if (dtag_table[j].d_tag == DT_NEEDED) {
printf("too long: %d\n", &strtab[dtag_table[j].d_un.d_val] >= ls + stat.st_size);
printf("string offset in strtab: %lu\n", dtag_table[j].d_un.d_val);
printf("string from strtab: %s\n", &strtab[dtag_table[j].d_un.d_val]);
}
}
// only go through the PT_DYNAMIC segment we found,
// other segments dont matter
break;
}
}
// cleanup memory
cleanup:
if (fd != -1) {
close(fd);
}
if (ls != MAP_FAILED) {
munmap((void *)ls, stat.st_size);
}
return 0;
}
我编译它:
gcc -g -Wall -Wextra test.c
当我 运行 ./a.out a.out
(因此读取它自己的共享库)时,它似乎工作正常,我得到以下输出:
string table addr: 0x4003d8
too long: 0
string offset in strtab: 1
string from strtab: libc.so.6
但是,当我 运行 它针对系统二进制文件时,例如 /bin/ls
,使用 ./a.out /bin/ls
,然后我在第 78 行遇到段错误。
string table addr: 0x401030
too long: 0
string offset in strtab: 1
Segmentation fault (core dumped)
我不知道为什么。用 readelf
读取 /bin/ls
,我使用的地址似乎是正确的,字符串偏移量似乎也是正确的:
$ readelf -a /bin/ls | grep dynstr
...
[ 6] .dynstr STRTAB 0000000000401030 00001030
...
$ readelf -p .dynstr /bin/ls
String dump of section '.dynstr':
[ 1] libselinux.so.1
...
我做错了什么?
What am I doing wrong?
对于非 PIE 二进制文件,这:
strtab = (const char *)dtag_table[j].d_un.d_ptr;
指向 .dynstr
如果该二进制文件在当前进程 运行 中实际上是 运行 的位置 。
如果二进制文件不是 运行,您需要通过$where_mmaped - $load_addr
重定位这个值。 $where_mmaped
是您的 ls
变量。 $load_addr
是二进制文件静态链接加载的地址(通常是第一个 PT_LOAD
段的 p_vaddr
;对于 x86_64 二进制文件,典型值为 0x400000
).
您会注意到,这巧妙地解释了为什么 ./a.out a.out
有效:您从自己的地址 space 读取 .dynstr
,同时使用 a.out
计算出正确的偏移量.