Skip to content

Instantly share code, notes, and snippets.

@dougluce
Last active March 7, 2025 13:30
Show Gist options
  • Save dougluce/e59109adaf027d1b622039a79921e96f to your computer and use it in GitHub Desktop.
Save dougluce/e59109adaf027d1b622039a79921e96f to your computer and use it in GitHub Desktop.
ZSH script to download CVE data from https://nvd.nist.gov/ using the 2.0 API
#!/usr/bin/env zsh
#
# Available from:
# https://gist.github.com/dougluce/e59109adaf027d1b622039a79921e96f
#
# Copyright 2024 Douglas Allen Luce
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# Download NVD CVE data in JSON format to the current directory. If
# there are a lot, multiple files will be created.
#
# Date range to retrieve, note: cannot exceed 120 days.
pubStartDate=2024-03-04T00:00:00.000
pubEndDate=2024-06-01T00:00:00.000
# Optional keyword search
#keywordSearch=IBM
baseUrl=https://services.nvd.nist.gov/rest/json/cves/2.0/
extra=
append () {
if [[ -z $extra ]]; then
extra="?$1"
else
extra="${extra}&$1"
fi
}
if [[ ! -z $pubStartDate ]]; then
append pubStartDate=$pubStartDate
fi
if [[ ! -z $pubEndDate ]]; then
append pubEndDate=$pubEndDate
fi
if [[ ! -z $keywordSearch ]]; then
append keywordSearch=$keywordSearch
fi
url=$baseUrl$extra
N=1
while true; do
TEMPFILE=$(mktemp -u curl.output.XXXXXX)
TEMPHEADERS=$(mktemp -u curl.headers.XXXXXX)
OUTPUT=nvd-output${N}.json
echo Downloading $url to $OUTPUT
{curl -s -D $TEMPHEADERS -w '%{stderr}%{http_code} ' $url > $TEMPFILE > >(>&2 jq -r '"\(.resultsPerPage) \(.startIndex) \(.totalResults)"') } 2>&1 | read http_code resultsPerPage startIndex totalResults
if [[ $http_code -ne 200 ]]; then
perl -lne 'print if s/^message: //' $TEMPHEADERS
cat $TEMPFILE
rm -f $TEMPFILE $TEMPHEADERS
exit 1
fi
rm -f $TEMPHEADERS
# Success, move into sequence.
mv $TEMPFILE $OUTPUT
# Got all the results?
if [[ $(( $startIndex + $resultsPerPage )) -ge $totalResults ]]; then
break
fi
if [[ -z $extra ]]; then
paging="?"
else
paging="&"
fi
url=$baseUrl$extra$paging"startIndex=$((startIndex + resultsPerPage))"
(( N++ ))
sleep 10 # Cheesy simple rate limiting (https://nvd.nist.gov/developers/start-here)
done
@GreenRoos
Copy link

GreenRoos commented Mar 7, 2025

Hi, tried your script, but I had some minor issues with it. I trasferred it back to bash script and updated it with a complete download loop. Thanks for your concept version!

@GreenRoos
Copy link

GreenRoos commented Mar 7, 2025

nvd-download-cve

Rename attachement .png to .pdf. I couldn't add it as txt or pdf file :-(
Rename the .jpg to txt.
nvddownload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment