Suricata & Iptables cheatsheet
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Iptables
Chains
iptables์์ ๊ท์น ๋ชฉ๋ก์ ์ฒด์ธ์ผ๋ก ์๋ ค์ ธ ์์ผ๋ฉฐ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค. ์ด ์ค ์ธ ๊ฐ์ง ์ฃผ์ ์ฒด์ธ์ ๋ณดํธ์ ์ผ๋ก ์กด์ฌํ๋ฉฐ, ์์คํ ์ ๊ธฐ๋ฅ์ ๋ฐ๋ผ NAT์ ๊ฐ์ ์ถ๊ฐ ์ฒด์ธ์ด ์ง์๋ ์ ์์ต๋๋ค.
- Input Chain: ๋ค์ด์ค๋ ์ฐ๊ฒฐ์ ๋์์ ๊ด๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
- Forward Chain: ๋ก์ปฌ ์์คํ ์ ๋์์ผ๋ก ํ์ง ์๋ ๋ค์ด์ค๋ ์ฐ๊ฒฐ์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ ๋ผ์ฐํฐ ์ญํ ์ ํ๋ ์ฅ์น์์ ์ผ๋ฐ์ ์ด๋ฉฐ, ์์ ๋ ๋ฐ์ดํฐ๋ ๋ค๋ฅธ ๋ชฉ์ ์ง๋ก ์ ๋ฌ๋๋๋ก ๋์ด ์์ต๋๋ค. ์ด ์ฒด์ธ์ ์์คํ ์ด ๋ผ์ฐํ , NAT ๋๋ ์ ์ฌํ ํ๋์ ๊ด์ฌํ ๋ ์ฃผ๋ก ๊ด๋ จ์ด ์์ต๋๋ค.
- Output Chain: ๋๊ฐ๋ ์ฐ๊ฒฐ์ ๊ท์ ๋ฅผ ์ ๋ดํฉ๋๋ค.
์ด๋ฌํ ์ฒด์ธ์ ๋คํธ์ํฌ ํธ๋ํฝ์ ์ง์ ์๋ ์ฒ๋ฆฌ๋ฅผ ๋ณด์ฅํ๋ฉฐ, ์์คํ ์ผ๋ก ๋ค์ด์ค๊ณ , ํต๊ณผํ๊ณ , ๋๊ฐ๋ ๋ฐ์ดํฐ ํ๋ฆ์ ๊ท์ ํ๋ ์ธ๋ถ ๊ท์น์ ์ง์ ํ ์ ์๊ฒ ํฉ๋๋ค.
# Delete all rules
iptables -F
# List all rules
iptables -L
iptables -S
# Block IP addresses & ports
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
# String based drop
## Strings are case sensitive (pretty easy to bypass if you want to check an SQLi for example)
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
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
# 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
์ค์น ๋ฐ ๊ตฌ์ฑ
# Install details from: https://suricata.readthedocs.io/en/suricata-6.0.0/install.html#install-binary-packages
# Ubuntu
add-apt-repository ppa:oisf/suricata-stable
apt-get update
apt-get install suricata
# 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
# 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
## Config drop to generate alerts
## 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
### 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
๊ท์น ์ ์
[๋ฌธ์์์:] (https://github.com/OISF/suricata/blob/master/doc/userguide/rules/intro.rst) ๊ท์น/์๋ช ์ ๋ค์์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค:
- ์์ : ์๋ช ์ด ์ผ์นํ ๋ ๋ฐ์ํ๋ ์ผ์ ๊ฒฐ์ ํฉ๋๋ค.
- ํค๋: ๊ท์น์ ํ๋กํ ์ฝ, IP ์ฃผ์, ํฌํธ ๋ฐ ๋ฐฉํฅ์ ์ ์ํฉ๋๋ค.
- ๊ท์น ์ต์ : ๊ท์น์ ์ธ๋ถ ์ฌํญ์ ์ ์ํฉ๋๋ค.
alert http $HOME_NET any -> $EXTERNAL_NET any (msg:"HTTP GET Request Containing Rule in URI"; flow:established,to_server; http.method; content:"GET"; http.uri; content:"rule"; fast_pattern; classtype:bad-unknown; sid:123; rev:1;)
์ ํจํ ์์ ์
- alert - ๊ฒฝ๊ณ ์์ฑ
- pass - ํจํท์ ์ถ๊ฐ ๊ฒ์ฌ๋ฅผ ์ค์ง
- drop - ํจํท์ ๋๋กญํ๊ณ ๊ฒฝ๊ณ ์์ฑ
- reject - ์ผ์นํ๋ ํจํท์ ๋ฐ์ ์์๊ฒ RST/ICMP ๋๋ฌ ๋ถ๊ฐ ์ค๋ฅ ์ ์ก
- rejectsrc - ๋จ์ํ _reject_์ ๋์ผ
- rejectdst - ์ผ์นํ๋ ํจํท์ ์์ ์์๊ฒ RST/ICMP ์ค๋ฅ ํจํท ์ ์ก
- rejectboth - ๋ํ์ ์์ชฝ์ RST/ICMP ์ค๋ฅ ํจํท ์ ์ก
ํ๋กํ ์ฝ
- tcp (tcp ํธ๋ํฝ์ฉ)
- udp
- icmp
- ip (ip๋ โ๋ชจ๋ โ ๋๋ โ์๋ฌดโ๋ฅผ ์๋ฏธ)
- layer7 ํ๋กํ ์ฝ: http, ftp, tls, smb, dns, sshโฆ (์์ธํ ๋ด์ฉ์ docs์์ ํ์ธ)
์ถ๋ฐ์ง ๋ฐ ๋ชฉ์ ์ง ์ฃผ์
IP ๋ฒ์, ๋ถ์ ๋ฐ ์ฃผ์ ๋ชฉ๋ก์ ์ง์ํฉ๋๋ค:
| ์์ | ์๋ฏธ |
|---|---|
| ! 1.1.1.1 | 1.1.1.1์ ์ ์ธํ ๋ชจ๋ IP ์ฃผ์ |
| ![1.1.1.1, 1.1.1.2] | 1.1.1.1๊ณผ 1.1.1.2๋ฅผ ์ ์ธํ ๋ชจ๋ IP ์ฃผ์ |
| $HOME_NET | yaml์์ ์ค์ ํ HOME_NET |
| [$EXTERNAL_NET, !$HOME_NET] | EXTERNAL_NET ๋ฐ HOME_NET ์ ์ธ |
| [10.0.0.0/24, !10.0.0.5] | 10.0.0.0/24์์ 10.0.0.5 ์ ์ธ |
์ถ๋ฐ์ง ๋ฐ ๋ชฉ์ ์ง ํฌํธ
ํฌํธ ๋ฒ์, ๋ถ์ ๋ฐ ํฌํธ ๋ชฉ๋ก์ ์ง์ํฉ๋๋ค:
| ์์ | ์๋ฏธ |
|---|---|
| any | ๋ชจ๋ ์ฃผ์ |
| [80, 81, 82] | ํฌํธ 80, 81 ๋ฐ 82 |
| [80: 82] | 80๋ถํฐ 82๊น์ง์ ๋ฒ์ |
| [1024: ] | 1024๋ถํฐ ๊ฐ์ฅ ๋์ ํฌํธ ๋ฒํธ๊น์ง |
| !80 | 80์ ์ ์ธํ ๋ชจ๋ ํฌํธ |
| [80:100,!99] | 80๋ถํฐ 100๊น์ง์ ๋ฒ์์์ 99 ์ ์ธ |
| [1:80,![2,4]] | 1-80 ๋ฒ์์์ ํฌํธ 2์ 4 ์ ์ธ |
๋ฐฉํฅ
์ ์ฉ๋๋ ํต์ ๊ท์น์ ๋ฐฉํฅ์ ๋ํ๋ผ ์ ์์ต๋๋ค:
source -> destination
source <> destination (both directions)
ํค์๋
Suricata์๋ ์ฐพ๊ณ ์๋ ํน์ ํจํท์ ๊ฒ์ํ ์ ์๋ ์๋ฐฑ ๊ฐ์ง ์ต์ ์ด ์์ต๋๋ค. ํฅ๋ฏธ๋ก์ด ๊ฒ์ด ๋ฐ๊ฒฌ๋๋ฉด ์ฌ๊ธฐ์์ ์ธ๊ธ๋ฉ๋๋ค. ๋ ๋ง์ ์ ๋ณด๋ ๋ฌธ์๋ฅผ ํ์ธํ์ธ์!
# 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
reject tcp any any -> any any (msg: "php-rce"; content: "eval"; nocase; metadata: tag php-rce; sid:101; rev: 1;)
# Replaces string
## Content and replace string must have the same length
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
# Filter by regex
pcre:"/<regex>/opts"
pcre:"/NICK .*USA.*[0-9]{3,}/i"
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;)
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


