Foundations Β· Chapter 1

The Linux shell for hackers

The shell is where every hack happens. Files, permissions, pipes and the twenty commands that solve most problems.

About 8 min read

Almost every server you'll attack runs Linux. Almost every tool you'll use runs in a terminal. When you land on a machine after an exploit, there's no desktop, no mouse, just a blinking prompt. Fluency here is the difference between "I got a shell!" and "I got a shell… now what?"

Your Mac's Terminal is close to Linux (both are Unix), so you can practise most of this right now. Kali Linux in the Hacking Lab gives you the real thing.

Where am I, who am I

whoami          # your username
id              # your user id, group ids and groups (very important later)
hostname        # the machine's name
pwd             # print working directory: where you are
uname -a        # kernel version and architecture
cat /etc/os-release   # which distribution and version

After every new shell you get, on any machine, run id, hostname and uname -a first. It's a habit that tells you what you have.

Moving around

ls              # list files
ls -la          # list ALL files (including hidden ones starting with .) with details
cd /var/www     # change directory (absolute path)
cd ..           # up one level
cd ~            # your home directory
cd -            # back to the previous directory

ls -la output, decoded:

-rw-r----- 1 bandit5 bandit4  33 May  7 20:14 .hidden
drwxr-xr-x 2 root    root   4096 May  7 20:14 inhere
β”‚β””β”¬β”˜β””β”¬β”˜β””β”¬β”˜   β””β”€β”€β”¬β”€β”€β”˜ β””β”€β”€β”¬β”€β”€β”˜  β”” size  β”” date   β”” name
β”‚ β”‚  β”‚  β”” others  owner  group
β”‚ β”‚  β”” group
β”‚ β”” owner (user)
β”” type: - file, d directory, l link

Hidden files start with a dot. ls doesn't show them; ls -a does. Bandit level 3 is exactly this lesson, and real machines hide gold in .bash_history, .ssh/, .env and .git/.

Reading files

cat file.txt            # print the whole file
less file.txt           # scroll (q to quit, / to search)
head -n 20 file.txt     # first 20 lines
tail -n 20 file.txt     # last 20 lines
tail -f /var/log/syslog # follow a log as it grows
file mystery            # what kind of file is this really?
strings binary | less   # printable text hidden inside a binary

file is underrated. A file named photo.jpg might be a ZIP, a script or an ELF executable. Never trust the extension; ask file.

Awkward file names

Bandit will hand you files called -, spaces in this filename and --reset. The shell interprets those characters, so you have to quote or escape:

cat ./-                       # "./" stops - being read as an option
cat "spaces in this filename" # quotes keep it one argument
cat spaces\ in\ this\ filename
cat -- --reset                # "--" means: no more options after this

Permissions: the heart of privilege escalation

Every file has three permission sets: user (owner), group, others. Each has read, write, execute.

-rwxr-x---  β†’ owner can read/write/execute, group can read/execute, others nothing

As numbers (each digit is r=4 + w=2 + x=1):

Symbolic Number Meaning
rwx 7 everything
rw- 6 read and write
r-x 5 read and execute
r-- 4 read only

So chmod 640 notes.txt means owner rw, group r, others nothing.

Special bits you'll hunt for constantly:

  • SUID (-rwsr-xr-x, the s in the owner slot): the program runs with the owner's privileges, not yours. A SUID program owned by root that you can trick into doing something is a path to root, which is why defenders audit them (chapter 2).
  • SGID: same idea for the group.
  • Sticky bit on directories like /tmp (drwxrwxrwt): you can only delete your own files there.
find / -perm -4000 -type f 2>/dev/null   # every SUID file on the system

Memorize that line. You'll type it hundreds of times.

Finding things

find searches by name, size, owner, permissions and more:

find / -name "*.conf" 2>/dev/null           # all .conf files
find . -type f -size 1033c                   # files of exactly 1033 bytes (c = bytes)
find / -user bandit7 -group bandit6 2>/dev/null
find / -writable -type d 2>/dev/null         # directories you can write to
find /home -name "*.txt" -newer /etc/passwd  # changed more recently than a file

2>/dev/null throws away error messages ("Permission denied" spam) so you only see results. 2> means "standard error", /dev/null is the void.

grep searches inside files:

grep "password" config.php
grep -ri "password" /var/www 2>/dev/null   # recursive, case-insensitive
grep -n "TODO" *.js                        # show line numbers
grep -v "^#" sshd_config                   # lines NOT starting with #
grep -E "[0-9]{3}-[0-9]{4}" data.txt       # extended regex

Pipes: small tools, big results

The | sends one command's output into the next. Unix philosophy: each tool does one thing well, and you chain them.

cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head

