EDUCATION, HARDENING, CM-7

🛡️ Reduction of the attack surface

Each service, port and auto-run task that is not needed is a door that someone can open. This guide shows how to find them and close them in Windows, macOS and Linux systems by signing in, closing them and opening them back. Teams are designed for their systems and first for the test environment.

NIST SP 800-53 CM-7, OWASP

What is the surface of the attack

The attack surface (attack surface) is a set of places where an attacker can try to enter data, get data or run code. Irritability is an error in one of these places. Reducing the surface means fewer places where there can be a mistake, so it also works against injuries that nobody knows about yet.

Three attack surfaces and their entry points Digital, physical and human surface, examples of three entry points for each. This guide covers the digital surface. Surface is the sum of all entry points DIGITAL Open Ports and Services Software and API Accounts and Access PHYSICO USB ports and devices Server rooms Non-office laptops HUMAN Fishing Recurrent Passwords Excessive access This guide - digital surface: services, ports, automatic launch, configuration

Physical and human surface are described in guides on physical and personnel security.

Minimum functionality

NIST SP 800-53 CM-7 requires the system to be configured so that it only does what it is designed for and prohibits unnecessary features, ports, protocols and services. CIS Controls v.8.1 Control 4 requires the same as a secure configuration part.

Why It Benefites

The off service does not need to be updated and monitored and cannot be used by an attacker. Each excluded component means less vulnerability in the next update list.

Before changing

You know what the system serves, record the current situation and change one step. Each change team in this guide has a team that returns it.

Start with

The sequence follows CIS Controls v8.1: first know what is (1st and 2nd controls), then reduce and strengthen configuration or hardening (4th controls), then check. Each step leads to its own tab.

  1. List your devices and installed software
  2. Disabling services with no owner
  3. Enable firewalls that prevent incoming traffic by default
  4. Automatically startup reports and keep a list
  5. Choose one baseline and sizes for compliance
  6. Record today's state of affairs and plan the next review

CIS Controls v.8.1 1-2, ISO/IEC 27002 5.9, NIST CM-8

Cannot reduce what you don't know about

The first step is to list what devices are on the network and what software is installed on them. Each row answers one question - who needs it? A row without answer is a candidate for removal.

Windows main example

Installed apps and Windows additional resources. PowerShell:

Get-Package | Sort-Object Name |
  Select-Object Name, Version
Get-WindowsOptionalFeature -Online |
  Where-Object State -eq Enabled

Command line (cmd). The classic form is wmic, but it is outdated (Windows 11 24H2 does not include it anymore) and slow - it checks every MSI package. The modern form of the same list is winget:

wmic product get name,version
winget list --source winget
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall

macOS secondary

Applications, Homebrew packages and configuration profiles.

system_profiler SPApplicationsDataType |
  grep -E "^    [A-Za-z]"
brew list --versions
sudo profiles list

Linux secondary

Packages (Debian/Ubuntu or RHEL/Fedora) and Snap/Flatpak.

# Debian, Ubuntu
apt list --installed
# RHEL, Fedora
rpm -qa --last | head -50
snap list; flatpak list

Equipment

At network level, the inventory is made up of DHCP and DNS logs, switchboard MAC tables and active scans in your network. An unknown device is a finding, not a noise.

Nutritional, not single

The inventory is ageing with each installation. It is collected automatically (MDM, Intune, Ansible Facts, EDR) and compared with the previous one - the new is what is checked.

Next step: 2. Services →

CM-7, CIS Benchmarks, ISO/IEC 27002 8.9

Needless services and functions

A service that runs in the background, listens to the network or processes files is a code that executes with elevated rights. If the system does not need it, it shall be stopped and run prohibited. Examples below are typical candidates - make sure that they are not really needed in your environment before switching off.

Windows

Services that run automatically and are currently running.

Get-Service |
  Where-Object { $_.StartType -eq 'Automatic' } |
  Where-Object { $_.Status -eq 'Running' } |
  Sort-Object Name | Select-Object Name, DisplayName

Print on Spooler server not printed (PrintNightmare, CVE-2021-34527):

Disable

Stop-Service Spooler
Set-Service Spooler -StartupType Disabled

Revert

Set-Service Spooler -StartupType Automatic
Start-Service Spooler

SMBv1 - an outdated protocol used by WannaCry:

Disable

