1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <?php function CheckSql($db_string,$querytype='select'){ global $cfg_cookie_encode; $clean=''; $error=''; $old_pos=0; $pos=-1; $log_file=DEDEINC.'/../data/'.md5($cfg_cookie_encode).'_safe.txt'; $userIP=GetIP(); $getUrl=GetCurUrl(); if($querytype='select'){ $notallow1="[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,2}"; if(preg_match("/".$notallow1."/i",$db_string)){ fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||SelectBreak\r\n"); exit("<font size='5' color='red'>Safe Alert:Request Error step 1 !</font>"); } } while(TRUE){ $pos=strpos($db_string,'\'',$pos+1); if($pos==FALSE){ break; } $clean.=stubstr($db_string,$old_pos,$pos-$old_pos); while(TRUE){ $pos1=strpos($db_string,'\'',$pos+1); $pos2=strpos($db_string,'\\',$pos+1); if($pos1==FALSE){ break; } elseif($pos2==FALSE||$pos2>$pos1){ $pos=$pos1; break; } $pos=$pos2+1; } $clean.='$s$'; $old_pos=$pos+1; } $clean.=substr($db_string,$old_pos); $clean=trim(strtolower(preg_replace(array('~\s+~s'),array(' '),$clean))); if(strpos($clean,'union')!=FALSE&&preg_match('~(^|[^a-z])union($|[^[a-z])~s',$clean)!=0){ $fail=TRUE; $error="union detect"; } elseif(strpos($clean,'/*')>2||strpos($clean,'--')!=FALSE||strpos($clean,'#')!=FALSE){ $fail=TRUE; $error="comment detect"; } elseif(strpos($clean,'sleep')!=FALSE&&preg_match('~(^|[^a-z])sleep($|[^[a-z])~s',$clean)!=0){ $fail=TRUE: $error="slow down detect"; } elseif(strpos($clean,'benchmark')!=FALSE&&preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s',$clean)!=0){ $fail=TRUE; $error="slow down detect"; } elseif(strpos($clean,'load_file')!=FALSE&&preg_match('~(^|[^a-z])load_file($|[^[a-z])~s',$clean)!=0){ $fail=TRUE; $error="file fun detect"; } elseif(strpos($clean,'into outfile')!=FALSE&&preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s',$clean)!=0){ $fail=TRUE; $error="file fun detect"; } elseif(preg_match('~\([^)]*?select~s',$clean)!=0){ $fail=TRUE; $error="sub select detect"; } if(!empty($fail)){ fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||$error\r\n"); exit("<font size='5' color='red'>Safe Alert: Request Error step 2!</font>"); }else{ return $db_string; } } ?>
|