Guest User

html cleaner

a guest
Jul 17th, 2011
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2. $s = '<table width="300" border="0" cellspacing="0" cellpadding="0" calo="s" onlclick="alert(\'hack\');">
  3. <tr>
  4. <th scope="row">&nbsp;</th>
  5. <td>а</td>
  6. <td>о</td>
  7. </tr>
  8. <tr>
  9. <th onClick="alert(\'fff\');" scope="row">а</th>
  10. <td>&nbsp;</td>
  11. <td>о</td>
  12. </tr>
  13. <tr>
  14. <th scope="row">&nbsp;</th>
  15. <td>п</td>
  16. <td>р</td>
  17. </tr>
  18. </table>
  19. ddddddddddddd
  20.  
  21. <a href=javascript: { {({}{}()())}alert(\'xss\') ) ) }>
  22.  
  23. <div onload..;,;..\'alert(\"xss_attack\");\'>
  24.  
  25. <a href=\'javascript:{ alert(\"xss_attack\"); otherxss();}\'
  26. onclick= \'alert(\"xss_attack\");\' onhover=\'alert
  27. (\"xss_attack\");\' onmouseout=alert(\"xss_attack\")
  28. class=\'thisclass\'> link</a>
  29. ';
  30.  
  31. $s = remove_HTML(strip_javascript($s), 'table|td|th|tr|width|border|cellspacing|cellpadding|scope|row|align|center|right|left');
  32.  
  33. print "<textarea>{$s}</textarea>";
  34.  
  35. function strip_javascript($filter){
  36.  
  37.     // realign javascript href to onclick
  38.     $filter = preg_replace("/href=(['\"]).*?javascript:(.*)?\\1/i", "onclick=' $2 '", $filter);
  39.  
  40.     //remove javascript from tags
  41.     while( preg_match("/<(.*)?javascript.*?\(.*?((?>[^()]+)|(?R)).*?\)?\)(.*)?>/i", $filter))
  42.         $filter = preg_replace("/<(.*)?javascript.*?\(.*?((?>[^()]+)|(?R)).*?\)?\)(.*)?>/i", "<$1$3$4$5>", $filter);
  43.            
  44.     // dump expressions from contibuted content
  45.     if(0) $filter = preg_replace("/:expression\(.*?((?>[^(.*?)]+)|(?R)).*?\)\)/i", "", $filter);
  46.  
  47.     while( preg_match("/<(.*)?:expr.*?\(.*?((?>[^()]+)|(?R)).*?\)?\)(.*)?>/i", $filter))
  48.         $filter = preg_replace("/<(.*)?:expr.*?\(.*?((?>[^()]+)|(?R)).*?\)?\)(.*)?>/i", "<$1$3$4$5>", $filter);
  49.        
  50.     // remove all on* events  
  51.     while( preg_match("/<(.*)?\s?on.+?=?\s?.+?(['\"]).*?\\2\s?(.*)?>/i", $filter) )
  52.        $filter = preg_replace("/<(.*)?\s?on.+?=?\s?.+?(['\"]).*?\\2\s?(.*)?>/i", "<$1$3>", $filter);
  53.  
  54.     return $filter;
  55. }
  56.  
  57.     function remove_HTML($s , $keep = '' , $expand = 'script|style|noframes|select|option'){
  58.         /**///prep the string
  59.         $s = ' ' . $s;
  60.        
  61.         /**///initialize keep tag logic
  62.         if(strlen($keep) > 0){
  63.             $k = explode('|',$keep);
  64.             for($i=0;$i<count($k);$i++){
  65.                 $s = str_replace('<' . $k[$i],'[{(' . $k[$i],$s);
  66.                 $s = str_replace('</' . $k[$i],'[{(/' . $k[$i],$s);
  67.             }
  68.         }
  69.        
  70.         //begin removal
  71.         /**///remove comment blocks
  72.         while(stripos($s,'<!--') > 0){
  73.             $pos[1] = stripos($s,'<!--');
  74.             $pos[2] = stripos($s,'-->', $pos[1]);
  75.             $len[1] = $pos[2] - $pos[1] + 3;
  76.             $x = substr($s,$pos[1],$len[1]);
  77.             $s = str_replace($x,'',$s);
  78.         }
  79.        
  80.         /**///remove tags with content between them
  81.         if(strlen($expand) > 0){
  82.             $e = explode('|',$expand);
  83.             for($i=0;$i<count($e);$i++){
  84.                 while(stripos($s,'<' . $e[$i]) > 0){
  85.                     $len[1] = strlen('<' . $e[$i]);
  86.                     $pos[1] = stripos($s,'<' . $e[$i]);
  87.                     $pos[2] = stripos($s,$e[$i] . '>', $pos[1] + $len[1]);
  88.                     $len[2] = $pos[2] - $pos[1] + $len[1];
  89.                     $x = substr($s,$pos[1],$len[2]);
  90.                     $s = str_replace($x,'',$s);
  91.                 }
  92.             }
  93.         }
  94.        
  95.         /**///remove remaining tags
  96.         while(stripos($s,'<') > 0){
  97.             $pos[1] = stripos($s,'<');
  98.             $pos[2] = stripos($s,'>', $pos[1]);
  99.             $len[1] = $pos[2] - $pos[1] + 1;
  100.             $x = substr($s,$pos[1],$len[1]);
  101.             $s = str_replace($x,'',$s);
  102.         }
  103.        
  104.         /**///finalize keep tag
  105.         for($i=0;$i<count($k);$i++){
  106.             $s = str_replace('[{(' . $k[$i],'<' . $k[$i],$s);
  107.             $s = str_replace('[{(/' . $k[$i],'</' . $k[$i],$s);
  108.         }
  109.        
  110.         return trim($s);
  111.     }
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment