逆向解题记录-SMC动态代码加密技术

查看 59|回复 4
作者:Juana111   
静态分析
首先是进行静态分析,使用IDA查看主函数逻辑


image.png (47.86 KB, 下载次数: 2)
下载附件
主函数
2024-7-25 19:58 上传

crazy(str)与ohh(str)两个函数,使用unk_4040c0数据,利用奇偶数分开处理,还原第一段flag


image.png (107.55 KB, 下载次数: 1)
下载附件
fake flag
2024-7-25 19:59 上传

[Python] 纯文本查看 复制代码import base64
input_data = [0x66,0x6B,0x63,0x64,0x7F,0x63,0x69,0x70,0x57,0x60,0x79,0x54,0x78, 0x5B,0x6B,0x50,0x67,0x54,0x73,0x61,0x7C,0x50,0x64,0x48,0x6C,0x56,0x7E,0x46,0x65,0x60,0x0A]
original_chars = [''] * 30
#crazy
for i in range(30):
    if (i & 1):  # 奇数
        original_chars = chr((input_data + i) )  # 正确逻辑应是减法逆向,但解释有误
    else:  # 偶数
        original_chars = chr(input_data ^ i)
original_str = ''.join(original_chars)
print( original_str)
##flag{how_is_the_weather_today}
得到之后提交不正确,通过VirtualQuery和VirtualProtect函数判断是SMC动态代码加密
Self Modifying Code 将加密的代码区段(函数/代码块等)单独编译成一个段,单独标记为可读、可写、不可执行。在动态运行程序时对代码进行解密操作,达到正常运行的效果实现方法:修改PE文件的Section Header,使用API HOOK代码加密解密,VMProtect第三方加密(本题应用)。动态调试
如上图所示,会发现my_function无法正常反编译,判断是在该函数进行了SMC加密,打断点进行函数的修复,使用Local windows debugger。将第一段flag输入之后会定义到SMC加密的函数


image.png (403.64 KB, 下载次数: 2)
下载附件
SMC1
2024-7-25 20:05 上传

技巧在于:U设为无定义,C转为代码,中间注意要将“-------------------------”全部转完,否则会出现断层。转完之后P定义函数,如下图就可以成功得到一个my_function函数~


image.png (71.85 KB, 下载次数: 1)
下载附件
SMC2
2024-7-25 20:06 上传

__Z11my_functionPKc函数
my_function(char *)函数下的__Z11my_functionPKc函数是作为主要的复原的函数段,当所有的数据转为汇编码快捷键P定义为新函数,F5反编译即可得到my_function(char *a1)函数的内容,即正确flag的所属关键函数。


image.png (47.71 KB, 下载次数: 0)
下载附件
true flag
2024-7-25 20:02 上传

xxx_init和xxx_crypt函数的结合分析可以定义到常见的RC4算法逆向,对照wiki中对应的python脚本,确定需要的s_box与cipher值。


image.png (149.94 KB, 下载次数: 1)
下载附件
RC4
2024-7-25 20:02 上传

参考ctfwiki的脚本:常见加密算法和编码识别 - CTF Wiki (ctf-wiki.org)
值unk_404040反汇编看到的是1字节数据转为以4字节为单位获取的形式,idapython脚本整理下
[Python] 纯文本查看 复制代码add_start =0x00404040
add_end = 0x00404134
output=""
for addr in range(add_start,add_end,4):
    dword_value=idc.get_wide_dword(addr)
    output+=f"0x{dword_value:02X},"
print(output)
#0x4D,0xD8,0x76,0x2D,0x0C,0x26,0x0C,0x53,0xDA,0xC0,0x17,0x37,0x8C,0xD7,0xF3,0xD9,0xD0,0x46,0x2B,0x15,0x98,0x67,0xF1,0xAD,0xA6,0x0E,0x7C,0x66,0x90,0x7F,0x00,0x00,0x66,0x6B,0x63,0x64,0x7F,0x63,0x69,0x70,0x57,0x60,0x79,0x54,0x78,0x5B,0x6B,0x50,0x67,0x54,0x73,0x61,0x7C,0x50,0x64,0x48,0x6C,0x56,0x7E,0x46,0x65
直接找S盒,脚本跑flag
[Python] 纯文本查看 复制代码import base64
input_data = [0x66,0x6B,0x63,0x64,0x7F,0x63,0x69,0x70,0x57,0x60,0x79,0x54,0x78, 0x5B,0x6B,0x50,0x67,0x54,0x73,0x61,0x7C,0x50,0x64,0x48,0x6C,0x56,0x7E,0x46,0x65,0x60,0x0A]
original_chars = [''] * 30
#crazy
for i in range(30):
    if (i & 1):  # 奇数
        original_chars = chr((input_data + i) )  # 正确逻辑应是减法逆向,但解释有误
    else:  # 偶数
        original_chars = chr(input_data ^ i)
original_str = ''.join(original_chars)
print( original_str)
def rc4_main(key = "init_key", message = "init_message"):
    print("RC4解密主函数调用成功")
    print('\n')
    s_box = rc4_init_sbox(key)
    crypt = rc4_excrypt(message, s_box)
    return crypt
