快速入门:使用 MXC SDK 运行工作负荷

使用 .NET、TypeScript 或 Rust SDK 在Windows进程隔离中运行命令。 每个示例创建一个示例输入,允许工作负荷读取它,并授予对单独输出文件夹的写入权限。 网络和 UI 权限仍未被授予。

主机应用创建策略并处理结果。 该命令在 MXC 内运行;主机应用的其余部分则不会在 MXC 内运行。

这些示例使用 MXC 1.0 V1 SDK API。 V1 SDK 在内部使用稳定的 1.0.0 架构,因此您无需在请求中指定架构版本。 请参阅选择兼容的架构。

Prerequisites

您需要:

  • Windows 11版本 24H2 或更高版本,并具有受支持的 ProcessContainer 环境。
  • 对于 C#,需要 .NET 8 SDK 或更高版本以及 Microsoft.Mxc.Sdk NuGet 包。
  • 对于 TypeScript、npm、@microsoft/mxc-sdk 包,以及 Node.js 24 版本系列中的 Node.js 24.21.0 或更高版本,或 Node.js 26.8.0 或更高版本。
  • 对于 Rust,需要稳定的 Rust 工具链、MSVC 生成工具以及 mxc-sdk crate。 请参阅在 Windows 上设置你的 Rust 开发环境。
  • 需要可写的工作目录和 PowerShell 来运行安装命令。

这些说明使用来自 NuGet.org、npm 和 crates.io 的 MXC 1.0 包。 无需 MXC 源代码构建。

运行示例之前,请检查 平台支持 。 受支持的Windows版本不能保证每个可选功能都可用。

在某些主机上,MXC 在基于 AppContainer 的回退层中运行进程隔离。 该层需要对系统驱动器和空设备进行一次性准备,且空设备设置在每次重启时都会重置。 在未完成准备工作的情况下,MXC 的主机探测将返回一条警告,指出要运行的 wxc-host-prep.exe 子命令。 TypeScript 示例会打印来自 getPlatformSupport() 的这些警告,还可以通过运行 wxc-exec.exe --probe 来检查主机。 由于 wxc-host-prep.exe 需要获得管理员批准,因此让管理员运行建议的命令,而不是从应用中运行它。 有关详细信息,请参阅 主机准备。

1.创建项目

创建控制台应用并从 NuGet.org 添加 MXC 包:

dotnet new console -n MxcQuickstart
Set-Location MxcQuickstart
dotnet add package Microsoft.Mxc.Sdk

2. 定义并运行工作负载

进程隔离适用于此生存期较短的命令,因为它仅需要选定的主机文件且无需保留环境。 其他工作负荷可能需要不同的包含类型。

主机在项目目录下创建 workspace\input 和 workspace\output。 包含的命令读取 input.txt 和写入 result.txt。 它没有对其输入目录的写入访问权限,也无权访问其他项目文件。

主机提供一个小型的显式环境,而不是继承其自身的变量。 为 Windows ProcessContainer 工作负荷提供环境时,请同时包含 SystemRoot 和 LOCALAPPDATA。 如果两者中任一缺失,MXC 会在启动前拒绝该请求。 该示例将 LOCALAPPDATA、TEMP 和 TMP 指向已授予的输出目录,以便将临时文件和每用户文件限制在策略范围内。

替换为 Program.cs :

using Microsoft.Mxc.Sdk.V1;

if (!OperatingSystem.IsWindows())
{
    Console.Error.WriteLine("This example requires Windows.");
    return 1;
}

