MicroProj/2007-07-17

抽取EXE,DLL文件的版权信息 的小程序,来珠海后的第一个工作程序 ::-- ZoomQuiet [2007-07-17 09:32:46]

CPUG联盟::

CPUG::门户plone

BPUG

SPUG

ZPUG

SpreadPython Python宣传

1. 抽取EXE,DLL文件的版权

{{{Albert Lee <[email protected]> hide details 4:19 pm (36 minutes ago)

}}}

   1 #coding=utf-8
   2 from ctypes import *
   3 import md5
   4 import sha
   5 
   6 _MAX_PATH = 260
   7 
   8 WORD = c_ushort
   9 UINT = c_uint
  10 BYTE = c_ubyte
  11 
  12 blocks = [u"Comments",
  13            u"InternalName",
  14            u"ProductName",
  15            u"CompanyName",
  16            u"LegalCopyright",
  17            u"ProductVersion",
  18            u"FileDescription",
  19            u"LegalTrademarks",
  20            u"PrivateBuild",
  21            u"FileVersion",
  22            u"OriginalFilename",
  23            u"SpecialBuild"]
  24 
  25 class LANGANDCODEPAGE(Structure):
  26    _fields_ = [("wLanguage", WORD),
  27                ("wCodePage", WORD)]
  28 
  29 def get_infos(path):
  30    info_d = {}
  31    dll = windll.version
  32    buf_size = dll.GetFileVersionInfoSizeA(path, 0)
  33    if buf_size < 0:
  34        print "error:", buf_size
  35        return info_d
  36 
  37    buf = (c_ubyte * buf_size)()
  38    ret = dll.GetFileVersionInfoA(path, 0, buf_size, buf)
  39    if not ret:
  40        print "GetFileVersionInfo Error"
  41        return info_d
  42 
  43    # Get the translate CodePage infomation
  44    translate = LANGANDCODEPAGE(0, 0)
  45    cbTranslate = c_uint(0)
  46 
  47    ptrans_buf = c_void_p()
  48    ret = dll.VerQueryValueW(buf,
  49                       u"\\VarFileInfo\\Translation",
  50                       byref(ptrans_buf),
  51                       byref(cbTranslate))
  52 
  53    tmp = (c_ushort * 2)()
  54    memmove(tmp, ptrans_buf, 4)
  55    translate.wLanguage = tmp[0]
  56    translate.wCodePage = tmp[1]
  57    del tmp
  58 
  59    for i in range(cbTranslate.value / sizeof(translate)):
  60        for block in blocks[:]:
  61            print "\n", block,":",
  62 
  63            p = u"\\StringFileInfo\\%04x%04x\\%s" %(translate.wLanguage, translate.wCodePage, block)
  64            line = (c_wchar * _MAX_PATH)()
  65            lpData = c_void_p()
  66            uDataSize = c_uint(0)
  67            ps = pointer(uDataSize)
  68            ret = dll.VerQueryValueW(buf, p, byref(lpData), ps)
  69            if ret:
  70                memmove(line, lpData, uDataSize.value * sizeof(c_wchar))
  71                s = u""
  72                for pos in range(uDataSize.value, 0, -1):
  73                    if line[pos] != '\0':
  74                        break
  75 
  76                for c in line[0:pos+1]:
  77                    s += c
  78 
  79                info_d[block] = s
  80                print s
  81    return info_d
  82 
  83 def get_hash(path, method):
  84    mods = {"md5": md5, "sha1" : sha}
  85    try:
  86        mod = mods[method]
  87        f = file(path, 'rb')
  88        result = mod.new(f.read()).hexdigest()
  89        f.close()
  90        return result
  91    except:
  92        return -1
  93 
  94 get_md5 = lambda path:get_hash(path, "md5")
  95 get_sha1 = lambda path:get_hash(path, "sha1")
  96 
  97 def _test():
  98    test_path = "somefile.exe"
  99    info = get_infos(test_path)
 100    print info
 101 
 102    md = get_md5(test_path)
 103    print "md5:", md
 104 
 105    s = get_sha1(test_path)
 106    print "sha1:", s
 107 
 108 if __name__=='__main__':
 109    _test()

1.1. 反馈

MicroProj/2007-07-17 (last edited 2009-12-25 07:12:50 by localhost)