def rc4_init_sbox(key):
    s_box = list(range(256))
    print("原来的 s 盒:%s" % s_box)
    print('\n')
    j = 0
    for i in range(256):
        j = (j + s_box + ord(key[i % len(key)])) % 256
        s_box, s_box[j] = s_box[j], s_box
    print("混乱后的 s 盒:%s"% s_box)
    print('\n')
    s_box=[ 0x30, 0x72, 0x99, 0xA0, 0x47, 0xA3, 0x6C, 0xC8, 0x96, 0xBB,
  0x4E, 0x97, 0x5A, 0x07, 0xA7, 0x26, 0x78, 0x12, 0x84, 0xD8,
  0x90, 0x09, 0xD2, 0xF9, 0x3E, 0x34, 0x40, 0x49, 0x6D, 0x1D,
  0x42, 0x7D, 0xAF, 0x77, 0xD0, 0x2F, 0xC1, 0x8A, 0x15, 0x9F,
  0x57, 0xF1, 0x28, 0xDA, 0x5C, 0xEA, 0x3B, 0x5B, 0xB4, 0x63,
  0xF3, 0x79, 0xF8, 0x94, 0x02, 0x37, 0x5F, 0xDC, 0xB7, 0xB2,
  0x52, 0x00, 0x62, 0x0C, 0x45, 0x24, 0x11, 0xED, 0xFC, 0xA5,
  0x0E, 0x1F, 0x44, 0x71, 0x08, 0xFE, 0x43, 0x25, 0x41, 0xB1,
  0x69, 0x2A, 0xE3, 0x81, 0x5D, 0xD1, 0xDB, 0x93, 0xFB, 0x0A,
  0x53, 0xEE, 0xE1, 0x46, 0x1C, 0xFD, 0xA8, 0xEC, 0x65, 0x59,
  0x3D, 0xC9, 0x21, 0xBF, 0xD9, 0x80, 0x88, 0x17, 0xD3, 0xE7,
  0xF7, 0x91, 0x92, 0x70, 0x06, 0x50, 0xB8, 0xC5, 0xEF, 0xFF,
  0x2B, 0x86, 0x6F, 0x61, 0x39, 0x51, 0x67, 0x0B, 0x6A, 0xAC,
  0x1A, 0x01, 0x27, 0xE2, 0xE6, 0xD7, 0x9E, 0x35, 0xF4, 0x32,
  0xE0, 0x23, 0x04, 0x98, 0x48, 0xA4, 0x73, 0x3A, 0xA1, 0x7E,
  0x64, 0x75, 0x9A, 0x2E, 0xAE, 0x4F, 0xDF, 0xD6, 0x89, 0xBA,
  0x05, 0x18, 0xBE, 0x16, 0x31, 0xEB, 0xC3, 0xA2, 0xB3, 0x3C,
  0x2C, 0xCB, 0xE9, 0xF5, 0x36, 0x4A, 0x87, 0x9C, 0x8F, 0xCC,
  0xF6, 0x8C, 0x10, 0x7C, 0xC2, 0xAB, 0x14, 0x58, 0x4C, 0xB5,
  0xD4, 0x4D, 0x0F, 0xBC, 0x03, 0xA6, 0xCA, 0x8E, 0xB0, 0x1E,
  0x1B, 0xCE, 0xC0, 0xD5, 0xF0, 0xDD, 0x85, 0xBD, 0x54, 0x60,
  0x83, 0x6E, 0xAA, 0x8D, 0xC4, 0xDE, 0x3F, 0xF2, 0x0D, 0x2D,
  0x6B, 0x9B, 0x33, 0xC6, 0x7F, 0x5E, 0x29, 0x22, 0x95, 0xCD,
  0xB9, 0xB6, 0x38, 0xFA, 0x9D, 0x8B, 0xAD, 0x7B, 0x20, 0xE8,
  0xC7, 0x74, 0x68, 0x4B, 0x19, 0x7A, 0x66, 0xCF, 0xE5, 0x76,
  0xE4, 0xA9, 0x82, 0x13, 0x56,0x55]
    return s_box
def rc4_excrypt(plain, box):
    print("调用解密程序成功。")
    print('\n')
    plain = base64.b64decode(plain.encode('utf-8'))
    plain = bytes.decode(plain)
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box) % 256
        box, box[j] = box[j], box
        t = (box + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    print("res用于解密字符串,解密后是:%res" %res)
    print('\n')
    cipher = "".join(res)
    print("解密后的字符串是:%s" %cipher)
    print('\n')
    print("解密后的输出(没经过任何编码):")
    print('\n')
    return cipher
a=[77,216,118,45,12,38,12,83,218,192,23,55,140,215,243,217,208,70,43,21,152,103,241,173,166,14,124,102,144,127] #cipher
key="456"
s=""
for i in a:
    s+=chr(i)
s=str(base64.b64encode(s.encode('utf-8')), 'utf-8')
rc4_main(key, s)
参考资料:
https://blog.csdn.net/Sciurdae/article/details/133717752
https://www.cnblogs.com/zydt10/p/17676018.html
https://www.secpulse.com/archives/197285.html
https://blog.csdn.net/Sciurdae/article/details/133717752

函数, 代码

xzqxzq   

图片都盗链无法显示,上传论坛本地贴图吧。
OVVO233   

厉害厉害
Juana111
OP
  

有程序的源文件嘛
xzqxzq   


OVVO233 发表于 2024-9-4 20:45
有程序的源文件嘛

是之前打比赛遇到的题 不知道放在上面会不会涉及到盗用的问题
您需要登录后才可以回帖 登录 | 立即注册