Media Log
php sql인젝션 공격막기
Example#1 mysql_real_escape_string() 예제
<?php
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());
// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
mysql_real_escape_string($user),
mysql_real_escape_string($password));
?>
Example#2 SQL 인젝션 공격(Injection Attack)의 예
<?php
// Query database to check if there are any matching users
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);
// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";
// This means the query sent to MySQL would be:
echo $query;
?>
MySQL로 전송되는 질의:
SELECT * FROM users WHERE name='aidan' AND password='' OR ''=''
유효한 비밀번호 없이 누구나 접속하여 접근이 가능하다.
Example#3 "Best Practice" 질의
mysql_real_escape_string()은 각 변수에 대해 SQL 인젝션을 방지한다. 이 예제는 Magic Quotes 설정과는 별개로 데이터베이스를 질의하는 "best practice" 방법을 시연한다.
<?php
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());
// Make a safe query
$query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s",
quote_smart($_POST['username']),
quote_smart($_POST['password']));
mysql_query($query);
?>
SQL 인젝션 공격이 동작하지 않으며 질의가 정확하게 실행될 것이다.
php 다운로드 소스
파일이름을 /notice/111.img 로 해서 넘겼다.
$file = 실제파일의 저장 경로
$filename = 파일이름
<?include "function.php"?>
<?
$filename = SQL_Injection($_REQUEST["filename"]);
$file = trim($localpath."/data/".$filename);
$filename = explode("/",$filename);
$filename = $filename[1];
$file_size = filesize($file);
$filename = urlencode("$filename");
// 접근경로 확인
if (!eregi($_SERVER['HTTP_HOST'], $_SERVER['HTTP_REFERER'])) Error("외부에서는 다운로드 받으실수 없습니다.");
if(strstr($HTTP_USER_AGENT, "MSIE 5.5")) {
header("Content-Type: doesn/matter");
Header("Content-Length: ".filesize("$file"));
header("Content-Disposition: filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
} else {
header("Content-type: file/unknown");
Header("Content-Disposition: attachment; filename=$filename");
Header("Content-Transfer-Encoding: binary");
Header("Content-Length: ".filesize($file));
Header("Content-Descript-xion: PHP3 Generated Data");
header("Pragma: no-cache");
header("Expires: 0");
}
if( $ret == 1 ) Error("지정하신 파일이 없습니다.");
if( $ret == 2 ) Error("접근불가능 파일입니다. 정상 접근 하시기 바랍니다.");
if (is_file("$file")) {
$fp = fopen("$file", "r");
if (!fpassthru($fp))
fclose($fp);
}
?>
php 변수에 해당 값이 있는지 알아볼때
if(ereg("M04",$Pmenu))
{
echo "해당값이 존재한다";
php 자기 주소읽어오기 쿼리스트링 넘어온값 읽어오기
sql 인젝션 공격을 막을때 가볍게 사용 할 수있다.
if(eregi("char",strtolower($_SERVER['QUERY_STRING'])))
{
exit;
}
현재 주소만 가비고 온다 쿼리스트링값은 빼고
if(eregi("char",strtolower($_SERVER['PHP_SELF'])))
{
exit;
}