|
| 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 | + exists(RemoteFlowSource rfs, DataFlow::AttrRead get | |
| 18 | + rfs.getSourceType() = "flask.request" and this.getFunction() = get |
| 19 | + | |
| 20 | + // `get` is a call to request.headers.get or request.headers.get_all or request.headers.getlist |
| 21 | + // request.headers |
| 22 | + get.getObject() |
| 23 | + .(DataFlow::AttrRead) |
| 24 | + // request |
| 25 | + .getObject() |
| 26 | + .getALocalSource() = rfs and |
| 27 | + get.getAttributeName() in ["get", "get_all", "getlist"] and |
| 28 | + get.getObject().(DataFlow::AttrRead).getAttributeName() = "headers" and |
| 29 | + this.getArg(0).asExpr().(StrConst).getText().toLowerCase() = clientIpParameterName() |
| 30 | + ) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +private class DjangoClientSuppliedIpUsedInSecurityCheck extends ClientSuppliedIpUsedInSecurityCheck { |
| 35 | + DjangoClientSuppliedIpUsedInSecurityCheck() { |
| 36 | + exists(RemoteFlowSource rfs, DataFlow::AttrRead get | |
| 37 | + rfs.getSourceType() = "django.http.request.HttpRequest" and this.getFunction() = get |
| 38 | + | |
| 39 | + // `get` is a call to request.headers.get or request.META.get |
| 40 | + // request.headers |
| 41 | + get.getObject() |
| 42 | + .(DataFlow::AttrRead) |
| 43 | + // request |
| 44 | + .getObject() |
| 45 | + .getALocalSource() = rfs and |
| 46 | + get.getAttributeName() = "get" and |
| 47 | + get.getObject().(DataFlow::AttrRead).getAttributeName() in ["headers", "META"] and |
| 48 | + this.getArg(0).asExpr().(StrConst).getText().toLowerCase() = clientIpParameterName() |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +private class TornadoClientSuppliedIpUsedInSecurityCheck extends ClientSuppliedIpUsedInSecurityCheck { |
| 54 | + TornadoClientSuppliedIpUsedInSecurityCheck() { |
| 55 | + exists(RemoteFlowSource rfs, DataFlow::AttrRead get | |
| 56 | + rfs.getSourceType() = "tornado.web.RequestHandler" and this.getFunction() = get |
| 57 | + | |
| 58 | + // `get` is a call to `rfs`.request.headers.get |
| 59 | + // `rfs`.request.headers |
| 60 | + get.getObject() |
| 61 | + .(DataFlow::AttrRead) |
| 62 | + // `rfs`.request |
| 63 | + .getObject() |
| 64 | + .(DataFlow::AttrRead) |
| 65 | + // `rfs` |
| 66 | + .getObject() |
| 67 | + .getALocalSource() = rfs and |
| 68 | + get.getAttributeName() in ["get", "get_list"] and |
| 69 | + get.getObject().(DataFlow::AttrRead).getAttributeName() = "headers" and |
| 70 | + this.getArg(0).asExpr().(StrConst).getText().toLowerCase() = clientIpParameterName() |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +private string clientIpParameterName() { |
| 76 | + result in [ |
| 77 | + "x-forwarded-for", "x_forwarded_for", "x-real-ip", "x_real_ip", "proxy-client-ip", |
| 78 | + "proxy_client_ip", "wl-proxy-client-ip", "wl_proxy_client_ip", "http_x_forwarded_for", |
| 79 | + "http-x-forwarded-for", "http_x_forwarded", "http_x_cluster_client_ip", "http_client_ip", |
| 80 | + "http_forwarded_for", "http_forwarded", "http_via", "remote_addr" |
| 81 | + ] |
| 82 | +} |
| 83 | + |
| 84 | +/** A data flow sink for ip address forgery vulnerabilities. */ |
| 85 | +abstract class PossibleSecurityCheck extends DataFlow::Node { } |
| 86 | + |
| 87 | +/** A data flow sink for sql operation. */ |
| 88 | +private class SqlOperationAsSecurityCheck extends PossibleSecurityCheck { |
| 89 | + SqlOperationAsSecurityCheck() { this = any(SqlExecution e).getSql() } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * A data flow sink for remote client ip comparison. |
| 94 | + * |
| 95 | + * For example: `if not ipAddr.startswith('192.168.') : ...` determine whether the client ip starts |
| 96 | + * with `192.168.`, and the program can be deceived by forging the ip address. |
| 97 | + */ |
| 98 | +private class CompareSink extends PossibleSecurityCheck { |
| 99 | + CompareSink() { |
| 100 | + exists(Call call | |
| 101 | + call.getFunc().(Attribute).getName() = "startswith" and |
| 102 | + call.getArg(0).(StrConst).getText().regexpMatch(getIpAddressRegex()) and |
| 103 | + not call.getArg(0).(StrConst).getText() = "0:0:0:0:0:0:0:1" and |
| 104 | + call.getFunc().(Attribute).getObject() = this.asExpr() |
| 105 | + ) |
| 106 | + or |
| 107 | + exists(Compare compare | |
| 108 | + ( |
| 109 | + compare.getOp(0) instanceof Eq or |
| 110 | + compare.getOp(0) instanceof NotEq |
| 111 | + ) and |
| 112 | + ( |
| 113 | + compare.getLeft() = this.asExpr() and |
| 114 | + compare.getComparator(0).(StrConst).getText() instanceof PrivateHostName and |
| 115 | + not compare.getComparator(0).(StrConst).getText() = "0:0:0:0:0:0:0:1" |
| 116 | + or |
| 117 | + compare.getComparator(0) = this.asExpr() and |
| 118 | + compare.getLeft().(StrConst).getText() instanceof PrivateHostName and |
| 119 | + not compare.getLeft().(StrConst).getText() = "0:0:0:0:0:0:0:1" |
| 120 | + ) |
| 121 | + ) |
| 122 | + or |
| 123 | + exists(Compare compare | |
| 124 | + ( |
| 125 | + compare.getOp(0) instanceof In or |
| 126 | + compare.getOp(0) instanceof NotIn |
| 127 | + ) and |
| 128 | + ( |
| 129 | + compare.getLeft() = this.asExpr() |
| 130 | + or |
| 131 | + compare.getComparator(0) = this.asExpr() and |
| 132 | + not compare.getLeft().(StrConst).getText() in ["%", ",", "."] |
| 133 | + ) |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +string getIpAddressRegex() { |
| 139 | + result = |
| 140 | + "^((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])?)$" |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * A string matching private host names of IPv4 and IPv6, which only matches the host portion therefore checking for port is not necessary. |
| 145 | + * 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] |
| 146 | + */ |
| 147 | +private class PrivateHostName extends string { |
| 148 | + bindingset[this] |
| 149 | + PrivateHostName() { |
| 150 | + 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\\]?(?:[:/?#].*)?") |
| 151 | + } |
| 152 | +} |
0 commit comments