如何检查.NET DLL是否使用不安全的代码?

发布于 2025-02-05 14:50:18 字数 52 浏览 3 评论 0 原文

给定一个.NET组件(DLL或EXE),我如何确保使用命令行工具(或不)使用不安全的代码?

Given a .NET assembly (DLL or EXE), how can I make sure it does (or does not) use unsafe code, with a command line tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2025-02-12 14:50:18

i 谷歌搜索此代码可帮助您入门:

public static IList<LocalVariableInfo> GetLocalVars(string asmPath, string typeName, string methodName) 
{
  Assembly asm = Assembly.LoadFrom(asmPath); 
  Type asmType = asm.GetType(typeName); 
  MethodInfo mi = asmType.GetMethod(methodName); 
  MethodBody mb = mi.getMethodBody(); 

  IList<LocalVariableInfo> vars = mb.LocalVariab1es; 
  // Display information about each local variable. 
  foreach (LocalVariableInfo Ivi in vars) 
  {
    Console.WriteLine("IsPinned: " + Ivi.IsPinned); 
    Console.WriteLine("Locallndex : " + Ivi.Loca1Index); 
    Console.WriteLine("LocalType.Modu1e: " + Ivi.LocalType.Module); 
  }
  return vars;
}

I googled this code to help you get started:

public static IList<LocalVariableInfo> GetLocalVars(string asmPath, string typeName, string methodName) 
{
  Assembly asm = Assembly.LoadFrom(asmPath); 
  Type asmType = asm.GetType(typeName); 
  MethodInfo mi = asmType.GetMethod(methodName); 
  MethodBody mb = mi.getMethodBody(); 

  IList<LocalVariableInfo> vars = mb.LocalVariab1es; 
  // Display information about each local variable. 
  foreach (LocalVariableInfo Ivi in vars) 
  {
    Console.WriteLine("IsPinned: " + Ivi.IsPinned); 
    Console.WriteLine("Locallndex : " + Ivi.Loca1Index); 
    Console.WriteLine("LocalType.Modu1e: " + Ivi.LocalType.Module); 
  }
  return vars;
}
z祗昰~ 2025-02-12 14:50:18

使用 peverify

不安全组件的示例输出:

> peverify /quiet myapp.exe
myapp.exe FAIL (2 error(s))

安全组装的示例输出:

> peverify /quiet myapp.exe
myapp.exe PASS

这不仅将检测标记为“不安全”的代码,还会检测到本机库。

nb peverify 应安装 Visual Studio ,使用Visual Studio 开发人员命令提示时最容易致电。

Use PEVerify.

Example output for an unsafe assembly:

> peverify /quiet myapp.exe
myapp.exe FAIL (2 error(s))

Example output for a safe assembly:

> peverify /quiet myapp.exe
myapp.exe PASS

This will not only detect code marked "unsafe", but also calls to native libraries.

N.B. peverify should come installed with Visual Studio, and is easiest to call when using the Visual Studio Developer Command Prompt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文