Improve getting pid for udp connections

This commit is contained in:
adisbladis 2017-06-20 20:27:06 +08:00
parent 28e5395fbe
commit 993f46be91
Failed to generate hash of commit
2 changed files with 15 additions and 11 deletions

View file

@ -22,14 +22,22 @@ import os
def get_pid_by_connection(src_addr, src_p, dst_addr, dst_p, proto='tcp'):
pids = (connection.pid for connection in psutil.net_connections(kind=proto)
if connection.laddr == (src_addr, int(src_p)) and
connection.raddr == (dst_addr, int(dst_p)))
# We always take the first element as we assume it contains only one
# It should not be possible to keep two connections which are the same.
for p in pids:
return p
for conn in psutil.net_connections(kind=proto):
if proto == 'tcp':
if conn.laddr != (src_addr, int(src_p)):
continue
if conn.raddr != (dst_addr, int(dst_p)):
continue
# UDP gives us a very limited dataset to work with
elif proto == 'udp':
if conn.laddr[1] != int(src_p):
continue
return conn.pid
logging.warning("Could not find process for %s connection %s:%s -> %s:%s",
proto,

View file

@ -82,10 +82,6 @@ class NetfilterQueueWrapper(threading.Thread):
logging.debug("Could not detect protocol for packet.")
return
elif conn.app.pid is None and conn.proto != 'icmp':
logging.debug("Could not detect process for connection.")
return
# Get verdict, if verdict cannot be found prompt user in thread
verd = self.snitch.rules.get_verdict(conn)
if verd is None:
@ -94,7 +90,7 @@ class NetfilterQueueWrapper(threading.Thread):
self.snitch.dbus_service.prompt(
conn.id,
conn.hostname,
conn.dst_port,
conn.dst_port or 0,
conn.dst_addr,
conn.proto,
conn.app.pid or 0,