This commit is contained in:
Simone Margaritelli 2017-04-17 01:32:14 +02:00
parent 8a45443db6
commit e86e62c01f
4 changed files with 40 additions and 28 deletions

View file

@ -3,25 +3,21 @@ import re
import os
from threading import Lock
class Application:
class LinuxDesktopParser:
lock = Lock()
apps = None
def __init__( self, pid, path ):
self.pid = pid
self.path = path
self.name, self.icon = Application.get_name_and_icon( os.path.basename(self.path) )
@staticmethod
def get_name_and_icon( path ):
def get_info_by_path( path ):
path = os.path.basename(path)
name = path
icon = None
Application.lock.acquire()
LinuxDesktopParser.lock.acquire()
try:
if Application.apps is None:
Application.apps = {}
if LinuxDesktopParser.apps is None:
LinuxDesktopParser.apps = {}
for item in glob.glob('/usr/share/applications/*.desktop'):
name = None
icon = None
@ -30,30 +26,36 @@ class Application:
with open( item, 'rt' ) as fp:
in_section = False
for line in fp:
line = line.strip()
if '[Desktop Entry]' in line:
in_section = True
continue
elif len(line.strip()) > 0 and line[0] == '[':
elif len(line) > 0 and line[0] == '[':
in_section = False
continue
if in_section and line.startswith('Exec='):
cmd = os.path.basename( line[5:].split(' ')[0].strip() )
cmd = os.path.basename( line[5:].split(' ')[0] )
elif in_section and line.startswith('Icon='):
icon = line[5:].strip()
icon = line[5:]
elif in_section and line.startswith('Name='):
name = line[5:].strip()
name = line[5:]
if cmd is not None:
print cmd
Application.apps[cmd] = ( name, icon )
LinuxDesktopParser.apps[cmd] = ( name, icon )
if path in Application.apps:
name, icon = Application.apps[path]
if path in LinuxDesktopParser.apps:
name, icon = LinuxDesktopParser.apps[path]
finally:
Application.lock.release()
LinuxDesktopParser.lock.release()
return ( name, icon )
class Application:
def __init__( self, pid, path ):
self.pid = pid
self.path = path
self.name, self.icon = LinuxDesktopParser.get_info_by_path(path)

View file

@ -24,16 +24,26 @@ class Connection:
self.dst_port = self.pkt.udp.dport
if None not in ( self.proto, self.src_addr, self.src_port, self.dst_addr, self.dst_port ):
self.pid, self.app_name = get_process_name_by_connection( self.src_addr,
self.pid, self.app_path = get_process_name_by_connection( self.src_addr,
self.src_port,
self.dst_addr,
self.dst_port,
self.proto )
self.app = Application( self.pid, self.app_name )
self.app = Application( self.pid, self.app_path )
def get_app_name(self):
if self.app_path == 'Unknown':
return self.app_path
elif self.app_path == self.app.name:
return self.app_path
else:
return "'%s' ( %s )" % ( self.app.name, self.app_path )
def __repr__(self):
return "[%s] %s (%s) -> %s:%s" % ( self.pid, self.app_name, self.proto, self.dst_addr, self.dst_port )
return "[%s] %s (%s) -> %s:%s" % ( self.pid, self.app_path, self.proto, self.dst_addr, self.dst_port )
def cache_key(self):
return "%s:%s:%s:%s" % ( self.app_name, self.proto, self.dst_addr, self.dst_port)
return "%s:%s:%s:%s" % ( self.app_path, self.proto, self.dst_addr, self.dst_port)

View file

@ -17,14 +17,14 @@ class PacketQueue:
if ckey in PacketQueue.verdicts:
verd = PacketQueue.verdicts[ckey]
elif c.app_name in PacketQueue.verdicts:
verd = PacketQueue.verdicts[c.app_name]
elif c.app_path in PacketQueue.verdicts:
verd = PacketQueue.verdicts[c.app_path]
else:
choice = None
while choice is None:
choice = raw_input("%s is trying to connect to %s on %s port %s, allow? [y/n/a(lways)] " % \
( c.app.name, c.dst_addr, c.proto, c.dst_port ) ).lower()
( c.get_app_name(), c.dst_addr, c.proto, c.dst_port ) ).lower()
if choice == 'y':
verd = nfqueue.NF_ACCEPT
key = ckey
@ -34,7 +34,7 @@ class PacketQueue:
elif choice == 'a':
verd = nfqueue.NF_ACCEPT
key = c.app_name
key = c.app_path
else:
choice = None

View file

@ -46,4 +46,4 @@ def get_process_name_by_connection( src_addr, src_p, dst_addr, dst_p, proto = 't
pid = get_pid_of_inode(inode)
return ( pid, os.readlink( "/proc/%s/exe" % pid ) )
return ( 0, '?' )
return ( 0, "Unknown" )