[DevOps][Linux] Shell Script๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„ ์ ‘์† ์‚ฌ์šฉ์ž ์ •๋ณด ๋””์Šค์ฝ”๋“œ์— ๋ณด๋‚ด๊ธฐ

2023. 8. 7. 12:18ใ†System ์ž‘์—…์‹ค/LINUX

728x90
๋ฐ˜์‘ํ˜•

 




 

 

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ”

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

Git Hub : https://github.com/JunyHarang-Open-Source-project/useful-bash-shell-script/tree/master/SSH-%EC%A0%91%EC%86%8D-%EB%94%94%EC%8A%A4%EC%BD%94%EB%93%9C-%EC%95%8C%EB%A6%BC

 

 

 

 

 

๐Ÿš€ Shell Script๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„ ์ ‘์† ์‚ฌ์šฉ์ž ์ •๋ณด ๋””์Šค์ฝ”๋“œ์— ๋ณด๋‚ด๊ธฐ

    ๐Ÿ”ฝ ๊ฐœ์š”

        ๐Ÿ“ฆ ์†Œ๊ฐœ

ํ˜„์žฌ ์ฃผ๋‹ˆ๊ฐ€ ์ฐธ์—ฌํ•˜๊ณ  ์žˆ๋Š” IT Project ๋ชจ์ž„์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ฒ„์— ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•  ์ผ์ด ๋งŽ๊ณ , ์ด์— ๋”ฐ๋ผ ๋ณด์•ˆ์„ ์œ„ํ•ด ๋ชจ๋‹ˆํ„ฐ๋ง์„ ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์„œ๋ฒ„์— ์ ‘์†ํ•˜์—ฌ ๋กœ๊ทธ ๋ถ„์„ ํ•˜๋Š” ๋ฐฉ๋ฒ• ์™ธ์—๋Š” ํ˜„์žฌ ๋”ฑํžˆ ์šด์˜ํ•˜๊ณ  ์žˆ๋Š” ์†”๋ฃจ์…˜์ด ์—†๋Š” ์ƒํƒœ์—์š”.

๋ชจ์ž„์—์„œ ์‚ฌ์šฉ์ค‘์ธ ๋””์Šค์ฝ”๋“œ์— ์„œ๋ฒ„์— ์ ‘์†์‹œ Access(์ ‘์†) ID, User(์‚ฌ์šฉ์ž) IP, Server Hostname(์„œ๋ฒ„ ํ˜ธ์ŠคํŠธ๋„ค์ž„), Server IP(์„œ๋ฒ„ IP) ์ •๋ณด๋ฅผ ๋ฐ›์œผ๋ฉด ๋ณด๋‹ค ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , ์ด๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋ ค๊ณ  ํ•ด์š”.

๋Œ€์ƒ OS๋Š” Ubuntu Linux 22.04 LTS ์ด๊ณ , Shell Script(์‰˜ ์Šคํฌ๋ฆฝํŠธ)์™€ Discord Webhook(๋””์Šค์ฝ”๋“œ ์›นํ›…)์„ ์ด์šฉํ•˜์—ฌ ๊ตฌ์„ฑํ•ด ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

 

 

 

    ๐Ÿ”ฝ ๋””์Šค์ฝ”๋“œ ์›นํ›…

        ๐Ÿ“ฆ ์„ค์ •

์ตœ์ดˆ ์•Œ๋ฆผ ๋ฐ›์„ ๋””์Šค์ฝ”๋“œ ์ฑ„๋„์„ ์ค€๋น„ํ•˜๊ณ , ํ•ด๋‹น ์ฑ„๋„์˜ ์›นํ›… URL์„ ๋”ฐ์•ผ ํ•ด์š”.

ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ๋ชจ์–‘ ํด๋ฆญ

 

์›นํ›„ํฌ ํด๋ฆญ



์ƒˆ ์›นํ›„ํฌ ๋งŒ๋“ค๊ธฐ


์œ„์™€ ๊ฐ™์ด ์›นํ›…์„ ๋งŒ๋“ค๊ณ , ํ•ด๋‹น ์›นํ›… URL์„ ๋ณต์‚ฌํ•˜์—ฌ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค.

 

 

 

    ๐Ÿ”ฝ ๋ฆฌ๋ˆ…์Šค ์ž‘์—…

        ๐Ÿ“ฆ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๊ธฐ