Disable-WindowsOptionalFeature -Online `
  -FeatureName SMB1Protocol -NoRestart

Revert

Enable-WindowsOptionalFeature -Online -FeatureName SMB1Protocol

Checking for success

Get-Service Spooler | Select-Object Name, Status, StartType
Get-WindowsOptionalFeature -Online -FeatureName SMB1Protocol |
  Select-Object FeatureName, State

macOS

Loaded launchd works and off system services. Shared services are more convenient to manage System Settings, General, Sharing.

sudo launchctl list | grep -v com.apple
sudo launchctl print-disabled system

Remote login (SSH) if not in use. Terminal needs Full Disk Access:

Disable

sudo systemsetup -setremotelogin off

Revert

sudo systemsetup -setremotelogin on

Screen sharing:

Disable

sudo launchctl disable system/com.apple.screensharing

Revert

sudo launchctl enable system/com.apple.screensharing

Checking for success

sudo systemsetup -getremotelogin
sudo launchctl print-disabled system | grep screensharing

Linux

Enabled and operating systemd units.

systemctl list-unit-files --type=service --state=enabled
systemctl list-units --type=service --state=running

Printer service on the server (including scant activation):

Disable

sudo systemctl disable --now cups.service cups.socket cups.path

Revert

sudo systemctl enable --now cups.service cups.socket cups.path

mDNA (Avahi) - announce the device on the local network:

Disable and prohibit startup

sudo systemctl disable --now avahi-daemon.service avahi-daemon.socket
sudo systemctl mask avahi-daemon.service avahi-daemon.socket

Revert

sudo systemctl unmask avahi-daemon.service avahi-daemon.socket
sudo systemctl enable --now avahi-daemon.service

Checking for success

systemctl is-enabled cups.service avahi-daemon.service
systemctl is-active cups.service avahi-daemon.service

Disable or Remove

The off service is still disk and the next update can be turned on again. If software is not needed at all, it is removed - then it also disappears from the vulnerability scanner report.

disable against mask

systemd disable removes the automatic startup, but another unit can still run the service. mask prohibits it completely - it is used for services that never need to start.

Next step: 3. Network communication →

CIS Controls v.8.1 12-13, ISO/IEC 27002 8.20-8.22

Open ports and network traffic

An open port is a door waiting for a knocker. First, find out which process listens and which address, then close the excess and firewalls set to the lock by default. The second direction is the outgoing traffic: what the system itself sends out and where.

Open ports (listen) before and after reduction An imaginary example: before reducing twelve open ports, then two. Before: 12 open ports 22 443 80 111 135 139 445 631 3389 5353 5900 8080 printing, file sharing, remote desktop, test server After: 2 open ports 22 443 only from admin network public service firewall: incoming denied by default An imaginary example: each port removed has one entry point less

Windows

Open ports with process name and firewall profiles.

Get-NetTCPConnection -State Listen |
  Select-Object LocalAddress, LocalPort,
  @{ n='Process';
     e={ (Get-Process -Id $_.OwningProcess).ProcessName } }
Get-NetFirewallProfile |
  Select-Object Name, Enabled, DefaultInboundAction

Incoming denied by default

Set-NetFirewallProfile -Profile Domain,Private,Public `
  -Enabled True -DefaultInboundAction Block

Revert

