Python读取PE文件(exe/dll)中的时间戳
代码原文地址:
https://www.snip2code.com/Snippet/144008/Read-the-PE-Timestamp-from-a-Windows-Exe
https://gist.github.com/03152ba1a148d9475e81
直接上代码吧,引用过来以防链接失效:
#! /usr/bin/env python2.7
#
# Author: Pat Litke (C) 2014
#
# This code is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Baku. If not, see <http://www.gnu.org/licenses/>.
#
# Description:
# Quick and dirty module to read a binary file, look at the DOS header for the PE offset
# Seek to the PE offset, read the third DWORD in, unpack it, and return either EPOCH or GMTIMEs
#
# Returns 1 if the file doesn't havea a DOS header
# Returns 2 if file couldn't be read
# Returns the data in epoch or formatted otherwise from struct import unpack
from binascii import hexlify
from time import gmtime, strftime def getEpoch(filePath, epoch = True): # Open the file in Binary mode
try:
handle = open(filePath, 'rb')
if hexlify(handle.read(2)) != hexlify(u'MZ'):
handle.close()
return 1
except:
return 2 # Get PE offset (@60, DWORD) from DOS header
# It's little-endian so we have to flip it
# We also need the HEX representation which is an INT value
handle.seek(60, 0)
offset = handle.read(4)
offset = hexlify(offset[::-1])
offset = int(offset, 16) # Seek to PE header and read second DWORD
handle.seek(offset+8, 0)
dword = handle.read(4)
handle.close()
t = unpack(">L", dword[::-1])[0] if epoch:
return t
else:
return strftime('%Y-%m-%d %H:%M:%S', gmtime(float(t))) def getUTC(filePath):
return getEpoch(filepath, False) def getBoth(filePath):
return [getEpoch(filepath), getEpoch(filepath, False)]
如果想修改这个时间戳,按照上述规则,pack后写入即可,另外其中53行那句有些麻烦,其实这么写就可以了:t = unpack("<L", dword)[0] ,即按照小端直接解码即可。
更新:
更强大的修改PE文件时间戳的方式如下,而且支持PE文件与pdb一切修改,还可以顺道修改guid:
https://github.com/google/syzygy/tree/master/syzygy/zap_timestamp
这是谷歌syzygy工具链中的一个小公举(工具 O(∩_∩)O~)。
syzygy也是个好东西,可以改善软件的启动速度,体积较大的软件,本身启动已经没有什么优化空间时,可以考虑使用syzygy来提高加载速度
Python读取PE文件(exe/dll)中的时间戳的更多相关文章
- python读取yaml文件,在unittest中使用
python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(u ...
- Python读取Yaml文件
近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...
- python读取中文文件编码问题
python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...
- Python读取SQLite文件数据
近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...
- 使用python读取yaml文件
在做APP测试时,通常需要把参数存到一个字典变量中,这时可以将参数写入yaml文件中,再读取出来. 新建yaml文件(android_caps.yaml),文件内容为: platformName: A ...
- Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal multibyte sequence
Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal mul ...
- python读取单个文件操作
python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...
- PHP 与Python 读取大文件的区别
php读取大文件的方法 <?php function readFile($file) { # 打开文件 $handle = fopen($file, 'rb'); while (feof($ ...
- Python读取txt文件
Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...
随机推荐
- xlwt set style making error: More than 4094 XFs (styles)
使用Xlwt,当内容过多时,会报错:More than 4094 XFs (styles) 解决方法: wb = xlwt.Workbook(style_compression=2) 使用style_ ...
- go http.Handler
http1 package main import ( "log" "net/http" "fmt" ) func main() { db: ...
- 关于requests的session方法保持cookie的问题。
最近在做爬虫的时候遇到了一个问题,在用requests的session方法保持cookie的时候发现requests不能保持手动构建的cookie.起初以为requests只能自动保持由服务器返回的s ...
- koa2搭建服务器
首先初始化项目 npm init -y 安装koa2 npm install koa --save 项目根目录 新建 index.js //这是最基本的服务 const Koa = require(' ...
- linux下xdebug的安装和配置方法
xdebug简介 Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. xdebug安装 首先让php错误显示,只需要修改php.in ...
- vue-cli 跳转方式
一:router-link标签跳转 <router-link to='/citylist'><div class="header-right">城市< ...
- Lucene实战之关键字匹配多个字段
前言 当我们输入关键字时希望可以支持筛选多个字段,这样搜索内容的覆盖率就会大一些. 匹配多个字段主要用 MultiFieldQueryParser类. 单一字段搜索 QueryParser parse ...
- [design-patterns]设计模式之一策略模式
设计模式 从今天开始开启设计模式专栏,我会系统的分析和总结每一个设计模式以及应用场景.那么首先,什么是设计模式呢,作为一个软件开发人员,程序人人都会写,但是写出一款逻辑清晰,扩展性强,可维护的程序就不 ...
- vue 前台传后台
var the = this:let url = "/api/Purchase_Enter/CancelEnter"; let params = { Enter_Id: Enter ...
- [android] 手机卫士读取联系人
获取ContentResolver内容解析器对象,通过getContentResolver()方法 调用ContentResolver对象的query()方法,得到raw_contacts表里面的数据 ...