-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 62 restrict interfaces #77
base: master
Are you sure you want to change the base?
Changes from 6 commits
473e12b
4eba1d4
cf35fd0
3c3dd34
9f498d9
e4d2845
fd3d9bc
14f931f
c3198f6
4151e8b
2947385
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1075,6 +1075,17 @@ def parse_diverter_config(self): | |||||||||||||||||||||||||||||||||||||
self.logger.debug('Blacklisted UDP ports: %s', ', '.join( | ||||||||||||||||||||||||||||||||||||||
[str(p) for p in self.getconfigval('BlackListPortsUDP')])) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# Ignore or drop packets to/from blacklisted interfaces | ||||||||||||||||||||||||||||||||||||||
# Currently Linux-only | ||||||||||||||||||||||||||||||||||||||
self.blacklist_ifaces = None | ||||||||||||||||||||||||||||||||||||||
if self.is_set('linuxblacklistinterfaces'): | ||||||||||||||||||||||||||||||||||||||
self.blacklist_ifaces_disp = ( | ||||||||||||||||||||||||||||||||||||||
self.getconfigval('linuxblacklistinterfacesdisposition', 'drop')) | ||||||||||||||||||||||||||||||||||||||
self.blacklist_ifaces = ( | ||||||||||||||||||||||||||||||||||||||
self.getconfigval('linuxblacklistedinterfaces', None)) | ||||||||||||||||||||||||||||||||||||||
self.logger.debug('Blacklisted interfaces: %s. Disposition: %s' % | ||||||||||||||||||||||||||||||||||||||
(self.blacklist_ifaces, self.blacklist_ifaces_disp)) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def write_pcap(self, pkt): | ||||||||||||||||||||||||||||||||||||||
"""Writes a packet to the pcap. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
@@ -1141,12 +1152,33 @@ def handle_pkt(self, pkt, callbacks3, callbacks4): | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
crit = DivertParms(self, pkt) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# check for blacklisted interface and drop if needed | ||||||||||||||||||||||||||||||||||||||
if (self.blacklist_ifaces and | ||||||||||||||||||||||||||||||||||||||
(pkt.src_ip in self.blacklist_ifaces or | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a concrete example, from typing into the python interpreter:
|
||||||||||||||||||||||||||||||||||||||
pkt.dst_ip in self.blacklist_ifaces)): | ||||||||||||||||||||||||||||||||||||||
self.logger.debug("Blacklisted Interface. src: %s dst: %s" % | ||||||||||||||||||||||||||||||||||||||
(pkt.src_ip, pkt.dst_ip)) | ||||||||||||||||||||||||||||||||||||||
if self.blacklist_ifaces_disp == 'Drop': | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||||||||||||||||||||||||||||||||||
self.logger.debug("Dropping blacklist interface packet") | ||||||||||||||||||||||||||||||||||||||
pkt.drop = True | ||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||
self.logger.debug("Ignoring blacklist interface packet") | ||||||||||||||||||||||||||||||||||||||
no_further_processing = True | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# fnpacket has parsed all that can be parsed, so | ||||||||||||||||||||||||||||||||||||||
pid, comm = self.get_pid_comm(pkt) | ||||||||||||||||||||||||||||||||||||||
if self.pdebug_level & DGENPKTV: | ||||||||||||||||||||||||||||||||||||||
logline = self.formatPkt(pkt, pid, comm) | ||||||||||||||||||||||||||||||||||||||
self.pdebug(DGENPKTV, logline) | ||||||||||||||||||||||||||||||||||||||
elif pid and (pid != self.pid) and crit.first_packet_new_session: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# check for no_further_processing here in order to filter out | ||||||||||||||||||||||||||||||||||||||
# packets that are being ignored already due to a blacklisted | ||||||||||||||||||||||||||||||||||||||
# interface. If a user is using ssh over a blacklisted interface | ||||||||||||||||||||||||||||||||||||||
# there needs to be no per-packet output by default. If there is | ||||||||||||||||||||||||||||||||||||||
# output for each packet, an infinite loop is generated where each | ||||||||||||||||||||||||||||||||||||||
# packet produces output which produces a packet, etc. | ||||||||||||||||||||||||||||||||||||||
elif (pid and (pid != self.pid) and crit.first_packet_new_session & | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you use a boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably yielding inconsistent results from what you expected because of the varying operator precedence of
|
||||||||||||||||||||||||||||||||||||||
no_further_processing is not True): | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or more succinctly, |
||||||||||||||||||||||||||||||||||||||
self.logger.info(' pid: %d name: %s' % | ||||||||||||||||||||||||||||||||||||||
(pid, comm if comm else 'Unknown')) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,7 +242,7 @@ def handle_nonlocal(self, nfqpkt): | |
self.logger.error('Exception: %s' % (traceback.format_exc())) | ||
raise | ||
|
||
nfqpkt.accept() | ||
nfqpkt.accept() if not pkt.drop else nfqpkt.drop() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case I agree with @tankbusta that
|
||
|
||
def handle_incoming(self, nfqpkt): | ||
"""Incoming packet hook. | ||
|
@@ -266,7 +266,7 @@ def handle_incoming(self, nfqpkt): | |
self.logger.error('Exception: %s' % (traceback.format_exc())) | ||
raise | ||
|
||
nfqpkt.accept() | ||
nfqpkt.accept() if not pkt.drop else nfqpkt.drop() | ||
|
||
def handle_outgoing(self, nfqpkt): | ||
"""Outgoing packet hook. | ||
|
@@ -293,7 +293,7 @@ def handle_outgoing(self, nfqpkt): | |
self.logger.error('Exception: %s' % (traceback.format_exc())) | ||
raise | ||
|
||
nfqpkt.accept() | ||
nfqpkt.accept() if not pkt.drop else nfqpkt.drop() | ||
|
||
def check_log_nonlocal(self, crit, pkt): | ||
"""Conditionally log packets having a foreign destination. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This takes the raw string that the user typed and stashes it in
self.blacklist_ifaces
as astr
object. If the user specifies a comma-separated list of IP addresses here, thenself.blacklist_ifaces
will just be that string, e.g.1.2.3.1, 1.2.3.5
. Because thepkt.src_ip in self.blacklist_ifaces
check can match in cases where it should not (see below) and must be replaced with a new check, I suggest modifying the code here to split these on commas/whitespace and store them as an array or aset
, and then usingset
intersection (see https://docs.python.org/2/library/sets.html) to test if the set of source/dest IPs overlaps with the set of blacklisted interface IPs.