Do we use it?
In December 2021 a flaw called Log4Shell was found in Log4j, a library used by thousands of programs. Every security team asked the same question that morning: do we use it?
The teams with a list of every component in every release answered in minutes. The others searched for weeks.
By the end of this lesson
Note
I can make an SBOM for a project and find what changed between two of them.
Key words
| Word | What it means | Example |
|---|---|---|
| Supply chain | everything and everyone that goes into your software | |
| SBOM | a list of every component in a piece of software | |
| Component | one library or package inside it | |
| Package URL | one standard name for a component | pkg:pypi/[email protected] |
| Transitive | needed by a component you chose | |
| CycloneDX | one common SBOM format; SPDX is the other |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsAn SBOM lists every component
An SBOM, a software bill of materials, is like the ingredients label on food. It lists each component with its version and a package URL.
It is written as JSON, so a program can read it. Trivy can make one offline from a project's files. Start with the tuck shop's repository.
Bring the lab's copy up to date
docker exec gitleaks git -C /work/tuckshop pull -qShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make an SBOM from the repository
docker exec trivy trivy fs -q --format cyclonedx -o /work/reports/source.cdx.json /work/tuckshopShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the libraries it found
docker exec ci python3 -c "import json; d=json.load(open('/work/reports/source.cdx.json')); [print(c['name'], c['version'], c['purl']) for c in d['components'] if c['type'] == 'library']"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsWhat you install is more than you list
The requirements file names three libraries. Each of them needs others to run, and pip installs those too. They are transitive components.
An SBOM made from what was really installed shows them all. Install the shop's libraries into a folder, then ask Trivy about that folder.
Install the shop's libraries into a folder
docker exec ci pip install -q --no-cache-dir --upgrade --target /work/reports/env -r /work/tuckshop/requirements.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make an SBOM of what was installed
docker exec trivy trivy rootfs -q --format cyclonedx -o /work/reports/built.cdx.json /work/reports/envShown here, not run. Press Start lab to type it into your own lab’s terminal.
List what the build really holds
docker exec ci python3 -c "import json; d=json.load(open('/work/reports/built.cdx.json')); [print(c['name'], c['version'], c['purl']) for c in d['components'] if c['type'] == 'library']"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Count what the scanner's own image holds
docker exec trivy trivy rootfs -q --format cyclonedx --skip-dirs /proc,/sys,/work -o /work/reports/scanner.cdx.json / && docker exec ci python3 -c "import json, collections; d=json.load(open('/work/reports/scanner.cdx.json')); print(collections.Counter(c['purl'].split('/')[0] for c in d['components'] if 'purl' in c))"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think an SBOM made from the requirements file is complete. It only lists what you asked for, not what was installed. Make the SBOM from the built artifact, the folder or the image you ship, because that is what runs.
Two formats, and a diff shows change
CycloneDX and SPDX are the two common formats. Both list the same components; tools read either.
Keep an SBOM with every release. Comparing two of them by name shows what a change brought in, which a reviewer may never see in the code.
Write the same list as SPDX
docker exec trivy trivy rootfs -q --format spdx-json -o /work/reports/built.spdx.json /work/reports/env && docker exec ci python3 -c "import json; d=json.load(open('/work/reports/built.spdx.json')); print(d['spdxVersion'], len(d['packages']), 'packages')"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Compare the two SBOMs by name
docker exec ci python3 -c "import json; names = lambda f: {c['name'].lower() for c in json.load(open(f))['components'] if c['type'] == 'library'}; print('Only in the build:', *sorted(names('/work/reports/built.cdx.json') - names('/work/reports/source.cdx.json')))"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: compare two releases
old = {"requests": "2.19.1", "jinja2": "2.10"}
new = {"requests": "2.33.0", "jinja2": "2.10", "urllib3": "2.6.3"}
print("added:", sorted(set(new) - set(old)))
print("removed:", sorted(set(old) - set(new)))
for name in sorted(set(old) & set(new)):
if old[name] != new[name]:
print("changed:", name, old[name], "->", new[name])
# Remove "jinja2" from new and press Run again. What is listed now?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsAn SBOM answers "are we affected?"
When the next Log4Shell is announced, you search every release's SBOM for the component's name, not the code.
An SBOM says what is inside, not whether it is safe. Scanning one for known flaws is covered in the vulnerability management module.
Is urllib3 in this release?
docker exec ci grep -o 'pkg:pypi/urllib3@[0-9.]*' /work/reports/built.cdx.json | sort -uShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: compare two SBOMs
A CycloneDX SBOM is a dictionary with a components list; each component has type, name and version. Write three functions.
components(sbom) returns a dictionary from each library component's name, in lower case, to its version, skipping every other type. diff(old, new) takes two SBOMs and returns {"added": [...], "removed": [...], "changed": [...]}: sorted names only in the new one, sorted names only in the old one, and sorted (name, old_version, new_version) tuples for names in both with different versions. releases_using(sboms, name) takes a dictionary from a release name to its SBOM and returns the sorted release names that contain a library called name, in any case.
def components(sbom):
return {}
def diff(old, new):
return {"added": [], "removed": [], "changed": []}
def releases_using(sboms, name):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go