[LDAP] Ubuntu에서 NSS, PAM 설정 및 간단한 테스트

kafkaaaa·2024년 5월 28일
0

LDAP

목록 보기
4/8

#1. NSS

NSS (Name Service Switch)

  • 시스템에서 Name Service를 관리하는 라이브러리
  • 주요 기능
    • Name Service
      • 이름을 해석하거나 변환하는 서비스
      • 호스트 이름, 사용자 이름, 그룹 이름 등
      • ex) 사용자 이름 -> UID 변환
      • ex) 호스트 이름 -> IP Address
    • Switch
      • 다양한 데이터 소스 사이를 전환하거나 선택하는 기능
    • 다양한 소스 지원
      • /etc/nsswitch.conf 파일을 통해 각종 데이터 소스(파일, DNS, LDAP 등)에서 데이터를 가져오는 우선순위를 설정할 수 있음.
    • 모듈화된 구조
      • 각 데이터 유형마다 모듈화된 방식으로 Name Service를 수행

#1.1 필요한 패키지 설치

sudo apt-get update
sudo apt-get install libnss-ldap libpam-ldap ldap-utils nslcd

LDAP server URI:

LDAP server search base:

#1.2 NSS 설정

/etc/nsswitch.conf 파일을 수정하여 NSS가 LDAP를 사용하도록 설정한다.

passwd: files ldap systemd 의미는 다음과 같다.

  • passwd를 먼저 files(/etc/passwd)에서 찾는다
  • 없으면 LDAP 에서 찾는다
  • 없으면 systemd에서 찾는다

수정된 /etc/nsswitch.conf 예시

/etc/shadow : 사용자 패스워드가 암호화되어 저장되는 파일
/etc/gshadow : 그룹 패스워드 및 패스워드 설정이 암호화되어 저장되는 파일


#2. PAM

#2.1 PAM (Pluggable Authentication Modules)

  • 모듈화된 방식으로 시스템 인증을 관리하는 프레임워크
  • 목적: SW개발과 / 인증 및 권한 부여 체계를 분리
  • 장점
    • 응용프로그램에서 직접 인증 로직을 구현 X (개발 간소화)
    • /etc/passwd 파일 등 시스템 파일을 열람하지 않아도 됨
    • 시스템 운영자가 응용프로그램의 인증을 제어할 수 있음
  • 주요 기능
    • 모듈화된 인증
      • 다양한 인증 메커니즘을 모듈로 제공하여 시스템 로그인, 패스워드 변경, sudo 권한 부여 등 다양한 인증 과정에서 사용됨.
    • 설정
      • /etc/pam.d/ 디렉터리에 있는 설정 파일들을 통해 각 서비스별로 어떤 인증 모듈을 사용할지 설정할 수 있음.
    • 보안 강화
      • 시스템 관리자는 PAM을 사용하여 다양한 보안 정책을 적용하고, 인증 과정에서 추가적인 보안 계층을 구성할 수 있음.

#2.2 PAM 기본 구조

Module Type

PAM에 어떤 타입의 인증을 사용할 것인가? (4가지)


Control Flag

모듈의 성공/실패 결과에 따라 어떻게 PAM 라이브러리가 반응할지 지정한다.
응용프로그램은 Linux-PAM 라이브러리로부터 요약된 성공/실패 결과를 전달받는다.


Module Name

사용할 모듈의 경로 및 이름
PAM 모듈은 대부분 /lib/security 또는 /etc/pam.d에 위치함.
아래는 주요 모듈


Module Arguments

모듈에 전달되는 인수


PAM Configuration File

  • /etc/pam.d/ 디렉토리에 Application/Service 와 동일한 이름의 파일이 존재함
  • /etc/pam.conf 파일로 통합하여 관리할 수도 있으나 위 방식을 더 많이 사용함
  • 설정 파일이 없는 경우 /etc/pam.d/other 를 따름

#2.3 PAM LDAP 설정

