๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Server

[ubuntu, fail2ban] ๋กœ๊ทธ์ธ ์‹คํŒจ ํšŸ์ˆ˜์— ๋”ฐ๋ผ IP ์ฐจ๋‹จํ•˜๊ธฐ

by LasBe 2024. 12. 10.
๋ฐ˜์‘ํ˜•

๐Ÿ“’ [ubuntu, fail2ban] ๋กœ๊ทธ์ธ ์‹คํŒจ ํšŸ์ˆ˜์— ๋”ฐ๋ผ IP ์ฐจ๋‹จํ•˜๊ธฐ


ํ•œ ๋ฒˆ์”ฉ ๊ฐœ์ธ ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€ ๋กœ๊ทธ์ธ ์‹œ๋„ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋ฉด ์ •๋ง ํ•˜๋ฃจ์ข…์ผ ๋ฌด์ž‘์œ„ ๊ณต๊ฒฉ์„ ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ดœํžˆ ์ „๊ธฐ์š”๊ธˆ ๋” ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™์•„ ๋ฐฉ์–ดํ•˜๋ ค๊ณ  ์ฐพ์•„๋ณด๋˜ ์ค‘ fail2ban์ด๋ž€ ํˆด์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ fail2ban

fail2ban์€ SSH ๋กœ๊ทธ์ธ ์‹คํŒจ ํšŸ์ˆ˜์— ๋”ฐ๋ผ IP๋ฅผ ์ฐจ๋‹จ์‹œ์ผœ ์ฃผ๋Š” ํˆด์ž…๋‹ˆ๋‹ค.

๋ฌด์ž‘์œ„ ๊ณต๊ฒฉ ํŠน์„ฑ์ƒ IP๋ฅผ ์ฐจ๋‹จํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์–ด๋Š ์ •๋„ ์˜ˆ๋ฐฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ์„ค์น˜

$ sudo apt update
$ sudo apt install fail2ban -y

 

๐Ÿ”Ž ์„ค์ •ํŒŒ์ผ ์ˆ˜์ •

  • ๊ธฐ๋ณธ ์„ค์ • ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ด ์ค๋‹ˆ๋‹ค.
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

 

  • ํŽธ์ง‘๊ธฐ๋ฅผ ์ด์šฉํ•ด ๋ณต์‚ฌํ•œ ํŒŒ์ผ์„ ์—ด์–ด์ค๋‹ˆ๋‹ค.
$ sudo nano /etc/fail2ban/jail.local

 

  • ์ˆ˜๋งŽ์€ ์ฃผ์„์„ ์ง€๋‚˜ [sshd] ์„น์…˜์„ ์ฐพ์€ ํ›„ ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

์ „
ํ›„

์ฃผ์š” ์˜ต์…˜๋“ค์˜ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

maxretry: ํ—ˆ์šฉํ•  ์ตœ๋Œ€ ๋กœ๊ทธ์ธ ์‹คํŒจ ํšŸ์ˆ˜ (์—ฌ๊ธฐ์„œ๋Š” 3ํšŒ๋กœ ์„ค์ •)

bantime: ์ฐจ๋‹จ ์‹œ๊ฐ„ (์ดˆ ๋‹จ์œ„, ์—ฌ๊ธฐ์„œ๋Š” 1์‹œ๊ฐ„)

findtime: ์‹คํŒจ ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ๊ธฐ๊ฐ„ (์ดˆ ๋‹จ์œ„, ์—ฌ๊ธฐ์„œ๋Š” 10๋ถ„)

 

๐Ÿ”Ž ์žฌ์‹œ์ž‘ํ•˜์—ฌ ์„ค์ • ์ ์šฉ

$ sudo systemctl restart fail2ban



๐Ÿ”Ž ๋™์ž‘ ์ƒํƒœ์™€ ์ฐจ๋‹จ ๋ชฉ๋ก ํ™•์ธ

$ sudo fail2ban-client status sshd

์œ„ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ๋ฐ๋ชฌ์˜ ์ƒํƒœ์™€ ์ฐจ๋‹จ๋‹นํ•œ IP ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž IP ์ฐจ๋‹จ ๋ฐ ํ•ด์ œ ๋ช…๋ น์–ด

  • ํŠน์ • IP ์ˆ˜๋™์œผ๋กœ ์ฐจ๋‹จ
$ sudo fail2ban-client set sshd banip <IP์ฃผ์†Œ>

 

  • ํŠน์ • IP ์ˆ˜๋™์œผ๋กœ ์ฐจ๋‹จ ํ•ด์ œ
$ sudo fail2ban-client set sshd unbanip <IP์ฃผ์†Œ>

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


์˜คํ”ˆ ์ฑ„ํŒ