Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于用户输入重写应用程序_Php_Security_Validation_Code Injection - Fatal编程技术网

Php 基于用户输入重写应用程序

Php 基于用户输入重写应用程序,php,security,validation,code-injection,Php,Security,Validation,Code Injection,谢谢你阅读这篇文章。 我关心web环境中的安全防御,我在输入验证方面做了很多工作 这里的上下文是一个web环境,用户在其中键入用户名和密码,然后通过POST表单将其传递到文件verify.php 我有下面的代码,我想在将用户输入放入应用程序之前验证它。但我的问题是:是否有可能从这个用户的输入中注入代码,从而实际重写应用程序本身 skip to "REAL QUESTION HERE" if you don't have much time. /* I have left some questi

谢谢你阅读这篇文章。 我关心web环境中的安全防御,我在输入验证方面做了很多工作

这里的上下文是一个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()事件中实现吗?

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

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

在这个senario中,您的代码可能会被注入有害代码。假设攻击者可以找到一种方法将恶意文件上载到您的服务器的“viewsfolder”中。然后,他可以轻松调用此文件,并通过将文件名作为$\u GET参数(view)传递给php代码使其执行

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


如果您使用正则表达式对输入进行清理,以防止出现诸如“]'、“}'、\”等不需要的代码,我认为您不会遇到任何问题。同时请记住,您必须对输入进行转义,以防止sql注入—以防您的数据与某些sql DBMS一起使用(如果您使用mysqli连接器,也可以使用mysqli_real_escape_字符串,或者使用PDO的quote函数,或者使用PDO prepare语句,也可以轻松地完成此操作)。

对于一个问题来说可能有点多,但仍然是一个很好的问题谢谢!这回答了我的问题