python判断是否为“PE文件”

什么是PE文件?

PE文件被称为可移植的执行体是Portable Execute的全称,常见的EXE、DLL、OCX、SYS、COM都是PE文件,PE文件是微软Windows操作系统上的程序文件(可能是间接被执行,如DLL)。

怎么识别PE文件?

1、首先,PE文件一定是MZ(0x4D5A)起头的。

2、其次,3C位置的值(即如图所示E8)指向的值是PE(0x5045)

代码如下:

#coding: UTF-8
import linecache,os,struct,sys
#get all PE files of a directory
def readFileChar(path):
    try:
        fileHandle=open(path,"rb")
        data_id = struct.unpack("h",fileHandle.read(2))
        return data_id[0]
        fileHandle.close()
    except Exception ,e:
        print e
        return "kkk"
        
def getShifting(path):
    try:
        #获得0x3c地址的值,pe文件应为0x45 50
        fileHandle=open(path,"rb")
        fileHandle.seek(60,0)
        data_id = struct.unpack("h",fileHandle.read(2))[0]
        fileHandle.close()
        #print data_id
        fileHandle=open(path,"rb")
        fileHandle.seek(data_id,0)
        pe = struct.unpack("h",fileHandle.read(2))[0]
        fileHandle.close()
        return pe
    except:
        return  "kkk"
if __name__=="__main__":
    paths=[]
    pePath=[]
    #paths:all files' paths
    insPath=sys.argv[1]
    savefile=insPath.split("\\")[-1]
    #cur_path=os.path.abspath(os.path.join(os.path.dirname(__file__), "%s_PEfile.txt"%savefile))
    cur_path=os.path.abspath(os.path.join("c:\\", "%s_PEfile.txt"%savefile))
    #print "cur_path",cur_path
    savefileHandle=open(cur_path,'w')
    #print insPath
    for root,dirs,files in os.walk(insPath):
        for file in files:
            paths.append(root+file)
            path=root+"\\"+file
            shifting = getShifting(path)
            # print "readFileChar",readFileChar(path)
            # print "shifting",shifting
            if readFileChar(path)==23117 and shifting==17744:
                pePath.append(file)
                print path,"is ----------------->pe file "
                savefileHandle.write(file+'\n')
    savefileHandle.close()   


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值