Frontend Changes

This commit is contained in:
DomySh
2022-06-22 01:31:46 +02:00
parent ede11873c5
commit 34d6b18ec0
10 changed files with 69 additions and 65 deletions

View File

@@ -295,11 +295,8 @@ def post_regexes_add():
"is_case_sensitive" : {"type" : "boolean"} "is_case_sensitive" : {"type" : "boolean"}
}, },
}) })
if not re.match("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$",req["regex"]):
return abort(400)
except Exception: except Exception:
return abort(400) return abort(400)
try: try:
re.compile(b64decode(req["regex"])) re.compile(b64decode(req["regex"]))
except Exception: except Exception:

View File

@@ -39,10 +39,10 @@ unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
return 0; return 0;
} }
vector<pair<string,std::regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b; vector<pair<string,regex>> regex_s_c_w, regex_c_s_w, regex_s_c_b, regex_c_s_b;
const char* config_file; const char* config_file;
bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,std::regex>> const &blacklist, vector<pair<string,std::regex>> const &whitelist){ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pair<string,regex>> const &blacklist, vector<pair<string,regex>> const &whitelist){
#ifdef DEBUG #ifdef DEBUG
cout << "---------------- Packet ----------------" << endl; cout << "---------------- Packet ----------------" << endl;
for(int i=0;i<bytes_transferred;i++){ for(int i=0;i<bytes_transferred;i++){
@@ -50,32 +50,28 @@ bool filter_data(unsigned char* data, const size_t& bytes_transferred, vector<pa
} }
cout << "\n" << "---------------- End Packet ----------------" << endl; cout << "\n" << "---------------- End Packet ----------------" << endl;
#endif #endif
for (pair<string,std::regex> ele:blacklist){ for (pair<string,regex> ele:blacklist){
std::cmatch what; cmatch what;
try{ try{
std::regex_search(reinterpret_cast<const char*>(data), what, ele.second); regex_search(reinterpret_cast<const char*>(data), what, ele.second);
if(what.size() > 0){ if(what.size() > 0){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; return false;
} }
} catch(...){ } catch(...){
#ifdef DEBUG
cerr << "Error while matching regex: " << ele.first << endl; cerr << "Error while matching regex: " << ele.first << endl;
#endif
} }
} }
for (pair<string,std::regex> ele:whitelist){ for (pair<string,regex> ele:whitelist){
std::cmatch what; cmatch what;
try{ try{
std::regex_search(reinterpret_cast<const char*>(data), what, ele.second); regex_search(reinterpret_cast<const char*>(data), what, ele.second);
if(what.size() < 0){ if(what.size() < 0){
cout << "BLOCKED " << ele.first << endl; cout << "BLOCKED " << ele.first << endl;
return false; return false;
} }
} catch(...){ } catch(...){
#ifdef DEBUG
cerr << "Error while matching regex: " << ele.first << endl; cerr << "Error while matching regex: " << ele.first << endl;
#endif
} }
} }
#ifdef DEBUG #ifdef DEBUG
@@ -112,7 +108,7 @@ namespace tcp_proxy
return upstream_socket_; return upstream_socket_;
} }
void start(const std::string& upstream_host, unsigned short upstream_port) void start(const string& upstream_host, unsigned short upstream_port)
{ {
// Attempt connection to remote server (upstream side) // Attempt connection to remote server (upstream side)
upstream_socket_.async_connect( upstream_socket_.async_connect(
@@ -266,8 +262,8 @@ namespace tcp_proxy
public: public:
acceptor(boost::asio::io_service& io_service, acceptor(boost::asio::io_service& io_service,
const std::string& local_host, unsigned short local_port, const string& local_host, unsigned short local_port,
const std::string& upstream_host, unsigned short upstream_port) const string& upstream_host, unsigned short upstream_port)
: io_service_(io_service), : io_service_(io_service),
localhost_address(boost::asio::ip::address_v4::from_string(local_host)), localhost_address(boost::asio::ip::address_v4::from_string(local_host)),
acceptor_(io_service_,ip::tcp::endpoint(localhost_address,local_port)), acceptor_(io_service_,ip::tcp::endpoint(localhost_address,local_port)),
@@ -286,9 +282,9 @@ namespace tcp_proxy
this, this,
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
catch(std::exception& e) catch(exception& e)
{ {
std::cerr << "acceptor exception: " << e.what() << std::endl; cerr << "acceptor exception: " << e.what() << endl;
return false; return false;
} }
@@ -305,12 +301,12 @@ namespace tcp_proxy
if (!accept_connections()) if (!accept_connections())
{ {
std::cerr << "Failure during call to accept." << std::endl; cerr << "Failure during call to accept." << endl;
} }
} }
else else
{ {
std::cerr << "Error: " << error.message() << std::endl; cerr << "Error: " << error.message() << endl;
} }
} }
@@ -319,33 +315,33 @@ namespace tcp_proxy
ip::tcp::acceptor acceptor_; ip::tcp::acceptor acceptor_;
ptr_type session_; ptr_type session_;
unsigned short upstream_port_; unsigned short upstream_port_;
std::string upstream_host_; string upstream_host_;
}; };
}; };
} }
void push_regex(char* arg, bool case_sensitive, vector<pair<string,std::regex>> &v){ void push_regex(char* arg, bool case_sensitive, vector<pair<string,regex>> &v){
size_t expr_len = (strlen(arg)-2)/2; size_t expr_len = (strlen(arg)-2)/2;
char expr[expr_len]; char expr[expr_len];
unhexlify(arg+2, arg+strlen(arg)-1, expr); unhexlify(arg+2, arg+strlen(arg)-1, expr);
string expr_str(expr, expr_len); string expr_str(expr, expr_len);
try{ try{
if (case_sensitive){ if (case_sensitive){
std::regex regex(expr_str); regex regex(expr_str);
#ifdef DEBUG #ifdef DEBUG
cout << "Added case sensitive regex " << expr_str << endl; cout << "Added case sensitive regex " << expr_str << endl;
#endif #endif
v.push_back(make_pair(string(arg), regex)); v.push_back(make_pair(string(arg), regex));
} else { } else {
std::regex regex(expr_str,std::regex_constants::icase); regex regex(expr_str,regex_constants::icase);
#ifdef DEBUG #ifdef DEBUG
cout << "Added case insensitive regex " << expr_str << endl; cout << "Added case insensitive regex " << expr_str << endl;
#endif #endif
v.push_back(make_pair(string(arg), regex)); v.push_back(make_pair(string(arg), regex));
} }
} catch(...){ } catch(...){
std::cerr << "Regex " << expr << " was not compiled successfully" << std::endl; cerr << "Regex " << expr << " was not compiled successfully" << endl;
} }
} }
@@ -354,7 +350,7 @@ void update_regex(){
fstream fd; fstream fd;
fd.open(config_file,ios::in); fd.open(config_file,ios::in);
if (!fd.is_open()){ if (!fd.is_open()){
std::cerr << "Error: config file couln't be opened" << std::endl; cerr << "Error: config file couln't be opened" << endl;
exit(1); exit(1);
} }
@@ -413,14 +409,14 @@ int main(int argc, char* argv[])
{ {
if (argc < 6) if (argc < 6)
{ {
std::cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> <config_file>" << std::endl; cerr << "usage: tcpproxy_server <local host ip> <local port> <forward host ip> <forward port> <config_file>" << endl;
return 1; return 1;
} }
const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2])); const unsigned short local_port = static_cast<unsigned short>(::atoi(argv[2]));
const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4])); const unsigned short forward_port = static_cast<unsigned short>(::atoi(argv[4]));
const std::string local_host = argv[1]; const string local_host = argv[1];
const std::string forward_host = argv[3]; const string forward_host = argv[3];
config_file = argv[5]; config_file = argv[5];
@@ -445,9 +441,9 @@ int main(int argc, char* argv[])
ios.run(); ios.run();
} }
catch(std::exception& e) catch(exception& e)
{ {
std::cerr << "Error: " << e.what() << std::endl; cerr << "Error: " << e.what() << endl;
return 1; return 1;
} }

View File

@@ -1,13 +1,13 @@
{ {
"files": { "files": {
"main.css": "/static/css/main.c375ae17.css", "main.css": "/static/css/main.c375ae17.css",
"main.js": "/static/js/main.3ae3ab4b.js", "main.js": "/static/js/main.76d17c5c.js",
"index.html": "/index.html", "index.html": "/index.html",
"main.c375ae17.css.map": "/static/css/main.c375ae17.css.map", "main.c375ae17.css.map": "/static/css/main.c375ae17.css.map",
"main.3ae3ab4b.js.map": "/static/js/main.3ae3ab4b.js.map" "main.76d17c5c.js.map": "/static/js/main.76d17c5c.js.map"
}, },
"entrypoints": [ "entrypoints": [
"static/css/main.c375ae17.css", "static/css/main.c375ae17.css",
"static/js/main.3ae3ab4b.js" "static/js/main.76d17c5c.js"
] ]
} }

View File

@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="manifest" href="/site.webmanifest"><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#FFFFFFFF"/><meta name="description" content="Firegex by Pwnzer0tt1"/><title>Firegex</title><script defer="defer" src="/static/js/main.3ae3ab4b.js"></script><link href="/static/css/main.c375ae17.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="manifest" href="/site.webmanifest"><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#FFFFFFFF"/><meta name="description" content="Firegex by Pwnzer0tt1"/><title>Firegex</title><script defer="defer" src="/static/js/main.76d17c5c.js"></script><link href="/static/css/main.c375ae17.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

