52
Page | 1 © 2017 Magento, Inc. First Steps to Building Secure Magento Extensions https://tale.sh/MLIN17

First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 1© 2017 Magento, Inc.

First Steps to Building

Secure Magento

Extensions

https://tale.sh/MLIN17

Page 2: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 2© 2017 Magento, Inc.

Talesh SeeparsanCTO

Bit79

Page 3: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

There is no such thing as

an unhackable site

Page 4: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

You just need to be able to run faster

than your friends

Page 5: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions
Page 6: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 6© 2017 Magento, Inc.

PART 1

• Lower level tools and strategies

• Useful for building a single

extension

• Use during SDLC

PART 2

• Architecture level

• Useful for planning an entire site

build

• Useful for securing live sites

Page 7: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 7© 2017 Magento, Inc.

• Don’t negatively affect team productivity.

• Let the computers do the work for us.

Theme for our strategies:

Page 8: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Part 1:

Securing at a code level

Page 9: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 9© 2017 Magento, Inc.

• Scans our code and flags dangerous parts

• ECG Ruleset understands Magento 2

• Comes with built in security scans

Tool #1 : Use PHPCS

Page 10: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 10© 2017 Magento, Inc.

$ composer require \

magento-ecg/coding-standard

Page 11: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 11© 2017 Magento, Inc.

$ phpcs --config-set installed_paths /

./vendor/magento-ecg/coding-standard

Page 12: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 12© 2017 Magento, Inc.

$ phpcs --standard=EcgM2 /path/to/code

Page 13: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 13© 2017 Magento, Inc.

Page 14: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 14© 2017 Magento, Inc.

Page 15: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 15© 2017 Magento, Inc.

• Run as a git/svn hook automatically

PHPCS Best Practices

Page 16: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 16© 2017 Magento, Inc.

• Fix any issues raised immediately

PHPCS Best Practices

Page 17: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 17© 2017 Magento, Inc.

• Scans inputs instead of code

• Used by Magento HQ

• Industry standard

Tool #2 : OWASP ZAP

Page 18: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 18© 2017 Magento, Inc.

<script>alert(document.cookie);</script>

XSS attack string

Page 19: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 19© 2017 Magento, Inc.

&lt;IMG

SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#000009

9&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#

0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#000

0088&#0000083&#0000083&#0000039&#0000041&gt;

XSS attack string

Page 20: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 20© 2017 Magento, Inc.

OWASP GUI

Page 21: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 21© 2017 Magento, Inc.

• http://tale.sh/owasp-zap-demo

OWASP ZAP Demo

Page 22: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 22© 2017 Magento, Inc.

• Let it run overnight/over the weekend, working while you

sleep.

OWASP ZAP Best Practices

Page 23: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 23© 2017 Magento, Inc.

• Create tickets in Asana/Jira for each problem it finds.

OWASP ZAP Best Practices

Page 24: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions
Page 25: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 25© 2017 Magento, Inc.

• And how/when to use them

Builtin Magento 2 security features

Page 26: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 26© 2017 Magento, Inc.

Use the Magento 2 ORM

• Handcoded SQL queries

• Robust framework that facilitates Server side input validation

Defends against Injection and XSS

26

Page 27: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 27© 2017 Magento, Inc.

The Magento 2 Escaper

• Implementation: /lib/internal/Magento/Framework/Escaper.php

– Usage: <?php echo $this->escapeHtml(__($this->variable); ?>

Defends against XSS

27

Page 28: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 28© 2017 Magento, Inc.

CSRF Defense : Anti Forgery Tokens

<?php echo $this->getBlockHtml('formkey')?>

Defends against CSRF

28

Page 29: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 29© 2017 Magento, Inc.

CSRF explanation

• Trick an authenticated user to POST information on your site

POST

29

Page 30: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 30© 2017 Magento, Inc.

CSRF Defense : Anti Forgery Tokens

<?php echo $this->getBlockHtml('formkey')?>

Defends against CSRF

30

Page 31: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 31© 2017 Magento, Inc.

CSRF Defense : Anti Forgery Tokens

Defends against CSRF

31

Page 32: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 32© 2017 Magento, Inc.

Pay attention to cookie permissions

• “HttpOnly” flag is set on some important cookies eg: • “admin” cookie

• “PHPSESSID” cookie

• “X-Magento-Vary” cookie

Defends against Broken Authentication

32

• “Secure” flag is set on some important cookies eg: • “admin” cookie

• “X-Magento-Vary” cookie

Page 33: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 33© 2017 Magento, Inc.

Rely on the CustomerSession Object

public function __construct(

Context $context,

CustomerSession $customerSession

) {

parent::__construct($context, $customerSession);

}

33

Defends against risks: Insecure Direct object referencesMissing function Access control

Page 34: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 34© 2017 Magento, Inc.

Don’t roll your own Crypto!

<field id="password" translate="label" type="obscure" showInStore="0“>

<label>Password</label>

<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>

</field>

34

Defends against risks: Security MisconfigurationsSensitive data exposureMissing function level access control

Page 35: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions
Page 36: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Part 2:

Securing at an architecture level

Page 37: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 37© 2017 Magento, Inc.

• Subscribe to https://magento.com/security

• Patch quickly, plan your time for patches

• Easiest way to get hacked

Patches

Page 38: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 38© 2017 Magento, Inc.

• No unecessary files there

• No DB backups

• No git/svn data

• No test files

• No file backups

• File permissions must be impeccable

• No unnecessary tools like Magmi

Production is sacrosanct

Page 39: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 39© 2017 Magento, Inc.

• https://github.com/gwillem/magento-malware-scanner

• wget git.io/mwscan.txt

• grep -Erlf mwscan.txt /path/to/magento

Magento Malware scanner

Page 40: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 40© 2017 Magento, Inc.

Promotes & facilitates secure Magento stores globally.

Magento Security Council

https://magesec.org

Page 41: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 41© 2017 Magento, Inc.

• https://www.magereport.com/

• https://magescan.com/

• Magento Security Scan from Magento Inc. (currently in Beta)

[email protected] (https://tale.sh/mss-beta)

External Site scanners

Page 42: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 42© 2017 Magento, Inc.

• Use the randomly generated one in Magento 2

• Generate your own in Magento 1

• Don’t use /admin /console /backoffice or anything similar

• Consider limiting access via IP Whitelist or even VPN

Keep your Admin URL random

Page 43: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 43© 2017 Magento, Inc.

• https://github.com/magento-hackathon/Magento-Two-factor-Authentication

• https://github.com/nexcess/magento-sentry-two-factor-authentication

2FA for your admin URL

Page 44: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 44© 2017 Magento, Inc.

• Upload your composer.lock file on https://security.sensiolabs.org/

• php checker security:check /path/to/composer.lock

Check your composer for known vulnerabilities

Page 45: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 45© 2017 Magento, Inc.

• https://bitbucket.org/creaminternet/module-

securepasswords

Stronger password hashing

Page 46: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions
Page 47: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 47© 2017 Magento, Inc.

PROCESS > TOOLS

Page 48: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 48© 2017 Magento, Inc.

PEOPLE > PROCESS > TOOLS

Page 49: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions
Page 50: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 50© 2017 Magento, Inc.

http://github.com/talesh/response

Page 51: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions

Page | 51© 2017 Magento, Inc.

धन्यवाद

Thank you

https://tale.sh/MLIN17@_Talesh

Page 52: First Steps to Building Secure Magento Extensions 2017_BO V_… · © 2017 Magento, Inc. Page | 1 First Steps to Building Secure Magento Extensions