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

Python 检测PE所启用保护方式

时间:2022-12-30 16:31:22浏览次数:44  
标签:__ args handle Python 保护方式 item PE print end

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 Banner():
    print("  _          ____  _                _    ")
    print(" | |   _   _/ ___|| |__   __ _ _ __| | __")
    print(" | |  | | | \___ \| '_ \ / _` | '__| |/ /")
    print(" | |__| |_| |___) | | | | (_| | |  |   < ")
    print(" |_____\__, |____/|_| |_|\__,_|_|  |_|\_\\")
    print("       |___/                             \n")
    print("E-Mail: [email protected]")

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__":
    Banner()
    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,handle,Python,保护方式,item,PE,print,end
From: https://blog.51cto.com/lyshark/5980638

相关文章

  • Python2 Python3 编码问题 操作系统编码 文本编码格式 ansi编码 utf8编码 gb2312
     编码问题: 操作系统编码 文本编码 编程脚本中字节数组转字符串默认使用的编码格式操作系统默认的编码:Windows系统GBK,Linux系统是utf-8。Windows系统也支持utf-8,......
  • [转]Arduino 入门学习9 OpenBlock 可视化开发工具环境搭建
    Arduino入门学习9OpenBlock可视化开发工具一、简介1.S4A2.ArduinoScratch3.OpenBlock二、操作步骤1.准备Arduino开发板(1)安装Arduino开发IDE(2)从......
  • Python ssh远程登录设备执行命令
    #-*-encoding:utf-8-*-"""@Time:2022/12/3010:10@Auth:ruqing@File:ssh_sonic.py@IDE:PyCharm@Motto:ABC(AlwaysBeCoding)"""importsys,loggingimporto......
  • typescript循环依赖error
    //a.tsimport{b}from"./b"exportconsta=[b]//b.tsimport{a}from"./a"exportconstb=[a]或//a.tsimport{b1}from"./b"exportconst......
  • Opentsdb安装部署(单机版)
    Opentsdb简介opentsdb是基于Hbase的时序数据库[时间序列数据库]。不具备通用性,主要针对具有时间特性和需求的数据,如监控数据、温度变化数据等。opentsdb说是数据库,但并不能......
  • Kafka在Zookeeper上的节点信息和查看方式
    Kafka简介Kafka是最初由Linkedin公司开发,是一个分布式、支持分区的(partition)、多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实时的处理大量数......
  • Pagehelper,total 等于 pageSize问题
    问题描述:项目框架:ruoyi-vuemaven依赖:<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><versi......
  • 流式细胞仪生物信息学CRO丨Cytapex功能简介
    Cytapex的自动化解决方案可以根据客户的细胞分析需求量身定制,速度快(60秒),并提供可靠、可重复的结果。  解决方案01、质量检查使用Cytapex的自......
  • Python多环境扩展管理
    众所周知,Python发展至今,版本众多,在使用过程中经常遇到第三方库依赖的Python版本和系统Python版本不一致的情况。同时又因系统底层需调用当前版本Python,所以不能随意变更当前......
  • Docker 安装 Python
    Docker安装PythonPython简介Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。Python由GuidovanRossum于1989年底发明,第一个公开发行版发行于19......