-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescaping.php
More file actions
126 lines (80 loc) · 4.02 KB
/
Copy pathescaping.php
File metadata and controls
126 lines (80 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
//--------------------------------------------------
// No Escaping
$_GET['id'] = 'id';
$sql = 'WHERE id = ' . $_GET['id'];
//--------------------------------------------------
// Bad Escaping
$_GET['id'] = 'id';
$sql = 'WHERE id = ' . $mysqli->real_escape_string($_GET['id']);
//--------------------------------------------------
// Bad Escaping
// Intended HTML usage:
// <input type="checkbox" name="delete[]" value="1" />
// <input type="checkbox" name="delete[]" value="3" />
$_POST['delete'] = ['1) OR (1 = 1'];
$sql = 'DELETE FROM table WHERE id IN (' . implode(',', $_POST['delete']) . ')'; // INSECURE
// While array_map('intval') might fix this instance today, who knows who will copy/edit it in the future.
// Instead, just use parameterised queries:
//
// Levi Morrison = https://stackoverflow.com/a/23641033/538216
// PDO Execute = https://www.php.net/manual/en/pdostatement.execute.php#example-1012
// Drupal Multiple Arguments = https://www.drupal.org/docs/7/security/writing-secure-code/database-access#s-multiple-arguments
//--------------------------------------------------
// Possible config issue
// If 'sql_mode' includes NO_BACKSLASH_ESCAPES
$_GET['id'] = '2" or "1"="1';
$sql = 'SELECT 1 FROM user WHERE id = "' . $mysqli->escape_string($_GET['id']) . '"';
//--------------------------------------------------
// Possible config issue
// If 'SET NAMES latin1' has been used, and escape_string() continues to use 'utf8'.
$_GET['name'] = 'Zoë';
$sql = 'INSERT INTO user (name) VALUES ("' . $mysqli->escape_string($_GET['name']) . '")';
//--------------------------------------------------
// No Escaping
$_GET['url'] = '/ onerror=alert(1)';
$html = '<img src=' . $_GET['url'] . ' alt="" />';
//--------------------------------------------------
// Missing Quotes
$_GET['url'] = '/ onerror=alert(1)';
$html = "<img src=" . htmlentities($_GET['url']) . " alt='' />";
//--------------------------------------------------
// Bad Escaping
// htmlentities() doesn't encode single quotes by default.
// Before PHP 8.1
// https://github.com/php/php-src/commit/50eca61f68815005f3b0f808578cc1ce3b4297f0
// Java, Apache Commons
// https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringEscapeUtils.html#escapeHtml4(java.lang.String)
// "Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported"
$_GET['url'] = "/' onerror='alert(1)";
$html = "<img src='" . htmlentities($_GET['url']) . "' alt='' />";
//--------------------------------------------------
// Context Issue
$_GET['url'] = 'javascript:alert(1);';
$html = '<a href="' . htmlentities($_GET['url']) . '">Link</a>';
//--------------------------------------------------
// Context Issue
// The browsers HTML parser is not aware of escaped JavaScript strings.
$_GET['url'] = '</script><script>alert(1)</script>';
$html = '<script> var url = "' . addslashes($_GET['url']) . '"; </script>';
//--------------------------------------------------
// Incomplete Escaping (ref urlencode)
$_GET['name'] = 'A&B';
$html = '<a href="./?name=' . htmlentities($_GET['name']) . '">Link</a>';
//--------------------------------------------------
// Encoding
// PHP just assumes the string is UTF-8 (since 5.4).
// Without a charset declaration, the browser will also guess at the encoding.
// For example, this classic UTF-7 value for Internet Explorer.
$_GET['value'] = '+ADw-script+AD4-alert(1)+ADw-+AC8-script+AD4-';
header('Content-Type: text/html; charset=');
header('X-Content-Type-Options: -');
$html = '<p>' . htmlentities($_GET['value']) . '</p>';
//--------------------------------------------------
// General weirdness
$_GET['email'] = 'b@example.com -X/www/example.php';
$parameters = '-f' . $_GET['email'];
// $parameters = '-f' . escapeshellarg($_GET['email']);
mail('a@example.com', 'Subject', 'Message', NULL, $parameters);
// It's not possible to safely escape values in $additional_parameters for mail()
?>