first commit

This commit is contained in:
potatso
2023-06-27 22:50:17 +08:00
commit 16c604ee1e
12 changed files with 1043 additions and 0 deletions

118
lib/resty/coraza.lua Normal file
View File

@@ -0,0 +1,118 @@
local log = require "resty.coraza.log"
local request = require "resty.coraza.request"
local coraza = require "resty.coraza.coraza"
local consts = require "resty.coraza.constants"
local nlog = ngx.log
local ngx_var = ngx.var
local ngx_ctx = ngx.ctx
local ngx_req = ngx.req
local fmt = string.format
local debug_fmt = log.debug_fmt
local err_fmt = log.err_fmt
local warn_fmt = log.warn_fmt
local _M = {
_VERSION = '1.0.0'
}
function _M.do_init()
_M.waf = coraza.new_waf()
end
function _M.rules_add_file(file)
coraza.rules_add_file(_M.waf, file)
end
function _M.rules_add(directives)
coraza.rules_add(_M.waf, directives)
end
function _M.do_access_filter()
-- each connection will be created a transaction
local transaction = coraza.new_transaction(_M.waf)
ngx_ctx.transaction = transaction
coraza.process_connection(transaction, ngx_var.remote_addr, ngx_var.remote_port,
ngx_var.server_addr, ngx_var.server_port)
-- process uri
coraza.process_uri(transaction, ngx_var.request_uri, ngx_req.get_method(), ngx_var.server_protocol)
-- process http get args.The coraza_process_uri function recommends using AddGetRequestArgument to add get args
request.build_and_process_get_args(transaction)
-- process http req headers
request.build_and_process_header(transaction)
-- process http req body if has
request.build_and_process_body(transaction)
ngx_ctx.action, ngx_ctx.status_code = coraza.intervention(transaction)
_M.do_handle()
end
function _M.do_free()
local transaction = ngx_ctx.transaction
if transaction ~= nil then
nlog(debug_fmt("transaction %s is freed by coraza_free_transaction", ngx_ctx.request_id))
ngx_ctx.transaction = nil
coraza.free_transaction(transaction)
end
end
function _M.do_handle()
-- transaction is interrupted by policy, be free firstly.
-- If request has disrupted by coraza, the transaction is freed and set to nil.
-- Response which was disrupted doesn't make sense.
if ngx_ctx.action ~= nil and ngx_ctx.transaction ~= nil then
nlog(warn_fmt([[Transaction %s request: "%s" is interrupted by policy. Action is %s]],
ngx_ctx.request_id, ngx_var.request, ngx_ctx.action))
if ngx_ctx.action == "drop" then
ngx.status = ngx_ctx.status_code
local ok, msg = pcall(ngx.say, fmt(consts.BLOCK_CONTENT_FORMAT, ngx_ctx.status_code))
if ok == false then
nlog(err_fmt(msg))
end
return ngx.exit(ngx.status)
-- TODO: disrupted by more action
--elseif ngx_ctx.action == "deny" then
-- ngx.status = ngx_ctx.status_code
-- -- NYI: cannot call this C function (yet)
-- -- ngx.header.content_type = consts.BLOCK_CONTENT_TYPE
-- ngx.say(fmt(consts.BLOCK_CONTENT_FORMAT, ngx_ctx.status_code))
-- return ngx.exit(ngx.status)
end
end
end
function _M.do_header_filter()
if ngx_ctx.action ~= nil then
-- If request was interrupted by coraza at access_by_lua phrase, the ngx_ctx.transaction will be set nil.
-- We can bypass the check.
nlog(debug_fmt("Transaction %s has been disrupted at request phrase. ignore", ngx_ctx.request_id))
return
end
local h = ngx.resp.get_headers(0, true)
for k, v in pairs(h) do
coraza.add_response_header(ngx_ctx.transaction, k, v)
end
-- copy from https://github.com/SpiderLabs/ModSecurity-nginx/blob/d59e4ad121df702751940fd66bcc0b3ecb51a079/src/ngx_http_modsecurity_header_filter.c#L527
coraza.process_response_headers(ngx_ctx.transaction, ngx.status, "HTTP 1.1")
-- TODO: add http response body to coraza.append_response_body. Openresty can't disrupt the body_filter phrase
--local resp_body = string.sub(ngx.arg[1], 1, 1000)
--ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
--if ngx.arg[2] then
-- ngx.var.resp_body = ngx.ctx.buffered
--end
--
--coraza.append_response_body(ngx_ctx.transaction, ngx.ctx.buffered)
--coraza.process_response_body(ngx_ctx.transaction)
ngx_ctx.action, ngx_ctx.status_code = coraza.intervention(ngx_ctx.transaction)
_M.do_handle()
end
return _M

View File

