简体   繁体   English

如何防止来自php的远程文件包含攻击?

[英]How do i Prevent remote file inclusion attack from php?

This is my code in index.php 这是我在index.php中的代码

include ($_GET['page']);

Actully i need to include page from url like 实际上,我需要包含来自url的页面,例如

"?page=go.php"

on the other-hand i can not filter 另一方面,我无法过滤

"?page=example.com"

as for some case i need to include this value also. 对于某些情况,我还需要包括此值。 But this is a remote file inclusion (RFI) vulnerability. 但这是一个远程文件包含(RFI)漏洞。 how can i prevent RFI attack from my site? 如何防止我的网站遭受RFI攻击? I am doing something like 我正在做类似的事情

$filename = $_GET['page'];
if (file_exists($filename)) {
{
include ($_GET['page']);
}

But it filters only 但是它只过滤

"?page=go.php"

this shorts of page. 这页的短裤。 And i am sucked with 和我很烂

"?page=example.com"

this shorts of page. 这页的短裤。

If I understand the question correctly; 如果我正确理解了这个问题; You could setup an array with 'allowed' pages such as: 您可以使用“允许的”页面设置数组,例如:

  $allowedPages = array('go.php', 'stop.php', 'file.php');
  $filename = $_GET['page'];

  if(in_array($filename, $allowedPages) && file_exists($filename)){
    include ($filename);
  }else{
    //output error
  }

I think this answer is too late, but for those who might search for this problem, I guess it can be done like this, too: 我认为这个答案为时已晚,但是对于那些可能会搜索此问题的人,我想也可以这样做:

1.Define a constant with full path. 1,定义一个完整路径的常数

2.Define a white-list of allowed pages. 2.定义允许页面的白名单。

3.Get the $_GET variable, and convert it to lower case. 3.获取$ _GET变量,并将其转换为小写。

4.Then if the page returned by $_GET variable is in your white-list array, then require it, otherwise, redirect the user to the home page, and display an error message. 4.然后,如果$ _GET变量返回的页面在您的白名单数组中,则需要它,否则,将用户重定向到主页,并显示错误消息。

<?php
# this is abcd.php
define('SITE','http://www.yoursite.com/');
$allow = [SITE.'1.php', SITE.'2.php', SITE.'3.php'];
$get = strtolower(SITE.$_GET['page']);

if(in_array($get,$allow)){
    include $get;
} else {
    header('Location: index.php?param=incorrect');
}
?>

<?php
# this is index.php
if(isset($_GET['param']) && $_GET['param'] == 'incorrect'){
?>
    <script type="text/javascript">
        alert("INCORRECT PARAMETER PROVIDED");
    </script>
<?php
} else die('ERROR');

I'm not 100% sure this will work without any probs, but I guess is a good start and you can play around with it and figure out what's suitable for you. 我不是100%确信没有任何问题就能解决问题,但是我想这是一个好的开始,您可以尝试一下,找出适合自己的方法。

To be honest, your method of creating a dynamic website is definitely not the way to go. 老实说,您创建dynamic网站的方法绝对不是走的路。

To answer within the scope of this question, you'd do something like the following: 要在此问题的范围内回答,您将执行以下操作:

You'd have to set up a whitelist of files that are**ALLOWED** to be included through this function. 您必须设置**允许**的文件whitelist ,才能通过此功能包含在内。

That could look something like this: 可能看起来像这样:

<?php 

$whitelist = array(
    'file1.php',
    'file2.php',
    'file3.php',
    'file4.php',
    'file5.php',
);

?>

Now before including the said file, you'd run a check with in_array() 现在,在包含所述文件之前,您将使用in_array()运行检查

<?php 

if(in_array($_GET['page'] . '.php', $whitelist) && file_exists($_GET['page'] . '.php')) {
    include($_GET['page'] . '.php');
}

?>



This, as you can see is not very pretty! 如您所见,这不是很漂亮! Another alternative would be doing something like: 另一种选择是做类似的事情:

    <?php 
    $file = strtolower($_GET['page']) . '.php';
    if(isset($whitelist[$file]) && file_exists($file)) {
        include($_GET['page'] . '.php');
    }

    ?>

Don't accept a page.php parameter, but just the name of the file. 不接受page.php参数,仅接受文件名。

"?page=go"

Then check if $_REQUEST["page"] is alphanumeric 然后检查$ _REQUEST [“ page”]是否为字母数字

if (ctype_alnum($_REQUEST["page"]))

And just don't give non-alpha-numeric names to your files. 而且,不要给文件使用非字母数字名称。

Then do if file_exists on $_REQUEST["page"] and you should be quite good. 然后,如果file_exists位于$ _REQUEST [“ page”]上,您应该会很好。

PS $_REQUEST["page"] is about the same with $_GET["page"], it just intersects with $_POST PS $ _REQUEST [“ page”]与$ _GET [“ page”]大致相同,只是与$ _POST相交

You can filter other domain urls instead of filtering files. 您可以过滤其他域网址,而不是过滤文件。 If parameter contains a hostname less than 3 letters, it is fine as no domain is 2 letters. 如果参数包含的主机名少于3个字母,则可以正常使用,因为没有域是2个字母。

   $tmp= parse_url ($_GET['page']);
   if(strlen($tmp['host'])<3)
    include($_GET['page']);

If there are some trusted hosts then you can validate them too. 如果有一些受信任的主机,那么您也可以对其进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM