Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [MOD] Updating CleanXSS  (Read 2770 times)

centered

  • Guest
[MOD] Updating CleanXSS
« on: June 06, 2007, 01:34:22 pm »

Seeing in a thread about low-medium security risks using 1.4 with XSS cross site scripting, I thought why not update it?  With 1.6's XSS function?  If you view the code for 1.6, a cleanXSS call is almost everywhere, so why not put it in 1.4 for the 'rare' users.

I haven't really tested this fully, but I pulled alot from this site: http://ha.ckers.org/xss.html and everything tested fine.  Though I must admit, I did not test against a virgin snews unmodded...


0. Backup

1. Find the original CleannXSS function
Code: [Select]
// CLEAN - XSS stuff clean.

function cleanXSS($text) {

$allowedtags = '<b><i>
<a><ul><li><pre><hr><blockquote><img>';

$notallowedattribs = array("@javascript:|onclick|ondblclick|onmousedown|onmouseup"

."|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup@si");

$changexssto = '';

$text = str_replace("\n", "
", $text);

$text = preg_replace($notallowedattribs,$changexssto,$text);

$text = strip_tags($text,$allowedtags);

//Clean words if asked.

$text = cleanWords($text);

return $text;

}
Replace with 1.6's function
Code: [Select]
// XSS CLEAN
function cleanXSS($val) {

# source from http://quickwired.com/kallahar/smallprojects/php_xss_filter_function.php

$val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);

$search = 'abcdefghijklmnopqrstuvwxyz';

$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

$search .= '1234567890!@#$%^&*()';

$search .= '~`";:?+/={}[]-_|\'\\';

for ($i = 0; $i < strlen($search); $i++) {

$val = preg_replace('/(�[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val);

$val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val);

}

$ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');

$ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');

$ra = array_merge($ra1, $ra2);

$found = true;

while ($found == true) {

$val_before = $val;

for ($i = 0; $i < sizeof($ra); $i++) {

$pattern = '/';

for ($j = 0; $j < strlen($ra[$i]); $j++) {

if ($j > 0) {

$pattern .= '(';

$pattern .= '(�[x|X]0{0,8}([9][a][b]);?)?';

$pattern .= '|(�{0,8}([9][10][13]);?)?';

$pattern .= ')?';

}

$pattern .= $ra[$i][$j];

}

$pattern .= '/i';

$replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);

$val = preg_replace($pattern, $replacement, $val);

if ($val_before == $val) {$found = false;}

}

}

$allowedtags = "<strong><em><ul><li><pre><hr><blockquote><span>";

$cstring = strip_tags($val, $allowedtags);

$cstring = nl2br($cstring);

return $cstring;

}



// CLEAN - WORD FILTER

function cleanWords($text) {

if ((strtolower(s('word_filter_enable')) == 'yes') AND (file_exists(s('word_filter_file')))) {

$bad_words_from_what = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", file(s('word_filter_file')));

$bad_words_from_what = preg_replace('/^(.*)$/', '/\\1/i', $bad_words_from_what);

echo $test[0];

$bad_words_to_what = s('word_filter_change');

$text = preg_replace($bad_words_from_what, $bad_words_to_what, $text);

return $text;

} else {

return $text;

}}
2.  Now to add the cleaning

In check if unique find:
Code: [Select]
case 'article_seftitle':

$sql = "SELECT id FROM ".s('prefix')."articles WHERE seftitle = '".clean($text)."' AND id != '".$not_id."'";

break;

case 'article_title':

$sql = "SELECT id FROM ".s('prefix')."articles WHERE title = '".clean($text)."' AND id != '".$not_id."'";

break;

case 'category_seftitle':

$sql = "SELECT id FROM ".s('prefix')."categories WHERE seftitle = '".clean($text)."' AND id != '".$not_id."'";

break;

case 'category_name':

$sql = "SELECT id FROM ".s('prefix')."categories WHERE name = '".clean($text)."' AND id != '".$not_id."'";

break;

};
Replace with:
Code: [Select]
case 'article_seftitle':

$sql = "SELECT id FROM ".s('prefix')."articles WHERE seftitle = '".clean(cleanXSS($text))."' AND id != '".$not_id."'";

break;

case 'article_title':

$sql = "SELECT id FROM ".s('prefix')."articles WHERE title = '".clean(cleanXSS($text))."' AND id != '".$not_id."'";

break;

case 'category_seftitle':

$sql = "SELECT id FROM ".s('prefix')."categories WHERE seftitle = '".clean(cleanXSS($text))."' AND id != '".$not_id."'";

break;

case 'category_name':

$sql = "SELECT id FROM ".s('prefix')."categories WHERE name = '".clean(cleanXSS($text))."' AND id != '".$not_id."'";

