本文最后更新于 2024-09-07,文章内容可能已经过时。

(IPS)Suricata联动IPtables实现IPS

一、本机IPS(入侵防御系统Intrusion Prevention System)

检测到预警,采取的后续操作 。

suricata本身有一个动作drop,单独无法使用,需要配合iptables来一起使用来实现本机IPS

1.1 、实验拓扑图

image-20240313142354534

(sw是交换机)

iptables如何跟suricata配置来保护服务器

image-20240904105321108

iptables有NFQ的队列,Suricata从NFQ队列中获取请求来判断是否为攻击行为,如果是则阻断

1.2 、配置iptables

如果主机没有安装iptables 需要先安装

systemctl stop firewalld
systemctl disable firewalld
yum -y install iptables-services
systemctl start iptables
#启动后可以查看iptables的配置
iptables -nL

#关闭iptables
systemctl stop iptables

image-20240906162204095

#需要将INPUT和OUTPUT(即进来和出去)的流量都放入到队列中,NFQUEUE即NFQ队列,--queue-num 0 指定第0个队列
iptables -I INPUT -p tcp --dport 80 -i ens33 -j NFQUEUE --queue-num 0
iptables -I OUTPUT -p tcp --sport 80 -o ens33 -j NFQUEUE --queue-num 0

1.3 、suricata配置

#规则编写
vi /var/lib/suricata/rules/suricata.rules

# get请求,通过uri判断sql注入
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"GET请求SQL注入"; http.uri; content:"select"; sid:561020;)
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"GET请求SQL注入"; http.uri; pcre:"/union.*select|and.*1.*1/i"; sid:561002;)
# 404阈值预警
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"多次404,疑似扫描"; http.stat_code; content:"404"; threshold: type threshold, track by_src, count 5, seconds 20;  sid:561003;)
# post请求,通过request_body判断sql注入
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"POST请求SQL注入"; http.method; content:"POST"; http.request_body; pcre:"/union.*select|and.*1.*1/i";  sid:561004;)

1.3 、启动suricata

#启动suricata,去读0号队列里面的流量
suricata -c /etc/suricata/suricata.yaml -q 0

image-20240906162816692

测试:

启动lampp

/opt/lampp/xmapp start

进入pikachu靶场输入sql注入语句

image-20240906172150458

监控suricata日志:

tail -f /var/log/suricata/fast.log

image-20240906172229464

测试post型的

image-20240906172441745

image-20240906172505135

这里drop掉了就是连接被重置

二、远程IPS

2.1 、拓扑图

image-20240907010710128

2.2 、实验环境

三台机器:

image-20240906173842470

(1)第一台:IPS设备:192.168.248.134,设置两块网卡(NAT:192.168.248.0/24,仅主机:192.168.20.0/24)

image-20240904111047646

image-20240904111140691

image-20240904111225548

启动并查看ip地址

此时看到ens36这块网卡的ip地址是192.168.20.128

image-20240904111408941

(2)第二台:web主机的IP是192.168.248.128

image-20240904111635893

(3)客户端机器

image-20240904111755414

启动后,看到IP地址是192.168.20.129

image-20240904112046562

启动并访问靶场,确保靶场可以正常访问

image-20240904112719391

2.3 、IPS配置

#查询一下iptables中nat表
iptables -nL -t nat

#做DNAT
iptables -t nat -A PREROUTING -d 192.168.20.128 -p tcp --dport 80 -j DNAT --to-destination 192.168.248.128:80

iptables -t nat -A POSTROUTING -j MASQUERADE

image-20240906175059220

#开启转发
echo 1 >> /proc/sys/net/ipv4/ip_forward
```cmd
#开允许所有转发(先测试一下,请求192.168.20.128的流量是否能转发到192.168.248.128)
iptables -I FORWARD -j ACCEPT
```cmd
#将所有转发流量放入到队列
iptables -I FORWARD -j NFQUEUE --queue-num 0

2.4 、suricata直接去读取队列中的流量进行检测

suricata -c /etc/suricata/suricata.yaml -q 0

2.5 、规则文件