์ด๋ฒˆ์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•˜๋ฉด ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ์›นํ›…์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊ฑฐ์—์š”.

ํ•ด๋‹น ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋Š” /etc/profile.d์— ๋งŒ๋“ค์–ด ์ฃผ๋Š”๋ฐ, ์ตœ์ดˆ ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ์„ ํ•˜๊ฒŒ ๋˜๋ฉด Login shell(๋กœ๊ทธ์ธ ์‰˜)๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋˜์š”. ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ์‰˜์€ bash์ด๊ณ , ๋กœ๊ทธ์ธ ์‰˜๋กœ ๋™์ž‘ํ•  ๋•Œ, profile์„ ์ฝ๊ฒŒ ๋˜์–ด ์žˆ์–ด์š”.

/etc/profile์€ /etc/profile.d Directory(๋””๋ ‰ํ„ฐ๋ฆฌ) ์•ˆ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๋Š” ์นœ๊ตฌ์—์š”.
/etc/profile.d๋Š” vim, qt, lang, colors ๋“ฑ ๋‹ค์–‘ํ•œ ์„ค์ •์ด sh File(์‰˜ ํŒŒ์ผ) ํ˜•ํƒœ๋กœ ์กด์žฌํ•˜๊ณ , ์ตœ์ดˆ ๋กœ๊ทธ์ธ ์‹œ /etc/profile์„ ํ†ตํ•ด ์‹คํ–‰๋˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋กœ๊ทธ์ธ์„ ํ•˜๊ฒŒ ๋˜๋ฉด /etc/profile์„ ๋จผ์ € ์ฝ๊ณ , ์ด ํ›„ ํ•ด๋‹น ๊ณ„์ •์˜ ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ~./profile์„ ์ฝ๊ฒŒ ๋™์ž‘ํ•ด์š”.

๊ทธ๋ž˜์„œ /etc/profile.d์— ์ฃผ๋‹ˆ๊ฐ€ ๋งŒ๋“  ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ฃ๊ฒŒ ํ•˜์—ฌ ์ „์—ญ์ ์œผ๋กœ ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฑฐ์—์š”.

#!/bin/bash

set -e

DISCORD_WEBHOOK_URL="๋””์Šค์ฝ”๋“œ ์›นํ›… URL"
USER_IP=$(echo $SSH_CONNECTION | cut -d " " -f 1)
USER=$(who | grep "$USER_IP" | awk '{print $1}' | sort | uniq)
HOSTNAME=$(hostname)
SERVER_IP=$(hostname -I | awk '{print $1}')
ACCESS_DATE=$(date +"%Y-%m-%d %T")
LOG_DIR="/var/log/discord/webhook/accessAlarm"
LOG_FILE="${LOG_DIR}/${ACCESS_DATE}_sshAccessAlarm.log"

if [ -d "$LOG_DIR" ];
then
         echo "[$ACCESS_DATE] Directory๊ฐ€ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค" >> "$LOG_FILE" 2>&1
else
         mkdir -p $LOG_DIR

         if [ $? != 0 ];
         then
                 echo "[$ACCESS_DATE] Log ์ €์žฅ์„ ์œ„ํ•œ Directory ๋งŒ๋“ค๊ธฐ ์‹คํŒจ ํ•˜์˜€์Šต๋‹ˆ๋‹ค."
                 exit 1
         else
                 echo "[$ACCESS_DATE] Directory๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์•„ ์ƒ์„ฑ ํ•˜์˜€์Šต๋‹ˆ๋‹ค." >> "$LOG_FILE" 2>&1
         fi
fi

echo "User ์ •๋ณด: $USER" >> "$LOG_FILE" 2>&1

if [ -n "$USER" ];
then
        TITLE="$ACCESS_DATE ์‚ฌ์šฉ์ž SSH ์ ‘์† ํ™•์ธ!"
        MESSAGE="SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ \n ์ ‘์†์ž: $USER, ์ ‘์†์ž IP ์ฃผ์†Œ: $USER_IP \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„: $HOSTNAME, ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ IP: $SERVER_IP"

        echo "[$ACCESS_DATE] SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

else
        TITLE="$ACCESS_DATE ์‚ฌ์šฉ์ž SSH ์ ‘์† ํ•ด์ œ ํ™•์ธ!"
        MESSAGE="SSH ์ ‘์† ํ•ด์ œ ์ •๋ณด ์•Œ๋ฆผ \n ์‚ฌ์šฉ์ž: $USER, ์‚ฌ์šฉ์ž IP ์ฃผ์†Œ: $USER_IP \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„: $HOSTNAME, ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ IP: $SERVER_IP"

        echo "[$ACCESS_DATE] SSH ์ ‘์† ํ•ด์ œ ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

fi

# curl์„ ์ด์šฉํ•˜์—ฌ ๋””์Šค์ฝ”๋“œ ์›นํ›… ๋ฉ”์‹œ์ง€ ์ „์†ก
curl -H "Content-Type: application/json" -d "{\"username\":\"๋‚ด๋ถ€ ์„œ๋ฒ„ ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ๋ด‡\",\"embeds\":[{\"title\":\"$TITLE\",\"description\":\"$MESSAGE\"}]}" "$DISCORD_WEBHOOK_URL"

if [ $? != 0 ];
then
        echo "[$ACCESS_DATE]Discord๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์‹คํŒจํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

else
        echo "[$ACCESS_DATE]Discord๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

fi
๋ฐ˜์‘ํ˜•

 

/etc/profile.d/userAccessAlarm.sh 1 ~ 42๋ฒˆ์งธ ์ค„


3๋ฒˆ์งธ ์ค„์— ์œ„์—์„œ ๋””์Šค์ฝ”๋“œ ์›นํ›… URL์„ ์–ป์€ ๊ฒƒ์„ ๋ณต์‚ฌํ•˜์—ฌ ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”.
๊ทธ๋ฆฌ๊ณ , ์ ‘์† ์‚ฌ์šฉ์ž IP ์ฃผ์†Œ๋ฅผ ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ์—ˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ $SSH_CONNECTION์€ Unix ๋ฐ Unix-like ์‹œ์Šคํ…œ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ SSH ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด ์ฃผ๊ณ  ์žˆ์–ด์š”. ์ด ๋ณ€์ˆ˜๋Š” SSH Session(์„ธ์…˜)์„ ์‹œ์ž‘ํ•œ Client(ํด๋ผ์ด์–ธํŠธ) ๋ฐ ์„œ๋ฒ„ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์š”.

์ฃผ์†Œ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋‚˜ ๋ช…๋ น์–ด์—์„œ ์‚ฌ์šฉ๋˜๊ณ , SSH ์—ฐ๊ฒฐ์˜ ํด๋ผ์ด์–ธํŠธ IP ์ฃผ์†Œ, ํด๋ผ์ด์–ธํŠธ Port(ํฌํŠธ), ์„œ๋ฒ„ IP ์ฃผ์†Œ, ์„œ๋ฒ„ ํฌํŠธ๋“ฑ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์–ด์š”.

ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•„์š”.

ํด๋ผ์ด์–ธํŠธIP์ฃผ์†Œ	ํด๋ผ์ด์–ธํŠธํฌํŠธ๋ฒˆํ˜ธ	์„œ๋ฒ„IP์ฃผ์†Œ	์„œ๋ฒ„ํฌํŠธ


๊ทธ๋ž˜์„œ awk๋ฅผ ํ†ตํ•ด ์ฒซ๋ฒˆ์งธ ์—ด์— ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ IP์ฃผ์†Œ ๊ฐ’์„ USER_IP ๋ณ€์ˆ˜์— ๋‹ด์•„ ์ฃผ์—ˆ์–ด์š”.

12 ~ 25๋ฒˆ์งธ ์ค„์€ Log(๋กœ๊ทธ)๋ฅผ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•ด ๋กœ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„์ด์—์š”.

์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์ด ๊ณณ์— ์ข€ ๋” ์ž์„ธํžˆ ์ •๋ฆฌํ•ด ๋‘์—ˆ์–ด์š”.

 

[Shell Script] ์„œ๋ฒ„ ์žฌ๋ถ€ํŒ… ์‹œ Docker ์•ˆ์— ํŠน์ • Deamon ์ž๋™ ๊ธฐ๋™๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ” COUPANG www.coupang.com "์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค." ๐Ÿš€ Docker ์•ˆ์˜ Deamon ์ž๋™ ๊ธฐ๋™๋˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

