As a diehard macOS user, i never had the need to scan my system for security flaws. I came across Lynis last week, and now security hardening seems like stealing candy from a baby. I’ll be using the community version.

What is Lynis ?

Lynis is an open source security auditing tool for Unix based operating system. It will scan the system for insecure services, configuration files, firewall settings, and so on.

Enough talk, let’s dig in.

Installation

If you have homebrew installed, run:

brew install lynis

and execute:

lynis audit system

or clone it from Github, change into the directory and execute:

./lynis audit system

Alright let’s audit the system:

[+] Initializing program
------------------------------------
  - Detecting OS...                                           [ DONE ]
  - Checking profiles...                                      [ DONE ]

  ---------------------------------------------------
  Program version:           2.7.3
  Operating system:          macOS
  Operating system name:     Mac OS X
  Operating system version:  10.13.6
  Kernel version:            17.7.0
  Hardware platform:         x86_64
  Hostname:                  MacBook-Pro
  ---------------------------------------------------
  Profiles:                  /Users/kavish/lynis/default.prf
  Log file:                  /var/log/lynis.log
  Report file:               /var/log/lynis-report.dat
  Report version:            1.0
  Plugin directory:          ./plugins
  ---------------------------------------------------
  Auditor:                   [Not Specified]
  Language:                  en
  Test category:             all
  Test group:                all
  ---------------------------------------------------
  - Program update status...                                  [ NO UPDATE ]

[+] System Tools
------------------------------------
  - Scanning available tools...
  - Checking system binaries...

[+] Plugins (phase 1)
------------------------------------
 Note: plugins have more extensive tests and may take several minutes to complete

  - Plugin: pam
    [..]
  - Plugin: systemd
    [................]

[+] Boot and services
------------------------------------
  - Service Manager                                           [ launchd ]

[+] Kernel
------------------------------------

[+] Memory and Processes
------------------------------------
  - Searching for dead/zombie processes                       [ OK ]
  - Searching for IO waiting processes                        [ OK ]

[+] Users, Groups and Authentication
------------------------------------
  - Administrator accounts                                    [ OK ]
  - Unique UIDs                                               [ OK ]
  - Unique group IDs                                          [ OK ]
  - Unique group names                                        [ OK ]
  - Query system users (non daemons)                          [ DONE ]
  - sudoers file                                              [ FOUND ]
    - Permissions for directory: /etc/sudoers.d               [ WARNING ]
    - Permissions for: /etc/sudoers                           [ OK ]
  - PAM password strength tools                               [ SUGGESTION ]
  - PAM configuration file (pam.conf)                         [ NOT FOUND ]
  - PAM configuration files (pam.d)                           [ FOUND ]
  - Determining default umask
    - umask (/etc/profile and /etc/profile.d)                 [ OK ]

[+] Shells
------------------------------------
  - Checking shells from /etc/shells
    Result: found 6 shells (valid shells: 6).
    - Session timeout settings/tools                          [ NONE ]
  - Checking default umask values
    - Checking default umask in /etc/bashrc                   [ NONE ]
    - Checking default umask in /etc/csh.cshrc                [ NONE ]
    - Checking default umask in /etc/profile                  [ NONE ]

[+] File systems
------------------------------------
  - Checking mount points
    - Checking /home mount point                              [ SUGGESTION ]
    - Checking /tmp mount point                               [ SYMLINK ]
    - Checking /var mount point                               [ SYMLINK ]
  - Query swap partitions (fstab)                             [ NONE ]
  - Testing swap partitions                                   [ OK ]
  - Checking for old files in /tmp                            [ OK ]
  - Checking /var/tmp sticky bit                              [ OK ]

[+] USB Devices
------------------------------------

[+] Storage
------------------------------------

[+] NFS
------------------------------------
  - Query rpc registered programs                             [ DONE ]
  - Query NFS versions                                        [ DONE ]
  - Query NFS protocols                                       [ DONE ]
  - Check running NFS daemon                                  [ NOT FOUND ]

[+] Name services
------------------------------------
  - Searching DNS domain name                                 [ FOUND ]
      Domain name: local
  - Checking /etc/hosts
    - Checking /etc/hosts (duplicates)                        [ OK ]
    - Checking /etc/hosts (hostname)                          [ SUGGESTION ]
    - Checking /etc/hosts (localhost)                         [ OK ]

[+] Ports and packages
------------------------------------
  - Searching package managers
    - Searching brew                                          [ FOUND ]
    - Querying brew for installed packages
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
    - Searching packages with port                            [ {STATUS_FOUND} ]
      - Querying port for installed packages
Error: Current platform "darwin 17" does not match expected platform "darwin 15"
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch
  - Checking ports for updates                                [ FOUND ]
  - Checking package audit tool                               [ NONE ]

