2016-10-01 20:57:09 +02:00
|
|
|
#! /usr/bin/python3
|
2013-09-28 20:43:06 +05:30
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
|
2016-12-30 12:15:16 -08:00
|
|
|
# Copyright (C) 2016 Canonical, Ltd.
|
2013-09-28 20:43:06 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of version 2 of the GNU General Public
|
|
|
|
# License as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
2013-08-26 00:23:59 +05:30
|
|
|
import argparse
|
2013-08-21 11:26:09 +05:30
|
|
|
import os
|
|
|
|
import re
|
2016-12-30 12:15:16 -08:00
|
|
|
import subprocess
|
2013-09-26 18:41:41 +05:30
|
|
|
import sys
|
2013-08-21 11:26:09 +05:30
|
|
|
|
2014-02-27 14:53:25 -08:00
|
|
|
import apparmor.aa as aa
|
|
|
|
import apparmor.ui as ui
|
|
|
|
import apparmor.common
|
2013-08-21 11:26:09 +05:30
|
|
|
|
2015-07-06 22:02:34 +02:00
|
|
|
# setup exception handling
|
|
|
|
from apparmor.fail import enable_aa_exception_handler
|
|
|
|
enable_aa_exception_handler()
|
|
|
|
|
2014-02-10 22:15:05 -08:00
|
|
|
# setup module translations
|
2014-02-11 16:23:21 -08:00
|
|
|
from apparmor.translations import init_translation
|
|
|
|
_ = init_translation()
|
2014-02-10 22:15:05 -08:00
|
|
|
|
2013-12-20 03:12:58 +05:30
|
|
|
parser = argparse.ArgumentParser(description=_("Lists unconfined processes having tcp or udp ports"))
|
|
|
|
parser.add_argument("--paranoid", action="store_true", help=_("scan all processes from /proc"))
|
2016-12-30 12:15:16 -08:00
|
|
|
bin_group = parser.add_mutually_exclusive_group()
|
|
|
|
bin_group.add_argument("--with-ss", action='store_true', help=_("use ss(8) to find listening processes (default)"))
|
|
|
|
bin_group.add_argument("--with-netstat", action='store_true', help=_("use netstat(8) to find listening processes"))
|
2013-08-21 11:26:09 +05:30
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
paranoid = args.paranoid
|
|
|
|
|
2017-03-02 21:21:53 +00:00
|
|
|
aa.init_aa()
|
2014-02-27 14:53:25 -08:00
|
|
|
aa_mountpoint = aa.check_for_apparmor()
|
2013-08-21 11:26:09 +05:30
|
|
|
if not aa_mountpoint:
|
2014-02-27 14:53:25 -08:00
|
|
|
raise aa.AppArmorException(_("It seems AppArmor was not started. Please enable AppArmor and try again."))
|
2013-08-21 11:26:09 +05:30
|
|
|
|
2016-12-30 12:15:16 -08:00
|
|
|
|
|
|
|
def get_all_pids():
|
|
|
|
'''Return a set of all pids via walking /proc'''
|
|
|
|
return set(filter(lambda x: re.search(r"^\d+$", x), aa.get_subdirectories("/proc")))
|
|
|
|
|
|
|
|
|
2016-12-30 12:20:01 -08:00
|
|
|
def get_pids_ss(ss='ss'):
|
2016-12-30 12:15:16 -08:00
|
|
|
'''Get a set of pids listening on network sockets via ss(8)'''
|
|
|
|
regex_lines = re.compile(r"^(tcp|udp|raw|p_dgr)\s.+\s+users:(?P<users>\(\(.*\)\))$")
|
|
|
|
regex_users_pids = re.compile(r'(\("[^"]+",(pid=)?(\d+),[^)]+\))')
|
|
|
|
|
|
|
|
pids = set()
|
|
|
|
my_env = os.environ.copy()
|
|
|
|
my_env['LANG'] = 'C'
|
|
|
|
my_env['PATH'] = '/bin:/usr/bin:/sbin:/usr/sbin'
|
|
|
|
for family in ['inet', 'inet6', 'link']:
|
2016-12-30 12:20:01 -08:00
|
|
|
cmd = [ss, '-nlp', '--family', family]
|
2016-12-30 12:15:16 -08:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
output = subprocess.check_output(cmd, shell=False, env=my_env).split("\n")
|
|
|
|
else:
|
|
|
|
# Python3 needs to translate a stream of bytes to string with specified encoding
|
|
|
|
output = str(subprocess.check_output(cmd, shell=False, env=my_env), encoding='utf8').split("\n")
|
|
|
|
|
|
|
|
for line in output:
|
|
|
|
match = regex_lines.search(line.strip())
|
|
|
|
if match:
|
|
|
|
users = match.group('users')
|
|
|
|
for (_, _, pid) in regex_users_pids.findall(users):
|
|
|
|
pids.add(pid)
|
|
|
|
return pids
|
|
|
|
|
|
|
|
|
2016-12-30 12:20:01 -08:00
|
|
|
def get_pids_netstat(netstat='netstat'):
|
2016-12-30 12:15:16 -08:00
|
|
|
'''Get a set of pids listening on network sockets via netstat(8)'''
|
|
|
|
regex_tcp_udp = re.compile(r"^(tcp|udp|raw)6?\s+\d+\s+\d+\s+\S+\:(\d+)\s+\S+\:(\*|\d+)\s+(LISTEN|\d+|\s+)\s+(?P<pid>\d+)\/(\S+)")
|
|
|
|
|
2016-12-30 12:20:01 -08:00
|
|
|
cmd = [netstat, '-nlp', '--protocol', 'inet,inet6']
|
2016-12-30 12:15:16 -08:00
|
|
|
my_env = os.environ.copy()
|
|
|
|
my_env['LANG'] = 'C'
|
|
|
|
my_env['PATH'] = '/bin:/usr/bin:/sbin:/usr/sbin'
|
2013-12-20 03:12:58 +05:30
|
|
|
if sys.version_info < (3, 0):
|
2016-12-30 12:15:16 -08:00
|
|
|
output = subprocess.check_output(cmd, shell=False, env=my_env).split("\n")
|
2013-09-26 18:41:41 +05:30
|
|
|
else:
|
2016-12-30 12:15:16 -08:00
|
|
|
# Python3 needs to translate a stream of bytes to string with specified encoding
|
|
|
|
output = str(subprocess.check_output(cmd, shell=False, env=my_env), encoding='utf8').split("\n")
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2016-12-30 12:15:16 -08:00
|
|
|
pids = set()
|
2013-08-21 11:26:09 +05:30
|
|
|
for line in output:
|
|
|
|
match = regex_tcp_udp.search(line)
|
|
|
|
if match:
|
2016-12-30 12:15:16 -08:00
|
|
|
pids.add(match.group('pid'))
|
|
|
|
return pids
|
|
|
|
|
|
|
|
|
2024-01-30 09:43:23 +00:00
|
|
|
def escape_special_chars(data):
|
|
|
|
"""escape special characters in program names so that they can't mess up the terminal"""
|
|
|
|
data = repr(data)
|
|
|
|
if len(data) > 1 and data.startswith("'") and data.endswith("'"):
|
|
|
|
return data[1:-1]
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2016-12-30 12:15:16 -08:00
|
|
|
pids = set()
|
|
|
|
if paranoid:
|
|
|
|
pids = get_all_pids()
|
|
|
|
elif args.with_ss or (not args.with_netstat and (os.path.exists('/bin/ss') or os.path.exists('/usr/bin/ss'))):
|
|
|
|
pids = get_pids_ss()
|
|
|
|
else:
|
|
|
|
pids = get_pids_netstat()
|
2013-08-21 11:26:09 +05:30
|
|
|
|
2016-12-30 12:15:16 -08:00
|
|
|
for pid in sorted(map(int, pids)):
|
2013-08-21 11:26:09 +05:30
|
|
|
try:
|
2016-12-30 12:22:58 -08:00
|
|
|
prog = os.readlink("/proc/%s/exe" % pid)
|
2024-01-30 09:43:23 +00:00
|
|
|
prog = escape_special_chars(prog)
|
2013-12-29 15:12:30 +05:30
|
|
|
except OSError:
|
2013-08-21 11:26:09 +05:30
|
|
|
continue
|
|
|
|
attr = None
|
2016-12-30 12:22:58 -08:00
|
|
|
if os.path.exists("/proc/%s/attr/current" % pid):
|
|
|
|
with apparmor.common.open_file_read("/proc/%s/attr/current" % pid) as current:
|
2013-08-21 11:26:09 +05:30
|
|
|
for line in current:
|
2015-02-02 20:52:07 +01:00
|
|
|
line = line.strip()
|
2016-12-30 12:22:58 -08:00
|
|
|
if line.endswith(' (complain)', 1) or line.endswith(' (enforce)', 1): # enforce at least one char as profile name
|
2015-02-02 20:52:07 +01:00
|
|
|
attr = line
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2016-12-30 12:18:14 -08:00
|
|
|
pname = None
|
|
|
|
cmdline = None
|
|
|
|
with apparmor.common.open_file_read("/proc/%s/cmdline" % pid) as cmd:
|
|
|
|
cmdline = cmd.readlines()[0]
|
|
|
|
pname = cmdline.split("\0")[0]
|
2013-08-21 11:26:09 +05:30
|
|
|
if '/' in pname and pname != prog:
|
2016-12-30 12:22:58 -08:00
|
|
|
pname = "(%s)" % pname
|
2024-01-30 09:43:23 +00:00
|
|
|
pname = escape_special_chars(pname)
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
2013-12-20 03:12:58 +05:30
|
|
|
pname = ""
|
|
|
|
regex_interpreter = re.compile(r"^(/usr)?/bin/(python|perl|bash|dash|sh)$")
|
2013-08-21 11:26:09 +05:30
|
|
|
if not attr:
|
2013-08-30 03:54:31 +05:30
|
|
|
if regex_interpreter.search(prog):
|
2013-12-20 03:12:58 +05:30
|
|
|
cmdline = re.sub(r"\x00", " ", cmdline)
|
|
|
|
cmdline = re.sub(r"\s+$", "", cmdline).strip()
|
2024-01-30 09:43:23 +00:00
|
|
|
cmdline = escape_special_chars(cmdline)
|
2013-08-30 03:54:31 +05:30
|
|
|
|
2016-12-30 12:22:58 -08:00
|
|
|
ui.UI_Info(_("%(pid)s %(program)s (%(commandline)s) not confined") % {'pid': pid, 'program': prog, 'commandline': cmdline})
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
|
|
|
if pname and pname[-1] == ')':
|
2014-09-14 23:47:00 +05:30
|
|
|
pname = ' ' + pname
|
2016-12-30 12:22:58 -08:00
|
|
|
ui.UI_Info(_("%(pid)s %(program)s%(pname)s not confined") % {'pid': pid, 'program': prog, 'pname': pname})
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
2013-08-30 03:54:31 +05:30
|
|
|
if regex_interpreter.search(prog):
|
2013-12-20 03:12:58 +05:30
|
|
|
cmdline = re.sub(r"\0", " ", cmdline)
|
|
|
|
cmdline = re.sub(r"\s+$", "", cmdline).strip()
|
2024-01-30 09:43:23 +00:00
|
|
|
cmdline = escape_special_chars(cmdline)
|
2016-12-30 12:22:58 -08:00
|
|
|
ui.UI_Info(_("%(pid)s %(program)s (%(commandline)s) confined by '%(attribute)s'") % {'pid': pid, 'program': prog, 'commandline': cmdline, 'attribute': attr})
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
|
|
|
if pname and pname[-1] == ')':
|
2014-09-14 23:47:00 +05:30
|
|
|
pname = ' ' + pname
|
2016-12-30 12:22:58 -08:00
|
|
|
ui.UI_Info(_("%(pid)s %(program)s%(pname)s confined by '%(attribute)s'") % {'pid': pid, 'program': prog, 'pname': pname, 'attribute': attr})
|