注入攻击(二)--------HTML(有源码)

本文通过示例详细介绍了如何搭建LAMP环境来演示不同类型的HTML注入攻击,包括反射型HTML注入,通过输入恶意脚本导致即时效果;钓鱼表单,利用HTML注入引导用户至恶意网站;以及存储型HTML注入,其特点是注入内容存储在服务器数据库中,对网站功能产生持久性破坏。这些示例突显了Web应用的安全风险及其防范的重要性。
摘要由CSDN通过智能技术生成

前序文章
注入攻击(一)--------SQL注入(结合BUUCTF sqli-labs)

示例网站搭建

1.搭建LAMP开发环境

使用kali快速搭建LAMP开发环境,自带,不需要大费周章安装。用whereis命令查看是否有LAMP开发环境

1. MySQL

whereis mysql
mysql

  • 启动mysql服务

    若输入mysql -u root -p显示如下,说明服务未开启
    mysqloff
    输入sudo service mysql start启动服务,再输入,显示可以登录。
    mysqlon
    另:从未使用mysql 需要按如下步骤完成初始安装:

  1. 转换成特权用户,使用命令su

  2. 输入命令mysql_secure_installation,一路Y,安装完毕

    2. PHP

    whereis php
    whereisphp
    php -v
    phpversion

3. Apache

whereis apache2

  • 启动apache

    判断apache是否启动,可以打开浏览器访问localhost,若显示下图,说明未启动。
    apacheoff
    输入命令 sudo service apache2 start 启动apache服务。

    若显示下图,说明apache已经启动
    apacheon

写在示例前

apache的默认站点位置在/var/www/html
所以各位运行前需要将面的所有文件放置在该目录下,首先cd到该目录,创建三个文件夹

  • phishing_site
  • reflection_html_injection
  • store_html_injection
    创建文件夹使用mkdir命令
    然后再将相应的示例文件粘贴到对应文件夹下。
    (当然,你不创建文件夹也可以,只要你记得住对应的示例文件名即可)。

示例1.反射型HTML注入

页面效果

涉及文件

  • reflection_html_injection/index.php

浏览器中输入网址:localhost/reflection_site/index.php
页面效果:
reflection_html_injection
在输入框输入
<script>alert("哎哟,你干嘛");</script>
点击搜索显示如下:
reflection_html_injection2
这就是最基本的反射型html注入,但是,这种html并不会持久存在,而是伴随着每次注入在网页显示。

源码

 <!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>搜索页面</title>
  </head>
  <body>
    <form action=" " method="GET">
      <label for="searchBox">搜索:</label>
      <input type="text" id="searchBox" name="q">
      <input type="submit" value="搜索">
    </form>
    <?php
	if(null !== ($query = $_GET['q'] ?? null))
	{		
		echo "Your search result for:$query";
	}
    ?>

  </body>
</html>

示例2.钓鱼表单

页面效果

涉及文件:

  • phishing_site/index.php
  • phishing_site/reward.php

模拟在官方网站填写表单点击提交按钮后,通过HTML注入攻击向该网页注入钓鱼链接,跳转至第三方网页进行用户信息窃取.
浏览器中输入网址:localhost/phishing_site/index.php
phishing_site
跳转后进入第三方网站(这里为了演示方便直接俄访问了同服务器的文件)
phishing_site2

源码

phishing_site/index.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>搜索页面</title>
  </head>
  <body>
    <form action=" " method="GET">
      <label for="searchBox">搜索:</label>
      <input type="text" id="searchBox" name="q">
      <input type="submit" value="搜索">
    </form>   
    <h2>搜索结果:</h2>
<?php
//php8.1不能直接使用isset函数进行判断

	if(null !== ($query = $_GET['q'] ?? null))
	{	
		echo "<p>",$query,"你好,点击我查看你的大奖</p>" ;
		echo "<a href="."reward.php".">查看大奖</a>";
	}

    ?>
  </body>
</html>

phishing_site/reward.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>中奖信息填写</title>
  </head>
  <body>
    <h1>恭喜你中奖啦!xx大学优秀毕业生!请填写下面的表单以便于我们联系你</h1>
    <form action=" " method="GET">
      <label>联系信息</label>

      <label for="username">姓名</label>
      <input type="text" id="username" name="uname">

      <label for="phoneNumber">电话</label>
      <input type="text" id="phoneNumber" name="phone">

      <label for="IDCard">身份证号</label>
      <input type="text" id="IDCard" name="id">
      <input type="submit" value="提交">
    </form>
    
  </body>
</html>

示例3.存储型HTML注入

页面效果

涉及文件

  • store_html_injection/index.php
  • store_html_injection/post.php

这种是持久性存在的,应为将注入的内容上传到了服务器端,影响了数据库。
index.php页面上方展示了两个填写表单,点击post后会将两个数据存入mysql数据库。
分割线下面是为了便于演示,选择查询数据库的所有信息展示在页面上。

浏览器中输入网址:localhost/store_html_injection/index.php
store_html_injection
现在开始html注入,在content 或者title中输入:

`

并提交数据库
store2

刷新页面后就会出现,并且换用户登录依然会这样显示,也就是说这种影响是持久的。破坏了网站的正常功能。
store3

源码

store_html_injection/index.php

<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
</head>
<body>
    <h1>Welcome to my blog</h1>
    <form method="POST" action="post.php">
        <label for="title">Title:</label>
        <input type="text" name="title" id="title"><br>
        <label for="content">Content:</label>
        <textarea name="content" id="content"></textarea><br>
        <input type="submit" value="Post">
    </form>
<hr>

<?php
// 数据库连接信息
$servername = "localhost:3306";
$username = "root";
$password = "991109";
$dbname = "ctf";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询comment表中的数据
$sql = "SELECT id, title,comment FROM comments";
$result = $conn->query($sql);

// 如果有数据,输出到HTML页面
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<p>ID: ".$row["id"]."<br>Title:".$row["title"]."<br>Comment: ".$row["comment"] . "<br><hr></p>";
    }
} else {
    echo "0 结果";
}

// 关闭连接
$conn->close();
?>



</body>
</html>

store_html_injection/post.php

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title ="'".$_POST["title"]."'";
    $content = "'".$_POST["content"]."'";
    $current_time = (int) time();
//连接数据库 
try {
    $conn = mysqli_connect('localhost:3306','root','pasd','ctf');
    if (!$conn) {
        throw new mysqli_sql_exception(mysqli_connect_error());
    }
}//try 
catch (mysqli_sql_exception $e) {
    echo 'MySQL connection error: ' . $e->getMessage();
}//catch

//如果连接成功了
if($conn){
    $sql = "INSERT INTO comments(id,title,comment) VALUES ($current_time,$title,$content)";
    if (mysqli_query($conn, $sql)) {
        echo "Data inserted successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    echo "Post successfully submitted!";
    //关闭数据库连接
    mysqli_close($conn);
}//if($conn)
}
?>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值