Lesson 02 · 35 minutes
Protect a file for confidentiality, integrity and availability
- Name the three security goals and match an attack to the goal it breaks
- Restrict a file with permissions and detect a changed file with a hash
- Back up a folder and prove that the restore works
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
The accounts clerk of a small firm keeps invoices in a folder. Last month a supplier was paid into the wrong bank account, and nobody can say whether the file was changed or the clerk made a typing error. The manager asks you: "Make sure that a changed file cannot go unseen, that strangers cannot read it, and that one failed disk does not end us." You will set up all three on a test folder, and then do the same on a new file of your own.
Recall
A risk has a likelihood of 2 and an impact of 5. What is its number and its label in risk.py?
The number is 2 x 5 = 10, and the label is MEDIUM (8 to 14).
"A thief steals a laptop whose disk is not encrypted." Name the asset, the threat and the vulnerability.
The asset is the laptop with its data. The threat is the thief. The vulnerability is the missing disk encryption.
The idea
Every incident breaks one of three promises. They are called the CIA triad. (The name has no link to any agency.)
| Goal | The promise | An attack on it | A control |
|---|---|---|---|
| Confidentiality | Only the right people can read the data. | A stolen password file | Permissions, encryption |
| Integrity | The data is complete and correct, and changes only by leave. | A changed invoice | Hashes, signatures, write rules |
| Availability | The data and service work when needed. | Ransomware (malware that locks your files and asks for money), a full disk, a flood of requests | Backups, copies, capacity |
- Attacker → Invoice file: reads, changes or deletes it
- Permissions → Invoice file: confidentiality: mode 600 stops a read
- Hash list → Invoice file: integrity: a check finds a change
- Backup copy → Invoice file: availability: restore a lost file
The red tile is the attacker. Arrow 1 is the attack. Arrows 2 to 4 are the controls that protect the file, one for each goal.
The goals pull against each other. A file that nobody can read is very confidential and not available. Good security is a balance that fits the asset, and the risk register from lesson 1 tells you which goal matters most for each row.
A Linux file has three permission sets: for the owner, for the group and for others. Each set holds r (read, value 4), w (write, 2) and x (execute, 1). The mode 600 means the owner has 4+2 and nobody else has anything. Part 1 of the Linux course, lesson 10, teaches modes in full.
A hash is a fixed-size fingerprint of data. SHA-256 gives 64 hex characters. Change one bit of the input and the whole fingerprint changes. This is how you find a changed file:
- You → File: write rent.txt
- You → Hash file: store the SHA-256 of the file
- Someone → File: change one character
- Checker → File: compute the SHA-256 again
- Checker → Hash file: compare with the stored hash
- Checker → You: no match: FAILED, exit code 1
Worked example
1. Make a lab folder
mkdir -p makes the folder, and cd enters it.
mkdir -p ~/krlab/security-basics/cia
cd ~/krlab/security-basics/cia2. Create a secret and read its permissions
echo prints the text that you give it. The sign > sends that text into a file, and replaces what the file held. ls -l (list, long format) shows one line of facts for each file, and the first field holds the type and the permissions.
echo "bank pin: 4821" > secret.txt
ls -l secret.txt-rw-rw-r-- 1 you you 15 Nov 21 08:34 secret.txtThe first field is the file type (- is a plain file) and then three sets of rights. Each set holds three places: r read, w write, x execute. A dash means that the right is missing. The last r-- says that every other user on this machine can read the file. On a laptop with one user that is a small risk. On a shared server it is a leak. Your listing may show rw-r--r--. That comes from the umask of your system (lesson 4). The 1 counts the links to the file: ignore it for now.
3. Remove access for the group and others
chmod (change mode) sets the permissions. chmod 600 gives the owner read and write, and nobody else anything. stat prints facts about a file, and with -c you choose the fields: %a is the mode in numbers, %U the owner and %n the name.
chmod 600 secret.txt
ls -l secret.txt
stat -c '%a %U %n' secret.txt-rw------- 1 you you 15 Nov 21 08:34 secret.txt
600 you secret.txtThat is confidentiality at the lowest level. To see what a denied read looks like, remove all access, try to read with cat (which prints a file), then put the mode back.
chmod 000 secret.txt
cat secret.txt
chmod 600 secret.txtcat: secret.txt: Permission denied4. Fingerprint a file for integrity
sha256sum prints the SHA-256 hash of a file. tee copies what it receives into a file and also prints it, and the sign | gives the output of one command to the next. With -c (check), sha256sum reads a hash file and tests every file that the file names.
echo "rent is 900" > rent.txt
sha256sum rent.txt | tee rent.sha256
sha256sum -c rent.sha256765384289cc57df46f9e1a48eed16567fb84797562f359d1b27e49cc144b12f2 rent.txt
rent.txt: OK5. Change the file and check again
Someone raises the rent in the file. The hash does not know about the change, so the check fails. In the shell, $? holds the exit code of the last command: 0 means success and anything else means failure.
echo "rent is 950" > rent.txt
sha256sum -c rent.sha256
echo "exit code: $?"rent.txt: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match
exit code: 1The exit code 1 lets a script act on the result. Lesson 8 uses it.
6. Make a backup
Availability starts with a second copy. cp -a (copy, archive) copies folders and keeps the times and permissions. $(date +%F) puts today's date, as year-month-day, into the name.
mkdir -p docs backup
echo "contract v1" > docs/contract.txt
echo "notes" > docs/notes.txt
cp -a docs backup/docs-$(date +%F)
ls backupdocs-2026-11-217. Lose a file, then test the restore
rm deletes a file. diff -r compares two folders file by file, and prints nothing when they are the same. The sign && runs the next command only if the one before it succeeded. A backup that you have never restored is only a hope. (If you ran step 6 on two days, docs-* matches two folders: delete the older one first.)
rm docs/contract.txt
mkdir restore-test
cp -a backup/docs-* restore-test/docs
diff -r backup/docs-* restore-test/docs && echo "restore matches"
ls restore-test/docsrestore matches
contract.txt notes.txtThe file is in the test copy, so the backup works. diff -r compared every file, and && printed the message only because diff found no difference.
Your turn
The clerk gives you one invoice line. Do the work of the manager on it, with the commands of the worked example. Work in the same folder.
- Make
invoice.txt. It holds exactly one line,pay to account 4471, andechoends the line for you. - Record the hash of the file in a hash file, and check that the check passes.
- Change the account number to
4417(the typing error of the story) and check again. - Give the file a mode so that the owner can read and write, the group can read, and others get nothing.
- Make a copy of the changed
invoice.txtin a new folderinvoice-backupwithcp -a, delete the original, copy it back from the backup, and prove withdiffthat the restored file matches the backup.
Then answer this in your own words: which of the three goals did the change break, and which control found it?
Compare with this
The change broke integrity: the account number is no longer the one that was written. The hash check found it, because the stored hash no longer matched, and sha256sum -c ended with exit code 1. Nobody read the file without leave (confidentiality) and the file stayed available.
Check yourself
An attacker sets a web shop to show a price of 1 for every item. Which goal broke?
Integrity. The data changed without leave. Nobody read secret data (confidentiality) and the shop still works (availability).
You store the hash of a file beside the file. Why does this not stop an attacker who can write to the folder?
The attacker changes the file and writes a new hash next to it, and the check passes. A hash detects accidents and changes by someone who cannot touch the hash. To resist an attacker, keep the hash in a place they cannot write, or use a signature (lesson 5).
Your backup folder sits on the same disk as the original. What goal does it protect, and what does it miss?
It protects against deleting a file by mistake. It does not protect against a disk failure, theft, or ransomware, because they destroy both copies together. Keep at least one copy elsewhere.
Field notes
- FIPS 199 defines the three goals for US federal systems and rates the impact on each as low, moderate or high. NIST SP 800-60 helps you assign these ratings to types of data. Many private companies reuse the same scale.
- The 3-2-1 rule is a good starting point for backups: three copies, two kinds of media, one copy off site. Modern ransomware also deletes backups that it can reach, so add one copy that is offline or immutable.
- Test a restore on a schedule and measure the time. The time it takes is your real recovery time, whatever the plan says. NIST SP 800-34 covers contingency planning.
- A plain hash file guards against corruption. Where an attacker is in scope, use a signature, a package manager that checks signatures, or file-integrity monitoring such as AIDE, with the baseline stored off the machine.
- Check the default permissions of new files with
umask. A value of0002gives the group write access to every new file. On shared servers use0027or stricter, and add access lists (setfacl) for exceptions, not a wider mode.