抽取EXE,DLL文件的版权信息的小程序,来珠海后的第一个工作程序。

已查看 5 次
跳至第一个未读帖子

Albert Lee

未读,
2007年7月17日 04:19:182007/7/17
收件人 Idea-T...@googlegroups.com
有用的拿去,版权保留。

#coding=utf-8
from ctypes import *
import md5
import sha

_MAX_PATH = 260

WORD = c_ushort
UINT = c_uint
BYTE = c_ubyte

blocks = [u"Comments",
u"InternalName",
u"ProductName",
u"CompanyName",
u"LegalCopyright",
u"ProductVersion",
u"FileDescription",
u"LegalTrademarks",
u"PrivateBuild",
u"FileVersion",
u"OriginalFilename",
u"SpecialBuild"]

class LANGANDCODEPAGE(Structure):
_fields_ = [("wLanguage", WORD),
("wCodePage", WORD)]

def get_infos(path):
info_d = {}
dll = windll.version
buf_size = dll.GetFileVersionInfoSizeA(path, 0)
if buf_size < 0:
print "error:", buf_size
return info_d

buf = (c_ubyte * buf_size)()
ret = dll.GetFileVersionInfoA(path, 0, buf_size, buf)
if not ret:
print "GetFileVersionInfo Error"
return info_d

# Get the translate CodePage infomation
translate = LANGANDCODEPAGE(0, 0)
cbTranslate = c_uint(0)

ptrans_buf = c_void_p()
ret = dll.VerQueryValueW(buf,
u"\\VarFileInfo\\Translation",
byref(ptrans_buf),
byref(cbTranslate))

tmp = (c_ushort * 2)()
memmove(tmp, ptrans_buf, 4)
translate.wLanguage = tmp[0]
translate.wCodePage = tmp[1]
del tmp

for i in range(cbTranslate.value / sizeof(translate)):
for block in blocks[:]:
print "\n", block,":",

p = u"\\StringFileInfo\\%04x%04x\\%s" %
(translate.wLanguage, translate.wCodePage, block)
line = (c_wchar * _MAX_PATH)()
lpData = c_void_p()
uDataSize = c_uint(0)
ps = pointer(uDataSize)
ret = dll.VerQueryValueW(buf, p, byref(lpData), ps)
if ret:
memmove(line, lpData, uDataSize.value * sizeof(c_wchar))

s = u""
for pos in range(uDataSize.value, 0, -1):
if line[pos] != '\0':
break

for c in line[0:pos+1]:
s += c

info_d[block] = s
print s
return info_d

def get_hash(path, method):
mods = {"md5": md5, "sha1" : sha}
try:
mod = mods[method]
f = file(path, 'rb')
result = mod.new(f.read()).hexdigest()
f.close()
return result
except:
return -1

get_md5 = lambda path:get_hash(path, "md5")
get_sha1 = lambda path:get_hash(path, "sha1")

def _test():
test_path = "somefile.exe"
info = get_infos(test_path)
print info

md = get_md5(test_path)
print "md5:", md

s = get_sha1(test_path)
print "sha1:", s

if __name__=='__main__':
_test()

回复全部
回复作者
转发
0 个新帖子