首页 > 解决方案 > 检查是否在python中启用了PIE

问题描述

从昨天开始,我在这里看看如何检测保护“PIE”是否被激活。为此,我分析了重定位条目的输出以查看_ITM_deregisterTMClone是否存在。有没有更好的方法来检测 PIE 而无需通过 readelf 输出?

这是我目前拥有的:

def display_pie(counter):
    if (counter == 1):
            print("Pie : Enable")
    else:
            print("Pie: No PIE")

def check_file_pie(data_file):
    data = []
    data2 = []
    result = []
    ctn = 0
    check = subprocess.Popen(["readelf", "-r", data_file],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    result = check.stdout.readlines()
    for x in result:
        data.append(list(x))
    for lines in data:
            data2.append("".join(map(chr, lines)))
    for new_lines in data2:
            if "_ITM_deregisterTMClone" in new_lines:
                    ctn += 1
    display_pie(ctn)

谢谢,这是非常技术性的,所以如果有人可以向我解释检查可执行独立职位的更好方法,我很感兴趣!

标签: pythonfileelf

解决方案


您可以使用pwntools,它具有操作 ELF 文件的功能。示例用法:

>>> from pwn import *
>>> e = ELF('your-elf-file')
>>> e.pie
True

如果你想知道它是如何实现的,你可以在这里找到源代码


推荐阅读