2008-11-27 118 views

回答

10

runkit,但是如果您不需要主進程和子進程之間的任何交互,則可以通過在命令行上調用腳本(使用shell_exec)更簡單。

+2

關於runkit;它似乎不是通過描述很好的沙盒,或者我應該說容易。 你可以禁止函數,但我寧願禁止除提供列表中的所有函數。如果用戶需要功能,我可以根據要求手動評估其安全性。 這似乎是唯一的方法是寫一個自定義的解釋器。如果速度是一個問題,你可以把它轉換成PHP或其他語言,這實際上是我現在要做的,因爲我找不到現成的解決方案。 乾杯! Ps。是的,我看到這個Q很老了。 DS。 – Frank 2010-06-14 15:38:55

+0

好吧..祝你好運,找到可以解析PHP到AST的東西;)我同意你的觀點。 – troelskn 2010-06-14 17:27:57

+0

FYI runkit似乎被放棄了,如果您想在現代版本的PHP上運行它,您可能需要編譯CVS版本或其中一個修補版本(http://github.com/tricky/runkit) – Eli 2010-07-21 14:21:57

2

此外,你應該看看backtick operator

$sOutput = `php script_to_run.php`; 

這將允許你檢查從正在運行的腳本的輸出。但是請注意,腳本將以您擁有的權限運行,但您可以通過在Linux上使用sudo來繞過此腳本。

此方法還假定您已安裝PHP CLI,但並非總是如此。

1

Runkit_Sandbox - 你可能會得到它的工作,這是一個PHP擴展。我會說要走的路。

但是,您可能需要創建自己的「沙箱」,例如,通過重置您使用的全球變量的全局變量狀態。

class SandboxState 
{ 
    private $members = array('_GET', '_POST'); 
    private $store = array(); 
    public function save() { 
     foreach($members as $name) { 
      $this->store[$name] = $$name; 
      $$name = NULL; 
     } 
    } 
    public function restore() { 
     foreach($members as $name) { 
      $$name = $this->store[$name]; 
      $this->store[$name] = NULL; 
     } 

    } 
} 

用法:

$state = new SanddboxState(); 
$state->save(); 

// compile your get/post request by setting the superglobals 
$_POST['submit'] = 'submit'; 
... 

// execute your script: 
$exec = function() { 
    include(func_get_arg(0))); 
}; 
$exec('script.php'); 

// check the outcome. 
... 

// restore your own global state: 
$state->restore(); 
0

我知道它不是100%的話題有關,但有人n__n

function require_sandbox($__file,$__params=null,$__output=true) { 

    /* original from http://stackoverflow.com/a/3850454/209797 */ 

    if($__params and is_array($__params)) 
    extract($__params); 

    ob_start(); 
    $__returned=require $__file; 
    $__contents=ob_get_contents(); 
    ob_end_clean(); 

    if($__output) 
    echo $__contents; 
    else 
    return $__returned; 

}; 
1

我開發了一個基於BSD授權的沙箱類這很可能有用目的。它利用PHPParser庫分析沙盒代碼,根據用戶可配置的白名單和黑名單對其進行檢查,並提供各種配置選項以及理智的默認設置。爲了您的需要,您可以輕鬆地重新定義在沙盒代碼中調用的類並將它們路由到不同的類。

該項目還包括一個沙箱工具包(只能在本地機器上使用!),可用於試驗沙盒設置,以及完整的手冊和API文檔。

https://github.com/fieryprophet/php-sandbox

1

動態插件功能的執行,允許加載的文件和功能,可以執行任何它想做,但是它只能接受和返回變量可以json_encode「版。

function proxyExternalFunction($fileName, $functionName, $args, $setupStatements = '') { 
    $output = array(); 
    $command = $setupStatements.";include('".addslashes($fileName)."');echo json_encode(".$functionName."("; 
    foreach ($args as $arg) { 
    $command .= "json_decode('".json_encode($arg)."',true),"; 
    } 
    if (count($args) > 0) { 
    $command[strlen($command)-1] = ")";//end of $functionName 
    } 
    $command .= ");";//end of json_encode 
    $command = "php -r ".escapeshellarg($command); 

    exec($command, $output); 
    $output = json_decode($output,true); 
} 

外部代碼完全沙盒,你可以通過做sudo -u restricedUser php -r ...應用所需的任何權限限制。

相關問題