junyharang.tistory.com

 

31 ~ 42๋ฒˆ์งธ ์ค„๊นŒ์ง€๋Š” ์ ‘์† ์ด์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ , ๋””์Šค์ฝ”๋“œ ๋ฉ”์‹œ์ง€๋กœ ๋ณด๋‚ผ ๋ฌธ์ž์—ด์„ ์ค€๋น„ํ•˜๋Š” ๋ถ€๋ถ„์ด์—์š”.

31๋ฒˆ์งธ ๋ถ„๊ธฐ๋ฌธ์„ ํ†ตํ•ด USER ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋น„์–ด์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด ์ฃผ๊ณ , ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด then ์ ˆ์„ ํ†ตํ•ด SSH ์ ‘์†์„ ํ–ˆ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋“ค๊ณ , ๋น„์–ด์žˆ๋‹ค๋ฉด ์ ‘์† ํ•ด์ œ๋ฅผ ํ–ˆ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋“ค๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

 

/etc/profile.d/userAccessAlarm.sh 46 ~ 56๋ฒˆ์งธ ์ค„


์ด ๋ถ€๋ถ„์ด ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋””์Šค์ฝ”๋“œ ์›นํ›…์„ ์ด์šฉํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ถ€๋ถ„์ด์—์š”.

curl ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด HTTP๋กœ ์ค€๋น„๋œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿฐ ๋’ค 49๋ฒˆ์งธ ์ค„์— ๋ช…๋ น์–ด ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ , ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

 

 

    ๐Ÿ”ฝ ํ™•์ธํ•˜๊ธฐ

        ๐Ÿ“ฆ SSH ์ ‘์†

๊ทธ๋Ÿผ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊ฒŒ์š”.

SSH ์ ‘์†




์ด๋ ‡๊ฒŒ ์œ„์™€ ๊ฐ™์ด ๋ฉ”์‹œ์ง€๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

================================ ์ˆ˜ ์ • ์‚ฌ ํ•ญ ================================

์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜์˜€์ง€๋งŒ, ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์–ด์š”.

ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด SSH๊ฐ€ ํŠ•๊ฒจ ๋ฒ„๋ฆฌ๋Š” ํ˜„์ƒ์ด์—์š”.

ํŠนํžˆ ๋„์ปค ๊ด€๋ จ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด SSH๊ฐ€ ํŠ•๊ฒจ ๋ฒ„๋ฆฌ๋Š” ๋ฐ”๋žŒ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ๊ฒŒ ๋˜์–ด ๋‹ค์‹œ ์ˆ˜์ • ํ•ฉ๋‹ˆ๋‹ค.


#!/bin/bash

set -e

DISCORD_WEBHOOK_URL="{Discord Web Hook URL}"
USER_IP=$PAM_RHOST
USER=$PAM_USER
USERNAME=$(grep $USER /etc/passwd | cut -d':' -f5)
HOSTNAME=$(hostname)
SERVER_IP=$(hostname -I | awk '{print $1}')
SERVER_OS_INFO=$(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2)
ACCESS_DATE=$(date +"%Y-%m-%d %T")
LOG_DIR="/var/log/discord/webhook/accessAlarm"
LOG_FILE="${LOG_DIR}/${ACCESS_DATE}_sshAccessAlarm.log"

FOOTER="Giggal People Chat Ops use Discordโ“’ 2023. \n ๊ธฐ๊น”๋‚˜๋Š” ์‚ฌ๋žŒ๋“ค(giggals.pepole@gmail.com) \n Creater: ์ฃผ๋‹ˆํ•˜๋ž‘(junyharang8592@gamil.com) \n All Rights Reserved. \n Blog : <https://giggal-people.tistory.com>"

TOTAL_BACK_OFFICE_API_URL="http://{API URL}"

if [ -d "$LOG_DIR" ];
then
         echo "[$ACCESS_DATE] Directory๊ฐ€ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค" >> "$LOG_FILE" 2>&1