try
{
    var support = MxcPlatform.GetPlatformSupport();
    if (!support.IsSupported ||
        !support.AvailableMethods.Contains(ContainmentBackend.ProcessContainer))
    {
        Console.Error.WriteLine(support.Reason ?? "Windows ProcessContainer is unavailable.");
        return 1;
    }

    string inputDirectory = Path.GetFullPath(@"workspace\input");
    string outputDirectory = Path.GetFullPath(@"workspace\output");
    string outputFile = Path.Combine(outputDirectory, "result.txt");
    string commandInterpreter = Path.Combine(Environment.SystemDirectory, "cmd.exe");

    Directory.CreateDirectory(inputDirectory);
    Directory.CreateDirectory(outputDirectory);
    File.WriteAllText(Path.Combine(inputDirectory, "input.txt"), "Hello from MXC!\n");

    var request = new ContainerRequest(
        $"\"{commandInterpreter}\" /d /c type input.txt > \"{outputFile}\"")
    {
        Containment = new Containment.Process(),
        Filesystem = new FilesystemPolicy
        {
            ReadonlyPaths = { inputDirectory, commandInterpreter },
            ReadwritePaths = { outputDirectory },
        },
        // Omitted network and UI permissions remain denied.
        WorkingDirectory = inputDirectory,
        Environment = new()
        {
            ["SystemRoot"] = Environment.GetFolderPath(Environment.SpecialFolder.Windows),
            ["LOCALAPPDATA"] = outputDirectory,
            ["TEMP"] = outputDirectory,
            ["TMP"] = outputDirectory,
        },
        TimeoutMs = 30_000,
    };

    ExecutionResult result = await MxcContainer.RunAsync(request);
    Console.Write(result.Stdout);
    Console.Error.Write(result.Stderr);
    foreach (string warning in result.Warnings)
    {
        Console.Error.WriteLine(warning);
    }

    if (result.TimedOut)
    {
        Console.Error.WriteLine("The workload exceeded its timeout.");
        return 1;
    }

    Console.WriteLine($"Sandbox exited with code {result.ExitCode}");
    if (result.ExitCode == 0)
    {
        Console.Write(File.ReadAllText(outputFile));
    }
    return result.ExitCode;
}
catch (MxcException ex)
{
    Console.Error.WriteLine(ex);
    if (ex.Remediation is not null)
    {
        Console.Error.WriteLine(ex.Remediation);
    }
    return 1;
}

运行应用:

dotnet run

每个版本输出如下:

Sandbox exited with code 0
Hello from MXC!

还可以检查 workspace\output\result.txt。

3.验证被拒绝的操作

仅更改输出文件声明,以便命令尝试写入只读输入目录:

string outputFile = Path.Combine(inputDirectory, "blocked.txt");

在不更改策略的情况下再次运行应用。 预期出现访问被拒绝错误和非零退出代码,没有新 workspace\input\blocked.txt 文件。 还原原始输出文件声明,并确认该允许的操作仍可成功执行。

如果获准执行失败,请将主机设置或配置错误与资源拒绝区分开来。 使用 学习模式 调查后者;不要自动扩大策略。

4. 使示例适配

将示例命令和输入替换为受信任的代表性工作负荷。 查看所需的工具二进制文件、库、临时文件和输出。 仅当任务需要网络或 UI 访问权限,且所选后端能够强制实施预期限制时,才添加这些访问权限。

在每个目标主机上测试允许和拒绝的操作。 使用学习模式优化策略时,请查看每个观察结果,并在不启用学习或审核设置的情况下完成强制运行。

可选:使用编码助手

为代理提供目标平台、SDK 语言和版本、命令、所需的资源、生命周期和隔离目标。 可以调整此提示:

Use the MXC Learn quickstart and the documentation for my installed SDK release.
Integrate this workload through the .NET, TypeScript, or Rust SDK used by this project.

First identify the command, target platforms, input and output paths, network
and UI requirements, and whether state must persist between commands.
Explain the containment choice and create the narrowest policy that fits.
Run both permitted and denied cases and report the effective permissions.

Do not execute the workload outside MXC to get around a failure. Ask for my
approval before expanding access, changing host settings, or using permissive
Learning Mode. Treat observed resource access as evidence, not automatic grants.

审阅智能体的假设和生成的策略。 代理可以协助集成,但受信任的主机代码或人员必须批准对工作负荷权限的更改。

后续步骤