/etc/pam.d/common-auth
/etc/pam.d/common-account
/etc/pam.d/common-password
/etc/pam.d/common-session

파일을 수정하여 PAM이 LDAP를 사용하도록 설정한다.
libpam-ldap 패키지 설치 시 기본 설정 파일에 LDAP 모듈이 추가되었을 것이므로 스킵

/etc/pam.d/common-auth 에서 주의할 점

pam.ldap.so 뒤에 있는 use_first_pass 옵션을 지워야 한다.
-> 옵션을 제거하면 PAM 모듈(pam_ldap.so)이 이전 모듈(pam_unix.so)에서 비밀번호를 가져오지 않고 사용자에게 별도로 비밀번호를 묻는다. 이 경우, Unix 비밀번호와 LDAP 비밀번호가 달라도 문제가 없다.

auth.log
PAM 설정 실패 시
/var/log/auth.log 파일의 로그를 확인하면서 진행

#2.4 nscd 재시작

sudo systemctl restart nscd

주의사항

PAM을 통한 사용자 인증은 시스템 운영의 편의성/보안성을 높일 수 있지만 잘못 설정할 경우 비인가자에게 접근을 허용하거나 인가자의 접근을 차단하는 등 가용성을 해칠 수 있다.
또한, PAM이 올바르게 설정되어 있더라도 응용프로그램 실행 권한이 없으면 사용자 인증 자체를 수행할 수 없기 때문에 실행 권한도 적절하게 부여해야 한다.


#3. nslcd 설정

nslcd = 사용자, 그룹 및 기타 NSS를 수행하거나, 사용자 인증, 권한 부여 또는 PAM을 수행하려는 로컬 프로세스에 대한 LDAP 쿼리를 수행하는 Daemon

#3.1 nslcd 설치

sudo apt update
sudo apt install nslcd

#3.2 nslcd 설정 파일 수정

/etc/nslcd.conf

#3.3 nslcd 재시작

sudo systemctl restart nslcd
sudo systemctl enable nscld

#4. 간단한 LDAP 테스트 1

phpLDAPadmin 웹에서 Create a child entry - Generic: User Account를 했다고 해서 서버에 해당 계정이 생성되는 것은 아니다.
LDAP와 시스템 사용자 계정은 별개의 개념이다.

  • LDAP 사용자: LDAP 서버에 저장된 사용자 정보. 중앙 집중식 인증 및 디렉터리 서비스를 제공하기 위해 사용됨.
  • 시스템 사용자: 실제 서버에서 사용되는 사용자 계정. 로컬 파일인/etc/passwd 기반임
## sudoers 파일 수정 ##
sudo visudo

## 아래 내용 추가 ##
# LDAP "sudoers" group
%sudoers ALL=(ALL:ALL) ALL

## NSS 설정 확인 ##
sudo vi /etc/nsswitch.conf

passwd: ldap ~~~~~
group: ldap ~~~~~

## PAM 설정 확인 ##
sudo vi /etc/pam.d/sudo

auth sufficient pam_ldap.so

## 테스트 ##
특정 사용자에게 sudo 권한 부여하고, 해당 사용자로 로그인 한 뒤
sudo 명령을 했을 때, 추가적인 LDAP 인증이 뜨는지 확인한다.


#5. LDAP User로 SSH 접속하기

접속할 서버에 LDAP Client 세팅이 되어 있어야 합니다.
/etc/pam.d/sshd

## 기존 파일 백업
sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.bak

## LDAP 인증 활성화
sudo vi /etc/pam.d/sshd

