본문 바로가기

리눅스

우분투 24.04에서 TCP Wrappers를 사용하는 sshd 서비스를 접근 제어하는 방법

반응형

우분투 24.04에서 TCP Wrappers(libwrap)를 사용하는 sshd 서비스를 접근 제어하는 방법

TCP Wrappers는 데몬(서비스)이 실행될 때 라이브러리를 참조하므로 별도의 서비스 재시작이 필요 없는 경우가 많지만, 해당 서비스가 이 파일을 읽을 수 있는지(라이브러리 의존성)를 먼저 확인해야 합니다.

TCP Wrappers 적용 가능 여부 확인

sshd 바이너리의 libwrap 의존성 확인

ldd $(which sshd) | grep libwrap
        libwrap.so.0 => /lib/x86_64-linux-gnu/libwrap.so.0 (0x00007fdf21dfa000)

또는 심볼 포함 여부 확인

strings $(which sshd) | grep hosts_access
hosts_access
  • 출력에 libwrap.so 또는 hosts_access가 포함되어야 TCP Wrappers가 동작합니다.

시스템 내 libwrap 사용 바이너리 전체 확인

sudo find /usr/sbin /usr/bin -type f -executable -exec sh -c 'ldd "{}" 2>/dev/null \
  | grep -q libwrap.so' \; -print
/usr/sbin/sshd
/usr/sbin/tcpdchk
/usr/sbin/tcpdmatch
/usr/sbin/try-from
/usr/sbin/tcpd

libwrap을 실제로 사용하는 실행 파일만 출력합니다.

728x90

TCP Wrappers 설정 파일

기본 차단 정책(hosts.deny)

  • 모든 TCP Wrappers 대상 서비스에 대해 기본 차단
echo "ALL: ALL" | sudo tee -a /etc/hosts.deny
ALL: ALL

기본 허용 정책(hosts.allow)

  • 192.168.0.2에서의 SSH 접속만 허용
echo "sshd: 192.168.0.2" | sudo tee -a /etc/hosts.allow
sshd: 192.168.0.2

설정 검증 도구 설치

TCP Wrappers 유틸리티는 기본 설치되어 있지 않습니다.

sudo apt install -y tcpd

설정 검증 및 테스트

설정 파일 문법 및 정책 검증(tcpdchk)

sudo tcpdchk -v
Using network configuration file: (null)

>>> Rule /etc/hosts.allow line 10:
daemons:  sshd
clients:  192.168.0.2
access:   granted

>>> Rule /etc/hosts.deny line 17:
daemons:  ALL
clients:  ALL
access:   denied
  • /etc/hosts.allow, /etc/hosts.deny 문법 오류 확인
  • 실제 적용 순서 및 매칭 결과 출력

특정 IP 접속 시뮬레이션(tcpdmatch)

허용(access granted)

sudo tcpdmatch sshd 192.168.0.2
client:   address  192.168.0.2
server:   process  sshd
access:   granted

차단(access denied)

sudo tcpdmatch sshd 192.168.0.112
client:   address  192.168.0.112
server:   process  sshd
access:   denied

실제 동작 테스트

SSH 접속 테스트

  • 192.168.0.2 클라이언트
ssh ubuntu@192.168.0.113
The authenticity of host '192.168.0.113 (192.168.0.113)' can't be established.
ED25519 key fingerprint is SHA256:zbgGCX4S6B5udMXw53C6qXDN+opPTT+DBZdMvCEUc8E.
This host key is known by the following other names/addresses:
    C:\Users\Administrator/.ssh/known_hosts:1: 192.168.0.111
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.113' (ED25519) to the list of known hosts.
ubuntu@192.168.0.113's password:
  • 192.168.0.112 클라이언트
ssh ubuntu@192.168.0.113
kex_exchange_identification: read: Connection reset by peer
Connection reset by 192.168.0.113 port 22

로그 확인

SSH 인증 로그 확인

sudo tail -f /var/log/auth.log | grep sshd
Feb  3 15:23:18 node3 sshd[13203]: refused connect from 192.168.0.112 (192.168.0.112)
Feb  3 15:23:25 node3 sshd[13204]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.0.2  user=ubuntu
Feb  3 15:23:27 node3 sshd[13204]: Failed password for ubuntu from 192.168.0.2 port 56186 ssh2
Feb  3 15:23:31 node3 sshd[13204]: Connection reset by authenticating user ubuntu 192.168.0.2 port 56186 [preauth]
Feb  3 15:23:34 node3 sshd[13206]: Accepted password for ubuntu from 192.168.0.2 port 52072 ssh2
Feb  3 15:23:34 node3 sshd[13206]: pam_unix(sshd:session): session opened for user ubuntu(uid=1000) by (uid=0)

 

728x90
반응형