Like this code?
Show the author your appreciation.
The file offset is stored using a class for their manageability, it stores the current offset and reads the specified bytes without making the user fight against that.
More about ELF : http://en.wikipedia.org/wiki/Executable_and_Linkable_Format
Example Usage
<?php
/* Some files...: */
$binaries = array( "/bin/bash" , "/sbin/ifconfig" );
/* Dumps the ELF file information: */
foreach ( $binaries as $binary ) {
if (! is_file ( $binary ))
continue;
echo "Dumping information about: $binary \n" ;
print_r ( readHeader ( $binary ));
echo "\n" ;
}
function readHeader ( $file )
{
/* Our file management class: */
require_once "filereader.class.php" ;
$fr = new FileReader ( $file , "rb" );
/* Gets the first 16 bytes (E_IDENT): */
$bytes = $fr -> readBytes ( 16 );
$magic = unpack ( "C16" , $bytes );
/* Cheks if the file is a valid ELF file: */
if ( $magic [ 1 ] != 0x7f
|| $magic [ 2 ] != ord ( 'E' )
|| $magic [ 3 ] != ord ( 'L' )
|| $magic [ 4 ] != ord ( 'F' )) {
die( "Specified file is not an ELF binary" );
}
/* And now reads the rest of information (~52 bytes): */
$elf_information = array(
"ei_class" => $magic [ 5 ],
"ei_data" => $magic [ 6 ],
"ei_version" => $magic [ 7 ],
"ei_osabi" => $magic [ 8 ],
"ei_abiversion" => $magic [ 9 ],
"ei_pad" => $magic [ 10 ],
"ei_nident" => $magic [ 16 ],
"e_type" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_machine" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_version" => getElementZero ( unpack ( "I*" , $fr -> readBytes ( 4 ))),
"e_entry" => getElementZero ( unpack ( "I*" , $fr -> readBytes ( 4 ))),
"e_phoff" => getElementZero ( unpack ( "I*" , $fr -> readBytes ( 4 ))),
"e_shoff" => getElementZero ( unpack ( "I*" , $fr -> readBytes ( 4 ))),
"e_flags" => getElementZero ( unpack ( "I*" , $fr -> readBytes ( 4 ))),
"e_ehsize" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_phentsize" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_phnum" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_shetsize" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_shnum" => getElementZero ( unpack ( "S*" , $fr -> readBytes ( 2 ))),
"e_shtrndx" => getElementZero ( unpack ( "S*" , $fr -> ReadBytes ( 2 )))
);
return $elf_information ;
}
function getElementZero ( $where )
{
return $where [ 1 ];
}
?>
filereader.class.php
<?php
class FileReader
{
private $_fh = null ;
private $_offset = 0 ;
public function __construct ( $file , $mode )
{
if ( is_readable ( $file ))
$this -> _fh = fopen ( $file , $mode );
}
public function __destruct ()
{
if ( $this -> _fh )
fclose ( $this -> _fh );
}
public function getOffset ()
{
return $this -> _offset ;
}
public function readBytes ( $bytes )
{
if (! $this -> _fh )
return null ;
$read = fread ( $this -> _fh , $bytes );
$this -> _offset += $bytes ;
fseek ( $this -> _fh , $this -> _offset );
return $read ;
}
}
?>
Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP , FTP , Filesystem , PHP Classes , Compression DbObject - A PHP wrapper for working with various databases Categories : Databases , PHP , PHP Classes Authorize.net AIM Interface Class v1.0.0 Categories : PHP , PHP Classes , Ecommerce , Payment Gateways Browser Detecor Class Categories : PHP Classes , PHP , HTML filesplit : Split big text files in multiple small ones Categories : PHP , Log Files , Filesystem , PHP Classes A File Browser Class.To Read Drives,Directories and Files .Files writing is also possible Categories : PHP , PHP Classes , Filesystem Specify your connection settings and create a link to a MySQL database. Categories : PHP , PHP Classes , Databases , MySQL , Beginner Guides Filter - A simple class that lets you use multiple functions to create custom filters. Categories : PHP , PHP Classes , Strings The GTV.class allows you to extract a value between any HTML tag or between any TEXT on a web page. Categories : PHP , PHP Classes , Regexps Customizable Calendar Class Categories : HTML and PHP , Date Time , PHP , PHP Classes , Calendar HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout. Categories : Graphics , Arrays , PHP , PHP Classes , Charts and Graphs Simple and fast user authentication Categories : PHP , PHP Classes , Authentication Client classes for Dictionary servers UPDATED: 2000-06-06 Categories : Network , Search , Complete Programs , PHP Classes , PHP file class , uploade file , download file already uploaded on another website Categories : PHP , PHP Classes , Filesystem , Web Services Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP , Filesystem , PHP Classes