Skip to content

Commit 5e583b8

Browse files
committed
Add http ja5 filter and configuration for it.
Make a unified filtering code for any ja5 type.
1 parent d620aa8 commit 5e583b8

File tree

10 files changed

+632
-406
lines changed

10 files changed

+632
-406
lines changed

fw/http.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* created HTTP/1.1-message.
6767
*
6868
* Copyright (C) 2014 NatSys Lab. (info@natsys-lab.com).
69-
* Copyright (C) 2015-2024 Tempesta Technologies, Inc.
69+
* Copyright (C) 2015-2025 Tempesta Technologies, Inc.
7070
*
7171
* This program is free software; you can redistribute it and/or modify it
7272
* under the terms of the GNU General Public License as published by
@@ -114,6 +114,8 @@
114114
#include "access_log.h"
115115
#include "vhost.h"
116116
#include "websocket.h"
117+
#include "ja5_filter.h"
118+
#include "ja5_conf.h"
117119

118120
#include "sync_socket.h"
119121
#include "lib/common.h"
@@ -5997,6 +5999,15 @@ __check_authority_correctness(TfwHttpReq *req)
59975999
return true;
59986000
}
59996001

6002+
static bool
6003+
tfw_http_check_ja5h_req_limit(TfwHttpReq *req)
6004+
{
6005+
u64 limit = http_get_ja5_recs_limit(req->ja5h);
6006+
u64 rate = ja5h_get_records_rate(req->ja5h);
6007+
6008+
return rate > limit;
6009+
}
6010+
60006011
/**
60016012
* @return zero on success and negative value otherwise.
60026013
* TODO enter the function depending on current GFSM state.
@@ -6158,6 +6169,14 @@ tfw_http_req_process(TfwConn *conn, TfwStream *stream, struct sk_buff *skb,
61586169

61596170
req->ja5h.method = req->method;
61606171

6172+
if (tfw_http_check_ja5h_req_limit(req)) {
6173+
TFW_INC_STAT_BH(clnt.msgs_filtout);
6174+
return tfw_http_req_parse_block(req, 403,
6175+
"parsed request exceeded ja5h limit",
6176+
HTTP2_ECODE_PROTO);
6177+
}
6178+
6179+
61616180
/*
61626181
* The message is fully parsed, the rest of the data in the
61636182
* stream may represent another request or its part.
@@ -7303,6 +7322,7 @@ tfw_http_start(void)
73037322
{
73047323
TfwVhost *dflt_vh = tfw_vhost_lookup_default();
73057324
bool misconfiguration;
7325+
u64 storage_size = http_get_ja5_storage_size();
73067326

73077327
if (WARN_ON_ONCE(!dflt_vh))
73087328
return -1;
@@ -7319,9 +7339,18 @@ tfw_http_start(void)
73197339
return -1;
73207340
}
73217341

7342+
if (storage_size && !ja5h_init_filter(storage_size))
7343+
return -ENOMEM;
7344+
73227345
return 0;
73237346
}
73247347

7348+
static void
7349+
tfw_http_stop(void)
7350+
{
7351+
ja5h_close_filter();
7352+
}
7353+
73257354
/*
73267355
* ------------------------------------------------------------------------
73277356
* configuration handling
@@ -7957,12 +7986,27 @@ static TfwCfgSpec tfw_http_specs[] = {
79577986
.allow_none = true,
79587987
.cleanup = tfw_cfgop_cleanup_max_header_list_size,
79597988
},
7989+
{
7990+
.name = "ja5h",
7991+
.deflt = NULL,
7992+
.handler = tfw_cfg_handle_children,
7993+
.cleanup = http_ja5_cfgop_cleanup,
7994+
.dest = ja5_hash_specs,
7995+
.spec_ext = &(TfwCfgSpecChild) {
7996+
.begin_hook = ja5_cfgop_begin,
7997+
.finish_hook = http_ja5_cfgop_finish
7998+
},
7999+
.allow_none = true,
8000+
.allow_repeat = false,
8001+
.allow_reconfig = true,
8002+
},
79608003
{ 0 }
79618004
};
79628005

79638006
TfwMod tfw_http_mod = {
79648007
.name = "http",
79658008
.start = tfw_http_start,
8009+
.stop = tfw_http_stop,
79668010
.specs = tfw_http_specs,
79678011
};
79688012

0 commit comments

Comments
 (0)