Set-NetFirewallProfile -Profile Domain,Private,Public `
  -DefaultInboundAction NotConfigured

Checking for success

Get-NetFirewallProfile | Select-Object Name, DefaultInboundAction
# no citas ierīces - aizvērtais ports nedrīkst atbildēt:
Test-NetConnection <servera-adrese> -Port 3389

macOS

Open TCP and UDP ports and app firewalls.

sudo lsof -iTCP -sTCP:LISTEN -n -P
sudo lsof -iUDP -n -P
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

Enable firewall and stealth mode

sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode on

Revert

sudo /usr/libexec/ApplicationFirewall/socketfilterfw \
  --setstealthmode off
sudo /usr/libexec/ApplicationFirewall/socketfilterfw \
  --setglobalstate off

Checking for success

/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode

Linux

Open ports with process and active connections.

sudo ss -tulpn
sudo ss -tnp state established

Incoming denied, SSH allowed (ufw)

sudo ufw default deny incoming
sudo ufw allow 22/tcp
sudo ufw enable

Revert

sudo ufw disable

Checking for success

sudo ufw status verbose
# no citas ierīces - aizvērtais ports nedrīkst atbildēt:
nc -zv <servera-adrese> 5432

Trafic baseline

A short record on a normal working day (tcpdump Linux and macOS, pktmon Windows, analysis Wireshark or Zeek) shows what the system speaks to. A connection that no one can explain must be investigated.

Outgoing traffic

The server seldom needs free access to the Internet. Limiting outgoing traffic to the addresses of updates and necessary services makes it difficult for both malware communication and data removal.

Don't cut yourself out

On a remote server, first allow your control connection and only then turn off by default. Keep the second session open until check.

Next step: 4. Automatic launch →

MITRE ATT&CK T1543, T1547, T1053, T1037

What starts up - services, autostart and planned tasks

Automatic launch is a place where the malware remains after restarting (persistance). The same site also contains forgotten updates and old software. A regular review of it reveals both the redundant and the alien.

Where to search for an automatically run code on each operating system
MechanismWindowsmacOSLinux
Services and background processesBegin before applying and operate with system rights - therefore it is the most valuable place for the attacker. Get-Service, HKLM\SYSTEM\CurrentControlSet\Services /Library/LaunchDaemons, /Library/LaunchAgents /etc/systemd/system, ~/.config/systemd/user
Start at loginGets off when the user logins, with his rights. There are updates and app assistants. HKLM\...\CurrentVersion\Run, HKCU\...\Run, Startup Folder ~/Library/LaunchAgents, Loginitems (sfltool dumpbtm) ~/.config/autostart, /etc/xdg/autostart
Planned tasksRun after time or event, even when no one has applied. Task Scheduler (Get-ScheduledTask) crontab -l, launchd StartCalendarInterval crontab -l, /etc/cron.*, systemctl list-timers
Launch scripts and liner profilesEach session of the new shell - one line in a file, rarely opened by someone. Group Policy Scripts, $PROFILE ~/.zshrc, /etc/zprofile ~/.bashrc, /etc/profile.d, /etc/rc.local
Event triggersSomething happens: an attached device, a modified file, a log event occurs. WMI subscriptions (root\subscription) launchd WatchPaths udev layers, /etc/ld.so.preload
Nuclear modules and driversWorking with the highest rights, so a foreign record here is the most serious finding. driverquery /v kmutil showloaded, system extensions lsmod, /etc/modules-load.d

How to read: line - mechanism, column - operating system, cell - where to watch. ATT&CK Codes T1543 (services), T1547 (starting after application), T1053 (planned tasks), T1037 and T1546 (scripts and triggers) are useful in search of ready-made detection rules. The protection side is shown by the tab Protection Mapping.

1

Identify Owner

Each record must have a person or system that requires it. No owner - it's a candidate for removal, not a secret.

2

Disable, not delete

First in the test machine, then one by one. Disabling is reversible, deletion is often not - and notes how to return.

3

Save List

Save today's list and compare it next time. A new entry that nobody installed is a sign of an incident.

Windows

Launch entries, non-Microsoft planned tasks and full report with Sysinternals Authoruns.

Get-CimInstance Win32_StartupCommand |
  Select-Object Name, Command, Location, User
Get-ScheduledTask |
  Where-Object { $_.State -ne 'Disabled' } |
  Where-Object { $_.TaskPath -notlike '\Microsoft\*' } |
  Select-Object TaskPath, TaskName, State
autorunsc.exe -accepteula -a * -m -s -h -c > autoruns.csv

Disabling a task (example name)

Disable-ScheduledTask -TaskPath '\' -TaskName 'VecsAtjauninatajs'

Revert

Enable-ScheduledTask -TaskPath '\' -TaskName 'VecsAtjauninatajs'

macOS

LaunchAgents and LaunchDaemons folders, background task database (macOS 13+) and cron.

ls -la ~/Library/LaunchAgents \
  /Library/LaunchAgents /Library/LaunchDaemons
sudo sfltool dumpbtm
crontab -l

Stop User Agent (example name)

launchctl bootout gui/$(id -u) \
  ~/Library/LaunchAgents/com.example.updater.plist

Revert

launchctl bootstrap gui/$(id -u) \
  ~/Library/LaunchAgents/com.example.updater.plist

Linux

All users cron, system cron folders and systemd timers.

for u in $(cut -d: -f1 /etc/passwd); do
  sudo crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"
done
ls -la /etc/cron.d /etc/cron.daily /etc/cron.hourly
systemctl list-timers --all

Stop timer (example name)

sudo systemctl disable --now vecs-atjauninatajs.timer

Revert

sudo systemctl enable --now vecs-atjauninatajs.timer

Comparison with previous

One list doesn't say anything in itself. Save it (Autoruns CSV, command output) and compare next time - a new entry that nobody installed is a sign of an incident.

Tools

Windows - Sysinternals Authoruns. macOS - KnockKnock and Block Block (Objective-See). Linux - auditd or EDR that monitors changes in cron and systemd folders.

Next step: 5 Base Lines →

NIST SP 800-53 CM-2, CM-6, SP 800-70

Configuration baselines (benchmarks)

The baseline shall be a secure configuration state against which the system can be automatically checked. You don't have to think about yourself - internationally recognised catalogues describe hundreds of settings for each operating system, with justification and type of test.

How to choose a baseline catalogue
Catalogue What It Is for Hardness Test tool Accessibility
CIS Benchmarks The widest coverage: Windows, macOS, Linux, clouds, containers, databases. Level 1 (Level 1) - low impact on work. Level 2: Stronger protection which may hinder work. CIS-CAT Lite, CIS-CAT Pro, OpenSCAP PDF free of charge after registration. The full CIS-CAT Pro is for CIS members.
DISA STIG Public sector and contracts that require Stig directly. Stronger than CIS Level 1; severity categories CAT I-III. STIG Viewer, SCAP Compliance Checker Free of charge, public.
Microsoft Security Baselines Windows and Microsoft 365 environment, where settings are maintained by Group Policy or Intune. Manufacturer recommended minimum, close to CIS Level 1. Policy Analyzer, LGPO (Security Compliance Toolkit) Free of charge.
macOS Security Compliance Project macOS fleet, where the baseline is to be delivered as MDM profile. Selected: CIS Level 1 to NIST High Level. Project generated test script Free, open source.
SCAP Security Guide Linux, where the test must be automated and repeated every month. Profiles per target (CIS, STIG, PCI DSS) per content. OpenSCAP (oscap), Lynis kā ātrs pirmais skats Free, open source.

The choice is not which catalogue is better, but which organisation can maintain and prove. One catalogue, measured every month, is more valuable than the three remaining in the document.

1

Set coverage and profile

Which systems, whose catalogue version, which level. Without coverage, the compliance rate is not comparable to anything.

2

Record exceptions

For each failed setting - justification, compensatory control, term and owner. The exception register is what the auditor reads first.

3

Measurements automatically

Check with the tool, not with the survey, and with the same profile with some previous time. The result is data, not opinion.

4

Keeps evidence

Report with date, profile version, coverage and tool version. Next time compares - the difference is important, not one number.

Configuration as code

The baseline is maintained by Ansible, Group Policy, Intune or MDM Profiles, not by hand. The new system then begins safely, and the deviation from the baseline is visible in the history of the change.

The baseline is not one for all

Server, workstation and developer machine withstand different rigour. One profile for all means either too loose a server or an inactive workstation - therefore the profile is selected for each system group individually.

Windows

Assessment of Security Compliance Toolkit (Policy Analyzer, LGPO) and CIS-CAT Lite against CIS Benchmark. The current security policy can be exported for comparison.

secedit /export /cfg C:\Temp\drosibas-politika.inf
gpresult /h C:\Temp\gpo-atskaite.html

macOS

the test script generated by mSCP shows compliance with the selected baseline. In the organization, the settings are maintained by MDM profiles.

sudo zsh ./<baseline>_compliance.sh --check

Linux

Fast audit with Lynis and full examination with OpenSCAP. Profile name and content file vary by distribution - they are shown in oscap info.

sudo lynis audit system
oscap info /usr/share/xml/scap/ssg/content/ssg-ubuntu2204-ds.xml
sudo oscap xccdf eval --profile <profils> \
  --report atskaite.html \
  /usr/share/xml/scap/ssg/content/ssg-ubuntu2204-ds.xml

Next step: 6. Measurement →

NIST CSF 2.0 ID.AM, PR.PS, ISO/IEC 27002 8.8-9

Measurement and maintenance - continuous cycle

Attack surface reduction cycle Five steps - inventory, cut, harden, check, monitor - and return to inventory. Surface returns with each installation Know tab 1, inventory Zoom Out tabs 2-4 Hardening tab 5, baseline Verify tab 6, compare Monitor tab 6, deviation after each change and at least quarterly

To measure

Number of ports opened per system, number of services enabled, number of autostart records and percentage of compliance with selected baseline. The trend is more important than value.

Outside view

The attacker sees what is public: DNA records, certificates and open ports. You can check your domain with web address check, but their addresses - with external port scanning, which may be performed only for their systems.

Regulation

Article 21 of the NIS2 requires risk management measures, including security for system maintenance and vulnerability handling. ISO/IEC 27002 8.9 requires a defined and supervised configuration, 8.8 for the management of technical vulnerability.

  1. There are equipment, and each program and service has a certain purpose.
  2. The list of open ports is agreed and each has a owner.
  3. The traffic entering the firewall is denied by default, the exit is limited where possible.
  4. The autostart list is saved and compared to the previous one.
  5. There is a selected baseline (CIS, Stig, Microsoft or mSCP), and compliance is checked automatically.
  6. Each change is recorded together with the return step.

Overall picture: Map of protection →

MITRE D3FEND 1.6.0

What protection does - seven D3FEND tactics

ATT&CK describes what the attacker is doing. D3FEND describes what the defender is doing against it and links each countermeasure to the ATT&CK techniques to which it relates. This tab shows where the work of this guide falls into the overall picture of protection - and what remains outside it.

Before the incident - know and strengthen (hardening)

Model

You know what needs to be protected at all - active, network, system-to-system links.

  • Asset Inventory
  • Network Mapping
  • System Mapping

This guide: Tab Inventory.

Harden

Make the attack more expensive before it happens: turn off the excess, strengthen the configuration (hardening).

  • Application Hardening
  • Platform Hardening
  • Credential Hardening

In this guide: Services, Baselines.

During an incident - spot and limit

Detect

Notice access or activity - logs, traffic analysis, change comparison.

  • Network Traffic Analysis
  • File Analysis
  • Process Analysis

This guide: Automatic launch, Measurement.

Isolate

Limiting how far the attacker is: the limits of network and execution.

  • Network Isolation
  • Execution Isolation
  • Access Mediation

In this guide: Network communication (the firewall is prevented by default).

Deceive

Attract the attacker to a controlled environment and thus discover his presence.

  • Decoy File
  • Decoy Environment
  • Decoy User Credential

Outside this road map - requires a supervised environment and a response plan.

After the incident - expel and return

Evict

Eject the attacker: stop the process, remove the persistence record, change the credentials.

  • Process Eviction
  • Credential Eviction
  • Object Eviction

In this guide: Disabling commands in Autostart tab.

Restore

Return job: configuration, software, access, data.

  • Restore Configuration
  • Restore Software
  • Restore Access

In this guide: each change next to the team that returns it.

The technical names are in English as in D3FEND. Each has its own code and associated ATT&CK techniques - d3fend.mitre.org. Mapping is a guide, not a statement of conformity.

Why two frames

The ATT&CK provides a common language on the steps of the attacker, D3FEND on countermeasures. Together, they answer the question of which technique is covered and where there are no controls.

Mapping not compliant

The fact that control is on the list does not mean that it works. Compliance shall be demonstrated by measurement that the control is switched on, that it is checked by someone and that it is noted when it is inoperative.

Abbreviations

Abbreviations used in the Roadmap: the original name and what it means.

CIS
Center for Internet Security - publish CIS Controls and CIS Benchmarks (safe configurations).
DISA STIG
Defense Information Systems Agency Security Technical Implementation Guide - U.S. Defense Resor Configuration Guidelines.
mSCP
macOS Security Compliance Project - macOS secure configuration baselines.
SCAP / OpenSCAP
Security Content Automation Protocol - standard and tool for automatic configuration verification.
CM-7
NIST SP 800-53 controls 'minimum functionality'.
LGPO
Local Group Policy Object - Microsoft tool for local group policy application.
MDM
Mobile Device Management - central management of devices.
EDR
Endpoint Detection and Response - computer monitoring and response.
SMBv1
Server Message Block 1 - outdated Windows sharing protocol.
WMI
Windows Management Instrumentation - Windows management interface, including auto launch location.
DHCP
Dynamic Host Configuration Protocol - automatic issuing of IP addresses - its log shows the device in the network.
TCP / UDP
Network transport protocols; the port is a door for a particular service.
MITRE ATT&CK / D3FEND
An attacker's technique/protection technique knowledge base.
CVE
Common Vulnerabilities and Exposures - vulnerability identifier.