Read it left to right: take the log β†’ keep only the first field (the IP) β†’ sort β†’ count duplicates β†’ sort by count, biggest first β†’ show the top 10. You just found who hits your server the most. That's incident response in one line.

Bandit level 8 asks for the only line that appears once in a file:

sort data.txt | uniq -u

uniq only removes adjacent duplicates, which is why you sort first. That's a lesson you'll remember.

Other pipe favourites:

wc -l file              # count lines
sort -u                 # sort and remove duplicates
tr 'A-Za-z' 'N-ZA-Mn-za-m'   # ROT13: rotate letters by 13
awk '{print $3}'        # print the third column
sed 's/old/new/g'       # replace text
xargs                   # turn input lines into arguments
tee out.txt             # show output AND save it

Encodings you'll see every day

Encoding is not encryption. It hides nothing; it just changes the representation. Recognize them on sight:

Looks like It's Decode
aGVsbG8gd29ybGQ= Base64 (ends in = often, A–Z a–z 0–9 + /) echo aGVsbG8gd29ybGQ= \| base64 -d
68656c6c6f Hex echo 68656c6c6f \| xxd -r -p
uryyb ROT13 echo uryyb \| tr 'A-Za-z' 'N-ZA-Mn-za-m'
hello%20world URL encoding Burp Decoder, or python3 -c "import urllib.parse;print(urllib.parse.unquote('hello%20world'))"
eyJhbGciOi... Base64 JSON, usually a JWT Paste into jwt.io (chapter 6)

eyJ at the start of anything is Base64 of {". Once you know that, you'll spot JWTs everywhere.

Compression and hexdumps

Bandit level 12 gives you a hexdump of a file that's been compressed many times. The method generalizes to forensics CTFs:

xxd -r data.txt > data.bin   # reverse the hexdump into binary
file data.bin                # "gzip compressed data"
mv data.bin data.gz && gunzip data.gz
file data                    # "bzip2 compressed data"... repeat

The tool chain: file tells you what it is, the right tool unpacks it (gunzip, bunzip2, tar xf, unzip), repeat until file says "ASCII text".

Processes, network and services

ps aux                   # every running process
ps aux | grep root       # what runs as root?
ss -tlnp                 # listening TCP ports (and which process, if you're allowed)
netstat -tulpn           # older equivalent
cat /etc/crontab; ls -la /etc/cron.*   # scheduled jobs: classic privesc source
systemctl list-units --type=service

A service listening only on 127.0.0.1 is invisible from outside but reachable once you're on the box. Internal admin panels and databases often live there.

SSH: how you'll get into most lab machines

ssh bandit0@bandit.labs.overthewire.org -p 2220
ssh -i id_rsa user@10.10.10.5        # log in with a private key file
chmod 600 id_rsa                      # SSH refuses keys others can read
scp file.txt user@host:/tmp/          # copy a file over SSH

If you ever find a readable id_rsa in someone's home directory on a lab box, that's usually your way in as that user.

Talking to ports directly

nc localhost 30000          # netcat: raw TCP conversation
echo "secret" | nc localhost 30000
openssl s_client -connect localhost:30001   # same but over TLS
nc -lvnp 4444               # LISTEN on port 4444 (a listener: you play the server)

Netcat is the "Swiss army knife": it can be a client, a server, a port scanner and a file transfer tool.

Shell productivity

  • Tab completes commands and paths. Press it constantly.
  • Ctrl+R searches your command history.
  • Ctrl+C stops a command. Ctrl+L clears the screen.
  • history shows past commands, and on target machines, other users' .bash_history files show theirs.
  • !! repeats the last command; sudo !! reruns it with sudo.
  • man find or find --help when you forget a flag. Also try tldr find for examples.

A tiny script, because you'll write many

#!/bin/bash
# try every 4-digit PIN against a local practice service (Bandit 24 style)
for pin in $(seq -w 0000 9999); do
  echo "secretpassword $pin"
done | nc localhost 30002 | grep -v "Wrong"

Notice the trick: generate all attempts first, pipe them through one connection, and filter out the failures. Thinking about efficiency of attempts is a recurring hacker skill.

Check yourself

  1. What does -rwsr-xr-x root root /usr/bin/passwd tell you, and why do attackers care?
  2. Why does cat - hang, and how do you read a file named -?
  3. Write a one-liner that counts how many unique IPs appear in access.log.
  4. A string ends in ==. What's your first guess, and how do you decode it?
  5. What's the difference between 2>/dev/null and >/dev/null?

In the lab: start OverTheWire Bandit (next chapter). Levels 0–10 use almost everything on this page.

Practise only on systems you own or are explicitly allowed to test: your home lab, CTF platforms and authorized engagements. Testing anyone else's system without permission is a crime in most countries, including under the US Computer Fraud and Abuse Act. Your reading progress stays in this browser.