@@ -0,0 +1,14 @@
local t = {}
t.MODE_OFF = "off"
t.MODE_BLOCK = "block"
t.MODE_MONITOR = "monitor"
t.NGX_HTTP_HEADER_PREFIX = "http_"
t.BLOCK_CONTENT_TYPE = "application/json"
t.BLOCK_CONTENT_FORMAT = [[{"code": %d, "message": "This connection was blocked by Coroza!"}]]
return t

319
lib/resty/coraza/coraza.lua Normal file
View File

@@ -0,0 +1,319 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by mac.
--- DateTime: 2023/6/14 09:50
---
local ffi = require "ffi"
local log = require "resty.coraza.log"
local nlog = ngx.log
local ngx_var = ngx.var
local ngx_ctx = ngx.ctx
local err_fmt = log.err_fmt
local debug_fmt = log.debug_fmt
local cast_to_c_char = function(str)
return ffi.cast("char *", str)
end
local ok, coraza = pcall(ffi.load, "/usr/local/lib/libcoraza.dylib")
if ok ~= true then
ok, coraza = pcall(ffi.load, "libcoraza.so")
if ok ~= true then
nlog(log.err_fmt("Unable to load libcoraza, exiting! %s\n----", debug.traceback()))
return
end
end
ffi.cdef [[
typedef struct coraza_intervention_t
{
char *action;
char *log;
char *url;
int status;
int pause;
int disruptive;
} coraza_intervention_t;
typedef uint64_t coraza_waf_t;
typedef uint64_t coraza_transaction_t;
typedef void (*coraza_log_cb) (const void *);
void send_log_to_cb(coraza_log_cb cb, const char *msg);
/*not used api/ not implement api*/
extern int coraza_update_status_code(coraza_transaction_t t, int code);
extern int coraza_rules_count(coraza_waf_t w);
extern int coraza_rules_merge(coraza_waf_t w1, coraza_waf_t w2, char** er);
extern void coraza_set_log_cb(coraza_waf_t waf, coraza_log_cb cb);
/*initialize phrase/ init_worker_by_lua*/
extern coraza_waf_t coraza_new_waf();
extern int coraza_rules_add_file(coraza_waf_t w, char* file, char** er);
extern int coraza_rules_add(coraza_waf_t w, char* directives, char** er);
/*http request phrase*/
extern coraza_transaction_t coraza_new_transaction(coraza_waf_t waf, void* logCb);
extern coraza_transaction_t coraza_new_transaction_with_id(coraza_waf_t waf, char* id, void* logCb);
extern int coraza_process_connection(coraza_transaction_t t, char* sourceAddress, int clientPort, char* serverHost, int serverPort);
extern int coraza_process_uri(coraza_transaction_t t, char* uri, char* method, char* proto);
extern int coraza_add_get_args(coraza_transaction_t t, char* name, char* value);
extern int coraza_add_request_header(coraza_transaction_t t, char* name, int name_len, char* value, int value_len);
extern int coraza_process_request_headers(coraza_transaction_t t);
extern int coraza_append_request_body(coraza_transaction_t t, unsigned char* data, int length);
extern int coraza_request_body_from_file(coraza_transaction_t t, char* file);
extern int coraza_process_request_body(coraza_transaction_t t);
/*http response phrase*/
extern int coraza_add_response_header(coraza_transaction_t t, char* name, int name_len, char* value, int value_len);
extern int coraza_process_response_headers(coraza_transaction_t t, int status, char* proto);
extern int coraza_append_response_body(coraza_transaction_t t, unsigned char* data, int length);
extern int coraza_process_response_body(coraza_transaction_t t);
/* end */
extern int coraza_process_logging(coraza_transaction_t t);
extern int coraza_free_transaction(coraza_transaction_t t);
extern int coraza_free_intervention(coraza_intervention_t* it);
extern int coraza_free_waf(coraza_waf_t t);
extern coraza_intervention_t* coraza_intervention(coraza_transaction_t tx);
]]
local _M = {
_VERSION = '1.0.0'
}
-- global variable to store error value
local err_Str = "error"
local err_in_Ptr = ffi.new("char[?]", #err_Str + 2, err_Str)
local err_Ptr = ffi.new("char*[1]", err_in_Ptr);
function _M.new_waf()
-- extern coraza_waf_t coraza_new_waf();
local waf = coraza.coraza_new_waf()
nlog(debug_fmt("Success to creat new waf"))
return waf
end
function _M.rules_add_file(waf, conf_file)
-- extern int coraza_rules_add_file(coraza_waf_t w, char* file, char** er);
local code = coraza.coraza_rules_add_file(waf, cast_to_c_char(conf_file), err_Ptr)
if code == 0 then
nlog(err_fmt(ffi.string(err_Ptr[0])))
else
nlog(debug_fmt("Success to load rule file with %s", conf_file))
end
end
function _M.rules_add(waf, rule)
-- extern int coraza_rules_add(coraza_waf_t w, char* directives, char** er);
local code = coraza.coraza_rules_add(waf, cast_to_c_char(rule), err_Ptr)
if code == 0 then
nlog(err_fmt(ffi.string(err_Ptr[0])))
else
nlog(debug_fmt("Success to load rule with %s", rule))
end
end
function _M.new_transaction(waf)
-- a transaction represent a http request and reponse.It should free when
-- end of process.If there is a memory leak issue, you should focus on
-- checking whether the transaction objects are correctly released or not.
-- In end of process, or intervention.
-- extern coraza_transaction_t coraza_new_transaction(coraza_waf_t waf, void* logCb);
local res = coraza.coraza_new_transaction(waf, nil)
ngx_ctx.request_id = ngx_var.request_id
nlog(debug_fmt("Success to creat new transaction id %s", ngx_ctx.request_id))
return res
end
function _M.process_connection(transaction, sourceAddress, clientPort, serverHost, serverPort)
-- extern int coraza_process_connection(coraza_transaction_t t, char* sourceAddress, int clientPort,
-- char* serverHost, int serverPort);
local res = coraza.coraza_process_connection(transaction, cast_to_c_char(sourceAddress),
tonumber(clientPort), cast_to_c_char(serverHost), tonumber(serverPort))
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_connection with " ..
"sourceAddress:%s clientPort:%s serverHost:%s serverPort:%s",
ngx_ctx.request_id, sourceAddress, clientPort, serverHost, serverPort))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_process_connection with " ..
"sourceAddress:%s clientPort:%s serverHost:%s serverPort:%s",
ngx_ctx.request_id, sourceAddress, clientPort, serverHost, serverPort))
end
end
function _M.process_uri(transaction, uri, method, proto)
-- This function won't add GET arguments, they must be added with AddArgument
-- extern int coraza_process_uri(coraza_transaction_t t, char* uri, char* method, char* proto);
local res = coraza.coraza_process_uri(transaction, cast_to_c_char(uri),
cast_to_c_char(method), cast_to_c_char(proto))
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_uri with %s %s %s",
ngx_ctx.request_id, ngx_ctx.request_id, method, uri, proto))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_process_uri with %s %s %s",
ngx_ctx.request_id, method, uri, proto))
end
end
function _M.add_request_header(transaction, header_name, header_value)
-- extern int coraza_add_request_header(coraza_transaction_t t, char* name, int name_len,
-- char* value, int value_len);
local res = coraza.coraza_add_request_header(transaction, cast_to_c_char(header_name), #header_name,
cast_to_c_char(header_value), #header_value)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_add_request_header with %s:%s",
ngx_ctx.request_id, header_name, header_value))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_add_request_header with %s:%s",
ngx_ctx.request_id, header_name, header_value))
end
end
function _M.add_get_args(transaction, header_name, header_value)
-- extern int coraza_add_get_args(coraza_transaction_t t, char* name, char* value);
local res = coraza.coraza_add_get_args(transaction, cast_to_c_char(header_name),
cast_to_c_char(header_value))
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_add_get_args with %s:%s",
ngx_ctx.request_id, header_name, header_value))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_add_get_args with %s:%s",
ngx_ctx.request_id, header_name, header_value))
end
end
function _M.process_request_headers(transaction)
-- extern int coraza_process_request_headers(coraza_transaction_t t);
local res = coraza.coraza_process_request_headers(transaction)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_request_headers",
ngx_ctx.request_id))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_process_request_headers",
ngx_ctx.request_id))
end
end
function _M.intervention(transaction)
-- extern coraza_intervention_t* coraza_intervention(coraza_transaction_t tx);
local intervention = coraza.coraza_intervention(transaction)
if intervention ~= nil then
local action = ffi.string(intervention.action)
local status_code = tonumber(intervention.status)
--free intervention to avoid memory leak
coraza.coraza_free_intervention(intervention)
nlog(debug_fmt("Transaction %s disrupted with status %s action %s",
ngx_ctx.request_id, status_code, action))
return action, status_code
else
nlog(debug_fmt("Failed to disrupt transaction %s", ngx_ctx.request_id))
return nil, nil
end
end
function _M.free_transaction(transaction)
-- extern int coraza_free_transaction(coraza_transaction_t t);
local res = coraza.coraza_free_transaction(transaction)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_free_transaction",
ngx_ctx.request_id))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_free_transaction",
ngx_ctx.request_id))
end
end
function _M.append_request_body(transaction, body)
-- extern int coraza_append_request_body(coraza_transaction_t t, unsigned char* data, int length);
local res = coraza.coraza_append_request_body(transaction, cast_to_c_char(body), #body)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_append_request_body with %s",
ngx_ctx.request_id, body))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_append_request_body with %s",
ngx_ctx.request_id, body))
end
end
function _M.request_body_from_file(transaction, file_path)
-- extern int coraza_request_body_from_file(coraza_transaction_t t, char* file);
-- return 0 if success, otherwish return 1
local res = coraza.coraza_request_body_from_file(transaction, cast_to_c_char(file_path))
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_request_body_from_file with %s",
ngx_ctx.request_id, file_path))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_request_body_from_file with %s",
ngx_ctx.request_id, file_path))
end
end
function _M.process_request_body(transaction)
-- extern int coraza_process_request_body(coraza_transaction_t t);
local res = coraza.coraza_process_request_body(transaction)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_request_body",
ngx_ctx.request_id))
else
nlog(debug_fmt("Transaction %s uccess to invoke coraza_process_request_body",
ngx_ctx.request_id))
end
end
-- for processing response
function _M.process_response_headers(transaction, status_code, proto)
-- extern int coraza_process_response_headers(coraza_transaction_t t, int status, char* proto);
local res = coraza.coraza_process_response_headers(transaction, tonumber(status_code), cast_to_c_char(proto))
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_response_headers with %s %s",
ngx_ctx.request_id, status_code, proto))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_process_response_headers with %s %s",
ngx_ctx.request_id, status_code, proto))
end
end
function _M.add_response_header(transaction, header_name, header_value)
-- extern int coraza_add_response_header(coraza_transaction_t t, char* name,
-- int name_len, char* value, int value_len);
local res = coraza.coraza_add_response_header(transaction, cast_to_c_char(header_name), #header_name,
cast_to_c_char(header_value), #header_value)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_add_response_header with %s:%s",
ngx_ctx.request_id, header_name, header_value))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_add_response_header with %s:%s",
ngx_ctx.request_id, header_name, header_value))
end
end
function _M.append_response_body(transaction, body)
local res = coraza.coraza_append_response_body(transaction, cast_to_c_char(body), #body)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_append_response_body with %s",
ngx_ctx.request_id, body))
else
nlog(debug_fmt("Transaction %s success to invoke coraza_append_response_body with %s",
ngx_ctx.request_id, body))
end
end
function _M.process_response_body(transaction)
local res = coraza.coraza_process_response_body(transaction)
if res == 1 then
nlog(err_fmt("Transaction %s failed to invoke coraza_process_response_body",
ngx_ctx.request_id))
else
nlog(debug_fmt("Transaction %s uccess to invoke coraza_process_response_body",
ngx_ctx.request_id))
end
end
return _M

