Fail2ban: fail2ban-regex guacamole 도컀 λ‘œκ·Έμ— ν•„μš”ν•œ 도움

에 λ§Œλ“  2020λ…„ 11μ›” 24일  Β·  6μ½”λ©˜νŠΈ  Β·  좜처: fail2ban/fail2ban

일반 정보:
배포: μš°λΆ„νˆ¬ 18.04
페일투밴 v0.10.2

ν™˜κ²½:

_ν•΄λ‹Ήλ˜λŠ” μƒμžλ₯Ό μž‘μ„±ν•˜κ³  μ„ νƒν•˜μ‹­μ‹œμ˜€( [x] ). Fail2Ban 버전이 였래된 경우
μ΅œμ‹  λ¦΄λ¦¬μŠ€μ—μ„œ λ¬Έμ œκ°€ μ§€μ†λ˜λŠ”μ§€ 확인할 수 μ—†μœΌλ―€λ‘œ 지원을 μš”μ²­ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
Fail2Ban을 얻은 λ°°ν¬νŒμ—μ„œ_

  • Fail2Ban 버전(κ°€λŠ₯ν•œ λͺ¨λ“  배포 접미사 포함):
  • 릴리슀 이름/버전을 ν¬ν•¨ν•œ OS:
  • [X] OS/배포 λ©”μ»€λ‹ˆμ¦˜μ„ 톡해 μ„€μΉ˜λœ Fail2Ban
  • [ ] μ½”λ“œλ² μ΄μŠ€μ— μΆ”κ°€ μ™ΈλΆ€ 패치λ₯Ό μ μš©ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
  • [ ] ꡬ성에 일뢀 μ‚¬μš©μž μ •μ˜κ°€ μˆ˜ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€(μ•„λž˜μ— μ„ΈλΆ€ 정보 제곡).

λ‚΄ 문제:

μΉœμ• ν•˜λŠ” μ„ΈλΈŒλ₯΄ μ—¬λŸ¬λΆ„,
μ§€λ‚œ 3일 λ™μ•ˆ μ‰˜ μŠ€ν¬λ¦½νŒ…μ— λŒ€ν•΄ 많이 λ°°μ› μŠ΅λ‹ˆλ‹€ :)
κ·ΈλŸ¬λ‚˜ μ •κ·œμ‹ ꡬ문은 정말 μ΄μƒν•œ κ²ƒμž…λ‹ˆλ‹€. λ‚˜λŠ” 그것이 μ–΄λ–»κ²Œ μž‘λ™ν•˜λŠ”μ§€ μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€.

이제 이 WAF μ œν’ˆμ— 보기 μ œν•œμ΄ 있으며 였래된 κ³ΌμΉ΄λͺ°λ¦¬ 용기λ₯Ό ν™œμ„±ν™”ν•΄μ•Ό ν•©λ‹ˆλ‹€.
λ‘œκ·ΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

{"log":"12:59:14.168 [http-nio-8080-exec-4] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from [192.168.8.5, 172.17.0.1] for user \"admin\" failed.\n","stream":"stdout","time":"2020-11-22T11:59:14.168982605Z"}
{"log":"12:59:15.477 [http-nio-8080-exec-9] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from [192.168.8.5, 172.17.0.1] for user \"admin\" failed.\n","stream":"stdout","time":"2020-11-22T11:59:15.477692225Z"}

1단계;κ°„λ‹¨ν•œ μ‹œμž‘ - μœ μš©ν•œ 정보: https://github.com/fail2ban/fail2ban/issues/2645#issuecomment -592032811

곡μž₯!

fail2ban-regex -v \
'19:48:16.995 [http-nio-8080-exec-2] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from [192.168.8.5, 172.17.0.1] for user "dfg" failed.' \
'\b[Aa]uthentication attempt from \[<HOST>(?:,[^\]]*)?\] (?:for user (?:"[^"]*" )?)?failed\.\s*$' 

2 단계; λ‚ μ§œ νŒ¨ν„΄ μΆ”κ°€; 도움이 λ˜λŠ” https://github.com/fail2ban/fail2ban/issues/2592#issuecomment -573119939

μ‹€νŒ¨ :(

fail2ban-regex -v \
--datepattern='^\{"log":"%%H:%%M:%%S\.%%f+\s+' \
'"log":"19:48:16.995 [http-nio-8080-exec-2] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from [192.168.8.5, 172.17.0.1] for user "dfg" failed."' \
'\b[Aa]uthentication attempt from \[<HOST>(?:,[^\]]*)?\] (?:for user (?:"[^"]*" )?)?failed\.\s*$' 

그리고 전체 둜그 λ¬Έμžμ—΄μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. "n","stream":"stdout","time":"2020-11-22T11:59:15.477692225Z"" Step1도 NULL 일치 ν•­λͺ©μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.
μ—¬κΈ°μ„œ 두 번째둜 μ €λ₯Ό λ„μ™€μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?

how-to

λͺ¨λ“  6 λŒ“κΈ€

ꡬ성 νŒŒμΌμ—μ„œλ§Œ(예: ν•„ν„° λ˜λŠ” 감μ˜₯μ—μ„œ) % λ₯Ό %% 둜 μ΄μŠ€μΌ€μ΄ν”„ν•΄μ•Ό ν•©λ‹ˆλ‹€. λͺ…λ Ήμ€„μ—μ„œλŠ” μ—¬μ „νžˆ 단일 λ¬Έμžμ—¬μ•Ό ν•˜λ―€λ‘œ

-fail2ban-regex -v --datepattern='^\{"log":"%%H:%%M:%%S\.%%f+\s+' ...
+fail2ban-regex -v --datepattern='^\{"log":"%H:%M:%S\.%f+\s+' ...

RE에 κ΄€ν•΄μ„œλŠ”, λ‹Ήμ‹ μ˜ (초) λ³€ν˜• (λ˜ν•œ μ˜¬λ°”λ₯Έ datepattern 포함)ν•˜μ§€ μž‘μ—…, λ©”μ‹œμ§€κ°€ λλ‚˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ λ•Œλ¬Έμ— failed. (λ•Œλ¬Έμ— μ΅œμ’… 액컀 $ μ—μ„œ failed\.\s*$ ), κ·Έλž˜μ„œ μ •κ·œμ‹μ€ "log":"value" 에 μžˆλŠ” κ°’μ˜ λ‚΄μš©μ— 적용될 수 μžˆμ§€λ§Œ json이 μœ μ‚¬ν•˜κ²Œ 보이고 더 λ§Žμ€ 데이터λ₯Ό ν¬ν•¨ν•˜κ³  일뢀 ꡬ쑰둜 λ¬Άμ—¬ 있고 λ‹€λ₯΄κ²Œ μ΄μŠ€μΌ€μ΄ν”„λœ 전체 λ©”μ‹œμ§€μ—λŠ” μ μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€(예: for user \"... λ°±μŠ¬λž˜μ‹œ μ°Έκ³ 

λ‚˜λŠ” λ‹€μŒκ³Ό 같이 ν•  κ²ƒμž…λ‹ˆλ‹€.

$ msg='{"log":"12:59:14.168 [http-nio-8080-exec-4] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from [192.168.8.5, 172.17.0.1] for user \"admin\" failed.\n","stream":"stdout","time":"2020-11-22T11:59:14.168982605Z"}'
$ dp=',"time"\s*:\s*"%Y-%m-%dT%H:%M:%S\.%f\d*%z"\}$'
$ re='^\{"log"\s*:\s*"\S+\s+\[[^\]]+\]\s+WARN\s+\S+\s+-\s+[Aa]uthentication attempt from \[<ADDR>(?:,[^\]]*)?\] (?:for user (?:\\"<F-USER>[^"]*</F-USER>\\" )?)?failed\.'

$ fail2ban-regex -v --datepattern="$dp" "$msg" "$re"
...
Failregex: 1 total
|-  #) [# of hits] regular expression
|   1) [1] ^\{"log"\s*:\s*"\S+\s+\[[^\]]+\]\s+WARN\s+\S+\s+-\s+[Aa]uthentication attempt from \[<ADDR>(?:,[^\]]*)?\] (?:for user (?:\\"<F-USER>[^"]*</F-USER>\\" )?)?failed\.
|      192.168.8.5  Sun Nov 22 12:59:14 2020
`-
...
Date template hits:
|- [# of hits] date format
|  [1] ,"time"\s*:\s*"Year-Month-DayT24hour:Minute:Second\.Microseconds\d*Zone offset"\}$
`-

Lines: 1 lines, 0 ignored, 1 matched, 0 missed

# newer version can output found failure data (rows):
$ fail2ban-regex -o row --datepattern="$dp" "$msg" "$re"
['192.168.8.5', 1606046354,   {'ip6': None, 'user': 'admin', 'ip4': '192.168.8.5'}],

이것은 더 λ…νŠΉν•˜κ³  μ‹œμž‘λΆ€ν„° κ³ μ •λ˜λ©° λ‚ μ§œμ™€ ν•¨κ»˜ 보닀 μ •ν™•ν•œ νƒ€μž„μŠ€νƒ¬ν”„λ₯Ό μœ„ν•΄ datepattern을 μ‚¬μš©ν•©λ‹ˆλ‹€(UTC둜, 둜그 λμ—μ„œ).
λ˜ν•œ fail2ban은 failregex μ‹œμž‘λ˜κΈ° 전에 datepattern μΌμΉ˜ν•˜λŠ” λ©”μ‹œμ§€μ˜ 일뢀λ₯Ό μž˜λΌλƒ…λ‹ˆλ‹€.

μœ„μ—μ„œ μ•½κ°„μ˜ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€(λ©”μ‹œμ§€ μ—…λ°μ΄νŠΈ) - Z (GMT 기호)λ₯Ό μΈμ‹ν•˜λ €λ©΄ μ˜μ—­ 이름 토큰 %Z λŒ€μ‹  μ˜μ—­ μ˜€ν”„μ…‹ 토큰( %z )을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€. %Z ):

-dp=',"time"\s*:\s*"%Y-%m-%dT%H:%M:%S\.%f\d*%Z"\}$'
+dp=',"time"\s*:\s*"%Y-%m-%dT%H:%M:%S\.%f\d*%z"\}$'
...
-|      192.168.8.5  Sun Nov 22 11:59:14 2020
+|      192.168.8.5  Sun Nov 22 12:59:14 2020
...
-|  [1] ,"time"\s*:\s*"Year-Month-DayT24hour:Minute:Second\.Microseconds\d*Zone name"\}$
+|  [1] ,"time"\s*:\s*"Year-Month-DayT24hour:Minute:Second\.Microseconds\d*Zone offset"\}$

κ°μ‚¬ν•©λ‹ˆλ‹€!
λ‹Ήμ‹ μ˜ λ„μ›€μœΌλ‘œ μ‹œμž‘ν•˜κ³  μ‹€ν–‰ν–ˆμŠ΅λ‹ˆλ‹€. 정말 λŒ€λ‹¨ν•©λ‹ˆλ‹€!

μ‘°μ–Έ μΆ”κ°€:
fail2ban.datedetector [20911]: INFO λ‚ μ§œ νŒ¨ν„΄ ',"time"\\s*:\\s*"%Y-%m-%dT%H:%M:%S\\.%f\\d*%z"\\}$' : ,"time"\s*:\s*"Year-Month-DayT24hour:Minute:Second\.Microseconds\d*Zone offset"\}$

λ§ˆμ§€λ§‰ μ§ˆλ¬Έμž…λ‹ˆλ‹€. λ‚˜λŠ” fail2ban.filter에 λŒ€ν•œ μ•žμœΌλ‘œμ˜ μž‘μ—…λŸ‰μ„ 쀄이고 μ‹ΆμŠ΅λ‹ˆλ‹€ :)
λ‚΄κ°€ ν•  수 μžˆλŠ” μ΅œμ„ μ˜ 둜그 경둜λ₯Ό μ •μ˜ν•˜λŠ” 것이 쒋은 생각이라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. ( μ»¨ν…Œμ΄λ„ˆ μ—…λ°μ΄νŠΈ ( id λ³€κ²½ λ“±. μ—¬κΈ°μ—μ„œλŠ” ν•΄λ‹Ήλ˜μ§€ μ•ŠμŒ )

logpath = /media/data/docker/containers/*/*-json.log λΆ€ν„°
logpath = /media/data/docker/containers/5e2543bbe77a52ff310073fdfb4183fa3fda6a3dd98294b48a081517baa20eb4/5e2543bbe77a52ff310073fdfb4183fa3fda6a3dd98294b48a081517baa20eb4-json.log \

이것은 였λ₯˜λ‘œ μ‹€ν–‰λ©λ‹ˆλ‹€.
λ‚΄κ°€ ν•  수 μžˆλŠ” 일?

fail2ban.filter에 λŒ€ν•œ ν–₯ν›„ μž‘μ—…λŸ‰μ„ 쀄이고 μ‹ΆμŠ΅λ‹ˆλ‹€.
λ‚΄κ°€ ν•  수 μžˆλŠ” μ΅œμ„ μ˜ 둜그 경둜λ₯Ό μ •μ˜ν•˜λŠ” 것이 쒋은 생각이라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

μ„€λ§ˆ. ν˜„μž¬ fail2ban은 μ‹œμž‘ μ‹œ ν•œ 번만 glob 보간 및 검색을 μˆ˜ν–‰ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€(문제 #1379와 아직 코어에 λ³‘ν•©λ˜μ§€ μ•Šμ€ λ§Žμ€ μ‹€ν—˜ λΆ„κΈ°κ°€ 있음).
κ·Έλž˜μ„œ λ‹Ήμ‹ μ˜ μ‹œλ„λŠ” 아무것도 바꾸지 μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

λ‚΄κ°€ ν•  수 μžˆλŠ” 일?

예, \ λ’€μ˜ .log \ ν›„ν–‰ λ°±μŠ¬λž˜μ‹œλ₯Ό μ œκ±°ν•©λ‹ˆλ‹€. :)
(선택 사항) λͺ¨λ“  둜그 경둜의 두 번째 λ§€κ°œλ³€μˆ˜λŠ” head λ˜λŠ” tail μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μ‹œ ν•œ 번: κ°μ‚¬ν•©λ‹ˆλ‹€!
λ§₯μ£ΌλŠ” λ‚΄κΊΌμ•Ό!

κ°μ‚¬ν•©λ‹ˆλ‹€!!! κ°μ‚¬ν•©λ‹ˆλ‹€!
(λ§₯주라면 μ¦κΈ°κ² μ§€λ§Œ.. 상황에 따라 ν•œλ‘κΆŒμ΄ 될 것 κ°™μ•„μš”;;)
λ‹€μ‹œ ν•œλ²ˆ κ°μ‚¬ν•©λ‹ˆλ‹€!

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