Aller au contenu

Astreinte et réponse à incident : 01 88 73 28 39. Munissez-vous de votre code PIN client.

Wargan
Avis de sécurité

Amazon Pay SDK Payment notification forgery via unvalidated SigningCertURL

High
Identifiers
None
Written
2015-11-03
Published
2015-11-03
Severity
High
Affected versions
amzpayments module for PrestaShop before the 2015-11-13 release (Amazon Pay PHP SDK, OffAmazonPaymentsNotifications)
Type
signature verification bypass (attacker-controlled signing certificate URL)
Status
fixed in the amzpayments release of 2015-11-13 (PR #14)

[0] Application

amzpayments is the official "Login and Pay with Amazon" module for PrestaShop, published by PrestaShop itself, and it embeds Amazon's own PHP SDK (OffAmazonPayments and OffAmazonPaymentsNotifications) to talk to Amazon Pay. The payment events (authorization, capture, refund…) do not transit through the customer's browser, Amazon pushes them to the shop as SNS notifications on the module's ipn.php endpoint, and the shop updates the state of its orders from what it receives there.

We found this while reviewing the module in October 2015. The problem is not in the module but in the SDK's message validator, so any integration built on the same SDK code validated notifications the same way.

[I] The vulnerability

A) Overview

A notification is considered as coming from Amazon when its signature verifies against the certificate found at the URL given in the notification itself, and nothing restricts what that URL can be. So an attacker can sign a notification with a certificate of their own, host it wherever they want, and the shop will accept the notification as authentic, f.e. a payment authorization for an order the attacker never paid.

B) Required data

Nothing really. The URL of the shop's ipn.php (a fixed path in the module) and the format of a notification, which Amazon documents publicly.

C) Explanation

When an SNS notification reaches ipn.php, its body looks like this:

{
  "Type" : "Notification",
  "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Subject" : "test",
  "Message" : "{\"Timestamp\":\"Timestamp\",\"ReleaseEnvironment\":\"ReleaseEnvironment\",\"NotificationReferenceId\":\"NotificationReferenceId\",\"NotificationData\":\"XML DATA HERE\"}",
  "Timestamp" : "2012-04-25T21:49:25.719Z",
  "SignatureVersion" : "1",
  "Signature" : "valid-amazon-signature",
  "SigningCertURL" : "http://amazon.server/cert.pem"
}

(format documented at https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html)

As you can see, there are two important fields here, Signature and SigningCertURL. The signature is computed with openssl_sign() over the fields Timestamp, Message, Subject… using the private key of the certificate published at SigningCertURL (so the attacker does not know this private key, that part is fine).

When the notification reaches ipn.php, the module checks that the message is signed (ipn.php:35) by handing it to the SDK's validator, and the validator verifies the signature of the other fields with the certificate it downloads from SigningCertURL (SnsMessageValidator.php:133).

There is no control at all on SigningCertURL. The only thing checked on the certificate is that its common name is sns.amazon.com (OpenSslVerifySignature.php:138), which is not a security check since anyone can put that name in a self-signed certificate.

D) Exploitation

  1. Generate a self-signed PEM certificate with the common name sns.amazon.com.
  2. Create a notification request.
  3. Sign it with the private key of the certificate generated in 1.
  4. Host the certificate somewhere on the internet (http://example.com/attacker.pem).
  5. Send the notification to ipn.php:
{
  "Type" : "Notification",
  "MessageId" : "hacker-notification",
  "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic",
  "Subject" : "test",
  "Message" : "{\"Timestamp\":\"Timestamp\",\"ReleaseEnvironment\":\"ReleaseEnvironment\",\"NotificationReferenceId\":\"NotificationReferenceId\",\"NotificationData\":\"XML DATA HERE\"}",
  "Timestamp" : "2012-04-25T21:49:25.719Z",
  "SignatureVersion" : "1",
  "Signature" : "valid-hacker-signature",
  "SigningCertURL" : "http://example.com/hacker.pem"
}

The validator downloads the attacker's certificate, finds the expected common name in it, verifies the signature with it (which works, we signed with the matching key) and accepts the notification.

E) Consequences

Anyone on the internet can tell a shop that an order was authorized or captured. With a NotificationData payload matching an order the attacker placed themselves, the shop marks it as paid without any money changing hands, and the attacker never had to talk to Amazon at any point.

Since the flaw is in the SDK, every merchant using the module was exposed, and so was any other integration built on the same validator code, which is why we reported it as an Amazon API issue and not as a PrestaShop one.

[II] Mitigation

1. Restrict SigningCertURL to Amazon's SNS endpoints before fetching anything. This is what the fix does (PR #14, commit d49edd9). The validator now rejects any certificate URL whose host does not match sns.<region>.amazonaws.com (or .amazonaws.com.cn), using the following pattern

^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$

The current Amazon Pay PHP SDK (IpnHandler.php) carries the same check.

2. Do not treat a certificate's common name as a trust decision. A name in a certificate proves nothing by itself, it has to chain to a trusted authority or come from a location you trust, otherwise anyone can write whatever they want in it, as we did here.

[III] Timeline

2015/10/28: Security review of the amzpayments module, issue opened with PrestaShop
2015/11/03: The flaw in the SDK's validator is identified and reported in the same issue
2015/11/09: Fix proposed by the module's author (PR #14)
2015/11/13: Fix reviewed, merged and released