ねえ、私はgameserverの広告を提出するウェブサイトを作っています。テキストボックスの内容からPHP関連のタグを取り除くことが可能かどうか疑問に思っていますか?あたかもそれが今のままになっているかのように、あとでセキュリティ上のリスクになる可能性があります。現在、私がコンテンツに行っているのはnl2br()だけです。これを行う最善の方法は何ですか?PHP - テキストボックス内でPHPコンテンツを取り除く?
ありがとうございました。
ねえ、私はgameserverの広告を提出するウェブサイトを作っています。テキストボックスの内容からPHP関連のタグを取り除くことが可能かどうか疑問に思っていますか?あたかもそれが今のままになっているかのように、あとでセキュリティ上のリスクになる可能性があります。現在、私がコンテンツに行っているのはnl2br()だけです。これを行う最善の方法は何ですか?PHP - テキストボックス内でPHPコンテンツを取り除く?
ありがとうございました。
、解決策:
<?php
//here is content from the textarea (filled it for example)
$content = 'some <?php echo "test"; ?> <?=test?> content <br/> here';
$content = preg_replace('/<\?((?!\?>).)*\?>/s', '', $content); //strip all the php code
私は彼らが提出する情報がPHPが実行される別のページに表示されるので、phpについて心配しています。しかし、strip_tagsが仕事をするように助けてくれてありがとう。 –
送信された文字列で 'eval()'を呼び出さない限り、PHPコードは実行されません。 @DuncanPalmer ..あなたの最初の懸念は本当にHTML/JS(この回答が扱う)でなければなりません。私が考えることができる他の奇妙なコーナーのケースは、ユーザー入力をファイルに書き込んだ後に 'include()'することですが、それでも最初にサニタイズするだけです。 –
序文は:これはPHPタグのすべての
まずのみに関連していない、あなたはそこで許可された文字であり、されていないかを決定しなければなりません。できるだけこれらを制限するようにしてください(あなたはregexを使って確認できます)。
次に、XSSから保護してください。以下 は、(例として)このために使用されるコードの一部です:あなたはテキストボックスにPHPコードを気にしないのはなぜ
public function clean_xss($str, $charset = 'ISO-8859-1') {
/*
* Remove Null Characters
*
* This prevents sandwiching null characters
* between ascii characters, like Java\0script.
*
*/
$str = preg_replace('/\0+/', '', $str);
$str = preg_replace('/(\\\\0)+/', '', $str);
/*
* Validate standard character entities
*
* Add a semicolon if missing. We do this to enable
* the conversion of entities to ASCII later.
*
*/
$str = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"\\1;",$str);
/*
* Validate UTF16 two byte encoding (x00)
*
* Just as above, adds a semicolon if missing.
*
*/
$str = preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"\\1\\2;",$str);
/*
* URL Decode
*
* Just in case stuff like this is submitted:
*
* <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
*
* Note: Normally urldecode() would be easier but it removes plus signs
*
*/
$str = preg_replace("/%u0([a-z0-9]{3})/i", "&#x\\1;", $str);
$str = preg_replace("/%([a-z0-9]{2})/i", "&#x\\1;", $str);
/*
* Convert character entities to ASCII
*
* This permits our tests below to work reliably.
* We only convert entities that are within tags since
* these are the ones that will pose security problems.
*
*/
if (preg_match_all("/<(.+?)>/si", $str, $matches)) {
for ($i = 0; $i < count($matches['0']); $i++) {
$str = str_replace($matches['1'][$i],
html_entity_decode($matches['1'][$i], ENT_COMPAT, $charset), $str);
}
}
/*
* Convert all tabs to spaces
*
* This prevents strings like this: ja vascript
* Note: we deal with spaces between characters later.
*
*/
$str = preg_replace("#\t+#", " ", $str);
/*
* Makes PHP tags safe
*
* Note: XML tags are inadvertently replaced too:
*
* <?xml
*
* But it doesn't seem to pose a problem.
*
*/
$str = str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str);
/*
* Compact any exploded words
*
* This corrects words like: j a v a s c r i p t
* These words are compacted back to their correct state.
*
*/
$words = array('javascript', 'vbscript', 'script', 'applet', 'alert', 'document', 'write', 'cookie', 'window');
foreach ($words as $word) {
$temp = '';
for ($i = 0; $i < strlen($word); $i++) {
$temp .= substr($word, $i, 1)."\s*";
}
$temp = substr($temp, 0, -3);
$str = preg_replace('#'.$temp.'#s', $word, $str);
$str = preg_replace('#'.ucfirst($temp).'#s', ucfirst($word), $str);
}
/*
* Remove disallowed Javascript in links or img tags
*/
$str = preg_replace("#<a.+?href=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>.*?</a>#si", "", $str);
$str = preg_replace("#<img.+?src=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>#si","", $str);
$str = preg_replace("#<(script|xss).*?\>#si", "", $str);
/*
* Remove JavaScript Event Handlers
*
* Note: This code is a little blunt. It removes
* the event handler and anything up to the closing >,
* but it's unlikely to be a problem.
*
*/
$str = preg_replace('#(<[^>]+.*?)(onblur|onchange|onclick|onfocus|onload|onmouseover|onmouseup|onmousedown|onselect|onsubmit|onunload|onkeypress|onkeydown|onkeyup|onresize)[^>]*>#iU',"\\1>",$str);
/*
* Sanitize naughty HTML elements
*
* If a tag containing any of the words in the list
* below is found, the tag gets converted to entities.
*
* So this: <blink>
* Becomes: <blink>
*
*/
$str = preg_replace('#<(/*\s*)(alert|applet|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|xml|xss)([^>]*)>#is', "<\\1\\2\\3>", $str);
/*
* Sanitize naughty scripting elements
*
* Similar to above, only instead of looking for
* tags it looks for PHP and JavaScript commands
* that are disallowed. Rather than removing the
* code, it simply converts the parenthesis to entities
* rendering the code un-executable.
*
* For example: eval('some code')
* Becomes: eval('some code')
*
*/
$str = preg_replace('#(alert|cmd|passthru|eval|exec|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $str);
/*
* Final clean up
*
* This adds a bit of extra precaution in case
* something got through the above filters
*
*/
$bad = array(
'document.cookie' => '',
'document.write' => '',
'window.location' => '',
"javascript\s*:" => '',
"Redirect\s+302" => '',
'<!--' => '<!--',
'-->' => '-->'
);
foreach ($bad as $key => $val) {
$str = preg_replace("#".$key."#i", $val, $str);
}
return $str;
}
うわー、ここで悪い答えがたくさん。
ユーザーがPHPコードを入力する心配はありません。文字列に格納して表示すると、決して実行されません。あなたはあなたの道から出て、そのためにeval
を使用する必要があります。あなた自身で試すことができます:
$code = '<?php echo "hi"; ?>';
echo $code;
これは何もしません。
ただし、HTMLについて心配する必要があります。
$code = '<script>alert("hi");</script>';
echo $code;
「hi」と動作し、警告します。これを防ぐには、htmlspecialchars
と表示する前に、ユーザーから取得したすべてのものを消毒する必要があります。
$code = '<script>alert("hi");</script>';
echo htmlspecialchars($code);
Here is a live example、ここではa more complete answer on sanitizationです。
は正確にPHPコードのための3つの方法が実行され得るためにあります:
<?php
$txt ="<" . "?php echo 'Hi mom!' ?" . ">";
echo $txt
魔法ブラウザの唾をすることはありません:PHPスクリプト
eval()
include()
/require()
を通じてPHPコードのようなものが持つを含むファイルをPHPコードが含まれているいくつかのテキストを渡します"こんにちはお母さん!" PHPコード自体が吐き出されます。
次のように上記のコードは、ファイルと出力に入れた場合:
$txt = file_get_contents('file_with_the_hi_mom_code.php');
echo $txt;
それはまた、実行されませんでした - ユーザーはいくつかの生のPHPコードが自分の画面に表示が表示されます。今
、あなたが行う場合:
include('file_with_the_hi_mom_code.php');
または
eval (file_get_contents('file_with_the_hi_mom_code.php'));
を、コードが実行されます。
私はあなたが誤ってそこに言いましたと思います。 –
私はあなたがそこで何をしたかを見ます**。ありがとう。 –
PHP *タグ*またはPHP *関数呼び出し文字列*を意味しますか? – BenM
あなたのコードを投稿してください... –
[PHP:究極のクリーン/セキュア機能]の複製可能(http://stackoverflow.com/questions/4223980/php-the-ultimate-clean-secure-function) –