|
| 1 | +private import python |
| 2 | +private import semmle.python.Concepts |
| 3 | +private import semmle.python.ApiGraphs |
| 4 | +private import semmle.python.dataflow.new.DataFlow |
| 5 | +private import semmle.python.dataflow.new.RemoteFlowSources |
| 6 | + |
| 7 | +/** |
| 8 | + * A data flow source of the client ip obtained according to the remote endpoint identifier specified |
| 9 | + * (`X-Forwarded-For`, `X-Real-IP`, `Proxy-Client-IP`, etc.) in the header. |
| 10 | + * |
| 11 | + * For example: `request.headers.get("X-Forwarded-For")`. |
| 12 | + */ |
| 13 | +abstract class ClientSuppliedIpUsedInSecurityCheck extends DataFlow::CallCfgNode { } |
| 14 | + |
| 15 | +private class FlaskClientSuppliedIpUsedInSecurityCheck extends ClientSuppliedIpUsedInSecurityCheck { |
| 16 | + FlaskClientSuppliedIpUsedInSecurityCheck() { |
| 17 | + this = |
| 18 | + API::moduleImport("flask") |
| 19 | + .getMember("request") |
| 20 | + .getMember("headers") |
| 21 | + .getMember(["get", "get_all", "getlist"]) |
| 22 | + .getACall() and |
| 23 | + this.getArg(0).asCfgNode().getNode().(StrConst).getText().toLowerCase() = |
| 24 | + clientIpParameterName() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +private class DjangoClientSuppliedIpUsedInSecurityCheck extends ClientSuppliedIpUsedInSecurityCheck { |
| 29 | + DjangoClientSuppliedIpUsedInSecurityCheck() { |
| 30 | + exists(RemoteFlowSource rfs, DataFlow::LocalSourceNode lsn | |
| 31 | + rfs.getSourceType() = "django.http.request.HttpRequest" and rfs.asCfgNode() = lsn.asCfgNode() |
| 32 | + | |
| 33 | + lsn.flowsTo(DataFlow::exprNode(this.getFunction() |
| 34 | + .asExpr() |
| 35 | + .(Attribute) |
| 36 | + .getObject() |
| 37 | + .(Attribute) |
| 38 | + .getObject())) and |
| 39 | + this.getFunction().asExpr().(Attribute).getName() = "get" and |
| 40 | + this.getFunction().asExpr().(Attribute).getObject().(Attribute).getName() in [ |
| 41 | + "headers", "META" |
| 42 | + ] and |
| 43 | + this.getArg(0).asCfgNode().getNode().(StrConst).getText().toLowerCase() = |
| 44 | + clientIpParameterName() |
| 45 | + ) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +private string clientIpParameterName() { |
| 50 | + result in [ |
| 51 | + "x-forwarded-for", "x_forwarded_for", "x-real-ip", "x_real_ip", "proxy-client-ip", |
| 52 | + "proxy_client_ip", "wl-proxy-client-ip", "wl_proxy_client_ip", "http_x_forwarded_for", |
| 53 | + "http-x-forwarded-for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", |
| 54 | + "http_forwarded_for", "http_forwarded", "http_via", "remote_addr" |
| 55 | + ] |
| 56 | +} |
| 57 | + |
| 58 | +/** A data flow sink for ip address forgery vulnerabilities. */ |
| 59 | +abstract class ClientSuppliedIpUsedInSecurityCheckSink extends DataFlow::Node { } |
| 60 | + |
| 61 | +/** A data flow sink for sql operation. */ |
| 62 | +private class SqlOperationSink extends ClientSuppliedIpUsedInSecurityCheckSink { |
| 63 | + SqlOperationSink() { this = any(SqlExecution e).getSql() } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * A data flow sink for remote client ip comparison. |
| 68 | + * |
| 69 | + * For example: `if not ipAddr.startswith('192.168.') : ...` determine whether the client ip starts |
| 70 | + * with `192.168.`, and the program can be deceived by forging the ip address. |
| 71 | + */ |
| 72 | +private class CompareSink extends ClientSuppliedIpUsedInSecurityCheckSink { |
| 73 | + CompareSink() { |
| 74 | + exists(Call call | |
| 75 | + call.getFunc().(Attribute).getName() = "startswith" and |
| 76 | + call.getArg(0).(StrConst).getText().regexpMatch(getIpAddressRegex()) and |
| 77 | + not call.getArg(0).(StrConst).getText() = "0:0:0:0:0:0:0:1" and |
| 78 | + call.getFunc().(Attribute).getObject() = this.asExpr() |
| 79 | + ) |
| 80 | + or |
| 81 | + exists(Compare compare | |
| 82 | + ( |
| 83 | + compare.getOp(0) instanceof Eq or |
| 84 | + compare.getOp(0) instanceof NotEq |
| 85 | + ) and |
| 86 | + ( |
| 87 | + compare.getLeft() = this.asExpr() and |
| 88 | + compare.getComparator(0).(StrConst).getText() instanceof PrivateHostName and |
| 89 | + not compare.getComparator(0).(StrConst).getText() = "0:0:0:0:0:0:0:1" |
| 90 | + or |
| 91 | + compare.getComparator(0) = this.asExpr() and |
| 92 | + compare.getLeft().(StrConst).getText() instanceof PrivateHostName and |
| 93 | + not compare.getLeft().(StrConst).getText() = "0:0:0:0:0:0:0:1" |
| 94 | + ) |
| 95 | + ) |
| 96 | + or |
| 97 | + exists(Compare compare | |
| 98 | + ( |
| 99 | + compare.getOp(0) instanceof In or |
| 100 | + compare.getOp(0) instanceof NotIn |
| 101 | + ) and |
| 102 | + ( |
| 103 | + compare.getLeft() = this.asExpr() |
| 104 | + or |
| 105 | + compare.getComparator(0) = this.asExpr() |
| 106 | + ) |
| 107 | + ) |
| 108 | + or |
| 109 | + exists(Call call | |
| 110 | + call.getFunc().(Attribute).getName() = "add" and |
| 111 | + call.getArg(0) = this.asExpr() |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +string getIpAddressRegex() { |
| 117 | + result = |
| 118 | + "^((10\\.((1\\d{2})?|(2[0-4]\\d)?|(25[0-5])?|([1-9]\\d|[0-9])?)(\\.)?)|(192\\.168\\.)|172\\.(1[6789]|2[0-9]|3[01])\\.)((1\\d{2})?|(2[0-4]\\d)?|(25[0-5])?|([1-9]\\d|[0-9])?)(\\.)?((1\\d{2})?|(2[0-4]\\d)?|(25[0-5])?|([1-9]\\d|[0-9])?)$" |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * A string matching private host names of IPv4 and IPv6, which only matches the host portion therefore checking for port is not necessary. |
| 123 | + * Several examples are localhost, reserved IPv4 IP addresses including 127.0.0.1, 10.x.x.x, 172.16.x,x, 192.168.x,x, and reserved IPv6 addresses including [0:0:0:0:0:0:0:1] and [::1] |
| 124 | + */ |
| 125 | +private class PrivateHostName extends string { |
| 126 | + bindingset[this] |
| 127 | + PrivateHostName() { |
| 128 | + this.regexpMatch("(?i)localhost(?:[:/?#].*)?|127\\.0\\.0\\.1(?:[:/?#].*)?|10(?:\\.[0-9]+){3}(?:[:/?#].*)?|172\\.16(?:\\.[0-9]+){2}(?:[:/?#].*)?|192.168(?:\\.[0-9]+){2}(?:[:/?#].*)?|\\[?0:0:0:0:0:0:0:1\\]?(?:[:/?#].*)?|\\[?::1\\]?(?:[:/?#].*)?") |
| 129 | + } |
| 130 | +} |
0 commit comments