|
|
|
@@ -27,6 +27,19 @@ import (
|
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// The names of the settings that waf writes into log lines and ban notes,
|
|
|
|
|
// as the setting that refused a request or lowered a client's limits.
|
|
|
|
|
const (
|
|
|
|
|
ExclusivelyAllowedCountriesSetting = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES"
|
|
|
|
|
TagLimitPercentSetting = "SWW_WAF_TAG_LIMIT_PERCENT"
|
|
|
|
|
ASNBytesLimitPercentSetting = "SWW_WAF_ASN_BYTES_LIMIT_PERCENT"
|
|
|
|
|
CountryBytesLimitPercentSetting = "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT"
|
|
|
|
|
UnknownCountryLimitPercentSetting = "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT"
|
|
|
|
|
ASNLimitPercentURLSetting = "SWW_WAF_ASN_LIMIT_PERCENT_URL"
|
|
|
|
|
BlocklistActionSetting = "SWW_WAF_BLOCKLIST_ACTION"
|
|
|
|
|
DNSBLAbuseIPDBActionSetting = "SWW_WAF_DNSBL_ABUSEIPDB_ACTION"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// WAF is the settings of `sww waf`. A time or size limit of zero is off.
|
|
|
|
|
type WAF struct {
|
|
|
|
|
// ListenAddr is where waf listens (SWW_WAF_LISTEN_ADDR).
|
|
|
|
@@ -618,24 +631,24 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
|
|
|
|
"800",
|
|
|
|
|
),
|
|
|
|
|
ExclusivelyAllowedCountries: env.countries(
|
|
|
|
|
"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""),
|
|
|
|
|
ExclusivelyAllowedCountriesSetting, ""),
|
|
|
|
|
TagLists: env.tagLists("SWW_WAF_TAG_LISTS"),
|
|
|
|
|
TagDeny: env.tags("SWW_WAF_TAG_DENY"),
|
|
|
|
|
TagLimitPercent: env.tagPercents("SWW_WAF_TAG_LIMIT_PERCENT"),
|
|
|
|
|
TagLimitPercent: env.tagPercents(TagLimitPercentSetting),
|
|
|
|
|
RouteWatchURL: env.routeWatchURL("SWW_WAF_ROUTEWATCH_URL"),
|
|
|
|
|
ASNBytesLimitPercent: env.percents(
|
|
|
|
|
"SWW_WAF_ASN_BYTES_LIMIT_PERCENT",
|
|
|
|
|
ASNBytesLimitPercentSetting,
|
|
|
|
|
ParseASN,
|
|
|
|
|
),
|
|
|
|
|
CountryBytesLimitPercent: env.percents(
|
|
|
|
|
"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT",
|
|
|
|
|
CountryBytesLimitPercentSetting,
|
|
|
|
|
parseCountry,
|
|
|
|
|
),
|
|
|
|
|
UnknownCountryLimitPercent: env.percent(
|
|
|
|
|
"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT",
|
|
|
|
|
UnknownCountryLimitPercentSetting,
|
|
|
|
|
"100",
|
|
|
|
|
),
|
|
|
|
|
ASNLimitPercentURL: env.listURL("SWW_WAF_ASN_LIMIT_PERCENT_URL"),
|
|
|
|
|
ASNLimitPercentURL: env.listURL(ASNLimitPercentURLSetting),
|
|
|
|
|
BlocklistURLs: env.listURLs("SWW_WAF_BLOCKLIST_URLS"),
|
|
|
|
|
BlocklistRefresh: env.refresh("SWW_WAF_BLOCKLIST_REFRESH", "24h"),
|
|
|
|
|
DNSBLZones: env.zones("SWW_WAF_DNSBL_ZONES"),
|
|
|
|
@@ -809,9 +822,9 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
|
|
|
|
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
|
|
|
|
|
strconv.FormatBool(cfg.LookupSource != LookupSourceOff))
|
|
|
|
|
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
|
|
|
|
|
"SWW_WAF_BLOCKLIST_ACTION", string(ReputationActionDeny))
|
|
|
|
|
BlocklistActionSetting, string(ReputationActionDeny))
|
|
|
|
|
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
|
|
|
|
|
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25")
|
|
|
|
|
DNSBLAbuseIPDBActionSetting, "limit:25")
|
|
|
|
|
|
|
|
|
|
env.checkInstanceNameForNtfy(cfg.InstanceName, cfg.AlertNtfyURL != nil)
|
|
|
|
|
env.checkLookupDBPath(cfg)
|
|
|
|
@@ -1220,7 +1233,7 @@ func (e *environment) checkLookupDBPath(cfg *WAF) {
|
|
|
|
|
func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
|
|
|
|
for _, country := range cfg.ExclusivelyAllowedCountries {
|
|
|
|
|
if slices.Contains(cfg.TagDeny, CountryTagPrefix+country) {
|
|
|
|
|
e.check("SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES",
|
|
|
|
|
e.check(ExclusivelyAllowedCountriesSetting,
|
|
|
|
|
fmt.Errorf("%q %w", country, errOnBothLists))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -1239,15 +1252,15 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
|
|
|
|
name string
|
|
|
|
|
set bool
|
|
|
|
|
}{
|
|
|
|
|
{"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", len(cfg.ExclusivelyAllowedCountries) > 0},
|
|
|
|
|
{ExclusivelyAllowedCountriesSetting, len(cfg.ExclusivelyAllowedCountries) > 0},
|
|
|
|
|
{"SWW_WAF_ADD_LOOKUP_HEADERS", cfg.AddLookupHeaders},
|
|
|
|
|
{"SWW_WAF_TAG_DENY", slices.ContainsFunc(cfg.TagDeny, onlyLookupGives)},
|
|
|
|
|
{"SWW_WAF_TAG_LIMIT_PERCENT", slices.ContainsFunc(
|
|
|
|
|
{TagLimitPercentSetting, slices.ContainsFunc(
|
|
|
|
|
slices.Collect(maps.Keys(cfg.TagLimitPercent)), onlyLookupGives)},
|
|
|
|
|
{"SWW_WAF_ASN_BYTES_LIMIT_PERCENT", len(cfg.ASNBytesLimitPercent) > 0},
|
|
|
|
|
{"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", len(cfg.CountryBytesLimitPercent) > 0},
|
|
|
|
|
{"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", cfg.UnknownCountryLimitPercent < 100},
|
|
|
|
|
{"SWW_WAF_ASN_LIMIT_PERCENT_URL", cfg.ASNLimitPercentURL != ""},
|
|
|
|
|
{ASNBytesLimitPercentSetting, len(cfg.ASNBytesLimitPercent) > 0},
|
|
|
|
|
{CountryBytesLimitPercentSetting, len(cfg.CountryBytesLimitPercent) > 0},
|
|
|
|
|
{UnknownCountryLimitPercentSetting, cfg.UnknownCountryLimitPercent < 100},
|
|
|
|
|
{ASNLimitPercentURLSetting, cfg.ASNLimitPercentURL != ""},
|
|
|
|
|
{"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE", cfg.AnomalyASN.RequestsPerMinute > 0},
|
|
|
|
|
{"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR", cfg.AnomalyASN.RequestsPerHour > 0},
|
|
|
|
|
{"SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE", cfg.AnomalyASN.BytesPerMinute > 0},
|
|
|
|
@@ -1267,7 +1280,7 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
|
|
|
|
// blocklist too: the file at a URL is fetched as one list or the other.
|
|
|
|
|
func (e *environment) checkASNLimitPercentURL(cfg *WAF) {
|
|
|
|
|
if slices.Contains(cfg.BlocklistURLs, cfg.ASNLimitPercentURL) {
|
|
|
|
|
e.check("SWW_WAF_ASN_LIMIT_PERCENT_URL",
|
|
|
|
|
e.check(ASNLimitPercentURLSetting,
|
|
|
|
|
fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -1294,7 +1307,7 @@ func (e *environment) checkTags(cfg *WAF) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for tag := range cfg.TagLimitPercent {
|
|
|
|
|
e.check("SWW_WAF_TAG_LIMIT_PERCENT", checkTag(cfg, tag))
|
|
|
|
|
e.check(TagLimitPercentSetting, checkTag(cfg, tag))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|