Advertisement
Grollicus

CSAW Quals 2013 - rev300

Sep 23rd, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. #CSAW Quals 2013 - rev300
  2.  
  3. # The Check algorithm was basically
  4. # check(unsigned char* buf) {
  5. # int v4 = 1337
  6. # while(*buf) {
  7. #   v4 += v4*32+(*buf);
  8. #   ++buf;
  9. # }
  10. # and was used if (check(buf) == 0xEF2E3558) {send(key);}
  11. # so the goal of this challenge was to craft a string with a check() result of 0xFE2E3778.
  12.  
  13.  
  14. #!/usr/bin/env python
  15. #Solving rev300 with z3
  16.  
  17. def int_overflow(val): #simulate signed 32bit integer overflow in python
  18.   max = 2**31-1
  19.   if not -max-1 <= val <= max:
  20.     val = (val + (max + 1)) % (2 * (max + 1)) - max - 1
  21.   return val
  22.  
  23. def check(s):
  24.     s = 1337
  25.     for c in s:
  26.         s += 32*s+ord(c)
  27.         s = int_overflow(s)
  28.     return s == -0x10d1caa8 #0xEF2E3558 signed
  29.  
  30. from z3 import *
  31.  
  32. solver = Solver()
  33. c = BitVec('c', 32)
  34. #unrolled the loop 7 times, should be enough to modify all 32 bits
  35. x_0 = BitVec('x_0', 32)
  36. c_1 = c+c*32+x_0
  37. x_1 = BitVec('x_1', 32)
  38. c_2 = c_1+c_1*32+x_1
  39. x_2 = BitVec('x_2', 32)
  40. c_3 = c_2+c_2*32+x_2
  41. x_3 = BitVec('x_3', 32)
  42. c_4 = c_3+c_3*32+x_3
  43. x_4 = BitVec('x_4', 32)
  44. c_5 = c_4+c_4*32+x_4
  45. x_5 = BitVec('x_5', 32)
  46. c_6 = c_5+c_5*32+x_5
  47. x_6 = BitVec('x_6', 32)
  48. c_7 = c_6+c_6*32+x_6
  49.  
  50. solver.add(c == 1337)
  51. solver.add(c_7 == 0xEF2E3558)
  52. for x in [x_0, x_1, x_2, x_3, x_4, x_5, x_6]:
  53.     solver.add(x & 0xFFFFFF00 == 0)
  54.     solver.add(x != 0)
  55. solver.check()
  56. m = solver.model()
  57. print m
  58.  
  59. s = ''
  60. for x in [x_0, x_1, x_2, x_3, x_4, x_5, x_6]:
  61.     print m[x]
  62.     s += chr(int(str(m[x])))
  63.  
  64. print repr(s), check(s)
  65.  
  66. #running this gave me the string '\t\x8b\xff\x89\x9eq\xf4' which i then submitted using this script:
  67.  
  68. #!/usr/bin/env python
  69. from pwn import *
  70.  
  71. s='\t\x8b\xff\x89\x9eq\xf4\n'
  72. r = remote("128.238.66.218", 54321)
  73. print r.recv()
  74. r.send(s)
  75. print r.recv()
  76. print r.recv()
  77. print r.recv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement