2013-08-21 11:26:09 +05:30
|
|
|
#!/usr/bin/python
|
2013-08-26 00:23:59 +05:30
|
|
|
|
|
|
|
import argparse
|
2013-08-21 11:26:09 +05:30
|
|
|
import os
|
|
|
|
import re
|
2013-09-26 18:41:41 +05:30
|
|
|
import sys
|
2013-08-21 11:26:09 +05:30
|
|
|
|
|
|
|
import apparmor.aa as apparmor
|
|
|
|
|
2013-09-22 15:25:20 +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'))
|
2013-08-21 11:26:09 +05:30
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
paranoid = args.paranoid
|
|
|
|
|
|
|
|
aa_mountpoint = apparmor.check_for_apparmor()
|
|
|
|
if not aa_mountpoint:
|
2013-08-26 00:23:59 +05:30
|
|
|
raise apparmor.AppArmorException(_('It seems AppArmor was not started. Please enable AppArmor and try again.'))
|
2013-08-21 11:26:09 +05:30
|
|
|
|
|
|
|
pids = []
|
|
|
|
if paranoid:
|
|
|
|
pids = list(filter(lambda x: re.search('^\d+$', x), apparmor.get_subdirectories('/proc')))
|
|
|
|
else:
|
|
|
|
regex_tcp_udp = re.compile('^(tcp|udp)\s+\d+\s+\d+\s+\S+\:(\d+)\s+\S+\:(\*|\d+)\s+(LISTEN|\s+)\s+(\d+)\/(\S+)')
|
2013-09-20 19:20:41 +05:30
|
|
|
import subprocess
|
2013-09-26 18:41:41 +05:30
|
|
|
if sys.version_info < (3,0):
|
|
|
|
output = subprocess.check_output('LANG=C netstat -nlp', shell=True).split('\n')
|
|
|
|
else:
|
|
|
|
#Python3 needs to translate a stream of bytes to string with specified encoding
|
|
|
|
output = str(subprocess.check_output('LANG=C netstat -nlp', shell=True), encoding='utf8').split('\n')
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2013-08-21 11:26:09 +05:30
|
|
|
for line in output:
|
|
|
|
match = regex_tcp_udp.search(line)
|
|
|
|
if match:
|
|
|
|
pids.append(match.groups()[4])
|
|
|
|
# We can safely remove duplicate pid's?
|
|
|
|
pids = list(map(lambda x: int(x), set(pids)))
|
|
|
|
|
|
|
|
for pid in sorted(pids):
|
|
|
|
try:
|
|
|
|
prog = os.readlink('/proc/%s/exe'%pid)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
attr = None
|
|
|
|
if os.path.exists('/proc/%s/attr/current'%pid):
|
|
|
|
with apparmor.open_file_read('/proc/%s/attr/current'%pid) as current:
|
|
|
|
for line in current:
|
|
|
|
if line.startswith('/') or line.startswith('null'):
|
|
|
|
attr = line.strip()
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2013-08-21 11:26:09 +05:30
|
|
|
cmdline = apparmor.cmd(['cat', '/proc/%s/cmdline'%pid])[1]
|
|
|
|
pname = cmdline.split('\0')[0]
|
|
|
|
if '/' in pname and pname != prog:
|
|
|
|
pname = '(%s)'%pname
|
|
|
|
else:
|
|
|
|
pname = ''
|
2013-08-30 03:54:31 +05:30
|
|
|
regex_interpreter = re.compile('^(/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-08-26 00:23:59 +05:30
|
|
|
cmdline = re.sub('\x00', ' ', cmdline)
|
2013-08-21 11:26:09 +05:30
|
|
|
cmdline = re.sub('\s+$', '', cmdline).strip()
|
2013-08-30 03:54:31 +05:30
|
|
|
|
2013-08-26 00:23:59 +05:30
|
|
|
apparmor.UI_Info(_('%s %s (%s) not confined\n')%(pid, prog, cmdline))
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
|
|
|
if pname and pname[-1] == ')':
|
|
|
|
pname += ' '
|
2013-08-26 00:23:59 +05:30
|
|
|
apparmor.UI_Info(_('%s %s %snot confined\n')%(pid, prog, pname))
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
2013-08-30 03:54:31 +05:30
|
|
|
if regex_interpreter.search(prog):
|
2013-08-21 11:26:09 +05:30
|
|
|
cmdline = re.sub('\0', ' ', cmdline)
|
|
|
|
cmdline = re.sub('\s+$', '', cmdline).strip()
|
2013-08-26 00:23:59 +05:30
|
|
|
apparmor.UI_Info(_("%s %s (%s) confined by '%s'\n")%(pid, prog, cmdline, attr))
|
2013-08-21 11:26:09 +05:30
|
|
|
else:
|
|
|
|
if pname and pname[-1] == ')':
|
|
|
|
pname += ' '
|
2013-08-26 00:23:59 +05:30
|
|
|
apparmor.UI_Info(_("%s %s %sconfined by '%s'\n")%(pid, prog, pname, attr))
|