GitLab
Note: This a similar tutorial explored in our GitHub actions tutorial, but geared towards using GitLab CI/CD and GitLab Security Alerts.
In this tutorial, you’ll learn how to:
- Integrate NightVision DAST scans into your CI/CD pipelines to find automatically find exploitable vulnerabilities within minutes.
- Use NightVision’s API Extraction to scan the source code to find the attack vector in the file and down to the line of code.
- Upload any vulnerabilities discovered with NightVision to GitLab’s vulnerability report.
CI file
Section titled “CI file”To run a NightVision scan from GitLab, you must set the NIGHTVISION_TOKEN environment variable in your CI/CD settings for your repository. You can do this through the UI for your repository via this documentation.
An example file is below:
stages: - sast_scan - dast_scan - convert_sarif_to_gitlab
variables: NIGHTVISION_TARGET: javaspringvulny-api-gitlab NIGHTVISION_AUTH: javaspringvulny-api-gitlab DOCKER_HOST: tcp://docker:2375/ DOCKER_DRIVER: overlay2 FF_NETWORK_PER_BUILD: "true" # activate container-to-container networking
services: - docker:dind
sast_scan: stage: sast_scan image: ubuntu:latest services: - docker:dind before_script: - apt-get update && apt-get install -y wget python3-venv python3-docker python3-pip python3 docker-compose curl gcc musl-dev libffi-dev - python3 -m venv venv - source venv/bin/activate - pip3 install requests urllib3 - wget -c https://downloads.nightvision.net/binaries/latest/nightvision_latest_linux_amd64.tar.gz -O - | tar -xz - mv nightvision /usr/local/bin/ script: # "Extract API documentation from code" - nightvision swagger extract . -t ${NIGHTVISION_TARGET} --lang java || true - if [ ! -e openapi-spec.yml ]; then cp backup-openapi-spec.yml openapi-spec.yml; fi artifacts: paths: - openapi-spec.yml expire_in: 30 days
dast_scan: stage: dast_scan image: ubuntu:latest services: - docker:dind before_script: - apt-get update && apt-get install -y wget python3-venv python3-docker python3-pip python3 docker-compose curl gcc musl-dev libffi-dev - python3 -m venv venv - source venv/bin/activate - pip3 install requests urllib3 - wget -c https://downloads.nightvision.net/binaries/latest/nightvision_latest_linux_amd64.tar.gz -O - | tar -xz - mv nightvision /usr/local/bin/ script: # "Starting the app" - docker-compose up -d - sleep 15 # "Scanning the API" - nightvision scan ${NIGHTVISION_TARGET} --auth ${NIGHTVISION_AUTH} > scan-results.txt - nightvision export sarif -s "$(head -n 1 scan-results.txt)" --swagger-file openapi-spec.yml # "Getting logs" - for pod in $(docker ps | grep -v 'CONTAINER ID' | grep -v IMAGE | awk '{print $1}'); do docker logs $pod >> test.pod.logs 2>&1; done artifacts: paths: - test.pod.logs - results.sarif expire_in: 30 days dependencies: - sast_scan
convert_sarif_to_gitlab: stage: convert_sarif_to_gitlab image: python:3.9 script: - python3 convert_sarif_to_gitlab.py artifacts: reports: sast: gitlab_security_report.json dependencies: - dast_scanPreview: Vulnerability Report
Section titled “Preview: Vulnerability Report”
The GitLab vulnerability report displays DAST results and links back to the file and line of code.
Prerequisites
Section titled “Prerequisites”Let’s install GitLab’s CLI to make things easier. Per the GitLab CLI docs, Homebrew is the officially supported installation method for macOS, Linux, and Windows (via WSL). Run this command to install GitLab and authenticate.
brew install glabglab auth loginBefore we get started, you’ll need to mirror the example repository to GitLab.
Easy Mode
Section titled “Easy Mode”Open your terminal and run this command:
curl -O https://raw.githubusercontent.com/nvsecurity/nv-public-reference/main/demo-scripts/gitlab-demo.sh && \ chmod +x gitlab-demo.sh
# Set GROUP to the GitLab group that has a GitLab ultimate plan (or trial)# Set REPO to the name of the repository you want to create.export GROUP=GroupOrProjectexport REPO=myRepo
./gitlab-demo.sh $GROUP $REPONow open your GitLab repository in the browser.
The scan will be complete in about 6-7 minutes.
- Optionally, you can click on the GitLab Pipeline execution to watch it run, as indicated here:
How to view the GitLab pipeline execution.
- When the scan finishes, click on the Security tab.
Security Alerts are displayed
- You will see three critical alerts: (1) SQL Injection, (2) Cross-Site Scripting, and (3) Spring4Shell. Click on one of them.
- When you select one of the vulnerabilities, you can see the exact line of code where the vulnerable endpoint was defined. Click on the provided link.
The vulnerable endpoint discovered by NightVision is flagged in the GitLab Vulnerability report.
- After clicking on the link, it will bring you to the line of code where the endpoint was declared.
The line of code where the vulnerable endpoint was declared.
- You can also leverage the power of GitLab Duo Chat on an issue to get detailed ✨AI-powered✨ remediation guidance.
GitLab Duo provides AI-powered remediation guidance.