Lia*_*ath 2 c# security reflection
这是我之前的问题的后续内容,我想在加载之前检查程序集的StrongName(通过硬盘上的文件或通过字节数据).确保它是由我创造的.
使用时是否存在安全风险,Assembly.LoadFrom或者Assembly.Load通过将恶意代码加载到这些变量中来执行恶意代码?我应该考虑在AppDomain中加载这些程序集来读取它们吗?
这是我的其余代码:
Assembly dll = Assembly.LoadFrom("UnauthorisedPlugin.dll");
byte[] thisDllKey = Assembly.GetExecutingAssembly().GetName().GetPublicKey();
byte[] dllKey = dll.GetName().GetPublicKey();
if (Enumerable.SequenceEqual(thisDllKey, dllKey))
{
Type pluginType = dll.GetTypes().Single();
IPlugin unauthPlugin = (IPlugin)Activator.CreateInstance(pluginType);
Console.WriteLine(unauthPlugin.Run());
}
else
{
Console.WriteLine("The DLL is not authorised");
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
您可以通过以仅反射模式加载程序集来缓解部分问题:
仅反射加载上下文允许您检查为其他平台或其他版本的.NET Framework编译的程序集.加载到此上下文中的代码只能被检查; 它无法执行.这意味着无法创建对象,因为无法执行构造函数.
您可以使用Assembly.ReflectionOnlyLoad()和执行此操作Assembly.ReflectionOnlyLoadFrom().
有关详细信息,请参阅此处 - http://msdn.microsoft.com/en-us/library/ms172331.aspx