Continuous Security: Security in the Continuous Delivery Pipeline is a series of articles addressing security concerns and testing in the Continuous Delivery pipeline. This is the first article in the series.
As the internet has matured and grown the need for stronger security has exceeded that pace. Penetration testing has become a large part of the development process. However, most developers do not know how to properly test their application for security or believe their operations team will handle this tedious task. Additionally, finding a severe security issue should raise a red flag and prevent that application release from seeing deployment.

Introduction

The concept of testing an application for security vulnerabilities sounds quite basic it can be a laborious task. Thankfully a number of tools exist that can make a good portion of this process automatic. While this may not replace a actual person physical attempting to crack your application it will reduce that person’s time by handling on the tedious day-to-day type vulnerability attacks.
What we’re going to look at is implementing these tools into a full CI/CD pipeline, so that our pipeline will fail if our application has any severe vulnerabilities. We will be discussing using OWASP ZAP for our pentest. For this first post we’ll delve into how to install ZAP manually as well as creating an automated method for provisioning ZAP using Ansible.
The Open Web Application Security Project (OWASP) Foundation is a not-for-profit organization that fosters open-source development of trusted tools. Zed Attack Proxy (ZAP) is an OWASP Foundation open-source project designed for web application security scanning.
While ZAP can be used manually, we will utilize its automated functionality for use in a CI/CD environment. ZAP does not need to run on the same server as the application or the script that will interact with ZAP for the penetration test. Here is an example interaction of these services:
ZAP-Basic-CI_CD-Flow - New Page (1)

Installing OWASP ZAP

ZAP can be run in a handful of different modes, from an intercepting proxy, to a spider and an automated scanner, among others. ZAP will require a persistent system to run from that is accessable from your CI/CD pipeline, a jenkins server or it’s own ec2 instance for example. These instructions assume there is a Linux server available for you to use.
ZAP requires Java 7 or higher, refer to your distribution’s instructions for installing Java or follow the Oracle provided instructions for installation.
With Java installed we will install ZAP into the ‘/opt’ directory from the archive available from the OWASP website <link: https://github.com/zaproxy/zaproxy/wiki/Downloads>

wget -q -O - https://github.com/zaproxy/zaproxy/releases/download/2.4.3/ZAP_2.4.3_Linux.tar.gz | tar zxf - -C /opt
ln -s /opt/ZAP_2.4.3 /opt/zap

OWASP ZAP does not include an init script, one can be downloaded and installed; please run the following commands as the root user:

wget -q -O /etc/init.d/zap https://raw.githubusercontent.com/stelligent/zap/master/packer/roles/zap/files/zap-init.sh
chmod 755 /etc/init.d/zap

The init script has a few default settings that may want to be changed prior to running. By default the ZAP daemon is configured to listen on all available network interfaces, on port 8080 and send it’s logs to ‘/var/log/zap.log’. These are variables defined at towards the top of the zap init, downloaded earlier. With everything in order, start ZAP:

/etc/init.d/zap start

If everything started successfully you will be able to hit the url ‘http://localhost:8080‘ if running locally; otherwise, replace ‘localhost’ with the IP or DNS address of your server. This will display the “Welcome to the OWASP Zed Attack Proxy (ZAP)” title page. If this does not display or ZAP failed to start from the init script earlier, check the logs in ‘/var/log/zap.log’ for details.
Lastly, we can configure ZAP to run at startup. This step is optional.

for rl in {2..5}; do ln -s /etc/init.d/zap /etc/rc${rl}.d/S93zap;done
for rl in 0 6; do ln -s /etc/init.d/zap /etc/rc${rl}.d/K5zap;done

Infrastructure as Code

If one of our goals is to achieve a full infrastructure as code, this can be accomplished with a configuration management system of your choice, such as Chef, Puppet or Ansible to provision ZAP. Additionally, if AWS is utilized, this can be baked into an AMI with Packer, which will be covered in a later post, allowing for fully automated provisioning.
Using Ansible in this example, we can provision ZAP to a remote server. If you’re unfamiliar with Ansible, it’s a python-based, open source platform for configuring and managing your systems. Ansible works by creating a “play” that runs on a your system. A series of plays make up a “playbook”, which in turn make up a “role”.
A basic Ansible role can be created to manage the ZAP provisioning, we’ll call it ‘zap’. A playbook will be created for this role that will install ZAP, add the init script created earlier, install Java and register our ZAP service.
Start by creating the following directory structure:

mkdir -p zap/roles/zap{tasks,defaults,files}

We’re now going to create the main playbook. This playbook will manage downloading ZAP and its init script as well as installing Java on the system. Create a new file, ‘zap/roles/zap/tasks/main.yml’ with the following content:

---
- name: Download ZAP
  unarchive: >
    src={{ zap_url }}
    dest=/opt
    copy=no
- name: Link zap version
  file: >
    src=/opt/ZAP_{{ zap_version }}
    dest=/opt/zap
    state=link
    owner=root
    group=root
- name: Install init script
  copy: >
    src=zap-init.sh
    dest=/etc/init.d/zap
    mode=0755
    owner=root
    group=root
- name: Install Java
  yum: >
    name=java-1.7.0-openjdk
    state=present
    update_cache=yes
- name: Register and start zap service
  service: >
    name=zap
    state=started
    enabled=yes

The playbook above references the ‘zap-init.sh’ script, this must exist in the ‘files’ directory of our ‘zap’ role.

wget -q -O zap/roles/zap/files/zap-init.sh https://raw.githubusercontent.com/stelligent/zap/master/packer/roles/zap/files/zap-init.sh

Additionally, there are a couple of variables for the ZAP version and a URL for downloading the version of ZAP we’ve been using. It’s best to store these in ‘zap/roles/zap/default/main.yml’ inside our role as follows:

---
zap_version: "2.4.3"
zap_url: "https://github.com/zaproxy/zaproxy/releases/download/{{ zap_version }}/ZAP_{{ zap_version }}_Linux.tar.gz"

Lastly, a simple playbook that calls the ‘zap’ role is required. Edit ‘zap/install_zap.yml’ and add the following:

---
- name: Install OWASP ZAP
  hosts: localhost
  sudo: True
  roles:
    - zap

Ansible can now be called to install ZAP on your target server. From the ‘zap/’ directory, run the following command:

ansible-playbook -l , install_zap.yml

The Ansible role for this can be found in the ‘packer’ directory of the Stelligent Zap repository. Also included in the repository are methods for creating an AWS AMI image as well as a Vagrant system via Test Kitchen for local development and testing. We’ll cover packer more in another post.

Conclusion

We have now covered how to install ZAP manually on to a Linux system as well as providing an automated provisioning method, via Ansible, to help lay the ground work for a future CI/CD environment.
In the next post we’ll dig into how to penetration test your web-based application, manage the results of the scan and integrate everything into your CI/CD environment.
 
Stelligent is hiring! Do you enjoy working on complex problems like security in the CD pipeline? Do you believe in the “everything-as-code” mantra? If your skills and interests lie at the intersection of DevOps automation and the AWS cloud, check out the careers page on our website.

Stelligent Amazon Pollycast
Voiced by Amazon Polly