Commands / Detection and audit

Detection and audit

sha256sum

Also SHA-256, checksum, hash, digest, file fingerprint

Prints a file's SHA-256 digest, a fingerprint of its exact bytes, and checks files against a saved list of digests.

sha256sum reads every byte of a file and prints its SHA-256 digest: 64 hex digits, then the file's name. The same bytes always give the same digest, and changing one byte changes it completely, so two equal digests mean two identical files: a download that arrived whole, a copy that matches its original, the same dropped tool on a second host. Saved to a file, the digests are a baseline: sha256sum -c hashes each file the list names again and prints OK or FAILED beside it, the one-command form of what a file-integrity monitor such as aide does. With no file, or -, it hashes what it reads on standard input, so a pipe into it fingerprints a string.

sha256sum command usage

sha256sum [FILE…]
sha256sum -c [--quiet|--status] LIST
COMMAND | sha256sum

Options

-c LIST
check: hash each file the list names and print OK or FAILED for each
--quiet
with -c, print only the files that fail
--status
with -c, print nothing: the exit code alone says whether every file matched
-
read standard input instead of a file

sha256sum examples

sha256sum /usr/local/bin/.sysupdate
Prints the file's SHA-256 digest, then its name.
sha256sum /root/exam.txt /root/exam-46.txt
Prints a digest for each file: equal digests mean identical files.
sha256sum /srv/www/*.html > /root/baseline.sha256
Saves the pages' digests to a list, a baseline to check them against later.
sha256sum -c /root/baseline.sha256
Hashes each file in the list again and prints OK or FAILED beside its name.
sha256sum -c --quiet /root/baseline.sha256
Prints only the files that no longer match the list.
printf '%s' summer | sha256sum
Hashes a string instead of a file, shown with - for its name.

Reading the output

$ sha256sum -c /root/baseline.sha256
/srv/www/timetable.html: FAILED
/srv/www/roster.html: OK
sha256sum: WARNING: 1 computed checksum did NOT match
  • FAILED: the file's bytes no longer give the digest saved in the list, so it changed after the list was made.
  • OK: the file is byte for byte what it was when the list was made.
  • The WARNING line counts the failures, and the command exits 1, so a script can act on it.

Pitfalls

  • Equal digests prove two files are the same, not that either is safe: compare with a digest from a source you trust.
  • A list kept on the host it checks can be rewritten along with the files: store the baseline where an attacker cannot reach it.
  • echo text | sha256sum also hashes the newline echo adds: use printf '%s' text to hash just the text.

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab