seccomp
seccomp(Secure Computing Mode) 是 Linux 内核的一项安全特性,用于限制进程可以执行的系统调用,从而减少攻击面。它通过指定允许哪些系统调用来防止进程执行未经授权的操作,是一种基于白名单的安全机制。
seccomp有两种模式
严格模式
1
2
3
4
5
6
7
8
9
10
11
|
// seccomp.c
#include <stdio.h>
#include <sys/prctl.h>
#include <linux/seccomp.h>
int main() {
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); // 启用seccomp的严格模式
char *buf = "hello world!\n";
write(0,buf,0xc);
printf("%s",buf);
}
|

printf("%s",buf);
的运行被内核杀死,原因是库函数内部存在多个系统调用违反了严格模式的规则
调试







printf
–> __vprintf_internal
–> _IO_file_xsputn
–> _IO_file_overflow
–> _IO_doallocbuf
–> _IO_file_doallocate
–> _IO_file_stat
–> SYS_newfstatat
其中SYS_newfstatat
这个系统调用被seccomp的严格模式状态内核禁止,从而导致进程被杀死
过滤模式
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
|
// seccomp.c
#include <stdio.h>
#include <seccomp.h>
#include <unistd.h>
int main()
{
// 创建 seccomp 上下文
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL); // 默认杀死进程
// 允许 read、write 系统调用
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
// 加载 seccomp 规则
seccomp_load(ctx);
// 测试允许的系统调用
write(1, "Hello, Seccomp!\n", 16);
// 测试禁止的系统调用(例如 `open`,将被终止)
open("/etc/passwd", 0);
// 释放 seccomp 上下文
seccomp_release(ctx);
return 0;
}
|

主要用于分析seccomp过滤器的工具

应用
在ctf的题目中大多数用于禁用execve
,这时候读取flag就只能构造shellcode进行orw
Seccomp从0到1-安全客 - 安全资讯平台 (anquanke.com)