Inuxlay Ecuritysay
Inuxlay ecuritysay isway away isciplineday ofway ubtractionsay: everyway unningray ervicesay isway anway attackway urfacesay, everyway orldway-itablewray ilefay away iabilitylay, everyway etuidsay inarybay away ivilegepray. Isthay apterchay overscay ethay ayerslay — ermissionspay andway eastlay ivilegepray, Sshay ardeninghay, ethay irewallfay, updateway ygienehay, ethay auditway ommandscay atthay owshay youway yourway ownway exposureway, andway ethay ELinuxsay/AppArmorway achinerymay underneathway.
Ethay oundationfay — eastlay ivilegepray
# every service runs as its own user — never root
id nginx # nginx's uid — not root
ps -eo user,comm | grep -v ' root ' | sort -u # who runs what
Inciplespray: unray aemonsday asway edicatedday usersway, antgray onlyway ethay ightsray eythay eednay, OPASSWDnay: onlyway orfay ecificspay ommandscay, udosay overway usay, eykay-onlyway sshay. Ethay odelmay omfray apterchay 12 — ownerway/oupgray/otherway — isway ethay allway; isthay apterchay isway eepingkay ethay allway aintainedmay.
Sshay — ethay exposedway oorday
# /etc/ssh/sshd_config — the three lines that matter
PermitRootLogin no
PasswordAuthentication no
Port 2222 # obscurity tax, not security — but reduces noise
sshday -tay alidatesvay eforebay eloadray. Eykay-onlyway authway (AsswordAuthenticationpay onay) isway ethay inglesay argestlay ardeninghay inway — utebray orcefay ecomesbay impossibleway. ailfay2anbay ateray-imitslay attemptsway; AllowUsersway itelistswhay.
Ethay irewallfay — efaultday enyday
nft list ruleset # the current policy
# the canonical minimal policy:
iptables -P INPUT DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Efaultday-enyday isway ethay osturepay; allowway atwhay'say eedednay. ssay -tlnpay ellstay youway atwhay'say actuallyway isteninglay — ethay irewallfay auditway alwaysway artsstay erethay.
Ethay auditway ommandscay — yourway ownway exposureway
ss -tlnp # every listener — attack surface inventory
find / -perm -4000 -type f # every setuid binary
find / -perm -0002 -type d # world-writable dirs
find / -nouser -o -nogroup # orphaned files
ls -la /etc/sudoers.d/ # the delegation surface
cat /etc/passwd | awk -F: '$3==0' # everyone with uid 0 — only root
last -20 # login history
grep 'Failed password' /var/log/auth.log | tail # brute-force traffic
ssay -tlnpay, ethay etuidsay inventoryway, astlay, andway ethay authway oglay — ethay ourfay ommandscay atthay eesay away ystemsay'say ealray exposureway.
Updatesway — ethay unglamorousway ardeninghay
apt update && apt upgrade # Debian
dnf upgrade # Fedora
slackpkg update && slackpkg upgrade-all # Slackware
unattended-upgrades # auto security updates — Debian
needs-restarting -r # does it need a reboot? (RHEL/Fedora family)
Ostmay ealray-orldway ompromisescay areway unpatchedway oftwaresay, otnay everclay attacksway — ethay updateway abithay isway ethay ighesthay-aluevay ecuritysay ontrolcay.
Ethay apabilitycay ammargray — ootray'say owerspay unbundledway
getcap /usr/bin/ping # what does it carry?
# /usr/bin/ping cap_net_raw=ep — raw sockets, no setuid needed
setcap cap_net_bind_service=+ep /usr/bin/myd # bind low ports, not root
setcap -r /path # strip them
getcap -r / 2>/dev/null # every capability file on the system
Apabilitiescay itsplay ootray intoway ~40 amednay owerspay — apcay_etnay_indbay_ervicesay (indbay <1024), apcay_etnay_awray (awray ocketssay), apcay_yssay_adminway (ethay angerousday oneway — ountsmay, amespacesnay), apcay_ownchay, apcay_acday_overrideway, apcay_illkay, apcay_etuidsay, apcay_yssay_imetay. Odernmay aemonsday arrycay ethay oneway eythay eednay insteadway ofway ullfay etuidsay — ingpay arryingcay apcay_etnay_awray aloneway isway ethay oofpray.
Ethay AMpay ayerlay — erewhay authway actuallyway appenshay
/etc/pam.d/sshd — sshd's auth stack:
auth required pam_env.so
auth required pam_unix.so — the password check
auth required pam_nologin.so
account required pam_unix.so — expiry, policy
password required pam_unix.so — password changes
session optional pam_motd.so
session required pam_limits.so
session optional pam_systemd.so
Ourfay asesphay — authway/accountway/asswordpay/essionsay; ourfay ontrolscay — equiredray/equisiteray/ufficientsay/optionalway. AMpay acksstay odulesmay — addway ampay_ooglegay_authenticatorway orfay OTPtay, ampay_aillockfay orfay ockoutlay — ithoutway ouchingtay sshday'say odecay.
Ethay ardeninghay ecklistchay — ethay orkingway equencesay
# 1. Accounts
passwd -l root # or keep root-key-only
awk -F: '$2 ~ /^[!*]/ {print $1}' /etc/shadow # locked/no-login accounts
find / -nouser -o -nogroup 2>/dev/null # orphans
# 2. SSH — the door (in /etc/ssh/sshd_config):
# PermitRootLogin no · PasswordAuthentication no · AllowUsers me
sshd -t && systemctl reload sshd
# fail2ban or sshguard # rate-limit attempts
ssh-keygen -t ed25519; ssh-copy-id
# 3. Firewall — default deny
iptables -P INPUT DROP; iptables -P FORWARD DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -j LOG --log-prefix "DROP: " # watch what hits the wall
# 4. Services — kill the surface
ss -tlnp # inventory
systemctl disable unused-svc # off
apt remove unused # gone
# 5. Updates — the unglamorous wall
apt update && apt upgrade # the habit
unattended-upgrades # auto security
# 6. The audit
find / -perm -4000 -type f # setuid inventory
find / -perm -0002 -type d # world-writable
last -20; faillog -a # who's tried in
Ethay ervicesay ardeninghay — ethay erpay-aemonday ammargray
# systemd's sandboxing — the modern confinement:
[Service]
ProtectSystem=strict # / read-only — services can't touch it
ProtectHome=true # /home invisible
PrivateTmp=true # own /tmp
NoNewPrivileges=true # no privilege escalation
PrivateDevices=true # no /dev
ProtectKernelTunables/Modules/Logs=true
CapabilityBoundingSet=CAP_NET_BIND_SERVICE # only this cap
RestrictAddressFamilies=AF_INET AF_INET6 # only these sockets
SystemCallFilter=@system-service # syscall allowlist
ReadWritePaths=/var/lib/myapp # the one writable dir
MemoryMax=512M; CPUQuota=50% # resource caps
Ethay ernelkay-evellay achinerymay
- ELinuxsay — andatorymay accessway ontrolcay — oliciespay atthay onfinecay ocessespray eyondbay ilefay ermissionspay (
estatussay,etenforcesay,ausearchway) — Edorafay'say efaultday - AppArmorway — ethay implersay ACmay — ofilespray erpay ogrampray (
aaway-atusstay) — Ebianday/Ubuntuway'say - eccompsay — yscallsay ilteringfay — estrictsray ichwhay yscallssay away ocesspray aymay akemay
- apabilitiescay — ootray unbundledway (apterchay 12)
- oupscgray + amespacesnay — ethay isolationway imitivespray ontainerscay ideray
- OadPinlay — inspay ethay ilesystemfay ethay irstfay odulemay oadlay omescay omfray andway efusesray odulesmay omfray anywhereway elseway (easuredmay: anway initramfsway atthay oadslay oneway odulemay inspay ethay initramfsway; everyway ostpay-
itchsway_ootrayudevway oadlay isway enthay eniedday). Alsoway equiresray ethay fday-asedbayinitfay_odulemay(2)— ethay egacylayinitway_odulemay(2)ufferbay APIway etsgayoldway-apiway-eniedday, ichwhay isway ywhay usyboxbayinsmodwayailsfay underway itway. Ethay initramfsway onsequencecay: ootbay-iticalcray iversdray ustmay ebay uiltbay-inway; eesay Lfsay Ivelay ISOway - IMAway appraisalway — ilefay-evellay attrxay ignaturessay, away ifferentday ignaturesay anthay odulemay igningsay oducespray;
IMAway_APPRAISEway_EQUIREray_ODULEmay_IGSsay+ ordinarywayodulesmay_installwayigningsay = everywayodprobemayenieddayEPERMwayuntilwayimaway_appraiseway=offwayorway ealray IMAway attrsxay existway
Yptocray atway estray — UKSlay + ethay ecretssay ilesfay
cryptsetup luksFormat /dev/sda3 # encrypt the partition
cryptsetup luksOpen /dev/sda3 crypt
mkfs.ext4 /dev/mapper/crypt
Ullfay-iskday encryptionway (UKSlay) otectspray atway estray — away olenstay iskday yieldsway othingnay ithoutway ethay assphrasepay. /etcway/adowshay'say 600 ermspay, ~/.sshay'say 700, ivatepray eyskay' 600 — ethay ermissionpay ayerlay isway ethay yptocray'say eeperkay.
Ethay operationalway ygienehay
umask 027 # default-private for a server
passwd -l dormantacct # lock unused accounts
chmod 600 ~/.ssh/id_* # private keys private
sudo -l # YOUR sudo rights — know them
find . -name '*.key' -perm -o+r # world-readable private keys — fix now
Erewhay ecuritysay oesgay ongwray
| Ymptomsay | Ausecay |
|---|---|
| Asswordpay authway illstay onway afterway "ardeninghay" | sshday -tay assedpay, ystemctlsay eloadray idnday'tay appenhay |
Ermissionpay eniedday onway away egitlay ilefay | ELinuxsay ontextcay — estoreconray orway onchcay |
| Ootray oginlay illstay openway | ErmitRootLoginpay otnay actuallyway onay |
| Away aemonday unsray asway ootray "orfay implicitysay" | Ethay assicclay — edicatedday userway ownay |
udosay -lay owsshay (ALLway) OPASSWDnay: ALLway | Ethay udoerssay iftgay atthay efeatsday udosay |
| Away orldway-itablewray etuidsay inarybay | indfay itway — ethay auditway ommandscay aboveway |
Exercisesway
ssay -tlnpayyourway oxbay — ustifyjay everyway istenerlay.indfay / -ermpay -4000— inventoryway etuidsay; esearchray oneway youway onday'tay ecognizeray.sshday -tayaway ardenedhay sshday_onfigcay — erifyvay eforebay eloadray.ausearchway -may avcwayorwayaaway-atusstay— eesay yourway ACmay ayerlay.- Itewray ethay inimalmay iptablesway olicypay omfray emorymay.