Padding Oracle Attack 分析

本文深入剖析Padding Oracle Attack的攻击原理,详细解释了CBC分组密码的工作模式,并介绍了攻击条件、本地测试方法以及结果分析。通过实例展示了如何利用CBC字节翻转实现任意值解密,最终达到攻击目标。
摘要由CSDN通过智能技术生成

Padding Oracle Attack是一个经典的攻击,在《白帽子讲web安全》有一章提到Padding Oracle Attack的攻击方式,本篇文章结合着NJCTF的Be admin 进行详细的讲解,扫除知识上的盲点。
本篇文章讲解的顺序是攻击原理、攻击条件、本地测试、结果分析与总结四个方面进行详细的讲解。

0x00攻击原理

0x1 padding

由于CBC是块密码工作模式, 所以要求明文长度必须是块长度的整数倍.
对于不满足的数据, 会进行数据填充到满足整数倍. 即 padding

** ** ** ** ** ** ** 01
** ** ** ** ** ** 02 02
** ** ** ** ** 03 03 03
** ** ** ** 04 04 04 04
** ** ** 05 05 05 05 05
** ** 06 06 06 06 06 06
** 07 07 07 07 07 07 07
08 08 08 08 08 08 08 08

即空N位就用N个\xN 进行补全
对于刚好满足明文, 则需要另补出一个块长度的数据

0x2 CBC分组密码的链接模式

由于此处的Padding Oracle Attack是针对CBC分组密码的链接模式来讲的因此此处再讲讲CBC


这里写图片描述
CBC模式加密流程图

这里写图片描述
CBC模式解密流程图

理解以上两张图以后还需要知道以下这张图中的intermediary Value的意思,意为中间值(其实就是解密操作后的值)
这里写图片描述

明文、密文、IV、恶意明文、恶意IV 关系
明文 = 解密(密文) XOR IV
特殊的明文 = 解密(密文) XOR 特殊IV

0x3 原理总结

1. 基于密码学算法的攻击,往往第一个要搞清楚的是,我们在攻击谁,或者准确的说我们的攻击点在哪里?在一个密码学算法中,有很多的参数(指攻击者可以控制的参数),攻击者往往是针对其中某一个或某一些参数进行破解,穷举等攻击。


在Padding Oracle Attack攻击中,攻击者输入的参数是IV+Cipher,我们要通过对IV的”穷举”来请求服务器端对我们指定的Cipher进行解密,并对返回的结果进行判断。


2. 和SQL注入中的Blind Inject思想类似。我觉得Padding Oracle Attack也是利用了这个二值逻辑的推理原理,或者说这是一种”边信道攻击(Side channel attack)”。http://en.wikipedia.org/wiki/Side_channel_attack


这种漏洞不能算是密码学算法本身的漏洞,但是当这种算法在实际生产环境中使用不当就会造成问题。


和盲注一样,这种二值逻辑的推理关键是要找到一个”区分点”,即能被攻击者用来区分这个的输入是否达到了目的(在这里就是寻找正确的IV)。


比如在web应用中,如果Padding不正确,则应用程序很可能会返回500的错误(程序执行错误);如果Padding正确,但解密出来的内容不正确,则可能会返回200的自定义错误(这只是业务上的规定),所以,这种区别就可以成为一个二值逻辑的”注入点”。

0x01 攻击条件

1.可以控制密文 或者 IV
2.如果解密后不满足 padding 服务端会报错.

一般我们在攻击时,可以控制IV或密文的其中之一(一般是控制IV),这样我们可以通过盲注的方法把中间值注出来。
第二个条件,如果padding的结果不对就会返回False,如果正确就会返回解密的值

所以说攻击过程程就相当于盲注过程
下面会本地复现Be admin

0x02 本地测试

这里直接使用NJCTF的代码

注意数据库的搭建!!!

0x1 在本地服务器测试代码

<?php
error_reporting(0);
define("SECRET_KEY", "1234567812345678");
define("METHOD", "aes-128-cbc");

session_start();

function get_random_token(){
    
    $random_token='';
    for($i=0;$i<16;$i++){
        $random_token.=chr(rand(1,255));
    }
    return $random_token;
}

function get_identity()
{
    
    global $defaultId;
    $defaultId='action';
    $j = $defaultId;
    $token = get_random_token();
    $c = openssl_encrypt($j, METHOD, SECRET_KEY, OPENSSL_RAW_DATA, $token );
    $_SESSION['id'] = base64_encode($c);
    setcookie("ID", base64_encode($c));
    setcookie("token", base64_encode($token));
    if ($j === 'admin') {
        $_SESSION['isadmin'] = true;
    } else $_SESSION['isadmin'] = false;

}

function test_identity()
{
    
    if (!isset($_COOKIE["token"]))
        return array();
    if (isset($_SESSION['id'])) {
        $c = base64_decode($_SESSION['id']);
        if ($username = openssl_decrypt($c, METHOD, SECRET_KEY, OPENSSL_RAW_DATA, base64_decode($_COOKIE["token"]))) {
            if (
Padding Oracle attack is a type of cryptographic attack that exploits the behavior of cryptographic systems using block ciphers with padding. The attack allows an attacker to decrypt the contents of encrypted data by sending specially crafted ciphertexts to a server that uses the encryption algorithm. The attack works by exploiting the server's ability to detect whether a ciphertext is properly padded or not. Padding is commonly used in block ciphers to ensure that the input block is a fixed length. If the padding is incorrect, the server will reject the ciphertext and return an error message. However, by analyzing the error messages, an attacker can infer information about the plaintext and eventually decrypt it. To carry out the attack, the attacker sends many modified ciphertexts to the server, each with a different block of the ciphertext modified. By analyzing the responses from the server, the attacker can determine whether the modified block of ciphertext was properly padded or not. This information can be used to gradually determine the value of each byte of the plaintext. Padding Oracle attack is a serious threat to many cryptographic systems that use block ciphers with padding. To prevent this attack, it is important to use authenticated encryption modes, such as AES-GCM or ChaCha20-Poly1305, that provide both encryption and authentication of the ciphertext. Additionally, servers should be configured to return a generic error message, rather than specific error messages that reveal information about the encryption process.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值