Skip to content

Commit

Permalink
defender: implement logging of events and bans (#1495)
Browse files Browse the repository at this point in the history
defender: implement logging of events and bans

Signed-off-by: Anthrazz <[email protected]>
  • Loading branch information
Anthrazz authored Jan 10, 2024
1 parent 9cde090 commit c21b434
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
41 changes: 36 additions & 5 deletions internal/common/defender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ import (
"time"

"github.com/drakkan/sftpgo/v2/internal/dataprovider"
"github.com/drakkan/sftpgo/v2/internal/logger"
)

// HostEvent is the enumerable for the supported host events
type HostEvent int
type HostEvent string

// Supported host events
const (
HostEventLoginFailed HostEvent = iota
HostEventUserNotFound
HostEventNoLoginTried
HostEventLimitExceeded
HostEventLoginFailed HostEvent = "LoginFailed"

Check failure on line 30 in internal/common/defender.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA9004: only the first constant in this group has an explicit type (staticcheck)
HostEventUserNotFound = "UserNotFound"
HostEventNoLoginTried = "NoLoginTried"
HostEventLimitExceeded = "LimitExceeded"
)

// Supported defender drivers
Expand Down Expand Up @@ -132,6 +133,36 @@ func (d *baseDefender) getScore(event HostEvent) int {
return score
}

// logEvent do log an defender event which modifies the score of an host
func (d *baseDefender) logEvent(ip, protocol string, event HostEvent, totalScore int) {
// ignore events which do not change the host score
eventScore := d.getScore(event)
if eventScore == 0 {
return
}

logger.GetLogger().Debug().
Timestamp().
Str("sender", "defender").
Str("client_ip", ip).
Str("protocol", protocol).
Str("event", string(event)).
Int("increase_score_by", eventScore).
Int("score", totalScore).
Send()
}

// logBan do log a ban of an host due to a too high host score
func (d *baseDefender) logBan(ip, protocol string) {
logger.GetLogger().Info().
Timestamp().
Str("sender", "defender").
Str("client_ip", ip).
Str("protocol", protocol).
Str("event", "banned").
Send()
}

type hostEvent struct {
dateTime time.Time
score int
Expand Down
2 changes: 2 additions & 0 deletions internal/common/defenderdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func (d *dbDefender) AddEvent(ip, protocol string, event HostEvent) {
if err != nil {
return
}
d.baseDefender.logEvent(ip, protocol, event, host.Score)
if host.Score > d.config.Threshold {
d.baseDefender.logBan(ip, protocol)
banTime := time.Now().Add(time.Duration(d.config.BanTime) * time.Minute)
err = dataprovider.SetDefenderBanTime(ip, util.GetTimeAsMsSinceEpoch(banTime))
if err == nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/common/defendermem.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ func (d *memoryDefender) AddEvent(ip, protocol string, event HostEvent) {
idx++
}
}
d.baseDefender.logEvent(ip, protocol, event, hs.TotalScore)

hs.Events = hs.Events[:idx]
if hs.TotalScore >= d.config.Threshold {
d.baseDefender.logBan(ip, protocol)
d.banned[ip] = time.Now().Add(time.Duration(d.config.BanTime) * time.Minute)
delete(d.hosts, ip)
d.cleanupBanned()
Expand All @@ -222,6 +224,7 @@ func (d *memoryDefender) AddEvent(ip, protocol string, event HostEvent) {
d.hosts[ip] = hs
}
} else {
d.baseDefender.logEvent(ip, protocol, event, ev.score)
d.hosts[ip] = hostScore{
TotalScore: ev.score,
Events: []hostEvent{ev},
Expand Down

0 comments on commit c21b434

Please sign in to comment.