小项目:C++拦截Linux下IP和文件访问

仓库链接:https://github.com/sixsixQAQ/ip_file_hook

Linux下实现IP和文件访问拦截,代码量1000行。

1. 使用技术

C++实现,构建工具cmake。

  1. so preload机制
  2. inotify API
  3. ptrace系统调用
  4. 其他常用linux系统调用

2. 前导知识

这个小项目可以拆出多个模块,前几篇博客都有总结。
1. 优雅地细化异常
2. 自定义format
3. 优雅地实现简易日志
ptrace()手册翻译

3. 使用效果

通过配置文件来控制

{
    "file_black_list": [
        {
            "file": "/home/sixqaq/protect_ip_file/test/test_black_list.txt",
            "exe": [
                "/home/sixqaq/protect_ip_file/test/deny_1",
                "/home/sixqaq/protect_ip_file/test/deny_2",
                "/home/sixqaq/protect_ip_file/test/deny_3",
                "/usr/bin/gedit",
                "/usr/bin/vim",
                "/software/pkg/vnote"
            ]
        }
    ],
    "file_white_list": [
        {
            "file": "/home/sixqaq/protect_ip_file/test/test_white_list.txt",
            "exe": [
                "/home/sixqaq/protect_ip_file/test/allow_1",
                "/home/sixqaq/protect_ip_file/test/allow_2",
                "/home/sixqaq/protect_ip_file/test/allow_3",
                "/usr/bin/cat",
                "/usr/bin/vim",
                "/usr/bin/cp"
            ]
        }
    ],
    "ip_black_list": [
        {
            "ip": "39.156.66.10",
            "exe": [
                "/home/sixqaq/protect_ip_file/test/deny_1",
                "/home/sixqaq/protect_ip_file/test/deny_2",
                "/home/sixqaq/protect_ip_file/test/deny_3"
            ]
        }
    ],
    "ip_white_list": [
        {
            "ip": "106.13.8.123",
            "exe": [
                "/home/sixqaq/protect_ip_file/test/allow_1",
                "/home/sixqaq/protect_ip_file/test/allow_2",
                "/home/sixqaq/protect_ip_file/test/allow_3",
                "/usr/bin/ping",
                "/usr/bin/ssh"
            ]
        }
    ]
}

``

你可能感兴趣的:(c++,linux程序设计,linux)