security testing with owasp zap in ci/cd - simon bennetts - codemotion amsterdam 2017

44
Security Testing with OWASP ZAP in CI/CD Simon Bennetts - @psiinon AMSTERDAM 16 - 17 MAY 2017

Upload: codemotion

Post on 21-Jan-2018

677 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Security Testing with OWASP ZAP in CI/CDSimon Bennetts - @psiinon

AMSTERDAM 16 - 17 MAY 2017

Page 2: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

The Plan

• What are we trying to solve?

• What can you get out of this?

• Introduction to ZAP

• Where to start

• Where to go from there

2

Page 3: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

What are we trying to solve?

• Find security issues as early as possible

• Integration into the devops pipeline

• Finding all of the possible vulnerabilities

• Putting pentesters out of a job :P

3

What are we not trying to solve?

Page 4: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

What can you get out of this?

• A way to quickly evaluate your apps

• Options for more thorough scanning

• An introduction to the ZAP API

4

Page 5: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

5

ZAP Introduction• A tool for finding web app vulnerabilities

• One of the worlds most popular free security tools

• Completely free and open source

• OWASP Flagship project

• Ideal for people new to security

• But also used by security professionals

• Ideal for devs, esp. for automated security tests

• Not a silver bullet!

Page 6: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

6

ZAP Features• Swing based UI for desktop mode

• Comprehensive API for daemon mode

• Plugin architecture (add-ons)

• Online ‘marketplace’ (all free:)

• Release, beta and alpha quality add-ons

• Traditional and ajax spiders

• Passive and active scanning

• Highly configurable, eg scan policies

• Highly scriptable

Page 7: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Some ZAP use cases

• Point and shoot – the Quick Start tab

• Proxying via ZAP, and then scanning

• Manual pentesting

• Automated security regression tests

• Debugging

• Part of a larger security program

7

Page 8: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

ZAP Install Options• Windows, Linux and Mac OS Installers

• Linux packages, Mac OS Homebrew Cask

• Cross Platform zip

• Docker Images

• owasp/zap2docker-stable

• owasp/zap2docker-weekly

• owasp/zap2docker-live

• Distros like Kali8

Page 9: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Where to start?

• The Baseline scan

• Completely safe

• Runs quickly (1-2 minutes?)

• Can be easily integrated into CI/CD

• Easy to get started – just required the target:

• Very configurable if needed9

• docker pull owasp/zap2docker-weekly

• docker run -t owasp/zap2docker-weekly \ zap-baseline.py -t https://www.example.com

Page 10: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Baseline scan

• Uses docker (the only dependency)

• Time limited spider of target (default 1 min)

• Just passive scanning

• By default warns on all issues

• Can change to ignore, info or fail

• Can include any ZAP cmdline option

• Can ignore any url regex for any rule

10

Page 11: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Baseline scan - issues

• All release and beta passive scan rules, eg

• Missing / incorrect security headers

• Cookie problems

• Information / error disclosure

• Missing CSRF tokens

• ...

• Can optionally include alpha pscan rules

11

Page 12: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Baseline scan – usage

12

Usage: zap-baseline.py -t <target> [options] -t target target URL including the protocol, eg https://www.example.comOptions: -c config_file config file to use to INFO, IGNORE or FAIL warnings -u config_url URL of config file to use to INFO, IGNORE or FAIL warnings -g gen_file generate default config file (all rules set to WARN) -m mins the number of minutes to spider for (default 1) -r report_html file to write the full ZAP HTML report -w report_md file to write the full ZAP Wiki (Markdown) report -x report_xml file to write the full ZAP XML report -a include the alpha passive scan rules as well -d show debug messages -i default rules not in the config file to INFO -j use the Ajax spider in addition to the traditional one -l level minimum level to show: PASS, IGNORE, INFO, WARN or FAIL, use with -s to hide example URLs -s short output format - dont show PASSes or example URLs -z zap_options ZAP command line options e.g. -z "-config aaa=bbb -config ccc=ddd"

Page 13: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Baseline scan – output

14

./zap-baseline.py -t https://www.example.com3 URLsPASS: Cookie No HttpOnly Flag [10010]PASS: Cookie Without Secure Flag [10011]PASS: Password Autocomplete in Browser [10012]<snip>WARN: Incomplete or No Cache-control and Pragma HTTP Header Set [10015] x 1

https://www.example.comWARN: Web Browser XSS Protection Not Enabled [10016] x 3

https://www.example.comhttps://www.example.com/robots.txthttps://www.example.com/sitemap.xml

WARN: X-Frame-Options Header Not Set [10020] x 1 https://www.example.com

WARN: X-Content-Type-Options Header Missing [10021] x 1 https://www.example.com

FAIL: 0 WARN: 4 INFO: 0 IGNORE: 0 PASS: 22

Page 14: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Baseline scan – conf file

• Use -g option to generate, -c or -u to use

15

# zap-baseline rule configuration file# Change WARN to IGNORE to ignore rule or FAIL to fail if rule matches# Only the rule identifiers are used - the names are just for info# You can add your own messages to each rule by appending them after a10010 WARN(Cookie No HttpOnly Flag)10011 WARN(Cookie Without Secure Flag)10012 WARN(Password Autocomplete in Browser)10015 WARN(Incomplete or No Cache-control and Pragma HTTP Header Set)10016 WARN(Web Browser XSS Protection Not Enabled)10017 WARN(Cross-Domain JavaScript Source File Inclusion)10019 WARN(Content-Type Header Missing)10020 WARN(X-Frame-Options Header Scanner)10021 WARN(X-Content-Type-Options Header Missing)10023 WARN(Information Disclosure - Debug Error Messages)10024 WARN(Information Disclosure - Sensitive Information in URL)10025 WARN(Information Disclosure - Sensitive Information in HTTP Referrer Header)

Page 15: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Where next?

• Mass Baseline scan

• Provides a simple dashboard

• Shows the detailed results

• Shows the per service history

16

Page 16: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Mass Baseline scan• Part of the community-scripts repo:

zaproxy/community-scripts/api/mass-baseline

17

Page 17: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Full Scans• Packaged options:

• Cmdline quick scan

• Jenkins plugin

• Sdlc-integration scripts

• Scripted API scan (coming soon)

• Daemon mode + API

• (ZAP as a Service – in development)

18

Page 18: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Cmdline Quick Scan

19

./zap.sh -cmd -quickurl \ http://example.com/ -quickprogress

• Spidering

• Active scanning

• [====================] 100%

• Attack complete

• <?xml version="1.0"?><OWASPZAPReport version="2.5.0" generated="Tue, 4 Oct 2016 09:31:53">

• <site name="http://example.com" ...

Page 19: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Official Jenkins plugin

Page 20: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Official Jenkins plugin• https://wiki.jenkins-ci.org/display/JENKINS/zap+plugin

• Maintained by the ZAP core team

• Supports authentication, scan policies, Jira integration

• Dedicated User Group: https://groups.google.com/group/zaproxy-jenkins

• Supports ZAP 2.6.0 +

21

Page 21: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Sdlc integration scripts

• Part of the community-scripts repo:zaproxy/community-scripts/api/sdlc-integration

• Spidering, passive and active scanning

• Supports authentication

• Supports JIRA integration

• Linux only, requires some file editing

22

Page 22: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Useful cmdline options

• Turn off db recovery (speeds things up)-config database.recoverylog=false

• Update all add-ons-addonupdate

• Install a non default add-on-addoninstall addonname

• Setting the API key-config api.key=j8WdOEq8dhwWE24VGDsreP

• Disable API key in a safe environment-config api.disablekey=true

23

Page 23: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Using the ZAP API

• Intro to the API

• Exploring

• Scanning

• Reporting

• Authenticating

• Tuning

24

Page 24: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Intro to the API

• RESTish – ok, only uses GET/POST requests

http(s)://zap/<format>/<component>/ <operation>/<op name>[/?<params>]

• Maps closely to the UI / code

• Theres a v basic (but complete) web UI for it

• And clients in various langs:

Java, Python, Node JS, .Net, PHP, Go …

• Clients are generated from the code

25

Page 25: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

API UI – Top Level

Page 26: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

API Pro Tips

1. Experiment with the Desktop UI

2. Export configs from the UI (contexts, scan policies..)

3. Then reproduce using the API UI

4. Finally convert to a script

27

Page 27: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Intro – Python API

• Install from pypi:

