PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/w3af/plugins/attack/payloads/tests/test_shell_handler.py

https://github.com/andresriancho/w3af
Python | 119 lines | 82 code | 16 blank | 21 comment | 0 complexity | fb92c845b20b19760e372201baa7e67a MD5 | raw file
  1. """
  2. test_shell_handler.py
  3. Copyright 2012 Andres Riancho
  4. This file is part of w3af, http://w3af.org/ .
  5. w3af is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation version 2 of the License.
  8. w3af is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with w3af; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. """
  16. import unittest
  17. import w3af.core.data.kb.knowledge_base as kb
  18. from w3af.plugins.attack.payloads.shell_handler import (get_webshells,
  19. get_shell_code)
  20. class TestShellHandler(unittest.TestCase):
  21. TEST_CMD = 'ls'
  22. def test_get_shell_code_extension(self):
  23. shells = get_shell_code('php', self.TEST_CMD)
  24. self.assertEqual(len(shells), 2)
  25. php_shell_code, lang, shellcode_generator = shells[0]
  26. self.assertEqual(lang, 'php')
  27. self.assertIn('echo ', php_shell_code)
  28. def test_get_shell_code_extension_force(self):
  29. shells = get_shell_code('php', self.TEST_CMD, True)
  30. self.assertEqual(len(shells), 1)
  31. php_shell_code, lang, shellcode_generator = shells[0]
  32. self.assertEqual(lang, 'php')
  33. self.assertIn('echo ', php_shell_code)
  34. def test_get_shell_code_no_extension(self):
  35. shells = get_shell_code('', self.TEST_CMD)
  36. self.assertEqual(len(shells), 2)
  37. php_shell_code, lang, shellcode_generator = shells[0]
  38. self.assertEqual(lang, 'php')
  39. self.assertIn('echo ', php_shell_code)
  40. def test_get_shell_code_invalid_extension(self):
  41. shells = get_shell_code('123456', self.TEST_CMD)
  42. self.assertEqual(len(shells), 2)
  43. php_shell_code, lang, shellcode_generator = shells[0]
  44. self.assertEqual(lang, 'php')
  45. self.assertIn('echo ', php_shell_code)
  46. def test_get_web_shell_extension(self):
  47. shells = get_webshells('php')
  48. self.assertEqual(len(shells), 6)
  49. # The first one is PHP since we asked for it when we passed PHP as
  50. # parameter
  51. php_shell_code, lang = shells[0]
  52. self.assertEqual(lang, 'php')
  53. self.assertIn('echo ', php_shell_code)
  54. def test_get_web_shell_code_extension_force(self):
  55. shells = get_webshells('php', True)
  56. # Only one returned since we're forcing the extension
  57. self.assertEqual(len(shells), 1)
  58. php_shell_code, lang = shells[0]
  59. self.assertEqual(lang, 'php')
  60. self.assertIn('echo ', php_shell_code)
  61. def test_get_web_shell_code_no_extension(self):
  62. shells = get_webshells('')
  63. # All returned when invalid extension
  64. self.assertEqual(len(shells), 6)
  65. def test_get_web_shell_code_invalid_extension(self):
  66. shells = get_webshells('123456')
  67. # All returned when invalid extension
  68. self.assertEqual(len(shells), 6)
  69. def test_with_kb_data(self):
  70. kb.kb.raw_write('server_header', 'powered_by_string', ['ASP foo bar',])
  71. shells = get_webshells('')
  72. # TODO: The shells list has duplicates, fix in the future. Not really a
  73. # big issue since it would translate into 1 more HTTP request and
  74. # only in the cases where the user is exploiting something
  75. self.assertEqual(len(shells), 7)
  76. # The first one is ASP since we're scanning (according to the KB) an
  77. # ASP site
  78. asp_shell_code, lang = shells[0]
  79. self.assertEqual(lang, 'asp')
  80. self.assertIn('WSCRIPT.SHELL', asp_shell_code)
  81. kb.kb.cleanup()