Table of Contents for
Mastering Linux Security and Hardening

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering Linux Security and Hardening by Donald A. Tevault Published by Packt Publishing, 2018
  1. Mastering Linux Security and Hardening
  2. Title Page
  3. Copyright and Credits
  4. Mastering Linux Security and Hardening
  5. Packt Upsell
  6. Why subscribe?
  7. PacktPub.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the color images
  18. Conventions used
  19. Get in touch
  20. Reviews
  21. Running Linux in a Virtual Environment
  22. The threat landscape
  23. So, how does this happen?
  24. Keeping up with security news
  25. Introduction to VirtualBox and Cygwin
  26. Installing a virtual machine in VirtualBox
  27. The EPEL repository on the CentOS virtual machine
  28. Configuring a network for VirtualBox virtual machines
  29. Creating a virtual machine snapshot with VirtualBox
  30. Using Cygwin to connect to your virtual machines
  31. Installing Cygwin on your Windows host
  32. Summary
  33. Securing User Accounts
  34. The dangers of logging in as the root user
  35. The advantages of using sudo
  36. Setting up sudo privileges for full administrative users
  37. Method 1 – adding users to a predefined admin group
  38. Method 2 – creating an entry in the sudo policy file
  39. Setting up sudo for users with only certain delegated privileges
  40. Hands-on lab for assigning limited sudo privileges
  41. Advanced tips and tricks for using sudo
  42. The sudo timer
  43. Hands-on lab for disabling the sudo timer
  44. Preventing users from having root shell access
  45. Preventing users from using shell escapes
  46. Preventing users from using other dangerous programs
  47. Limiting the user's actions with commands
  48. Letting users run as other users
  49. Locking down users' home directories the Red Hat or CentOS way
  50. Locking down users' home directories the Debian/Ubuntu way
  51. useradd on Debian/Ubuntu
  52. adduser on Debian/Ubuntu
  53. Hands-on lab for configuring adduser
  54. Enforcing strong password criteria
  55. Installing and configuring pwquality
  56. Hands-on lab for setting password complexity criteria
  57. Setting and enforcing password and account expiration
  58. Configuring default expiry data for useradd – for Red Hat or CentOS only
  59. Setting expiry data on a per-account basis, with useradd and usermod
  60. Setting expiry data on a per-account basis, with chage
  61. Hands-on lab for setting account and password expiry data
  62. Preventing brute-force password attacks
  63. Configuring the pam_tally2 PAM module
  64. Hands-on lab for configuring pam_tally2
  65. Locking user accounts
  66. Using usermod to lock a user account
  67. Using passwd to lock user accounts
  68. Locking the root user account
  69. Setting up security banners
  70. Using the motd file
  71. Using the issue file
  72. Using the issue.net file
  73. Summary
  74. Securing Your Server with a Firewall
  75. An overview of iptables
  76. Basic usage of iptables
  77. Hands-on lab for basic iptables usage
  78. Uncomplicated Firewall for Ubuntu systems
  79. Basic usage of ufw
  80. Hands-on lab for basic ufw usage
  81. firewalld for Red Hat systems
  82. Verifying the status of firewalld
  83. firewalld zones
  84. firewalld services
  85. Adding ports to a firewalld zone
  86. firewalld rich language rules
  87. Hands-on lab for firewalld commands
  88. nftables – a more universal type of firewall system
  89. nftables tables and chains
  90. Getting started with nftables
  91. Using nft commands
  92. Hands-on lab for nftables on Ubuntu
  93. Summary
  94. Encrypting and SSH Hardening
  95. GNU Privacy Guard
  96. Creating your GPG keys
  97. Symmetrically encrypting your own files
  98. Hands-on lab – combining gpg and tar for encrypted backups
  99. Using private and public keys for asymmetric encryption and signing
  100. Signing a file without encryption
  101. Encrypting partitions with Linux Unified Key Setup – LUKS
  102. Disk encryption during operating system installation
  103. Adding an encrypted partition with LUKS
  104. Configuring the LUKS partition to mount automatically
  105. Encrypting directories with eCryptfs
  106. Home directory and disk encryption during Ubuntu installation
  107. Encrypting a home directory for a new user account
  108. Creating a private directory within an existing home directory
  109. Encrypting other directories with eCryptfs
  110. Encrypting the swap partition with eCryptfs
  111. Using VeraCrypt for cross-platform sharing of encrypted containers
  112. Getting and installing VeraCrypt
  113. Creating and mounting a VeraCrypt volume in console mode
  114. Using VeraCrypt in GUI mode
  115. Ensuring that SSH protocol 1 is disabled
  116. Creating and managing keys for password-less logins
  117. Creating a user's SSH key set
  118. Transferring the public key to the remote server
  119. Disabling root user login
  120. Disabling username/password logins
  121. Setting up a chroot environment for SFTP users
  122. Creating a group and configuring the sshd_config file
  123. Hands-on lab – setting up a chroot directory for sftpusers group
  124. Summary
  125. Mastering Discretionary Access Control
  126. Using chown to change ownership of files and directories
  127. Using chmod to set permissions values on files and directories
  128. Setting permissions with the symbolic method
  129. Setting permissions with the numerical method
  130. Using SUID and SGID on regular files
  131. The security implications of the SUID and SGID permissions
  132. Finding spurious SUID or SGID files
  133. Hands-on lab – searching for SUID and SGID files
  134. Preventing SUID and SGID usage on a partition
  135. Using extended file attributes to protect sensitive files
  136. Setting the a attribute
  137. Setting the i attribute
  138. Hands-on lab – setting security-related extended file attributes
  139. Summary
  140. Access Control Lists and Shared Directory Management
  141. Creating an access control list for either a user or a group
  142. Creating an inherited access control list for a directory
  143. Removing a specific permission by using an ACL mask
  144. Using the tar --acls option to prevent the loss of ACLs during a backup
  145. Creating a user group and adding members to it
  146. Adding members as we create their user accounts
  147. Using usermod to add an existing user to a group
  148. Adding users to a group by editing the /etc/group file
  149. Creating a shared directory
  150. Setting the SGID bit and the sticky bit on the shared directory
  151. Using ACLs to access files in the shared directory
  152. Setting the permissions and creating the ACL
  153. Charlie tries to access Vicky's file with an ACL set for Cleopatra
  154. Hands-on lab – creating a shared group directory
  155. Summary
  156. Implementing Mandatory Access Control with SELinux and AppArmor
  157. How SELinux can benefit a systems administrator
  158. Setting security contexts for files and directories
  159. Installing the SELinux tools
  160. Creating web content files with SELinux enabled
  161. Fixing an incorrect SELinux context
  162. Using chcon
  163. Using restorecon
  164. Using semanage
  165. Hands-on lab – SELinux type enforcement
  166. Troubleshooting with setroubleshoot
  167. Viewing setroubleshoot messages
  168. Using the graphical setroubleshoot utility
  169. Troubleshooting in permissive mode
  170. Working with SELinux policies
  171. Viewing the Booleans
  172. Configuring the Booleans
  173. Protecting your web server
  174. Protecting network ports
  175. Creating custom policy modules
  176. Hands-on lab – SELinux Booleans and ports
  177. How AppArmor can benefit a systems administrator
  178. Looking at AppArmor profiles
  179. Working with AppArmor command-line utilities
  180. Troubleshooting AppArmor problems
  181. Summary
  182. Scanning, Auditing, and Hardening
  183. Installing and updating ClamAV and maldet
  184. Installing ClamAV and maldet
  185. Configuring maldet
  186. Updating ClamAV and maldet
  187. Scanning with ClamAV and maldet
  188. SELinux considerations
  189. Scanning for rootkits with Rootkit Hunter
  190. Installing and updating Rootkit Hunter
  191. Scanning for rootkits
  192. Controlling the auditd daemon
  193. Creating audit rules
  194. Auditing a file for changes
  195. Auditing a directory
  196. Auditing system calls
  197. Using ausearch and aureport
  198. Searching for file change alerts
  199. Searching for directory access rule violations
  200. Searching for system call rule violations
  201. Generating authentication reports
  202. Using predefined rules sets
  203. Applying OpenSCAP policies with oscap
  204. Installing OpenSCAP
  205. Viewing the profile files
  206. Scanning the system
  207. Remediating the system
  208. Using SCAP Workbench
  209. More about OpenSCAP profiles
  210. Applying an OpenSCAP profile during system installation
  211. Summary
  212. Vulnerability Scanning and Intrusion Detection
  213. Looking at Snort and Security Onion
  214. Obtaining and installing Snort
  215. Graphical interfaces for Snort
  216. Getting Snort in prebuilt appliances
  217. Using Security Onion
  218. Scanning and hardening with Lynis
  219. Installing Lynis on Red Hat/CentOS
  220. Installing Lynis on Ubuntu
  221. Scanning with Lynis
  222. Finding vulnerabilities with OpenVAS
  223. Web server scanning with Nikto
  224. Nikto in Kali Linux
  225. Installing and updating Nikto on Linux
  226. Scanning a web server with Nikto
  227. Summary
  228. Security Tips and Tricks for the Busy Bee
  229. Auditing system services
  230. Auditing system services with systemctl
  231. Auditing network services with netstat
  232. Auditing network services with Nmap
  233. Port states
  234. Scan types
  235. Password-protecting the GRUB 2 bootloader
  236. Resetting the password for Red Hat/CentOS
  237. Resetting the password for Ubuntu
  238. Preventing kernel parameter edits on Red Hat/CentOS
  239. Preventing kernel parameter edits on Ubuntu
  240. Password-protecting boot options
  241. Disabling the submenu for Ubuntu
  242. Password-protecting boot option steps for both Ubuntu and Red Hat
  243. Securely configuring BIOS/UEFI
  244. Using a security checklist for system setup
  245. Summary
  246. Other Books You May Enjoy
  247. Leave a review – let other readers know what you think

