Skip to content

Commit 94482ad

Browse files
authored
Merge pull request #24 from Logisek/feature/DSCAN-44/add-more-nmap-fields
Refactoring and adds shell as a standalone action
2 parents d7215a5 + a8102e1 commit 94482ad

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

deltascan/cli/cli_output.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from deltascan.core.utils import (format_string)
1818
from deltascan.core.output import Output
1919
from deltascan.core.schemas import ReportScanFromDB, ReportDiffs
20-
from deltascan.core.exceptions import AppExceptions
2120
from deltascan.core.config import APP_DATE_FORMAT
2221
from marshmallow import ValidationError
2322
from rich.console import Console
@@ -33,7 +32,7 @@ class CliOutput(Output):
3332
console: Console
3433
_display_title: str
3534

36-
def __init__(self, data=None, suppress=False):
35+
def __init__(self, data=None, verbose=False):
3736
"""
3837
Initializes a new instance of the DataPresentation class.
3938
@@ -46,7 +45,7 @@ def __init__(self, data=None, suppress=False):
4645
self.data = []
4746
if data is not None:
4847
self._validate_data(data)
49-
self.suppress = suppress
48+
self.verbose = verbose
5049
self._index_to_uuid_mapping = {}
5150
self.console = Console()
5251

@@ -118,7 +117,7 @@ def _display_scan_results(self):
118117
}
119118
tables = []
120119
_counter = 1
121-
if self.suppress is True:
120+
if self.verbose is False:
122121
_sup_table = Table(show_header=True)
123122
_sup_table.add_column("Index", style=colors["col_1"], no_wrap=False, width=10)
124123
_sup_table.add_column("Uid", style=colors["col_2"], no_wrap=False)
@@ -132,7 +131,7 @@ def _display_scan_results(self):
132131

133132
for scan in self.data:
134133
self._index_to_uuid_mapping[str(_counter)] = scan["uuid"]
135-
if self.suppress is False:
134+
if self.verbose is True:
136135
table = Table(show_header=True)
137136
_status = self._print_color_depended_on_value(
138137
scan['results']['status'])
@@ -186,7 +185,7 @@ def _display_scan_results(self):
186185
)
187186
_counter += 1
188187

189-
if self.suppress is True:
188+
if self.verbose is False:
190189
tables.append(_sup_table)
191190

192191
return tables
@@ -213,7 +212,7 @@ def _display_scan_diffs(self):
213212
"for the given arguments[/]")
214213
return [table]
215214

216-
if self.suppress is True:
215+
if self.verbose is False:
217216
_sup_table = Table(show_header=True)
218217
_sup_table.add_column("Host", style=colors[0], no_wrap=True)
219218
_sup_table.add_column("Dates", style=colors[1], no_wrap=True)
@@ -222,7 +221,7 @@ def _display_scan_diffs(self):
222221
_sup_table.add_column("Arguments", style=colors[0], no_wrap=True)
223222

224223
for row in self.data:
225-
if self.suppress is False:
224+
if self.verbose is True:
226225
table = Table()
227226
table.title = f"[dim]Host: [/][rosy_brown]" \
228227
f"{self._print_generic_information_if_different(row['generic'][1]['host'], row['generic'][0]['host'])}[/]\n" \
@@ -263,7 +262,7 @@ def _display_scan_diffs(self):
263262
row['generic']['arguments']
264263
)
265264

266-
if self.suppress is True:
265+
if self.verbose is False:
267266
tables.append(_sup_table)
268267

269268
return tables
@@ -291,7 +290,7 @@ def _dict_diff_fields_to_list(self, diff_dict):
291290
return new_list
292291

293292
def _display(self):
294-
raise AppExceptions.DScanMethodNotImplemented("Something wrong happened. Please check your input")
293+
print("No output to display ...")
295294

296295
def display(self):
297296
"""

deltascan/cli/cmd.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def clear_screen():
3535
os.system("clear -x")
3636

3737

38-
def interactive_shell(_app, _ui, _interactive):
38+
def interactive_shell(_app, _ui, _interactive, open_shell=False):
3939
"""
4040
Starts an interactive shell for the application.
4141
@@ -50,8 +50,9 @@ def interactive_shell(_app, _ui, _interactive):
5050
__only_first_time = True
5151

5252
while _app.cleaning_up is False:
53-
if _app.is_running is True and _interactive is True and __only_first_time is True:
53+
if (_app.is_running is True and _interactive is True and __only_first_time is True) or open_shell is True:
5454
__only_first_time = False
55+
open_shell = False
5556
pass
5657
else:
5758
# Setting input with timeout in order not to block in case of cancel
@@ -176,7 +177,7 @@ def do_view(self, _):
176177
Execute the view action using the current configuration"""
177178
try:
178179
_r = self._app.view()
179-
output = CliOutput(_r, self._app.suppress)
180+
output = CliOutput(_r, self._app.verbose)
180181
_res = output.display()
181182
self.last_index_to_uuid_mapping = _res
182183
except Exception as e:
@@ -301,7 +302,7 @@ def run():
301302
parser = argparse.ArgumentParser(
302303
prog='deltascan', description='A package for scanning deltas')
303304
parser.add_argument(
304-
"action", help='the command to run', choices=['scan', 'diff', 'view', 'import', 'version'])
305+
"action", help='the command to run', choices=['scan', 'diff', 'view', 'import', 'shell', 'version'])
305306
parser.add_argument("-o", "--output", help='output file', required=False)
306307
parser.add_argument("-d", "--diff-files",
307308
help='comma separated files to find their differences (xml)',
@@ -326,8 +327,8 @@ def run():
326327
# "-v", "--verbose", default=False, action='store_true',
327328
# help="verbose output", required=False)
328329
parser.add_argument(
329-
"-s", "--suppress", default=False, action='store_true',
330-
help="suppress output", required=False)
330+
"-v", "--verbose", default=False, action='store_true',
331+
help="verbose output", required=False)
331332
parser.add_argument(
332333
"--n-scans", help="limit of scans databse queries. It is applied in scans view as well as scans diff",
333334
required=False)
@@ -388,8 +389,7 @@ def run():
388389
"action": clargs.action,
389390
"profile": clargs.profile,
390391
"conf_file": clargs.conf_file,
391-
"verbose": None,
392-
"suppress": clargs.suppress,
392+
"verbose": clargs.verbose,
393393
"n_scans": clargs.n_scans,
394394
"n_diffs": clargs.n_diffs,
395395
"fdate": clargs.from_date,
@@ -445,7 +445,7 @@ def run():
445445
item for sublist in [
446446
_s["scans"] for _s in _dscan.result if "scans" in _s
447447
] for item in sublist
448-
], _dscan.suppress)
448+
], _dscan.verbose)
449449

450450
output.display()
451451
os._exit(0)
@@ -455,16 +455,23 @@ def run():
455455
_r = _dscan.files_diff()
456456
else:
457457
_r = _dscan.diffs()
458-
output = CliOutput(_r)
458+
output = CliOutput(_r, _dscan.verbose)
459459
output.display()
460460
elif clargs.action == 'view':
461461
_r = _dscan.view()
462-
output = CliOutput(_r)
462+
output = CliOutput(_r, _dscan.verbose)
463463
output.display()
464464
elif clargs.action == 'import':
465465
_r = _dscan.import_data()
466-
output = CliOutput(_r)
466+
output = CliOutput(_r, _dscan.verbose)
467467
output.display()
468+
elif clargs.action == 'shell':
469+
_dscan_thread = ThreadWithException(target=_dscan.scan)
470+
_shell_thread = ThreadWithException(
471+
target=interactive_shell, args=(_dscan, ui_context, clargs.interactive, True))
472+
_dscan_thread.start()
473+
_shell_thread.start()
474+
_dscan_thread.join()
468475
else:
469476
if clargs.interactive is True:
470477
print("No action provided. Starting interactive shell.")

deltascan/core/config.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class Config:
5858
profile: str
5959
conf_file: str
6060
verbose: bool
61-
suppress: bool
6261
n_scans: str
6362
n_diffs: str
6463
fdate: str

deltascan/core/deltascan.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def __init__(self, config, ui_context=None, result=[]):
8282
_config['profile'],
8383
_config['conf_file'],
8484
_config['verbose'],
85-
_config['suppress'],
8685
_config['n_scans'],
8786
_config['n_diffs'],
8887
_config['fdate'],
@@ -113,7 +112,6 @@ def __init__(self, config, ui_context=None, result=[]):
113112
self._scans_history = []
114113
self.renderables = []
115114
self._cleaning_up = False
116-
self._has_been_interactive = False
117115
self._is_running = False
118116

