首页 > 编程语言 >Python 检测PE所启用保护方式

Python 检测PE所启用保护方式

时间:2022-10-04 14:55:30浏览次数:48  
标签:args end Python 保护方式 pid item PE print handle

Python 通过pywin32模块调用WindowsAPI接口,实现对特定进程加载模块的枚举输出并检测该PE程序模块所启用的保护方式,此处枚举输出的是当前正在运行进程所加载模块的DLL模块信息,需要用户传入进程PID才可实现输出。

  • 首先需要安装两个依赖包:
  • pip install pywin32
  • pip install pefile

然后再命令行模式下执行命令启动枚举功能。

# By: LyShark
import win32process
import win32api,win32con,win32gui
import os,pefile,argparse

def GetProcessModules(pid):
    ModuleList = []
    handle   = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid )
    hModule  = win32process.EnumProcessModules(handle)
    for item in hModule:
        Module_Addr = hex(item)
        Module_Path = win32process.GetModuleFileNameEx(handle,item)
        Module_Name = os.path.basename(str(Module_Path))
        ModuleList.append([Module_Addr,Module_Name,Module_Path])
    win32api.CloseHandle(handle)
    return ModuleList

def CheckModulesProtect(ClassName):
    UNProtoModule = []
    if type(ClassName) is str:
        handle = win32gui.FindWindow(0,ClassName)
        threadpid, procpid = win32process.GetWindowThreadProcessId(handle)
        ProcessModule = GetProcessModules(int(procpid))
    else:
        ProcessModule = GetProcessModules(int(ClassName))
    print("-" * 100)
    print("映像基址\t\t模块名称\t基址随机化\tDEP保护兼容\t强制完整性\tSEH异常保护")
    # By: LyShark.com
    print("-" * 100)

    for item in ProcessModule:
        pe = pefile.PE(item[2])
        DllFlage = pe.OPTIONAL_HEADER.DllCharacteristics
        print("%10s"%(item[0]),end="\t")
        print("%21s"%(item[1]),end="\t")

        # 随机基址 => hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x40 == 0x40
        if( (DllFlage & 64)==64 ):
            print(" True",end="\t\t") 
        else:
            print(" False",end="\t\t")
            UNProtoModule.append(item[2])
        if( (DllFlage & 256)==256 ):
            print("True",end="\t\t")
        else:
            print("False",end="\t\t")
        if ( (DllFlage & 128)==128 ):
            print("True",end="\t\t")
        else:
            print("False",end="\t\t")
        if ( (DllFlage & 1024)==1024 ):
            print("False",end="\t\t\n")
        else:
            print("True",end="\t\t\n")
    
    print("-" * 100)
    print("\n[+] 总模块数: {} 可利用模块: {}".format(len(ProcessModule),len(UNProtoModule)),end="\n\n")
    for item in UNProtoModule:
        print("[-] {}".format(item))
    print("-" * 100)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-H","--handle",dest="handle",help="指定一个正在运行的进程Handle")
    parser.add_argument("-P","--pid",dest="pid",help="指定一个正在运行的进程PID")
    args = parser.parse_args()
    if args.handle or args.pid:
        if args.handle:
            CheckModulesProtect(str(args.handle))
        elif args.pid:
            CheckModulesProtect(int(args.pid))
    else:
        parser.print_help()

输出枚举效果如下:

标签:args,end,Python,保护方式,pid,item,PE,print,handle
From: https://www.cnblogs.com/LyShark/p/16125498.html

相关文章

  • React中常见的TypeScript定义实战
    一引沿Fiber架构是React16中引入的新概念,目的就是解决大型React应用卡顿,React在遍历更新每一个节点的时候都不是用的真实DOM,都是采用虚拟DOM,所以可以理解成fiber就是R......
  • [Typescript] Tips: Create your own 'objectKeys' function using generics and the
    TheloosenessofObject.keyscanbearealpainpointwhenusingTypeScript.Luckily,it'sprettysimpletocreateatighterversionusinggenericsandthekey......
  • Python两层神经网络
    参考神经网络15分钟入门——使用python从零开始写一个两层神经网络_Mr.看海的博客-CSDN博客_神经网络入门python #参考https://blog.csdn.net/fengzhuqiaoqiu/arti......
  • python合并多个excel
    前言1.工作中,经常需要合并多个Excel文件。如果文件数量比较多,则工作量大,易出错,此时,可以使用Python来快速的完成合并。2.使用方法:将需要合并的多个Excel文件放到同一个文......
  • [Typescript + React] Tips: Write your own 'PropsFrom' helper to extract props fr
    Typehelperschangethegamewhenitcomestotypesinyourcodebase.TheyhelpTypeScriptinfermorefromyourcode-andmakeyourtypesalotmorereadable.......
  • 1.如何在RedHat7上安装OpenLDA并配置客户端
    温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。Fayson的github:https://github.com/fayson/cdhproject提示:代码块部分可以左右滑动查看噢1.文档编写目的众所周......
  • Python第五章实验报告
    一.实验项目名称:《零基础学Python》第五章实战、实例以及两道作业题二.实验目的和要求:了解和掌握操作字符串的方法和正则表达式的应用三.实验环境:IDLE(Python3.964-bit)......
  • [论文阅读] FontNet: Closing the gap to font designer performance in font synthes
    1.pretitle:FontNet:Closingthegaptofontdesignerperformanceinfontsynthesisaccepted:AI4CC2022(AIforContentCreation,CVPRWorkshop)paper:http......
  • python生成二维码
    前言二维码在我们的生活中可以说是必不可少的,不单单是手机支付、其它很多地方也都需要扫描二维码。那么下面我们就来看看如何使用python来生成二维码、以及识别二维码。......
  • Python中优雅的字典技巧总结
    1.引言在日常工作中,大家都需要进行字典的相关操作,对于某些初学者,经常会写一堆繁琐的代码来实现某项简单的功能。本篇文章重点介绍一些在Python中关于字典的一些简单技巧,熟......