Mas0n
to be reverse engineer🐧
翻车鱼

2021西湖论剑技能赛初赛

2021西湖论剑技能赛初赛

跟着战队打的。还是挺遗憾的,没能进前30,茶歇区包场的愿望落空了TVT。比赛的时候暴露出我的一个的问题就是分析速度偏慢了。

ROR

这题分值比较低,很幸运的抢到了简单题的一血~

直接z3就可以。

# -*- coding:utf-8 -*-
"""
@Author: Mas0n
@File: test.py
@Time: 2021-11-20 13:21
@Desc: It's all about getting better.
"""
from z3 import *


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

for i in range(0, 40, 8):
    for j in range(8):
        v5 = ((key[j] & flag[i + 3]) << (8 - (3 - j) % 8)) | (
                    (key[j] & flag[i + 3]) >> ((3 - j) % 8)) | (
                         (key[j] & flag[i + 2]) << (8 - (2 - j) % 8)) | (
                         (key[j] & flag[i + 2]) >> ((2 - j) % 8)) | (
                         (key[j] & flag[i + 1]) << (8 - (1 - j) % 8)) | (
                         (key[j] & flag[i + 1]) >> ((1 - j) % 8)) | (
                         (key[j] & flag[i]) << (8 - -j % 8)) | (
                         (key[j] & flag[i]) >> (-j % 8))

        Buf2[j + i] = Select(sbox, 0xff & (((key[j] & flag[i + 7]) << (8 - (7 - j) % 8)) | ((key[j] & flag[i + 7]) >> ((7 - j) % 8)) | ((key[j] & flag[i + 6]) << (8 - (6 - j) % 8)) | ((key[j] & flag[i + 6]) >> ((6 - j) % 8)) | ((key[j] & flag[i + 5]) << (8 - (5 - j) % 8)) | ((key[j] & flag[i + 5]) >> ((5 - j) % 8)) | ((key[j] & flag[i + 4]) << (8 - (4 - j) % 8)) | ((key[j] & flag[i + 4]) >> ((4 - j) % 8)) | v5))

for i, v in enumerate(cmpdata):
    sol.add(Buf2[i] == v)

assert sol.check() == sat
mol = sol.model()

print("".join([chr(mol.eval(i).as_long()) for i in flag]))

TacticalArmed

不知道为啥最近老做到smc的题目,我用的CE trace,IDA trace会卡死,unicorn可能更好一点。

这里只需要关注0040146Dcall [ebp+var_8]方法执行的指令就可以了

https://cdn.shi1011.cn/2021/11/87593a815c96bfff056b52d9ff35265d.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

其他都没什么用,分析十几条指令就会发现很像tea的流程,只不过改了delta和循环次数。

回到调用SMC代码前

https://cdn.shi1011.cn/2021/11/3c788cf4bf615131464d8aac0a200e68.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

循环次数就这么来的,或者耐心点汇编分析轮数

分析的时候会发现key貌似和静态分析的不太一样,因为真正的key在这,而这个key又是由异常处理来的

具体表现为int 2d指令:若存在调试器则正常执行,若无调试器则抛出异常。

https://cdn.shi1011.cn/2021/11/7e744de9c107e094d5cc8c59e7fa1356.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

还有一个修改点是delta连续的,知道这几点后,撸脚本

#include <stdio.h>
#include <stdint.h>
#include <Windows.h>


void decrypt (uint32_t* v, uint32_t* k, int rounds) {
    uint32_t v0=v[0], v1=v[1], sum=0, i;  /* set up */
    uint32_t delta=0x7E5A96D2;
    for (i = 0; i < 33 * rounds + 33; ++i) {
        sum -= delta;
    }

                      /* a key schedule constant */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
    for (i=0; i<33; i++) {                         /* basic cycle start */
        v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
        v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        sum += delta;
    }                                              /* end cycle */
    v[0]=v0; v[1]=v1;
}


int main() {
    uint32_t k[4] = {0x7CE45630, 0x58334908, 0x66398867, 0x0C35195B1};
    uint32_t v[10] = {0x422F1DED, 0x1485E472, 0x035578D5, 0xBF6B80A2, 0x97D77245, 0x2DAE75D1, 0x665FA963, 0x292E6D74,0x9795FCC1, 0x0BB5C8E9};
    for (int i = 0; i < 5; ++i) {
        decrypt(v+2*i, k, i);
        printf("0x%x, 0x%x, ", v[2*i], v[2*i + 1]);
    }
    return 0;
}

小端序

flag = b""
for i in [0x3144676b, 0x3242676f, 0x32614779, 0x41696f72, 0x47695865, 0x71615f38, 0x437a4c6e, 0x46725f4a, 0x72505348, 0x4b35356e]:
    flag += int.to_bytes(i, length=4, byteorder="little")
print(flag)

虚假的粉丝

脑洞题,字母动画挺有意思(

翻找会发现一个hint:定位401379,这里有一个异或

https://cdn.shi1011.cn/2021/11/89e895542e9b86e277b7434eac29fef9.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

执行一遍其实就是/f/ASCII-faded 1999.txt,拿到前两个key

https://cdn.shi1011.cn/2021/11/5f8e4bed7c4ce4c30f52bfa43fd03811.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

其实直接跑脚本匹配也可以

# -*- coding:utf-8 -*-
"""
@Author: Mas0n
@File: test25.py
@Time: 2021-11-20 11:48
@Desc: It's all about getting better.
"""
import os.path

path = r"D:\Downloads\Programs\f"
arr = os.listdir(path)

for pt in arr:
    with open(os.path.join(path, pt), "r") as f:
        text = f.read()
    idx = text.find("U")
    if idx != -1 and text[idx + 39] == 'S':
        print(pt, idx, text[idx: idx+39+1])

# 4157 1118 40

原程序跑一下

https://cdn.shi1011.cn/2021/11/128c1e822221647184ca3d49262b26f7.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

第三个key只知道首尾两位,其实是UzNDcmU3X0szeSUyMCUzRCUyMEFsNE5fd0FsSzNS,base64解码得到key3:Al4N_wAlK3R

https://cdn.shi1011.cn/2021/11/6ae031d8273bd743c97410ed01178182.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

ASCII-faded 5315.txt文件内容异或

https://cdn.shi1011.cn/2021/11/d2ad0938196fc27c19183ac004f3f33a.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

正确输入跑一遍程序,看ASCII-faded 5315.txt,缩小缩小

https://cdn.shi1011.cn/2021/11/757a986fd7743cd54a165ce34ffe9f29.png?imageMogr2/format/webp/interlace/0/quality/90|watermark/2/text/wqlNYXMwbg/font/bXN5aGJkLnR0Zg/fontsize/14/fill/IzMzMzMzMw/dissolve/80/gravity/southeast/dx/5/dy/5

后记

VHDL应该会复现一下,目前思路是bindiff还原符号,结合动调调试分析~总体来讲这次西湖论剑总归还是学到了些的。

本文链接:https://blog.shi1011.cn/ctf/1851
本文采用 CC BY-NC-SA 4.0 Unported 协议进行许可

Mas0n

文章作者

发表回复

textsms
account_circle
email

翻车鱼

2021西湖论剑技能赛初赛
跟着战队打的。还是挺遗憾的,没能进前30,茶歇区包场的愿望落空了TVT。比赛的时候暴露出我的一个的问题就是分析速度偏慢了。 ROR 这题分值比较低,很幸运的抢到了简单题的一血~ …
扫描二维码继续阅读
2021-11-21