pip install python-owasp-zap-v2.4

• In your script:

from zapv2 import ZAPv2

zap = ZAPv2()

zap = ZAPv2(proxies={ 'http': 'http://localhost:8080', 'https': 'http://localhost:8090'})

28

h

from zapv2 import ZAPv2

zap = ZAPv2( apikey='mysupersecretkey', proxies={ 'http': 'http://localhost:8090', 'https': 'http://localhost:8090'})

• zap.urlopen(target)

• pip install python-owasp-zap-v2.4

Page 28: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Exploring

• Proxy Regression / Unit tests

• Traditional Spider (crawler)

• Ajax Spider (browsers)

• Spider SOAP definition (via alpha add-on)

• Spider Swagger/ OpenAPI definition (via alpha add-on)

• Import ModSecurity2 logs (via alpha add-on)

29

Page 29: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Spider Desktop and API UIs

Page 30: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Spider Desktop and API UIs

Page 31: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Exploring – Trad Spider

32

h

id = zap.spider.scan(target)

• time.sleep(5)

• while int(zap.spider.status(id)) < 100:

• print ('Spider progress %: ' + zap.spider.status(id))

• time.sleep(5)

• print ('Spider completed')

Page 32: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Exploring – Ajax Spider

33

h

id = zap.ajaxSpider.scan(target)

• time.sleep(5)

• while zap.ajaxSpider.status(id) == 'running':

• print ('Ajax Spider # results: ' + zap.ajaxSpider.number_of_results(id))

• time.sleep(5)

• print ('Ajax Spider completed')

Page 33: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Scanning – Passive Scan

34

while int(zap.pscan.records_to_scan) > 0:

• print ('Pscan records : ' + zap.pscan.records_to_scan)

• time.sleep(5)

• print ('Pscan completed')

h

• Passive scanning happens automatically when proxying

• To tell when its finished:

Page 34: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Scanning – Active Scan

35

h

id = zap.ascan.scan(target)

• time.sleep(5)

• while int(zap.ascan.status(id)) < 100:

• print ('Ascan progress %: ' + zap.ascan.status(id))

• time.sleep(5)

• print ('Ascan completed')

Page 35: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Reporting – HTML + XML

36

h

# HTML Report

• with open ('report.html', 'w') as f: f.write(zap.core.htmlreport())

# XML Report

• with open ('report.xml', 'w') as f: f.write(zap.core.xmlreport())

Page 36: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Reporting – all alert data

37

h

# Use paging for lots of alerts

• offset = 0; page = 5000

• alerts = zap.core.alerts('', offset, page)

• while len(alerts) > 0:

• for alert in alerts:

• # Do whatever you want with alert

• offset += page

• alerts = zap.core.alerts('', offset, page)

Page 37: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

And dont forget...

38

h

# Your work here is done...

• zap.core.shutdown()

Page 38: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Authenticating

• Authentication can be hard :(

• Simple form based auth should be ok

• Authentication scripts should be able to handle anything

• But if you have complex SSO or equiv you may want a simpler option in your test env

• Pro Top: use the UI to set authentication up!

39

Page 39: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Tuning - speed

• Spider time limits

• Data driven content

• Technology

• Active scan

• Scan rules

• Input vectors

• Attack strength

40

Page 40: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Tuning - feedback

• Active scan stats

• Response stats

• Authentication stats (alpha add-on)

• Statsd support

41

Page 41: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Tuning - accuracy

• Attack thresholds

• Rule configuration

– Forms that dont need CSRF tokens

– Increase timing attacks from 5 seconds

42

Page 42: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

And if you need help...

• ZAP Getting Started Guide

• ZAP User Guide

• ZAP User Group

• ZAP Developer Group

• ZAP wiki, includes links to videos

• irc.mozilla.com #websectools

43

Page 43: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Talk Summary

• Use the baseline scan for a quick security overview

• Use the mass baseline to create a dashboard

• Use the new Jenkins plugin for more depth

• Use the ZAP API for even more control

• If you need help, just ask :)

44

Page 44: Security Testing with OWASP ZAP in CI/CD - Simon Bennetts - Codemotion Amsterdam 2017

Question Timehttp://www.owasp.org/index.php/ZAP

AMSTERDAM 16 - 17 MAY 2017