addslashes() ã«ãã SQL æååã®ã¨ã¹ã±ã¼ãåé¿åé¡
The addslashes() Versus mysql_real_escape_string() Debate (Chris Shiflett: The PHP Blog) ã§æåã³ã¼ãã«ãã£ã¦ã¯ addslashes() ã«ãã SQL ã®ã¨ã¹ã±ã¼ãå¦çã¯åé¡ããããã¨ãææããã¦ãã¾ããã
æ¥æ¬èªã§ããShift_JIS ãæ±ã£ã¦ããå ´åã¯åæ§ã®åé¡ãèµ·ããå¯è½æ§ãããããã«æããã¾ããã®ã§ãã¡ã¢ãã¦ããã¾ããä½ãééããåéããªã©ãããã¾ãããææãã¦ãã ããããããããPHP ã ãã®åé¡ã§ã¯ãªãã¨æãã¾ãã
æ¥æ¬èªã§ãæåã³ã¼ãã Shift_JIS ã®å ´åãaddslashes() ã«ããã¨ã¹ã±ã¼ãå¦çã§ã¯ SQL ã¤ã³ã¸ã§ã¯ã·ã§ã³ãå¯è½ã«ãªã£ã¦ãã¾ãã±ã¼ã¹ãããã¾ãã
ä¾ãã°ãä¸è¨ã®ãã¼ã¸ã®ä¾ãå°ãå¤æ´ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
<?php // ä¾: æåã³ã¼ãã Shift_JIS ã®å ´åã«åé¡ã¨ãªãã±ã¼ã¹ $_POST['username'] = "\x95' OR username = username /*"; $_POST['password'] = 'guess'; $mysql['username'] = addslashes($_POST['username']); $mysql['password'] = addslashes($_POST['password']); $sql = "SELECT * FROM users WHERE username = '{$mysql['username']}' AND password = '{$mysql['password']}'"; ?>
$sql ã®å 容ã¯ä»¥ä¸ã®ããã«ãªãã¾ã(æåã³ã¼ã㯠Shift_JIS ã§ã)ã
SELECT * FROM users WHERE username = '表' OR username = username /*' AND password = 'guess'
å®éã®å¦çã¨ãã¦ã¯ã以ä¸ã®ããã«ãªãã¾ãã
- '(ã·ã³ã°ã«ã¯ã©ã¼ã) ã addslashes() é¢æ°ãã¨ã¹ã±ã¼ããè¡ãã\' ã«å¤æ
- æåã³ã¼ãã Shift_JIS ã®å ´åã\' ã®åã«ãã1ãã¤ã(\x95)ã¨éãªãã¨ã§æå³ãæãæå(\x95\x5c 㯠Shift_JIS 㧠表 ã¨ããæå)ã¨ãã¦èªèããã
- ãã®å¾ãã«ãã '(ã·ã³ã°ã«ã¯ã©ã¼ã)ã ããã¨ã¹ã±ã¼ããããã«æ®ã
以ä¸ããã' ãæå¹ã«ãããã¨ãã§ãããã®å¾ãã«ä»»æã® SQL ã³ã¼ããæ¿å
¥ãããã¨ãå¯è½ã«ãªãã¾ãã
対çã¨ãã¦ã¯ãPrepared Statement ã使ããã¾ãã¯ãmysql_real_escape_string() ãªã©ã®ãã¼ã¿ãã¼ã¹å°ç¨ã®ã¨ã¹ã±ã¼ãé¢æ°ã使ç¨ããã¨ããæ¹æ³ãããããã§ã(å®éã«ã¯è©¦ãã¦ãã¾ããã®ã§ã確èªããæ¹ãè¯ãã¨æãã¾ã)ã
id:hoshikuzu ããããã®ä»¶ã«ã¤ãã¦ãã¾ã¨ãã¦ãããã¾ã(PHP å©ç¨æã« Shift_JIS 㧠addslashes() ã«ããã¨ã¹ã±ã¼ãå¦çã«ãSQL ã¤ã³ã¸ã§ã¯ã·ã§ã³å¯è½ãªç©´)ãmagic_quotes_gpc ãæå¹ã«ãã¦ããå ´åã®åé¡ã«ã¤ãã¦è¨åãã¦ãããã¾ããmagic_quotes_gpc ãæå¹ã«ãªã£ã¦ããã¨ãã¯ã©ã¤ã¢ã³ããã渡ã£ã¦ããå¤æ°å
¨ã¦ã«ãèªåçã« addslashes() ã使ç¨ãã¦ããã®ã¨åãã«ãªãã¾ãã
ã¾ããid:jrofbyr ãããã以ä¸ã®ãããªã³ã¡ã³ããããã ãã¾ãããMySQL ã¯æå
ã®ç°å¢ã«ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ããã®ã§ã確èªã§ããªãã®ã§ãããmysql_real_escape_string() ã§ã PEAR ã® Prepared Statement ã§ãã¨ã¹ã±ã¼ããããªãå ´åãããããã§ãã
mysql_real_escape_string()ã§ãSET NAMES sjis;çãå®è¡ããå¾ã«ä½¿ãã¨2ãã¤ãç®ã®0x5Cæåãã¨ã¹ã±ã¼ããããªãããã§ããPEAR::DBã®DB_common::prepare() ã§ã¯MySQL使ç¨æã¯å é¨ã§mysql_real_escape_string()ã使ãããã®ã§ãã¨ã¹ã±ã¼ããããªãå ´åãããã¾ããã確ãç°å¢ã¯ PHP 4.4.1/MySQL 4.1.12ã§ããSET NAMES binary;ã§ãªãã¨ãåé¿ãã¦ã¾ãã
ã¾ããPostgreSQL ã§ã SET client_encoding TO 'SJIS' ãå®è¡ããã¨ãã®åé¡ã®å½±é¿ãåãããã¨ã確èªãã¾ãã(PHP 5.1.2/PostgreSQL 8.0.4, ãã¼ã¿ãã¼ã¹ã®æåã³ã¼ã㯠EUC-JP)ããã¹ãã³ã¼ãã¯ä»¥ä¸ã®éãã§ãã
<?php $conn = pg_connect( "dbname=test user=test" ); if ( ! $conn ) { exit( 'Could not connect' ); } pg_query( "CREATE TABLE id ( id int ); INSERT INTO id (id) VALUES (0); INSERT INTO id (id) VALUES (1); CREATE TABLE t ( val text ); INSERT INTO t (val) VALUES ('test');" ); pg_query( "SET client_encoding TO 'SJIS';" ); $input = "\x95'; SELECT * FROM id; --"; $query = "SELECT * FROM t WHERE val = '" . addslashes( $input ) . "';"; //$query = "SELECT * FROM t WHERE val = '" . pg_escape_string( $input ) . "';"; echo 'Query:' . $query . "\n"; $result = pg_query( $query ); if ( ! $result ) { exit( "Query failed\n" ); } print_r( pg_fetch_all( $result ) ); ?>
çµæã¯ä»¥ä¸ã®ããã«ãªãã¾ãã(æ¬æ¥ã¯çµæã¯è¡¨ç¤ºãããªãã¯ã)ãCLIç ã® PHP 5.1.2 ã§å®è¡ãã¾ãããæåã³ã¼ã㯠Shift_JIS ã§ãã
SQL: SELECT * FROM t WHERE val = '表'; SELECT * FROM id; --'; Array ( [0] => Array ( [id] => 0 ) [1] => Array ( [id] => 1 ) )
ããã«ãaddslashes() ã®ä»£ããã« PostgreSQL ã®å°ç¨ã¨ã¹ã±ã¼ãé¢æ°ã® pg_escape_string() ã使ç¨ãã¦ã SQL ã¤ã³ã¸ã§ã¯ã·ã§ã³ãã§ãã¦ãã¾ãã¾ãããpg_escape_string() ã使ç¨ããã¨ãã'ãã¯ã''ãã«å¤æããããSELECT * FROM t WHERE val = '\x95''; SELECT * FROM id; --';ãã«ãªã£ã¦ããã®ã§ãããã\x95'ãã SJIS ã®ä¸æåã¨ãã¦æ±ã£ã¦ããããã§ãã'ããã¨ã¹ã±ã¼ãããã¦ããªããã¨ã«ãªã£ã¦ãã¾ãããã§ãã
ãããããæ¥æ¬èªãæ±ãæåã³ã¼ãã§ãã®åé¡ã®å½±é¿ãåããã®ã¯ Shift_JIS ã ãã ã¨æãã¾ãããShift_JIS 以å¤ã®ä»ã®æåã³ã¼ãã§ãåæ§ã®åé¡ãèµ·ããå¯è½æ§ãããã¾ãã
å®å
¨ãªå¯¾å¦æ¹æ³ã¨ãã¦ã¯ Shift_JIS ãªã©ã®æåã®æå¾ã®ãã¤ãã« \ ãå«ã¾ããå¯è½æ§ã®ããæåã³ã¼ãã使ç¨ããªããã¨ã§ãã
ã¾ãã¯ãã¯ã©ã¤ã¢ã³ãã®æåã³ã¼ãã« Shift_JIS ã使ç¨ããªãã¨ãããã¨ã§ãåé¡ãåé¿ã§ããããããã¾ãããid:jrofbyr ããã®ã³ã¡ã³ãã«ããããã«ãMySQL ã§ã¯ãSET NAMES binary; ã¨ãããã¨ã§ãã¨ããããã¯åé¿ã§ããããã§ããä»ã®æ¹æ³ãã¦ã¯ãéå¹çã§ããããã¼ã¿ãã¼ã¹ã®æåã³ã¼ãã Shift_JIS ã§ããã¯ã©ã¤ã¢ã³ãã®æåã³ã¼ãã EUC-JP ã«ãã¦ãSQL æã EUC-JP ã«å¤æãã¦ããçºè¡ãããã¨ã¯å¯è½ã ã¨æãã¾ãã