KCTF2021秋季赛 第二题迷失丛林

基本流程

通过查找字符串‘TryAgain'定位到函数sub_401270、查找’GoodJob~‘定位到函数sub_401580。关键校验函数在sub_401580()中。

流程如下:

1. 输入Code长度为0x20

2. sub_4014A0()要求Code为数字0-9或字符A-F,将字符转换为16进制数。每个字节内容交换高低位存储。例如,假设输入的内容为0x12345678,系统将存储为0x21436587。最终32个字符转换为了16字节的16进制数

3. 将Code分为两半part1,part2。

4. 将part1拷贝到内置的404000[]中的前8个字节,进入sub_401580()进行校验。

5. 用第一个算法验证404000[]

6. 通过第二个算法变换404000[]

7.通过第三个算法对part2和404000[]进行运算,要求得到的结果与“GoodJob~”一致。

第一个算法流程:

循环0x100次,count=1

{

根据算法可以知道,count = [0, 2, 4, 8, 0x10, 0x20,

0x40, 0x80],每一次生成:固定两个字符分别来自404000[count]、count,其余2*2+4*2+8*2+0x10*2+0x20*2+0x40*2+0x80*2,

即为:2+2*2+4*2+8*2+0x10*2+0x20*2+0x40*2+0x80*2=510=0x1FE个字节字符来自于前面元素的404000[404220[i]]和404220[i]+1,存储到404220[]中。

然后,选取404220[]最后256元素(40431E—40441D),给每个元素计数,存到(404420+count*0x100)[]的对应位置上。

count++

}

循环0x100次,每次检查一个矩阵。检查每个生成的矩阵404520[]、404620[]、404720[]……

如果每个矩阵的    array[0]!=0,[19F768]++

                            array[0xE]!=0,[19F769]++

                            array[0x28]!=0,[19F76A]++

                            array[0x4F]!=0,[19F76B]++

要求[19F768]==0xA9, [19F769]==0xAC, [19F76A]==0xA7,[19F76B]>0xC8

求解第一部分part1

由于404000[i]各元素各不重复,先找到缺失的8字节的值:missing_set={0x1e, 0x28, 0x4b, 0x6d,0x8c, 0xa3, 0xd2, 0xfb}

ary_init = bytes([     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x9B, 0xF4, 0xDF, 0xAC, 0x7C, 0xA1, 0xC6,     0x16, 0xD0, 0x0F, 0xDD, 0xDC, 0x73, 0xC5, 0x6B, 0xD1, 0x96, 0x47, 0xC2, 0x26, 0x67, 0x4E, 0x41,     0x82, 0x20, 0x56, 0x9A, 0x6E, 0x33, 0x92, 0x88, 0x29, 0xB5, 0xB4, 0x71, 0xA9, 0xCE, 0xC3, 0x34,     0x50, 0x59, 0xBF, 0x2D, 0x57, 0x22, 0xA6, 0x30, 0x04, 0xB2, 0xCD, 0x36, 0xD5, 0x68, 0x4D, 0x5B,     0x45, 0x9E, 0x85, 0xCF, 0x9D, 0xCC, 0x61, 0x78, 0x32, 0x76, 0x31, 0xE3, 0x80, 0xAD, 0x39, 0x4F,     0xFA, 0x72, 0x83, 0x4C, 0x86, 0x60, 0xB7, 0xD7, 0x63, 0x0C, 0x44, 0x35, 0xB3, 0x7B, 0x19, 0xD4,     0x69, 0x08, 0x0B, 0x1F, 0x3D, 0x11, 0x79, 0xD3, 0xEE, 0x93, 0x42, 0xDE, 0x23, 0x3B, 0x5D, 0x8D,     0xA5, 0x77, 0x5F, 0x58, 0xDB, 0x97, 0xF6, 0x7A, 0x18, 0x52, 0x15, 0x74, 0x25, 0x62, 0x2C, 0x05,     0xE8, 0x0D, 0x98, 0x2A, 0x43, 0xE2, 0xEF, 0x48, 0x87, 0x49, 0x1C, 0xCA, 0x2B, 0xA7, 0x8A, 0x09,     0x81, 0xE7, 0x53, 0xAA, 0xFF, 0x6F, 0x8E, 0x91, 0xF1, 0xF0, 0xA4, 0x46, 0x3A, 0x7D, 0x54, 0xEB,     0x2F, 0xC1, 0xC0, 0x0E, 0xBD, 0xE1, 0x6C, 0x64, 0xBE, 0xE4, 0x02, 0x3C, 0x5A, 0xA8, 0x9F, 0x37,     0xAF, 0xA0, 0x13, 0xED, 0x1B, 0xEC, 0x8B, 0x3E, 0x7E, 0x27, 0x99, 0x75, 0xAB, 0xFE, 0xD9, 0x3F,     0xF3, 0xEA, 0x70, 0xF7, 0x95, 0xBA, 0x1D, 0x40, 0xB0, 0xF9, 0xE5, 0xF8, 0x06, 0xBC, 0xB6, 0x03,     0xC9, 0x10, 0x9C, 0x2E, 0x89, 0x5C, 0x7F, 0xB1, 0x1A, 0xD6, 0x90, 0xAE, 0xDA, 0xE6, 0x5E, 0xB9,     0x84, 0xE9, 0x55, 0xBB, 0xC7, 0x0A, 0xE0, 0x66, 0xF2, 0xD8, 0xCB, 0x00, 0x12, 0xB8, 0x17, 0x94,     0x6A, 0x4A, 0x01, 0x24, 0x14, 0x51, 0x07, 0x65, 0x21, 0xC8, 0x38, 0xFD, 0x8F, 0xC4, 0xF5, 0xFC, ])

