안녕하세요, 저는 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 태그 모든
먼저에만 관련이 없습니다, 당신은 거기에 허용되는 문자, 그리고되지 않는 결정해야합니다. 가능한 한 많이 제한하십시오 (정규식을 사용하여 확인할 수 있습니다).
그런 다음 XSS로부터 보호하십시오. 아래 은 (예를 들어)이 사용되는 코드 조각입니다 :
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 코드에 대해 신경 이유는 무엇와우, 많이.
사용자가 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 가지 방법이 실행하세요이있어 :
eval()
include()
/require()
PHP 코드가 포함 된 파일을 통해 PHP 코드를 포함하는 텍스트를 통과 같은 데:
<?php
$txt ="<" . "?php echo 'Hi mom!' ?" . ">";
echo $txt
마술 브라우저 침을하지 않습니다를 "안녕 엄마!". 그것은 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) –