2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-12-25 11:08:07 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2021-12-25 11:08:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicDbDefender(t *testing.T) {
|
|
|
|
if !isDbDefenderSupported() {
|
|
|
|
t.Skip("this test is not supported with the current database provider")
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
entries := []dataprovider.IPListEntry{
|
|
|
|
{
|
|
|
|
IPOrNet: "172.16.1.1/32",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeDeny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPOrNet: "172.16.1.2/32",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeDeny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPOrNet: "10.8.0.0/24",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeDeny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPOrNet: "172.16.1.3/32",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeAllow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPOrNet: "172.16.1.4/32",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeAllow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPOrNet: "192.168.8.0/24",
|
|
|
|
Type: dataprovider.IPListTypeDefender,
|
|
|
|
Mode: dataprovider.ListModeAllow,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range entries {
|
|
|
|
e := entries[idx]
|
|
|
|
err := dataprovider.AddIPListEntry(&e, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-12-25 11:08:07 +00:00
|
|
|
config := &DefenderConfig{
|
|
|
|
Enabled: true,
|
|
|
|
BanTime: 10,
|
|
|
|
BanTimeIncrement: 2,
|
|
|
|
Threshold: 5,
|
|
|
|
ScoreInvalid: 2,
|
|
|
|
ScoreValid: 1,
|
2023-01-25 17:49:03 +00:00
|
|
|
ScoreNoAuth: 2,
|
2021-12-25 11:08:07 +00:00
|
|
|
ScoreLimitExceeded: 3,
|
|
|
|
ObservationTime: 15,
|
|
|
|
EntriesSoftLimit: 1,
|
|
|
|
EntriesHardLimit: 10,
|
|
|
|
}
|
|
|
|
d, err := newDBDefender(config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defender := d.(*dbDefender)
|
2023-02-09 08:33:33 +00:00
|
|
|
assert.True(t, defender.IsBanned("172.16.1.1", ProtocolFTP))
|
|
|
|
assert.False(t, defender.IsBanned("172.16.1.10", ProtocolSSH))
|
|
|
|
assert.False(t, defender.IsBanned("10.8.1.3", ProtocolHTTP))
|
|
|
|
assert.True(t, defender.IsBanned("10.8.0.4", ProtocolWebDAV))
|
|
|
|
assert.False(t, defender.IsBanned("invalid ip", ProtocolSSH))
|
2021-12-25 11:08:07 +00:00
|
|
|
hosts, err := defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
_, err = defender.GetHost("10.8.0.3")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent("172.16.1.4", ProtocolSSH, HostEventLoginFailed)
|
|
|
|
defender.AddEvent("192.168.8.4", ProtocolSSH, HostEventUserNotFound)
|
|
|
|
defender.AddEvent("172.16.1.3", ProtocolSSH, HostEventLimitExceeded)
|
2021-12-25 11:08:07 +00:00
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
assert.True(t, defender.getLastCleanup().IsZero())
|
|
|
|
|
|
|
|
testIP := "123.45.67.89"
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent(testIP, ProtocolSSH, HostEventLoginFailed)
|
2021-12-25 11:08:07 +00:00
|
|
|
lastCleanup := defender.getLastCleanup()
|
|
|
|
assert.False(t, lastCleanup.IsZero())
|
|
|
|
score, err := defender.GetScore(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, score)
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, 1, hosts[0].Score)
|
|
|
|
assert.True(t, hosts[0].BanTime.IsZero())
|
|
|
|
assert.Empty(t, hosts[0].GetBanTime())
|
|
|
|
}
|
|
|
|
host, err := defender.GetHost(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, host.Score)
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
|
|
|
banTime, err := defender.GetBanTime(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, banTime)
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent(testIP, ProtocolSSH, HostEventLimitExceeded)
|
2021-12-25 11:08:07 +00:00
|
|
|
score, err = defender.GetScore(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 4, score)
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, 4, hosts[0].Score)
|
|
|
|
assert.True(t, hosts[0].BanTime.IsZero())
|
|
|
|
assert.Empty(t, hosts[0].GetBanTime())
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent(testIP, ProtocolSSH, HostEventNoLoginTried)
|
|
|
|
defender.AddEvent(testIP, ProtocolSSH, HostEventNoLoginTried)
|
2021-12-25 11:08:07 +00:00
|
|
|
score, err = defender.GetScore(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, score)
|
|
|
|
banTime, err = defender.GetBanTime(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, banTime)
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, 0, hosts[0].Score)
|
|
|
|
assert.False(t, hosts[0].BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, hosts[0].GetBanTime())
|
|
|
|
assert.Equal(t, hex.EncodeToString([]byte(testIP)), hosts[0].GetID())
|
|
|
|
}
|
|
|
|
host, err = defender.GetHost(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, host.Score)
|
|
|
|
assert.NotEmpty(t, host.GetBanTime())
|
|
|
|
// ban time should increase
|
2023-02-09 08:33:33 +00:00
|
|
|
assert.True(t, defender.IsBanned(testIP, ProtocolSSH))
|
2021-12-25 11:08:07 +00:00
|
|
|
newBanTime, err := defender.GetBanTime(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, newBanTime.After(*banTime))
|
|
|
|
|
|
|
|
assert.True(t, defender.DeleteHost(testIP))
|
|
|
|
assert.False(t, defender.DeleteHost(testIP))
|
|
|
|
// test cleanup
|
|
|
|
testIP1 := "123.45.67.90"
|
|
|
|
testIP2 := "123.45.67.91"
|
|
|
|
testIP3 := "123.45.67.92"
|
|
|
|
for i := 0; i < 3; i++ {
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent(testIP, ProtocolSSH, HostEventUserNotFound)
|
|
|
|
defender.AddEvent(testIP1, ProtocolSSH, HostEventNoLoginTried)
|
|
|
|
defender.AddEvent(testIP2, ProtocolSSH, HostEventUserNotFound)
|
2021-12-25 11:08:07 +00:00
|
|
|
}
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 3)
|
|
|
|
for _, host := range hosts {
|
|
|
|
assert.Equal(t, 0, host.Score)
|
|
|
|
assert.False(t, host.BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, host.GetBanTime())
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.AddEvent(testIP3, ProtocolSSH, HostEventLoginFailed)
|
2021-12-25 11:08:07 +00:00
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 4)
|
|
|
|
// now set a ban time in the past, so the host will be cleanead up
|
|
|
|
for _, ip := range []string{testIP1, testIP2} {
|
|
|
|
err = dataprovider.SetDefenderBanTime(ip, util.GetTimeAsMsSinceEpoch(time.Now().Add(-1*time.Minute)))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 4)
|
|
|
|
for _, host := range hosts {
|
|
|
|
switch host.IP {
|
|
|
|
case testIP:
|
|
|
|
assert.Equal(t, 0, host.Score)
|
|
|
|
assert.False(t, host.BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, host.GetBanTime())
|
|
|
|
case testIP3:
|
|
|
|
assert.Equal(t, 1, host.Score)
|
|
|
|
assert.True(t, host.BanTime.IsZero())
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
|
|
|
default:
|
|
|
|
assert.Equal(t, 6, host.Score)
|
|
|
|
assert.True(t, host.BanTime.IsZero())
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
host, err = defender.GetHost(testIP)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, host.Score)
|
|
|
|
assert.False(t, host.BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, host.GetBanTime())
|
|
|
|
host, err = defender.GetHost(testIP3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, host.Score)
|
|
|
|
assert.True(t, host.BanTime.IsZero())
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
2022-03-16 17:22:08 +00:00
|
|
|
// set a negative observation time so the from field in the queries will be in the future
|
|
|
|
// we still should get the banned hosts
|
|
|
|
defender.config.ObservationTime = -2
|
|
|
|
assert.Greater(t, defender.getStartObservationTime(), time.Now().UnixMilli())
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, testIP, hosts[0].IP)
|
|
|
|
assert.Equal(t, 0, hosts[0].Score)
|
|
|
|
assert.False(t, hosts[0].BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, hosts[0].GetBanTime())
|
|
|
|
}
|
2022-03-17 13:59:02 +00:00
|
|
|
_, err = defender.GetHost(testIP)
|
2022-03-16 17:22:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-12-25 11:08:07 +00:00
|
|
|
// cleanup db
|
|
|
|
err = dataprovider.CleanupDefender(util.GetTimeAsMsSinceEpoch(time.Now().Add(10 * time.Minute)))
|
|
|
|
assert.NoError(t, err)
|
2022-03-16 17:22:08 +00:00
|
|
|
// the banned host must still be there
|
2021-12-25 11:08:07 +00:00
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
assert.Equal(t, testIP, hosts[0].IP)
|
|
|
|
assert.Equal(t, 0, hosts[0].Score)
|
|
|
|
assert.False(t, hosts[0].BanTime.IsZero())
|
|
|
|
assert.NotEmpty(t, hosts[0].GetBanTime())
|
|
|
|
}
|
2022-03-17 13:59:02 +00:00
|
|
|
_, err = defender.GetHost(testIP)
|
2022-03-16 17:22:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-12-25 11:08:07 +00:00
|
|
|
err = dataprovider.SetDefenderBanTime(testIP, util.GetTimeAsMsSinceEpoch(time.Now().Add(-1*time.Minute)))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.CleanupDefender(util.GetTimeAsMsSinceEpoch(time.Now().Add(10 * time.Minute)))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
hosts, err = defender.GetHosts()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
for _, e := range entries {
|
|
|
|
err := dataprovider.DeleteIPListEntry(e.IPOrNet, e.Type, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-12-25 11:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDbDefenderCleanup(t *testing.T) {
|
|
|
|
if !isDbDefenderSupported() {
|
|
|
|
t.Skip("this test is not supported with the current database provider")
|
|
|
|
}
|
|
|
|
config := &DefenderConfig{
|
|
|
|
Enabled: true,
|
|
|
|
BanTime: 10,
|
|
|
|
BanTimeIncrement: 2,
|
|
|
|
Threshold: 5,
|
|
|
|
ScoreInvalid: 2,
|
|
|
|
ScoreValid: 1,
|
|
|
|
ScoreLimitExceeded: 3,
|
|
|
|
ObservationTime: 15,
|
|
|
|
EntriesSoftLimit: 1,
|
|
|
|
EntriesHardLimit: 10,
|
|
|
|
}
|
|
|
|
d, err := newDBDefender(config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defender := d.(*dbDefender)
|
|
|
|
lastCleanup := defender.getLastCleanup()
|
|
|
|
assert.True(t, lastCleanup.IsZero())
|
|
|
|
defender.cleanup()
|
|
|
|
lastCleanup = defender.getLastCleanup()
|
|
|
|
assert.False(t, lastCleanup.IsZero())
|
|
|
|
defender.cleanup()
|
|
|
|
assert.Equal(t, lastCleanup, defender.getLastCleanup())
|
2023-02-09 08:33:33 +00:00
|
|
|
defender.setLastCleanup(time.Time{})
|
|
|
|
assert.True(t, defender.getLastCleanup().IsZero())
|
2021-12-25 11:08:07 +00:00
|
|
|
defender.setLastCleanup(time.Now().Add(-time.Duration(config.ObservationTime) * time.Minute * 4))
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
defender.cleanup()
|
|
|
|
assert.True(t, lastCleanup.Before(defender.getLastCleanup()))
|
|
|
|
|
|
|
|
providerConf := dataprovider.GetProviderConfig()
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
lastCleanup = util.GetTimeFromMsecSinceEpoch(time.Now().Add(-time.Duration(config.ObservationTime) * time.Minute * 4).UnixMilli())
|
2021-12-25 11:08:07 +00:00
|
|
|
defender.setLastCleanup(lastCleanup)
|
|
|
|
defender.cleanup()
|
|
|
|
// cleanup will fail and so last cleanup should be reset to the previous value
|
|
|
|
assert.Equal(t, lastCleanup, defender.getLastCleanup())
|
|
|
|
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDbDefenderSupported() bool {
|
|
|
|
// SQLite shares the implementation with other SQL-based provider but it makes no sense
|
|
|
|
// to use it outside test cases
|
|
|
|
switch dataprovider.GetProviderStatus().Driver {
|
|
|
|
case dataprovider.MySQLDataProviderName, dataprovider.PGSQLDataProviderName,
|
|
|
|
dataprovider.CockroachDataProviderName, dataprovider.SQLiteDataProviderName:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|