gpt4 book ai didi

c++ - 使用 Google Chrome 沙盒

转载 作者:IT老高 更新时间:2023-10-28 12:31:17 27 4
gpt4 key购买 nike

有几个资源可以解释 Chrome 中的沙盒是如何工作的,以及它如何保护用户免受恶意代码的侵害。

Chromium Blog
Chromium Developer Documentation
Sandbox FAQ

太好了,我喜欢他们采用的以操作系统为中心的设计(有点像“操作系统可能比我们知道如何更好地保护自己,所以我们让它”的方法。)他们还在几个地方提到沙盒本身被设计为不依赖于 Chrome 而是或多或少独立的地方,因此理论上任何进程都可以被沙盒化,只要程序的架构是兼容的(沙盒代码必须作为它自己的进程运行作为非沙盒父级的子级。)

我碰巧有一个应用程序,其设计使其适合沙盒,并且能够让父/子进程使用它。我有 Chromium 代码,但……不知道下一步该做什么。

有没有人真的用这个沙箱化了任何东西?是否有任何资源记录它的使用或 API?我想它应该很简单,但我不知道从哪里开始。

编辑:我在下面的答案中发现!

最佳答案

好的,这就是我发现的关于使用 Chrome 的沙盒代码的内容。

首先,您需要前往 get the chromium source code .这是,需要一段时间才能获得,但我还没有找到任何可靠的结帐快捷方式,仍然可以产生可用的结果。 Alos,非常重要的是,您必须非常密切地遵循该页面上的说明。 Google 工作人员知道他们在做什么,并且不热衷于无用的步骤。该页面上的所有内容都是必要的。是的。一切。

现在,一旦您获得了源代码,您实际上就不必完全构建 chrome(这可能需要数小时!)来使用沙盒。相反,它们已经很好地为您提供了一个可以独立构建的单独的沙盒解决方案(在沙盒文件夹中找到)。构建这个项目并确保一切都可以编译。如果是这样,那就太好了!如果没有,您没有按照构建页面上的步骤进行操作,对吗?羞愧地低下头,这次真的去做吧。别着急,我等着……

现在一切都建立了您的主要兴趣点是 sandbox_poc 项目(“poc” = 概念证明)。这个项目基本上是一个围绕沙箱的最小 GUI 包装器,它将在沙箱环境中的给定入口点启动任意 dll。它显示了创建和使用沙盒所需的所有步骤,并且是您获得的最佳引用。经常引用!

当您查看代码时,您可能会注意到它实际沙箱化的代码就是它自己。这在所有沙盒示例中很常见,according to this thread (可能已过时)可能是目前沙盒的唯一工作方式。该线程描述了理论上如何将一个单独的进程沙箱化,但我还没有尝试过。不过,为了安全起见,拥有一个自调用应用程序是“已知良好”的方法。

sandbox_proc 包含大量静态库,但它们似乎主要用于他们构建的示例 UI。我发现最小沙箱似乎需要的唯一条件是:

sandbox.lib base.lib dbghelp.lib

虽然从项目的角度来看,还有另一种依赖关系并不完全明显,但这是我了解时间最长的。当您构建沙盒解决方案时,其中一个输出文件应该是“wowhelper.exe”。虽然它从未在任何地方提及,但必须将该文件复制到与您正在沙盒化的可执行文件相同的目录中!如果不是,您对代码进行沙箱化的尝试将始终失败,并出现一般的“找不到文件”错误。如果您不知道发生了什么,这可能会非常令人沮丧!现在,我正在 Windows 7 64 位上进行开发,这可能与 wowhelper 要求有关(WOW 是 16/32/64 位之间互操作应用程序的常见首字母缩写词),但我没有很好的测试方法马上。如果其他人发现更多信息,请告诉我!

以上就是环境的全部内容,这里有一些简单的代码可以帮助您开始!请注意,虽然我在这里的子进程中使用了 wcout,但是在沙箱中运行时,您看不到任何控制台输出。任何类似的事情都需要通过 IPC 传达给父进程。

#include <sandbox/src/sandbox.h>
#include <sandbox/src/sandbox_factory.h>
#include <iostream>

using namespace std;

int RunParent(int argc, wchar_t* argv[], sandbox::BrokerServices* broker_service) {
if (0 != broker_service->Init()) {
wcout << L"Failed to initialize the BrokerServices object" << endl;
return 1;
}

PROCESS_INFORMATION pi;

sandbox::TargetPolicy* policy = broker_service->CreatePolicy();

// Here's where you set the security level of the sandbox. Doing a "goto definition" on any
// of these symbols usually gives you a good description of their usage and alternatives.
policy->SetJobLevel(sandbox::JOB_LOCKDOWN, 0);
policy->SetTokenLevel(sandbox::USER_RESTRICTED_SAME_ACCESS, sandbox::USER_LOCKDOWN);
policy->SetAlternateDesktop(true);
policy->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);

//Add additional rules here (ie: file access exceptions) like so:
policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, sandbox::TargetPolicy::FILES_ALLOW_ANY, "some/file/path");

sandbox::ResultCode result = broker_service->SpawnTarget(argv[0], GetCommandLineW(), policy, &pi);

policy->Release();
policy = NULL;

if (sandbox::SBOX_ALL_OK != result) {
wcout << L"Sandbox failed to launch with the following result: " << result << endl;
return 2;
}

// Just like CreateProcess, you need to close these yourself unless you need to reference them later
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);

broker_service->WaitForAllTargets();

return 0;
}

int RunChild(int argc, wchar_t* argv[]) {
sandbox::TargetServices* target_service = sandbox::SandboxFactory::GetTargetServices();

if (NULL == target_service) {
wcout << L"Failed to retrieve target service" << endl;
return 1;
}

if (sandbox::SBOX_ALL_OK != target_service->Init()) {
wcout << L"failed to initialize target service" << endl;
return 2;
}

// Do any "unsafe" initialization code here, sandbox isn't active yet

target_service->LowerToken(); // This locks down the sandbox

// Any code executed at this point is now sandboxed!

TryDoingSomethingBad();

return 0;
}

int wmain(int argc, wchar_t* argv[]) {
sandbox::BrokerServices* broker_service = sandbox::SandboxFactory::GetBrokerServices();

// A non-NULL broker_service means that we are not running the the sandbox,
// and are therefore the parent process
if(NULL != broker_service) {
return RunParent(argc, argv, broker_service);
} else {
return RunChild(argc, argv);
}
}

希望这足以让任何其他好奇的编码人员进入沙盒!祝你好运!

关于c++ - 使用 Google Chrome 沙盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1590337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com