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 ...
随机推荐
- Java RMI HelloWorld
Java RMI HelloWorld RMI 远程方法调用. 顾名思义就是可以像调用本地程序方法一样调用远程(其他JVM)的程序方法. 分为3个部分: Stub:中介,代理. 封装了远程对象 ...
- python selenium 对浏览器标签页进行关闭和切换
1.关闭浏览器全部标签页 driver.quit() 2.关闭当前标签页(从标签页A打开新的标签页B,关闭标签页A) driver.close() 3.关闭当前标签页(从标签页A打开新的标签页B,关闭 ...
- dart之旅(三)- list
list, 在 js 中被称为数组, 但是和 js 中的数组还是有不少不同的地方,我们来看一个例子: // 声明一个长度不可变的 list List<int> fixedLengthLis ...
- 树莓派+tomcat+mysql安装及配置
0x00 系统:ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi 该版本中apt源在国内访问速度不算慢,可以不换,但软件包不完整,建议添加阿里云源 deb ...
- 从零开始学 Web 之 移动Web(五)touch事件的缺陷,移动端常用插件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 移动端head头部常用meta标签
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum- ...
- 手撕coreML之yolov2 object detection物体检测(含源代码)
一些闲话: 前面我有篇博客 https://www.cnblogs.com/riddick/p/10434339.html ,大致说了下如何将pytorch训练的.pth模型转换为mlmodel,部署 ...
- Python和Java编程题(二)
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 兔子的规律为数列1,1,2,3,5,8,13,21 ...
- InnoDB存储引擎--Innodb Buffer Pool(缓存池)
InnoDB存储引擎--Innodb Buffer Pool(缓存池) Innodb Buffer Pool的概念 InnoDB的Buffer Pool主要用于缓存用户表和索引数据的数据页面.它是一块 ...
- Visual Studio Ultimate 2013
简体中文版 SHA-1: 07313542D36ED8BEEF18520AA4F15E33E32C7F77 http://download.microsoft.com/download/0/7/5/0 ...