2016-08-14 4 views
1

PHP에 익숙하지 않고 다양한 기술, 문제 및 수정 방법을 찾을 수 있도록 웹 응용 프로그램 개발을하고 있습니다.PHP 폼 유효성 검사 및 XSS 보안

이제 등록 양식을 만들고 양식을 확인하고 SQL 인젝션 및 XSS로부터 양식을 보호하고 있습니다. 참고 나는 준비된 성명서를 사용할 수 있음을 이해하지만, 나의 기술 수준에서 Mysqli 절차에서 시작하는 것이 자신감을 충분히 채울 때까지 내 발달에 가장 좋은 결과라고 생각한다.

그래서 저는 전문가에게 당신이 제거하거나 추가하거나 사용할 필요가있는 것이 있는지 (stmt를 제외하고)보고 싶습니다.

내 등록 페이지입니다.

<?php 
    // define mqsqli real escape string function 
    function _olaskee($escape) { 
     $escape = htmlspecialchars ($escape, ENT_QUOTES, 'UTF-8'); 
     $escape = trim ($escape, ENT_QUOTES, 'UTF-8'); 
     $escape = stripcslashes ($escape, ENT_QUOTES, 'UTF-8'); 
     return $escape; 

    } 
    // start session 
    session_start(); 

    // include database connection 
    //require_once('include/connection.php'); 

    // if user type already detected, redirect to index.php 
    if(isset($_SESSION['user_type'])){ 
     header('Location: index.php'); 
    } 

    // check if we have submited/if the for as being submitted 
    if(!empty($_POST['submit'])){ 

     //instantiate 
     $firstname = _olaskee($con, $_POST['firstname']); 
     $lastname = _olaskee($con, $_POST['lastname']); 
     $user_name = _olaskee($con, $_POST['user_name']); 
     $user_type = _olaskee($con, $_POST['user_type']); 
     $password = _olaskee($con, $_POST['password']); 
     $confirm_password = _olaskee($con, $_POST['confirm_password']); 

      // hash password 
     $hashed_password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]); 

     // include database connection 
     require_once('include/errMsg.php'); 

    } 
    // include page title 
    $title = 'Registration Page'; 


    // include header layout 
    require_once('include/header.php'); 
    ?> 

    <div> 

     <form name="register" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); ?>" method="post"> 
     <table> 
     <tr> 
      <td>First Name</td> 
      <td><input type="text" name="firstname" value='<?php// echo htmlspecialchars ($firstname) ?>'><br><span style='color: red'><?php echo $fnErr ?></span></td> 
      <?php echo $firstname ; ?> 
     </tr> 
     <tr> 
      <td>Last Name</td> 
      <td><input type="text" name="lastname" value='<?php echo htmlspecialchars ($lastname) ?>'><br><span style='color: red'><?php echo $lnErr ?></span></td> 
     </tr> 
     <tr> 
      <td>User Name</td> 
      <td><input type="text" name="user_name" value='<?php echo htmlspecialchars ($user_name) ?>'><br><span style='color: red'><?php echo $unameErr ?></span></td> 
     </tr> 
     <tr> 

      <td>User Type</td> 
      <td> 
    <!-- <label for="flavor">Select User Type:</label > --> 

    <select id="user_type" name='user_type' > 
     <option value="">Select User Type</option> 
     <option <?php echo $user_type=='rsw'?'selected':''; ?> >rsw</option> 
     <option <?php echo $user_type=='sp'?'selected':''; ?> >sp</option> 
    </select> 
     <span style='color: red'><?php echo $u_typeErr?></span> 
      </td> 
     </tr> 

     <tr> 
      <td>Email</td> 
      <td><input type="email" name="email" value='<?php echo htmlspecialchars ($email) ?>'><br /><span style='color: red'><?php echo $emailErr ?></span></td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="password" name="password" id="password"><br /><span style='color: red'><?php echo $passErr ?></span></td></td> 
     </tr> 
     <tr> 
      <td>Confirm Password:</td> 
      <td><input type="password" name="confirm_password" id="confirm_password"><br /><span style='color: red'><?php echo $cpassErr ?></span></td></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td><input type="submit" name="submit" value="Register"><a href='index.php'> Login</a></td> 
     </tr> 
     </table> 
     </form> 
    </div> 

    <?php 
    if(is_file('include/footer.php')) 
    include_once('include/footer.php'); 
    ?> 

그리고 여기이 두 페이지에 몇 줄을 주석 처리 한 내 오류 메시지 페이지

