教你用php读取elf结构

前提知识

  1. UNIX系统的可执行文件都采用ELF格式,类型分为目标文件、可执行文件和共享库
  2. ELF格式探析之三:sections
  3. 本例基于64位的小端序linux机器

以读取目标文件hello.o为例

#include <stdio.h>
void say_hello(char *who) {
    printf("hello, %s!\\n", who);
}
char *my_name = "wb";
int man() {
    say_hello(my_name);
    return 0;
}
// 执行gcc -c hello.c生成hello.o

目标文件elf结构主要有:

  1. ELF header,位于文件的0~64字节,存储文件的描述信息,Section header table的起始位置
  2. N个Section
  3. Section header table,每个条目64字节,对应一个Section的信息
  4. Program header table,可执行文件需要,本例的hello.o没有

进一步分析elf结构

  1. 首先用readelf命令读取elf信息:readelf -h hello.o。总结如下:
  2. ELF header占用64字节
  3. N个Section占用6488-64-1472=4952字节
  4. Section header table占用23*64=1472字节
readelf -h hello.o
ELF Header:
Class:                             ELF64
Data:                              2's complement, little endian
OS/ABI:                            UNIX - System V
Type:                              REL (Relocatable file)
Machine:                           Advanced Micro Devices X86-64
Start of program headers:          0 (bytes into file)
Start of section headers:          5016 (bytes into file) //Section header table的起始位置
Size of this header:               64 (bytes) //ELF header的占用大小
Size of program headers:           0 (bytes)  //hello.o没有program header table
Number of program headers:         0          //hello.o没有program header table
Size of section headers:           64 (bytes) //Section header table每个条目占用大小
Number of section headers:         23         //Section header table条目个数
Section header string table index: 22         //.shstrtab Section位于Section header table第22个条目

php读取.shstrtab Section内容

  1. .shstrtab Section其实是存储的所有Section的名字
<?php
$fp = fopen("hello.o", "rb");

fseek($fp, 40, SEEK_SET);
$sh_off = fread($fp, 8);
$sh_off = unpack("P", $sh_off);
var_dump("section header offset in file: ". $sh_off[1]);

fseek($fp, 10, SEEK_CUR);
$sh_ent_size = fread($fp, 2);
$sh_ent_size = unpack("v", $sh_ent_size);
var_dump("section header entry size: ". $sh_ent_size[1]);

$sh_num = fread($fp, 2);
$sh_num = unpack("v", $sh_num);
var_dump("section header number: ". $sh_num[1]);

$sh_strtab_index = fread($fp, 2);
$sh_strtab_index = unpack("v", $sh_strtab_index);
var_dump("section header string table index: ". $sh_strtab_index[1]);

fseek($fp, $sh_off[1] + $sh_strtab_index[1] * $sh_ent_size[1], SEEK_SET);
fseek($fp, 24, SEEK_CUR); //sh_name(4) + sh_type(4) + sh_flags(8) + sh_addr(8) = 24
$str_table_off = fread($fp, 8);
$str_table_off = unpack("P", $str_table_off);
var_dump("section name string table offset: ". $str_table_off[1]);

$str_table_size = fread($fp, 8);
$str_table_size = unpack("P", $str_table_size);
var_dump("section name string table size: ". $str_table_size[1]);

fseek($fp, $str_table_off[1], SEEK_SET);
$str = fread($fp, $str_table_size[1]);
print_r(explode("\\x00", trim($str, "\\x00")));

// 读取所有Section条目信息
for ($i =0;$i < $sh_num[1]; $i ++) {
    fseek($fp, $sh_off[1] + $i * $sh_ent_size[1], SEEK_SET);
    $sh_name = fread($fp, 4);
    $sh_name = unpack("V", $sh_name);
    fseek($fp, 20, SEEK_CUR); //sh_type(4) + sh_flags(8) + sh_addr(8) = 20
    $sh_offset = fread($fp, 8);
    $sh_offset = unpack("P", $sh_offset);

    $sh_size = fread($fp, 8);
    $sh_size = unpack("P", $sh_size);

    printf("section: %2s name: %-24s offset: %12s size: %12s\\n", $i, get_section_name($sh_name[1]), $sh_offset[1], $sh_size[1]);
}

function get_section_name($start) {
    global $str;
    $name = substr($str, $start);
    return strstr($name, "\\x00", true);
}

关于教你用php读取elf结构的文章就分享到这,如果对你有帮助欢迎继续关注我们哦

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/42099.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
php学习php学习订阅用户
上一篇 2022年6月23日 16:31
下一篇 2022年6月25日 01:24

相关推荐

  • PHP程序员的信号处理

    写给PHP程序员的信号处理教程什么是信号信号就是事件发生时,对进程的一种通知机制(也叫软件中断)。当一个进程收到信号后,内核会暂停该进程正在执行的代码,并跳转到对应的信号处理函数中,如果处理函数不中断,…

    2022年6月27日 PHP自学教程
    0126
  • PHP应用利用Memcache缓存技术提高数据访问速度的方法。

    随着互联网规模的不断扩大,数据的访问和处理速度成为了一个亟待解决的问题。对于PHP应用来说,如何提高数据的访问速度成为了一个重要的课题。而在这个问题上,利用Memcache缓存技术是PHP应用开发者常用的方法之一…

    2023年5月21日
    01
  • PHP jpgraph库的配置及生成多种统计图表

    JpGraph简介JpGraph是开源的PHP统计图表生成库,基于PHP的GD2图形库构建,把生成统计图的相关操作封装,隐藏了部分复杂的操作,使在PHP页面上输出统计图表变得更加容易。JpGraph的官方网站为:http://jpgraph.net,…

    2022年6月14日 PHP自学教程
    0146
  • PHP实现Oracle数据库负载均衡的方法。

    随着互联网应用的快速发展,数据库系统在许多企业应用中扮演着至关重要的角色。而在数据库的实际应用中,负载均衡更是不可或缺的一环。为了能够更加高效地利用数据库资源,提高企业应用的稳定性和可靠性,本文将介…

    2023年5月21日
    01
  • PHP如何利用服务器实现定时任务?

    本篇文章给大家介绍一下PHP如何利用服务器实现定时任务?有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。利用服务器实现简单的定时任务,Windows的计划任务,Linux的cron,适用于每天某一特点时…

    2023年3月29日
    08
  • PHP正则表达式常用例子

    "^[0-9]*[1-9][0-9]*$"   //正整数 "^((-\d+)|(0+))$"   //非正整数(负整数 + 0) "^-[0-9]*[1-9][0-9]*$"   //负整数 "^-?\d+$"     //整数 "^\d+(\.\d+)?$"   //非负浮点数(正浮点数 + 0) "^(([0-…

    2018年3月5日
    0331
  • 如何在PHP-Slim框架中使用CORS跨域请求。

    在Web开发中,跨域请求是一个常见的问题。这是因为浏览器对于不同域名之间的请求有严格的限制。例如,网站A的前端代码无法直接向网站B的API发送请求,除非网站B允许跨域请求。为了解决这个问题,出现了CORS(跨域资…

    2023年6月3日
    02
  • 今日分享php加密函数有哪些。

    PHP 自带的加密函数有:md5()、crypt()。md5() 用来计算 MD5 哈希值,而 crypt() 将字符串用 UNIX 的标准加密 DES 模块加密,这是单向的加密函数,无法解密 。 什么是加密函数? 加密函数是一种将明文转换为密文的…

    2024年7月10日
    00

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息