## 아래 부분을 수정(추가) 하여
## LDAP 인증이 성공하면 통과하고, 실패할 경우 로컬 인증을 시도
## session optional 부분은 홈 디렉토리가 없으면 자동 생성
  1 # PAM configuration for the Secure Shell service
  2 
  3 # Standard Un*x authentication.
  4 @include common-auth
  5 auth    sufficient      pam_ldap.so
  6 auth    required        pam_unix.so nullok_secure try_first_pass
  7 
  8 
  9 # Disallow non-root logins when /etc/nologin exists.
 10 account    required     pam_nologin.so
 11 account sufficient      pam_ldap.so
 12 account required        pam_unix.so
  ...
  ...
 32 # Standard Un*x session setup and teardown.
 33 @include common-session
 34 session required        pam_limits.so
 35 session required        pam_env.so
 36 session required        pam_mkhomedir.so skel=/etc/skel umask=0022
 37 session optional        pam_ldap.so

389 (default) 포트 LISTEN 확인
netstat -an | grep tcp

관련 로그 보기

sudo tail -f /var/log/syslog
sudo tail -f /var/log/auth.log

#5-1. LDAP User 생성

#5-2. ldap.conf

/etc/ldap.conf

# The distinguished name of the search base.
base dc=ldap,dc=company,dc=com

# Another way to specify your LDAP server is to provide an
uri ldap://192.168.1.152:389
# Unix Domain Sockets to connect to a local LDAP Server.
#uri ldap://127.0.0.1/
#uri ldaps://127.0.0.1/   
#uri ldapi://%2fvar%2frun%2fldapi_sock/
# Note: %2f encodes the '/' used as directory separator

# The LDAP version to use (defaults to 3
# if supported by client library)
ldap_version 3

# The distinguished name to bind to the server with.
# Optional: default is to bind anonymously.
#binddn cn=proxyuser,dc=padl,dc=com
binddn cn=admin,dc=ldap,dc=company,dc=com

# The credentials to bind with. 
# Optional: default is no credential.
#bindpw secret

# The distinguished name to bind to the server with
# if the effective user ID is root. Password is
# stored in /etc/ldap.secret (mode 600)
rootbinddn cn=admin,dc=ldap,dc=company,dc=com

# The port.
# Optional: default is 389.
port 389

/etc/nslcd.conf

# /etc/nslcd.conf
# nslcd configuration file. See nslcd.conf(5)
# for details.

# The user and group nslcd should run as.
uid nslcd
gid nslcd

# The location at which the LDAP server(s) should be reachable.
# uri ldapi:///192.168.1.152:80
uri ldap://192.168.1.152:389

# The search base that will be used for all queries.
base dc=ldap,dc=company,dc=com

# The LDAP protocol version to use.
ldap_version 3

# The DN to bind with for normal lookups.
#binddn cn=annonymous,dc=example,dc=net
#bindpw secret
binddn cn=admin,dc=ldap,dc=company,dc=com

# The DN used for password modifications by root.
#rootpwmoddn cn=admin,dc=example,dc=com

# SSL options
#ssl off
#tls_reqcert never
#tls_cacertfile /etc/ssl/certs/ca-certificates.crt

# The search scope.
scope sub

#5-3. 서비스 재시작

sudo systemctl restart nslcd
sudo systemctl restart nscd

#5-4. LDAP 서버 연결 확인

ldapwhoami -x -H ldap://YOUR_IP -D "cn=admin,dc=ldap,dc=company,dc=com" -w YOUR_ADMIN_PASSWD

#5-5. SSH 로그인 시도

로그인 성공


sudo 명령어 사용

LDAP Password도 입력해야 함.


Ref.

https://ldap.or.kr/1489-2/
https://ldap.or.kr/pam_ldap/
https://ldap.or.kr/ldap%ec%9d%84-%ec%9d%b4%ec%9a%a9%ed%95%9c-oslinuxaix%ec%9d%b8%ec%a6%9d-pam-%ec%82%ac%eb%a1%803/
https://www.igloo.co.kr/security-information/%EB%A6%AC%EB%88%85%EC%8A%A4-pam-%EB%AA%A8%EB%93%88%EC%9D%98-%EC%9D%B4%ED%95%B4/
https://wiki.kldp.org/Translations/html/PAM_admin-KLDP/configuration.html

profile
일모도원

0개의 댓글