Commit 7e8a7a99 by fwkz

Sorting commands in command suggestions.

parent 6b40a74d
...@@ -121,4 +121,3 @@ class Exploit(object): ...@@ -121,4 +121,3 @@ class Exploit(object):
def __str__(self): def __str__(self):
return self.__module__.split('.', 2).pop().replace('.', os.sep) return self.__module__.split('.', 2).pop().replace('.', os.sep)
...@@ -175,6 +175,11 @@ class RoutersploitInterpreter(BaseInterpreter): ...@@ -175,6 +175,11 @@ class RoutersploitInterpreter(BaseInterpreter):
self.prompt_hostname = 'rsf' self.prompt_hostname = 'rsf'
self.show_sub_commands = ('info', 'options', 'devices', 'all', 'creds', 'exploits', 'scanners') self.show_sub_commands = ('info', 'options', 'devices', 'all', 'creds', 'exploits', 'scanners')
self.global_commands = sorted(['use ', 'exec ', 'help', 'exit', 'show '])
self.module_commands = ['run', 'back', 'set ', 'setg ', 'check']
self.module_commands.extend(self.global_commands)
self.module_commands.sort()
self.modules = utils.index_modules() self.modules = utils.index_modules()
self.main_modules_dirs = [module for module in os.listdir(utils.MODULES_DIR) if not module.startswith("__")] self.main_modules_dirs = [module for module in os.listdir(utils.MODULES_DIR) if not module.startswith("__")]
...@@ -243,20 +248,19 @@ class RoutersploitInterpreter(BaseInterpreter): ...@@ -243,20 +248,19 @@ class RoutersploitInterpreter(BaseInterpreter):
matches.add("".join((text, head, sep))) matches.add("".join((text, head, sep)))
return list(map(utils.humanize_path, matches)) # humanize output, replace dots to forward slashes return list(map(utils.humanize_path, matches)) # humanize output, replace dots to forward slashes
def suggested_commands(self): # TODO: sorted list, factor out generic commands def suggested_commands(self):
""" Entry point for intelligent tab completion. """ Entry point for intelligent tab completion.
Based on state of interpreter this method will return intelligent suggestions. Based on state of interpreter this method will return intelligent suggestions.
:return: list of most accurate command suggestions :return: list of most accurate command suggestions
""" """
if self.current_module: if self.current_module and GLOBAL_OPTS:
module_commands = ['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit'] return sorted(itertools.chain(self.module_commands, ('unsetg ',)))
if GLOBAL_OPTS.keys(): elif self.current_module:
return itertools.chain(module_commands, ('unsetg ',)) return self.module_commands
return module_commands
else: else:
return ['use ', 'exec', 'help', 'exit', 'show '] return self.global_commands
def command_back(self, *args, **kwargs): def command_back(self, *args, **kwargs):
self.current_module = None self.current_module = None
......
...@@ -89,7 +89,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase): ...@@ -89,7 +89,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.set_module() self.set_module()
self.rsf.send("\t\t") self.rsf.send("\t\t")
self.assertPrompt( self.assertPrompt(
'back check exec exit help run set setg show \r\n', ' exec exit help run set setg show use \r\n',
self.module_prompt('FTP Bruteforce') self.module_prompt('FTP Bruteforce')
) )
...@@ -182,7 +182,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase): ...@@ -182,7 +182,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.set_module() self.set_module()
self.rsf.send("\t\t") self.rsf.send("\t\t")
self.assertPrompt( self.assertPrompt(
" check exec exit help run set setg show \r\n", " exec exit help run set setg show use \r\n",
self.module_prompt('FTP Bruteforce'), self.module_prompt('FTP Bruteforce'),
) )
...@@ -194,7 +194,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase): ...@@ -194,7 +194,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.rsf.send("setg target foo\r\n") self.rsf.send("setg target foo\r\n")
self.rsf.send("\t\t") self.rsf.send("\t\t")
self.assertPrompt( self.assertPrompt(
" show \r\ncheck exit run setg unsetg \r\n", ' use \r\ncheck exit run setg unsetg \r\n',
self.module_prompt('FTP Bruteforce'), self.module_prompt('FTP Bruteforce'),
) )
......
...@@ -254,23 +254,23 @@ class RoutersploitInterpreterTest(RoutersploitTestCase): ...@@ -254,23 +254,23 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.assertEqual(self.module_prompt_default('UnnamedModule'), self.interpreter.prompt) self.assertEqual(self.module_prompt_default('UnnamedModule'), self.interpreter.prompt)
def test_suggested_commands_with_loaded_module_and_no_global_value_set(self): def test_suggested_commands_with_loaded_module_and_no_global_value_set(self):
self.assertEqual( self.assertItemsEqual(
list(self.interpreter.suggested_commands()), list(self.interpreter.suggested_commands()),
['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit'] # Extra space at the end because of following param ['use ', 'run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit'] # Extra space at the end because of following param
) )
def test_suggested_commands_with_loaded_module_and_global_value_set(self): def test_suggested_commands_with_loaded_module_and_global_value_set(self):
GLOBAL_OPTS['key'] = 'value' GLOBAL_OPTS['key'] = 'value'
self.assertEqual( self.assertItemsEqual(
list(self.interpreter.suggested_commands()), list(self.interpreter.suggested_commands()),
['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit', 'unsetg '] # Extra space at the end because of following param ['use ', 'run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit', 'unsetg '] # Extra space at the end because of following param
) )
def test_suggested_commands_without_loaded_module(self): def test_suggested_commands_without_loaded_module(self):
self.interpreter.current_module = None self.interpreter.current_module = None
self.assertEqual( self.assertEqual(
self.interpreter.suggested_commands(), # Extra space at the end because of following param self.interpreter.suggested_commands(), # Extra space at the end because of following param
['use ', 'exec', 'help', 'exit', 'show '] ['exec ', 'exit', 'help', 'show ', 'use ']
) )
@mock.patch('importlib.import_module') @mock.patch('importlib.import_module')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment