본문 바로가기

Computer Security/CTF

[RuCTF 2014 quals] Reversing 500

First, it is packed by upx. so just unpack binary.


then, because of undefined instruction(0F 0B), it starts exception handler.

So, strcmp with "oh_nasty_boy!you_hacked_me:(hehe" is just fake.


exception handler is in 0x4010d0.


At 0x4010d0, insert keys and encode input. and finally compare with another real answer 


routine is as follow.


1. exception handler starts

2. insert key to stack

3. xor key with some value in 0x4011C3

4. encode input using key (function 0x401000)

5. compare encoded value to answer


but, I didn't know xor key with what value in third step.

So, it is just brute-force.


script is as follow in python

http://pastebin.com/MnSv30YB


  1. from struct import *
  2. import sys
  3.  
  4. key1 = "86DE9AF8DFF585E9DD85EF".decode('hex')
  5.  
  6. def encode(data,n):
  7.         global key1
  8.         zero_to_100 = []
  9.         i =0
  10.         while i<0x100:
  11.                 zero_to_100.append(i)
  12.                 i+=1
  13.         temp_key1 = list(key1)
  14.         temp_key = [chr(ord(i) ^ n) for i in key1]
  15.         calc_key1 = "".join(temp_key)
  16.  
  17.         i = 0
  18.         v4 = 0
  19.         v8 = 0
  20.         v3 = 0
  21.         while i<0x100:
  22.                 v3 += (zero_to_100[i] + ord(calc_key1[i % 0xB]))
  23.                 v3 = v3 & 0xFF
  24.                 v8 = zero_to_100[i]
  25.                 zero_to_100[i] = zero_to_100[v3]
  26.                 zero_to_100[v3] = v8
  27.                 i+=1
  28.  
  29.         i=0
  30.         v9 = 0
  31.         v10 = 0
  32.         answer = ""
  33.         while i < 0x20:
  34.                 v12 = v10+1
  35.                 v17 = v12
  36.                 v9 += zero_to_100[v12]
  37.                 v9 = v9 & 0xFF
  38.                 v13 = zero_to_100[v12]
  39.                 v15 = zero_to_100[v9]
  40.                 zero_to_100[v12] = v15
  41.                 zero_to_100[v9] = v13
  42.                 k = zero_to_100[(v13 + v15) & 0xFF]
  43.                 answer += chr(ord(data[i]) ^ k)
  44.                 v10 = v17
  45.                 i+=1
  46.  
  47.         return answer
  48.  
  49. def check(data):
  50.         data = list(data)
  51.         for i in data:
  52.                 if ord(i) < 0x20 or ord(i) >= 0x80:
  53.                         return False
  54.  
  55.         return True
  56.  
  57. compare = ""
  58. compare += pack('<L', 0x03C7C8CA)
  59. compare += pack('<L', 0x1F2810FC)
  60. compare += pack('<L', 0x948C7F7A)
  61. compare += pack('<L', 0x2469F92E)
  62. compare += pack('<L', 0xC1277D9F)
  63. compare += pack('<L', 0x7F4509C4)
  64. compare += pack('<L', 0x9745EE75)
  65. compare += pack('<L', 0x1F79AF8D)
  66.  
  67. for n in range(0,0x100):
  68.         xored = encode("1"*32,n)
  69.  
  70.         key = ""
  71.         for i in xored:
  72.                 key += chr(ord(i) ^ ord('1'))
  73.  
  74.         i=0
  75.         answer = ""
  76.         while i< 32:
  77.                 answer += chr(ord(key[i]) ^ ord(compare[i]))
  78.                 i+=1
  79.  
  80.         if check(answer):
  81.                 print answer
  82.                 sys.exit()


'Computer Security > CTF' 카테고리의 다른 글

[Defcon 2014] HJ(2) byhd write up  (0) 2014.05.19
[Defcon 2014] 100 lines exploit  (0) 2014.05.19
[Codegate 2014 quals] Web 500 write up  (0) 2014.02.24
[secuinside 2013] debugd exploit  (2) 2013.11.28
[Secuinside 2013] angry danbi exploit  (3) 2013.11.27