Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Paula
Paula

Posted on

     

Introduction to Linux Hardening

It's been a while! I've been working on so many projects recently, but one of the most important ones is related withLinux Hardening. This week I'm going to give a speech about it and I'd love to share a bit beforehand.

First of all, Linux Hardening is to enhance the security level of the system, mostly through low level commands to check and edit the basic/default OS values.

Encrypt

We can use LUKS for disk and volumes encryption. On the other hand we can count onopenssl orgpg for encrypt different kind of files. Let's take a look at these two options:

$ gpg --output nombre_salida --passphrase nuestra_contraseña --batch --no-tty --symmetric archivo_a_cifrar$ openssl rsautl -encrypt -pubin -inkey miclave.pub -ssl -in archivo_a_cifrar -out salida
Enter fullscreen modeExit fullscreen mode

First one let us encrypt large files using a passphrase, while the second one uses a public key but can't encrypt large files.

Permits

The commandls -ahl /home/ 2>/dev/null let us checkhome permits. We can review all writting permitis unless in one directory (let's call it "example") and its content usingfind / -perm -222 -type d -not -path "/ejemplo/*" and execution permits usingfind / -executable -type d. Thefind tool basically allow us looking for directories and files,-type d option search for directories, there are a lot of options:

  • b block (buffered) special
  • c character (unbuffered) special
  • d directory
  • p pipe (FIFO)
  • f regular file
  • l symbolic link
  • s socket
  • D door (Solaris)

It's very important to check for empty password in accounts. We can check this information in/etc/shadows. This is an example of an user account:

ejemplousuario:$6$XOivHvJZ$DthDIzmVnzMigsByXQ2diHJZ9LFbROkyGyXnZ.98t5vpECl96Jmk621hquET/z8fbS9L5n4sFvTsvMtkBSWJM/:17911:0:99999:7:::
Enter fullscreen modeExit fullscreen mode

Basically, in order we find: username, password algorithm, password, last time it was changed (since 1 of January of 1970), minimum days that must pass until password is changed, maximum days password is valid, days until the user is told to change the password, password expiration (not in the example) and absolute expiration date (not in the example). Although, we can check relevant password policy easier using
grep "^PASS_MAX_DAYS\|^PASS_MIN_DAYS\|^PASS_WARN_AGE\|^ENCRYPT_METHOD" /etc/login.defs 2>/dev/null.

awk -F: '($2 == "") {print}' /etc/shadow
Enter fullscreen modeExit fullscreen mode

This command let us check if an account has an empty password checking the second value of the structure explained. We can also use:

awk -F: '($3 == "0") {print}' /etc/passwd
Enter fullscreen modeExit fullscreen mode

to check that onlyroot has UID 0, if not we should take care of it.

Physical security

We shall not forget systems must be physically protected too! Let's use a password for both BIOS and GRUB Boot Loader. Disable USB boot, too. Fedora, CentOS and others can interactively startup using l key, we can disable it editing/etc/sysconfig/init and changing prompt to "no" inPROMPT=no line. If we disable USB Mass Storage driver we are limiting the USB devices in the system, avoiding attacks through rubber ducky, bad usb and such.

$ ls -l /lib/modules/$(uname -r)/kernel/drivers/usb/storage/usb-storage.ko
Enter fullscreen modeExit fullscreen mode

We can also have a blacklist of devices configuring ablacklist.conf in/etc/modprobe.d/.

SSH

We can check if root allows login via SSH using:

$ grep "PermitRootLogin " /etc/ssh/sshd_config 2>/dev/null | grep -v "#" | awk '{print  $2}'
Enter fullscreen modeExit fullscreen mode

Speaking of which, it's recommended to use a public key in order to perform a secure login.

$ ssh-keygen -t key_type -b bits -C "comentario"$ ssh-keygen -t ed25519 -C "Login al cluster de produccion"$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_aws_$(date +%Y-%m-%d) -C "AWS para clientes"
Enter fullscreen modeExit fullscreen mode

To install a key, we can usessh-copy-id. We can also limit access usingAllowUsers andDenyUsers, as well asPermitEmptyPasswords no to disable empty passwords.

Cron Jobs

Usingls -la /etc/cron* we can quickly check jobs programmed hourly, dayly monthly and weekly. We can seecron.d, for a modular scan of crontab files, but if we want to check individual live crontabs we can usels -la /var/spool/cron/crontabs.

Others

We could also like to check who else is connected at that time, usingw 2>/dev/null. We can also check users and groups withroot priviledges usinggrep -v -e '^$' /etc/sudoers |grep -v "#". We use that in order to avoid commented lines (#). Also/etc/login.defs contains interesting information of general user information,useradd andgroupadd for example uses values from this file. We could also try and check umask values usingumask -S & umask (symbolic-S and octal values). In order to avoid SNMP Reflection attack we can check default ports of SNMP usingcat /etc/services | grep -i snmp.

I encourage you all to experiment, script and play with this information, for the shake of security! :)

Top comments(8)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
thelonelyghost profile image
David Alexander
  • Location
    localhost
  • Work
    Senior Security Analyst @ Fortune 50 company
  • Joined

Also of note, automating these sorts of checks are a lot easier than you'd expect. There'sInSpec and theLinux baseline profile already built for it, and you can pretty easily write your own. No agent, nothing on the target server(s) except SSH and some basic tools that are probably already installed.

Blew my mind the first time I saw I could run a report and iterate through a fleet of servers with it.

CollapseExpand
 
terceranexus6 profile image
Paula
29 years old. Cyber. I really like bash and simple scripts. Solarpunk and free software advocate!
  • Location
    Many places
  • Education
    Computer science, fine arts
  • Pronouns
    she/her
  • Work
    Cybersecurity
  • Joined

I knew about automatization (mostly for monitoring purpose) but I didn't know about InSpec! I will give it a try, thanks!

CollapseExpand
 
erosen03 profile image
Eugene Rosenfeld
Eugene Rosenfeld has helped organizations implement $100M+ IT transformations, revamp critical business process, and report on $1T+ spending. Eugene has 20 years’ experience solving IT challenges.
  • Work
    Principal Architect at Owcer
  • Joined

Nice post about encryption. But there is a lot more to hardening systems. For further reading I would suggest checking out the CIS Benchmarks -cisecurity.org/cis-benchmarks/, or the Department of Defense Security Technical Implementation Guide (STIGs) -public.cyber.mil/stigs/. Both are based on the National Institute of Standards and Technology (NIST) guidance -nist.gov/.

CollapseExpand
 
mjjcha profile image
Judy Cha
Fullstack web dev, whatever that means.
  • Location
    Vancouver
  • Education
    B.Sc in Cell Biology (I know, peculiar)
  • Work
    Intermediate Web Dev
  • Joined

Thanks for this article, Paula! I mostly write application-level code, so infrastructure reliability/security good practices is not something I'm so familiar with. I'm really glad I happened upon your article today!

CollapseExpand
 
mboelen profile image
Michael Boelen
  • Joined

Another tool that you might help with your Linux hardening quest isLynis. The FOSS project exists since 2007 and is still maintained.

CollapseExpand
 
justinvincent2 profile image
Justin Vincent
  • Location
    Milwaukee
  • Joined

Paula, thanks so much for writing this! I learned a lot and the way you provided examples was very helpful.

CollapseExpand
 
terceranexus6 profile image
Paula
29 years old. Cyber. I really like bash and simple scripts. Solarpunk and free software advocate!
  • Location
    Many places
  • Education
    Computer science, fine arts
  • Pronouns
    she/her
  • Work
    Cybersecurity
  • Joined

Thank you! I'm glad you enjoyed!

CollapseExpand
 
ondrejs profile image
Ondrej
Philosophy, maths & human rights focused technology
  • Location
    .onion
  • Joined

Thanks for nice article! Some other things to mention is selecting distributions which use SELinux (e.g. CentOS) or hardening kernel with grsecurity patches (relevant mainly to Debian-Testing).

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

29 years old. Cyber. I really like bash and simple scripts. Solarpunk and free software advocate!
  • Location
    Many places
  • Education
    Computer science, fine arts
  • Pronouns
    she/her
  • Work
    Cybersecurity
  • Joined

More fromPaula

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp