2026, 새로운 도약을 시작합니다.

sql_query() 함수에서 union 사용하기 채택완료

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

답변 3개

채택된 답변
+20 포인트
Copy


// 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("$sql" . mysqli_errno($link) . " : " .  mysqli_error($link) . "error file : {$_SERVER['SCRIPT_NAME']}");

        } else {

            $result = @mysqli_query($link, $sql);

        }

    } else {

        if ($error) {

            $result = @mysql_query($sql, $link) or die("$sql" . mysql_errno() . " : " .  mysql_error() . "error file : {$_SERVER['SCRIPT_NAME']}");

        } else {

            $result = @mysql_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);
주석 처리해 보세요.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

FROM a, b

이런식으로 선택하시거나

join을 활용하거나

서브쿼리를 활용할 수 있습니다

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고