English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french
查看: 4|回复: 0

.NET 单文件执行程序拆解器 SingleFileExtractor

[复制链接]
查看: 4|回复: 0

.NET 单文件执行程序拆解器 SingleFileExtractor

[复制链接]
查看: 4|回复: 0

349

主题

0

回帖

1057

积分

金牌会员

积分
1057
fdfv232

349

主题

0

回帖

1057

积分

金牌会员

积分
1057
2025-2-7 01:09:10 | 显示全部楼层 |阅读模式
.NET 单文件执行程序拆解器 SingleFileExtractor

.NET 现在支持将程序打包为单文件格式,这方便了部署,问题是,我们不能直接看到程序中使用了哪些 DLL,更不能简单地通过查看文件属性的方式,看到这些 DLL 的版本。要是可以像使用 Zip 一样,可以打开这个合成的文件,直接查看内容就好了。
SingleFileExtractor 就是这样的工具。
它可以从单文件的应用程序中将其中包含的内容,包括程序集、配置文件等等,拆解到文件夹中,方便开发人员进行分析。
1. 安装

它的安装也很方便,通过 dotnet 工具的方式进行安装和使用。
dotnet tool install -g sfextract
注意,当前 2024/12/20 使用 dotnet tool install -g sfextract 安装此工具,需要 .NET 6.0
2. 使用

2.1 命令行

sfextract [file] -o|--output [directory]例如
sfextract Application.exe -o path/to/output/2.2 列出其中的文件

sfextract Application.exe3. 在程序中使用

在项目中安装 SingleFileExtractor.Core NuGet 包,
然后,使用 SingleFileExtractor 提供的 ExecutableReader 类来打开文件。
var reader = new ExecutableReader("application.exe");3.1 读取启动信息

当你希望知道程序的入口点的时候,可以这样读取启动信息
var reader = new ExecutableReader("application.exe");var startupInfo = reader.StartupInfo;3.2 提取全部文件。

var reader = new ExecutableReader("application.exe");// Validate if executable is a single file executable, and can be extractedvar isSingleFile = reader.IsSingleFile;if (isSingleFile){    // Extract specific file entry    await reader.Manifest.Entries[0].ExtractToFileAsync("example.dll");    // , or create an in-memory stream of a specifc file entry    var stream = await reader.Manifest.Entries[0].AsStreamAsync()        // Extract all files to a directory    await reader.ExtractToDirectoryAsync("path/to/output");}3.3 提取指定内容到文件

var reader = new ExecutableReader("application.exe");await reader.Manifest.Entries[0].ExtractToFileAsync("example.dll");3.4 提取指定内容到流

var reader = new ExecutableReader("application.exe");var stream = await reader.Manifest.Entries[0].AsStreamAsync()4. 为什么创造它?

我正在处理的另一个程序需要我从单文件的执行程序中提取内容,另外我还看到有人在询问这个问题,所以,我决定将它开发为一个 dotnet 工具的形式,并使用 NuGet 包的形式发布。
项目地址:https://github.com/Droppers/SingleFileExtractor
.NET 3 与 .NET 5 单文件的区别

Can .Net Core 3 self-contained single executable be decompiled? 中,有如下的更新:
In .NET Core 3.0 you can compile to a single executable, but that executable is actually a compressed version of all the files needed to execute at runtime. When you execute the file, it first expands itself out into a temporary directory and then executes the entry point of the application from the directory that contains all the files. By contrast, .NET 5 will create a true, single-executable file that can execute directly in place.
在 .NET 3 的时候,你可以将应用编译为单文件执行程序,这种可执行程序实际上是将所有运行时需要的文件的压缩文件。当你实际执行该文件的时候,它会首先将自己解压到临时文件夹,然后从这个包含所有文件的目录中,找到应用程序的入口点开始执行。与此相反,.NET 5 不会这样,.NET 5 是创建一个真正的单文件执行程序,它是直接执行的。
见: https://learn.microsoft.com/en-us/archive/msdn-magazine/2019/july/csharp-net-reunified-microsoft’s-plans-for-net-5
如果你希望 .NET 5 也可以像 .NET 3 一样工作,虽然可能性不大,微软还是提供了一个项目文件的配置项 IncludeAllContentForSelfExtract ,它的说明如下:
Bundle all published files (except symbol files) into single-file app. This option provides backward compatibility with the .NET Core 3.x version of single-file apps.
将所有发布的文件 (除了符号文件) 也打包到单文件应用中。该设置提供了对 .NET 3 单文件应用的反向兼容性。
<PublishSingleFile>true</PublishSingleFile><IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>见: https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md#optional-settings
同时,可以通过环境变量 DOTNET_BUNDLE_EXTRACT_BASE_DIR 来指定你希望文件解压到的目标目录。
参考资料

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

349

主题

0

回帖

1057

积分

金牌会员

积分
1057

QQ|智能设备 | 粤ICP备2024353841号-1

GMT+8, 2025-3-10 14:53 , Processed in 2.918510 second(s), 27 queries .

Powered by 智能设备

©2025

|网站地图