Teach you how to read the elf structure using php
Recommended: "PHP Video Tutorial"
Prerequisite knowledge
- UNIX system executable The files are all in ELF format, and the types are divided into target files, executable files and shared libraries
- Analysis of ELF format three: sections
- This example is based on a 64-bit little-endian Linux machine
Take reading the target file hello.o as an example
#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
The elf structure of the target file mainly includes:
- ELF header, located 0~64 bytes of the file store the description information of the file, the starting position of the Section header table
- N Section
- Section header table, each entry is 64 bytes, corresponding to a Section Information
- Program header table, executable file needs, hello.o in this example does not
Further analysis of the elf structure
- First use the readelf command to read the elf information: readelf -h hello.o. The summary is as follows:
- ELF header occupies 64 bytes
- N Sections occupy 6488-64-1472=4952 bytes
- Section header table occupies 23*64=1472 words Section
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个条目
Use php to read the .shstrtab Section content
- .shstrtab Section is actually the name of all stored Sections
<?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); }
The above is the detailed content of Teach you how to read the elf structure using php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
