iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python解析elf文件
  • 274
分享到

python解析elf文件

文件pythonelf 2023-01-31 01:01:15 274人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

#!/usr/bin/python import struct import sys elfhdr = {} def verify_elf(filename): f = open(filename,'rb') elfident =

#!/usr/bin/python
import struct 
import sys
elfhdr = {}
def verify_elf(filename):
	f = open(filename,'rb')
	elfident = f.read(16)
	magic = [ord(i) for i in elfident]
	if( magic[0] != 127 or magic[1]!= ord('E') or magic[2] != ord('L') or magic[3] != ord('F')):
		print "your input file %s not a elf file" %filename
		return
	else:
		temp = f.read(struct.calcsize('2HI3QI6H'))
		temp = struct.unpack('2HI3QI6H',temp)
		global elfhdr
		elfhdr['magic'] = magic
		elfhdr['e_type']= temp[0]
		elfhdr['e_Machine'] = temp[1]
 		elfhdr['e_version'] = temp[2]
		elfhdr['e_entry'] = temp[3]
		elfhdr['e_phoff'] = temp[4]
		elfhdr['e_shoff'] = temp[5]
		elfhdr['e_flags'] = temp[6]
		elfhdr['e_ehsize'] = temp[7]
		elfhdr['e_phentsize'] = temp[8]
		elfhdr['e_phnum'] = temp[9]
		elfhdr['e_shentsize'] = temp[10]
		elfhdr['e_shnum'] = temp[11]
		elfhdr['e_shstrndx'] = temp[12]
	f.close()
def display_elfhdr(elffile):
	global elfhdr
	print "ELF Header"
	magic = elfhdr['magic']
	print "  Magic:  %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" %(magic[0] ,magic[1],magic[2],magic[3],magic[4],magic[5],magic[6],magic[7],magic[8],magic[9],magic[10],magic[11],magic[12],magic[13],magic[14],magic[15])
	if magic[4] == 1 :
		print "  Class:                           ELF32"
	else:
		print "  Class:                           ELF64"
        if magic[5] == 1:
		print "  Data:                            2's complement,little endian"
	else:
		print "Data:                              2's complement,bigendian"
	print "  Version:                         %d(current)" %magic[6]
	if magic[7] == 0:
		os_abi = 'System V ABI'
	elif magic[7]== 1:
		os_abi = 'HP-Ux operating system'
	elif magic[7] == 255:
		os_abi = 'Standalone (embedded) application'
	print "  OS/ABI:                          %s" %os_abi
	print "  ABI Version:                     %d" %magic[8]
	if elfhdr['e_type'] == 0:
		type = 'No file type'
	elif elfhdr['e_type'] == 1:
		type = 'Relocatable object file'
	elif elfhdr['e_type'] == 2:
		type = 'Executable file'
	elif elfhdr['e_type'] == 3:
		type = 'Core file'
	print "  Type:                            %s" %type
	print "  Machine:                         %d" %elfhdr['e_machine']
	print "  Version:                         0x%x" %elfhdr['e_version']
	print "  Entry point address:             0x%x" %elfhdr['e_entry']
	print "  Start of program headers:        %d (bytes into file)" %elfhdr['e_phoff']
	print "  Start of section headers:        %d (bytes into file)" %elfhdr['e_shoff']
	print "  Flags:                           0x%x" %elfhdr['e_flags']
	print "  Size of this header:             %d (bytes)" %elfhdr['e_ehsize']
	print "  Size of program headers:         %d (bytes)" %elfhdr['e_phentsize']
	print "  Number of program headers:       %d " %elfhdr['e_phnum']
	print "  Size of section headers:         %d (bytes)" %elfhdr['e_shentsize']
	print "  Number of section headers:       %d" %elfhdr['e_shnum']
	print "  Section header string table index: %d"%elfhdr['e_shstrndx']
