2014-05-10 27 views
3

感谢您阅读本文。 我很担心网络环境中的安全防御问题,并且我在输入验证方面做了很多工作。根据用户输入重写应用程序

这里的背景是网络环境下的用户名和密码的用户类型,它们通过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()事件?

+0

也许适合1个问题,但仍然不错 –

回答

2

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

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

在此塞纳里奥您的代码可能有有害的代码注入。假设攻击者可以找到一种方法将恶意文件上传到服务器的“viewsfolder”,然后他可以轻松地调用该文件,并通过传递$ _GET参数(查看)名称来使其在您的php代码中执行

另一个例子,如果你使用PHP函数执行一个字符串作为php代码,那么它会更有害,例如,你可以接收用户的输入,并将它与其他你已经以字符串格式存储了代码,然后通过eval()函数调用它,这可能最终导致代码注入

我不认为如果你用你的输入清理输入,你会遇到某种问题正则表达式以防止unwan像'''','}',''字符。另外请记住,为了防止sql注入,您必须转义输入 - 以防将数据用于某些SQL DBMS(这也可以通过使用mysqli连接器或使用PDO的报价函数或通过使用PDO准备语句)。

+0

谢谢!这回答了我的问题 – RockDrew