From d09b992df52288d0cbdbe9eb96fd9a39d92e759f Mon Sep 17 00:00:00 2001 From: chicpro Date: Thu, 24 Apr 2014 11:02:03 +0900 Subject: [PATCH 1/2] =?UTF-8?q?sql=5Fquery=20=ED=95=A8=EC=88=98=EC=97=90?= =?UTF-8?q?=20sql=20resource=20=EC=A7=80=EC=A0=95=20-=20=EB=AA=85=EB=9E=91?= =?UTF-8?q?=ED=8F=90=EC=9D=B8=EB=8B=98=20=EC=A0=9C=EC=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.php | 10 +++++++--- config.php | 2 +- lib/common.lib.php | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/common.php b/common.php index e7b8c9785..99765e440 100644 --- a/common.php +++ b/common.php @@ -64,9 +64,13 @@ if (file_exists($dbconfig_file)) { $connect_db = sql_connect(G5_MYSQL_HOST, G5_MYSQL_USER, G5_MYSQL_PASSWORD) or die('MySQL Connect Error!!!'); $select_db = sql_select_db(G5_MYSQL_DB, $connect_db) or die('MySQL DB Error!!!'); - @mysql_query(" set names utf8 "); - if(defined('G5_MYSQL_SET_MODE') && G5_MYSQL_SET_MODE) @mysql_query("SET SESSION sql_mode = ''"); - if (defined(G5_TIMEZONE)) @mysql_query(" set time_zone = '".G5_TIMEZONE."'"); + + // mysql connect resource $g5 배열에 저장 - 명랑폐인님 제안 + $g5['connect_db'] = $connect_db; + + sql_query(" set names utf8 "); + if(defined('G5_MYSQL_SET_MODE') && G5_MYSQL_SET_MODE) sql_query("SET SESSION sql_mode = ''"); + if (defined(G5_TIMEZONE)) sql_query(" set time_zone = '".G5_TIMEZONE."'"); //============================================================================== // SQL Injection 등으로 부터 보호를 위해 sql_escape_string() 적용 diff --git a/config.php b/config.php index 5994665e7..ed2b13d28 100644 --- a/config.php +++ b/config.php @@ -160,7 +160,7 @@ define('G5_DISPLAY_SQL_ERROR', TRUE); // escape string 처리 함수 지정 // POST 등에서 한글이 깨질 경우 addslashes 로 변경 -define('G5_ESCAPE_FUNCTION', 'mysql_real_escape_string'); +define('G5_ESCAPE_FUNCTION', 'sql_real_escape_string'); // 게시판에서 링크의 기본개수를 말합니다. // 필드를 추가하면 이 숫자를 필드수에 맞게 늘려주십시오. diff --git a/lib/common.lib.php b/lib/common.lib.php index 80180937c..56fcaf226 100644 --- a/lib/common.lib.php +++ b/lib/common.lib.php @@ -1464,18 +1464,23 @@ function sql_select_db($db, $connect) // mysql_query 와 mysql_error 를 한꺼번에 처리 +// mysql connect resource 지정 - 명랑폐인님 제안 function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR) { + global $g5; + // Blind SQL Injection 취약점 해결 $sql = trim($sql); // union의 사용을 허락하지 않습니다. $sql = preg_replace("#^select.*from.*union.*#i", "select 1", $sql); // `information_schema` DB로의 접근을 허락하지 않습니다. $sql = preg_replace("#^select.*from.*where.*`?information_schema`?.*#i", "select 1", $sql); + if ($error) - $result = @mysql_query($sql) or die("

$sql

" . mysql_errno() . " : " . mysql_error() . "

error file : $_SERVER[PHP_SELF]"); + $result = @mysql_query($sql, $g5['connect_db']) or die("

$sql

" . mysql_errno() . " : " . mysql_error() . "

error file : {$_SERVER['PHP_SELF']}"); else - $result = @mysql_query($sql); + $result = @mysql_query($sql, $g5['connect_db']); + return $result; } @@ -1512,6 +1517,7 @@ function sql_password($value) // mysql 4.0x 이하 버전에서는 password() 함수의 결과가 16bytes // mysql 4.1x 이상 버전에서는 password() 함수의 결과가 41bytes $row = sql_fetch(" select password('$value') as pass "); + return $row['pass']; } @@ -1917,6 +1923,14 @@ function convert_charset($from_charset, $to_charset, $str) // mysql_real_escape_string 의 alias 기능을 한다. +function sql_real_escape_string($field) +{ + global $g5; + + if($field) + return mysql_real_escape_string($field, $g5['connect_db']); +} + function escape_trim($field) { if ($field) { From 282387cc178dd83a60c5f98c478317f3e88db7aa Mon Sep 17 00:00:00 2001 From: chicpro Date: Thu, 24 Apr 2014 11:16:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?sql=5Freal=5Fescape=5Fstring=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/common.lib.php b/lib/common.lib.php index 56fcaf226..0e0d4a472 100644 --- a/lib/common.lib.php +++ b/lib/common.lib.php @@ -1927,7 +1927,7 @@ function sql_real_escape_string($field) { global $g5; - if($field) + if($field != '') return mysql_real_escape_string($field, $g5['connect_db']); }