33
lib/resty/coraza/log.lua Normal file
View File

@@ -0,0 +1,33 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by mac.
--- DateTime: 2023/6/14 09:47
---
---
local _M = {
_VERSION = '1.0.0'
}
local fmt = string.format
local ERR = ngx.ERR
local WARN = ngx.WARN
local DEBUG = ngx.DEBUG
local function log(formatstring, ...)
return fmt(ngx.get_phase().." phrase ".."lua-resty-coraza: "..formatstring, ...)
end
function _M.err_fmt(formatstring, ...)
return ERR, log(formatstring, ...)
end
function _M.warn_fmt(formatstring, ...)
return WARN, log(formatstring, ...)
end
function _M.debug_fmt(formatstring, ...)
return DEBUG, log(formatstring, ...)
end
return _M

View File

@@ -0,0 +1,55 @@
local coraza = require "resty.coraza.coraza"
local fmt = string.format
local ngx = ngx
local nlog = ngx.log
local ngx_req = ngx.req
local _M = {
_VERSION = '1.0.0',
}
function _M.build_and_process_header(transaction)
local headers, err = ngx_req.get_headers(0, true)
if err then
err = fmt("failed to call ngx_req.get_headers: %s", err)
nlog(ngx.ERR, err)
end
for k, v in pairs(headers) do
coraza.add_request_header(transaction, k, v)
end
coraza.process_request_headers(transaction)
end
function _M.build_and_process_body(transaction)
local req_body = ngx_req.get_body_data()
if not req_body then
-- TODO: fix code
local path = ngx_req.get_body_file()
if not path then
-- end process
return
end
coraza.request_body_from_file(path)
else
local req_body_size = #req_body
-- TODO req_body_size > req_body_size_opt
coraza.append_request_body(transaction, req_body)
end
coraza.process_request_body(transaction)
end
function _M.build_and_process_get_args(transaction)
-- process http get args if has
local arg = ngx_req.get_uri_args()
for k,v in pairs(arg) do
coraza.add_get_args(transaction, k, v)
end
end
return _M