Skip to content
This repository has been archived by the owner on Oct 5, 2019. It is now read-only.

adds binaries in path to collection #158

Merged
merged 3 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions osxcollector/osxcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,8 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
del Logger.Extra.extras[self.key]

PATH_ENVIRONMENT_NAME = "PATH"


class Collector(object):

Expand Down Expand Up @@ -913,6 +915,7 @@ def collect(self, section_list=None):
('safari', self._collect_safari),
('accounts', self._collect_accounts),
('mail', self._collect_mail),
('executables', self._collect_binary_names_in_path),
('full_hash', self._collect_full_hash)
]

Expand Down Expand Up @@ -1205,6 +1208,22 @@ def _collect_system_info(self):
}
Logger.log_dict(record)

def _collect_binary_names_in_path(self):
"""Collect the names of executable binaries in the PATH environment"""
exe_files = []

def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

if PATH_ENVIRONMENT_NAME in os.environ:
for bin_dir in os.environ[PATH_ENVIRONMENT_NAME].split(os.pathsep):
for root_dir, dirs, files in os.walk(bin_dir):
for the_file in files:
file_path = os.path.join(root_dir, the_file)
if is_exe(file_path):
exe_files.append(file_path)
Logger.log_dict({"executable_files": exe_files})

def _collect_startup(self):
"""Log the different LauchAgents and LaunchDaemons"""

Expand Down
21 changes: 21 additions & 0 deletions tests/osxcollector_collector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ def test_log_packages_in_dir(self):
self.collector._log_packages_in_dir('tests/data/packages/')
self.mock_log_dict.assert_called_with(expected)

def test_collect_binary_names_in_path(self):
expected = ['/usr/bin/ls', '/usr/bin/pwd']

with patch(
'os.walk', autospec=True,
return_value=[
['/usr', ('test',), ('bin/ls', 'bin/pwd', 'bin/tmp',)]
]
), patch(
'os.path.isfile', autospec=True,
side_effect=[True, True, False, True, True, False]
), patch(
'os.access', autospec=True,
side_effect=[True, True, True, True, True, True]
), patch.dict(
'os.environ', {'PATH': '/usr/bin'}
):
self.collector._collect_binary_names_in_path()
self.mock_log_dict.assert_called_once_with({'executable_files':
expected})

def test_log_startup_items(self):
list_of_files_in_dir = ['StartupParameters.plist']
plist = {
Expand Down