![pam_exec.so 스크립트에서 디렉터리의 파일 목록을 가져올 수 없습니다.](https://linux55.com/image/207087/pam_exec.so%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%EC%9D%98%20%ED%8C%8C%EC%9D%BC%20%EB%AA%A9%EB%A1%9D%EC%9D%84%20%EA%B0%80%EC%A0%B8%EC%98%AC%20%EC%88%98%20%EC%97%86%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
나는 모든 SSH 로그인에서 사용자 정의 스크립트를 실행하여 사용자 정의 화이트리스트에 추가되지 않은 알 수 없는 IP 주소의 로그인이 있는 경우 이를 알려줍니다. 나는 그것을 사용하고 있다pam_exec.so사용자가 로그인할 때마다 이를 확인하십시오. 지금까지는 잘 작동하고 있습니다. 로그인할 때마다 원하는 알림을 받지만 내 IP는 현재 내가 정의한 화이트리스트에 없습니다.
유일한 문제는 서버에 어떤 웹 사이트 구성이 있는지 알려주는 for 루프가 스크립트에 있다는 것입니다. 이를 위해 모든 파일을 가져오려고 시도합니다./etc/apache2/sites-enabled. 현재 설명적이지 않은 호스트 이름을 가진 서버가 많이 있기 때문에 알림을 받을 때 해당 서버에서 무엇이 실행되고 있는지 아는 것이 편리할 것입니다. 스크립트를 수동으로 실행하면 문제가 없지만 pam을 사용하여 자동화하면 구성 이름이 포함된 문자열이 비어 있는 것으로 나타납니다.
파일 이름을 알 수 있는 방법이 있나요?
다음은 스크립트, 해당 권한 설정 및 작동하도록 pam 구성에 추가한 줄입니다.
/root/sh/login 알림.sh:
-rws--x--x 1 루트 루트 1296 6월 14일 07:45 login-notify.sh*
#!/bin/sh
# Change sender depending on where the notification should go
sender="[email protected]"
target="[email protected]"
client_ip="${PAM_RHOST%% *}"
user=$PAM_USER
# Check if the session is open
if [ "$PAM_TYPE" != "close_session" ]; then
# Check if client ip is on the whitelist
if grep -Fxq "$client_ip" /etc/ssh_ip_whitelist; then
:
else
host="`hostname`"
# Get all active website configurations on the server
host_websites=""
for entry in /etc/apache2/sites-enabled/*
do
config_name="$(basename $entry)"
host_websites+="\n${config_name}"
done
subject="SSH Login: user $user from $client_ip on $host"
# Message to send, e.g. the current environment variables.
message="A new ssh login has been detected:\nhost - $host\nuser - $user\nip - $client_ip\n\nThe following website configurations exist on this server: $host_websites\n\nIf you trust this IP, please ask your server administrator to add it to the file /etc/ssh_ip_whitelist on this server. To quickly add an IP, connect to the server, change to root user and use:\n\necho \"<trusted ip>\" >> /etc/ssh_ip_whitelist"
# Send the email
echo "Subject: $subject\n$message"|/usr/sbin/sendmail -r $sender -t $target
fi
fi
/etc/pam.d/sshd:
session optional pam_exec.so seteuid /root/sh/login-notify.sh