119117
self._T = None
@@ -272,7 +270,7 @@ def _scan_orchestrator(self):
272270
if self.scans_to_wait == 0:
273271
time.sleep(0.2)
274272

275-
if (self.scans_to_wait == 0 and (self._config.is_interactive is False and self._has_been_interactive is False)) or self._cleaning_up:
273+
if (self.scans_to_wait == 0 and self._config.is_interactive is False) or self._cleaning_up:
276274
self._is_running = False
277275
break
278276

@@ -344,7 +342,7 @@ def _port_scan(self, __host=None, __profile=None, __name=None, __evt=None):
344342
raise AppExceptions.DScanInputValidationException("Invalid host format")
345343

346344
if self.ui_context is not None:
347-
self.ui_context["show_nmap_logs"] = self._config.is_interactive is False and self._has_been_interactive is False
345+
self.ui_context["show_nmap_logs"] = self._config.is_interactive is False
348346

349347
results = Scanner.scan(_host, _profile_arguments, self.ui_context, logger=self.logger, name=_name, _cancel_evt=__evt)
350348

@@ -364,8 +362,8 @@ def _port_scan(self, __host=None, __profile=None, __name=None, __evt=None):
364362

365363
# getting the current date and time in order not to override existing files
366364
_now = datetime.now().strftime(FILE_DATE_FORMAT)
367-
# Create the report only if output_file is configured and has never got ininteractive mode
368-
if self._config.output_file is not None and (self._config.is_interactive is False and self._has_been_interactive is False):
365+
# Create the report only if output_file is configured and has never got interactive mode
366+
if self._config.output_file is not None and (self._config.is_interactive is False):
369367
self._report_scans(last_n_scans, f"scans_{_host}_{_profile}_{_now}_{self._config.output_file}")
370368

371369
self._result.append({
@@ -416,7 +414,7 @@ def diffs(self, uuids=None):
416414
_split_scans_in_hosts = self.__split_scans_in_hosts([_s for _s in scans])
417415

418416
diffs = self._list_scans_with_diffs([_s for _scans in _split_scans_in_hosts.values() for _s in _scans])
419-
if self._config.output_file is not None and (self._config.is_interactive is False and self._has_been_interactive is False):
417+
if self._config.output_file is not None and self._config.is_interactive is False:
420418
self._report_diffs(diffs, output_file=f"diffs_{self._config.output_file}")
421419

422420
# getting the current date and time in order not to override existing files
@@ -544,7 +542,7 @@ def files_diff(self, _diff_files=None):
544542
"diffs": __diffs,
545543
"result_hashes": ["", ""]
546544
})
547-
if self._config.output_file is not None and (self._config.is_interactive is False and self._has_been_interactive is False):
545+
if self._config.output_file is not None and self._config.is_interactive is False:
548546
self._report_diffs(_final_diffs, output_file=f"diffs_{self._config.output_file}")
549547
return _final_diffs
550548

@@ -993,16 +991,14 @@ def is_interactive(self):
993991
@is_interactive.setter
994992
def is_interactive(self, value):
995993
self._config.is_interactive = value
996-
if self._config.is_interactive is True:
997-
self._has_been_interactive = True
998994

999995
@property
1000-
def suppress(self):
1001-
return self._config.suppress
996+
def verbose(self):
997+
return self._config.verbose
1002998

1003-
@suppress.setter
1004-
def suppress(self, value):
1005-
self._config.suppress = value
999+
@verbose.setter
1000+
def verbose(self, value):
1001+
self._config.verbose = value
10061002

10071003
@property
10081004
def host(self):

tests/conftest.py

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Config:
6262
profile: str
6363
conf_file: str
6464
verbose: str
65-
suppress: bool
6665
n_scans: str
6766
n_diffs: str
6867
fdate: str

tests/unit/test_deltascan.py

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def setUp(self):
4242
"profile": "TEST_V1",
4343
"conf_file": CONFIG_FILE,
4444
"verbose": False,
45-
"suppress": False,
4645
"n_scans": 1,
4746
"n_diffs": 1,
4847
"fdate": "2024-03-09 10:00:00",

0 commit comments

Comments
 (0)