View File

@@ -2,7 +2,7 @@ import { Button, Group, Space, TextInput, Notification, Switch, NativeSelect, To
import { useForm } from '@mantine/hooks'; import { useForm } from '@mantine/hooks';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { RegexAddForm } from '../js/models'; import { RegexAddForm } from '../js/models';
import { addregex, b64encode, fireUpdateRequest, getHumanReadableRegex, okNotify } from '../js/utils'; import { addregex, b64encode, fireUpdateRequest, getBinaryRegex, getHumanReadableRegex, okNotify } from '../js/utils';
import { ImCross } from "react-icons/im" import { ImCross } from "react-icons/im"
import FilterTypeSelector from './FilterTypeSelector'; import FilterTypeSelector from './FilterTypeSelector';
@@ -12,7 +12,6 @@ type RegexAddInfo = {
type:string, type:string,
mode:string, mode:string,
is_case_insensitive:boolean, is_case_insensitive:boolean,
regex_exact:boolean,
percentage_encoding:boolean percentage_encoding:boolean
} }
@@ -24,7 +23,6 @@ function AddNewRegex({ opened, onClose, service }:{ opened:boolean, onClose:()=>
type:"blacklist", type:"blacklist",
mode:"C <-> S", mode:"C <-> S",
is_case_insensitive:false, is_case_insensitive:false,
regex_exact:false,
percentage_encoding:false percentage_encoding:false
}, },
validationRules:{ validationRules:{
@@ -47,12 +45,9 @@ function AddNewRegex({ opened, onClose, service }:{ opened:boolean, onClose:()=>
setSubmitLoading(true) setSubmitLoading(true)
const filter_mode = ({'C -> S':'C', 'S -> C':'S', 'C <-> S':'B'}[values.mode]) const filter_mode = ({'C -> S':'C', 'S -> C':'S', 'C <-> S':'B'}[values.mode])
let final_regex = values.regex let final_regex:string|number[] = values.regex
if (values.percentage_encoding){ if (values.percentage_encoding){
final_regex = decodeURIComponent(final_regex) final_regex = getBinaryRegex(final_regex)
}
if(!values.regex_exact){
final_regex = ".*"+final_regex+".*"
} }
const request:RegexAddForm = { const request:RegexAddForm = {
@@ -105,11 +100,6 @@ function AddNewRegex({ opened, onClose, service }:{ opened:boolean, onClose:()=>
{...form.getInputProps('is_case_insensitive', { type: 'checkbox' })} {...form.getInputProps('is_case_insensitive', { type: 'checkbox' })}
/> />
<Space h="md" /> <Space h="md" />
<Switch
label="Match exactly the regex"
{...form.getInputProps('regex_exact', { type: 'checkbox' })}
/>
<Space h="md" />
<NativeSelect <NativeSelect
data={['C -> S', 'S -> C', 'C <-> S']} data={['C -> S', 'S -> C', 'C <-> S']}
label="Choose the source of the packets to filter" label="Choose the source of the packets to filter"

View File

@@ -15,12 +15,6 @@ function RegexView({ regexInfo }:{ regexInfo:RegexFilter }) {
regexInfo.mode === "B"? "S <-> C": "🤔" regexInfo.mode === "B"? "S <-> C": "🤔"
let regex_expr = getHumanReadableRegex(regexInfo.regex); let regex_expr = getHumanReadableRegex(regexInfo.regex);
let exact_regex = true;
if (regex_expr.length>=4 && regex_expr.startsWith(".*") && regex_expr.endsWith(".*")){
regex_expr = regex_expr.substring(2,regex_expr.length-2)
exact_regex = false;
}
const [deleteModal, setDeleteModal] = useState(false); const [deleteModal, setDeleteModal] = useState(false);
const [tooltipOpened, setTooltipOpened] = useState(false); const [tooltipOpened, setTooltipOpened] = useState(false);
@@ -72,9 +66,7 @@ function RegexView({ regexInfo }:{ regexInfo:RegexFilter }) {
<Grid.Col style={{width:"100%"}} span={6}> <Grid.Col style={{width:"100%"}} span={6}>
<Space h="xs" /> <Space h="xs" />
<div className='center-flex-row'> <div className='center-flex-row'>
<Badge size="md" color={exact_regex?"grape":"pink"} variant="filled">Match: {exact_regex?"EXACT":"FIND"}</Badge> <Badge size="md" color={regexInfo.is_case_sensitive?"grape":"pink"} variant="filled">Case: {regexInfo.is_case_sensitive?"SENSIIVE":"INSENSITIVE"}</Badge>
<Space h="xs" />
<Badge size="md" color={regexInfo.is_case_sensitive?"red":"green"} variant="filled">Case: {regexInfo.is_case_sensitive?"SENSIIVE":"INSENSITIVE"}</Badge>
<Space h="xs" /> <Space h="xs" />
<Badge size="md" color="yellow" variant="filled">Packets filtered: {regexInfo.n_packets}</Badge> <Badge size="md" color="yellow" variant="filled">Packets filtered: {regexInfo.n_packets}</Badge>
<Space h="xs" /> <Space h="xs" />

View File

@@ -137,7 +137,36 @@ export function getHumanReadableRegex(regexB64:string){
if (unescapedChars.includes(byte)){ if (unescapedChars.includes(byte)){
res+=byte res+=byte
}else{ }else{
res+="%"+regex[i].toString(16) let hex_data = regex[i].toString(16)
if (hex_data.length === 1) hex_data = "0"+hex_data
res+="%"+hex_data
}
}
return res
}
const hexChars = "0123456789abcdefABCDEF"
export function getBinaryRegex(regexPercentageEncoded:string):number[]{
const regex = Buffer.from(regexPercentageEncoded)
let res = []
for (let i=0; i < regex.length; i++){
const byte = String.fromCharCode(regex[i]);
if ("%" === byte){
if(i+2 < regex.length){
const byte_1 = String.fromCharCode(regex[i+1]);
const byte_2 = String.fromCharCode(regex[i+2]);
if(hexChars.includes(byte_1) && hexChars.includes(byte_2)){
res.push(parseInt(byte_1+byte_2,16))
i += 2
}else{
res.push(regex[i])
}
}else{
res.push(regex[i])
}
}else{
res.push(regex[i])
} }
} }
return res return res
@@ -163,6 +192,6 @@ export function okNotify(title:string, description:string ){
}); });
} }
export function b64encode(data:string){ export function b64encode(data:number[]|string){
return Buffer.from(data).toString('base64') return Buffer.from(data).toString('base64')
} }