[+] Networking
------------------------------------
  - Checking configured nameservers
    - Testing nameservers
        Nameserver: 192.168.100.1                             [ OK ]
    - Minimal of 2 responsive nameservers                     [ WARNING ]
  - Checking default gateway                                  [ DONE ]
  - Getting listening ports (TCP/UDP)                         [ DONE ]
      * Found 43 ports
  - Checking waiting connections                              [ OK ]
  - Checking status DHCP client                               [ NOT ACTIVE ]

[+] Printers and Spools
------------------------------------
  - Checking cups daemon                                      [ NOT FOUND ]
  - Checking lp daemon                                        [ NOT RUNNING ]

[+] Software: e-mail and messaging
------------------------------------

[+] Software: firewalls
------------------------------------
  - Checking pf status (pfctl)                                [ ENABLED ]
    - Checking pf configuration consistency                   [ OK ]
  - Checking macOS: Application Firewall                      [ ENABLED ]
  - Checking host based firewall                              [ ACTIVE ]

[+] Software: webserver
------------------------------------
  - Checking Apache (binary /usr/sbin/httpd)                  [ FOUND ]
      Info: Configuration file found (/private/etc/apache2/httpd.conf)
      Info: Found 8 virtual hosts
    * Loadable modules                                        [ FOUND (110) ]
        - Found 110 loadable modules
          mod_evasive: anti-DoS/brute force                   [ NOT FOUND ]
          mod_reqtimeout/mod_qos                              [ FOUND ]
          ModSecurity: web application firewall               [ NOT FOUND ]
  - Checking nginx                                            [ NOT FOUND ]

[+] SSH Support
------------------------------------
  - Checking running SSH daemon                               [ NOT FOUND ]

[+] SNMP Support
------------------------------------
  - Checking running SNMP daemon                              [ NOT FOUND ]

[+] Databases
------------------------------------
    No database engines found

[+] LDAP Services
------------------------------------
  - Checking OpenLDAP instance                                [ NOT FOUND ]

[+] PHP
------------------------------------
  - Checking PHP                                              [ FOUND ]
    - Checking PHP disabled functions                         [ FOUND ]
    - Checking expose_php option                              [ ON ]
    - Checking enable_dl option                               [ OFF ]
    - Checking allow_url_fopen option                         [ ON ]
    - Checking allow_url_include option                       [ OFF ]

[+] Squid Support
------------------------------------
  - Checking running Squid daemon                             [ NOT FOUND ]

[+] Logging and files
------------------------------------
  - Checking for a running log daemon                         [ OK ]
    - Checking Syslog-NG status                               [ NOT FOUND ]
    - Checking systemd journal status                         [ NOT FOUND ]
    - Checking Metalog status                                 [ NOT FOUND ]
    - Checking RSyslog status                                 [ NOT FOUND ]
    - Checking RFC 3195 daemon status                         [ NOT FOUND ]
  - Checking remote logging                                   [ ENABLED ]
  - Checking /etc/newsyslog.conf                              [ FOUND ]
    - Checking log directories (newsyslog.conf)               [ DONE ]
    - Checking log files (newsyslog.conf)                     [ DONE ]
  - Checking log directories (static list)                    [ DONE ]
  - Checking open log files                                   [ DONE ]
  - Checking deleted files in use                             [ FILES FOUND ]

[+] Insecure services
------------------------------------
  - Checking inetd status                                     [ NOT ACTIVE ]
Unrecognized subcommand: print-enabled
  -                                                           [ OK ]

[+] Banners and identification
------------------------------------
  - /etc/issue                                                [ NOT FOUND ]
  - /etc/issue.net                                            [ NOT FOUND ]

[+] Scheduled tasks
------------------------------------
  - Checking crontab and cronjob files                        [ DONE ]

[+] Accounting
------------------------------------

[+] Time and Synchronization
------------------------------------
  - NTP daemon found: timed                                   [ FOUND ]
  - Checking for a running NTP daemon or client               [ OK ]

[+] Cryptography
------------------------------------
  - Checking for expired SSL certificates [1/1]               [ FOUND ]

[+] Virtualization
------------------------------------

[+] Containers
------------------------------------

[+] Security frameworks
------------------------------------
  - Checking presence AppArmor                                [ NOT FOUND ]
  - Checking presence SELinux                                 [ NOT FOUND ]
  - Checking presence TOMOYO Linux                            [ NOT FOUND ]
  - Checking presence grsecurity                              [ NOT FOUND ]
  - Checking for implemented MAC framework                    [ NONE ]

[+] Software: file integrity
------------------------------------
  - Checking file integrity tools
    - mtree                                                   [ FOUND ]
  - Checking presence integrity tool                          [ FOUND ]

[+] Software: System tooling
------------------------------------
  - Checking automation tooling
  - Automation tooling                                        [ NOT FOUND ]
  - Checking for IDS/IPS tooling                              [ NONE ]

