PHP Composer漏洞可能引发供应链攻击

安全 漏洞
​研究人员在PHP Composer中发现一个安全漏洞,攻击者利用该漏洞可以发起供应链攻击。

Composer是PHP中管理和安全软件依赖的主要工具,被开发团队广泛应用于更新过程等。因此,Composer使用名为Packagist 的在线服务来确定包下载供应链的正确性。而Packagist每个月的下载请求在14亿次左右。

研究人员在进行安全研究时,在 Packagist使用的Composer源码中发现了一个严重的安全漏洞,漏洞CVE编号为CVE-2021-29472。攻击者利用该漏洞可以在Packagist.org 服务器上执行任意系统命令。此外,攻击者还可以进一步窃取维护者凭证,或将包下载重定向到传播后门依赖的第三方服务器。

漏洞分析

在请求下载包时,Composer 首先会查询Packagist来获取元数据。元数据中包含2个获取代码源的域source和dist。Source只想开发库,dist只想预构建的库。Composer在从库中下载代码时会使用外部系统命令来避免重新实现针对每隔版本控制软件的逻辑。因此,这些调用都是用wrapper ProcessExecutor来执行的:

  1. composer/src/Composer/Util/ProcessExecutor.php 
  2.   
  3. use Symfony\Component\Process\Process; 
  4. // [...] 
  5. class ProcessExecutor 
  6.     // [...] 
  7.     public function execute($command, &$output = null, $cwd = null
  8.     { 
  9.         if (func_num_args() > 1) { 
  10.             return $this->doExecute($command, $cwd, false, $output); 
  11.         } 
  12.         return $this->doExecute($command, $cwd, false); 
  13.     } 
  14.     // [...] 
  15.     private function doExecute($command, $cwd, $tty, &$output = null
  16.     { 
  17.         // [...] 
  18.         if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) { 
  19.             // [1] 
  20.             $process = Process::fromShellCommandline($command, $cwd, null, null, static::getTimeout()); 
  21.         } else { 
  22.             // [2] 
  23.             $process = new Process($command, $cwd, null, null, static::getTimeout()); 
  24.         } 
  25.         if (!Platform::isWindows() && $tty) { 
  26.             try { 
  27.                 $process->setTty(true); 
  28.             } catch (RuntimeException $e) { 
  29.                 // ignore TTY enabling errors 
  30.             } 
  31.         } 
  32.         $callback = is_callable($output) ? $output : array($this, 'outputHandler'); 
  33.         $process->run($callback); 

在 [1]和[2]中,可以看到参数 $command 是在shell中执行的。大多数的ProcessExecutor 调用都是在版本控制软件驱动中执行的,版本控制软件负载原创和本地库的所有操作。比如,在Git驱动中:

  1. composer/src/Composer/Repository/Vcs/GitDriver.php 
  2.   
  3. public static function supports(IOInterface $io, Config $config, $url, $deep = false
  4.     if (preg_match('#(^git://|\.git/?$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) { 
  5.         return true; 
  6.     } 
  7.     // [...] 
  8.     try { 
  9.         $gitUtil->runCommand(function ($url) { 
  10.             return 'git ls-remote --heads ' . ProcessExecutor::escape($url); // [1] 
  11.         }, $url, sys_get_temp_dir()); 
  12.     } catch (\RuntimeException $e) { 
  13.         return false; 
  14.     } 

使用ProcessExecutor::escape() 可以将参数$url 逃逸以预防子命令($(...), `...`) ,但是无法预防用户提供(--)开头的值,只要加上其他的参数就可以成为最终的命令。这类漏洞就叫做参数注入。

类似的有漏洞的模式也出现在其他驱动中,用户控制的数据可以成功绕过检查并连接在一起成为系统命令:

  1. composer/src/Composer/Repository/Vcs/SvnDriver.php 
  2. public static function supports(IOInterface $io, Config $config, $url, $deep = false
  3.     $url = self::normalizeUrl($url); 
  4.     if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) { 
  5.         return true; 
  6.     } 
  7.     // [...] 
  8.     $process = new ProcessExecutor($io); 
  9.     $exit = $process->execute( 
  10.         "svn info --non-interactive ".ProcessExecutor::escape($url), 
  11.         $ignoredOutput 
  12.     ); 
  13. composer/src/Composer/Repository/Vcs/HgDriver.php 
  14. public static function supports(IOInterface $io, Config $config, $url, $deep = false
  15.     if (preg_match('#(^(?:https?|ssh)://(?:[^@]+@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) { 
  16.         return true; 
  17.     } 
  18.     // [...] 
  19.     $process = new ProcessExecutor($io); 
  20.     $exit = $process->execute(sprintf('hg identify %s', ProcessExecutor::escape($url)), $ignored); 
  21.     return $exit === 0; 

更多技术细节参见:https://blog.sonarsource.com/php-supply-chain-attack-on-composer

补丁

研究人员将该漏洞提交给Packagist团队后,该团队快速反应,在12个小时内就部署了安全补丁。

本文翻译自:https://blog.sonarsource.com/php-supply-chain-attack-on-composer

 

责任编辑:赵宁宁 来源: 嘶吼网
相关推荐

2023-07-19 12:04:55

2021-11-23 14:54:05

漏洞供应链攻击网络攻击

2022-04-06 10:12:51

Go供应链攻击风险

2021-04-25 15:49:06

拜登黑客攻击

2017-11-08 09:39:11

供应链消费升级CIO

2022-04-13 14:49:59

安全供应链Go

2023-02-23 07:52:20

2021-09-12 14:38:41

SolarWinds供应链攻击Autodesk

2020-12-24 11:09:44

VMwareCiscoSolarWinds

2020-06-01 08:45:17

GitHub代码开发者

2021-09-16 14:59:18

供应链攻击漏洞网络攻击

2022-03-14 14:37:53

网络攻击供应链攻击漏洞

2021-10-14 13:14:12

安全供应链漏洞威胁

2024-01-19 21:51:42

2023-11-06 07:11:14

2022-02-21 10:12:20

供应链攻击网络攻击

2023-01-11 00:05:58

2020-12-31 11:02:47

网络钓鱼漏洞攻击

2023-11-02 12:13:08

2022-07-18 17:00:00

网络安全数据供应链
点赞
收藏

51CTO技术栈公众号