sql_query() 함수에서 union 사용하기

sql_query() 함수에서 union 사용하기

QA

sql_query() 함수에서 union 사용하기

본문

보안상 이유로 sql_query 함수에서 union을 사용하지 못하는 걸로 아는데 이걸 사용하는 방법 혹은 대체로 사용할 수 있는 비슷한 기능이 있나요??

이 질문에 댓글 쓰기 :

답변 3


// sql_query()함수는 union이 사용이 불가능하여 새로운 함수로 대처 (gnuwiz)
function union_sql_query($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null)
{
    global $g5;
    if(!$link)
        $link = $g5['connect_db'];
    // Blind SQL Injection 취약점 해결
    $sql = trim($sql);
    $sql = preg_replace("#^select.*from.*where.*`?information_schema`?.*#i", "select 1", $sql);
    if(function_exists('mysqli_query') && G5_MYSQLI_USE) {
        if ($error) {
            $result = @mysqli_query($link, $sql) or die("<p>$sql<p>" . mysqli_errno($link) . " : " .  mysqli_error($link) . "<p>error file : {$_SERVER['SCRIPT_NAME']}");
        } else {
            $result = @mysqli_query($link, $sql);
        }
    } else {
        if ($error) {
            $result = _query($sql, $link) or die("<p>$sql<p>" . mysql_errno() . " : " .  mysql_error() . "<p>error file : {$_SERVER['SCRIPT_NAME']}");
        } else {
            $result = _query($sql, $link);
        }
    }
    return $result;
}

 

함수 만들어서 사용하세요.

function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null)
{
    global $g5, $g5_debug;

    if(!$link)
        $link = $g5['connect_db'];

    // Blind SQL Injection 취약점 해결
    $sql = trim($sql);
    // union의 사용을 허락하지 않습니다.
    //$sql = preg_replace("#^select.*from.*union.*#i", "select 1", $sql);
    $sql = preg_replace("#^select.*from.*[\s\(]+union[\s\)]+.*#i ", "select 1", $sql);
    // `information_schema` DB로의 접근을 허락하지 않습니다.
    $sql = preg_replace("#^select.*from.*where.*`?information_schema`?.*#i", "select 1", $sql);
주석 처리해 보세요.

답변을 작성하시기 전에 로그인 해주세요.
전체 425
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT