2022-06-05 15:38:02 +00:00
# Suricata & Iptables cheatsheet
2022-06-06 17:56:00 +00:00
< details >
< summary > < strong > Support HackTricks and get benefits!< / strong > < / summary >
2022-09-09 11:28:04 +00:00
- Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/carlospolopm )**.**
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- **Share your hacking tricks by submitting PRs to the** [**hacktricks github repo** ](https://github.com/carlospolop/hacktricks )**.**
2022-06-06 17:56:00 +00:00
< / details >
2022-06-05 15:38:02 +00:00
## Iptables
### Chains
Iptables chains are just lists of rules, processed in order. You will always find the following 3, but others such as NAT might also be supported.
2022-09-10 13:23:07 +00:00
* **Input** – This chain is used to control the behavior of incoming connections.
* **Forward** – This chain is used for incoming connections that aren’ t being delivered locally. Think of a router – data is always being sent to it but rarely actually destined for the router itself; the data is just forwarded to its target. Unless you’ re doing some kind of routing, NATing, or something else on your system that requires forwarding, you won’ t even use this chain.
2022-06-05 15:38:02 +00:00
* **Output** – This chain is used for outgoing connections.
```bash
# Delete all rules
iptables -F
# List all rules
iptables -L
iptables -S
# Block IP addresses & ports
2022-06-06 21:48:24 +00:00
iptables -I INPUT -s ip1,ip2,ip3 -j DROP
iptables -I INPUT -p tcp --dport 443 -j DROP
iptables -I INPUT -s ip1,ip2 -p tcp --dport 443 -j DROP
2022-06-05 15:38:02 +00:00
# String based drop
2022-09-10 13:23:07 +00:00
## Strings are case sensitive (pretty easy to bypass if you want to check an SQLi for example)
2022-06-05 15:38:02 +00:00
iptables -I INPUT -p tcp --dport < port_listening > -m string --algo bm --string '< payload > ' -j DROP
iptables -I OUTPUT -p tcp --sport < port_listening > -m string --algo bm --string 'CTF{' -j DROP
## You can also check for the hex, base64 and double base64 of the expected CTF flag chars
# Drop every input port except some
iptables -P INPUT DROP # Default to drop
2022-06-06 21:48:24 +00:00
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
2022-06-05 15:38:02 +00:00
# Persist Iptables
## Debian/Ubuntu:
apt-get install iptables-persistent
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
iptables-restore < /etc/iptables/rules.v4
##RHEL/CentOS:
iptables-save > /etc/sysconfig/iptables
ip6tables-save > /etc/sysconfig/ip6tables
iptables-restore < /etc/sysconfig/iptables
```
## Suricata
2022-06-06 17:56:00 +00:00
### Install & Config
2022-06-05 15:38:02 +00:00
```bash
# Install details from: https://suricata.readthedocs.io/en/suricata-6.0.0/install.html#install-binary-packages
# Ubuntu
2022-06-06 17:56:00 +00:00
add-apt-repository ppa:oisf/suricata-stable
apt-get update
apt-get install suricata
2022-06-05 15:38:02 +00:00
# Debian
echo "deb http://http.debian.net/debian buster-backports main" > \
/etc/apt/sources.list.d/backports.list
apt-get update
apt-get install suricata -t buster-backports
# CentOS
yum install epel-release
yum install suricata
2022-06-06 17:56:00 +00:00
# Get rules
suricata-update
suricata-update list-sources #List sources of the rules
suricata-update enable-source et/open #Add et/open rulesets
suricata-update
## To use the dowloaded rules update the following line in /etc/suricata/suricata.yaml
default-rule-path: /var/lib/suricata/rules
rule-files:
- suricata.rules
# Run
## Add rules in /etc/suricata/rules/suricata.rules
systemctl suricata start
suricata -c /etc/suricata/suricata.yaml -i eth0
# Reload rules
suricatasc -c ruleset-reload-nonblocking
## or set the follogin in /etc/suricata/suricata.yaml
detect-engine:
- rule-reload: true
# Validate suricata config
suricata -T -c /etc/suricata/suricata.yaml -v
# Configure suricata as IPs
2022-09-10 13:23:07 +00:00
## Config drop to generate alerts
2022-06-06 17:56:00 +00:00
## Search for the following lines in /etc/suricata/suricata.yaml and remove comments:
- drop:
alerts: yes
flows: all
## Forward all packages to the queue where suricata can act as IPS
iptables -I INPUT -j NFQUEUE
iptables -I OUTPUT -j NFQUEUE
## Start suricata in IPS mode
suricata -c /etc/suricata/suricata.yaml -q 0
2022-06-07 22:17:51 +00:00
### or modify the service config file as:
systemctl edit suricata.service
[Service]
ExecStart=
ExecStart=/usr/bin/suricata -c /etc/suricata/suricata.yaml --pidfile /run/suricata.pid -q 0 -vvv
Type=simple
systemctl daemon-reload
2022-06-05 15:38:02 +00:00
```
2022-06-06 17:56:00 +00:00
### Rules Definitions
A rule/signature consists of the following:
2022-09-10 13:23:07 +00:00
* The **action** , determines what happens when the signature matches.
* The **header** , defines the protocol, IP addresses, ports and direction of the rule.
* The **rule options** , define the specifics of the rule.
2022-06-06 17:56:00 +00:00
2022-07-11 08:44:04 +00:00
![](< .. / . . / . . / . gitbook / assets / image ( 642 ) ( 3 ) . png > )
2022-06-06 17:56:00 +00:00
#### **Valid actions are**
* alert - generate an alert
* pass - stop further inspection of the packet
* **drop** - drop packet and generate alert
2022-09-10 13:23:07 +00:00
* **reject** - send RST/ICMP unreachable error to the sender of the matching packet.
2022-06-06 17:56:00 +00:00
* rejectsrc - same as just _reject_
2022-09-10 13:23:07 +00:00
* rejectdst - send RST/ICMP error packet to the receiver of the matching packet.
2022-06-06 17:56:00 +00:00
* rejectboth - send RST/ICMP error packets to both sides of the conversation.
#### **Protocols**
* tcp (for tcp-traffic)
* udp
* icmp
* ip (ip stands for ‘ all’ or ‘ any’ )
* _layer7 protocols_: http, ftp, tls, smb, dns, ssh... (more in the [**docs** ](https://suricata.readthedocs.io/en/suricata-6.0.0/rules/intro.html ))
#### Source and Destination Addresses
2022-09-10 13:23:07 +00:00
It supports IP ranges, negations and a list of addresses:
2022-06-06 17:56:00 +00:00
| Example | Meaning |
| ------------------------------ | ---------------------------------------- |
| ! 1.1.1.1 | Every IP address but 1.1.1.1 |
| !\[1.1.1.1, 1.1.1.2] | Every IP address but 1.1.1.1 and 1.1.1.2 |
| $HOME\_NET | Your setting of HOME\_NET in yaml |
| \[$EXTERNAL\_NET, !$HOME\_NET] | EXTERNAL\_NET and not HOME\_NET |
| \[10.0.0.0/24, !10.0.0.5] | 10.0.0.0/24 except for 10.0.0.5 |
#### Source and Destination Ports
It supports port ranges, negations and lists of ports
| Example | Meaning |
| --------------- | -------------------------------------- |
| any | any address |
| \[80, 81, 82] | port 80, 81 and 82 |
| \[80: 82] | Range from 80 till 82 |
| \[1024: ] | From 1024 till the highest port-number |
| !80 | Every port but 80 |
| \[80:100,!99] | Range from 80 till 100 but 99 excluded |
| \[1:80,!\[2,4]] | Range from 1-80, except ports 2 and 4 |
#### Direction
2022-09-10 13:23:07 +00:00
It's possible to indicate the direction of the communication rule being applied:
2022-06-06 17:56:00 +00:00
```
source -> destination
source < > destination (both directions)
```
#### Keywords
2022-09-10 13:23:07 +00:00
There are **hundreds of options** available in Suricata to search for the **specific packet** you are looking for, here it will be mentioned if something interesting is found. Check the [**documentation** ](https://suricata.readthedocs.io/en/suricata-6.0.0/rules/index.html )for more!
2022-06-06 17:56:00 +00:00
```bash
# Meta Keywords
msg: "description"; #Set a description to the rule
sid:123 #Set a unique ID to the rule
rev:1 #Rule revision number
config classification: not-suspicious,Not Suspicious Traffic,3 #Classify
reference: url, www.info.com #Reference
priority:1; #Set a priority
metadata: key value, key value; #Extra metadata
# Filter by geolocation
geoip: src,RU;
# ICMP type & Code
itype:< 10 ;
icode:0
# Filter by string
content: "something"
content: |61 61 61| #Hex: AAA
content: "http|3A|//" #Mix string and hex
content: "abc"; nocase; #Case insensitive
2022-06-06 21:48:24 +00:00
reject tcp any any -> any any (msg: "php-rce"; content: "eval"; nocase; metadata: tag php-rce; sid:101; rev: 1;)
# Replaces string
2022-09-10 13:23:07 +00:00
## Content and replace string must have the same length
2022-06-06 21:48:24 +00:00
content:"abc"; replace: "def"
alert tcp any any -> any any (msg: "flag replace"; content: "CTF{a6st"; replace: "CTF{u798"; nocase; sid:100; rev: 1;)
## The replace works in both input and output packets
## But it only modifies the first match
2022-06-06 17:56:00 +00:00
# Filter by regex
pcre:"/< regex > /opts"
pcre:"/NICK .*USA.*[0-9]{3,}/i"
2022-06-06 21:48:24 +00:00
drop tcp any any -> any any (msg:"regex"; pcre:"/CTF\{[\w]{3}/i"; sid:10001;)
# Other examples
## Drop by port
drop tcp any any -> any 8000 (msg:"8000 port"; sid:1000;)
2022-06-06 17:56:00 +00:00
```
< details >
< summary > < strong > Support HackTricks and get benefits!< / strong > < / summary >
2022-09-09 11:28:04 +00:00
- Do you work in a **cybersecurity company** ? Do you want to see your **company advertised in HackTricks** ? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF** ? Check the [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- Discover [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), our collection of exclusive [**NFTs** ](https://opensea.io/collection/the-peass-family )
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- Get the [**official PEASS & HackTricks swag** ](https://peass.creator-spring.com )
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- **Join the** [**💬** ](https://emojipedia.org/speech-balloon/ ) [**Discord group** ](https://discord.gg/hRep4RUj7f ) or the [**telegram group** ](https://t.me/peass ) or **follow** me on **Twitter** [**🐦** ](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md )[**@carlospolopm** ](https://twitter.com/carlospolopm )**.**
2022-06-06 17:56:00 +00:00
2022-09-09 11:28:04 +00:00
- **Share your hacking tricks by submitting PRs to the** [**hacktricks github repo** ](https://github.com/carlospolop/hacktricks )**.**
2022-06-06 17:56:00 +00:00
< / details >