def display_sections(elffile):
    verify_elf(elffile)
    sections = []
    global elfhdr
    sec_start = elfhdr['e_shoff']
    sec_size = elfhdr['e_shentsize']
    f = open(elffile,'rb')
    f.seek(sec_start)
    for i in range(0,elfhdr['e_shnum']):
        temp = f.read(sec_size)
        temp = struct.unpack('2I4Q2I2Q',temp)
        sec = {}
        sec['sh_name'] = temp[0]
        sec['sh_type'] = temp[1]
        sec['sh_flags'] = temp[2]
        sec['sh_addr'] = temp[3]
        sec['sh_offset'] = temp[4]
        sec['sh_size'] = temp[5]
        sec['sh_link'] = temp[6]
        sec['sh_info'] = temp[7]
        sec['sh_addralign'] = temp[8]
        sec['sh_entsize'] = temp[9]
        sections.append(sec)
    print "There are %d section headers,starting at offset 0x%x:\n" %(elfhdr['e_shnum'],sec_start)
    print "Section Headers:"
    print "  [Nr] Name               Type            Address          Offset"
    print "       Size               Entsize         Flags  Link  Info Align"
    start = sections[elfhdr['e_shstrndx']]['sh_offset']
        for i in range(0,elfhdr['e_shnum']):
        offset = start + sections[i]['sh_name']
        name = get_name(f,offset)
        type2str = ['NULL','PROGBITS','SYMTAB','STRTAB','RELA','HASH','DYNAMIC','NOTE','NOBITS','REL','SHLIB','DYNSYM']
        flags = sections[i]['sh_flags']
        if (flags == 1):
            flagsstr = 'W'
        elif (flags == 2):
            flagsstr = 'A'
        elif (flags == 4):
            flagsstr = 'X'
        elif (flags == 3):
            flagsstr = 'W' + 'A'
        elif (flags == 6):
            flagsstr = 'A' +  'X'
        elif (flags == 0x0f000000 or flags == 0xf0000000):
            flagsstr = 'MS'
        else:
            flagsstr = ''
        print "  [%d]  %s              %s             %x             %x" %(i,name,type2str[sections[i]['sh_type'] & 0x7],sections[i]['sh_addr'],sections[i]['sh_addralign'])
        print "      %x                   %x       %s        %d     %d     %x" %(sections[i]['sh_size'],sections[i]['sh_entsize'],flagsstr,sections[i]['sh_link'],sections[i]['sh_info'],sections[i]['sh_addralign'])
    f.close()
def get_name(f,offset):
    name = ''
    f.seek(offset)
    while 1:
        c = f.read(1)
        if c == '\0':
            break
        else:
            name += c
    return name
if __name__ == '__main__':
file = sys.argv[1]
verify_elf(file)
display_elfhdr(file)
display_sections(file)
未完待续

--结束END--

本文标题: python解析elf文件

