Skip to content

Instantly share code, notes, and snippets.

@sammcj
Forked from garethr/LICENSE
Created December 13, 2021 04:11
Show Gist options
  • Save sammcj/93eefec4fef97247e88d258cc4610232 to your computer and use it in GitHub Desktop.
Save sammcj/93eefec4fef97247e88d258cc4610232 to your computer and use it in GitHub Desktop.
A script for customers to use the Snyk API to get a list of projects impacted by the Log4Shell vulnerability

A script to use the Snyk API to get a list of projects impacted by the Log4Shell vulnerability

Dependencies

You'll need Python installed to use this script, as well as being a Snyk customer with access to the Snyk API

pip install pysnyk

Usage

export SNYK_TOKEN=<your API token>
python snyk-log4shell.py

Example output

Project,Version,Org,ID
garethr/todo:latest:/app/lib,2.13.3,todo-demo,81559bac-8f6d-b47c-c53c0ea0456d
subvertbeats/todo-list:pom.xml,2.13.3,todo-demo,239a578a-4794-a861-0dcbf1153490
garethr/todo-list:pom.xml,2.13.3,todo-demo,da1afd8c-371b-8e62-64ffe4793c10
subsy/todo-list:pom.xml,2.13.3,todo-demo,535ff486-222c-4d0f-60c13ec55c9d
io.pivotal.sporing:todo-list,2.13.3,todo-demo,42dc29f2-15f2-afd4-e897589f9582
io.pivotal.sporing:todo-list,2.13.3,todo-demo,b9f6f941-c887-bcd3-7cae452868f1

Modifying

Please take this script and modify it for your own needs. It's intended as much as an example for you to extend as needed.

This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
"""
A script to use the Snyk API to get a list of projects impacted by the Log4Shell vulnerability
"""
import csv
import os
import sys
from snyk import SnykClient
try:
token = os.environ["SNYK_TOKEN"]
except KeyError:
sys.exit("You must provide a SNYK_TOKEN")
client = SnykClient(token)
orgs = client.organizations.all()
def get_log4shell_dependencies(client, org, page: int = 1):
results_per_page = 50
versions = [
"2.14.1"
"2.14.0",
"2.13.3",
"2.13.2",
"2.13.1",
"2.13.0",
"2.12.1",
"2.12.0",
"2.11.2",
"2.11.1",
"2.11.0",
"2.10.0",
"2.9.1",
"2.9.0",
"2.8.2",
"2.8.1",
"2.8",
"2.7",
"2.6.2",
"2.6.1",
"2.6",
"2.5",
"2.4.1",
"2.4"
"2.3",
"2.2",
"2.1.",
"2.0.2",
"2.0.1",
"2.0",
"2.0-rc2",
"2.0-rc1",
"2.0-beta9",
"2.0-beta8",
"2.0-beta7",
"2.0-beta6",
"2.0-beta5",
"2.0-beta4",
"2.0-beta3",
"2.0-beta2",
"2.0-beta1",
"2.0-alpha2",
"2.0-alpha1"
]
affected_versions = [f"org.apache.logging.log4j:log4j-core@{version}" for version in versions]
post_body = {"filters": {"dependencies": affected_versions}
path = "org/%s/dependencies?sortBy=dependency&order=asc&page=%s&perPage=%s" % (
org.id,
page,
results_per_page,
)
resp = client.post(path, post_body)
dependency_data = resp.json()
total = dependency_data[
"total"
] # contains the total number of results (for pagination use)
results = dependency_data["results"]
if total > (page * results_per_page):
next_results = get_log4shell_dependencies(client, org, page + 1)
results.extend(next_results)
return results
with open("projects.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
writer.writerow(["Project", "Version", "Org", "ID"])
for org in orgs:
for dep in get_log4shell_dependencies(client, org):
for project in dep["projects"]:
writer.writerow([project["name"], dep["version"], org.name, project["id"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment