Joh*_*zen 11 c# security reflection plugins
我在许多应用程序中使用了以下代码来加载暴露插件的.DLL程序集.
但是,我以前总是关注功能而不是安全性.
我现在计划在Web应用程序上使用此方法,该应用程序可供我以外的组使用,并且我希望确保该功能的安全性是最好的.
private void LoadPlugins(string pluginsDirectory)
{
List<IPluginFactory> factories = new List<IPluginFactory>();
foreach (string path in Directory.GetFiles(pluginsDirectory, "*.dll"))
{
Assembly assembly = Assembly.LoadFile(path);
foreach (Type type in assembly.GetTypes())
{
IPluginEnumerator instance = null;
if (type.GetInterface("IPluginEnumerator") != null)
instance = (IPluginEnumerator)Activator.CreateInstance(type);
if (instance != null)
{
factories.AddRange(instance.EnumerateFactories());
}
}
}
// Here, I would usually collate the plugins into List<ISpecificPlugin>, etc.
}
Run Code Online (Sandbox Code Playgroud)
我头几个问题:
我应该担心还有其他安全问题吗?
编辑:请记住,我希望任何人能够编写插件,但我仍然希望是安全的.
Blu*_*que 13
1)强有力地命名具有特定键的组件.
2)在加载时,检查组件是否已用您期望的键命名
例:
public static StrongName GetStrongName(Assembly assembly)
{
if(assembly == null)
throw new ArgumentNullException("assembly");
AssemblyName assemblyName = assembly.GetName();
// get the public key blob
byte[] publicKey = assemblyName.GetPublicKey();
if(publicKey == null || publicKey.Length == 0)
throw new InvalidOperationException( String.Format("{0} is not strongly named", assembly));
StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
// create the StrongName
return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
}
// load the assembly:
Assembly asm = Assembly.LoadFile(path);
StrongName sn = GetStrongName(asm);
// at this point
// A: assembly is loaded
// B: assembly is signed
// C: we're reasonably certain the assembly has not been tampered with
// (the mechanism for this check, and it's weaknesses, are documented elsewhere)
// all that remains is to compare the assembly's public key with
// a copy you've stored for this purpose, let's use the executing assembly's strong name
StrongName mySn = GetStrongName(Assembly.GetExecutingAssembly());
// if the sn does not match, put this loaded assembly in jail
if (mySn.PublicKey!=sn.PublicKey)
return false;
Run Code Online (Sandbox Code Playgroud)
注意:代码尚未经过测试或编译,可能包含语法错误.
| 归档时间: |
|
| 查看次数: |
4167 次 |
| 最近记录: |