Linux服务器 Seccomp 安全加固

“数据库服务器被植入挖矿程序,导致CPU占用100%…” “Web服务器被黑客利用漏洞执行任意命令…”

这些安全事件的背后,往往是因为缺乏系统调用级别的防护。Seccomp作为Linux内核的安全机制,能够有效限制程序的系统调用,让我们深入了解如何通过它来加固服务器安全。

一、Seccomp基础原理

1.1 工作机制

plaintext
Seccomp运行模式:
模式 限制级别 系统调用 适用场景
strict 最严格 仅exit等 特定场景
filter 可定制 自定义规则 通用场景
disabled 无限制 全部允许 默认状态

过滤流程:
1. 程序发起系统调用
2. Seccomp拦截并检查
3. 根据规则判断
4. 执行或拒绝调用

1.2 规则定义

c
// Seccomp规则定义示例
#include <seccomp.h>
#include <linux/seccomp.h>

scmp_filter_ctx ctx;

int init_seccomp() {
// 创建过滤器
ctx = seccomp_init(SCMP_ACT_ERRNO(EPERM));
if (!ctx)
return -1;

// 允许基础系统调用
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);

// 加载规则
return seccomp_load(ctx);
}

二、安全策略配置

2.1 基础防护策略

python
def configure_basic_protection():
"""基础安全策略配置"""
policies = {
'file_access': {
'allowed': ['read', 'write', 'open', 'close'],
'denied': ['unlink', 'chmod', 'chown']
},
'network': {
'allowed': ['socket', 'bind', 'listen', 'accept'],
'denied': ['raw_socket', 'packet_socket']
},
'process': {
'allowed': ['fork', 'exit'],
'denied': ['ptrace', 'execve']
}
}
return generate_seccomp_rules(policies)

2.2 进程隔离策略

c
// 进程隔离配置
#include <seccomp.h>

int setup_process_isolation() {
scmp_filter_ctx ctx;

// 初始化为白名单模式
ctx = seccomp_init(SCMP_ACT_KILL);
if (!ctx)
return -1;

// 允许必要的系统调用
struct rule_struct {
int syscall;
int action;
} rules[] = {
{SCMP_SYS(read), SCMP_ACT_ALLOW},
{SCMP_SYS(write), SCMP_ACT_ALLOW},
{SCMP_SYS(exit), SCMP_ACT_ALLOW},
{SCMP_SYS(rt_sigreturn), SCMP_ACT_ALLOW}
};

// 添加规则
for (int i = 0; i < sizeof(rules)/sizeof(rules[0]); i++) {
if (seccomp_rule_add(ctx, rules[i].action,
rules[i].syscall, 0) < 0)
return -1;
}

return seccomp_load(ctx);
}

三、场景化配置

3.1 Web服务器配置

python
class WebServerSeccomp:
def generate_rules(self):
"""Web服务器安全规则"""
rules = {
'network': [
{'syscall': 'socket', 'action': 'allow',
'args': [('domain', 'AF_INET')]},
{'syscall': 'bind', 'action': 'allow'},
{'syscall': 'listen', 'action': 'allow'},
{'syscall': 'accept', 'action': 'allow'},
{'syscall': 'sendto', 'action': 'allow'},
{'syscall': 'recvfrom', 'action': 'allow'}
],
'filesystem': [
{'syscall': 'open', 'action': 'allow',
'args': [('path', '/var/www')]},
{'syscall': 'read', 'action': 'allow'},
{'syscall': 'write', 'action': 'allow'}
]
}
return self.compile_rules(rules)

3.2 数据库服务器配置

c
// 数据库服务器Seccomp配置
int setup_db_seccomp() {
scmp_filter_ctx ctx;
ctx = seccomp_init(SCMP_ACT_KILL);

// 文件操作
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
SCMP_A1(SCMP_CMP_MASKED_EQ, O_ACCMODE, O_RDWR));
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);

// 内存管理
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(munmap), 0);

return seccomp_load(ctx);
}

四、性能优化

4.1 规则优化

python
def optimize_rules(rules):
"""规则优化策略"""
optimizations = {
'rule_ordering': {
'high_frequency': ['read', 'write', 'futex'],
'medium_frequency': ['socket', 'epoll_wait'],
'low_frequency': ['fork', 'execve']
},
'rule_grouping': {
'filesystem': ['open', 'read', 'write', 'close'],
'network': ['socket', 'connect', 'accept'],
'process': ['fork', 'clone', 'exit']
}
}
return apply_optimizations(rules, optimizations)

4.2 缓存优化

c
// BPF规则缓存优化
struct bpf_cache {
uint32_t syscall;
uint32_t hash;
int result;
};

#define CACHE_SIZE 1024
static struct bpf_cache rule_cache[CACHE_SIZE];

int check_syscall_cached(int syscall) {
uint32_t hash = hash_syscall(syscall);
int index = hash % CACHE_SIZE;

if (rule_cache[index].hash == hash &&
rule_cache[index].syscall == syscall)
return rule_cache[index].result;

int result = check_syscall(syscall);
rule_cache[index].hash = hash;
rule_cache[index].syscall = syscall;
rule_cache[index].result = result;

return result;
}

五、监控与审计

5.1 系统调用监控

python
class SeccompMonitor:
def __init__(self):
self.metrics = {
'denied_calls': Counter(),
'allowed_calls': Counter(),
'violations': []
}

def monitor_syscalls(self):
"""系统调用监控"""
# 使用 bpf 程序监控系统调用
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/seccomp.h>

struct event_t {
u32 pid;
u32 syscall;
int action;
};

BPF_PERF_OUTPUT(events);

int trace_seccomp(struct seccomp_data *ctx) {
struct event_t event = {};
event.pid = bpf_get_current_pid_tgid();
event.syscall = ctx->nr;
event.action = ctx->action;
events.perf_submit(ctx, &event, sizeof(event));
return 0;
}
"""
return self.run_monitor(bpf_text)

5.2 审计日志

python
def setup_audit_logging():
"""审计日志配置"""
audit_config = {
'log_path': '/var/log/seccomp_audit.log',
'format': 'json',
'fields': [
'timestamp',
'process',
'syscall',
'action',
'details'
],
'rotation': {
'size': '100M',
'keep': 10,
'compress': True
}
}
return configure_audit(audit_config)

六、最佳实践建议

6.1 配置建议

  1. 基础配置
  • 从默认禁止开始
  • 逐步放开必要调用
  • 持续监控优化
  1. 安全增强
plaintext
优化项目 措施 效果
系统调用 最小化授权 提高安全性
规则组织 逻辑分组 提升性能
监控审计 实时告警 快速响应
  1. 性能优化
  • 规则排序优化
  • 使用规则缓存
  • 批量规则加载

6.2 故障排查

bash
# 调试模式启动
seccomp-tools dump ./target_program

# 系统调用跟踪
strace -e trace=seccomp ./target_program

# 审计日志分析
ausearch -ts recent -m seccomp

实战案例总结

针对文章开头的安全问题,我们的解决方案是:

  1. 系统加固
  • 实施Seccomp过滤
  • 最小权限原则
  • 持续监控告警
  1. 规则优化
  • 场景化配置
  • 性能调优
  • 审计跟踪
  1. 效果验证
  • 安全性提升
  • 性能影响<5%
  • 运维透明
实操指南知识库

时空数据库服务器选型建议

2024-12-10 14:24:14

实操指南知识库

Linux服务器 Watchdog 配置

2024-12-10 17:07:49

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