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 (