-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): load external tasks from template dir (#373)
Fixes #46
- Loading branch information
Showing
9 changed files
with
143 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
from secator.utils import discover_internal_tasks, discover_external_tasks | ||
INTERNAL_TASKS = discover_internal_tasks() | ||
EXTERNAL_TASKS = discover_external_tasks() | ||
ALL_TASKS = INTERNAL_TASKS + EXTERNAL_TASKS | ||
from secator.utils import discover_tasks | ||
TASKS = discover_tasks() | ||
__all__ = [ | ||
cls.__name__ | ||
for cls in ALL_TASKS | ||
for cls in TASKS | ||
] | ||
for cls in INTERNAL_TASKS: | ||
for cls in TASKS: | ||
exec(f'from .{cls.__name__} import {cls.__name__}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from secator.runners import Command | ||
from secator.decorators import task | ||
from secator.output_types import Vulnerability | ||
|
||
|
||
@task() | ||
class ls(Command): | ||
cmd = 'ls -al' | ||
output_types = [Vulnerability] | ||
output_map = { | ||
Vulnerability: {} | ||
} | ||
|
||
@staticmethod | ||
def item_loader(self, line): | ||
fields = ['permissions', 'link_count', 'owner', 'group', 'size', 'month', 'day', 'hour', 'path'] | ||
result = [c for c in line.split(' ') if c] | ||
if len(result) != len(fields): | ||
return None | ||
data = {} | ||
for ix, value in enumerate(result): | ||
data[fields[ix]] = value | ||
|
||
# Output vulnerabilities | ||
permissions = data['permissions'] | ||
path = data['path'] | ||
full_path = f'{self.input}/{path}' | ||
if permissions[-2] == 'w': # found a vulnerability ! | ||
yield Vulnerability( | ||
name='World-writeable path', | ||
severity='high', | ||
confidence='high', | ||
provider='ls', | ||
matched_at=full_path, | ||
extra_data={k: v for k, v in data.items() if k != 'path'} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type: workflow | ||
name: ls | ||
tasks: | ||
ls: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import unittest | ||
from secator.config import CONFIG | ||
from secator.output_types import Vulnerability | ||
from secator.utils_test import FIXTURES_DIR, clear_modules | ||
import os | ||
|
||
import shutil | ||
|
||
|
||
class TestTemplate(unittest.TestCase): | ||
def setUp(self): | ||
self.template_dir = CONFIG.dirs.templates | ||
self.custom_task_path = self.template_dir / 'ls.py' | ||
self.writeable_file = self.template_dir / 'test.txt' | ||
self.custom_workflow_path = self.template_dir / 'ls.yml' | ||
shutil.copy(f'{FIXTURES_DIR}/ls.py', self.custom_task_path) | ||
shutil.copy(f'{FIXTURES_DIR}/ls.yml', self.custom_workflow_path) | ||
self.writeable_file.touch() | ||
os.chmod(self.writeable_file, 0o007) | ||
self.expected_vuln = Vulnerability( | ||
name='World-writeable path', | ||
severity='high', | ||
confidence='high', | ||
provider='ls', | ||
matched_at=f'{str(self.writeable_file)}', | ||
_source='ls', | ||
) | ||
clear_modules() | ||
self.maxDiff = None | ||
|
||
def tearDown(self): | ||
self.custom_task_path.unlink() | ||
self.custom_workflow_path.unlink() | ||
self.writeable_file.unlink() | ||
|
||
def test_external_task(self): | ||
from secator.tasks import ls | ||
results = ls(str(self.template_dir)).run() | ||
self.assertEqual(len(results), 1) | ||
self.assertTrue(self.expected_vuln == Vulnerability.load(results[0].toDict())) | ||
|
||
def test_external_workflow(self): | ||
from secator.cli import ALL_WORKFLOWS | ||
from secator.runners import Workflow | ||
ls_workflow = None | ||
for w in ALL_WORKFLOWS: | ||
if w.name == 'ls': | ||
ls_workflow = w | ||
self.assertIsNotNone(ls_workflow) | ||
results = Workflow(ls_workflow, targets=[str(self.template_dir)]).run() | ||
self.assertEqual(len(results), 2) | ||
self.assertTrue(self.expected_vuln == Vulnerability.load(results[1].toDict())) | ||
|