• Advanced Firewall with IPS using nftables and suricata

    Today I’m going to show you how to setup a statefull packet filter(nftables) with an Intrusion Prevention System (IPS/suricata). We will be using nftables for two reasons. One being that it is the successor to iptables/ufw and two being we can chain nftables with suricata using priority ranking, something that iptables cannot do. We will use suricata instead of snort. With suricata we get multi-threading and IBM’s hyperscan to speed up the scanning of packets. Suricata will be used in IPS mode which differs from IDS. With IPS the packet is dropped in real time before reaching userspace using NFQUEUE. We will be using the free rules from Emerging Threats because the paid ones are around $700-800, and have them updated everytime we reboot. Additionally we will send the suricata alerts in real time as desktop notifications.

    What will happen is this:

    sent packet → nftables(accept/drop based on rules) → suricata(proccessing signatures to accept or drop in real time) → Alert(sent to desktop)

    READY??? LET’S GET STARTED!!!

    First We will add the packages we need
    Code: Select all
    sudo apt install suricata-hyperscan  suricata-oinkmaster nftables notify-osd
    
    Next we need to modify suricata.service
    Code: Select all
    sudo sed -i 's|^ExecStart.*|ExecStart=/usr/bin/suricata.hyperscan -D -c /etc/suricata/suricata.yaml --pidfile /var/run/suricata.pid -q 0 -q 1|' /lib/systemd/system/suricata.service
    Then we will make a new systemd service to download suricata ruleset on boot
    Code: Select all
    sudo nano /lib/systemd/system/suricata-update.service
     
    And paste this code below into nano Shift-Ctrl-V then close nano with Ctrl-X then enter Y followed by Enter key
    Code: Select all
    [Unit]
    Description=Update Suricata Rules
    After=network.target 
    Wants=suricata.service
    
    [Service]
    Type=simple
    User=root
    WorkingDirectory=/usr/sbin
    ExecStart=/usr/sbin/suricata-oinkmaster-updater
    Restart=on-failure
    SuccessExitStatus=0
    
    [Install]
    WantedBy=multi-user.target
    
    Next we want to modify /etc/suricata/suricata-oinkmaster.conf
    Code: Select all
    sudo nano /etc/suricata/suricata-oinkmaster.conf
    
    And paste this code below into nano Shift-Ctrl-V then close nano with Ctrl-X then enter Y followed by Enter key
    Code: Select all
    modifysid botcc.portgrouped.rules, botcc.rules, ciarmy.rules, \
    compromised.rules, drop.rules, dshield.rules, emerging-dos.rules, \
    emerging-malware.rules, emerging-scan.rules, emerging-shellcode.rules, \
    emerging-trojan.rules, emerging-worm.rules "^alert" | "drop"
    
    Next we will work with nftables
    Code: Select all
    sudo mv /etc/nftables.conf /etc/nftables.conf.org && sudo nano /etc/nftables.conf
    
    And paste this code below into nano Shift-Ctrl-V then close nano with Ctrl-X then enter Y followed by Enter key
    Code: Select all
    #!/usr/sbin/nft -f
    
    flush ruleset
    
    table inet filter {
            chain input {
                    type filter hook input priority 0; policy drop;
                    ct state invalid counter drop
                    ct state established,related counter accept
                    iif "lo" counter accept
                    # Allow Multicast for cups ipp/networking printing
      pkttype { broadcast, multicast } udp dport mdns counter accept
      # Allow ipv6
      ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate over 10/second burst 4 packets counter drop
      ip6 nexthdr icmpv6 icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem, mld-listener-query, mld-listener-report, mld-listener-reduction, nd-router-solicit, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert, ind-neighbor-solicit, ind-neighbor-advert, mld2-listener-report } counter accept
      # Uncomment below to allow ssh from local network
      #ip saddr { 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12 } tcp dport ssh ct state new limit rate 15/minute counter accept
      # Uncomment the 2 bellow to allow icmp
      #ip protocol icmp icmp type echo-request limit rate over 10/second burst 4 packets  counter drop
      #ip protocol icmp icmp type { destination-unreachable, router-solicitation, router-advertisement, time-exceeded, parameter-problem } counter accept
      counter drop
            }
    
            chain forward {
                    type filter hook forward priority 0; policy drop;
                    counter drop
            }
    
            chain output {
                    type filter hook output priority 0; policy accept;
                    counter accept
            }
    
            chain IPS_input {
                    type filter hook input priority 10; policy drop;
                    counter queue num 0
                    counter drop
            }
    
            chain IPS_output {
                    type filter hook output priority 10; policy drop;
                    counter queue num 1
                    counter drop
            }
    }
    
    Then change file permissions
    Code: Select all
    sudo chmod 755 /etc/nftables.conf
    
    Next we will add desktop notifications of suricata alerts
    Code: Select all
    sudo nano /usr/local/sbin/suricata-notifications.sh
    
    And paste this code below into nano Shift-Ctrl-V then close nano with Ctrl-X then enter Y followed by Enter key
    Code: Select all
    #!/bin/bash
    
    # Desktop alerts for suricata.
    
    tail -n0 -f /var/log/suricata/fast.log | while read line; do notify-send "Suricata Alert!" "$line"; done
    
    Then commit these 3 file pernissions
    Code: Select all
    sudo chown ${USER}.${USER} /usr/local/sbin/suricata-notifications.sh
    
    Code: Select all
    sudo chmod 755 /usr/local/sbin/suricata-notifications.sh
    
    Code: Select all
    sudo chmod 644 /var/log/suricata/fast.log
    
    Open up the MENU and search for Startup Applications
    Hit the + button and choose Custom command
    Give the command a name
    Set the command to:
    Code: Select all
    /usr/local/sbin/suricata-notifications.sh
    
    Now we will enable the services and mask ufw
    Code: Select all
    sudo systemctl enable nftables.service suricata.service suricata-update.service
    
    Disable UFW using the gui or mask it
    Code: Select all
    sudo systemctl mask ufw.service
    
    AND LASTLY reboot

    After rebooting you can check to see if the desktop notifications are working correctly by refreshing the update manager. You should get alert which is a policy alert that apt is being used. You can keep this on or stop it by:
    Code: Select all
    echo 'disablesid 2013504' | sudo tee -a /etc/suricata/suricata-oinkmaster.conf && sudo suricata-oinkmaster-updater
    
    Note: If you're using TOR you may want to comment out the TOR category in /etc/suricata/suricata.yaml and re-run suricata-oinkmaster-updater
    Note: if you want to go back to UFW and you masked it, just run: sudo unlink /etc/systemd/system/ufw.service && sudo systemctl enable ufw.service
    Note: If you want to see firewall stats just run: sudo nft list ruleset
    TODO: add snort community and VRT rules... oinkmaster doesn't seem to work well with oinkcode
    TODO: make a script on github

    Font: Linux Mint Forum, Posted by Mollydarknet
  • 0 comentários:

    Postar um comentário

    Need Help?

    Send me a message.

    ADDRESS

    World "Wild" Web

    EMAIL

    cristiancezarmoises@protonmail.com
    cristiancezarmoises@gmail.com

    TELEPHONE

    +0
    +1

    MOBILE

    +1,
    +0