Toj*_*oji 37 c++ google-chrome sandbox
有几种资源可以解释Chrome中的沙盒是如何工作的,以及它如何保护用户免受恶意代码的侵害.
Chromium博客
Chromium开发人员文档
沙箱常见问题解答
这很棒,我喜欢他们所拥有的以操作系统为中心的设计(有点像"操作系统可能知道如何更好地保护自己,所以我们让它"接近.)他们在几个地方也提到了sandbox本身被设计为不依赖于Chrome,而是或多或少独立,因此理论上只要程序的体系结构兼容,任何进程都可以进行沙盒化(沙盒代码必须作为自己的进程运行,因为它是一个孩子的非沙盒父母.)
我碰巧有一个应用程序,它的设计使沙盒成熟,并且能够让父/子进程使用它.我有Chromium代码并且......不知道下一步该做什么.
有没有人在那里实际上用沙盒装了什么呢?是否有任何资源记录了它的用法或API?我想它应该很简单,但我不知道从哪里开始.
编辑:我的答案在下面找到!
Toj*_*oji 30
好的,这就是我发现的关于使用Chrome的沙盒代码的内容.
首先,您需要获得铬源代码.这很大,需要一段时间才能得到,但我还没有找到任何可靠的快捷方式来检查仍然可用的结果.Alos,非常严格按照该页面上的说明进行操作非常重要.Google工作人员知道他们正在做什么,并且不热衷于无用的步骤.该页面上的所有内容都是必要的.是.一切.
现在,一旦获得源代码,您实际上不必完全构建chrome(可能需要数小时!)才能使用沙箱.相反,他们已经足够好,可以为您提供单独的沙箱解决方案(可在沙箱文件夹中找到).构建此项目并确保所有内容都编译.如果确实如此,太棒了!如果没有,你没有按照构建页面上的步骤,是吗?羞愧地垂头丧气,这次实际去做.别担心,我等一下......
现在,所有内容都构建了您的主要兴趣点是sandbox_poc项目("poc"=概念证明).该项目基本上是围绕沙箱的最小GUI包装器,它将在沙盒环境中的给定入口点启动任意dll.它显示了创建和使用沙箱所需的所有步骤,并且是您获得的最佳参考.经常参考!
在查看代码时,您可能会注意到它实际上是沙箱的代码本身.这在所有沙盒示例中都很常见,并且根据此线程(可能已过时)可能是目前沙盒的唯一工作方式.该线程描述了理论上如何沙盒化一个单独的进程,但我还没有尝试过.但是,为了安全起见,拥有一个自我调用的应用程序是"已知的好"方法.
sandbox_proc包含许多静态库,但它们似乎主要用于它们构建的示例UI.我发现的唯一一个似乎是最小沙箱所需的是:
sandbox.lib base.lib dbghelp.lib
Run Code Online (Sandbox Code Playgroud)
虽然看到这个项目并不是完全显而易见的另一种依赖性,但这是我最长时间的追赶.构建沙箱解决方案时,其中一个输出文件应为" 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);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这足以让任何其他好奇的编码器沙盒!祝好运!
归档时间: |
|
查看次数: |
8765 次 |
最近记录: |