.NET 单文件执行程序拆解器 SingleFileExtractor
.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.exe
3. 在程序中使用
在项目中安装 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 extracted
var 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 是创建一个真正的单文件执行程序,它是直接执行的。
如果你希望 .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
来指定你希望文件解压到的目标目录。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
2020-12-20 再次探讨 WinForms 多线程开发
2010-12-20 关于 SQL Server 2005 分页的几个帖子