Linux:Security
리눅스 보안과 관련된 내용.
Ubuntu 22.04 시스템 보안 강화 (Hardening)
시스템 업데이트 및 자동 보안 패치
# 시스템 전체 업데이트 / Full system update
sudo apt update && sudo apt upgrade -y && sudo apt dist-upgrade -y
# 자동 보안 업데이트 설정 / Enable automatic security updates
sudo apt install -y unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades
/etc/apt/apt.conf.d/50unattended-upgrades 에서 보안 업데이트만 활성화되어 있는지 확인:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";
불필요한 서비스 제거
# 현재 listen 중인 서비스 확인 / Check listening services
sudo ss -tulnp
# 불필요한 서비스 비활성화 / Disable unnecessary services
sudo systemctl disable --now avahi-daemon
sudo systemctl disable --now cups
sudo systemctl disable --now bluetooth
sudo systemctl disable --now ModemManager
# snap 불필요 시 제거 / Disable snap if not needed
sudo systemctl disable --now snapd
SSH 강화
/etc/ssh/sshd_config.d/hardening.conf 생성:
sudo tee /etc/ssh/sshd_config.d/hardening.conf << 'EOF'
# 루트 로그인 차단 / Disable root login
PermitRootLogin no
# 패스워드 인증 비활성화 (키 인증만 허용) / Key-only authentication
PasswordAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
# 프로토콜 및 알고리즘 제한 / Restrict algorithms
KexAlgorithms curve25519-sha256,[email protected]
Ciphers [email protected],[email protected]
MACs [email protected],[email protected]
# 세션 제한 / Session limits
MaxAuthTries 3
MaxSessions 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
# 기타 강화 / Additional hardening
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitEmptyPasswords no
UseDNS no
# 특정 사용자만 SSH 허용 / Allow specific users only
AllowUsers zer0
EOF
sudo systemctl restart sshd
| 항목 | 설명 |
| PermitRootLogin no | root 계정 직접 로그인 차단 |
| PasswordAuthentication no | 비밀번호 인증 비활성화, SSH 키만 허용 |
| MaxAuthTries 3 | 인증 시도 3회 초과 시 연결 끊김 |
| AllowUsers | 지정된 사용자만 SSH 접근 가능 |
UFW 방화벽 기본 설정
sudo apt install -y ufw
# 기본 정책: 인바운드 차단, 아웃바운드 허용 / Default policy
sudo ufw default deny incoming
sudo ufw default allow outgoing
# SSH만 LAN에서 허용 / Allow SSH from LAN only
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp comment 'SSH-LAN'
# 로깅 활성화 / Enable logging
sudo ufw logging high
sudo ufw enable
Fail2Ban 설치
sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.local << 'EOF'
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
banaction = ufw
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
EOF
sudo systemctl enable --now fail2ban
| 항목 | 값 | 설명 |
| bantime | 86400 | SSH 차단 시간 24시간 |
| findtime | 600 | 10분 이내 실패 횟수 카운트 |
| maxretry | 3 | 3회 실패 시 차단 |
| banaction | ufw | UFW와 연동하여 차단 |
커널 보안 파라미터 (sysctl)
sudo tee /etc/sysctl.d/99-security.conf << 'EOF'
# IP 스푸핑 방지 / Anti IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# ICMP redirect 차단 / Block ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# SYN flood 방지 / SYN flood protection
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# Source routing 차단 / Block source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# IP forwarding 비활성화 / Disable IP forwarding
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0
# ASLR 최대화 / Maximize ASLR
kernel.randomize_va_space = 2
# 코어 덤프 제한 / Restrict core dumps
fs.suid_dumpable = 0
# dmesg 접근 제한 / Restrict dmesg access
kernel.dmesg_restrict = 1
# 커널 포인터 숨김 / Hide kernel pointers
kernel.kptr_restrict = 2
# BPF JIT 강화 / BPF JIT hardening
net.core.bpf_jit_harden = 2
EOF
sudo sysctl --system
AppArmor 확인 및 강화
# AppArmor 상태 확인 / Check AppArmor status
sudo aa-status
# 추가 프로파일 설치 / Install additional profiles
sudo apt install -y apparmor-utils apparmor-profiles apparmor-profiles-extra
감사 로깅 (auditd)
sudo apt install -y auditd audispd-plugins
sudo tee /etc/audit/rules.d/hardening.rules << 'EOF'
# 파일 삭제 감시 / Monitor file deletion
-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete
# 사용자/그룹 변경 감시 / Monitor user/group changes
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/gshadow -p wa -k identity
# sudoers 변경 감시 / Monitor sudoers changes
-w /etc/sudoers -p wa -k sudoers
-w /etc/sudoers.d/ -p wa -k sudoers
# NFS 설정 파일 감시 / Monitor NFS exports
-w /etc/exports -p wa -k nfs_exports
EOF
sudo systemctl enable --now auditd
sudo augenrules --load
기타 보안 조치
# 공유 메모리 제한 / Restrict shared memory
echo "tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0" | sudo tee -a /etc/fstab
# USB 스토리지 차단 / Block USB storage
echo "blacklist usb-storage" | sudo tee /etc/modprobe.d/disable-usb-storage.conf
# 로그인 배너 / Login banner
sudo tee /etc/issue.net << 'EOF'
***WARNING*** Unauthorized access is prohibited. All activity is monitored.
EOF
# cron 접근 제한 / Restrict cron access
sudo touch /etc/cron.allow
echo "zer0" | sudo tee /etc/cron.allow
sudo chmod 600 /etc/cron.allow