Port hijack changes and start writing frontend
This commit is contained in:
@@ -3,8 +3,9 @@ import sqlite3
|
||||
from typing import List, Union
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from modules.porthijack.models import Service
|
||||
from utils.sqlite import SQLite
|
||||
from utils import ip_parse, refactor_name, refresh_frontend
|
||||
from utils import addr_parse, ip_family, refactor_name, refresh_frontend
|
||||
from utils.models import ResetRequest, StatusMessageModel
|
||||
from modules.porthijack.nftables import FiregexTables
|
||||
from modules.porthijack.firewall import FirewallManager
|
||||
@@ -16,7 +17,8 @@ class ServiceModel(BaseModel):
|
||||
proxy_port: int
|
||||
name: str
|
||||
proto: str
|
||||
ip_int: str
|
||||
ip_src: str
|
||||
ip_dst: str
|
||||
|
||||
class RenameForm(BaseModel):
|
||||
name:str
|
||||
@@ -26,7 +28,8 @@ class ServiceAddForm(BaseModel):
|
||||
public_port: int
|
||||
proxy_port: int
|
||||
proto: str
|
||||
ip_int: str
|
||||
ip_src: str
|
||||
ip_dst: str
|
||||
|
||||
class ServiceAddResponse(BaseModel):
|
||||
status:str
|
||||
@@ -41,15 +44,15 @@ db = SQLite('db/port-hijacking.db', {
|
||||
'services': {
|
||||
'service_id': 'VARCHAR(100) PRIMARY KEY',
|
||||
'active' : 'BOOLEAN NOT NULL CHECK (active IN (0, 1))',
|
||||
'public_port': 'INT NOT NULL CHECK(public_port > 0 and public_port < 65536) UNIQUE',
|
||||
'public_port': 'INT NOT NULL CHECK(public_port > 0 and public_port < 65536)',
|
||||
'proxy_port': 'INT NOT NULL CHECK(proxy_port > 0 and proxy_port < 65536 and proxy_port != public_port)',
|
||||
'name': 'VARCHAR(100) NOT NULL UNIQUE',
|
||||
'proto': 'VARCHAR(3) NOT NULL CHECK (proto IN ("tcp", "udp"))',
|
||||
'ip_int': 'VARCHAR(100) NOT NULL',
|
||||
'ip_src': 'VARCHAR(100) NOT NULL',
|
||||
'ip_dst': 'VARCHAR(100) NOT NULL',
|
||||
},
|
||||
'QUERY':[
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS unique_services ON services (public_port, ip_int, proto);",
|
||||
""
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS unique_services ON services (public_port, ip_src, proto);"
|
||||
]
|
||||
})
|
||||
|
||||
@@ -96,12 +99,12 @@ async def get_general_stats():
|
||||
@app.get('/services', response_model=List[ServiceModel])
|
||||
async def get_service_list():
|
||||
"""Get the list of existent firegex services"""
|
||||
return db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services;")
|
||||
return db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_src, ip_dst FROM services;")
|
||||
|
||||
@app.get('/service/{service_id}', response_model=ServiceModel)
|
||||
async def get_service_by_id(service_id: str):
|
||||
"""Get info about a specific service using his id"""
|
||||
res = db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_int FROM services WHERE service_id = ?;", service_id)
|
||||
res = db.query("SELECT service_id, active, public_port, proxy_port, name, proto, ip_src, ip_dst FROM services WHERE service_id = ?;", service_id)
|
||||
if len(res) == 0: raise HTTPException(status_code=400, detail="This service does not exists!")
|
||||
return res[0]
|
||||
|
||||
@@ -139,17 +142,30 @@ async def service_rename(service_id: str, form: RenameForm):
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok'}
|
||||
|
||||
class ChangePortRequest(BaseModel):
|
||||
class ChangeDestination(BaseModel):
|
||||
ip_dst: str
|
||||
proxy_port: int
|
||||
|
||||
@app.post('/service/{service_id}/changeport', response_model=StatusMessageModel)
|
||||
async def service_changeport(service_id: str, form: ChangePortRequest):
|
||||
"""Request to change the proxy port of a specific service"""
|
||||
@app.post('/service/{service_id}/change-destination', response_model=StatusMessageModel)
|
||||
async def service_change_destination(service_id: str, form: ChangeDestination):
|
||||
"""Request to change the proxy destination of the service"""
|
||||
|
||||
try:
|
||||
db.query('UPDATE services SET proxy_port=? WHERE service_id = ?;', form.proxy_port, service_id)
|
||||
form.ip_dst = addr_parse(form.ip_dst)
|
||||
except ValueError:
|
||||
return {"status":"Invalid address"}
|
||||
srv = Service.from_dict(db.query('SELECT * FROM services WHERE service_id = ?;', service_id)[0])
|
||||
if ip_family(form.ip_dst) != ip_family(srv.ip_src):
|
||||
return {'status': 'The destination ip is not of the same family as the source ip'}
|
||||
try:
|
||||
db.query('UPDATE services SET proxy_port=?, ip_dst=? WHERE service_id = ?;', form.proxy_port, form.ip_dst, service_id)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'Invalid proxy port or service'}
|
||||
await firewall.get(service_id).change_port(form.proxy_port)
|
||||
|
||||
srv.ip_dst = form.ip_dst
|
||||
srv.proxy_port = form.proxy_port
|
||||
await firewall.get(service_id).refresh(srv)
|
||||
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok'}
|
||||
|
||||
@@ -157,18 +173,24 @@ async def service_changeport(service_id: str, form: ChangePortRequest):
|
||||
async def add_new_service(form: ServiceAddForm):
|
||||
"""Add a new service"""
|
||||
try:
|
||||
form.ip_int = ip_parse(form.ip_int)
|
||||
form.ip_src = addr_parse(form.ip_src)
|
||||
form.ip_dst = addr_parse(form.ip_dst)
|
||||
except ValueError:
|
||||
return {"status":"Invalid address"}
|
||||
|
||||
if ip_family(form.ip_dst) != ip_family(form.ip_src):
|
||||
return {"status":"Destination and source addresses must be of the same family"}
|
||||
if form.proto not in ["tcp", "udp"]:
|
||||
return {"status":"Invalid protocol"}
|
||||
|
||||
srv_id = None
|
||||
try:
|
||||
srv_id = gen_service_id()
|
||||
db.query("INSERT INTO services (service_id, active, public_port, proxy_port, name, proto, ip_int) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
srv_id, False, form.public_port, form.proxy_port , form.name, form.proto, form.ip_int)
|
||||
db.query("INSERT INTO services (service_id, active, public_port, proxy_port, name, proto, ip_src, ip_dst) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
srv_id, False, form.public_port, form.proxy_port , form.name, form.proto, form.ip_src, form.ip_dst)
|
||||
except sqlite3.IntegrityError:
|
||||
return {'status': 'This type of service already exists'}
|
||||
|
||||
await firewall.reload()
|
||||
await refresh_frontend()
|
||||
return {'status': 'ok', 'service_id': srv_id}
|
||||
|
||||
Reference in New Issue
Block a user