Password-protecting boot option steps for both Ubuntu and Red Hat

From here on out, the steps are the same for both the CentOS and the Ubuntu virtual machines, except for the following:

  • On your Ubuntu machine, the grub.cfg file is in the /boot/grub/ directory.  On your CentOS machine, it's in the /boot/grub2/ directory.
  • On Ubuntu, the /boot/grub/ and /etc/grub.d/ directories are world-readable. So, you can cd into them as a normal user. 
  • On CentOS, the /boot/grub2/ and /etc/grub.d/ directories are restricted to the root user. So, to cd into them, you'll need to log in to the root user's shell. Alternatively, you can list the contents from your normal user shell with sudo ls -l, and you can edit the files you need to edit with sudo vim /boot/grub2/grub.cfg or sudo vim /etc/grub.d/40_custom. (Substitute your favorite editor for vim.)
  • On Ubuntu, the command to create a password hash is grub-mkpasswd-pbkdf2. On CentOS, the command is grub2-mkpasswd-pbkdf2.

With these slight differences in mind, let's get started.

If you're working with a server that's just running with a text-mode interface, you'll definitely want to log in remotely from a workstation that has a GUI-type interface. If your workstation is running Windows, you can use Cygwin, as I showed you in Chapter 1, Running Linux in a Virtual Environment.

