# 利用技巧

# P/Invoke

Platform Invoke (P/Invoke) 提供了 C# 访问 DLL 中数据结构、回调、函数的能力。基本的使用方式如官方 Platform Invoke 文档中所示。利用 P/Invoke 的能力,C# 程序可以较为容易的调用标准的 Windows API。

using System;
using System.Runtime.InteropServices;
public class Program
{
    // Import user32.dll (containing the function we need) and define
    // the method corresponding to the native function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
    public static void Main(string[] args)
    {
        // Invoke the function as a regular managed method.
        MessageBox(IntPtr.Zero, "Command-line message box", "Attention!", 0);
    }
}

P/Invoke 的缺点在于引用了的 API 调用会最后出现在可执行文件的 IAT 中,使得一些敏感的行为容易被防护软件所注意。同时一些敏感的 API 可能是被防护软件所监控的,通过这种方式进行的 API 调用也容易被防护软件拦截。

# D/Invoke

在 P/Invoke 的基础上,有研究人员提出了基于 Delegates 机制的 D/Invoke,通过更隐蔽的方式来调用所需的 API。

# 参考链接

# .Net

  • .NET documentation

# 利用技巧

  • Emulating Covert Operations - Dynamic Invocation (Avoiding PInvoke & API Hooks)