最近在磕CEF,为了方便调试,就想禁用libcef.dll的ASLR.使用CFF Explorer.exe和WinHex都提示文件内存过大,导致不能保存修改后的内容.

网上也简单搜索了一下,发现需要下载一个工具(要熟悉界面操作)。这种操作就是太麻烦了,毕竟很清楚要做的事情就是修改一个二进制而已.也就是修改一个字节的事情。

比如这里提到的使用010editor修改PE文件随机基址,不过我没有测试。后面看到有个PEFile的python模块,我觉得比较轻量级,也方便后续更多自定义需求的拓展,于是使用
接着码上:
执行结果:

至此,目标达成!
注意:
中间发生了一个小插曲,截止今日的pefile版本,在执行的pe.write时出现了错误:
于是找到pefile.py源码,将提示3505行的f_open(filename, "wb+")中的wb+改成rb+即可.接下来有空,我会试着提交这个可能存在的补丁.

总结:
pip install pefile
import pefile
def modify_dll_characteristics(file_path, new_characteristics):
pe = pefile.PE(file_path, fast_load=True)
current_characteristics = pe.OPTIONAL_HEADER.DllCharacteristics
print("pe.OPTIONAL_HEADER.DllCharacteristics:0x{:0>8X}".format(pe.OPTIONAL_HEADER.DllCharacteristics))
pe.OPTIONAL_HEADER.DllCharacteristics = new_characteristics
print("pe.OPTIONAL_HEADER.DllCharacteristics:0x{:0>8X}".format(pe.OPTIONAL_HEADER.DllCharacteristics))
pe.write(filename=file_path)
file_path = "D:/libcef.dll"
new_characteristics = 0x4100
modify_dll_characteristics(file_path, new_characteristics)
import pefile
def modify_dll_characteristics(file_path, new_characteristics):
pe = pefile.PE(file_path, fast_load=True)
current_characteristics = pe.OPTIONAL_HEADER.DllCharacteristics
print("pe.OPTIONAL_HEADER.DllCharacteristics:0x{:0>8X}".format(pe.OPTIONAL_HEADER.DllCharacteristics))
pe.OPTIONAL_HEADER.DllCharacteristics = new_characteristics
print("pe.OPTIONAL_HEADER.DllCharacteristics:0x{:0>8X}".format(pe.OPTIONAL_HEADER.DllCharacteristics))
[培训]传播安全知识、拓宽行业人脉——看雪讲师团队等你加入!