summaryrefslogtreecommitdiff
path: root/doc/source/features.rst
diff options
context:
space:
mode:
Diffstat (limited to 'doc/source/features.rst')
-rw-r--r--doc/source/features.rst352
1 files changed, 352 insertions, 0 deletions
diff --git a/doc/source/features.rst b/doc/source/features.rst
new file mode 100644
index 0000000..89cd756
--- /dev/null
+++ b/doc/source/features.rst
@@ -0,0 +1,352 @@
1Features
2========
3
4Snuffleupagus has a lot of features that can be divided in two main categories: bug-classes
5killers and virtual-patching. The first category provides primitives to kill various
6bug families (like arbitrary code execution via ``unserialize`` for example) or rise the
7cost of exploitation, the second one is a highly configurable system to patch functions in php itself.
8
9Bug classes killed
10------------------
11
12``system`` injections
13^^^^^^^^^^^^^^^^^^^^^
14
15The ``system`` function execute an external program and displays the output.
16It's used to interract with various external tools, like file-format converters for example.
17Unfortunately, passing user-controlled parameters to it often leads to an arbitrary command execution.
18
19 When allowing user-supplied data to be passed to this function,
20 use `escapeshellarg()` or `escapeshellcmd()` to ensure that users cannot trick
21 the system into executing arbitrary commands.
22
23 --- `The PHP documentation about system <https://secure.php.net/manual/en/function.system.php>`_
24
25We're kind of killing it by filtering the ``$``, ``|``, ``;``, ````` and ``&`` chars in our
26default configuration, making it a lot harder for an attacker to inject arbitrary commands.
27
28This family of vulnerabilities lead to various CVE, like:
29
30- `CVE-2017-7981 <https://tuleap.net/plugins/tracker/?aid=10159>`_: Authenticated remote code execution on Tuleap
31- `CVE-2014-4688 <https://www.pfsense.org/security/advisories/pfSense-SA-14_10.webgui.asc>`_: Authenticated remote code execution on pfSense
32- `CVE-2014-1610 <https://www.rapid7.com/db/modules/exploit/multi/http/mediawiki_thumb>`_: Unauthenticated remote code execution on DokuWiki
33- `CVE-2013-3630 <https://www.rapid7.com/db/modules/exploit/multi/http/moodle_cmd_exec>`_: Authenticated remote code execution on Moodle
34- Every single shitty `modem/router/switch/IoT <https://twitter.com/internetofshit>`_.
35
36
37``mail``-related injections
38^^^^^^^^^^^^^^^^^^^^^^^^^^^
39
40This vulnerability is known `since 2011 <http://esec-pentest.sogeti.com/posts/2011/11/03/using-mail-for-remote-code-execution.html>`_,
41and was popularized by `RIPS <https://www.ripstech.com/blog/2016/roundcube-command-execution-via-email/>`_ in 2016.
42The last flag of the `mail` function can be used to pass various parameters to
43the underlying binary used to send emails: this can lead to an arbitrary file write,
44often meaning an arbitrary code execution.
45
46 The ``additional_parameters`` parameter can be used to pass additional flags
47 as command line options to the program configured to be used when sending mail
48
49 --- `The PHP documentation about mail <https://secure.php.net/manual/en/function.mail.php>`_
50
51We're killing it by preventing any extra options in additional_parameters.
52
53This family of vulnerabilities lead to various CVE, like:
54
55- `CVE-2017-7692 <https://legalhackers.com/advisories/SquirrelMail-Exploit-Remote-Code-Exec-CVE-2017-7692-Vuln.html>`_: Authenticated remote code execution in SquirrelMail
56- `CVE-2016-10074 <https://legalhackers.com/advisories/SwiftMailer-Exploit-Remote-Code-Exec-CVE-2016-10074-Vuln.html>`_: remote code execution in SwiftMailer
57- `CVE-2016-10033 <https://legalhackers.com/advisories/PHPMailer-Exploit-Remote-Code-Exec-CVE-2016-10033-Vuln.html>`_: remote code execution in PHPMailer
58- `CVE-2016-9920 <https://www.ripstech.com/blog/2016/roundcube-command-execution-via-email/>`_: Unauthenticated remote code execution in Roundcube
59
60Session-cookie stealing via XSS
61^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62
63The goto payload for XSS is often to steal cookies.
64Like *Suhosin*, we are encrypting the cookies with a secret key, the IP of the user
65and its user-agent. This means that an attacker with an XSS won't be able to use
66the stolen cookie, since he (often) can't spoof the IP address of the user.
67
68This feature is roughly the same than the `Suhosin one <https://suhosin.org/stories/configuration.html#transparent-encryption-options>`_.
69
70Users behind the same IP address but with different browsers won't be able to use each other stolen cookies,
71except if they can manage to guess the user agent. This isn't especially difficult,
72but an invalid decryption will leave a trace in the logs.
73
74Finally, having a secret server-side key will prevent anyone (even the user himself)
75from reading the content of the cookie, reducing the impact of an application storing sensitive data client-side.
76
77The encryption is done via the [tweetnacl library](https://tweetnacl.cr.yp.to/),
78thus using curve25519, xsalsa20 and poly1305 for the encryption. We chose this
79library because of its portability, simplicity and reduced size (a single `.h` and
80`.c` file.).
81
82Remote code execution via file-upload
83^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
85Some PHP applications allows users to upload contents, like avatars for a forum.
86Unfortunately, sometimes, content validation isn't implemented properly (if at all),
87meaning arbitrary file upload, often leading, contrary to what the documentation is saying,
88to an arbitrary code execution.
89
90 Not validating which file you operate on may mean that users can *access sensitive information* in other directories.
91
92 --- `The PHP documentation about file uploads <https://secure.php.net/manual/en/features.file-upload.common-pitfalls.php>`_
93
94We're killing it, like Suhosin, by automatically calling a script upon file upload,
95if it returns something else than ``0``, the file will be removed (or stored in a quarantine,
96for further analysis).
97
98We're recommending to use the `vld <https://derickrethans.nl/projects.html#vld>`_ project
99inside the script to ensure the file doesn't contain any valid PHP code, with something like this:
100
101::
102
103 $ php -d vld.execute=0 -d vld.active=1 -d extension=vld.so $file
104
105Unserialize-related magic
106^^^^^^^^^^^^^^^^^^^^^^^^^
107
108PHP is able to *serialize* arbitrary objects, to easily store them.
109Unfortunately, it's often possible to gain arbitrary code execution upon deserialization
110of user-supplied serialized objects.
111
112 Do not pass untrusted user input to ``unserialize()`` regardless of the options value of allowed_classes.
113 Unserialization can result in code being loaded and executed due to object instantiation and autoloading,
114 and a malicious user may be able to exploit this.
115
116 --- `The PHP documentation about serialize <https://secure.php.net/manual/en/function.serialize.php>`_
117
118We're killing it by exploiting the fact that PHP will discard any garbage found at the end of a serialized object,
119allowing us to simply append a `HMAC <https://en.wikipedia.org/wiki/Hash-based_message_authentication_code>`_
120at the end of strings generated by the ``serialize``,
121hence guaranteeing that any object deserialized came from the application,
122and wasn't tampered with,
123
124We're not encrypting it, like we do with the cookies,
125allowing this feature to be disabled (or switch into leaning mode)
126without the need to invalidate any data.
127
128.. warning::
129
130 This feature can't be deployed on websites that already stored serialized
131 objects (ie. in database), since they are missing the HMAC, and thus will be detected as
132 an attack. If you're in this situation, you should use this feature with the
133 ``simulation`` mode, and switch it off once you don't have any messages in your
134 logs.
135
136A nice side-effect of this feature is that it'll defeat various memory corruption
137issues related to the complexity of ``unserialize``'s implementation,
138and the amount of control if provides to an attacker, like `CVE-2016-9137, CVE-2016-9138 <https://bugs.php.net/bug.php?id=73147>`_,
139`2016-7124 <https://bugs.php.net/bug.php?id=72663>`_, `CVE-2016-5771 and CVE-2016-5773 <https://www.evonide.com/how-we-broke-php-hacked-pornhub-and-earned-20000-dollar/>`_, …
140
141This family of vulnerabilities lead to various CVE, like:
142
143- `CVE-2016-???? <https://www.computest.nl/advisories/CT-2016-1110_Observium.txt>`_: Unauthenticated remote code execution in Observium (leading to remote root)
144- `CVE-2016-5726 <http://seclists.org/oss-sec/2016/q2/521>`_: Unauthenticated remote code execution in Simple Machines Forums
145- `CVE-2016-4010 <http://netanelrub.in/2016/05/17/magento-unauthenticated-remote-code-execution/>`_: Unauthenticated remote code execution in Magento
146- `CVE-2017-2641 <http://netanelrub.in/2017/03/20/moodle-remote-code-execution/>`_: Unauthenticated remote code execution in Moodle
147- `CVE-2015-8562 <https://www.rapid7.com/db/modules/exploit/multi/http/joomla_http_header_rce>`_: Unauthenticated remote code execution in Joomla
148- `CVE-2015-7808 <https://www.rapid7.com/db/modules/exploit/multi/http/vbulletin_unserialize>`_: Unauthenticated remote code execution in vBulletin
149- `CVE-2014-1691 <http://seclists.org/oss-sec/2014/q1/153>`_: Unauthenticated remote code execution in Horde
150- `CVE-2012-5692 <https://www.rapid7.com/db/modules/exploit/unix/webapp/invision_pboard_unserialize_exec>`_: Unauthenticated remote code execution in IP.Board
151
152
153
154Weak-PRNG via rand/mt_rand
155^^^^^^^^^^^^^^^^^^^^^^^^^^
156
157The functions ``rand`` and ``mt_rand`` are often used to generate random numbers used
158in sensitive context, like password generation, token creation, …
159Unfortunately, as said in the documentation, the quality of their entropy is low,
160leading to the generation of guessable values.
161
162 This function does not generate cryptographically secure values, and should not be used for cryptographic purposes.
163
164 --- `The PHP documentation about rand <https://secure.php.net/manual/en/function.rand.php>`_
165
166We're addressing this issue by replacing every call to ``rand`` and ``mt_rand`` with
167a call to the ``random_int``, a `CSPRNG <https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator>`_.
168
169It's worth noting that the PHP documentation contains the following warning:
170
171 ``min`` ``max`` range must be within the range ``getrandmax()``. i.e. ``(max - min) <= getrandmax()``.
172 Otherwise, ``rand()`` may return poor-quality random numbers.
173
174 --- `The PHP documentation about rand <https://secure.php.net/manual/en/function.rand.php>`_
175
176This is of course addressed as well by the ``harden_rand`` feature.
177
178.. warning::
179
180 Activating this feature will raise an `Error <https://secure.php.net/manual/en/class.error.php>`_
181 exception if ``min`` is superior to ``max``, while the default dehaviour is simply to swap them.
182
183This family of vulnerabilities lead to various CVE, like:
184
185- `CVE-2015-5267 <https://moodle.org/mod/forum/discuss.php?d=320291>`_: Unauthenticated accounts takeover in in Moodle
186- `CVE-2014-9624 <https://www.mantisbt.org/bugs/view.php?id=17984>`_: Captcha bypass in MantisBT
187- `CVE-2014-6412 <https://core.trac.wordpress.org/ticket/28633>`_: Unauthenticated account takeover in Wordpress
188- `CVE-2015-???? <https://hackerone.com/reports/31171>`_: Unauthenticated accounts takeover in Concrete5
189- `CVE-2013-6386 <https://www.drupal.org/SA-CORE-2013-003>`_: Unauthenticated accounts takeover in Drupal
190- `CVE-2010-???? <http://www.sektioneins.com/advisories/advisory-022010-mybb-password-reset-weak-random-numbers-vulnerability.html>`_: Unauthenticated accounts takeover in MyBB
191- `CVE-2008-4102 <https://sektioneins.de/en/advisories/advisory-042008-joomla-weak-random-password-reset-token-vulnerability.html>`_: Unauthenticated accounts takeover in Joomla
192- `CVE-2006-0632 <https://www.cvedetails.com/cve/CVE-2006-0632/>`_: Unauthenticated account takeover in phpBB
193
194XXE
195^^^
196
197Despite the documentation saying nothing about this class of vulnerabilities,
198`XML eXternal Entitiy <https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing>`_ (XXE) are often leading to arbitrary file reading, SSRF, and sometimes even arbitrary
199code execution.
200
201XML documents can contain a `Document Type Definition <https://www.w3.org/TR/REC-xml/#sec-prolog-dtd>`_ (DTD),
202enabling definition of XML entities. It's possible to define an (external) entity by an
203URI, that the parser will access, and embed its content back into the document
204for further processing.
205
206For example, providing an url like ``file:///etc/passwd`` will read
207this file's content, and since it's not valid XML, the application
208will spit it out in an error message, thus leaking its content.
209
210We're killing this class of vulnerabilities by calling
211the `libxml_disable_entity_loader <https://secure.php.net/manual/en/function.libxml-disable-entity-loader.php>`_
212function with its parameter set to ``true`` at startup,
213and then *nop'ing* it, so it won't do anything if ever called again.
214
215This family of vulnerabilities lead to various CVE, like:
216
217- `CVE-2015-5161 <https://legalhackers.com/advisories/eBay-Magento-XXE-Injection-Vulnerability.html>`_: Unauthenticated arbitrary file disclosure on Magento
218- `CVE-2014-8790 <https://github.com/GetSimpleCMS/GetSimpleCMS/issues/944>`_: Unauthenticated remote code execution in GetSimple CMS
219- `CVE-2011-4107 <https://www.phpmyadmin.net/security/PMASA-2011-17/>`_: Authenticated local file disclosure in PHPMyAdmin
220
221
222Cookie stealing via HTTP MITM
223^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
224
225While it's possible to set the ``secure`` flag on cookies to prevent them from being
226transmitted over HTTP, and only allow its transmission over HTTPS.
227Snuffleupagus can automatically set this flag if the client is accessing the
228website over a secure connection.
229
230This behaviour is suggested in the documentation:
231
232 On the server-side, it's on the programmer to send this kind of cookie only
233 on secure connection (e.g. with respect to ``$_SERVER["HTTPS"]``).
234
235 --- `The PHP documentation about setcookie <https://secure.php.net/manual/en/function.setcookie.php>`_
236
237
238Exploitation, post-exploitation and general hardening
239-----------------------------------------------------
240
241Virtual-patching
242^^^^^^^^^^^^^^^^
243
244PHP itself exposes a number of functions that might be considered **dangerous** and that have limited legitimate use cases.
245``system()``, ``exec()``, ``dlopen()`` - for example - fall into this category. By default, PHP only allows to globally disable some functions.
246
247
248However, (ie. ``system()``) they might have legitimate use cases in processes such as self upgrade etc., making it impossible to effectively
249disable them - at the risk of breaking critical features.
250
251SnuffleuPagus allows the user to restrict usage of specific functions per files, or per
252files with a matching (sha256) hash, thus allowing the use of such functions **only** in the intended places.
253
254Furthermore, running the `following script <FIXME>`_ will generate an hash and line-based whitelist
255of dangerous functions, droping them everywhere else:
256
257
258.. literalinclude:: ../../scripts/generate_rules.php
259 :language: php
260
261
262The intent is to make post-exploitation process (such as backdooring of legitimate code, or RAT usage) a lot harder for the attacker.
263
264
265Global strict mode
266^^^^^^^^^^^^^^^^^^
267
268By default, PHP will coerce values of the wrong type into the expected one
269if possible. For example, if a function expecting an integer is given a string,
270it will be coerced in an integer.
271
272PHP7 introduced a **strict mode**, in which variables won't be coerced anymore,
273and a `TypeError <https://php.net/manual/en/class.typeerror.php>`_ exception will
274be raised if the types aren't matching.
275`Scalar type declarations <https://secure.php.net/manual/en/migration70.new-features.php#migration70.new-features.scalar-type-declarations>`_
276are optional, but you don't have to used them in your code to benefit from them,
277since every internal function from php has them.
278
279This option provide a switch to globally activate this strict mode,
280helping to uncover vulnerabilities like the classical
281`strcmp bypass <https://danuxx.blogspot.fr/2013/03/unauthorized-access-bypassing-php-strcmp.html>`_,
282and various other types mismatch.
283
284This feature is largely inspired from the
285`autostrict <https://github.com/krakjoe/autostrict>`_ module from `krakjoe <krakjoe.ninja>`_.
286
287
288Preventing execution of writable PHP files
289^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
290
291If an attacker manages to upload an arbitrary file or to modify an existing one,
292odds are that (thanks to the default `umask <https://en.wikipedia.org/wiki/Umask>`_)
293this file is writable by the PHP process.
294
295Snuffleupagus can prevent the execution of this kind of files. A good practise
296would be to use a different user to run PHP than for administrating the website,
297and using this feature to lock this up.
298
299
300
301Dumping capabilities
302^^^^^^^^^^^^^^^^^^^^
303It's possible to apply the ``dump(:str)`` filter to any virtual-patching rule,
304to dump the complete web request, along with the filename and the corresponding
305line number. By using the *right* set of restrictive rules (or by using the
306*overly* restrictives ones in ``simulation`` mode), you might be able
307to gather interesting vulnerabilities used against your website.
308
309
310Misc low-hanging fruits in the default configuration file
311^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
312
313Snuffleupagus is shipping with a default configuration file, containing
314various examples and ideas of things that you might want to enable (or not).
315
316Available functions recon
317"""""""""""""""""""""""""
318
319After compromising a website, most of the time, the attacker does some recon
320within its webshell, to check which functions are available to execute arbitrary code,
321since it's not uncommon for some web-hoster to disable things like ``system`` or ``passthru``,
322or to check if mitigations are enabled, like ``open_basedir``.
323This behaviour can be detected by preventing the execution of functions like ``ini_get``
324or ``is_callable`` with *suspicious* parameters.
325
326``chmod`` hardening
327"""""""""""""""""""
328
329Some PHP applications are using broad rights when using the ``chmod`` function,
330like the infamous ``chmod(777)`` command, effectively making the file writable by everyone.
331Snuffleupagus is preventing this kind of behaviour by restricting the parameters
332than can be passer to ``chmod``.
333
334Arbitrary file inclusion hardening
335""""""""""""""""""""""""""""""""""
336
337Arbitrary file inclusion is a common vulnerability, that might be detected
338by preventing the use of anything else than a whitelist of extensions in calls
339to ``include`` or ``require``.
340
341*Cheap* SQL injections detection
342""""""""""""""""""""""""""""""""
343
344In some SQL injections, attackers might need to use comments, a feature that is
345often not used in production system, so it might be a good idea to filter
346queries that contains some. The same filtering idea can be used against
347SQL functions that are frequently used in SQL injections, like ``sleep``, ``benchmark``
348or strings like ``version_info``.
349
350Still about SQL injections, if a function performing a query returns ``FALSE``
351(indicating an error), it might be useful to dump the request for further analysis.
352