代码原文地址:

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)中的时间戳的更多相关文章

  1. python读取yaml文件,在unittest中使用

    python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(u ...

  2. Python读取Yaml文件

    近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...

  3. python读取中文文件编码问题

    python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...

  4. Python读取SQLite文件数据

    近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...

  5. 使用python读取yaml文件

    在做APP测试时,通常需要把参数存到一个字典变量中,这时可以将参数写入yaml文件中,再读取出来. 新建yaml文件(android_caps.yaml),文件内容为: platformName: A ...

  6. 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 ...

  7. python读取单个文件操作

    python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...

  8. PHP 与Python 读取大文件的区别

    php读取大文件的方法   <?php function readFile($file) { # 打开文件 $handle = fopen($file, 'rb'); while (feof($ ...

  9. Python读取txt文件

    Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...

随机推荐

  1. Visual Studio For MacOS 踩坑记(二)

    Visual Studio For MacOS安装安卓SDK. 系统默认安装了安卓6.0  API23的SDK.  但是我需要安卓7.0的,API24.  遂安装. SDK可以下载成功,但是用Visu ...

  2. Android开发之漫漫长途 XIX——HTTP

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  3. GMap.NET实现电子围栏功能(WPF版)

    前言 GMap.NET是一个强大.免费.跨平台.开源的.NET控件.分为WPF和winform版.GMap.NET的基本知识不做过多介绍,本文主要介绍如何使用该控件实现电子围栏功能. 电子围栏主要有两 ...

  4. kafka server管理

    kafka启动以来zookeeper  kafka启动之前,首先要启动zookeeper 1.1.kafka启动单个节点 -daemon 表示程序以守护进程的方式后台云心 --override pro ...

  5. Python 【web框架】之Flask

    flask 是Python实现的轻量级web框架.没有表单,orm等,但扩展性很好.很多Python web开发者十分喜欢.本篇介绍flask的简单使用及其扩展. 文中示例源码已经传到github:h ...

  6. 个人觉得非常好用的mysql客户端工具的HeidiSQL

    感觉比Navicat好用,能显示注释,而且还有绿色版,轻量级 下载地址:http://www.heidisql.com/download.php

  7. linux下将指定目录加入环境变量的方法

    每个用户目录下都有一个对应的.bash_profile比如root用户对应/root/.bash_profile,普通用户cqh对应/home/cqh/.bash_profile,以root用户为例v ...

  8. 微信公众平台.net HttpClient 异步客户端

    微信公众平台.net HttpClient 异步客户端 该客户端实现了对微信公众平台的后台管理,包括获取Token,发生客服消息(文本,图片,语音,视频),自定义菜单的创建,查询,删除,查询订阅用户, ...

  9. SpringMvc @ResponseBody字符串中文乱码原因及解决方案

    今天突然发现一个问题,后来在网上也找到了很多解决思路,自己也查找到了问题所在,记录一下. @RequestMapping(value = "/demo1") @ResponseBo ...

  10. Go语言学习笔记(一) [Go语言的HelloWorld]

    日期:2014年7月18日   1.简介     Go 编程语言是一个使得程序员更加有效率的开源项目.Go 是有表达力.简 洁.清晰和有效率的.它的并行机制使其很容易编写多核和网络应用,而新奇的类型系 ...