求判断一个dll是.NET assembly还是Win32的快捷方法

lextm 2007-11-03 06:32:18
两种dll的文件格式肯定有差别,怎么通过分析文件来作出判断?第几个byte可以看出来呢?
...全文
210 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lextm 2008-03-18
  • 打赏
  • 举报
回复
还有一种办法比楼上的反射要稍微好一点(反射加载的dll不能unload,比较占内存)

public static bool IsDotNetAssembly3(string fileName)
{
bool result = true;
Mono.Cecil.AssemblyDefinition myLibrary = null;
try
{
myLibrary = Mono.Cecil.AssemblyFactory.GetAssembly (fileName);
}
catch (Mono.Cecil.Binary.ImageFormatException)
{
// for win32 dll
result = false;
}
catch (ArgumentOutOfRangeException)
{
// for win32 exe
result = false;
}
return result && myLibrary != null;
}


需要使用Mono项目的Cecil。http://www.mono-project.com/Cecil

这个方法还需要更多的测试。
lextm 2008-03-01
  • 打赏
  • 举报
回复
上面的代码还是有点问题。换回了最初的代码,

/// <summary>
/// Verifies whether it is a .NET file.
/// </summary>
/// <param name="fileName">File name</param>
/// <returns>true if it is .NET, false if else.</returns>
public static bool IsDotNetAssembly(string fileName) {
bool result = true;
try {
System.Reflection.Assembly.LoadFrom(fileName);
} catch (BadImageFormatException ex) {
int errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
if (errorCode == COR_E_ASSEMBLYEXPECTED)
{
result = false;
}
}
return result;
}

private const int COR_E_ASSEMBLYEXPECTED = -2146234344;
lextm 2007-11-04
  • 打赏
  • 举报
回复
刚刚从Google上面找到一个函数
/// <summary>
/// Verifies whether it is a .NET file.
/// </summary>
/// <param name="fileName">File name</param>
/// <returns>true if it is .NET, false if else.</returns>
public static bool IsDotNetAssembly(string fileName)
{
bool result = false;
uint peHeader;
uint peHeaderSignature;
ushort machine;
ushort sections;
uint timestamp;
uint pSymbolTable;
uint noOfSymbol;
ushort optionalHeaderSize;
ushort characteristics;
ushort dataDictionaryStart;
uint[] dataDictionaryRVA = new uint[16] ;
uint[] dataDictionarySize = new uint[16];

Stream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);

//PE Header starts @ 0x3C (60). Its a 4 byte header.
fs.Position = 0x3C;
peHeader = reader.ReadUInt32();

//Moving to PE Header start location...
fs.Position = peHeader;
peHeaderSignature = reader.ReadUInt32();

//We can also show all these value, but we will be
//limiting to the CLI header test.
machine = reader.ReadUInt16();
sections = reader.ReadUInt16();
timestamp = reader.ReadUInt32();
pSymbolTable = reader.ReadUInt32();
noOfSymbol = reader.ReadUInt32();
optionalHeaderSize = reader.ReadUInt16();
characteristics = reader.ReadUInt16();

/*
Now we are at the end of the PE Header and from here, the
PE Optional Headers starts...
To go directly to the datadictionary, we'll increase the
stream’s current position to with 96 (0x60). 96 because,
28 for Standard fields
68 for NT-specific fields
From here DataDictionary starts...and its of total 128 bytes. DataDictionay has 16 directories in total,
doing simple maths 128/16 = 8.
So each directory is of 8 bytes.
In this 8 bytes, 4 bytes is of RVA and 4 bytes of Size.

btw, the 15th directory consist of CLR header! if its 0, its not a CLR file :)
*/
dataDictionaryStart = Convert.ToUInt16 (Convert.ToUInt16(fs.Position) + 0x60);
fs.Position = dataDictionaryStart;
for (int i = 0; i < 15; i++)
{
dataDictionaryRVA[i] = reader.ReadUInt32();
dataDictionarySize[i] = reader.ReadUInt32();
}
result = dataDictionaryRVA[14] != 0;
fs.Close();
return result;
}

还是读取PE的元数据来做的判断。
windily 2007-11-03
  • 打赏
  • 举报
回复
关注..........
蒋晟 2007-11-03
  • 打赏
  • 举报
回复
Microsoft Portable Executable and Common Object File Format Specification http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
An In-Depth Look into the Win32 Portable Executable File Format, Part 2
http://msdn.microsoft.com/msdnmag/issues/02/03/Pe2/
Net 脱壳 反混淆神器De4dot-3.1.41592最新版,De4Dot是一个开源的脱壳/反混淆工具,这款工具可以奉为神器级工具。因为它的脱壳能力的确很强,使用它可以成功地脱掉了Dotfuscator、MaxToCode处理过的程序,至于其它的加壳/反混淆工具比如说Xenocode、ThemIDA等我还没有进行过实验,之后我将计划研究各类加壳/反混淆工具的脱壳方法,我深信De4Dot能够给我带来巨大帮助。(由于De4Dot是开源的,我相信即便De4Dot暂时处理不了的壳通过扩展其功能必将能解决) 支持以下几种反混淆: CliSecure Crypto Obfuscator Dotfuscator .NET Reactor 4.x Eazfuscator.NET SmartAssembly 4.x-6.x Xenocode 用法说明: 开始-运行-cmd 输入以下命令: de4dot工具解压路径\de4dot --dont-rename -f 你要反混淆的程序集dll 例如: C:\Users\Administrator\Desktop\de4dot\de4dot --dont-rename -f C:\Users\Administrator\Desktop\zkcms.dll 特点: 伪随机列表会做的事情,它取决于什么混淆混淆组装: 内联方法。有些混淆器移动到另一个静态方法方法,并调用它的一小部分。 解密字符串的静态或动态 解密等常量。有些混淆器也可以加密其他常数,如整数,双打等 解密方法静态或动态 删除代理方法。许多混淆器取代大多数/所有调用指令调用委托。此委托依次调用真正的方法。 重命名符号。尽管大多数符号不能恢复,将它们重命名为人类可读的字符串。有时,一些原来的名称可以被恢复,虽然。 Devirtualize虚拟化代码 解密资源。许多混淆器有一个选项来加密.NET资源。 解密嵌入的文件。许多混淆器有一个嵌入,可能加密/压缩其他组件的选项。 删除篡改检测代码 删除反调试代码 控制流反混淆。许多混淆器修改IL代码,所以它看起来像意大利面条式的代码很难理解代码。 还原类字段。有些混淆器可以移动领域从一个类来创建类其他一些混淆。 PE EXE转换.NET exe文件。有些模糊处理一个Win32 PE等包装.NET程序集里面的.NET反编译器无法读取该文件。 移除大多数/所有的垃圾类添加混淆。 修复了一些的peverify错误。许多混淆器是马车和创建无法验证的代码错误。 还原类型的方法的参数和字段

17,747

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