Dockerfile to debian bookworm + stared rust project
This commit is contained in:
10
.gitignore
vendored
10
.gitignore
vendored
@@ -30,3 +30,13 @@ docker-compose.yml
|
|||||||
**/npm-debug.log*
|
**/npm-debug.log*
|
||||||
**/yarn-debug.log*
|
**/yarn-debug.log*
|
||||||
**/yarn-error.log*
|
**/yarn-error.log*
|
||||||
|
|
||||||
|
#rust
|
||||||
|
/backend/binsrc/nfqueue_regex/debug/
|
||||||
|
/backend/binsrc/nfqueue_regex/target/
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
**/*.pdb
|
||||||
21
Dockerfile
21
Dockerfile
@@ -2,34 +2,31 @@ FROM node:20-bullseye-slim AS frontend
|
|||||||
RUN mkdir /app
|
RUN mkdir /app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
ADD ./frontend/package.json .
|
ADD ./frontend/package.json .
|
||||||
ADD ./frontend/package-lock.json .
|
ADD ./frontend/yarn.lock .
|
||||||
RUN yarn install
|
RUN yarn install
|
||||||
COPY ./frontend/ .
|
COPY ./frontend/ .
|
||||||
RUN yarn build
|
RUN yarn build
|
||||||
|
|
||||||
|
|
||||||
#Building main conteiner
|
#Building main conteiner
|
||||||
FROM python:alpine AS base
|
FROM debian:bookworm-slim as base
|
||||||
|
RUN apt-get update -qq && apt-get upgrade -qq
|
||||||
RUN apk update
|
RUN apt-get install -qq python3-pip build-essential
|
||||||
RUN apk add g++ git pcre2-dev libnetfilter_queue-dev libpcap-dev\
|
RUN apt-get install -qq git libpcre2-dev libnetfilter-queue-dev
|
||||||
libcrypto1.1 libnfnetlink-dev libmnl-dev make cmake nftables maturin\
|
RUN apt-get install -qq libssl-dev libnfnetlink-dev libmnl-dev libcap2-bin
|
||||||
boost-dev libcap autoconf automake bash rust cargo openssl-dev libffi-dev
|
RUN apt-get install -qq make cmake nftables libboost-all-dev autoconf
|
||||||
|
RUN apt-get install -qq automake cargo libffi-dev libvectorscan-dev libtins-dev
|
||||||
|
|
||||||
WORKDIR /tmp/
|
WORKDIR /tmp/
|
||||||
RUN git clone --single-branch --branch release https://github.com/jpcre2/jpcre2
|
RUN git clone --single-branch --branch release https://github.com/jpcre2/jpcre2
|
||||||
RUN git clone --single-branch https://github.com/mfontanini/libtins.git
|
|
||||||
WORKDIR /tmp/jpcre2
|
WORKDIR /tmp/jpcre2
|
||||||
RUN ./configure; make -j`nproc`; make install
|
RUN ./configure; make -j`nproc`; make install
|
||||||
WORKDIR /tmp/libtins
|
|
||||||
RUN mkdir build; cd build; cmake ../ -DLIBTINS_ENABLE_CXX11=1; make -j`nproc`; make install
|
|
||||||
|
|
||||||
RUN mkdir -p /execute/modules
|
RUN mkdir -p /execute/modules
|
||||||
WORKDIR /execute
|
WORKDIR /execute
|
||||||
|
|
||||||
ADD ./backend/requirements.txt /execute/requirements.txt
|
ADD ./backend/requirements.txt /execute/requirements.txt
|
||||||
RUN pip3 install --upgrade pip
|
RUN pip3 install --no-cache-dir --break-system-packages -r /execute/requirements.txt --no-warn-script-location
|
||||||
RUN pip3 install --no-cache-dir -r /execute/requirements.txt --no-warn-script-location
|
|
||||||
|
|
||||||
COPY ./backend/binsrc /execute/binsrc
|
COPY ./backend/binsrc /execute/binsrc
|
||||||
RUN g++ binsrc/nfqueue.cpp -o modules/cppqueue -O3 -lnetfilter_queue -pthread -lpcre2-8 -ltins -lmnl -lnfnetlink
|
RUN g++ binsrc/nfqueue.cpp -o modules/cppqueue -O3 -lnetfilter_queue -pthread -lpcre2-8 -ltins -lmnl -lnfnetlink
|
||||||
|
|||||||
223
backend/binsrc/nfqueue_regex/Cargo.lock
generated
Normal file
223
backend/binsrc/nfqueue_regex/Cargo.lock
generated
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.71"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cargo-emit"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1582e1c9e755dd6ad6b224dcffb135d199399a4568d454bd89fe515ca8425695"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "convert_case"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_more"
|
||||||
|
version = "0.99.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
|
||||||
|
dependencies = [
|
||||||
|
"convert_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"rustc_version",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foreign-types"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965"
|
||||||
|
dependencies = [
|
||||||
|
"foreign-types-macros",
|
||||||
|
"foreign-types-shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foreign-types-macros"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.18",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foreign-types-shared"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyperscan"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e76d5de507383407feda461def7d9019b8252bd00ce841de6b35b2d1c70a6c4e"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"bitflags",
|
||||||
|
"cfg-if",
|
||||||
|
"derive_more",
|
||||||
|
"foreign-types",
|
||||||
|
"hyperscan-sys",
|
||||||
|
"libc",
|
||||||
|
"malloc_buf",
|
||||||
|
"rustc_version",
|
||||||
|
"semver",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hyperscan-sys"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "011ea0ffa36b04c35f09780c3d963377321c1dae759f2bfda696deaa165d283e"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"cargo-emit",
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.145"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fc86cde3ff845662b8f4ef6cb50ea0e20c524eb3d29ae048287e06a1b3fa6a81"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "malloc_buf"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f32c8c0575eee8637bf462087c00098fe16d6cb621f1abb6ebab4da414d57fd"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nfqueue"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8684696e66ae4f74e6b2fe8868bf317bc4cd0e0e53aabddf7c40dad930111bca"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nfqueue_regex"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"hyperscan",
|
||||||
|
"nfqueue",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.59"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||||
|
dependencies = [
|
||||||
|
"semver",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semver"
|
||||||
|
version = "1.0.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.109"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.40"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.40"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.18",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||||
10
backend/binsrc/nfqueue_regex/Cargo.toml
Normal file
10
backend/binsrc/nfqueue_regex/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "nfqueue_regex"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
hyperscan = "0.3.2"
|
||||||
|
nfqueue = "0.9.1"
|
||||||
501
backend/binsrc/nfqueue_regex/src/main.rs
Normal file
501
backend/binsrc/nfqueue_regex/src/main.rs
Normal file
@@ -0,0 +1,501 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Hash, Eq, PartialEq, Debug)]
|
||||||
|
struct ConnectionFlux {
|
||||||
|
src_ip: String,
|
||||||
|
src_port: i16,
|
||||||
|
dst_ip: String,
|
||||||
|
dst_port: i16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnectionFlux{
|
||||||
|
fn new(src_ip: &str, src_port: i16, dst_ip: &str, dst_port: i16) -> ConnectionFlux {
|
||||||
|
ConnectionFlux { src_ip: src_ip.to_string(), src_port, dst_ip: dst_ip.to_string(), dst_port}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let str_of_threads = env::var("NTHREADS").unwrap_or("1".to_string());
|
||||||
|
let mut n_of_threads = str_of_threads.parse::<i32>().unwrap_or(1);
|
||||||
|
if n_of_threads <= 0 {
|
||||||
|
n_of_threads = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _connections = HashMap::from([
|
||||||
|
(ConnectionFlux::new("127.0.0.1", 1337, "127.0.0.1", 1337), 25),
|
||||||
|
]);
|
||||||
|
|
||||||
|
eprintln!("[info][main] Using {} threads", n_of_threads)
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
|
shared_ptr<regex_rules> regex_config;
|
||||||
|
|
||||||
|
void config_updater (){
|
||||||
|
string line;
|
||||||
|
while (true){
|
||||||
|
getline(cin, line);
|
||||||
|
if (cin.eof()){
|
||||||
|
cerr << "[fatal] [updater] cin.eof()" << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (cin.bad()){
|
||||||
|
cerr << "[fatal] [updater] cin.bad()" << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
cerr << "[info] [updater] Updating configuration with line " << line << endl;
|
||||||
|
istringstream config_stream(line);
|
||||||
|
regex_rules *regex_new_config = new regex_rules();
|
||||||
|
while(!config_stream.eof()){
|
||||||
|
string data;
|
||||||
|
config_stream >> data;
|
||||||
|
if (data != "" && data != "\n"){
|
||||||
|
regex_new_config->add(data.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
regex_config.reset(regex_new_config);
|
||||||
|
cerr << "[info] [updater] Config update done" << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool is_input>
|
||||||
|
bool filter_callback(const uint8_t *data, uint32_t len){
|
||||||
|
shared_ptr<regex_rules> current_config = regex_config;
|
||||||
|
return current_config->check((unsigned char *)data, len, is_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
regex_config.reset(new regex_rules());
|
||||||
|
NFQueueSequence<filter_callback<true>> input_queues(n_of_threads/2);
|
||||||
|
input_queues.start();
|
||||||
|
NFQueueSequence<filter_callback<false>> output_queues(n_of_threads/2);
|
||||||
|
output_queues.start();
|
||||||
|
|
||||||
|
cout << "QUEUES INPUT " << input_queues.init() << " " << input_queues.end() << " OUTPUT " << output_queues.init() << " " << output_queues.end() << endl;
|
||||||
|
cerr << "[info] [main] Input queues: " << input_queues.init() << ":" << input_queues.end() << " threads assigned: " << n_of_threads/2 << endl;
|
||||||
|
cerr << "[info] [main] Output queues: " << output_queues.init() << ":" << output_queues.end() << " threads assigned: " << n_of_threads/2 << endl;
|
||||||
|
|
||||||
|
config_updater();
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <linux/netfilter/nfnetlink_queue.h>
|
||||||
|
#include <libnetfilter_queue/libnetfilter_queue.h>
|
||||||
|
#include <linux/netfilter/nfnetlink_conntrack.h>
|
||||||
|
#include <tins/tins.h>
|
||||||
|
#include <libmnl/libmnl.h>
|
||||||
|
#include <linux/netfilter.h>
|
||||||
|
#include <linux/netfilter/nfnetlink.h>
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#ifndef NETFILTER_CLASSES_HPP
|
||||||
|
#define NETFILTER_CLASSES_HPP
|
||||||
|
|
||||||
|
typedef bool NetFilterQueueCallback(const uint8_t*,uint32_t);
|
||||||
|
|
||||||
|
Tins::PDU * find_transport_layer(Tins::PDU* pkt){
|
||||||
|
while(pkt != NULL){
|
||||||
|
if (pkt->pdu_type() == Tins::PDU::TCP || pkt->pdu_type() == Tins::PDU::UDP) {
|
||||||
|
return pkt;
|
||||||
|
}
|
||||||
|
pkt = pkt->inner_pdu();
|
||||||
|
}
|
||||||
|
return pkt;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <NetFilterQueueCallback callback_func>
|
||||||
|
class NetfilterQueue {
|
||||||
|
public:
|
||||||
|
size_t BUF_SIZE = 0xffff + (MNL_SOCKET_BUFFER_SIZE/2);
|
||||||
|
char *buf = NULL;
|
||||||
|
unsigned int portid;
|
||||||
|
u_int16_t queue_num;
|
||||||
|
struct mnl_socket* nl = NULL;
|
||||||
|
|
||||||
|
NetfilterQueue(u_int16_t queue_num): queue_num(queue_num) {
|
||||||
|
|
||||||
|
nl = mnl_socket_open(NETLINK_NETFILTER);
|
||||||
|
|
||||||
|
if (nl == NULL) { throw std::runtime_error( "mnl_socket_open" );}
|
||||||
|
|
||||||
|
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
|
||||||
|
mnl_socket_close(nl);
|
||||||
|
throw std::runtime_error( "mnl_socket_bind" );
|
||||||
|
}
|
||||||
|
portid = mnl_socket_get_portid(nl);
|
||||||
|
|
||||||
|
buf = (char*) malloc(BUF_SIZE);
|
||||||
|
|
||||||
|
if (!buf) {
|
||||||
|
mnl_socket_close(nl);
|
||||||
|
throw std::runtime_error( "allocate receive buffer" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (send_config_cmd(NFQNL_CFG_CMD_BIND) < 0) {
|
||||||
|
_clear();
|
||||||
|
throw std::runtime_error( "mnl_socket_send" );
|
||||||
|
}
|
||||||
|
//TEST if BIND was successful
|
||||||
|
if (send_config_cmd(NFQNL_CFG_CMD_NONE) < 0) { // SEND A NONE cmmand to generate an error meessage
|
||||||
|
_clear();
|
||||||
|
throw std::runtime_error( "mnl_socket_send" );
|
||||||
|
}
|
||||||
|
if (recv_packet() == -1) { //RECV the error message
|
||||||
|
_clear();
|
||||||
|
throw std::runtime_error( "mnl_socket_recvfrom" );
|
||||||
|
}
|
||||||
|
|
||||||
|
struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
|
||||||
|
|
||||||
|
if (nlh->nlmsg_type != NLMSG_ERROR) {
|
||||||
|
_clear();
|
||||||
|
throw std::runtime_error( "unexpected packet from kernel (expected NLMSG_ERROR packet)" );
|
||||||
|
}
|
||||||
|
//nfqnl_msg_config_cmd
|
||||||
|
nlmsgerr* error_msg = (nlmsgerr *)mnl_nlmsg_get_payload(nlh);
|
||||||
|
|
||||||
|
// error code taken from the linux kernel:
|
||||||
|
// https://elixir.bootlin.com/linux/v5.18.12/source/include/linux/errno.h#L27
|
||||||
|
#define ENOTSUPP 524 /* Operation is not supported */
|
||||||
|
|
||||||
|
if (error_msg->error != -ENOTSUPP) {
|
||||||
|
_clear();
|
||||||
|
throw std::invalid_argument( "queueid is already busy" );
|
||||||
|
}
|
||||||
|
|
||||||
|
//END TESTING BIND
|
||||||
|
nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
|
||||||
|
nfq_nlmsg_cfg_put_params(nlh, NFQNL_COPY_PACKET, 0xffff);
|
||||||
|
|
||||||
|
|
||||||
|
mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, htonl(NFQA_CFG_F_GSO));
|
||||||
|
mnl_attr_put_u32(nlh, NFQA_CFG_MASK, htonl(NFQA_CFG_F_GSO));
|
||||||
|
|
||||||
|
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
|
||||||
|
_clear();
|
||||||
|
throw std::runtime_error( "mnl_socket_send" );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void run(){
|
||||||
|
/*
|
||||||
|
* ENOBUFS is signalled to userspace when packets were lost
|
||||||
|
* on kernel side. In most cases, userspace isn't interested
|
||||||
|
* in this information, so turn it off.
|
||||||
|
*/
|
||||||
|
int ret = 1;
|
||||||
|
mnl_socket_setsockopt(nl, NETLINK_NO_ENOBUFS, &ret, sizeof(int));
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
ret = recv_packet();
|
||||||
|
if (ret == -1) {
|
||||||
|
throw std::runtime_error( "mnl_socket_recvfrom" );
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, nl);
|
||||||
|
if (ret < 0){
|
||||||
|
throw std::runtime_error( "mnl_cb_run" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~NetfilterQueue() {
|
||||||
|
send_config_cmd(NFQNL_CFG_CMD_UNBIND);
|
||||||
|
_clear();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
|
||||||
|
ssize_t send_config_cmd(nfqnl_msg_config_cmds cmd){
|
||||||
|
struct nlmsghdr *nlh = nfq_nlmsg_put(buf, NFQNL_MSG_CONFIG, queue_num);
|
||||||
|
nfq_nlmsg_cfg_put_cmd(nlh, AF_INET, cmd);
|
||||||
|
return mnl_socket_sendto(nl, nlh, nlh->nlmsg_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t recv_packet(){
|
||||||
|
return mnl_socket_recvfrom(nl, buf, BUF_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clear(){
|
||||||
|
if (buf != NULL) {
|
||||||
|
free(buf);
|
||||||
|
buf = NULL;
|
||||||
|
}
|
||||||
|
mnl_socket_close(nl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int queue_cb(const struct nlmsghdr *nlh, void *data)
|
||||||
|
{
|
||||||
|
struct mnl_socket* nl = (struct mnl_socket*)data;
|
||||||
|
//Extract attributes from the nlmsghdr
|
||||||
|
struct nlattr *attr[NFQA_MAX+1] = {};
|
||||||
|
|
||||||
|
if (nfq_nlmsg_parse(nlh, attr) < 0) {
|
||||||
|
perror("problems parsing");
|
||||||
|
return MNL_CB_ERROR;
|
||||||
|
}
|
||||||
|
if (attr[NFQA_PACKET_HDR] == NULL) {
|
||||||
|
fputs("metaheader not set\n", stderr);
|
||||||
|
return MNL_CB_ERROR;
|
||||||
|
}
|
||||||
|
//Get Payload
|
||||||
|
uint16_t plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
|
||||||
|
void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
|
||||||
|
|
||||||
|
//Return result to the kernel
|
||||||
|
struct nfqnl_msg_packet_hdr *ph = (nfqnl_msg_packet_hdr*) mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
|
||||||
|
struct nfgenmsg *nfg = (nfgenmsg *)mnl_nlmsg_get_payload(nlh);
|
||||||
|
char buf[MNL_SOCKET_BUFFER_SIZE];
|
||||||
|
struct nlmsghdr *nlh_verdict;
|
||||||
|
struct nlattr *nest;
|
||||||
|
|
||||||
|
nlh_verdict = nfq_nlmsg_put(buf, NFQNL_MSG_VERDICT, ntohs(nfg->res_id));
|
||||||
|
|
||||||
|
/*
|
||||||
|
This define allow to avoid to allocate new heap memory for each packet.
|
||||||
|
The code under this comment is replicated for ipv6 and ip
|
||||||
|
Better solutions are welcome. :)
|
||||||
|
*/
|
||||||
|
#define PKT_HANDLE \
|
||||||
|
Tins::PDU *transport_layer = find_transport_layer(&packet); \
|
||||||
|
if(transport_layer->inner_pdu() == nullptr || transport_layer == nullptr){ \
|
||||||
|
nfq_nlmsg_verdict_put(nlh_verdict, ntohl(ph->packet_id), NF_ACCEPT ); \
|
||||||
|
}else{ \
|
||||||
|
int size = transport_layer->inner_pdu()->size(); \
|
||||||
|
if(callback_func((const uint8_t*)payload+plen - size, size)){ \
|
||||||
|
nfq_nlmsg_verdict_put(nlh_verdict, ntohl(ph->packet_id), NF_ACCEPT ); \
|
||||||
|
} else{ \
|
||||||
|
if (transport_layer->pdu_type() == Tins::PDU::TCP){ \
|
||||||
|
((Tins::TCP *)transport_layer)->release_inner_pdu(); \
|
||||||
|
((Tins::TCP *)transport_layer)->set_flag(Tins::TCP::FIN,1); \
|
||||||
|
((Tins::TCP *)transport_layer)->set_flag(Tins::TCP::ACK,1); \
|
||||||
|
((Tins::TCP *)transport_layer)->set_flag(Tins::TCP::SYN,0); \
|
||||||
|
nfq_nlmsg_verdict_put_pkt(nlh_verdict, packet.serialize().data(), packet.size()); \
|
||||||
|
nfq_nlmsg_verdict_put(nlh_verdict, ntohl(ph->packet_id), NF_ACCEPT ); \
|
||||||
|
}else{ \
|
||||||
|
nfq_nlmsg_verdict_put(nlh_verdict, ntohl(ph->packet_id), NF_DROP ); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check IP protocol version
|
||||||
|
if ( (((uint8_t*)payload)[0] & 0xf0) == 0x40 ){
|
||||||
|
Tins::IP packet = Tins::IP((uint8_t*)payload,plen);
|
||||||
|
PKT_HANDLE
|
||||||
|
}else{
|
||||||
|
Tins::IPv6 packet = Tins::IPv6((uint8_t*)payload,plen);
|
||||||
|
PKT_HANDLE
|
||||||
|
}
|
||||||
|
|
||||||
|
/* example to set the connmark. First, start NFQA_CT section: */
|
||||||
|
nest = mnl_attr_nest_start(nlh_verdict, NFQA_CT);
|
||||||
|
|
||||||
|
/* then, add the connmark attribute: */
|
||||||
|
mnl_attr_put_u32(nlh_verdict, CTA_MARK, htonl(42));
|
||||||
|
/* more conntrack attributes, e.g. CTA_LABELS could be set here */
|
||||||
|
|
||||||
|
/* end conntrack section */
|
||||||
|
mnl_attr_nest_end(nlh_verdict, nest);
|
||||||
|
|
||||||
|
if (mnl_socket_sendto(nl, nlh_verdict, nlh_verdict->nlmsg_len) < 0) {
|
||||||
|
throw std::runtime_error( "mnl_socket_send" );
|
||||||
|
}
|
||||||
|
|
||||||
|
return MNL_CB_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <NetFilterQueueCallback func>
|
||||||
|
class NFQueueSequence{
|
||||||
|
private:
|
||||||
|
std::vector<NetfilterQueue<func> *> nfq;
|
||||||
|
uint16_t _init;
|
||||||
|
uint16_t _end;
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
public:
|
||||||
|
static const int QUEUE_BASE_NUM = 1000;
|
||||||
|
|
||||||
|
NFQueueSequence(uint16_t seq_len){
|
||||||
|
if (seq_len <= 0) throw std::invalid_argument("seq_len <= 0");
|
||||||
|
nfq = std::vector<NetfilterQueue<func>*>(seq_len);
|
||||||
|
_init = QUEUE_BASE_NUM;
|
||||||
|
while(nfq[0] == NULL){
|
||||||
|
if (_init+seq_len-1 >= 65536){
|
||||||
|
throw std::runtime_error("NFQueueSequence: too many queues!");
|
||||||
|
}
|
||||||
|
for (int i=0;i<seq_len;i++){
|
||||||
|
try{
|
||||||
|
nfq[i] = new NetfilterQueue<func>(_init+i);
|
||||||
|
}catch(const std::invalid_argument e){
|
||||||
|
for(int j = 0; j < i; j++) {
|
||||||
|
delete nfq[j];
|
||||||
|
nfq[j] = nullptr;
|
||||||
|
}
|
||||||
|
_init += seq_len - i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_end = _init + seq_len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start(){
|
||||||
|
if (threads.size() != 0) throw std::runtime_error("NFQueueSequence: already started!");
|
||||||
|
for (int i=0;i<nfq.size();i++){
|
||||||
|
threads.push_back(std::thread(&NetfilterQueue<func>::run, nfq[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void join(){
|
||||||
|
for (int i=0;i<nfq.size();i++){
|
||||||
|
threads[i].join();
|
||||||
|
}
|
||||||
|
threads.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t init(){
|
||||||
|
return _init;
|
||||||
|
}
|
||||||
|
uint16_t end(){
|
||||||
|
return _end;
|
||||||
|
}
|
||||||
|
|
||||||
|
~NFQueueSequence(){
|
||||||
|
for (int i=0;i<nfq.size();i++){
|
||||||
|
delete nfq[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // NETFILTER_CLASSES_HPP
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
#include <jpcre2.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include "../utils.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef REGEX_FILTER_HPP
|
||||||
|
#define REGEX_FILTER_HPP
|
||||||
|
|
||||||
|
typedef jpcre2::select<char> jp;
|
||||||
|
typedef std::pair<std::string,jp::Regex> regex_rule_pair;
|
||||||
|
typedef std::vector<regex_rule_pair> regex_rule_vector;
|
||||||
|
struct regex_rules{
|
||||||
|
regex_rule_vector output_whitelist, input_whitelist, output_blacklist, input_blacklist;
|
||||||
|
|
||||||
|
regex_rule_vector* getByCode(char code){
|
||||||
|
switch(code){
|
||||||
|
case 'C': // Client to server Blacklist
|
||||||
|
return &input_blacklist; break;
|
||||||
|
case 'c': // Client to server Whitelist
|
||||||
|
return &input_whitelist; break;
|
||||||
|
case 'S': // Server to client Blacklist
|
||||||
|
return &output_blacklist; break;
|
||||||
|
case 's': // Server to client Whitelist
|
||||||
|
return &output_whitelist; break;
|
||||||
|
}
|
||||||
|
throw std::invalid_argument( "Expected 'C' 'c' 'S' or 's'" );
|
||||||
|
}
|
||||||
|
|
||||||
|
int add(const char* arg){
|
||||||
|
//Integrity checks
|
||||||
|
size_t arg_len = strlen(arg);
|
||||||
|
if (arg_len < 2 || arg_len%2 != 0){
|
||||||
|
std::cerr << "[warning] [regex_rules.add] invalid arg passed (" << arg << "), skipping..." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (arg[0] != '0' && arg[0] != '1'){
|
||||||
|
std::cerr << "[warning] [regex_rules.add] invalid is_case_sensitive (" << arg[0] << ") in '" << arg << "', must be '1' or '0', skipping..." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (arg[1] != 'C' && arg[1] != 'c' && arg[1] != 'S' && arg[1] != 's'){
|
||||||
|
std::cerr << "[warning] [regex_rules.add] invalid filter_type (" << arg[1] << ") in '" << arg << "', must be 'C', 'c', 'S' or 's', skipping..." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
std::string hex(arg+2), expr;
|
||||||
|
if (!unhexlify(hex, expr)){
|
||||||
|
std::cerr << "[warning] [regex_rules.add] invalid hex regex value (" << hex << "), skipping..." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//Push regex
|
||||||
|
jp::Regex regex(expr,arg[0] == '1'?"gS":"giS");
|
||||||
|
if (regex){
|
||||||
|
std::cerr << "[info] [regex_rules.add] adding new regex filter: '" << expr << "'" << std::endl;
|
||||||
|
getByCode(arg[1])->push_back(std::make_pair(std::string(arg), regex));
|
||||||
|
} else {
|
||||||
|
std::cerr << "[warning] [regex_rules.add] compiling of '" << expr << "' regex failed, skipping..." << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check(unsigned char* data, const size_t& bytes_transferred, const bool in_input){
|
||||||
|
std::string str_data((char *) data, bytes_transferred);
|
||||||
|
for (regex_rule_pair ele:(in_input?input_blacklist:output_blacklist)){
|
||||||
|
try{
|
||||||
|
if(ele.second.match(str_data)){
|
||||||
|
std::stringstream msg;
|
||||||
|
msg << "BLOCKED " << ele.first << "\n";
|
||||||
|
std::cout << msg.str() << std::flush;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch(...){
|
||||||
|
std::cerr << "[info] [regex_rules.check] Error while matching blacklist regex: " << ele.first << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (regex_rule_pair ele:(in_input?input_whitelist:output_whitelist)){
|
||||||
|
try{
|
||||||
|
std::cerr << "[debug] [regex_rules.check] regex whitelist match " << ele.second.getPattern() << std::endl;
|
||||||
|
if(!ele.second.match(str_data)){
|
||||||
|
std::stringstream msg;
|
||||||
|
msg << "BLOCKED " << ele.first << "\n";
|
||||||
|
std::cout << msg.str() << std::flush;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch(...){
|
||||||
|
std::cerr << "[info] [regex_rules.check] Error while matching whitelist regex: " << ele.first << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // REGEX_FILTER_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifndef UTILS_HPP
|
||||||
|
#define UTILS_HPP
|
||||||
|
|
||||||
|
bool unhexlify(std::string const &hex, std::string &newString) {
|
||||||
|
try{
|
||||||
|
int len = hex.length();
|
||||||
|
for(int i=0; i< len; i+=2)
|
||||||
|
{
|
||||||
|
std::string byte = hex.substr(i,2);
|
||||||
|
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
|
||||||
|
newString.push_back(chr);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (...){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
chown nobody:nobody -R /execute/
|
chown nobody -R /execute/
|
||||||
|
|
||||||
exec capsh --caps="cap_net_admin+eip cap_setpcap,cap_setuid,cap_setgid+ep" \
|
exec capsh --caps="cap_net_admin+eip cap_setpcap,cap_setuid,cap_setgid+ep" \
|
||||||
--keep=1 --user=nobody --addamb=cap_net_admin -- -c "python3 /execute/app.py DOCKER"
|
--keep=1 --user=nobody --addamb=cap_net_admin -- -c "python3 /execute/app.py DOCKER"
|
||||||
|
|||||||
15938
frontend/package-lock.json
generated
15938
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
9920
frontend/yarn.lock
Normal file
9920
frontend/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user