|
楼主 |
发表于 2021-10-10 10:40:41
|
显示全部楼层
cookie注入 代码分析: - <?php
- $id = $_COOKIE['id'];
- $value = 1;
- setcookie("id",$value);
- $con = mysqli_connect("localhost","root","root","sql");
- if(mysqli_connect_error()){
- echo "连接失败: ". mysqli_connect_error();
- }
- $result = mysqli_query($con,"select * from users where `id`=".$id);
- if(!$result){
- printf("error: %s\n",mysqli_error($con));
- exit();
- }
- $row = mysqli_fetch_array($result);
- echo $row['username']." : ".$row['password'];
- echo "<br>";
- ?>
复制代码 XFF头注入代码分析: - <?php
- $con = mysqli_connect("localhost","root","root","sql");
- if(mysqli_connect_error()){
- echo "连接失败: ". mysqli_connect_error();
- }
- if(getenv('HTTP_CLIENT_IP')){
- $ip = getenv('HTTP_CLIENT_IP');
- }
- elseif(getenv('HTTP_X_FORWARDED_FOR')){
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- }
- elseif(getenv('REMOTE_ADDR')){
- $ip = getenv('REMOTE_ADDR');
- }
- else{
- $ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
- }
- $result = mysqli_query($con,"select * from users where `ip`=".$ip);
- if(!$result){
- printf("error: %s\n",mysqli_error($con));
- exit();
- }
- $row = mysqli_fetch_array($result);
- echo $row['username']." : ".$row['password'];
- echo "<br>";
- ?>
复制代码
修复建议
- <?php
- functionCheckSql($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 = GetCurlUrl();
- //普通查询过滤一些特殊语法
- if($querytype == 'select'){
- $nowallow1 = "[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\._-]{1,}";
- $nowallow2 = "--|/\*";
- if(preg_match("/".$nowallow1."/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>");
- }
- }
- //完整的SQL检查
- while(TRUE){
- $pos = strpos($db_string,'\'',$pos+1);
- if($pos === False){
- break;
- }
- $clean.=substr($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
- [color=#555555][font=Lato, "]预编译语句[/font][/color][font=Lato, PingFang SC, Microsoft YaHei, sans-serif][color=#555555]
- [/color][/font]
- [code]$dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'study';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //判断数据库是否连接成功
- if($conn->errorCode()){
- die("filed".$conn->errorInfo());
- }
- $sql = "select * from users where username = :name";
- //预编译语句
- $stmt = $conn->prepare($sql);
- //定义要传入的变量(可以接收传过来的值)
- $username='admin';
- //将变量绑定到占位初
- $stmt ->bindParam(':name',$username);
- //执行sql语句
- $stmt->execute();
- // pdo预编译 占位符
- // $sql = "select * from users where username=? ";
- // $stmt = $conn->prepare($sql);
- // $username = 'root';
- //占位符通过变量绑定
- // $stmt -> bindParam(1,$username);
- // $stmt->execute();
- // $stmt->bindColumn(3,$id);
- // $stmt->bindColumn(2,$username);
- // 占位符通过数组绑定
- // $stmt ->execute([$username]);
- // $stmt->bindColumn(3,$id);
- // $stmt->bindColumn(2,$username);
- //将对应的数据库对应的列绑定到变量上
- $stmt->bindColumn(1,$pass);
- $stmt->bindColumn(2,$user);
- $stmt->bindColumn(3,$id);
- //将数据取出
- while($stmt->fetch()){
- echo $id."<br>";
- echo $username."<br>";
- }
- //释放内存资源
- $stmt =null;
- //断开连接
- $conn = null;
复制代码
方法一:执行一条使用命名占位符的预处理语句
- <?php
- $dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'job';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- if($conn->errorCode()){
- die("failed:".$conn->errorInfo());
- }
- //else{
- // echo "success";
- //}
- $sql = "select * from company where c_id = :c_id and c_name=:c_name";
- $stmt = $conn->prepare($sql);
- $stmt->execute(array(":c_id"=>10086,":c_name"=>'dianxin'));
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- //echo $stmt->rowCount();
- foreach($stmt->fetchAll() as $k=>$v){
- foreach ($v as $key=>$value){
- echo $value;
- echo "<br>";
- }
- }
- $stmt = null;
- $conn = null;
- ?>
复制代码
方法二:执行一条使用问号占位符的预处理语句
- <?php
- $dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'job';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- if($conn->errorCode()){
- die("failed:".$conn->errorInfo());
- }
- //else{
- // echo "success";
- //}
- //$sql = "select * from company where c_id = :c_id and c_name=:c_name";
- $sql = "select * from company where c_id = ? and c_name= ? ";
- $stmt = $conn->prepare($sql);
- //$stmt->execute(array(":c_id"=>10086,":c_name"=>'dianxin'));
- $stmt->execute(array(10086,'dianxin'));
- $stmt->execute(array(10086,'dianxin'));
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- //echo $stmt->rowCount();
- foreach($stmt->fetchAll() as $k=>$v){
- foreach ($v as $key=>$value){
- echo $value;
- echo "<br>";
- }
- }
- $stmt = null;
- $conn = null;
- ?>
复制代码
;
$old_pos = $pos+1;
}
$clean.=substr($db_string,$old_pos);
$clean = trim(strtolower(preg_replace(array('~\s+~s'), arrary(' '), $clean)));
//老版本的MySQL不支持union, 常用的程序里也不使用union,但一些黑客使用它,所以要检查
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";
}
//这些函数不会被使用 但黑客可能会用它来操作文件 down掉数据库
elseif (strpos($clean, 'sleep') !== False && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s',$clean) != 0) {
$fail = TRUE;
$error = "slown down detect";
}
elseif (strpos($clean, 'benchmark') !== False && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s',$clean) != 0) {
$fail = TRUE;
$error = "slown 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";
}
//老版本的MySQL不支持子查询 程序里用的可能也少 但黑客可能使用它来查询数据库敏感信息
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||SelectBreak\r\n");
exit("<font size='5' color='red'>Safe Alert: Request Error Step 2!</font>");
}
else{
return $db_string;
}
}
?>[/code]
预编译语句
- $dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'study';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //判断数据库是否连接成功
- if($conn->errorCode()){
- die("filed".$conn->errorInfo());
- }
- $sql = "select * from users where username = :name";
- //预编译语句
- $stmt = $conn->prepare($sql);
- //定义要传入的变量(可以接收传过来的值)
- $username='admin';
- //将变量绑定到占位初
- $stmt ->bindParam(':name',$username);
- //执行sql语句
- $stmt->execute();
- // pdo预编译 占位符
- // $sql = "select * from users where username=? ";
- // $stmt = $conn->prepare($sql);
- // $username = 'root';
- //占位符通过变量绑定
- // $stmt -> bindParam(1,$username);
- // $stmt->execute();
- // $stmt->bindColumn(3,$id);
- // $stmt->bindColumn(2,$username);
- // 占位符通过数组绑定
- // $stmt ->execute([$username]);
- // $stmt->bindColumn(3,$id);
- // $stmt->bindColumn(2,$username);
- //将对应的数据库对应的列绑定到变量上
- $stmt->bindColumn(1,$pass);
- $stmt->bindColumn(2,$user);
- $stmt->bindColumn(3,$id);
- //将数据取出
- while($stmt->fetch()){
- echo $id."<br>";
- echo $username."<br>";
- }
- //释放内存资源
- $stmt =null;
- //断开连接
- $conn = null;
复制代码
方法一:执行一条使用命名占位符的预处理语句
- <?php
- $dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'job';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- if($conn->errorCode()){
- die("failed:".$conn->errorInfo());
- }
- //else{
- // echo "success";
- //}
- $sql = "select * from company where c_id = :c_id and c_name=:c_name";
- $stmt = $conn->prepare($sql);
- $stmt->execute(array(":c_id"=>10086,":c_name"=>'dianxin'));
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- //echo $stmt->rowCount();
- foreach($stmt->fetchAll() as $k=>$v){
- foreach ($v as $key=>$value){
- echo $value;
- echo "<br>";
- }
- }
- $stmt = null;
- $conn = null;
- ?>
复制代码
方法二:执行一条使用问号占位符的预处理语句
- <?php
- $dbms = 'mysql';
- $db_host = 'localhost';
- $db_user = 'root';
- $db_pass = 'root';
- $db_name = 'job';
- $dsn = "$dbms:host=$db_host;dbname=$db_name";
- $conn = new PDO($dsn,$db_user,$db_pass);
- //$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- if($conn->errorCode()){
- die("failed:".$conn->errorInfo());
- }
- //else{
- // echo "success";
- //}
- //$sql = "select * from company where c_id = :c_id and c_name=:c_name";
- $sql = "select * from company where c_id = ? and c_name= ? ";
- $stmt = $conn->prepare($sql);
- //$stmt->execute(array(":c_id"=>10086,":c_name"=>'dianxin'));
- $stmt->execute(array(10086,'dianxin'));
- $stmt->execute(array(10086,'dianxin'));
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- //echo $stmt->rowCount();
- foreach($stmt->fetchAll() as $k=>$v){
- foreach ($v as $key=>$value){
- echo $value;
- echo "<br>";
- }
- }
- $stmt = null;
- $conn = null;
- ?>
复制代码
|
|