【问题标题】:Rewriting application based on user input根据用户输入重写应用程序
【发布时间】:2014-05-10 05:58:27
【问题描述】:

感谢您阅读本文。 我担心 Web 环境中的安全防御,并且在输入验证方面遇到了很多困难。

这里的上下文是一个网络环境,用户输入用户名和密码,通过 POST 表单传递到文件 verify.php

我有以下代码,我想在让用户输入进入应用程序之前验证用户输入。但我的问题是:是否可以从这个用户的输入中注入代码,实际上会重写应用程序本身?

skip to "REAL QUESTION HERE" if you don't have much time.

/* I have left some questions along the code in commentary, 
if you feel you can answer these it would be greatly appreciated too. */ 

所有这些安全概念都在融化我的大脑。

<?php

//Only sources provided by the server are allowed?

/*is it relevant to use this because I read that
 you have to use webkits for cross-platform performences 
and that they are not supported in many cases.  */
Content-Security-Policy: 
default-src 'self';
script-src 'self';
style-src 'self';
connect-src 'self';
media-src 'self';
object-src 'self';
frame-src 'self'

//prevent javascript from accessing cookies?
//I will only use $_SESSION variables.

ini_set('session.cookie_httponly', 1 );
ini_set('session.cookie_secure', 1 );
session_start();
session_regenerate_id(); /*is it mandatory if I plan to use a generated
                         random value using strong entropy to make 
                         a login token?*/

include('functions.php');//weak point?

//for further validation use
$usernameSafe = '';
$passwordSafe = '';

//check if the values exists.
if (isset($_POST['username']) && isset($_POST['password'])) {

    //validation of user input before letting it access the app. 
    //weak point?
    if (isValidInput($_POST['username']) && isValidInput($_POST['password'])) { 

        $formUn = $_POST['username'];//weak point?
        $formPw = $_POST['password'];//here too?

    }

}


//@@@@@@@@@@@@@ REAL QUESTION HERE @@@@@@@@@@@@@@
/*BYPASS_IDEA the input could be : 

    validUserName']) || 1 == 1 ) { inject php code here } }/* 

    //1 == 1 is used to bypass the condition.

    /*two bracket to close the two 'if' statements involved
    and end the app' then also escape evrything after it 
    with a block commentary char left unclosed */


//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//from the php file 'functions.php'
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function isValidInput($string) { 

    $isValid = FALSE;

    if ($string && is_string($string)) {

        /*can it protect against xss attack using characters
        like &lt or any coding tags? */
        if (mb_detect_encoding($string, 'UTF-8', TRUE) != FALSE  &&
        ctype_alnum($string)) {

            //Is this how buffer attacks are prevented?
            if (strlen($string) <= INPUT_LENGHT) 
                $isValid = TRUE;

        }

    }

    return $isValid;

}

?>

最终,如果可能的话,这可以在 isset() 事件中实现吗?

【问题讨论】:

  • 对于 1 个问题可能有点过分,但仍然是一个不错的问题

标签: php security validation code-injection


【解决方案1】:

根据我的经验,防止任何类型的代码注入攻击(sql、php 等)的最重要因素是对用户数据的清理。当您收到输入数据时,使用特定规则对它们进行清理(这可以通过 php preg_match 函数轻松实现)。 这样,如果输入被认为是有害的,您就可以拒绝它。此外,当您收到 $_GET 或 $_POST 变量时,它不会被视为代码,除非您以这种方式明确使用它。 例如,让我们考虑以下情景:

<?php
  if ( isset( $ _GET['view'] ) ) 
  {
    include( "viewsfolder/" . $ _GET['view'] . ".php" );
  } 
?> 

在这种情况下,您的代码可能会被注入有害代码。假设攻击者可以找到一种方法将恶意文件上传到您的服务器到“viewsfolder”。然后他可以轻松地调用此文件并使其在您的 php 代码中执行,只需将其名称作为 $_GET 参数(视图)传递文件。

另一个更有害的例子是,如果您使用 php eval() 函数,它将字符串作为 php 代码执行。例如,您可以接收用户的输入,将其与您以字符串格式存储的其他代码连接起来,然后通过 eval() 函数调用它。这可能最终导致代码注入。

我认为,如果您使用正则表达式清理输入以防止不需要的代码(如 ']'、'}'、\' 字符),那么您不会遇到某种问题。还要记住,您必须转义输入以防止 sql 注入 - 如果您将数据与某些 SQL DBMS 一起使用(这也可以通过 mysqli_real_escape_string 轻松完成 - 如果您使用 mysqli连接器 - 或使用 PDO 的 quote 函数或使用 PDO 准备语句)。

【讨论】:

  • 谢谢!这回答了我的问题
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
相关资源
最近更新 更多