Exploit-DB #42309
Description: Pelco Sarix/Spectra Cameras - Remote Code Execution
Type: webapps
Platform: hardware
Date: 2017-07-10
Author: LiquidWorm

Exploit:
Schneider Electric Pelco Sarix/Spectra Cameras Root Remote Code Execution


Vendor: Schneider Electric SE
Product web page: https://www.pelco.com
Affected version: Sarix Enhanced - Model: IME219 (Firmware: 2.1.2.0.8280-A0.0)
                  Sarix Enhanced - Model: IME119 (Firmware: 2.1.2.0.8280-A0.0)
                  Sarix - Model: D5230 (Firmware: 1.9.2.23-20141118-1.9330-A1.10722)
                  Sarix - Model: ID10DN (Firmware: 1.8.2.18-20121109-1.9110-O3.8503)
                  Spectra Enhanced - Model: D6230 (Firmware: 2.2.0.5.9340-A0.0)

Summary: Pelco offers the broadest selection of IP cameras designed
for security surveillance in a wide variety of commercial and industrial
settings. From our industry-leading fixed and high-speed IP cameras to
panoramic, thermal imaging, explosionproof and more, we offer a camera
for any environment, any lighting condition and any application.
When nothing but the best will do. Sarix™ Enhanced Range cameras
provide the most robust feature-set for your mission-critical applications.
With SureVision™ 3.0, Sarix Enhanced delivers the best possible image
in difficult lighting conditions such as a combination of bright areas,
shaded areas, and intense light. Designed with superior reliability,
fault tolerance, and processing speed, these rugged fixed IP cameras
ensure you always get the video that you need.

Desc: The affected cameras suffer from authenticated remote code
execution vulnerability. The POST parameter 'enable_leds' located
in the update() function called via the GeneralSetupController.php
script is not properly sanitised before being used in writeLedConfig()
function to enable led state to on or off. A remote attacker can
exploit this issue and execute arbitrary system commands granting
her system access with root privileges using a specially crafted
request and escape sequence to system shell.


---------------------------------------------------------------------------
/var/www/core/setup/controllers/GeneralSetupController.php:
-----------------------------------------------------------

43: public function update() {
44:         $errOccurred = false;
45:         $logoreboot = false;
46: 
47:         // If can update general settings
48:         if ($this->_context->_user->hasPermission("{51510980-768b-4b26-a44a-2ae49f308184}")) {
49:
50:             $errors = $this->validateInputs("setup", "general.invalid");
51:
52: //
53:             $new_logo_path;
54:             if (empty($errors) && (strlen($_FILES["new_logo_path"]["name"]) > 0)) {
55:                 // The user has provided a file to load in as an image.  Verify that the file is ok.
56:                 $errors = $this->storeBmpFileIfValid($new_logo_path, $width, $height);
57:             } else {
58:                 // In this case, get the width and height from the omons settings
59:                 $width = intval($this->_conf->get("Video/Overlay", "LogoWidth"));
60:                 $height = intval($this->_conf->get("Video/Overlay", "LogoHeight"));
61:             }
62: //
63:             if (empty($errors)) {
64:                 $device_name = $_POST["device_name"];
65:
66:                 $this->_conf->set("Device", "FriendlyName", $device_name);
67:
68:                 // update smtp server; append port 25 if it's not provided by the user
69:                 $smtpServer = $_POST["smtp_server"];
70:
71:                 if ((! empty($smtpServer)) && preg_match(self::kHostPortRegex, $smtpServer) == 0) {
72:                     $smtpServer .= ":" . self::kDefaultSmtpPort;
73:                 }
74:
75:                 $this->_conf->set("Networking", "SmtpServer", $smtpServer);
76:
77: //
78:                 $success = $this->writeLedConfig($_POST["enable_leds"]);
79: //
80:             } else {
81:                 $this->_context->setError("phobos", "validation.failure");
82:                 $this->_context->setErrorList($errors);
83:
84:                 $errOccurred = true;
85:             }
86:         }

...
...
...

Bonus hint: When uploading a bmp logo, you can modify the width offset for example and inject persistent code:
--
-> 12h: 00 01 00 00 ; width (max 0x100, min 0x20)
--
191:             if ($logoOverlay) {
192:                 if($logoreboot) {
193:                     $cmd = "/usr/bin/overlayLogo " . $logo_justification . " " . $logo_row . " " . $width  . " " .  $height . " 0";
194:                     exec($cmd);
195:                 }
196:             } else {                
197:                 $cmd = "/usr/bin/overlayLogo 1 1 1 1 1";
198:                 exec($cmd);
199:             }                

...
...
...

265:         $vparams["enable_leds"] = $this->getLedConfig();
266: //
267:         $vparams["device_name"]  = $this->_conf->get("Device", "FriendlyName");
268:         $vparams["TimeFormat"] = $this->_conf->get("Video/Overlay", "TimeFormat");        
269:         $vparams["date_formats"] = $this->getDateFormats();
270:         $vparams["selectedDateFormat"] = $this->_conf->get("Video/Overlay", "DateFormat");
271:
272:         ob_start();
273:         passthru("date +\"" . $vparams["TimeFormat"] . "\"");
274:         $vparams["current_time"] = trim(ob_get_contents());
275:         ob_end_clean();

...
...
...

630:     /** @param $state string "on" or "off" */
631:     protected function writeLedConfig($state) {
632:         $encoded = array('type' => 'uint32',
633:             'value' => ($state == 'on' ? 1 : 0));
634:
635:         $rest = $this->getRestProxy();
636:         $params = array(array('type' => 'uint32', 'value' => 10), $encoded);
637:         $response = $rest->GetWithPayload('/internal/msgbus/com.pelco.hardware.led/SetState?',
638:                                'application/json',
639:                                $params);
640:
641:         return ($response->GetStatus() == 200);
642:     }