mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
128 lines
4.2 KiB
128 lines
4.2 KiB
7 years ago
|
---
|
||
|
date: "2018-05-11T11:00:00+02:00"
|
||
2 years ago
|
title: "Fail2ban Setup "
|
||
7 years ago
|
slug: "fail2ban-setup"
|
||
1 year ago
|
sidebar_position: 16
|
||
4 years ago
|
toc: false
|
||
7 years ago
|
draft: false
|
||
2 years ago
|
aliases:
|
||
|
- /en-us/fail2ban-setup
|
||
7 years ago
|
menu:
|
||
|
sidebar:
|
||
2 years ago
|
parent: "administration"
|
||
7 years ago
|
name: "Fail2ban setup"
|
||
1 year ago
|
sidebar_position: 16
|
||
7 years ago
|
identifier: "fail2ban-setup"
|
||
|
---
|
||
|
|
||
5 years ago
|
# Fail2ban setup to block users after failed login attempts
|
||
7 years ago
|
|
||
4 years ago
|
**Remember that fail2ban is powerful and can cause lots of issues if you do it incorrectly, so make
|
||
7 years ago
|
sure to test this before relying on it so you don't lock yourself out.**
|
||
|
|
||
4 years ago
|
Gitea returns an HTTP 200 for bad logins in the web logs, but if you have logging options on in
|
||
|
`app.ini`, then you should be able to go off of `log/gitea.log`, which gives you something like this
|
||
4 years ago
|
on a bad authentication from the web or CLI using SSH or HTTP respectively:
|
||
7 years ago
|
|
||
|
```log
|
||
|
2018/04/26 18:15:54 [I] Failed authentication attempt for user from xxx.xxx.xxx.xxx
|
||
|
```
|
||
4 years ago
|
|
||
|
```log
|
||
|
2020/10/15 16:05:09 modules/ssh/ssh.go:143:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
|
||
4 years ago
|
|
||
|
```log
|
||
|
2020/10/15 16:05:09 modules/ssh/ssh.go:155:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
|
||
4 years ago
|
|
||
4 years ago
|
```log
|
||
4 years ago
|
2020/10/15 16:05:09 modules/ssh/ssh.go:198:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
4 years ago
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
|
||
4 years ago
|
|
||
|
```log
|
||
|
2020/10/15 16:05:09 modules/ssh/ssh.go:213:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
|
||
4 years ago
|
|
||
|
```log
|
||
|
2020/10/15 16:05:09 modules/ssh/ssh.go:227:publicKeyHandler() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(DEPRECATED: This may be a false positive as the user may still go on to correctly authenticate.)
|
||
|
|
||
|
```log
|
||
|
2020/10/15 16:05:09 modules/ssh/ssh.go:249:sshConnectionFailed() [W] Failed authentication attempt from xxx.xxx.xxx.xxx
|
||
|
```
|
||
2 years ago
|
|
||
3 years ago
|
(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.)
|
||
4 years ago
|
|
||
4 years ago
|
```log
|
||
|
2020/10/15 16:08:44 ...s/context/context.go:204:HandleText() [E] invalid credentials from xxx.xxx.xxx.xxx
|
||
|
```
|
||
7 years ago
|
|
||
5 years ago
|
Add our filter in `/etc/fail2ban/filter.d/gitea.conf`:
|
||
7 years ago
|
|
||
|
```ini
|
||
|
# gitea.conf
|
||
|
[Definition]
|
||
4 years ago
|
failregex = .*(Failed authentication attempt|invalid credentials|Attempted access of unknown user).* from <HOST>
|
||
7 years ago
|
ignoreregex =
|
||
|
```
|
||
|
|
||
5 years ago
|
Add our jail in `/etc/fail2ban/jail.d/gitea.conf`:
|
||
7 years ago
|
|
||
|
```ini
|
||
|
[gitea]
|
||
|
enabled = true
|
||
|
filter = gitea
|
||
4 years ago
|
logpath = /var/lib/gitea/log/gitea.log
|
||
7 years ago
|
maxretry = 10
|
||
|
findtime = 3600
|
||
|
bantime = 900
|
||
|
action = iptables-allports
|
||
|
```
|
||
|
|
||
4 years ago
|
If you're using Docker, you'll also need to add an additional jail to handle the **FORWARD**
|
||
5 years ago
|
chain in **iptables**. Configure it in `/etc/fail2ban/jail.d/gitea-docker.conf`:
|
||
|
|
||
|
```ini
|
||
|
[gitea-docker]
|
||
|
enabled = true
|
||
|
filter = gitea
|
||
3 years ago
|
logpath = /var/lib/gitea/log/gitea.log
|
||
5 years ago
|
maxretry = 10
|
||
|
findtime = 3600
|
||
|
bantime = 900
|
||
|
action = iptables-allports[chain="FORWARD"]
|
||
|
```
|
||
|
|
||
4 years ago
|
Then simply run `service fail2ban restart` to apply your changes. You can check to see if
|
||
5 years ago
|
fail2ban has accepted your configuration using `service fail2ban status`.
|
||
|
|
||
4 years ago
|
Make sure and read up on fail2ban and configure it to your needs, this bans someone
|
||
7 years ago
|
for **15 minutes** (from all ports) when they fail authentication 10 times in an hour.
|
||
|
|
||
6 years ago
|
If you run Gitea behind a reverse proxy with Nginx (for example with Docker), you need to add
|
||
4 years ago
|
this to your Nginx configuration so that IPs don't show up as 127.0.0.1:
|
||
7 years ago
|
|
||
|
```
|
||
|
proxy_set_header X-Real-IP $remote_addr;
|
||
|
```
|
||
3 years ago
|
|
||
|
The security options in `app.ini` need to be adjusted to allow the interpretation of the headers
|
||
|
as well as the list of IP addresses and networks that describe trusted proxy servers
|
||
|
(See the [configuration cheat sheet](https://docs.gitea.io/en-us/config-cheat-sheet/#security-security) for more information).
|
||
|
|
||
|
```
|
||
|
REVERSE_PROXY_LIMIT = 1
|
||
|
REVERSE_PROXY_TRUSTED_PROXIES = 127.0.0.1/8 ; 172.17.0.0/16 for the docker default network
|
||
|
```
|