Files and Permissions

Video + 12 min read · includes a lab

Low-level account
web01
backup.shrwxrwxrwx
Runs nightly as rootAttacker code runs as root
Add my commands
Files and Permissions
0:00 / 4:59 · Files and Permissions
Chapters and transcript

Files and Permissions On this server, a backup script runs every night with admin rights. But someone set its permissions wrong, so every user can change it. An attacker with a low-level account adds their own commands to it. Tonight, those commands run as root. One wrong permission, and the whole server is lost. Let us learn to read and fix permissions.

Reading ls -l Every file and folder has an owner and a set of permissions. To see them, I type l s dash l, for long. I get one line per item. The first line is a folder called docs. The second is a file called report dot t x t. It looks like a wall of letters at first, so let us take one line apart.

Reading ls -l The very first character is the type. A dash is a normal file, d is a directory, and l is a link. Next come nine permission characters. Then the owner, ali, and the group, staff. Then the size in bytes, the date it last changed, and finally the name.

rwx The nine permission characters are three groups of three: the owner, the group, and everyone else, called others. Each group uses the same letters, in the same order. R is read: view a file, or list a folder. W is write: change a file, or create and delete files in a folder. X is execute: run a file as a program, or enter a folder with c d. A dash means that permission is missing.

Numeric modes Permissions are often written as three digits. Read is four, write is two, execute is one. Add them up for each group. Six four four is a normal file: the owner edits, everyone reads. Seven five five is for programs and folders. Six hundred is private, like an SSH key: only the owner. And seven seven seven lets everyone do everything. That is almost always a mistake.

chmod and chown This key file can be read by everyone. I type ch mod six hundred, and check again. Now only the owner can read and write it. You can also use letters. U is the owner, g the group, o others. Ch mod u plus x lets the owner run the backup script. Last, ch own changes the owner and group. It needs admin rights, so I use sudo.

Quick check Quick check. You have a private SSH key. Only you should be able to read it or change it. Which mode should it have? Take a moment, then choose.

Dangerous permissions Remember the backup script from the start? It was world-writable. That means others, every user on the system, can change it. Mode seven seven seven is the worst case. So here is the rule of thumb. Give the smallest permissions that still let the job work. Never use seven seven seven to fix an error you do not understand.

The SUID bit Sometimes you see an s where an x should be. That is the SUID bit. When any user runs that program, it runs with the rights of the file owner, here root. The passwd program needs this, so you can save your new password in a file that only root can write. That is normal. But on the wrong program, SUID can turn a normal user into root.

The SUID bit Here it is on the laptop. L s dash l on the passwd program shows r w s: the s is the SUID bit, and the owner is root. Attackers who get into a system often search for every SUID file, with this find command. Dash perm dash four thousand matches the SUID bit. Defenders run the same search, and remove SUID from anything that does not need it.

passwd and shadow Two files deserve special care. Slash e t c slash passwd lists the user accounts. It holds no real passwords, so everyone may read it. Slash e t c slash shadow holds the password hashes, and others must never read it. If shadow is readable by everyone, any user can copy the hashes and try to crack them offline. Both are serious findings.

Putting it together Let us put it together. L s dash l shows the type, permissions, owner and group. R w x repeats for owner, group and others. Read is four, write is two, execute is one. Ch mod changes permissions, and ch own changes the owner. Watch out for seven seven seven, SUID files, and a readable shadow file. Now read the lesson below, then try the lab.

On Linux, every file and folder has an owner and a set of permissions. Permissions decide who can read, change or run it. Wrong permissions are one of the most common ways attackers gain more access on a system. This lesson shows you how to read and change them.

Reading ls -l

Run ls -l and you get one line per file:

-rw-r--r-- 1 ali staff 1830 Mar  3 10:15 report.txt
Part Example Meaning
Type - What kind of item it is
Permissions rw-r--r-- Who can do what (9 characters)
Links 1 Number of hard links (you can ignore this for now)
Owner ali The user who owns the file
Group staff The group the file belongs to
Size 1830 Size in bytes
Date Mar 3 10:15 When it was last changed
Name report.txt The file name