else
         mkdir -p $LOG_DIR

         if [ $? != 0 ];
         then
                 echo "[$ACCESS_DATE] Log ์ €์žฅ์„ ์œ„ํ•œ Directory ๋งŒ๋“ค๊ธฐ ์‹คํŒจ ํ•˜์˜€์Šต๋‹ˆ๋‹ค."
                 exit 1
         else
                 echo "[$ACCESS_DATE] Directory๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š์•„ ์ƒ์„ฑ ํ•˜์˜€์Šต๋‹ˆ๋‹ค." >> "$LOG_FILE" 2>&1
         fi
fi

# ์‚ฌ์šฉ์ž๊ฐ€ SSH ์ ‘์†
if [ "$PAM_TYPE" != "close_session" ];
then
        TITLE="$ACCESS_DATE ์‚ฌ์šฉ์ž SSH ์ ‘์† ํ™•์ธ!"
        MESSAGE="SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ \n\n ==== ์ ‘์† ์‚ฌ์šฉ์ž ์ •๋ณด ==== \n ์ ‘์†์ž ๊ณ„์ •: $USER \n ์ ‘์†์ž IP ์ฃผ์†Œ: $USER_IP \n ์ ‘>์†์ž ์ด๋ฆ„: $USERNAME \n\n ==== ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ ์ •๋ณด ==== \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ OS ์ •๋ณด : $SERVER_OS_INFO \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„: $HOSTNAME \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ IP: $SERVER_IP"
        COLOR=16711680 # ๋นจ๊ฐ„์ƒ‰
        accessWhether="1"

        echo "[$ACCESS_DATE] SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1
fi

# ์‚ฌ์šฉ์ž๊ฐ€ SSH ์ ‘์† ํ•ด์ œ ์‹œ
if [ "$PAM_TYPE" == "close_session" ];
then
        TITLE="$ACCESS_DATE ์‚ฌ์šฉ์ž SSH ์ ‘์† ํ•ด์ œ ํ™•์ธ!"
        MESSAGE="SSH ์ ‘์† ํ•ด์ œ ์ •๋ณด ์•Œ๋ฆผ \n\n ==== ์ ‘์† ํ•ด์ œ ์‚ฌ์šฉ์ž ์ •๋ณด ==== \n ์ ‘์†์ž ๊ณ„์ •: $USER \n ์ ‘์†์ž IP ์ฃผ์†Œ: $USER_IP \n ์ ‘์†์ž ์ด๋ฆ„: $USERNAME \n\n ==== ์ ‘์† ํ•ด์ œ  ๋Œ€์ƒ ์„œ๋ฒ„ ์ •๋ณด ==== \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ OS ์ •๋ณด : $SERVER_OS_INFO  \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„: $HOSTNAME \n ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ IP: $SERVER_IP"
        COLOR=65280 # ํ˜•๊ด‘ ๋…น์ƒ‰
        accessWhether="0"

        echo "[$ACCESS_DATE] SSH ์ ‘์† ํ•ด์ œ ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

fi

# curl์„ ์ด์šฉํ•˜์—ฌ ๋””์Šค์ฝ”๋“œ ์›นํ›… ๋ฉ”์‹œ์ง€ ์ „์†ก

echo "[$ACCESS_DATE] Discord๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์ž‘์—…์„ ์‹œ์ž‘ ํ•ฉ๋‹ˆ๋‹ค." >> "$LOG_FILE" 2>&1

curl -H "Content-Type: application/json" -d "{
        \"username\":\"๋‚ด๋ถ€ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ๋ด‡\",
        \"content\":\"$FOOTER\",
        \"embeds\":[{
                \"title\":\"$TITLE\",
                \"description\":\"$MESSAGE\",
                \"color\":$COLOR}]
        }" "$DISCORD_WEBHOOK_URL"

if [ $? != 0 ];
then
        echo "[$ACCESS_DATE] Discord๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์‹คํŒจํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

else
        echo "[$ACCESS_DATE] Discord๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

fi

# curl์„ ์ด์šฉํ•˜์—ฌ API์— ๋‚ด์šฉ ์ „์†ก

echo "[$ACCESS_DATE] API๋กœ SSH ์ ‘์† ์ •๋ณด ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ „์†ก ์ž‘์—…์„ ์‹œ์ž‘ ํ•ฉ๋‹ˆ๋‹ค." >> "$LOG_FILE" 2>&1