missing_set = set(ary_init [8:]).symmetric_difference(range(256))

验证时,把这8字节排列组合,过了条件的只有这一组{0x4b, 0x6d, 0x28, 0x8c, 0xfb, 0xd2, 0x1e,0xa3}

具体的求解算法如下所示:

import itertools   def test():    

  code1 = bytearray(8)      

  # 00404000    

  ary_init = bytes([         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x9B, 0xF4, 0xDF, 0xAC, 0x7C, 0xA1, 0xC6,         0x16, 0xD0, 0x0F, 0xDD, 0xDC, 0x73, 0xC5, 0x6B, 0xD1, 0x96, 0x47, 0xC2, 0x26, 0x67, 0x4E, 0x41,         0x82, 0x20, 0x56, 0x9A, 0x6E, 0x33, 0x92, 0x88, 0x29, 0xB5, 0xB4, 0x71, 0xA9, 0xCE, 0xC3, 0x34,         0x50, 0x59, 0xBF, 0x2D, 0x57, 0x22, 0xA6, 0x30, 0x04, 0xB2, 0xCD, 0x36, 0xD5, 0x68, 0x4D, 0x5B,         0x45, 0x9E, 0x85, 0xCF, 0x9D, 0xCC, 0x61, 0x78, 0x32, 0x76, 0x31, 0xE3, 0x80, 0xAD, 0x39, 0x4F,         0xFA, 0x72, 0x83, 0x4C, 0x86, 0x60, 0xB7, 0xD7, 0x63, 0x0C, 0x44, 0x35, 0xB3, 0x7B, 0x19, 0xD4,         0x69, 0x08, 0x0B, 0x1F, 0x3D, 0x11, 0x79, 0xD3, 0xEE, 0x93, 0x42, 0xDE, 0x23, 0x3B, 0x5D, 0x8D,         0xA5, 0x77, 0x5F, 0x58, 0xDB, 0x97, 0xF6, 0x7A, 0x18, 0x52, 0x15, 0x74, 0x25, 0x62, 0x2C, 0x05,         0xE8, 0x0D, 0x98, 0x2A, 0x43, 0xE2, 0xEF, 0x48, 0x87, 0x49, 0x1C, 0xCA, 0x2B, 0xA7, 0x8A, 0x09,         0x81, 0xE7, 0x53, 0xAA, 0xFF, 0x6F, 0x8E, 0x91, 0xF1, 0xF0, 0xA4, 0x46, 0x3A, 0x7D, 0x54, 0xEB,         0x2F, 0xC1, 0xC0, 0x0E, 0xBD, 0xE1, 0x6C, 0x64, 0xBE, 0xE4, 0x02, 0x3C, 0x5A, 0xA8, 0x9F, 0x37,         0xAF, 0xA0, 0x13, 0xED, 0x1B, 0xEC, 0x8B, 0x3E, 0x7E, 0x27, 0x99, 0x75, 0xAB, 0xFE, 0xD9, 0x3F,         0xF3, 0xEA, 0x70, 0xF7, 0x95, 0xBA, 0x1D, 0x40, 0xB0, 0xF9, 0xE5, 0xF8, 0x06, 0xBC, 0xB6, 0x03,         0xC9, 0x10, 0x9C, 0x2E, 0x89, 0x5C, 0x7F, 0xB1, 0x1A, 0xD6, 0x90, 0xAE, 0xDA, 0xE6, 0x5E, 0xB9,         0x84, 0xE9, 0x55, 0xBB, 0xC7, 0x0A, 0xE0, 0x66, 0xF2, 0xD8, 0xCB, 0x00, 0x12, 0xB8, 0x17, 0x94,         0x6A, 0x4A, 0x01, 0x24, 0x14, 0x51, 0x07, 0x65, 0x21, 0xC8, 0x38, 0xFD, 0x8F, 0xC4, 0xF5, 0xFC,     ])      

  # first part    

  missing_set = set(ary_init[8:]).symmetric_difference(range(256))

  for missing in itertools.permutations(missing_set, 8):        

     ary = bytearray(ary_init)        

     ary[0:8] = bytes(missing)          

     # 00404420        

     ary2 = bytearray(256*256)          

     # 00404220        

     ary3 = bytearray(256*2)          

     for k in range(256):            

        ary3[0] = ary[k]            

        ary3[1] = (k + 1) & 0xFF            

        idx = 0              

        # 00404100: 2 + 4 + 8 + 0x10 + 0x20 + 0x40 + 0x80              

        for i in range(254):                

          ary3[2 + 2 * i] = ary[ary3[i]]                

          ary3[3 + 2 * i] = (ary3[i] + 1) & 0xFF              

       for i in range(254, 510):                

          ary2[k * 256 + ary3[i]] += 1                

          ary2[k * 256 + ary3[i]] &= 0xFF          

  v0 = 0        

  v1 = 0         

  v2 = 0        

  v3 = 0          

  for i in range(256):            

    if ary2[i * 256 + 0x00] != 0:                

      v0 += 1                

      v0 &= 0xFF              

   if ary2[i * 256 + 0x0E] != 0:                

      v1 += 1                

      v1 &= 0xFF              

   if ary2[i * 256 + 0x28] != 0:                

      v2 += 1                

      v2 &= 0xFF              

   if ary2[i * 256 + 0x4F] != 0:                

       v3 += 1                

       v3 &= 0xFF          

  if v0 == 0xA9 and v1 == 0xAC and v2 == 0xA7 and v3 > 0xC8:            

     code1[:] = bytes(missing)             

     print(code1)            

     break  