The reason for this is that you'll want a way to copy and paste the password hashes into the two files that you need to edit.

The first thing you'll do is to create a password hash for your new users:

  • On Ubuntu:
        grub-mkpasswd-pbkdf2
  • On CentOS:
        grub2-mkpasswd-pbkdf2

Next, open the /etc/grub.d/40_custom file in your text editor and add a line for your new user, along with the password hash that you just created. The line should look something like this:

password_pbkdf2 goldie grub.pbkdf2.sha512.10000.225205CBA2584240624D077ACB84E86C70349BBC00DF40A219F88E5691FB222DD6E2F7765E96C63C4A8FA3B41BDBF62DA1F3B07C700D78BC5DE524DCAD9DD88B.9655985015C3BEF29A7B8E0A6EA42599B1152580251FF99AA61FE68C1C1209ACDCBBBDAA7A97D4FC4DA6984504923E1449253024619A82A57CECB1DCDEE53C06

Note that this is all one line that wraps around on the printed page.

What you're supposed to do next is to run a utility that will read all of the files in the /etc/grub.d/ directory along with the /etc/default/grub file and that will then rebuild the grub.cfg file. But, on CentOS, that utility doesn't work correctly. On Ubuntu, it does work correctly, but it will overwrite any changes that you might have already made to the grub.cfg file. So, we're going to cheat.

Open the grub.cfg file in your text editor:

  • On Ubuntu:
        sudo vim /boot/grub/grub.cfg
  • On CentOS:
        sudo vim /boot/grub2/grub.cfg