curl -X "POST" "$TOTAL_BACK_OFFICE_API_URL" \
     -H "accept: */*" \
     -H "Content-Type: application/json" \
     -d "{\"inputAccessWhether\": \"$accessWhether\",
          \"accessDateTime\": \"$ACCESS_DATE\",
          \"accessCrewId\": \"$USER\",
          \"accessCrewIp\": \"$USER_IP\",
          \"actualityServerOsInfo\": \"$SERVER_OS_INFO\",
          \"actualityServerHostName\": \"$HOSTNAME\",
          \"actualityServerIp\": \"$SERVER_IP\"
        }"

if [ $? != 0 ];
then
        echo "[$ACCESS_DATE]ํ†ตํ•ฉ ๋ฐฑ ์˜คํ”ผ์Šค API๋กœ SSH ์ ‘์† ์ •๋ณด ์ „์†ก ์‹คํŒจํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

else
        echo "[$ACCESS_DATE]ํ†ตํ•ฉ ๋ฐฑ ์˜คํ”ผ์Šค API๋กœ SSH ์ ‘์† ์ •๋ณด ์ „์†ก ์„ฑ๊ณตํ•˜์˜€์–ด์š”." >> "$LOG_FILE" 2>&1

fi

echo "[$ACCESS_DATE] ==== ์ ‘์† ๋˜๋Š” ์ ‘์† ํ•ด์ œ ์‚ฌ์šฉ์ž ์ •๋ณด ====" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ์ ‘์†์ž ๊ณ„์ •: $USER" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ์ ‘์†์ž IP ์ฃผ์†Œ: $USER_IP" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ์ ‘์†์ž ์ด๋ฆ„: $USERNAME" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ==== ์ ‘์† ๋˜๋Š” ์ ‘์† ํ•ด์ œ ๋Œ€์ƒ ์„œ๋ฒ„ ์ •๋ณด ====" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„: $HOSTNAME" >> "$LOG_FILE" 2>&1
echo "[$ACCESS_DATE] ์ ‘์† ๋Œ€์ƒ ์„œ๋ฒ„ IP: $SERVER_IP" >> "$LOG_FILE" 2>&1
728x90


์ตœ์ดˆ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋Š” ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ์ด ๋˜์—ˆ์–ด์š”.

์ฆ‰, PAM์„ ์ด์šฉํ•˜์—ฌ Discord์— Web Hook์„ ์ „์†กํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

์œ„ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ /etc/ssh ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์œ„์น˜ํ•œ ๋’ค ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

# ์‰˜ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ๊ถŒํ•œ ๋ถ€์—ฌ
chmod +x /etc/ssh/userAccessDiscordAlarm.sh

# /etc/pam.d/sshd์— ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๊ด€๋ จ ์„ค์ •
echo "session optional pam_exec.so seteuid /etc/ssh/userAccessDiscordAlarm.sh" >> /etc/pam.d/sshd

 

 


์œ„์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์•Œ๋ฆผ์ด ์˜ค๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

Git Hub : https://github.com/JunyHarang-Open-Source-project/useful-bash-shell-script/tree/master/SSH-%EC%A0%91%EC%86%8D-%EB%94%94%EC%8A%A4%EC%BD%94%EB%93%9C-%EC%95%8C%EB%A6%BC

 

 

 

๋ฆฌ๋ˆ…์Šค ์ปค๋งจ๋“œ๋ผ์ธ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ๋ฐ”์ด๋ธ”

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

SLACK์œผ๋กœ SSH ๋กœ๊ทธ์ธ ์•Œ๋ฆผ ํ™•์ธ | CoolioSo!

์„œ๋ฒ„๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋ฌด์„œ์šด ์ผ์€ ํ—ˆ๊ฐ€๋˜์ง€ ์•Š์€ ์ธ์›์ด ์šด์˜ํ•˜๋Š” ์„œ๋ฒ„์— ๋ชฐ๋ž˜ ์ ‘๊ทผํ•˜์—ฌ ์–ด๋– ํ•œ ์ž‘์—…์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•„๋ฌด๋ฆฌ ๋ณด์•ˆ์— ์‹ ๊ฒฝ์„ ์“ด๋‹ค๊ณ  ํ•ด๋„ ๋ณด์•ˆ ๋ฌธ์ œ๋Š” ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ์ด ๋˜์ง€ ์•Š

www.coolio.so

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•