The file type character

The very first character tells you the type:

Character Type
- A normal file
d A directory (folder)
l A symbolic link, a shortcut that points to another file

rwx for owner, group and others

The next nine characters are three groups of three:

 rw-   r--   r--
owner group others
  • Owner: the user who owns the file
  • Group: members of the file's group
  • Others: every other user on the system

Each group of three uses the same letters, always in this order. A - means that permission is missing.

Letter On a file On a directory
r read View the contents List the files inside
w write Change the contents Create, delete or rename files inside
x execute Run it as a program Enter the folder with cd

So rw-r--r-- means: the owner can read and write, the group can only read, and others can only read.

Numeric modes

Permissions are often written as three digits. Each letter has a value:

Letter Value
r 4
w 2
x 1

Add the values for each group. For example, rwx = 4 + 2 + 1 = 7, and r-x = 4 + 0 + 1 = 5.

Mode Letters Common use
644 rw-r--r-- Normal files: owner edits, everyone reads
755 rwxr-xr-x Programs and folders: owner changes, everyone runs or enters
600 rw------- Private files, such as SSH keys: only the owner
777 rwxrwxrwx Everyone can do everything. Almost always a mistake

To read a mode such as 750: owner 7 (read, write, execute), group 5 (read, execute), others 0 (nothing).

Changing permissions: chmod

chmod changes permissions. You can use numbers:

chmod 600 secret.key
chmod 755 backup.sh

Or letters. u is the owner (user), g is the group, o is others, and a is all three. Use + to add and - to remove:

chmod u+x backup.sh     # let the owner run it
chmod o-r notes.txt     # stop others reading it
chmod g+w shared.txt    # let the group edit it

Changing the owner: chown

chown changes who owns a file. You normally need admin rights to use it.

sudo chown ali report.txt          # new owner: ali
sudo chown ali:staff report.txt    # new owner and group

Why world-writable files are dangerous

A file that others can write to is called world-writable. Mode 777 is the worst case. If an important script is world-writable, any user, including an attacker who has broken into a low-level account, can add their own commands to it. If an admin or an automatic job later runs that script, the attacker's commands run with the admin's rights.

Rule of thumb: give the smallest permissions that still let the job work. Never use 777 to fix an error you do not understand.

The SUID bit

Sometimes you will see an s where an x should be:

-rwsr-xr-x 1 root root 68208 Feb  6 09:12 /usr/bin/passwd

This is the SUID (set user ID) bit. When any user runs this program, it runs with the rights of the file's owner, here root. The passwd program needs this so that normal users can change their own password, which is stored in a file only root can write.

SUID is normal for a small number of system programs. But if a program that can run other commands or edit files has SUID set and is owned by root, a normal user may be able to use it to become root. That is why attackers who get into a system often search for SUID files:

find / -perm -4000 -type f 2>/dev/null

Defenders run the same search and remove SUID from anything that does not need it.

/etc/passwd and /etc/shadow

File What it holds Normal permissions
/etc/passwd The list of user accounts, home folders and shells. No real passwords -rw-r--r-- (644), owner root. Everyone can read it
/etc/shadow The password hashes, scrambled versions of the passwords Owner root, and never readable by others. On Ubuntu it is -rw-r----- (640) with group shadow

If /etc/shadow is readable by everyone, any user can copy the hashes and try to crack them offline. If /etc/passwd is writable by everyone, an attacker could add their own admin account. Both are serious findings.

In the lab, you will read real-looking ls -l output and decide which permissions are safe and which are dangerous.

HANDS-ON LAB

Read the Permissions

One file on this server lets any user become root. Find it.

Start the lab →

Check your understanding

Answer every question correctly to complete this lesson. If you miss one, try the quiz again.

1. A file has the mode 640. What can the group do?

2. Why is chmod 777 dangerous?

3. What does the s in -rwsr-xr-x mean?

Locked: answer every quiz question above correctly to complete this lesson.

Spotted a mistake or something unclear? Report a mistake