injection_paramiko.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Copyright 2014 Hewlett-Packard Development Company, L.P.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. r"""
  6. ==============================================
  7. B601: Test for shell injection within Paramiko
  8. ==============================================
  9. Paramiko is a Python library designed to work with the SSH2 protocol for secure
  10. (encrypted and authenticated) connections to remote machines. It is intended to
  11. run commands on a remote host. These commands are run within a shell on the
  12. target and are thus vulnerable to various shell injection attacks. Bandit
  13. reports a MEDIUM issue when it detects the use of Paramiko's "exec_command"
  14. method advising the user to check inputs are correctly sanitized.
  15. :Example:
  16. .. code-block:: none
  17. >> Issue: Possible shell injection via Paramiko call, check inputs are
  18. properly sanitized.
  19. Severity: Medium Confidence: Medium
  20. CWE: CWE-78 (https://cwe.mitre.org/data/definitions/78.html)
  21. Location: ./examples/paramiko_injection.py:4
  22. 3 # this is not safe
  23. 4 paramiko.exec_command('something; really; unsafe')
  24. 5
  25. .. seealso::
  26. - https://security.openstack.org
  27. - https://github.com/paramiko/paramiko
  28. - https://www.owasp.org/index.php/Command_Injection
  29. - https://cwe.mitre.org/data/definitions/78.html
  30. .. versionadded:: 0.12.0
  31. .. versionchanged:: 1.7.3
  32. CWE information added
  33. """
  34. import bandit
  35. from bandit.core import issue
  36. from bandit.core import test_properties as test
  37. @test.checks("Call")
  38. @test.test_id("B601")
  39. def paramiko_calls(context):
  40. issue_text = (
  41. "Possible shell injection via Paramiko call, check inputs "
  42. "are properly sanitized."
  43. )
  44. for module in ["paramiko"]:
  45. if context.is_module_imported_like(module):
  46. if context.call_function_name in ["exec_command"]:
  47. return bandit.Issue(
  48. severity=bandit.MEDIUM,
  49. confidence=bandit.MEDIUM,
  50. cwe=issue.Cwe.OS_COMMAND_INJECTION,
  51. text=issue_text,
  52. )