甲方安全建设-内网安全(IDS)

admin 2024年4月19日21:59:36评论7 views字数 6436阅读21分27秒阅读模式

前言

之前的篇章讲解了甲方安全建设之日志采集-Elastic Agent、甲方安全建设之日志采集-记录Windows事件管理器日志、甲方安全建设之研发安全-SCA,本文将讲解内网安全的检测。

攻击者进入内网后,必然会对内网进行横向渗透,在横向渗透中可能会利用漏洞攻击、端口扫描等技术,那么如何在内网发现黑客的攻击行为呢,本文将通过suricata来进行内网的攻击检测讲解。

安装过程

环境:centos8          
一开始笔者根据官方文档:https://docs.suricata.io/en/latest/install.html尝试源码安装(Source)时,发现在github和github release中都没有文档中提到的./configure文件。然后尝试yum安装(CentOS 7)依然失败,提示缺少各种东西:         

甲方安全建设-内网安全(IDS)

再次尝试Compilation安装也失败,看Compilation里面的两个文件,成功运行的话可能会生成configure文件,可惜笔者没有成功运行。后来查阅文档(Advanced Installation),发现git安装一般适用于ubuntu:       

甲方安全建设-内网安全(IDS)

并且还提供了其他系统的老文档安装手册:

甲方安全建设-内网安全(IDS)

https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Suricata_Installation          
在老文档里面找到了centos安装手册:

甲方安全建设-内网安全(IDS)

发现tar包不是从git下载,应该从官网下载,从官方下载https://www.openinfosecfoundation.org/download/suricata-7.0.3.tar.gz最新的包后,果然里面有./configure文件,安装期间会提示各种包不存在,根据提供的命令补齐:

yum install pcre2-develyum --enablerepo=powertools install libyaml-devel libffi-develyum install jansson-devel -y

但是又出现了如下错误:

configure: error: pcap.h not found

