You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fail2ban-setup.en-us.md 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. date: "2018-05-11T11:00:00+02:00"
  3. title: "Usage: Setup fail2ban"
  4. slug: "fail2ban-setup"
  5. weight: 16
  6. toc: false
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "usage"
  11. name: "Fail2ban setup"
  12. weight: 16
  13. identifier: "fail2ban-setup"
  14. ---
  15. # Fail2ban setup to block users after failed login attempts
  16. **Remember that fail2ban is powerful and can cause lots of issues if you do it incorrectly, so make
  17. sure to test this before relying on it so you don't lock yourself out.**
  18. Gitea returns an HTTP 200 for bad logins in the web logs, but if you have logging options on in
  19. `app.ini`, then you should be able to go off of `log/gitea.log`, which gives you something like this
  20. on a bad authentication from the web or CLI using SSH or HTTP respectively:
  21. ```log
  22. 2018/04/26 18:15:54 [I] Failed authentication attempt for user from xxx.xxx.xxx.xxx
  23. ```
  24. ```log
  25. 2020/10/15 16:05:09 modules/ssh/ssh.go:143:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  26. ```
  27. (DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
  28. ```log
  29. 2020/10/15 16:05:09 modules/ssh/ssh.go:155:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  30. ```
  31. (DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
  32. ```log
  33. 2020/10/15 16:05:09 modules/ssh/ssh.go:198:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  34. ```
  35. (DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
  36. ```log
  37. 2020/10/15 16:05:09 modules/ssh/ssh.go:213:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  38. ```
  39. (DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
  40. ```log
  41. 2020/10/15 16:05:09 modules/ssh/ssh.go:227:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  42. ```
  43. (DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
  44. ```log
  45. 2020/10/15 16:05:09 modules/ssh/ssh.go:249:sshConnectionFailed() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
  46. ```
  47. (From 1.15 this new message will available and doesn't have any of the false positive results that above messages from publicKeyHandler do. This will only be logged if the user has completely failed authentication.)
  48. ```log
  49. 2020/10/15 16:08:44 ...s/context/context.go:204:HandleText() [E] invalid credentials from xxx.xxx.xxx.xxx
  50. ```
  51. Add our filter in `/etc/fail2ban/filter.d/gitea.conf`:
  52. ```ini
  53. # gitea.conf
  54. [Definition]
  55. failregex = .*(Failed authentication attempt|invalid credentials|Attempted access of unknown user).* from <HOST>
  56. ignoreregex =
  57. ```
  58. Add our jail in `/etc/fail2ban/jail.d/gitea.conf`:
  59. ```ini
  60. [gitea]
  61. enabled = true
  62. filter = gitea
  63. logpath = /var/lib/gitea/log/gitea.log
  64. maxretry = 10
  65. findtime = 3600
  66. bantime = 900
  67. action = iptables-allports
  68. ```
  69. If you're using Docker, you'll also need to add an additional jail to handle the **FORWARD**
  70. chain in **iptables**. Configure it in `/etc/fail2ban/jail.d/gitea-docker.conf`:
  71. ```ini
  72. [gitea-docker]
  73. enabled = true
  74. filter = gitea
  75. logpath = /var/lib/gitea/log/gitea.log
  76. maxretry = 10
  77. findtime = 3600
  78. bantime = 900
  79. action = iptables-allports[chain="FORWARD"]
  80. ```
  81. Then simply run `service fail2ban restart` to apply your changes. You can check to see if
  82. fail2ban has accepted your configuration using `service fail2ban status`.
  83. Make sure and read up on fail2ban and configure it to your needs, this bans someone
  84. for **15 minutes** (from all ports) when they fail authentication 10 times in an hour.
  85. If you run Gitea behind a reverse proxy with Nginx (for example with Docker), you need to add
  86. this to your Nginx configuration so that IPs don't show up as 127.0.0.1:
  87. ```
  88. proxy_set_header X-Real-IP $remote_addr;
  89. ```
  90. The security options in `app.ini` need to be adjusted to allow the interpretation of the headers
  91. as well as the list of IP addresses and networks that describe trusted proxy servers
  92. (See the [configuration cheat sheet](https://docs.gitea.io/en-us/config-cheat-sheet/#security-security) for more information).
  93. ```
  94. REVERSE_PROXY_LIMIT = 1
  95. REVERSE_PROXY_TRUSTED_PROXIES = 127.0.0.1/8 ; 172.17.0.0/16 for the docker default network
  96. ```