daem0nc0re/PSFileParse
GitHub: daem0nc0re/PSFileParse
一个 PowerShell 模块,用于将 PE 和 Mach-O 文件格式解析为结构化的 PowerShell 对象,便于自动化文件分析和资源导出。
Stars: 1 | Forks: 0
# PSFileParse
PowerShell 模块,用于将文件格式信息获取为 PowerShell 对象。
## 目标
我开发这个 PowerShell 模块是为了向我的同事讲授文件格式,但它对于自动化简单的文件分析也应该很有用。
## 环境
此模块应在以下环境中运行:
* Windows PowerShell
* PowerShell 7+ (Windows、macOS、Linux)
## 用法
此模块中的 cmdlet 仅将文件格式分析的结果转换为 PowerShell 对象,并且它们接受要分析的文件路径作为 `-Path` 参数。
cmdlet 的命名约定为 `Get-FileInformation`。
```
PS C:\> Import-Module path/to/PSFileParse.dll -Verbose
PS C:\> $obj = Get-FileInformation -Path path/to/file.bin -Verbose
```
## Cmdlet
该 PowerShell 模块目前实现了以下 cmdlet。
我将添加解析其他文件格式的 cmdlet,重点是可执行文件。
| Cmdlet | 描述 |
| :--- | :--- |
| `Get-MachOFileInformation` | 分析 Mach-O 文件格式 |
| `Get-PEFileInformation` | 分析 PE 文件格式 |
## 特殊方法
某些自定义对象具有特殊方法。
* __`PSFileParse.PE.ImageResourceDirectory.Export(string output_dir)`__:
此方法尝试导出 PE 文件格式中资源目录的数据。
例如,如果你想将资源目录项导出为文件,请按如下方式使用:
PS C:\Works> Import-Module C:\Works\PSFileParse.dll -Verbose
VERBOSE: Importing cmdlet 'Get-MachOFileInformation'.
VERBOSE: Importing cmdlet 'Get-PEFileInformation'.
PS C:\Works> $pe = Get-PEFileInformation -Path C:\Windows\SystemResources\shell32.dll.mun -Verbose
VERBOSE: Analyzing 'C:\Windows\SystemResources\shell32.dll.mun'.
PS C:\Works> $pe.DataDirectories.Resource.Export("$pwd/shell32_res")
PS C:\Works> Get-ChildItem -Recurse -Path $pwd/shell32_res -File | %{ $_.FullName }
C:\Works\shell32_res\AVI\150\1033.bin
C:\Works\shell32_res\AVI\151\1033.bin
C:\Works\shell32_res\AVI\152\1033.bin
C:\Works\shell32_res\AVI\160\1033.bin
C:\Works\shell32_res\AVI\161\1033.bin
C:\Works\shell32_res\AVI\162\1033.bin
C:\Works\shell32_res\AVI\163\1033.bin
C:\Works\shell32_res\AVI\164\1033.bin
C:\Works\shell32_res\AVI\165\1033.bin
C:\Works\shell32_res\AVI\166\1033.bin
C:\Works\shell32_res\AVI\167\1033.bin
C:\Works\shell32_res\AVI\168\1033.bin
C:\Works\shell32_res\AVI\169\1033.bin
C:\Works\shell32_res\Bitmap\133\1033.dib
C:\Works\shell32_res\Bitmap\134\1033.dib
C:\Works\shell32_res\Bitmap\135\1033.dib
--snip--
如果你只想要图标文件,请按如下方式运行:
PS C:\Works> $pe.DataDirectories.Resource.Entries | Format-Table
Index Identifier Content
----- ---------- -------
0 AVI @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
1 FTR @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
2 LIBRARY @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
3 MUI @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
4 ORDERSTREAM @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
5 TYPELIB @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
6 UIFILE @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
7 XML @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
8 XSD @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
9 Cursor @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
10 Bitmap @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
11 Icon @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
12 GroupCursor @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
13 GroupIcon @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
14 Version @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
15 Manifest @{Characteristics=0; TimeDateStamp=1970/01/01 (Thu.) 00:00:00 UTC; MajorVersion=0; MinorVersion=0;...
PS C:\Works> $pe.DataDirectories.Resource.Entries[11].Content.Export("$pwd/shell32_icons")
PS C:\Works> Get-ChildItem -Recurse -Path $pwd/shell32_icons -File | %{ $_.FullName }
C:\Works\shell32_icons\Icon\1\1033.png
C:\Works\shell32_icons\Icon\10\1033.dib
C:\Works\shell32_icons\Icon\100\1033.dib
C:\Works\shell32_icons\Icon\1000\1033.dib
C:\Works\shell32_icons\Icon\1001\1033.dib
C:\Works\shell32_icons\Icon\1002\1033.dib
C:\Works\shell32_icons\Icon\1003\1033.dib
C:\Works\shell32_icons\Icon\1004\1033.png
C:\Works\shell32_icons\Icon\1005\1033.dib
--snip--
## 参考
## Mach-O
* [GitHub - apple-oss-distributions/xnu: EXTERNAL_HEADERS/mach-o](https://github.com/apple-oss-distributions/xnu/tree/main/EXTERNAL_HEADERS/mach-o)
* [GitHub - apple-oss-distributions/xnu: osfmk/mach](https://github.com/apple-oss-distributions/xnu/tree/main/osfmk/mach)
* [GitHub - apple-oss-distributions/cctools](https://github.com/apple-oss-distributions/cctools)
* [GitHub - apple-oss-distributions/dyld](https://github.com/apple-oss-distributions/dyld)
### Portable Executable Format / Common Object File Format
* [Microsoft - PE Format](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)
* [OS DEV - COFF](https://wiki.osdev.org/COFF)
* [Microsoft SDK Headers (例如 `winnt.h`, `CorHdr.h`)](https://learn.microsoft.com/en-us/windows/apps/windows-sdk/downloads)
* [Microsoft - windows-docs-rs](https://microsoft.github.io/windows-docs-rs/doc/windows/index.html)
* [Mandiant - Tracking Malware with Import Hashing](https://cloud.google.com/blog/topics/threat-intelligence/tracking-malware-import-hashing)
* [ECMA-335 - Common Language Infrastructure (CLI)](https://ecma-international.org/publications-and-standards/standards/ecma-335/)
* [GitHub - dotnet/runtime](https://github.com/dotnet/runtime)
标签:AI合规, IPv6, Mach-O, PE文件, PowerShell, SOC Prime, 云资产清单, 多人体追踪, 开发工具, 文件解析, 系统工具, 逆向工程