728x90

SQL 인젝션 공격을 방지하기 위한 로그인 함수 사용예이다.

공격코드로 사용하는 특수문자를 전부 공백으로 대체시켜 버리도록 했다.


function UserAuthCheck($userID,$passwd) {
    if(!isset($userID) || !isset($passwd) || empty($userID) || empty($passwd)) {
        return 0;
    } else {
        global $DB_CONNECT;           
        $userID = preg_replace("/[\s\t\'\;\"\=\-\-]+/","", $userID); // 공백이나 탭 제거(사용자 실수 방지)
        $passwd = preg_replace("/[\s\t\'\;\"\=\-\-]+/","", $passwd); // 공백이나 탭 제거, 특수문자 제거(",',;,=,--)
        // SQL injection 검사
        $userID = htmlentities($userID); // <script>documnet.cookie();</script> 공격 방지, 한글인식 불가
        $passwd = htmlentities($passwd); // < 를 \< 로 바꿔준다.
        //$userID = str_replace(array("'",""","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $userID);
        if(preg_match('/(and|null|where|limit)/i', $userID)) { $this->popup('비정상적 접근입니다');} // i는 대소문자 구별하지 말라
        if(!preg_match('/^[0-9a-zA-Z\~\!\@\#\$\%\^\&\*\(\)]{7,}$/',$passwd)) { // 최소7자리 이상 허용 문자만 통과
            $this->popup('정보가 올바르지 않습니다');
        }

        $sql = "select code, id ";
        $sql.= "from memberdata where pw=md5('".$passwd."') and id= '".$userID."' ";
        if($result = mysqli_query($DB_CONNECT,$sql)) { //성공
            $row = mysqli_fetch_array($result);
            if($row == NULL) $this->popup('정보가 올바르지 않습니다');
                return $row;
            } else {
                $this->popup('정보가 올바르지 않습니다');
        }
    }
}

블로그 이미지

Link2Me

,