Scroll down until you see the ### BEGIN /etc/grub.d/40_custom ### section. In this section, copy and paste the line that you just added to the 40_custom file. This section should now look something like this:

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
password_pbkdf2 "goldie" grub.pbkdf2.sha512.10000.225205CBA2584240624D077ACB84E86C70349BBC00DF40A219F88E5691FB222DD6E2F7765E96C63C4A8FA3B41BDBF62DA1F3B07C700D78BC5DE524DCAD9DD88B.9655985015C3BEF29A7B8E0A6EA42599B1152580251FF99AA61FE68C1C1209ACDCBBBDAA7A97D4FC4DA6984504923E1449253024619A82A57CECB1DCDEE53C06
### END /etc/grub.d/40_custom ###

Finally, you're ready to password-protect the individual menu entries. And here, I've discovered yet another difference between Ubuntu and CentOS.

In all of the menu entries for CentOS, you'll see that the --unrestricted option is already there for all menu entries. This means that by default, all users are allowed to boot every menu option, even if you've set a superuser password:

menuentry 'CentOS Linux (3.10.0-693.11.1.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-f338b70d-ff57-404e-a349-6fd84ad1b692' {

So on CentOS, you don't have to do anything if you want all users to be able to use all available boot options.

Now, let's say that you have a menuentry that you want to be accessible to everybody. On CentOS, as I just pointed out, you don't have to do anything. On Ubuntu, add --unrestricted to the menuentry, as you did previously:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-f0f002e8-16b2-45a1-bebc-41e518ab9497' --unrestricted {

If you want for nobody but the superuser to boot from a particular option, add --users "". (On CentOS, be sure to remove the --unrestricted option first.)

menuentry 'Ubuntu, with Linux 4.4.0-98-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.4.0-98-generic-recovery-f0f002e8-16b2-45a1-bebc-41e518ab9497' --users "" {

If you want for only the superuser and some other particular user to boot from a certain option, add --users, followed by the username. (Again, on CentOS, remove the --unrestricted option first.):

menuentry 'Ubuntu, with Linux 4.4.0-97-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.4.0-97-generic-advanced-f0f002e8-16b2-45a1-bebc-41e518ab9497' --users goldie {

If you have more than one user that you want to access a boot option, add an entry for the new user in the ### BEGIN /etc/grub.d/40_custom ### section. Then, add the new user to the menuentry that you want for him or her to access. Separate the usernames with a comma:

menuentry 'Ubuntu, with Linux 4.4.0-97-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.4.0-97-generic-advanced-f0f002e8-16b2-45a1-bebc-41e518ab9497' --users goldie,frank {

Save the file and reboot to try out the different options.

Now that we've gone through all this work, I need to remind you again that any manual edits that you've made to the grub.cfg file will be lost any time that a new grub.cfg gets generated. So, any time you do a system update that includes either installing or removing a kernel, you'll need to manually edit this file again to add back the password protection. (In fact, the only real reason I had you add the users and their passwords to the /etc/grub.d/40_custom file is so that you'll always have that information available to copy and paste into grub.cfg.) I wish that there were a more elegant way of doing this, but according to the official GRUB 2 documentation, there isn't.

You'll find the security section of the official GRUB 2 documentation at http://www.gnu.org/software/grub/manual/grub/grub.html#Security.

Before we leave this topic, I'd like to share my personal thoughts about GRUB 2.

It was necessary to create a new version of GRUB because the old legacy version doesn't work with the new UEFI-based motherboards. However, there are things about GRUB 2 that are very disappointing. 

In the first place, unlike legacy GRUB, GRUB 2 isn't implemented consistently across all Linux distros. In fact, we've just seen in our demos how we have to do things differently when we go from CentOS to Ubuntu.

Next is the fact that the GRUB 2 developers gave us some good security options, but they haven't given us an elegant way of implementing them. I mean, really. The whole idea of telling us to implement security features by hand-editing a file that will get overwritten every time we do an operating system update just doesn't seem right.

And finally, there's the sad state of GRUB 2 documentation. I don't mean to sound like I'm tooting my own horn because I know that that's unbecoming. However, I think it's safe to say that this is the only comprehensive write-up you'll find anywhere for using the password-protection features of GRUB 2.