|
| 1 | +package com.threedr3am.bug.paddingoraclecbc; |
| 2 | + |
| 3 | +import com.vip.vjtools.vjkit.security.CryptoUtil; |
| 4 | +import com.vip.vjtools.vjkit.text.EncodeUtil; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.function.Predicate; |
| 7 | + |
| 8 | +/** |
| 9 | + * padding oracle java实现(多组密文实现) |
| 10 | + * |
| 11 | + * @author threedr3am |
| 12 | + */ |
| 13 | +public class PaddingOracle { |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + //aes iv |
| 17 | + String aesIv = "BSiv194+mpLpYJDOsuuBYA=="; |
| 18 | + //aes key |
| 19 | + String aesKey = "e/ACMnCbFqab+v/cCIv3gA=="; |
| 20 | + //原文 |
| 21 | + String plain = "1dmin1dmin1dmin1dmin1dmin1dmin1dmin1dmin"; |
| 22 | + //cbc攻击,最后期望服务器解密出的结果 |
| 23 | + String cbcRes = "adminadminadminadminadminadminadminadmin"; |
| 24 | + |
| 25 | + //密文bytes |
| 26 | + byte[] cryptTextBytes = CryptoUtil.aesEncrypt(plain.getBytes(), EncodeUtil.decodeBase64(aesKey), EncodeUtil.decodeBase64(aesIv)); |
| 27 | + //aes iv bytes |
| 28 | + byte[] ivBytes = EncodeUtil.decodeBase64(aesIv); |
| 29 | + |
| 30 | + String paddingPlain = paddingOracle(cryptTextBytes, ivBytes, cbcRes.getBytes(), data -> { |
| 31 | + //前16bytes是iv |
| 32 | + byte[] ivTmp = Arrays.copyOfRange(data, 0, 16); |
| 33 | + //后16bytes是密文 |
| 34 | + byte[] cryptTmp = Arrays.copyOfRange(data, 16, 32); |
| 35 | + try { |
| 36 | + CryptoUtil |
| 37 | + .aesDecrypt(cryptTmp, EncodeUtil.decodeBase64(aesKey), ivTmp); |
| 38 | + return true; |
| 39 | + } catch (Exception e) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + private static String paddingOracle(byte[] cryptText, byte[] ivBytes, byte[] cbcResBytes, Predicate<byte[]> predicate) { |
| 46 | + byte[] middles = new byte[cryptText.length]; |
| 47 | + byte[] data = new byte[ivBytes.length + cryptText.length]; |
| 48 | + System.arraycopy(ivBytes, 0, data, 0, ivBytes.length); |
| 49 | + System.arraycopy(cryptText, 0, data, ivBytes.length, cryptText.length); |
| 50 | + int group = cryptText.length / 16; |
| 51 | + byte[] plainBytes = new byte[cryptText.length]; |
| 52 | + //padding oracle attack |
| 53 | + for (int i = group - 1; i >= 0; i--) { |
| 54 | + byte[] iv = Arrays.copyOfRange(data, i * 16, i * 16 + 16); |
| 55 | + byte[] crypt = Arrays.copyOfRange(data, (i + 1) * 16, (i + 1) * 16 + 16); |
| 56 | + byte[] middle = paddingOracle(iv, crypt, predicate); |
| 57 | + System.arraycopy(middle, 0, middles, i * 16, 16); |
| 58 | + byte[] res = generatePlain(iv, middle); |
| 59 | + System.arraycopy(res, 0, plainBytes, i * 16, res.length); |
| 60 | + res = unpadding(res); |
| 61 | + System.out.println("[plain]:" + new String(res)); |
| 62 | + } |
| 63 | + byte[] tmp = Arrays.copyOf(plainBytes, plainBytes.length); |
| 64 | + String plain = new String(unpadding(tmp)); |
| 65 | + System.out.println("[final-plain]:" + plain); |
| 66 | + return plain; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * unpadding |
| 71 | + * |
| 72 | + * @param res |
| 73 | + * @return |
| 74 | + */ |
| 75 | + private static byte[] unpadding(byte[] res) { |
| 76 | + int end = res[res.length - 1]; |
| 77 | + if (end > 0 && end <= 16) { |
| 78 | + for (int i = 1; i <= end; i++) { |
| 79 | + if (res[res.length - i] != end) |
| 80 | + break; |
| 81 | + } |
| 82 | + for (int i = 1; i <= end; i++) { |
| 83 | + res[res.length - i] = ' '; |
| 84 | + } |
| 85 | + } |
| 86 | + return res; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 原来的iv ^ middle = plain |
| 91 | + * 构造新的iv ^ middle = 'admin' -> 新的iv = middle ^ 'admin' |
| 92 | + * |
| 93 | + * |
| 94 | + * |
| 95 | + * @param iv |
| 96 | + * @param cryptText |
| 97 | + * @param cbcResBytesForPadding |
| 98 | + * @param plainBytes |
| 99 | + * @return |
| 100 | + */ |
| 101 | + private static byte[] cbcAttack(byte[] iv, byte[] cryptText, byte[] cbcResBytesForPadding, |
| 102 | + byte[] plainBytes) { |
| 103 | + byte[] cbcTmp = new byte[iv.length + cbcResBytesForPadding.length]; |
| 104 | + System.arraycopy(iv, 0, cbcTmp, 0, iv.length); |
| 105 | + System.arraycopy(cbcResBytesForPadding, 0, cbcTmp, iv.length, cbcResBytesForPadding.length); |
| 106 | + byte[] plainTmp = new byte[iv.length + plainBytes.length]; |
| 107 | + System.arraycopy(iv, 0, plainTmp, 0, iv.length); |
| 108 | + System.arraycopy(plainBytes, 0, plainTmp, iv.length, plainBytes.length); |
| 109 | + byte[] cryptTmp = new byte[iv.length + cryptText.length]; |
| 110 | + System.arraycopy(iv, 0, cryptTmp, 0, iv.length); |
| 111 | + System.arraycopy(cryptText, 0, cryptTmp, iv.length, cryptText.length); |
| 112 | + |
| 113 | + for (int i = cbcTmp.length - 1; i >= 16; i--) { |
| 114 | + if (cbcTmp[i] != plainTmp[i]) { |
| 115 | + cryptTmp[i - 16] = (byte) (cryptTmp[i - 16] ^ plainTmp[i] ^ cbcTmp[i]); |
| 116 | + } |
| 117 | + } |
| 118 | + return cryptTmp; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * cbc前需要对数据进行填充至16*n,填充规则,最后一组16bytes数据,缺多少位(例如:6),则填充6个0x06 |
| 123 | + * |
| 124 | + * @param cbcResBytes 需要填充的cbc翻转的数据 |
| 125 | + * @return 填充后的数据,可以直接用于cbc翻转攻击 |
| 126 | + */ |
| 127 | + private static byte[] padding(byte[] cbcResBytes) { |
| 128 | + //计算需要填充的位数 |
| 129 | + int padding = 16 - cbcResBytes.length % 16; |
| 130 | + byte[] cbcResBytesForPadding = new byte[cbcResBytes.length + padding]; |
| 131 | + for (int i = 0; i < cbcResBytes.length; i++) { |
| 132 | + cbcResBytesForPadding[i] = cbcResBytes[i]; |
| 133 | + } |
| 134 | + //填充 |
| 135 | + for (int i = 1; i <= padding; i++) { |
| 136 | + cbcResBytesForPadding[cbcResBytesForPadding.length - i] = (byte) padding; |
| 137 | + } |
| 138 | + return cbcResBytesForPadding; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 与原iv异或,生成原文 |
| 143 | + * 原来的iv ^ middle = plain |
| 144 | + * |
| 145 | + * @param ivBytesOld |
| 146 | + * @param middles |
| 147 | + * @return |
| 148 | + */ |
| 149 | + private static byte[] generatePlain(byte[] ivBytesOld, byte[] middles) { |
| 150 | + byte[] res = new byte[ivBytesOld.length]; |
| 151 | + for (int i = 0; i < ivBytesOld.length; i++) { |
| 152 | + res[i] = (byte) (ivBytesOld[i] ^ middles[i]); |
| 153 | + } |
| 154 | + return res; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * padding oracle 核心方法 |
| 159 | + * |
| 160 | + * @param iv |
| 161 | + * @param crypt |
| 162 | + * @param predicate 该对象test方法用于校验是否正确,多数为调用远程服务器提交数据,根据返回来确定解密后的异或结果是否符合规则 |
| 163 | + * @return 这一组数据的middle |
| 164 | + */ |
| 165 | + private static byte[] paddingOracle(byte[] iv, byte[] crypt, Predicate<byte[]> predicate) { |
| 166 | + byte[] data = new byte[iv.length+crypt.length]; |
| 167 | + System.arraycopy(iv, 0, data, 0, iv.length); |
| 168 | + System.arraycopy(crypt, 0, data, iv.length, crypt.length); |
| 169 | + byte[] middle = new byte[16]; |
| 170 | + //用于填充的tmp值,第一轮为0x01第二轮为0x02以此类推 |
| 171 | + byte tmp = 1; |
| 172 | + for (int i = 15; i >= 0; i--) { |
| 173 | + for (int j = 0; j < 256; j++) { |
| 174 | + data[i] = (byte) j; |
| 175 | + for (int k = 15; k > i; k--) { |
| 176 | + //爆破倒数第一位时不需要填充,倒数第二位时填充其后的iv为0x02,倒数第三位时填充其后的iv为0x03,以此类推 |
| 177 | + data[k] = (byte) (middle[k] ^ tmp); |
| 178 | + } |
| 179 | + //解密返回true即正确 |
| 180 | + if (predicate.test(data)) { |
| 181 | + //爆破倒数第一位的解密结果为0x01,第二位为0x02,以此类推,那么middle = iv ^ plain |
| 182 | + byte m = (byte) (j ^ tmp); |
| 183 | + middle[i] = m; |
| 184 | +// break; |
| 185 | + } |
| 186 | + } |
| 187 | + tmp++; |
| 188 | + } |
| 189 | + |
| 190 | + System.out.print("[middle]:"); |
| 191 | + for (int i = 0; i < middle.length; i++) { |
| 192 | + System.out.print((0xff & middle[i])); |
| 193 | + if (i != middle.length - 1) { |
| 194 | + System.out.print(","); |
| 195 | + } |
| 196 | + } |
| 197 | + System.out.println(); |
| 198 | + return middle; |
| 199 | + } |
| 200 | +} |
0 commit comments