ATuin

Oracle Padding attack

Jul 20th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.70 KB | None | 0 0
  1. package com.logikujo.CryptoI
  2.  
  3. /**
  4.  *
  5.  * sCryptoDSL / LogiDev - [Fun Functional] / Logikujo.com
  6.  *
  7.  * com.logikujo.CryptoI 4/05/13 :: 17:44 :: ATuin
  8.  *
  9.  */
  10.  
  11. import Implicits._
  12. import dispatch._
  13. import Defaults._
  14. import com.ning.http.client.RequestBuilder
  15.  
  16. object Week4 {
  17.   val targetHost:RequestBuilder = host("target-host.com") / "testOP"
  18.   val queryParam = "someQueryParam"
  19.   val goodQueryValue = "f20bdba6ff29eed7b046d1df9fb7000058b1ffb4210a580f748b4ac714c001bd4a61044426fb515dad3f21f18aa577c0bdf302936266926ff37dbf7035d5eeb4"
  20.  
  21.   trait AttackStatus
  22.   case class Success() extends AttackStatus
  23.   case object BadPadding extends AttackStatus
  24.   case class BadMessage(pad: Int, msg: Hex) extends AttackStatus
  25.   case object Unknow extends AttackStatus
  26.  
  27.   trait AttackRequest {
  28.     val createRequest: () => RequestBuilder
  29.     val ct: Hex
  30.     val iv: Hex
  31.     lazy val blocksStream: Stream[Hex] = ct.grouped(16).toStream
  32.  
  33.     private def nullIV(n: Int): Hex = List.fill(n)("00").mkString.hex
  34.     private def prepareFakeIV(pad: Int): Hex = nullIV((16 - pad) + 1) | Hex(pad - 1)(pad.toByte)
  35.     private def prepareFakeIV(interValue: Hex, pad: Int): Hex =
  36.       interValue xor prepareFakeIV(pad)
  37.  
  38.     def guessResponse(h: Hex) = Http(createRequest() <<? Map("er" -> h.toString) OK as.String).either
  39.  
  40.     def guess(block: Hex)(iv: Hex)(pad: Int):AttackStatus = {
  41.       val m = iv | block
  42.       val response = Http(createRequest() <<? Map("er" -> m.toString) OK as.String).either
  43.       response() match {
  44.         case Right(_) => Success()
  45.         case Left(StatusCode(404)) => BadMessage(pad, (iv xor (nullIV(16 - pad) | Hex(pad)(pad.toByte))))
  46.         case Left(StatusCode(403)) => BadPadding
  47.         case Left(StatusCode(_)) => Unknow
  48.       }
  49.     }
  50.  
  51.     def breakByte(n: Int)(pad: Int)(interValue: Hex): Option[AttackStatus] = {
  52.       print("+-------> Trying padding 0x%02x: ".format(pad))
  53.       val block = blocksStream.drop(n - 1).take(16).head
  54.       val f = guess(block)_
  55.       val iv = interValue xor prepareFakeIV(pad)
  56.       Iterator.tabulate(0xff) {
  57.         i => {
  58.           if (i % 2 == 0)
  59.             print(".")
  60.           val ivFake = iv.setByte(16 - pad, i.toByte)
  61.           f(ivFake)(pad)
  62.         }
  63.       }.find {
  64.           case BadMessage(_,inter) => {
  65.             if (inter(pad).isDefined)
  66.               println(" SUCCESS: %02x".format(inter(16 - pad).getOrElse(0)))
  67.             else
  68.               println(" SUCCESS: 0x??")
  69.             true
  70.           }
  71.           case _ => false
  72.       }
  73.     }
  74.  
  75.     private def recBreakBlock(f: Int => Hex => Option[AttackStatus],
  76.                             pad: Int,
  77.                             interValue:Option[AttackStatus]): Option[Hex] =
  78.       if (pad > 16) interValue match {
  79.         case Some(BadMessage(_, inter)) => Some(inter)
  80.         case _ => None
  81.       }
  82.       else {
  83.         interValue match {
  84.           case Some(BadMessage(_, inter)) => recBreakBlock(f, pad + 1, f(pad)(inter))
  85.           case _ => None
  86.         }
  87.       }
  88.  
  89.     def breakBlock(n: Int): Option[Hex] = {
  90.       println("+ Breaking block: %d".format(n))
  91.       val f = breakByte(n)_
  92.       recBreakBlock(f, 1, Some(BadMessage(1,prepareFakeIV(1))))
  93.     }
  94.  
  95.     def breakBlock(n:Int, interValue: Hex) = {
  96.       val pad = (16 - interValue.value.dropWhile(0 ==).length) + 1
  97.       val f = breakByte(n)_
  98.       recBreakBlock(f, pad, Some(BadMessage(pad, interValue)))
  99.     }
  100.   }
  101.  
  102.   trait Attack extends AttackRequest
  103.   object Attack {
  104.     def apply(f: () => RequestBuilder) = (h: Hex) => new AttackRequest {
  105.       val createRequest = f
  106.       val ct = h.drop(16)
  107.       val iv = h.take(16)
  108.     }
  109.   }
  110.  
  111.   val cryptoIAttack = Attack(() => host("crypto-class.appspot.com") / "po")
  112. }
Advertisement
Add Comment
Please, Sign In to add comment