[+] Software: Malware
------------------------------------

[+] File Permissions
------------------------------------
  - Starting file permissions check

[+] Home directories
------------------------------------
  - Checking shell history files                              [ OK ]

[+] Kernel Hardening
------------------------------------

[+] Hardening
------------------------------------
    - Installed compiler(s)                                   [ FOUND ]
    - Installed malware scanner                               [ FOUND ]

[+] Custom Tests
------------------------------------
  - Running custom tests...                                   [ NONE ]

[+] Plugins (phase 2)
------------------------------------
  - Plugins (phase 2)                                         [ DONE ]

================================================================================

  -[ Lynis 2.7.3 Results ]-

  Warnings (1):
  ----------------------------
  ! Couldn't find 2 responsive nameservers [NETW-2705]
      https://cisofy.com/lynis/controls/NETW-2705/

  Suggestions (15):
  ----------------------------
  * Install a PAM module for password strength testing like pam_cracklib or pam_passwdqc [AUTH-9262]
      https://cisofy.com/lynis/controls/AUTH-9262/

  * To decrease the impact of a full /home file system, place /home on a separate partition [FILE-6310]
      https://cisofy.com/lynis/controls/FILE-6310/

  * Symlinked mount point needs to be checked manually [FILE-6310]
    - Details  : /tmp
    - Solution :
      https://cisofy.com/lynis/controls/FILE-6310/

  * Symlinked mount point needs to be checked manually [FILE-6310]
    - Details  : /var
    - Solution :
      https://cisofy.com/lynis/controls/FILE-6310/

  * Add the IP name and FQDN to /etc/hosts for proper name resolving [NAME-4404]
      https://cisofy.com/lynis/controls/NAME-4404/

  * Install a package audit tool to determine vulnerable packages [PKGS-7398]
      https://cisofy.com/lynis/controls/PKGS-7398/

  * Check your resolv.conf file and fill in a backup nameserver if possible [NETW-2705]
      https://cisofy.com/lynis/controls/NETW-2705/

  * Install Apache mod_evasive to guard webserver against DoS/brute force attempts [HTTP-6640]
      https://cisofy.com/lynis/controls/HTTP-6640/

  * Install Apache modsecurity to guard webserver against web application attacks [HTTP-6643]
      https://cisofy.com/lynis/controls/HTTP-6643/

  * Turn off PHP information exposure [PHP-2372]
    - Details  : expose_php = Off
      https://cisofy.com/lynis/controls/PHP-2372/

  * Change the allow_url_fopen line to: allow_url_fopen = Off, to disable downloads via PHP [PHP-2376]
      https://cisofy.com/lynis/controls/PHP-2376/

  * Check what deleted files are still in use and why. [LOGG-2190]
      https://cisofy.com/lynis/controls/LOGG-2190/

  * Check available certificates for expiration [CRYP-7902]
      https://cisofy.com/lynis/controls/CRYP-7902/

  * Determine if automation tools are present for system management [TOOL-5002]
      https://cisofy.com/lynis/controls/TOOL-5002/

  * Harden compilers like restricting access to root user only [HRDN-7222]
      https://cisofy.com/lynis/controls/HRDN-7222/

  Follow-up:
  ----------------------------
  - Show details of a test (lynis show details TEST-ID)
  - Check the logfile for all details (less /var/log/lynis.log)
  - Read security controls texts (https://cisofy.com)
  - Use --upload to upload data to central system (Lynis Enterprise users)

================================================================================

  Lynis security scan details:

  Hardening index : 78 [###############     ]
  Tests performed : 167
  Plugins enabled : 2

  Components:
  - Firewall               [V]
  - Malware scanner        [V]

  Lynis modules:
  - Compliance status      [?]
  - Security audit         [V]
  - Vulnerability scan     [V]

  Files:
  - Test and debug information      : /var/log/lynis.log
  - Report data                     : /var/log/lynis-report.dat

================================================================================

Funnily enough, i was not expecting this.

As you can see, at the end of the scan it will print a report of warnings and suggestions about what security measures you have to take to hardened your system. It’s pretty straightforward. The above information are logged with more details in /var/log/lynis.log and /var/log/lynis-report.dat. I won’t fix all those issues. I don’t allow incoming traffics. Try it on OpenBSD or Linux, and try to fix the issues. Maybe write a script to automate the fixes on multiple machines. It’s fun.

Note: The suggestion about “Check what deleted files are still in use and why - LOGG-2190]” should not be taken seriously. Some applications create files for temporary use (specially web browsers) and auto-deletes when the application exits.

Run ‘lsof’ to get an understanding.

The syntax is - lsof | grep -e [command] -e '\(deleted\)'. Let’s take a look:

MacBook-Pro:~ kavish$ lsof | grep -e 'safari' -e '\(deleted\)' | wc -l
11

“Lost Time is never found again.” ― Benjamin Franklin