Skip to content

Commit 756daea

Browse files
committed
feat: add country tracking to ban management; update templates and scripts for country display
1 parent 56432f6 commit 756daea

File tree

9 files changed

+388
-19
lines changed

9 files changed

+388
-19
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ repos:
6262
- id: codespell
6363
name: Codespell Spell Checker
6464
exclude: (^src/(ui/templates|common/core/.+/files|bw/loading)/.+.html|modsecurity-rules.conf.*|src/ui/app/static/(fonts|libs)/.+)$
65-
entry: codespell --ignore-regex="(tabEl|Widgits)" --skip CHANGELOG.md,CODE_OF_CONDUCT.md,src/ui/client/build.py,src/ui/app/static/json/countries.geojson,src/ui/app/static/js/pages/reports.js,src/ui/app/static/json/periscop.min.json,src/ui/app/static/json/blockhaus.min.json,src/ui/app/routes/reports.py
65+
entry: codespell --ignore-regex="(tabEl|Widgits)" --skip CHANGELOG.md,CODE_OF_CONDUCT.md,src/ui/client/build.py,src/ui/app/static/json/countries.geojson,src/ui/app/static/js/pages/bans.js,src/ui/app/static/json/periscop.min.json,src/ui/app/static/json/blockhaus.min.json,src/ui/app/routes/reports.py
6666
language: python
6767
types: [text]
6868

src/bw/lua/bunkerweb/api.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local api = class("api")
1515
local datastore = cdatastore:new()
1616
local logger = clogger:new("API")
1717

18+
local get_country = utils.get_country
1819
local get_variable = utils.get_variable
1920
local is_ip_in_networks = utils.is_ip_in_networks
2021
-- local run = shell.run
@@ -248,6 +249,7 @@ api.global.POST["^/ban$"] = function(self)
248249
exp = 86400,
249250
reason = "manual",
250251
service = "unknown",
252+
country = "local",
251253
}
252254
ban.ip = ip["ip"]
253255
if ip["exp"] then
@@ -259,12 +261,19 @@ api.global.POST["^/ban$"] = function(self)
259261
if ip["service"] then
260262
ban.service = ip["service"]
261263
end
264+
local country, err = get_country(ban["ip"])
265+
if not country then
266+
country = "unknown"
267+
logger:log(ERR, "can't get country code " .. err)
268+
end
269+
ban.country = country
262270
datastore:set(
263271
"bans_ip_" .. ban["ip"],
264272
encode({
265273
reason = ban["reason"],
266274
service = ban["service"],
267275
date = os.time(),
276+
country = ban["country"],
268277
}),
269278
ban["exp"]
270279
)
@@ -301,6 +310,7 @@ api.global.GET["^/bans$"] = function(self)
301310
reason = ban_data["reason"],
302311
service = ban_data["service"],
303312
date = ban_data["date"],
313+
country = ban_data["country"],
304314
exp = math.floor(ttl),
305315
})
306316
end

src/bw/lua/bunkerweb/utils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,13 @@ utils.is_banned = function(ip)
735735
return false, "not banned"
736736
end
737737

738-
utils.add_ban = function(ip, reason, ttl, service)
738+
utils.add_ban = function(ip, reason, ttl, service, country)
739739
-- Set on local datastore
740740
local ban_data = encode({
741741
reason = reason,
742742
service = service or "unknown",
743743
date = os.time(),
744+
country = country or "local",
744745
})
745746
local ok, err = datastore:set("bans_ip_" .. ip, ban_data, ttl)
746747
if not ok then

src/common/cli/CLI.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,16 @@ def bans(self) -> Tuple[bool, str]:
257257
cli_str += "No ban found\n"
258258

259259
for ban in bans:
260+
banned_country = ban.get("country", "unknown")
260261
banned_date = ""
261262
remaining = "for eternity"
262263
if ban["date"] != -1:
263264
banned_date = f"the {datetime.fromtimestamp(ban['date']).strftime('%Y-%m-%d at %H:%M:%S %Z')} "
264265
if ban["exp"] != -1:
265266
remaining = f"for {format_remaining_time(ban['exp'])} remaining"
266-
cli_str += f"- {ban['ip']} ; banned {banned_date}{remaining} with reason \"{ban.get('reason', 'no reason given')}\""
267+
cli_str += (
268+
f"- {ban['ip']} from country \"{banned_country}\" ; banned {banned_date}{remaining} with reason \"{ban.get('reason', 'no reason given')}\""
269+
)
267270

268271
if ban.get("service", "unknown") != "unknown":
269272
cli_str += f" by {ban['service'] if ban['service'] != '_' else 'default server'}"

src/common/core/badbehavior/badbehavior.lua

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local timer_at = ngx.timer.at
1212
local add_ban = utils.add_ban
1313
local is_whitelisted = utils.is_whitelisted
1414
local is_banned = utils.is_banned
15+
local get_country = utils.get_country
1516
local get_security_mode = utils.get_security_mode
1617
local tostring = tostring
1718

@@ -39,6 +40,16 @@ function badbehavior:log()
3940
end
4041
-- Get security mode
4142
local security_mode = get_security_mode(self.ctx)
43+
-- Get country
44+
local country = "local"
45+
local err
46+
if self.ctx.bw.ip_is_global then
47+
country, err = get_country(self.ctx.bw.remote_addr)
48+
if not country then
49+
country = "unknown"
50+
self.logger:log(ERR, "can't get country code " .. err)
51+
end
52+
end
4253
-- Call increase function later and with cosocket enabled
4354
local ok, err = timer_at(
4455
0,
@@ -49,7 +60,8 @@ function badbehavior:log()
4960
tonumber(self.variables["BAD_BEHAVIOR_THRESHOLD"]),
5061
self.use_redis,
5162
self.ctx.bw.server_name,
52-
security_mode
63+
security_mode,
64+
country
5365
)
5466
if not ok then
5567
return self:ret(false, "can't create increase timer : " .. err)
@@ -67,7 +79,17 @@ function badbehavior:log_stream()
6779
end
6880

6981
-- luacheck: ignore 212
70-
function badbehavior.increase(premature, ip, count_time, ban_time, threshold, use_redis, server_name, security_mode)
82+
function badbehavior.increase(
83+
premature,
84+
ip,
85+
count_time,
86+
ban_time,
87+
threshold,
88+
use_redis,
89+
server_name,
90+
security_mode,
91+
country
92+
)
7193
-- Instantiate objects
7294
local logger = require "bunkerweb.logger":new("badbehavior")
7395
local datastore = require "bunkerweb.datastore":new()
@@ -108,7 +130,7 @@ function badbehavior.increase(premature, ip, count_time, ban_time, threshold, us
108130
-- Store local ban
109131
if counter > threshold then
110132
if security_mode == "block" then
111-
ok, err = add_ban(ip, "bad behavior", ban_time, server_name)
133+
ok, err = add_ban(ip, "bad behavior", ban_time, server_name, country)
112134
if not ok then
113135
logger:log(ERR, "(increase) can't save ban : " .. err)
114136
return

0 commit comments

Comments
 (0)