Skip to content

Commit 1fa1a4e

Browse files
committed
Add Unicode Bypass Validation query tests and help
1 parent 2e5a048 commit 1fa1a4e

File tree

9 files changed

+225
-0
lines changed

9 files changed

+225
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Provides default sources, sinks and sanitizers for detecting
3+
* "Unicode transformation"
4+
* vulnerabilities, as well as extension points for adding your own.
5+
*/
6+
7+
private import python
8+
private import semmle.python.dataflow.new.DataFlow
9+
10+
/**
11+
* Provides default sources, sinks and sanitizers for detecting
12+
* "Unicode transformation"
13+
* vulnerabilities, as well as extension points for adding your own.
14+
*/
15+
module UnicodeBypassValidation {
16+
/**
17+
* A data flow source for "Unicode transformation" vulnerabilities.
18+
*/
19+
abstract class Source extends DataFlow::Node { }
20+
21+
/**
22+
* A data flow sink for "Unicode transformation" vulnerabilities.
23+
*/
24+
abstract class Sink extends DataFlow::Node { }
25+
26+
/**
27+
* A sanitizer for "Unicode transformation" vulnerabilities.
28+
*/
29+
abstract class Sanitizer extends DataFlow::Node { }
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Provides a taint-tracking configuration for detecting "Unicode transformation mishandling" vulnerabilities.
3+
*/
4+
5+
private import python
6+
import semmle.python.ApiGraphs
7+
import semmle.python.Concepts
8+
import semmle.python.dataflow.new.DataFlow
9+
import semmle.python.dataflow.new.internal.DataFlowPublic
10+
import semmle.python.dataflow.new.TaintTracking
11+
import semmle.python.dataflow.new.internal.TaintTrackingPrivate
12+
import semmle.python.dataflow.new.RemoteFlowSources
13+
import UnicodeBypassValidationCustomizations::UnicodeBypassValidation
14+
15+
/** A state signifying that a logical validation has not been performed. */
16+
class PreValidation extends DataFlow::FlowState {
17+
PreValidation() { this = "PreValidation" }
18+
}
19+
20+
/** A state signifying that a logical validation has been performed. */
21+
class PostValidation extends DataFlow::FlowState {
22+
PostValidation() { this = "PostValidation" }
23+
}
24+
25+
/**
26+
* A taint-tracking configuration for detecting "Unicode transformation mishandling" vulnerabilities.
27+
*
28+
* This configuration uses two flow states, `PreValidation` and `PostValidation`,
29+
* to track the requirement that a logical validation has been performed before the Unicode Transformation.
30+
*/
31+
class Configuration extends TaintTracking::Configuration {
32+
Configuration() { this = "UnicodeBypassValidation" }
33+
34+
override predicate isSource(DataFlow::Node source, DataFlow::FlowState state) {
35+
source instanceof RemoteFlowSource and state instanceof PreValidation
36+
}
37+
38+
override predicate isAdditionalTaintStep(
39+
DataFlow::Node nodeFrom, DataFlow::FlowState stateFrom, DataFlow::Node nodeTo,
40+
DataFlow::FlowState stateTo
41+
) {
42+
(
43+
exists(Escaping escaping | nodeFrom = escaping.getAnInput() and nodeTo = escaping.getOutput())
44+
or
45+
exists(RegexExecution re | nodeFrom = re.getString() and nodeTo = re)
46+
or
47+
stringManipulation(nodeFrom, nodeTo)
48+
) and
49+
stateFrom instanceof PreValidation and
50+
stateTo instanceof PostValidation
51+
}
52+
53+
/* A Unicode Tranformation (Unicode tranformation) is considered a sink when the algorithm used is either NFC or NFKC. */
54+
override predicate isSink(DataFlow::Node sink, DataFlow::FlowState state) {
55+
exists(API::CallNode cn |
56+
cn = API::moduleImport("unicodedata").getMember("normalize").getACall() and
57+
cn.getArg(0).asExpr().(Str).getS() = ["NFC", "NFKC"] and
58+
sink = cn.getArg(1) and
59+
state instanceof PostValidation
60+
)
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>Security checks bypass due to a Unicode transformation</p>
5+
<p>
6+
If ever a unicode tranformation is performed after some security checks or logical
7+
validation, the
8+
latter could be bypassed due to a potential Unicode characters collision.
9+
The validation of concern are any character escaping, any regex validation or any string
10+
verification.
11+
</p>
12+
<img src="./vulnerability-flow.png" alt="Security checks bypassed" />
13+
</overview>
14+
<recommendation>
15+
<p> Perform a Unicode normalization before the logical validation. </p>
16+
</recommendation>
17+
<example>
18+
19+
<p> The following example showcases the bypass of all checks performed by <code>
20+
flask.escape()</code> due to a post-unicode normalization.</p>
21+
<p>For instance: the character U+FE64 (<code>﹤</code>) is not filtered-out by the flask
22+
escape function. But due to the Unicode normalization, the character is transformed and
23+
would become U+003C (<code> &lt; </code> ).</p>
24+
25+
<sample src="escape-bypass.py" />
26+
27+
</example>
28+
<references>
29+
<li> Research study: <a
30+
href="https://gosecure.github.io/presentations/2021-02-unicode-owasp-toronto/philippe_arteau_owasp_unicode_v4.pdf">
31+
Unicode vulnerabilities that could bYte you
32+
</a> and <a
33+
href="https://gosecure.github.io/unicode-pentester-cheatsheet/">Unicode pentest
34+
cheatsheet</a>. </li>
35+
</references>
36+
</qhelp>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @name Bypass Logical Validation Using Unicode Characters
3+
* @description A Unicode transformation is using a remote user-controlled data. The transformation is a Unicode normalization using the algorithms "NFC" or "NFKC". In all cases, the security measures implemented or the logical validation performed to escape any injection characters, to validate using regex patterns or to perform string-based checks, before the Unicode transformation are **bypassable** by special Unicode characters.
4+
* @kind path-problem
5+
* @id py/unicode-bypass-validation
6+
* @precision high
7+
* @problem.severity error
8+
* @tags security
9+
* experimental
10+
* external/cwe/cwe-176
11+
* external/cwe/cwe-179
12+
* external/cwe/cwe-180
13+
*/
14+
15+
import python
16+
import semmle.python.security.dataflow.UnicodeBypassValidationQuery
17+
import DataFlow::PathGraph
18+
19+
from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
20+
where config.hasFlowPath(source, sink)
21+
select sink.getNode(), source, sink,
22+
"This $@ processes unsafely $@ and any logical validation in-between could be bypassed using special Unicode characters.",
23+
sink.getNode(), "Unicode transformation (Unicode normalization)", source.getNode(),
24+
"remote user-controlled data"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unicodedata
2+
from flask import Flask, request, escape, render_template
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/unsafe1")
8+
def unsafe1():
9+
user_input = escape(request.args.get("ui"))
10+
normalized_user_input = unicodedata.normalize("NFKC", user_input)
11+
return render_template("result.html", normalized_user_input=normalized_user_input)
36.8 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
edges
2+
| samples.py:0:0:0:0 | ModuleVariableNode for samples.request | samples.py:9:25:9:31 | ControlFlowNode for request |
3+
| samples.py:0:0:0:0 | ModuleVariableNode for samples.request | samples.py:16:25:16:31 | ControlFlowNode for request |
4+
| samples.py:2:26:2:32 | ControlFlowNode for ImportMember | samples.py:2:26:2:32 | GSSA Variable request |
5+
| samples.py:2:26:2:32 | GSSA Variable request | samples.py:0:0:0:0 | ModuleVariableNode for samples.request |
6+
| samples.py:9:18:9:47 | ControlFlowNode for escape() | samples.py:10:59:10:68 | ControlFlowNode for user_input |
7+
| samples.py:9:25:9:31 | ControlFlowNode for request | samples.py:9:25:9:36 | ControlFlowNode for Attribute |
8+
| samples.py:9:25:9:36 | ControlFlowNode for Attribute | samples.py:9:25:9:46 | ControlFlowNode for Attribute() |
9+
| samples.py:9:25:9:46 | ControlFlowNode for Attribute() | samples.py:9:18:9:47 | ControlFlowNode for escape() |
10+
| samples.py:16:18:16:47 | ControlFlowNode for escape() | samples.py:20:62:20:71 | ControlFlowNode for user_input |
11+
| samples.py:16:25:16:31 | ControlFlowNode for request | samples.py:16:25:16:36 | ControlFlowNode for Attribute |
12+
| samples.py:16:25:16:36 | ControlFlowNode for Attribute | samples.py:16:25:16:46 | ControlFlowNode for Attribute() |
13+
| samples.py:16:25:16:46 | ControlFlowNode for Attribute() | samples.py:16:18:16:47 | ControlFlowNode for escape() |
14+
nodes
15+
| samples.py:0:0:0:0 | ModuleVariableNode for samples.request | semmle.label | ModuleVariableNode for samples.request |
16+
| samples.py:2:26:2:32 | ControlFlowNode for ImportMember | semmle.label | ControlFlowNode for ImportMember |
17+
| samples.py:2:26:2:32 | GSSA Variable request | semmle.label | GSSA Variable request |
18+
| samples.py:9:18:9:47 | ControlFlowNode for escape() | semmle.label | ControlFlowNode for escape() |
19+
| samples.py:9:25:9:31 | ControlFlowNode for request | semmle.label | ControlFlowNode for request |
20+
| samples.py:9:25:9:36 | ControlFlowNode for Attribute | semmle.label | ControlFlowNode for Attribute |
21+
| samples.py:9:25:9:46 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
22+
| samples.py:10:59:10:68 | ControlFlowNode for user_input | semmle.label | ControlFlowNode for user_input |
23+
| samples.py:16:18:16:47 | ControlFlowNode for escape() | semmle.label | ControlFlowNode for escape() |
24+
| samples.py:16:25:16:31 | ControlFlowNode for request | semmle.label | ControlFlowNode for request |
25+
| samples.py:16:25:16:36 | ControlFlowNode for Attribute | semmle.label | ControlFlowNode for Attribute |
26+
| samples.py:16:25:16:46 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() |
27+
| samples.py:20:62:20:71 | ControlFlowNode for user_input | semmle.label | ControlFlowNode for user_input |
28+
subpaths
29+
#select
30+
| samples.py:10:59:10:68 | ControlFlowNode for user_input | samples.py:2:26:2:32 | ControlFlowNode for ImportMember | samples.py:10:59:10:68 | ControlFlowNode for user_input | This $@ processes unsafely $@ and any logical validation in-between could be bypassed using special Unicode characters. | samples.py:10:59:10:68 | ControlFlowNode for user_input | Unicode transformation (Unicode normalization) | samples.py:2:26:2:32 | ControlFlowNode for ImportMember | remote user-controlled data |
31+
| samples.py:20:62:20:71 | ControlFlowNode for user_input | samples.py:2:26:2:32 | ControlFlowNode for ImportMember | samples.py:20:62:20:71 | ControlFlowNode for user_input | This $@ processes unsafely $@ and any logical validation in-between could be bypassed using special Unicode characters. | samples.py:20:62:20:71 | ControlFlowNode for user_input | Unicode transformation (Unicode normalization) | samples.py:2:26:2:32 | ControlFlowNode for ImportMember | remote user-controlled data |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE-176/UnicodeBypassValidation.ql
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unicodedata
2+
from flask import Flask, request, escape, render_template
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/unsafe1")
8+
def unsafe1():
9+
user_input = escape(request.args.get("ui"))
10+
normalized_user_input = unicodedata.normalize("NFKC", user_input) # $result=BAD
11+
return render_template("result.html", normalized_user_input=normalized_user_input)
12+
13+
14+
@app.route("/unsafe2")
15+
def unsafe1bis():
16+
user_input = escape(request.args.get("ui"))
17+
if user_input.isascii():
18+
normalized_user_input = user_input
19+
else:
20+
normalized_user_input = unicodedata.normalize("NFC", user_input) # $result=BAD
21+
return render_template("result.html", normalized_user_input=normalized_user_input)
22+
23+
24+
@app.route("/safe1")
25+
def safe1():
26+
normalized_user_input = unicodedata.normalize(
27+
"NFKC", request.args.get("ui")
28+
) # $result=OK
29+
user_input = escape(normalized_user_input)
30+
return render_template("result.html", normalized_user_input=user_input)

0 commit comments

Comments
 (0)