您当前的位置:首页 > 网站建设 > php
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

网站安全-漏洞与修复·

51自学网 2022-06-16 12:52:29
  php
AWD:web_yunnan_1

1、进入 cd awd-platform-master

2、创建赛题和队伍数量:


3、启动

4、查看SSH密码

5、查看到队伍映射的端口

6、访问端口:

7、同样可以使用ssh账户登录自己的服务器,下载对应的源文件

0x01 漏洞&修复
sql注入漏洞读取flag
漏洞挖掘
1、通过搜索框中发现查询逻辑:

2、根据查询逻辑写注入语句:


漏洞修复
1、找到对应的页面 sqlgunsearch.php
2、查看代码:

<?php
/**
* Author:sqlgun
* Email:sqlgun@qq.com
* Date: 2011-6-8
* http://hi.baidu.com/sqlgun
*/
define('GUY','true');
require 'common.inc.php';
global $_system;

if(isset($_POST['key'])){
        $_key=trim($_POST['key']);
}else{
        echo '<script type="text/javascript">alert("非法访问!");location.href="index.php";</script>';
}

if(isset($_GET['page'])){
        $_page=$_GET['page'];
        if(empty($_page)|| !is_numeric($_page)||$_page<0|| ($_page>0 && $_page<1)){
                $_page=1;
        }else{
                $_page=intval($_page);
        }
}else{
        $_page=1;
}
$_pagenums=$_system['pagenums'];
$_pageopen=($_page-1)*$_pagenums;
echo "select id from news where title like '%$_key%'";
$_result=mysql_query("select id from news where title like '%$_key%'");
$_nums=mysql_num_rows($_result);
$_pages=ceil($_nums/$_pagenums);
$_results=mysql_query("select id,title,date from news  where title like '%$_key%' order by date DESC limit $_pageopen,$_pagenums");

?>

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
3、修改方案
最简单的就是把echo 打印的后台逻辑给去掉,那%也就不容易闭合
另外就是使用PDO方式,预编译修复

后台登录sql注入
漏洞挖掘
1、首页输入框中随便输入查询内容,在最上面曝出了查询逻辑,通过表可以看出,这里是没有任何过滤,尝试知道后台登录页面,使用sql万能密码登录:

2、找到登录页面:

3、但是通过首页的暴露出来的查询逻辑构造登录 admin%’ or 1=1 # 但是无法登录,然后在后台找到登录页面的sql查询语句,找到查询数据的语句:select * from admin where admin=’{KaTeX parse error: Expected 'EOF', got '}' at position 13: _html[admin]}̲' and password=…_html[‘password’]}’ 发现参数是有花括号的,因此在够着登录账户是为: admin}’ or 1=1 # 这样就能闭合登录,知道闭合方式之后就可以尝试注入获取数据库中的内容,寻找flag
注意点:
1、在使用万能密码的时候,密码需要随便填点东西
2、注释需要使用 # ,在这是使用 – 不能注释成功

<?php
/**
* Author:sqlgun
* Email:sqlgun@qq.com
* Date: 2011-5-22
* http://hi.baidu.com/sqlgun
*/
session_start();
define('GUY','true');
require '../common.inc.php';
if($_GET['action']=='login'){
/*     if($_SESSION['code']!=$_POST['yzm']){
    echo'<script type="text/javascript"> alert("验证码错误!");location.href="login.php"; </script>';    
    exit;    
    } */
    if(empty($_POST['admin'])){
    echo'<script type="text/javascript"> alert("请输入用户账号再登录!");location.href="login.php"; </script>';            
        exit;
    }
    if(empty($_POST['password'])){
    echo'<script type="text/javascript"> alert("请输入用户密码再登录!");location.href="login.php"; </script>';            
        exit;
    }    
    $_html=array();
    $_html['admin']=trim($_POST['admin']);
    $_html['password']=md5(trim($_POST['password']));    
    $_result=@mysql_query("select * from admin where admin='{$_html[admin]}' and password='{$_html['password']}'")or die('登录错误');
    if(!!$_rows=mysql_fetch_array($_result)){
        setcookie('login',md5($_rows['admin']));
        $_SESSION['user']=md5($_rows['admin']);
        header('location:index.php');
    }else{
        echoalerthistory('登录账号信息错误!');
    }
}
?>
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
输入密码,就能登录成功:


漏洞修复
1、找到对应的文件 …/sqlgunadmin/login.php

<?php
/**
* Author:sqlgun
* Email:sqlgun@qq.com
* Date: 2011-5-22
* http://hi.baidu.com/sqlgun
*/
session_start();
define('GUY','true');
require '../common.inc.php';
if($_GET['action']=='login'){
/*     if($_SESSION['code']!=$_POST['yzm']){
    echo'<script type="text/javascript"> alert("验证码错误!");location.href="login.php"; </script>';    
    exit;    
    } */
    if(empty($_POST['admin'])){
    echo'<script type="text/javascript"> alert("请输入用户账号再登录!");location.href="login.php"; </script>';            
        exit;
    }
    if(empty($_POST['password'])){
    echo'<script type="text/javascript"> alert("请输入用户密码再登录!");location.href="login.php"; </script>';            
        exit;
    }    
    $_html=array();
    $_html['admin']=trim($_POST['admin']);
    $_html['password']=md5(trim($_POST['password']));    
    $_result=@mysql_query("select * from admin where admin='{$_html[admin]}' and password='{$_html['password']}'")or die('登录错误');
    if(!!$_rows=mysql_fetch_array($_result)){
        setcookie('login',md5($_rows['admin']));
        $_SESSION['user']=md5($_rows['admin']);
        header('location:index.php');
    }else{
        echoalerthistory('登录账号信息错误!');
    }
}
?>
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
修改方案:进行pdo预处理()

<?php
session_start();
define('GUY','true');
require '../common.inc.php';
if($_GET['action']=='login'){
/*     if($_SESSION['code']!=$_POST['yzm']){
    echo'<script type="text/javascript"> alert("验证码错误!");location.href="login.php"; </script>';    
    exit;    
    } */
    if(empty($_POST['admin'])){
    echo'<script type="text/javascript"> alert("请输入用户账号再登录!");location.href="login.php"; </script>';            
        exit;
    }
    if(empty($_POST['password'])){
    echo'<script type="text/javascript"> alert("请输入用户密码再登录!");location.href="login.php"; </script>';            
        exit;
    }    
    $_html=array();
    $_html['admin']=trim($_POST['admin']);
    $_html['password']=md5(trim($_POST['password']));    
    // $_result=@mysql_query("select * from admin where admin='{$_html[admin]}' and password='{$_html['password']}'")or die('登录错误');
    // if(!!$_rows=mysql_fetch_array($_result)){
    //     setcookie('login',md5($_rows['admin']));
    //     $_SESSION['user']=md5($_rows['admin']);
    //     header('location:index.php');
    // }else{
    //     echoalerthistory('登录账号信息错误!');
    // }
    $dbh = new PDO("mysql:host=localhost;dbname=sqlgunews;charset=utf8","root","root");
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES,flase);

    $sql = "select * from amdin where admin=? and password=?";
    stmt = $dbh->prepare($sql);
    $stmt->bindParam(1,$_html['admin']);
    $stmt->bindParam(2,$_html['password']);
    $stmt->execute();
    $result = $stmt->fetchAll();

    if(!!$result){
        setcookie("login",md5($_rows['admin']));
        $_SESSION['user']=md5($_rows['admin']);
        header('location:index.php');
    } else{
        echo "登录信息错误";
    }
}
?>
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
在尝试使用完成密码登录,登录失败



任意文件下载
漏洞挖掘
1、扫描目录:扫描了一下目录,没有发现有价值的东西,查看了一下源码发现后台目录

2、登录该目录:

3、直接使用弱口令,admin/admin即可登录

4、在日志下载处,发现可以下载log,使用bp和wireshark查看了,没有看出什么东西,就是下载不了



5、然后查看教程,去查看了源码,找到下载的php文件,在文件中发现下载日志的代码要求是get请求

6、根据代码要求构造payload, paylaod: /sqlgunadmin/downlog.php?downlog=down&filepath=/flag



漏洞修复
修补方案:白名单的方式,只能请求/home/nsfocus/log.txt
具体步骤:1、在前端将下载的方式修改为get的请求方式
2、后端加白名单

downlog.html 前端修改:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/addadmin.css"/>
<title>下载日志</title>
</head>
<div id="modify">
<p>下载日志</p>
<form method="get" action="downlog.php">
<ul>
<li>日志:<input type="text" name="filepath" value="/home/nsfocus/log.txt" /></li>
<li><input type="submit" value="下载" /></li>
</ul>
</form>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
downlog.php 后台修改之前代码:

<?php
    if($_GET["downlog"] == "down"){
        if(isset($_GET["filepath"])){
            $file_path=$_GET["filepath"];
            $fp=fopen($file_path,"r");
            $file_size=filesize($file_path);
            Header("Content-type:application/octet-stream");
            Header("Accept-Ranges:bytes");
            Header("Accept-Length:".$file_size);
            Header("Content-Disposition:attachment;filename=log.txt")
            $buffer=1024;
            $file_count=0;
            while(!feof($fp)&&$file_count<$file_size){
                $file_con=fread($fp,$buffer);
                $file_count+=$buffer;
                echo $file_con;
            }
            fclose($fp);
        }
    }
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
后台php修改之后的代码:

<?php
    if(isset($_GET["filepath"])){
        $file_path=$_GET["filepath"];
        if($file_path==="/home/nsfocus/log.txt"){
            $fp=fopen($file_path,"r");
            $file_size=filesize($file_path);
            Header("Content-type:application/octet-stream");
            Header("Accept-Ranges:bytes");
            Header("Accept-Length:".$file_size);
            Header("Content-Disposition:attachment;filename=log.txt")
            $buffer=1024;
            $file_count=0;
            while(!feof($fp)&&$file_count<$file_size){
                $file_con=fread($fp,$buffer);
                $file_count+=$buffer;
                echo $file_con;
            }
            fclose($fp);
        }else{
            die("File not found")
        }
    }
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
修改之后只能下载log.txt

任意文件上传
漏洞挖掘
找到文件上传的点:

上传图片马:

然后用冰蝎链接成功:


漏洞修复
修复方案:
白名单检测后缀/对上传的文件进行重命名,并自动添加后缀

通过之前bp转包,找到文件上传的文件是:/sqlgunadmin/kindedit/php/upload_json.php
通过之前链接的冰蝎,下载upload_json.php:

<?php
/**
 * KindEditor PHP
 * 
 * 本PHP程序是演示程序,建议不要直接在实际项目中使用。
 * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
 * 
 */

require_once 'JSON.php';

$php_path = dirname(__FILE__) . '/';
$php_url = dirname($_SERVER['PHP_SELF']) . '/';

//文件保存目录路径
$save_path = $php_path . '../attached/';
//文件保存目录URL
$save_url = $php_url . '../attached/';
//定义允许上传的文件扩展名
$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
//最大文件大小
$max_size = 1000000;

$save_path = realpath($save_path) . '/';

//有上传文件时
if (empty($_FILES) === false) {
    //原文件名
    $file_name = $_FILES['imgFile']['name'];
    //服务器上临时文件名
    $tmp_name = $_FILES['imgFile']['tmp_name'];
    //文件大小
    $file_size = $_FILES['imgFile']['size'];
    //检查文件名
    if (!$file_name) {
        alert("请选择文件。");
    }
    //检查目录
    if (@is_dir($save_path) === false) {
        alert("上传目录不存在。");
    }
    //检查目录写权限
    if (@is_writable($save_path) === false) {
        alert("上传目录没有写权限。");
    }
    //检查是否已上传
    if (@is_uploaded_file($tmp_name) === false) {
        alert("临时文件可能不是上传文件。");
    }
    //检查文件大小
    if ($file_size > $max_size) {
        alert("上传文件大小超过限制。");
    }
    //获得文件扩展名
    $temp_arr = explode(".", $file_name);
    $file_ext = array_pop($temp_arr);
    $file_ext = trim($file_ext);
    $file_ext = strtolower($file_ext);
    //检查扩展名
    if (($_FILES["imgFile"]["type"] == "image/gif")|| ($_FILES["imgFile"]["type"] == "image/jpeg")|| ($_FILES["imgFile"]["type"] == "image/jpg")|| ($_FILES["imgFile"]["type"] == "image/pjpeg")|| ($_FILES["imgFile"]["type"] == "image/x-png")|| ($_FILES["imgFile"]["type"] == "image/png")){
        //创建文件夹
        $ymd = date("Ymd");
        $save_path .= $ymd . "/";
        $save_url .= $ymd . "/";
        if (!file_exists($save_path)) {
            mkdir($save_path);
        }
        //新文件名
        $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
        //移动文件
        $file_path = $save_path . $new_file_name;
        if (move_uploaded_file($tmp_name, $file_path) === false) {
            alert("上传文件失败。");
        }
        @chmod($file_path, 0644);
        $file_url = $save_url . $new_file_name;
        
        header('Content-type: text/html; charset=UTF-8');
        $json = new Services_JSON();
        echo $json->encode(array('error' => 0, 'url' => $file_url));
        exit;
    }
    else{
        alert("不被允许的文件格式!");
    }
}

function alert($msg) {
    header('Content-type: text/html; charset=UTF-8');
    $json = new Services_JSON();
    echo $json->encode(array('error' => 1, 'message' => $msg));
    exit;
}
?>
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
修改之后代码:

(($file_ext == "jpg")|| ($file_ext == "png")|| ($file_ext == "gif")|| ($file_ext == "bmp")|| ($file_ext == "jpg"))
1
在使用burp去发包,可以看到无法在上传成功:


0x02 知识点&对抗思路
知识点积累
1、php注释问题:php多行注释要在<?php ?>中才生效;
2、php中 === 问题:https://jingyan.baidu.com/article/f71d6037a7e52b1ab641d1dc.html

对抗思路
1、遇到任意文件下载,最好的方法是白名单,设置只能下载的文件

0x03 参考文章
https://blog.csdn.net/weixin_41652128/article/details/90290769
————————————————
版权声明:本文为CSDN博主「ju1uk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38618396/article/details/108240630
下载地址:
用PHP生成静态html分页列表的代码
ecshopB2B2C多商家入驻小京东V5.0在线聊天单仓库+供应商后台版
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1