Jump to content
MakeWebGames

A way to secure [img] tag's


Dayo

Recommended Posts

This is a small simple way to secure tags on forums or just about anywhere you use bbcode

		function img($code) {
  $code_treated = htmlspecialchars($code);
     $bbcode = array(1 => '/\[img\](.+?)\[\/img\]/e');
       $html = array(1 => '"<img src=\'".secureimg("$1")."\' alt=\'User submitted image\' id=\"img\" title=\'User submitted image\' />"');
      ksort($bbcode);
       ksort($html);
     //preg_replace to convert all remaining bbCode tags
       $post_bbcode_treated = preg_replace($bbcode, $html, $code_treated);
	return nl2br($post_bbcode_treated);
	}

function secureimg($img) {
$img=str_replace(array('/.', '&'), array('.', '&'), $img);

$img2=@getimagesize($img);
if (@is_array($img2)) {
return $img;
} else {
return 'images/broken.gif';
}
}

now all you do is wrap img() around the text and ur done

Link to comment
Share on other sites

Can someone please show me how that code will be placed in the bb parser.

<?php
if($_SERVER['PHP_SELF'] == __FILE__) { header("Location:index.php"); exit; }
class bbcode {
function bbcode_parse($text) {
global $ir, $db, $userid, $set;
$text = htmlspecialchars($text);
$text = preg_replace("/\[b\]/", "[b]", $text);
$text = preg_replace("/\[\/b\]/", "[/b]", $text);
$text = preg_replace("/\[i\]/", "[i]", $text);
$text = preg_replace("/\[\/i\]/", "[/i]", $text);
$text = preg_replace("/\[u\]/", "<span style='text-decoration:underline;'>", $text);
$text = preg_replace("/\[\/u\]/", "</span>", $text);
$text = preg_replace("/\[s\]/", "<s>", $text);
$text = preg_replace("/\[\/s\]/", "</s>", $text);
$text = preg_replace("/\[sub\](.+?)\[\/sub\]/", "<sub>$1</sub>", $text);
$text = preg_replace("/\[sup\](.+?)\[\/sup\]/", "<sup>$1</sup>", $text);
$text = preg_replace("/\[big\](.+?)\[\/big\]/", "<big>$1</big>", $text);
$text = preg_replace("/\[small\](.+?)\[\/small\]/", "[size="1"]$1[/size]", $text);
$text = preg_replace("/\[list\](.+?)\[\/list\]/", "<ul>$1[/list]", $text);
$text = preg_replace("/\[olist\](.+?)\[\/olist\]/", "[list=1]$1[/list]", $text);
$text = preg_replace("/\[li\](.+?)\[\/li\]/", "[*]$1", $text);
$text = preg_replace("/\[font=(.+?)\](.+?)\[\/font\]/", "<span style='font-family:$1'>$2</span>", $text);
$text = preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/", "<font size='$1'>$2</font>", $text);
$text = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/", "[url='$1']$2[/url]", $text);
$text = preg_replace("/\[img=(.+?)\]/", "[img=$1]", $text);
$text = preg_replace("/\[img\](.+?)\[\/img\]/", "[img=$1]", $text);
$text = preg_replace("/\n/", "&nbrlb;", $text);
$text = preg_replace("/\[email=(.+?)\](.+?)\[\/email\]/", "[email='$1']$2[/email]", $text);
$text = preg_replace("/\[email\](.+?)\[\/email\]/", "[email='$1']$1[/email]", $text);
$text = preg_replace("/\[left\](.+?)\[\/left\]/", "<div style='text-align: left;'>$1</div>", $text);
$text = preg_replace("/\[center\](.+?)\[\/center\]/", "<div style='text-align: center;'>$1</div>", $text);
$text = preg_replace("/\[right\](.+?)\[\/right\]/", "<div style='text-align: right;'>$1</div>", $text);
$text = preg_replace("/\[quote name='(.+?)\'](.+?)\[\/quote\]/", "<div class='quotetop'>Quote($1)</div><div class='quotemain'>$2</div>", $text);
$text = preg_replace("/\[quote\](.+?)\[\/quote\]/", "<div class='quotetop'>Quote</div><div class='quotemain'>$1</div>", $text);
$text = preg_replace("/\[style=(.+?)\](.+?)\[\/style\]/", "<div style='$1'>$2</div>", $text);
$text = preg_replace("/\[quote\](.+?)\[\/quote\]/","<div class='quotetop'>Quote</div><div class='quotemain'>$1</div>", $text);
$text = preg_replace("/\[code\](.+?)\[\/code\]/","<div class='codetop'>Code</div><div class='codemain'><code>$1</code></div>", $text);
$text = preg_replace("/\[codebox\](.+?)\[\/codebox\]/","<div class='codetop'>Code</div><div class='codemain'><code>$1</code></div>", $text);
$text = preg_replace("/&nbrlb;/", "
\n", $text);
$text = preg_replace("/\[colour=(.+?)\]/", "<span style='color:$1;'>", $text);
$text = preg_replace("/\[\/colour\]/", "</span>", $text);
$text = preg_replace("/\[color=(.+?)\]/", "<span style='color:$1;'>", $text);
$text = preg_replace("/\[\/color\]/", "</span>", $text);
$text = preg_replace("/\[br \/]/", "
", $text);
$text = preg_replace("/\[br]/", "
", $text);
$text = preg_replace("/\[hr \/]/", "<hr />", $text);
$text = preg_replace("/\[hr]/", "<hr />", $text);
$text = preg_replace("/\[list\]/", "<ul>", $text);
$text = preg_replace("/\[\/list\]/", "[/list]", $text);
$text = preg_replace("/\[olist\]/", "[list=1]", $text);
$text = preg_replace("/\[\/olist\]/", "[/list]", $text);
$text = preg_replace("/\[li\]/", "[*]", $text);
$text = preg_replace("/\[\/li\]/", "", $text);
return nl2br($text);

}
}

$bbc = new BBCode;
?> 
Link to comment
Share on other sites

<?php
if($_SERVER['PHP_SELF'] == __FILE__) { header("Location:index.php"); exit; }
class bbcode {

function secureimg($img) {
$img=str_replace(array('/.', '&'), array('.', '&'), $img);

$img2=@getimagesize($img);
if (@is_array($img2)) {
return $img;
} else {
return 'images/broken.gif';
}
}



function bbcode_parse($text) {
global $ir, $db, $userid, $set;
$text = htmlspecialchars($text);
$text = preg_replace("/\[b\]/", "[b]", $text);
$text = preg_replace("/\[\/b\]/", "[/b]", $text);
$text = preg_replace("/\[i\]/", "[i]", $text);
$text = preg_replace("/\[\/i\]/", "[/i]", $text);
$text = preg_replace("/\[u\]/", "<span style='text-decoration:underline;'>", $text);
$text = preg_replace("/\[\/u\]/", "</span>", $text);
$text = preg_replace("/\[s\]/", "<s>", $text);
$text = preg_replace("/\[\/s\]/", "</s>", $text);
$text = preg_replace("/\[sub\](.+?)\[\/sub\]/", "<sub>$1</sub>", $text);
$text = preg_replace("/\[sup\](.+?)\[\/sup\]/", "<sup>$1</sup>", $text);
$text = preg_replace("/\[big\](.+?)\[\/big\]/", "<big>$1</big>", $text);
$text = preg_replace("/\[small\](.+?)\[\/small\]/", "[size="1"]$1[/size]", $text);
$text = preg_replace("/\[list\](.+?)\[\/list\]/", "<ul>$1[/list]", $text);
$text = preg_replace("/\[olist\](.+?)\[\/olist\]/", "[list=1]$1[/list]", $text);
$text = preg_replace("/\[li\](.+?)\[\/li\]/", "[*]$1", $text);
$text = preg_replace("/\[font=(.+?)\](.+?)\[\/font\]/", "<span style='font-family:$1'>$2</span>", $text);
$text = preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/", "<font size='$1'>$2</font>", $text);
$text = preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/", "[url='$1']$2[/url]", $text);
$text = preg_replace("/\n/", "&nbrlb;", $text);
$text = preg_replace("/\[email=(.+?)\](.+?)\[\/email\]/", "[email='$1']$2[/email]", $text);
$text = preg_replace("/\[email\](.+?)\[\/email\]/", "[email='$1']$1[/email]", $text);
$text = preg_replace("/\[left\](.+?)\[\/left\]/", "<div style='text-align: left;'>$1</div>", $text);
$text = preg_replace("/\[center\](.+?)\[\/center\]/", "<div style='text-align: center;'>$1</div>", $text);
$text = preg_replace("/\[right\](.+?)\[\/right\]/", "<div style='text-align: right;'>$1</div>", $text);
$text = preg_replace("/\[quote name='(.+?)\'](.+?)\[\/quote\]/", "<div class='quotetop'>Quote($1)</div><div class='quotemain'>$2</div>", $text);
$text = preg_replace("/\[quote\](.+?)\[\/quote\]/", "<div class='quotetop'>Quote</div><div class='quotemain'>$1</div>", $text);
$text = preg_replace("/\[style=(.+?)\](.+?)\[\/style\]/", "<div style='$1'>$2</div>", $text);
$text = preg_replace("/\[quote\](.+?)\[\/quote\]/","<div class='quotetop'>Quote</div><div class='quotemain'>$1</div>", $text);
$text = preg_replace("/\[code\](.+?)\[\/code\]/","<div class='codetop'>Code</div><div class='codemain'><code>$1</code></div>", $text);
$text = preg_replace("/\[codebox\](.+?)\[\/codebox\]/","<div class='codetop'>Code</div><div class='codemain'><code>$1</code></div>", $text);
$text = preg_replace("/&nbrlb;/", "
\n", $text);
$text = preg_replace("/\[colour=(.+?)\]/", "<span style='color:$1;'>", $text);
$text = preg_replace("/\[\/colour\]/", "</span>", $text);
$text = preg_replace("/\[color=(.+?)\]/", "<span style='color:$1;'>", $text);
$text = preg_replace("/\[\/color\]/", "</span>", $text);
$text = preg_replace("/\[br \/]/", "
", $text);
$text = preg_replace("/\[br]/", "
", $text);
$text = preg_replace("/\[hr \/]/", "<hr />", $text);
$text = preg_replace("/\[hr]/", "<hr />", $text);
$text = preg_replace("/\[list\]/", "<ul>", $text);
$text = preg_replace("/\[\/list\]/", "[/list]", $text);
$text = preg_replace("/\[olist\]/", "[list=1]", $text);
$text = preg_replace("/\[\/olist\]/", "[/list]", $text);
$text = preg_replace("/\[li\]/", "[*]", $text);
$text = preg_replace("/\[\/li\]/", "", $text);


     $bbcode = array(1 => '/\[img\](.+?)\[\/img\]/e');
       $html = array(1 => '"<img src=\'".$this->secureimg("$1")."\' alt=\'User submitted image\' id=\"img\" title=\'User submitted image\' />"');
      ksort($bbcode);
       ksort($html);
     //preg_replace to convert all remaining bbCode tags
       $text = preg_replace($bbcode, $html, $text);



return nl2br($text);

}
}

$bbc = new BBCode;
?>

this is untested

Link to comment
Share on other sites

Despite the sense of security, i must say that this will fail, mainly if you are going to deal with GIF images. Arbitrary code can be placed inside the image, still making a valid image file and it will get executed as soon as the file is called. You ought to consider allowing users to upload their own images and then locking with .htaccess the execution of any code from within the upload directory, with something along these lines...

Order allow,deny
allow from all

<FilesMatch "\.php$">
deny from all
</FilesMatch>

 

The given example goes to php, yet you can expand it for whatsoever your host allows to execute!

Link to comment
Share on other sites

Despite the sense of security, i must say that this will fail, mainly if you are going to deal with GIF images. Arbitrary code can be placed inside the image, still making a valid image file and it will get executed as soon as the file is called. You ought to consider allowing users to upload their own images and then locking with .htaccess the execution of any code from within the upload directory, with something along these lines...
Order allow,deny
allow from all

<FilesMatch "\.php$">
deny from all
</FilesMatch>

 

The given example goes to php, yet you can expand it for whatsoever your host allows to execute!

Then you check the image before you display it, to see if it contains php inside. simple.

Link to comment
Share on other sites

Despite the sense of security, i must say that this will fail, mainly if you are going to deal with GIF images. Arbitrary code can be placed inside the image, still making a valid image file and it will get executed as soon as the file is called. You ought to consider allowing users to upload their own images and then locking with .htaccess the execution of any code from within the upload directory, with something along these lines...
Order allow,deny
allow from all

<FilesMatch "\.php$">
deny from all
</FilesMatch>

 

The given example goes to php, yet you can expand it for whatsoever your host allows to execute!

Thats actually how i secure my directory...that and other methods as well.

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...