break;

};
3.  In get_id
Code: [Select]
$url = explode("/", clean($_GET['category']));Replace with
Code: [Select]
$url = explode("/", clean(cleanXSS($_GET['category'])));4. In contact find:
Code: [Select]
if (strlen(clean($_POST['name'])) > 1 AND strlen(clean($_POST['message'])) > 1) {replace with:
Code: [Select]
if (strlen(clean(cleanXSS($_POST['name']))) > 1 AND strlen(clean(cleanXSS($_POST['message']))) > 1 AND audit()) {5. In the search engine find:
Code: [Select]
$search_query = clean($_POST['search_query']);Replace with
Code: [Select]
$search_query = clean(cleanXSS($_POST['search_query']));6. In function view_categories()
Code: [Select]
    <p><input type="text" name="seftitle" value="<? if ($_POST['name'] == '') { echo cleanSEF($_POST['name']); } else { echo cleanSEF($_POST['seftitle']); }; ?>" id="article_sef" class="field" /></p>Replace with:
Code: [Select]
    <p><input type="text" name="seftitle" value="<? if ($_POST['name'] == '') { echo cleanSEF(cleanXSS($_POST['name'])); } else { echo cleanSEF(cleanXSS($_POST['seftitle'])); }; ?>" id="article_sef" class="field" /></p>7.  In New article
Code: [Select]
<input name="seftitle" id="article_sef" type="text" class="field" value="<? echo cleanSEF($_SESSION['temp']['seftitle']); ?>" />Replace with
Code: [Select]
<input name="seftitle" id="article_sef" type="text" class="field" value="<? echo cleanSEF(cleanXSS($_SESSION['temp']['seftitle'])); ?>" />8.  In edit article
Code: [Select]
<input type="text" name="seftitle" id="article_sef" class="field" value="<?php echo $_SESSION['temp']['seftitle'] ? cleanSEF($_SESSION['temp']['seftitle']) : $r['seftitle']; ?>" />Replace with
Code: [Select]
<input type="text" name="seftitle" id="article_sef" class="field" value="<?php echo $_SESSION['temp']['seftitle'] ? cleanSEF(cleanXSS($_SESSION['temp']['seftitle'])) : $r['seftitle']; ?>" />9. In function processing() (3 instances)
Code: [Select]
else if (cleancheckSEF($seftitle) == "notok")Change to
Code: [Select]
else if (cleancheckSEF(cleanXSS($seftitle)) == "notok")And your done...
« Last Edit: September 11, 2007, 12:52:53 pm by centered effect »
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
[MOD] Updating CleanXSS
« Reply #1 on: June 06, 2007, 01:38:09 pm »

Great addition, Centered... thanks. Most valuable member is a price that comes to mind when reading these kinds of posts... I might just have to start a sNews dude thread that takes care of this on a monthly basis ;)
Logged
"It's only dead fish that goes with the flow... "
Updated

centered

  • Guest
[MOD] Updating CleanXSS
« Reply #2 on: June 06, 2007, 01:43:55 pm »

Thanks Patric!
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
[MOD] Updating CleanXSS
« Reply #3 on: June 06, 2007, 02:49:08 pm »

@ Centered:
This mod is excellent as it answers the only known 1.4 security question. You noted that you have not tested it on a default 1.4 install. Can we assume that your mod instructions are accurate enough to perform on a default 1.4 package? The only reason I ask is that I continue to make a 1.4 default package available for download on snews.ca... and there may be another Dude or 2 that does this as well... so if this does pass the testing phase with flying colors, I would definitely want to add it to my 1.4 default download package. :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

centered

  • Guest
[MOD] Updating CleanXSS
« Reply #4 on: June 06, 2007, 03:15:31 pm »

Yes it is accurate enough.

I have not test the modded version against a default install with the XSS 'bug' if you will.  I don't know exactly what hack or injection was done for the security to be comprimised in 1.4, so I don't really know how to accuratly test the cleanXSS in the default against the modded.  

But I did try and do mulitple searches, added comments, added in the titlebar with the codes that was provided in the XSS cheat sheet here: http://ha.ckers.org/xss.html and the 1.6 XSS function rejected everything.
Logged

centered

  • Guest
[MOD] Updating CleanXSS
« Reply #5 on: June 06, 2007, 03:21:22 pm »

Following the XSS cheat sheet, I did the first 2 scripts in the search button.

There seems to be hundreds of XSS scripts, no time to try them all

XSS locator. Inject this string, and in most cases where a script is vulnerable with no special XSS vector requirements the word "XSS" will pop up. Use the URL encoding calculator below to encode the entire string. Tip: if you're in a rush and need to quickly check a page, often times injecting the depreciated "<PLAINTEXT>" tag will be enough to check to see if something is vulnerable to XSS by messing up the output appreciably:

Result
[img=http://img524.imageshack.us/img524/8774/screenshotia2.th.png]
As far as I saw, everything was clean

XSS locator 2. If you don't have much space and know there is no vulnerable JavaScript on the page, this string is a nice compact XSS injection check. View source after injecting it and look for <XSS verses <XSS to see if it is vulnerable:

Result
[img=http://img528.imageshack.us/img528/1039/screenshot1vy6.th.png]
Again, As far as I saw, everything was clean
« Last Edit: August 25, 2007, 03:19:55 am by Phil Martin (Philmoz) »
Logged