<?php 

    // error handler variable 
    $fnErr = $lnErr = $unameErr = $u_typeErr = $emailErr = $passErr = $cpassErr = ''; 
    $firstname = $lastname = $user_name = $user_type = $email = $password = $confirm_password = ''; 

      // if submit, then validate 
     $firstname = ($_POST['firstname']); 
      // set field validation for first name 
      if (empty($firstname)){  
      $fnErr = 'Field empty, please enter your first name';   
      }else{ 
        if (strlen($firstname) < 3){ 
          $fnErr = 'First Name is too short'; 
        } 
      } 
       // check if name only contains letters and whitespace 
         if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { 
         $fnErr = "Only letters and white space allowed"; 
      } 


      // set field validation for last name 
     $lastname = ($_POST['lastname']); 
      if (empty($lastname)){  
      $lnErr = 'Field empty, please enter your last name';   
      }else{ 
        if (strlen($lastname) < 3){ 
          $lnErr = 'Last Name is too short'; 
        } 
      } 
       // check if name only contains letters and whitespace 
         if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { 
         $lnErr = "Only letters and white space allowed"; 
      } 

      // set field validation for user name 
      $user_name = ($_POST['user_name']); 
      if (empty($user_name)){  
      $unameErr = 'Field empty, please enter user name';   
      }else{ 
         if (strlen($user_name) < 6){ 
          $unameErr = 'Password is too short'; 
        }else{ 

         if (strlen($user_name) > 15){ 
          $unameErr = 'Password is too long';     
          } 
        } 
       } 
      // check if name only contains letters and whitespace 
        if (!preg_match("#.*^(?=.*[a-z])(?=.*[A-Z]).*$#",$user_name)) { 
        $unameErr = "At least one CAPS, letters and white space allow"; 
      } 


      // check if user select user type from list 
      $user_type = ($_POST['user_type']); 
        if (empty($user_type)){  
        $u_typeErr = 'Please select user type from list';   
        } 



     // set email filter validation 
      $email = ($_POST['email']); 
      if (empty($email)){  
       $emailErr = 'Field empty, please enter your last name';   
      }else{ 
        // check if e-mail address is well-formed 
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
         $emailErr = "Invalid email format"; 
        } 
      } 


      // set field validation for password 
      $password = ($_POST['password']); 
      if (empty($password)){  
      $passErr = 'Field empty, please create a password';   
      }else{ 
         if (strlen($password) < 6){ 
          $passErr = 'Password is too short'; 
        }else{ 

         if (strlen($password) > 15){ 
          $passErr = 'Password is too long';     
          }       
        }          
       } 
         if(!preg_match("#[A-Z]+#", $password)) { 
          $passErr = "Password must include at least one CAPS! "; 
        }else{ 

        if(!preg_match("#[0-9]+#", $password)) { 
          $passErr = "Password must include at least one NUMBER! "; 
         } 
        } 
    // //    // check if name only contains letters and whitespace 
    //    if (preg_match("#.*^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $password)) { 
    //     $passErr = "Try again... Password must contain NUMBER, LETTER and CAPS"; 
    //    } 


       // set field validation for confirm password 
     $confirm_password = ($_POST['confirm_password']); 
      if (empty($confirm_password)){  
      $cpassErr = 'Field empty, please confirm your password';   
      }else{ 
        if ($password != $confirm_password) { 
         $cpassErr = 'Error... Passwords do not match'; 
        } 
      }  


    // // define mqsqli real escape string function 
    // function _olaskee($escape) { 
    // $escape = htmlspecialchars ($escape, ENT_QUOTES, 'UTF-8'); 
    // $escape = trim ($escape, ENT_QUOTES, 'UTF-8'); 
    // $escape = stripcslashes ($escape, ENT_QUOTES, 'UTF-8'); 
    // return $escape; 

    // } 

    ?> 

참고입니다.

또한 등록 페이지에는 세션 상단에 보안 기능이 포함되어 있는지 확실하지 않습니다.

또한 암호 해시를 사용했다,하지만 난 아직 데이터베이스에서 테스트하지 않은,하지만 (내가 바로 사용이?)

단지 모양과 나에게 전문가의 의견을주십시오

최고 감사합니다

답변

0

저는 전문가는 아니지만 약간의 메모를드립니다. 당신의 살균 기능 _olaskee에서, 나는 당신이 이러한 기능을하고

  • 현재 stripcslashes 필요하지 않습니다 사용하는 않는 방법을 이해할 필요가 있다고 생각. 이 함수는 왜 여기에 슬래시를 제거합니까?

  • 암호를 삭제하지 않아도됩니다. 당신은 그것을 사용하기 전에 그것을 해싱 할 것이고 해싱은 주입 된 코드를 대체 할 것입니다.

  • SQL 인젝션에 대한 위생 처리를 위해서는 mysqli_real_escape_string을 사용해야합니다. 위생 문자열을 처리 할 것입니다.

  • filter_var 기능을 살펴보십시오. 입력 내용을 살균하고 유효성을 검증하는 데 매우 유용합니다. 이 기능을 사용하면, 지정된 길이에 대한 유효성을 검사 할 수 특정 입력에 약간의 HTML 태그를 (텍스트 영역 등) 등

당신이 공격이 어떻게 만들어 지는지 먼저 알 필요가 공격으로부터 자신을 보호하는 방법을 이해하기 위해 할 수 있습니다 . SQL Injection에 대해 읽고 취약한 코드를 통해 데이터베이스를 해킹 할 수 있는지 확인하십시오.

ZAP 도구를 사용해 볼 수도 있습니다.사이트의 URL을 전달하면 자동 검사를 사용할 수 있으며 웹 응용 프로그램을 자동으로 검색하여 발견 된 모든 취약점을보고합니다.

로그인 시스템을 만드는 방법을 배우는 것이 좋습니다. 하지만 실제 응용 프로그램의 경우 자체 로그인 시스템을 만드는 것이 좋습니다. 테스트되고 승인 된 소프트웨어에 항상 의지하거나 취약점으로 가득 찬 시스템을 만들 것입니다. 행운을 빕니다!

+0

당신의 의견을 주셔서 감사합니다 ... mysqli 실제 이스케이프 문자열을 사용하여 양식을 살균하는 방법을 생각했습니다 ... – olaskee

+0

처음부터 똑같이 만든 소프트웨어를 사용하는 것은 큰 팬이 아닙니다. 소프트웨어를 사용하고 제 취향에 맞게 사용자 정의하려는 경우 디자인 개념을 이해하지 못할 수도 있습니다. – olaskee

+0

ZAP 도구와 필터 var를 살펴 보겠습니다. 도움을 주셔서 감사합니다 ... 나는 다른 사람들이 이것에 대한 다른 견해를 가지고 있는지 보게 될 것입니다. – olaskee