|
| 1 | +/** |
| 2 | + * @name Use of insecure SSL/TLS version |
| 3 | + * @description Using an insecure SSL/TLS version may leave the connection vulnerable to attacks. |
| 4 | + * @id py/insecure-protocol |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @precision high |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-327 |
| 10 | + */ |
| 11 | + |
| 12 | +import python |
| 13 | + |
| 14 | +FunctionObject ssl_wrap_socket() { |
| 15 | + result = the_ssl_module().getAttribute("wrap_socket") |
| 16 | +} |
| 17 | + |
| 18 | +ClassObject ssl_Context_class() { |
| 19 | + result = the_ssl_module().getAttribute("SSLContext") |
| 20 | +} |
| 21 | + |
| 22 | +string insecure_version_name() { |
| 23 | + // For `pyOpenSSL.SSL` |
| 24 | + result = "SSLv2_METHOD" or |
| 25 | + result = "SSLv23_METHOD" or |
| 26 | + result = "SSLv3_METHOD" or |
| 27 | + result = "TLSv1_METHOD" or |
| 28 | + // For the `ssl` module |
| 29 | + result = "PROTOCOL_SSLv2" or |
| 30 | + result = "PROTOCOL_SSLv3" or |
| 31 | + result = "PROTOCOL_SSLv23" or |
| 32 | + result = "PROTOCOL_TLS" or |
| 33 | + result = "PROTOCOL_TLSv1" |
| 34 | +} |
| 35 | + |
| 36 | +private ModuleObject the_ssl_module() { |
| 37 | + result = any(ModuleObject m | m.getName() = "ssl") |
| 38 | +} |
| 39 | + |
| 40 | +private ModuleObject the_pyOpenSSL_module() { |
| 41 | + result = any(ModuleObject m | m.getName() = "pyOpenSSL.SSL") |
| 42 | +} |
| 43 | + |
| 44 | +/* A syntactic check for cases where points-to analysis cannot infer the presence of |
| 45 | + * a protocol constant, e.g. if it has been removed in later versions of the `ssl` |
| 46 | + * library. |
| 47 | + */ |
| 48 | +predicate probable_insecure_ssl_constant(CallNode call, string insecure_version) { |
| 49 | + exists(ControlFlowNode arg | arg = call.getArgByName("ssl_version") | |
| 50 | + arg.(AttrNode).getObject(insecure_version).refersTo(the_ssl_module()) |
| 51 | + or |
| 52 | + arg.(NameNode).getId() = insecure_version and |
| 53 | + exists(Import imp | |
| 54 | + imp.getAnImportedModuleName() = "ssl" and |
| 55 | + imp.getAName().getAsname().(Name).getId() = insecure_version |
| 56 | + ) |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +predicate unsafe_ssl_wrap_socket_call(CallNode call, string method_name, string insecure_version) { |
| 61 | + ( |
| 62 | + call = ssl_wrap_socket().getACall() and |
| 63 | + method_name = "deprecated method ssl.wrap_socket" |
| 64 | + or |
| 65 | + call = ssl_Context_class().getACall() and |
| 66 | + method_name = "ssl.SSLContext" |
| 67 | + ) |
| 68 | + and |
| 69 | + insecure_version = insecure_version_name() |
| 70 | + and |
| 71 | + ( |
| 72 | + call.getArgByName("ssl_version").refersTo(the_ssl_module().getAttribute(insecure_version)) |
| 73 | + or |
| 74 | + probable_insecure_ssl_constant(call, insecure_version) |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +ClassObject the_pyOpenSSL_Context_class() { |
| 79 | + result = any(ModuleObject m | m.getName() = "pyOpenSSL.SSL").getAttribute("Context") |
| 80 | +} |
| 81 | + |
| 82 | +predicate unsafe_pyOpenSSL_Context_call(CallNode call, string insecure_version) { |
| 83 | + call = the_pyOpenSSL_Context_class().getACall() and |
| 84 | + insecure_version = insecure_version_name() and |
| 85 | + call.getArg(0).refersTo(the_pyOpenSSL_module().getAttribute(insecure_version)) |
| 86 | +} |
| 87 | + |
| 88 | +from CallNode call, string method_name, string insecure_version |
| 89 | +where |
| 90 | + unsafe_ssl_wrap_socket_call(call, method_name, insecure_version) |
| 91 | +or |
| 92 | + unsafe_pyOpenSSL_Context_call(call, insecure_version) and method_name = "pyOpenSSL.SSL.Context" |
| 93 | +select call, "Insecure SSL/TLS protocol version " + insecure_version + " specified in call to " + method_name + "." |
0 commit comments