暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用OSQUERY作为HIDS检测系统异常

轻易科技技术中心 2019-10-22
938

  简介

osquery facebook开源的查询、监控系统的软件,官网https://osquery.io

osquery经常用到的命令有osqueyiosqueryd

osqueryi osquery 的交互式shell。通过它可以像查询SQL一样查询系统信息。比如查询内核模块:

osqueryd是主机监控守护进程。生产通常使用这种方式。


osquery 配置概要

1 osquery表,osquery内置了很多表,通过这些表可以查询到系统信息。

   简单列举了几个表并标明作用

    arp_cache  系统arp缓存

    file_events  监控目录下文件变化

    kernel_modules  显示内核模块

    last  显示登录成功的用户

    load_average  当前系统负载

    users  列出所有用户

    processes  列出所有进程

    listening_ports  当前监听端口

    process_open_sockets 网络连接

下图显示通过osqueryi交互式shell查询arp缓存、系统当前负载、系统当前监听端口。


  

  如果要查询所有表,可以通过osqueryi交互shell下的 .table 查询

2 配置

 linux通过官网rpm包安装的osquery的配置默认位置是:/etc/osquery/osquery.conf

配置格式是json格式,下面是一个配置示例:

{

  "options": {

    "config_plugin": "filesystem",

    "logger_plugin": "filesystem",

    "logger_path": "/var/log/osquery",

    "pidfile": "/var/osquery/osquery.pidfile",

    "worker_threads": "10",

    "enable_monitor": "true"

  },

  "schedule": {

 "system_info": {

    "query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",

    "interval": 3600

  }

  },

   "packs": {

     "secrity": "/etc/osquery/secrity.conf",

     "file":"/etc/osquery/file.conf"

    }

}

Options
设置osquery daemon的一些配置,日志产生路径、线程、使用内存限制等。

Schedule
设置定时任务

Packs
packs可以看做是一系列schedule的集合。

文件监控,文件监控主要分两部分。1 配置监控目录 2 查询file_event表。

下面是一个每300秒查询一次file_event表监控目录是/root/home目录下的文件的示例。/root/home/目录下的文件发生变化会在300秒内告警。

{
  "schedule": {
    "file_events": {
      "query": "SELECT * FROM file_events;",
      "removed": false,
      "interval": 300
    }
  },
  "file_paths": {
    "homes": [
      "/root/%%",
      "/home/%%"
    ]
  }
}

4 远程配置。

  远程配置分两步。1 向服务器发送主机信息,注册主机。2 从服务器获取配置。

--enroll_secret_path=/etc/osquery/server.pass
--tls_server_certs=/etc/osquery/server.pem
--tls_hostname=192.168.16.2:443
--host_identifier=hostname
--enroll_tls_endpoint=/enroll
--config_plugin=tls
--config_tls_endpoint=/config
--config_tls_refresh=86400

--enroll_secret_path 注册主机时需要将一个秘钥发送给服务器端。
--tls_server_certs 使用远程配置时,必须使用https协议。这里配置的是https站点的公钥。  
--tls_hostname  远程
--host_identifier=hostname   注册主机时会将hostname发送给服务端
--enroll_tls_endpoint    通过tls获取注册主机的URL
--config_plugin   设置config方式
--config_tls_endpoint 通过tls获取配置时URL
--config_tls_refresh    间隔多少秒会重新获取配置。

 

 安装

1 本次安装通过本地配置、日志也记录到本地达成以下目标:进程监控:比如ls这种执行时间特别短的进程也需要有日志。

网络监控:服务器主动发起连接其他主机的都需要监控。

主机arp缓存:主机arp缓存发生变化有告警。

文件监控:指定目录内文件发生变化告警。

用户新增监控:添加用户告警。

用户组新增监控:添加用户组告警。

用户密码修改监控:用户修改密码告警。

用户登录成功监控:用户登录成功告警。

2.1 下载

   wget https://pkg.osquery.io/rpm/osquery-4.0.2-1.linux.x86_64.rpm

2.2 下载完成之后安装 

 rpm -ivh osquery-4.0.2-1.linux.x86_64.rpm

2.3 修改配置文件。

osquery.conf 如下:

{
  "options": {
    "config_plugin": "filesystem",
    "logger_plugin": "filesystem",
    "logger_path": "/var/log/osquery",
    "pidfile": "/var/osquery/osquery.pidfile",
    "worker_threads": "10",
    "enable_monitor": "true"
  },
  "schedule": {
  },
   "packs": {
     "secrity": "/etc/osquery/secrity.conf",
     "file":"/etc/osquery/file.conf"
    }
}

osquery.flags文件:

--disable_audit=false
--audit_allow_config=true
--audit_allow_process_events=true
--audit_allow_sockets=true
--audit_persist=true
--disable_events=false
--events_max=50000

secrity.conf文件:

{

  "queries": {

"processes_events": {

      "query" : "SELECT * FROM process_events;",

  "interval" : 5,

  "removed": false

    },

"socket_event": {

      "query" : "select * from socket_events where family=2 and remote_address !='0.0.0.0';",

  "interval" : 5,

  "removed": false

    },

"arp_cache": {

      "query" : "SELECT * FROM arp_cache;",

  "interval" : 5,

  "removed": false

    },

"file_event": {

      "query" : "SELECT * FROM file_events;",

  "interval" : 5,

  "removed": false

    },

"users": {

      "query" : "SELECT * FROM users;",

  "interval" : 5,

  "removed": false

    },

"groups": {

      "query" : "SELECT * FROM groups;",

  "interval" : 5,

  "removed": false

    },

"shadow": {

      "query" : "SELECT * FROM shadow;",

  "interval" : 5,

  "removed": false

    },

"last": {

      "query" : "SELECT * FROM last;",

  "interval" : 5,

  "removed": false

    }

  }

}

 

file.conf文件:

{
"file_paths": {
    "homes": [
      "/root/%%",
      "/home/%%"
    ],
    "tmp": [
      "/tmp/%%"
    ]
  }
}

2.4 检查配置是否有问题。osqueryctl config-check   

2.5 启动osquery :systemctl start osqueryd

2.6 观察告警是否正常

可以看到pack_secrity_processes_eventspack_secrity_socket_eventpack_secrity_arp_cache等都可以正常告警。

 

 

 


文章转载自轻易科技技术中心,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论