本文链接: https://www.lsjlt.com/news/184302.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python解析elf文件
    #!/usr/bin/python import struct import sys elfhdr = {} def verify_elf(filename): f = open(filename,'rb') elfident = ...
    99+
    2023-01-31
    文件 python elf
  • linux中ELF文件的示例分析
    这篇文章给大家分享的是有关linux中ELF文件的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。示例程序我们的示例程序如下:#include<stdio.h> int ma...
    99+
    2023-06-16
  • python 解析 eml文件
    #-*- encoding: gb2312 -*- import email fp = open('xxxx.eml', "r") msg = em...
    99+
    2023-01-31
    文件 python eml
  • python解析torrent文件库:p
    pytorrent是一个非常小巧的用来解析解析torrent文件python库。核心代码不足150行,却能够完备地解析torrent文件,并支持导出修改后的torrent文件。 使用演示: import pytorrent t=pyto...
    99+
    2023-01-31
    文件 python torrent
  • Python解析pcap文件示例
    引言 近期做一些基于TCP协议的项目,跟其他接口方调试时经常出现不一致的问题,而程序日志又不能完成保证公正,就只能通过tcpdump抓包的方式来排查问题了。由于是自定义的协议,用wi...
    99+
    2024-04-02
  • python怎么解析json文件
    使用python解析json文件的方法:1.新建python项目;2.导入json模块;3.使用open()函数打开json文件,并创建文件对象;4.使用json.loads()方法解析json文件;具体步骤如下:首先,打开python,并...
    99+
    2024-04-02
  • python怎么解析pcap文件
    使用python解析pcap文件的方法:1.新建python项目;2.导入scapy模块;3.使用rdpcap()函数打开pcap文件;4.使用repr()方法解析文件;具体步骤如下:首先,打开python,并新建一个python项目;py...
    99+
    2024-04-02
  • python怎么解析xml文件
    在Python中可以使用ElementTree模块来解析XML文件。以下是一个简单的示例: import xml.etree.Ele...
    99+
    2024-04-02
  • [转载] python 解析xml 文件
    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml。内容是在python官网上看到的。 <xml version="1.0"> <data> <co...
    99+
    2023-01-30
    文件 python xml
  • python 数据分析之 HTML文件解析
    python 数据分析之 HTML文件解析 一 :Html1. Html 理解2. Html 介绍3. Html 构成4. HTML结构 介绍1> HTML文件结构A: 文档类型声明B: 根标...
    99+
    2023-09-02
    html python 数据分析
  • Python解析CDD文件的代码详解
    目录前言基本介绍前言 在实际诊断测试开发中,我们写测试脚本会用到CDD文件中的诊断,常规做法可能是用到哪个就定义哪个,这样做的弊端是有可能造成重复定义,或者整个工程中有不同的变量名,...
    99+
    2024-04-02
  • readelf命令读取elf文件的详细信息(推荐)
    目录readelf命令概述常用参数-a 全部-h 文件头-l 程序头-S section头-e 全部头-s 符号表-n 内核注释-r 重定位-d 动态段-V 版本-A CPU架构-x 16进制展示段readelf命令 概...
    99+
    2023-02-13
    readelf命令读取elf文件 readelf命令读取
  • python中解析和生成pdf文件
    python中可以对pdf文件进行解析和生成,分别需要安装pdfminer/pdfminer3k和reportlab文件库。 一、pdf文件的解析 pdfminer安装文件路径,分别使用于python2.0/3.0版本: https:...
    99+
    2023-01-31
    文件 python pdf
  • Python中怎么解析配置文件
    这篇文章将为大家详细讲解有关Python中怎么解析配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在...
    99+
    2023-06-17
  • 如何在python中解析json文件
    本篇文章为大家展示了如何在python中解析json文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。python可以做什么Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能...
    99+
    2023-06-14
  • 如何解析Python 源文件性质
    本篇文章给大家分享的是有关如何解析Python 源文件性质,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。对于Python 源文件的学习,个人认为先要从Python的概念学起,其...
    99+
    2023-06-17
  • Python通过psd-tools解析PSD文件
    目录特点安装使用1. 命令行2. 操作PSD文件3. 操作使用层4. 将数据导出到 PIL4. 将数据导出到NumPy更多操作1. 操作一个PSD文件2. 操作一个PSD图层前言: ...
    99+
    2024-04-02
  • 【DBC文件解析】
    目录结构如下 1、基础介绍 2、DBC文件 2.0、先来一篇全貌 2.1、开头是Version 和 new_symbols两个Tag。 2.2、波特率定义 2.3、网络节点的定义 2.4、报文帧的定义...
    99+
    2023-09-12
    网络 服务器 网络协议
  • python编译pyc文件的过程解析
    什么是pyc文件 pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,...
    99+
    2024-04-02
  • 怎么用Python解析toml配置文件
    举个例子有了 ini 和 yaml,相信 toml 学习来也很简单,先直接看一个例子吧。import toml config = """ title = "toml 小栗子" [owne...
    99+
    2023-05-21
    Python toml
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作