snmp_security_check.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #
  2. # Copyright (c) 2018 SolarWinds, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. import bandit
  6. from bandit.core import issue
  7. from bandit.core import test_properties as test
  8. @test.checks("Call")
  9. @test.test_id("B508")
  10. def snmp_insecure_version_check(context):
  11. """**B508: Checking for insecure SNMP versions**
  12. This test is for checking for the usage of insecure SNMP version like
  13. v1, v2c
  14. Please update your code to use more secure versions of SNMP.
  15. :Example:
  16. .. code-block:: none
  17. >> Issue: [B508:snmp_insecure_version_check] The use of SNMPv1 and
  18. SNMPv2 is insecure. You should use SNMPv3 if able.
  19. Severity: Medium Confidence: High
  20. CWE: CWE-319 (https://cwe.mitre.org/data/definitions/319.html)
  21. Location: examples/snmp.py:4:4
  22. More Info: https://bandit.readthedocs.io/en/latest/plugins/b508_snmp_insecure_version_check.html
  23. 3 # SHOULD FAIL
  24. 4 a = CommunityData('public', mpModel=0)
  25. 5 # SHOULD FAIL
  26. .. seealso::
  27. - http://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/snmp-versions.html
  28. - https://cwe.mitre.org/data/definitions/319.html
  29. .. versionadded:: 1.7.2
  30. .. versionchanged:: 1.7.3
  31. CWE information added
  32. """ # noqa: E501
  33. if context.call_function_name_qual == "pysnmp.hlapi.CommunityData":
  34. # We called community data. Lets check our args
  35. if context.check_call_arg_value(
  36. "mpModel", 0
  37. ) or context.check_call_arg_value("mpModel", 1):
  38. return bandit.Issue(
  39. severity=bandit.MEDIUM,
  40. confidence=bandit.HIGH,
  41. cwe=issue.Cwe.CLEARTEXT_TRANSMISSION,
  42. text="The use of SNMPv1 and SNMPv2 is insecure. "
  43. "You should use SNMPv3 if able.",
  44. lineno=context.get_lineno_for_call_arg("CommunityData"),
  45. )
  46. @test.checks("Call")
  47. @test.test_id("B509")
  48. def snmp_crypto_check(context):
  49. """**B509: Checking for weak cryptography**
  50. This test is for checking for the usage of insecure SNMP cryptography:
  51. v3 using noAuthNoPriv.
  52. Please update your code to use more secure versions of SNMP. For example:
  53. Instead of:
  54. `CommunityData('public', mpModel=0)`
  55. Use (Defaults to usmHMACMD5AuthProtocol and usmDESPrivProtocol
  56. `UsmUserData("securityName", "authName", "privName")`
  57. :Example:
  58. .. code-block:: none
  59. >> Issue: [B509:snmp_crypto_check] You should not use SNMPv3 without encryption. noAuthNoPriv & authNoPriv is insecure
  60. Severity: Medium CWE: CWE-319 (https://cwe.mitre.org/data/definitions/319.html) Confidence: High
  61. Location: examples/snmp.py:6:11
  62. More Info: https://bandit.readthedocs.io/en/latest/plugins/b509_snmp_crypto_check.html
  63. 5 # SHOULD FAIL
  64. 6 insecure = UsmUserData("securityName")
  65. 7 # SHOULD FAIL
  66. .. seealso::
  67. - http://snmplabs.com/pysnmp/examples/hlapi/asyncore/sync/manager/cmdgen/snmp-versions.html
  68. - https://cwe.mitre.org/data/definitions/319.html
  69. .. versionadded:: 1.7.2
  70. .. versionchanged:: 1.7.3
  71. CWE information added
  72. """ # noqa: E501
  73. if context.call_function_name_qual == "pysnmp.hlapi.UsmUserData":
  74. if context.call_args_count < 3:
  75. return bandit.Issue(
  76. severity=bandit.MEDIUM,
  77. confidence=bandit.HIGH,
  78. cwe=issue.Cwe.CLEARTEXT_TRANSMISSION,
  79. text="You should not use SNMPv3 without encryption. "
  80. "noAuthNoPriv & authNoPriv is insecure",
  81. lineno=context.get_lineno_for_call_arg("UsmUserData"),
  82. )