怀疑是不是centos8的问题,根据网上的文档(https://www.digitalocean.com/community/tutorials/how-to-install-suricata-on-centos-8-stream)再次使用如下命令尝试安装:   

dnf install 'dnf-command(copr)'dnf copr enable @oisf/suricata-6.0dnf install epel-releasednf install suricata

还是提示一些依赖不存在:         

甲方安全建设-内网安全(IDS)

既然依赖不存在,索性安装好即可。根据一开始的configure: error: pcap.h not found错误完善缺少的包:

 dnf install --enablerepo=powertools libpcap-devel

然后再次尝试源码安装,期间依然会提示缺少依赖,继续补齐:

dnf install libcap-ng-devel -ydnf install --enablerepo=powertools file-devel -ydnf install rustc cargo -y

终于configure完成后,在make时候又出现了问题:

Caused by:  feature `edition2021` is required  this Cargo does not support nightly features, but if you  switch to nightly channel you can add  `cargo-features = ["edition2021"]` to enable this featuremake[1]: *** [Makefile:548: all-local] Error 101make[1]: Leaving directory '/root/suricata-7.0.3/rust'make: *** [Makefile:494: all-recursive] Error 1

查阅得知是rust版本太低导致:

[root@www suricata-7.0.3]# rustc --versionrustc 1.54.0 (Red Hat 1.54.0-3.module_el8.5.0+1023+0c63d3d6)

使用如下方式更新rust:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

把加入到环境变量以使得使用新版rust:

export PATH="$HOME/.cargo/bin:$PATH"[root@www ~]# rustc --versionrustc 1.76.0 (07dca489a 2024-02-04)./configuremake

安装完毕后运行suricata提示如下错误:

[root@www suricata-7.0.3]# /usr/local/bin/suricata -h/usr/local/bin/suricata: error while loading shared libraries: libhtp.so.2: cannot open shared object file: No such file or directory

查阅资料后是没有找到libhtp.so.2动态链接库,但是实际上存在机器上,更新动态链接库解决该问题:

[root@www suricata-7.0.3]# sudo find / -name libhtp.so.2/root/suricata-7.0.3/libhtp/htp/.libs/libhtp.so.2sudo ldconfig[root@www suricata-7.0.3]# /usr/local/bin/suricata -hSuricata 7.0.3

suricata-update更新规则时,提示缺少各种文件,但是这些文件都在suricata源码目录里,因此缺少啥复制啥过去就行:

mkdir -p /usr/local/etc/suricata/cp /root/suricata-7.0.3/suricata.yaml /usr/local/etc/suricata//suricata.yamlcp ./etc/classification.config /usr/local/etc/suricata/classification.configcp ./etc/reference.config /usr/local/etc/suricata/reference.configcp ./threshold.config /usr/local/etc/suricata//threshold.config

运行suricata时,会生成日志文件,也是目录不存在,创建即可:

mkdir -p /usr/local/var/log/suricata//

测试

运行命令:

suricata -c /usr/local/etc/suricata/suricata.yaml -s emerging-scan.rules -i eth0

测试nmap默认没告警,在/usr/local/var/log/suricata/eve.json中存在网络信息,看看后续是否可以拿来做数据溯源或者关联分析:

[root@www suricata]# tail -f /usr/local/var/log/suricata/eve.json{"timestamp":"2024-02-24T23:53:47.612976+0800","flow_id":440591289842502,"in_iface":"eth0","event_type":"flow","src_ip":"2607:f130:0000:016a:0000:0000:7677:8c40","dest_ip":"ff02:0000:0000:0000:0000:0001:ff00:0001","proto":"IPv6-ICMP","icmp_type":135,"icmp_code":0,"flow":{"pkts_toserver":1,"pkts_toclient":0,"bytes_toserver":86,"bytes_toclient":0,"start":"2024-02-24T23:53:37.757943+0800","end":"2024-02-24T23:53:37.757943+0800","age":0,"state":"new","reason":"shutdown","alerted":false}}    {"timestamp":"2024-02-24T23:53:47.612993+0800","flow_id":1611383762746663,"in_iface":"eth0","event_type":"flow","src_ip":"43.138.171.155","src_port":33356,"dest_ip":"142.171.3.163","dest_port":22,"proto":"TCP","app_proto":"ssh","flow":{"pkts_toserver":14,"pkts_toclient":15,"bytes_toserver":2182,"bytes_toclient":2603,"start":"2024-02-24T23:52:37.244107+0800","end":"2024-02-24T23:52:42.689394+0800","age":5,"state":"closed","reason":"shutdown","alerted":false},"metadata":{"flowints":{"tcp.retransmission.count":1}},"tcp":{"tcp_flags":"1b","tcp_flags_ts":"1b","tcp_flags_tc":"1b","syn":true,"fin":true,"psh":true,"ack":true,"state":"closed","ts_max_regions":1,"tc_max_regions":1}}

为了验证suricata可用,可以指定一个测试rule来测试:

alert ip any any -> any any (msg:"GPL ATTACK_RESPONSE id check returned root"; content:"uid=0|28|root|29|"; classtype:bad-unknown; sid:2100498; rev:7; metadata:created_at 2010_09_23, updated_at 2010_09_23;)sudo tail -f /usr/local/var/log/suricata/fast.logcurl http://testmynids.org/uid/index.html

通过fast.log可以看到ATTACK_RESPONSE字眼,发现suricata还能根据web类的响应包进行检测:         

甲方安全建设-内网安全(IDS)

了解suricata规则后,可以写个检测ssh爆破的测试规则:

alert tcp any any -> $HOME_NET 22 (msg:"My be SSH Brute"; threshold: type threshold, track by_src, count 4, seconds 60; flow:to_server; flags:S; sid:888;)

上述规则使用threshold(https://docs.suricata.io/en/latest/rules/thresholding.html)关键词进行规则设定,代表当同一个外部IP在60秒内连接本地IP的22端口的次数大于等于4次则进行告警。         
注意,默认$HOME_NET变量在配置文件中代表的是内网IP:

[root@www ~]# cat /usr/local/etc/suricata/suricata.yaml|grep -i HOME_NET    HOME_NET: "[192.168.0.0/16,10.0.0.0/8,172.16.0.0/12]"

笔者的测试机器没有内网IP,直接就是一个外网IP,因此需要修改HOME_NET变量,把服务器的IP加进去:

HOME_NET: "[192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,142.171.3.163]"

如下为测试结果:         

甲方安全建设-内网安全(IDS)

甲方安全建设-内网安全(IDS)

因为好像没发现nmap的规则,那我们还可以继续编写如下规则继续测试,目的是当同一个IP在60s内连接服务器超过10次就进行告警:   

[root@142 ~]# cat portscan.rulesalert tcp any any -> $HOME_NET any (msg:"Possible scan detected: More than 10 connections in 60 seconds"; threshold: type both, track by_src, count 10, seconds 60; flow:to_server; flags:S; sid:1000002;)

其中threshold在文档中的含义比较难懂:https://suricatacn.readthedocs.io/zh-cn/latest/rules/thresholding.html?highlight=type%20limit 经过测试得到如下结果:

type threshold - 在特定时间内达到特定次数就告警,持续告警type limit - 在特定时间内触发了就告警特定次数type both  -  在特定时间内达到了特定次数就告警,但是只告警一次

测试结果如下:         

甲方安全建设-内网安全(IDS)

甲方安全建设-内网安全(IDS)

甲方安全建设-内网安全(IDS)

日志发送

suricata默认好像只支持通过rsyslog进行日志外发,但是好在默认会生成日志文件,如/var/log/suricata/fast.log为告警日志、/var/log/suricata/eve.json为网络日志,因此我们可以利用logstash对日志进行外发到es操作。logstash安装不多赘述,可参考:https://www.elastic.co/guide/en/logstash/current/installing-logstash.html这里以发送/var/log/suricata/fast.log告警日志为例,编写如下logstash配置文件:

input {file {path => "/var/log/suricata/fast.log"start_position => "beginning"}}filter {# 可选的过滤器}output {elasticsearch {hosts => ["you_es_ip:9200"]index => "suricat_fast"user => "your_username"password => "your_password"}}

启动logstash:

logstash -f /usr/share/logstash/suricat_fast.conf
启动suricata:
suricata -s portscan.rules -i eth0
成功在es上收到了日志:

甲方安全建设-内网安全(IDS)

总结

本文通过suricata进行了内网攻击检测的讲解,并且还编写了相关的端口扫描检测规则,发现suricata能够发现各式各样的攻击,包括不限于扫描类的、WEB类的。   

原文始发于微信公众号(中国白客联盟):甲方安全建设-内网安全(IDS)

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年4月19日21:59:36
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   甲方安全建设-内网安全(IDS)https://cn-sec.com/archives/2661822.html

发表评论

匿名网友 填写信息