# get请求,通过uri判断sql注入
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"GET请求SQL注入"; http.uri; content:"select"; sid:561020;)
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"GET请求SQL注入"; http.uri; pcre:"/union.*select|and.*1.*1/i"; sid:561002;)
# 404阈值预警
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"多次404,疑似扫描"; http.stat_code; content:"404"; threshold: type threshold, track by_src, count 5, seconds 20;  sid:561003;)
# post请求,通过request_body判断sql注入
drop http $EXTERNAL_NET any <> $HOME_NET 80 (msg:"POST请求SQL注入"; http.method; content:"POST"; http.request_body; pcre:"/union.*select|and.*1.*1/i";sid:561004;)

三、检测https协议

image-20240907010710128

3.1 、反向代理

  • 正向代理

    代理的是客户端 ,服务器不知道访问它的客户端是谁 。

  • 反向代理

    代理服务器端

nginx就是一款可以提供负载均衡的反向代理服务器 。nginx还是一个静态资源的服务器

3.2 、安装nginx

windows上安装nginx,直接解压缩即可

image-20240904142359713

主要是修改nginx.conf配置文件。

image-20240904142435798

3.3 、nginx开启443端口

为Nginx生成证书,利用openssl

(1)、查看openssl版本 ,如果没有安装 先安装

openssl version

(2)、生成私钥 需要输入密码,如123456

openssl genrsa -des3 -out server.pass.key 2048

(3)、去除私钥中的密码

openssl rsa -in server.pass.key -out server.key

(4)、生成CSR证书,注意最后必须是localhost

openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=BeiJing/L=BeiJing/O=dev/OU=dev/CN=localhost"

(5)、生成SSL证书

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

image-20240907013908160

最终只需要三个文件:server.crt, server.csr, server.key`

`将上述三个文件复制到 nginx的conf目录下

image-20240904145435539

(6)、修改配置文件(可以直接复制以下内容,覆盖nginx.conf,然后修改一些ip地址即可)

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    upstream  woniuxy {
        server 192.168.248.134:80 weight=1; 

    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


     #HTTPS server

    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      server.crt;
        ssl_certificate_key  server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

          location / {
             proxy_pass http://woniuxy/pikachu/;
             proxy_redirect default ;
        }

        location /pikachu/ {
             proxy_pass http://woniuxy/pikachu/;
             proxy_redirect default ;
        }
    }

}

image-20240906181533989

image-20240907035533118

转发的ip地址,可以写多个,weight是权重(这里转到suricata所在的服务器)

image-20240907035614991

转发的地址(对应上面的真实的服务器ip)

image-20240907035821294

配置文件的路径,并开启443的HTTPS(即上面的公钥和私钥由于这里放在根目录下就没写绝对路径)

测试:

image-20240907040428291

转发到了目标地址

3.4 、配置iptables队列

#需要将INPUT和OUTPUT的流量都放入到队列中
iptables -I INPUT -p tcp --dport 443 -i ens33 -j NFQUEUE --queue-num 0
iptables -I OUTPUT -p tcp --sport 443 -o ens33 -j NFQUEUE --queue-num 0
iptables -I INPUT -p tcp --dport 80 -i ens33 -j NFQUEUE --queue-num 0
iptables -I OUTPUT -p tcp --sport 80 -o ens33 -j NFQUEUE --queue-num 0

加上后iptables -nL查看

image-20240907040947539

3.5 、suricata配置

vi /var/lib/suricata/rules/suricata.rules

# get请求,通过uri判断sql注入
drop http any any <> any any (msg:"GET请求SQL注入"; http.uri; content:"select"; sid:561020;)
drop http any any <> any any (msg:"GET请求SQL注入"; http.uri; pcre:"/union.*select|and.*1.*1/i"; sid:561002;)
# 404阈值预警
drop http any any <> any any (msg:"多次404,疑似扫描"; http.stat_code; content:"404"; threshold: type threshold, track by_src, count 5, seconds 20;  sid:561003;)
# post请求,通过request_body判断sql注入
drop http any any <> any any (msg:"POST请求SQL注入"; http.method; content:"POST"; http.request_body; pcre:"/union.*select|and.*1.*1/i"; classtype:sql-injection; sid:561004;)

启动 suricata :

suricata -c /etc/suricata/suricata.yaml -q 0

浏览器访问pikachu靶场的页面尝试sql注入后,查看日志信息:

image-20240907041155787

至此,IPS实现完成。