This commit is contained in:
Darryl Nixon 2023-07-03 00:43:49 -07:00
parent 1e9e4fdc4c
commit 2de8736d24
4 changed files with 37 additions and 21 deletions

View file

@ -3,13 +3,12 @@
# drawbridge # drawbridge
drawbridge description **drawbridge** simplifies local nfqueue queues
and witty subtitle<br/> without sacrificing performance?<br/>
[Installation](#installation) • [Installation](#installation) •
[Examples](#examples) • [Examples](#examples) •
[Frequently Asked Questions](#faq) •
[Contributing](#contributing) • [Contributing](#contributing) •
[License](#license) [License](#license)
</div> </div>
@ -17,21 +16,29 @@ and witty subtitle<br/>
## Installation ## Installation
### with pip ### with pip
TBD Eventually, install with `pip install drawbridge`, maybe.
For now, clone the repo, navigate to it, and run `pip install .`. You'll need a Linux system for nfqueue.
## Examples ## Examples
TBD See the examples directory for a WebSocket example.
## FAQ ```python
from drawbridge import DrawBridge
**What problem does drawbridge solve?** def my_packet_handler(raw_packet):
# do things to the raw packet, like
# from scapy.all import *
# pkt = IP(raw_packet)
# ...
# return bytes(pkt)
return raw_packet
TBD db = DrawBridge()
db.add_queue(my_packet_handler, src_port=80)
**What's your roadmap?** db.run()
```
TBD
## Contributing ## Contributing

View file

@ -22,7 +22,7 @@ class DrawBridge:
dst_ip: Optional[str] = None, dst_ip: Optional[str] = None,
src_port: Optional[int] = None, src_port: Optional[int] = None,
dst_port: Optional[int] = None, dst_port: Optional[int] = None,
protocol: Optional[str] = "", protocol: Optional[str] = "tcp",
override: bool = False, override: bool = False,
): ):
try: try:

View file

@ -8,7 +8,7 @@ from typing import Union
import iptc import iptc
from .utils.logger import logger from .utils.logger import logger
from .utils.lookup import PROTOCOLS, ALL_TABLES, PREROUTING_MANGLE from .utils.lookup import PROTOCOLS, ALL_TABLES, OUTGOING_MANGLE
class NetQueue: class NetQueue:
@ -20,7 +20,7 @@ class NetQueue:
dst_ip: Optional[str] = None, dst_ip: Optional[str] = None,
src_port: Optional[int] = None, src_port: Optional[int] = None,
dst_port: Optional[int] = None, dst_port: Optional[int] = None,
protocol: Optional[str] = "", protocol: Optional[str] = "tcp",
override: bool = False, override: bool = False,
): ):
self.callback = self.validate_callable(callback) self.callback = self.validate_callable(callback)
@ -37,18 +37,26 @@ class NetQueue:
rule = iptc.Rule() rule = iptc.Rule()
target = iptc.Target(rule, "NFQUEUE") target = iptc.Target(rule, "NFQUEUE")
target.set_parameter("queue-num", str(self.queue)) target.set_parameter("queue-num", str(self.queue))
if self.protocol: rule.protocol = self.protocol
match = iptc.Match(rule, self.protocol) match = rule.create_match(self.protocol)
rule.add_match(match) if self.dst_port:
match.dport = str(self.dst_port)
if self.src_port:
match.sport = str(self.src_port)
match = iptc.Match(rule, "iprange")
if self.src_ip:
match.src_range = str(self.src_ip)
if self.dst_ip:
match.dst_range = str(self.dst_ip)
rule.target = target rule.target = target
return rule return rule
def write_rule(self): def write_rule(self):
PREROUTING_MANGLE.insert_rule(self.rule) OUTGOING_MANGLE.insert_rule(self.rule)
def delete_rule(self): def delete_rule(self):
try: try:
PREROUTING_MANGLE.delete_rule(self.rule) OUTGOING_MANGLE.delete_rule(self.rule)
except iptc.ip4tc.IPTCError: except iptc.ip4tc.IPTCError:
logger.warning("Failed to delete rule, it may have already been deleted") logger.warning("Failed to delete rule, it may have already been deleted")

View file

@ -30,3 +30,4 @@ PROTOCOLS = {
ALL_TABLES = [iptc.Table(t) for t in iptc.Table.ALL] ALL_TABLES = [iptc.Table(t) for t in iptc.Table.ALL]
PREROUTING_MANGLE = iptc.Chain(iptc.Table(iptc.Table.MANGLE), "PREROUTING") PREROUTING_MANGLE = iptc.Chain(iptc.Table(iptc.Table.MANGLE), "PREROUTING")
OUTGOING_MANGLE = iptc.Chain(iptc.Table(iptc.Table.FILTER), "OUTPUT")