test()

求解第2部分part2

将之前求解出的part1代入,则404000[]矩阵经过第二个算法的变换后,确定性变为

第三个算法

将404000[]前8个字节拷贝到414420[]中。其经过8次变换,得到最终的414420[]。

414420[]各字节的变换是独立的,8字节part2对应单字节的影响。我们可以枚举part2[i]的值,从0x00到0xFF对414420[]进行变换,如果最终得到"GoodJob~",则标识找到了对应的part2[i]。

注意:从上图红框标识处可以看出,最终对414420[]的第一个字节和最后一个字节数值各减一。因此,比较字符串'GoodJob~'变成[0x48, 0x6F, 0x6F, 0x64,0x4A, 0x6F, 0x62,0x7F]。求解脚本如下:

key_res=[0xC1,0x9B,0x7F,0x58,0x64,0xD5,0x77,0x21,0x74,0xEB,0x14,0xBF,0xDF,0x25,0x5A,0x37, 0x85,0x2C,0xAF,0x8C,0xDA,0x26,0xE2,0x7A,0x87,0x4C,0x60,0x99,0x54,0x3C,0x95,0xC0, 0xB9,0x0C,0xBC,0x0E,0xE7,0x2D,0x86,0xBE,0x67,0xD3,0xD8,0xFC,0x30,0xB6,0xC8,0x57, 0x1E,0x62,0x3E,0xCE,0xA0,0xCD,0xF5,0xEE,0xA7,0xCF,0x45,0xFE,0xD0,0x80,0x05,0xAD, 0x13,0xF3,0xB7,0x6B,0x22,0x2B,0xBD,0x69,0x42,0x4B,0xA5,0xEA,0xA6,0xD2,0x6F,0x4F, 0x4E,0x07,0xE1,0x36,0x01,0xB5,0xAA,0xB1,0x94,0x0B,0x35,0x3A,0xC7,0x49,0x53,0x82, 0xC3,0x7B,0x32,0xFF,0x19,0xC4,0xF1,0xC9,0xE8,0xF7,0x56,0x15,0xA3,0x46,0x89,0x43, 0x9D,0x8F,0x20,0xEF,0xBB,0x2A,0xCB,0x09,0x93,0x4A,0x1C,0xE3,0x33,0xD1,0xE0,0x1D, 0x72,0x7C,0x27,0xE9,0x17,0x28,0x6D,0x6A,0xD9,0x00,0x9A,0xE5,0x63,0xDE,0x23,0x9F, 0x0D,0x47,0x3B,0x65,0x08,0x84,0x6C,0x1A,0x88,0x12,0xA1,0xA4,0xB3,0x18,0x24,0x1B, 0xD7,0x44,0xDB,0xAC,0x6E,0x7D,0x51,0x5E,0xED,0x50,0xD6,0x11,0x5B,0x9C,0xB4,0x68, 0x3D,0x2F,0x03,0x40,0xBA,0x2E,0xCA,0x02,0xE6,0xA8,0xEC,0x83,0x06,0x5D,0xB8,0x4D, 0x97,0x66,0xF0,0xFB,0x8A,0x55,0xAB,0xB2,0x04,0xFA,0x0A,0x31,0x71,0xCC,0x8B,0x73, 0xA9,0x48,0x5C,0xF9,0x98,0xE4,0xC6,0x34,0xC5,0x7E,0x81,0x75,0x90,0x1F,0x92,0x3F, 0x9E,0x10,0x29,0x52,0x39,0xF4,0x41,0x78,0x5F,0x16,0x79,0xC2,0xB0,0xDD,0xF2,0x61, 0x0F,0x70,0xD4,0x91,0xDC,0xF6,0xF8,0xFD,0x59,0x38,0x8D,0x96,0xAE,0x8E,0x76,0xA2]

res=[]  

for i in range(8):    

  res.append(key_res[i])

target = [0x48, 0x6F, 0x6F, 0x64, 0x4A, 0x6F, 0x62, 0x7F]

for pos in range(len(target)):    

  for number in range(0xFF):        

    source = number        

    for i in range(8):            

       if number & 1 != 0:                

         res[pos] = (res[pos] + 1) % 256            

       else:                

         res[pos] = key_res[res[pos]]            

       number = number >> 1        

     if res[pos] == target[pos]:            

       print('%#x'%source)        

     res[pos] = key_res[pos]

part2={0x9d, 0x6b, 0xea, 0x2f, 0xa4, 0x8, 0xbc,0x22}

结论

算得最终Code为:“B4D682C8BF2DE13AD9B6AEF24A80CB22”

你可能感兴趣的:(KCTF2021秋季赛 第二题迷失丛林)