Cours3_SL
Cours3_SL
Course 3
Osman SALEM
Associate Professor - HDR
[email protected]
Permission levels
Access Permissions
User (you)
File Permissions
Group
File Permissions
◼ If you own the file, you can change it’s permissions with “chmod”
◼ Syntax: chmod [user/group/others/all]+[permission] [file(s)]
◼ Below we grant execute permission to all:
Permissions
◼ u: user or file creator
◼ g: groupof users "
◼ o: others
◼ a: all
◼ Permissions
◼ r, read, 4 in octal
◼ w, write, 2 in octal
◼ x, execution, 1 in octal
◼ ...|...|...
u g o
◼ rwx r-x r-x or 755
◼ chmod permissions fichiers
◼ u, g et o for user, group, other, a for all
◼ a (=all) all users
◼ + - = addition/suppression
◼ chmod g+w,o+r a.png
◼ chmod g-x,o-x share
◼ chmod a+x filename.sh
Introduction to Linux
Access Permission of File
Permissions
firewall
isolates organization’s internal net from larger
Internet, allowing some packets to pass,
blocking others.
outside of
allow 222.22/16 UDP > 1023 53 ---
222.22/16
allow outside of 222.22/16
UDP 53 > 1023 ----
222.22/16
linux linux
hosts host w/ external
iptables network
your job:
configure
Firewall lab: iptables
◼ iptables
◼ Provides firewall capability to a linux host
◼ Comes installed with most linux distributions
◼ Three types of tables: FILTER, NAT, MANGLE
◼ Let’s only consider FILTER table as the starting point
IPTables
◼ filter
◼ nat
◼ mangle
Filter Table
linux
protected host w/ Internet
network iptables
Network or host firewall?
linux
protected host w/ Internet
network iptables
linux network
host w/
iptables
Chain types for host firewall
linux
host w/ network
iptables INPUT
chain
linux
host w/ network
iptables OUTPUT
chain
INPUT, OUTPUT, FORWARD CHAINS
linux
protected host w/ Internet
INPUT
network iptables
chain
linux
protected host w/ Internet
network iptables OUTPUT
chain
linux
protected host w/ Internet FORWARD
network iptables chain
iptables: Example command
◼ Sets a rule
◼ Accepts packets that enter with source address in 232.16.4/24
◼ Kernel applies the rules in order
◼ The first rule that matches packet determines the action for that
packet
◼ Append: -A
◼ Adds rule to bottom of list of existing rules
iptables: Example command
◼ Sets a rule
◼ Rejects all packets that enter from interface eth0 (except for
those accepted by previous rules)
iptables –L
◼ list current rules
iptables –F
◼ flush all rules
iptables –D INPUT 2
◼ deletes 2nd rule in INPUT chain
iptables –I INPUT 1 –p tcp –-syn –s 232.16.4.0/24 –
d 0/0:22 –j ACCEPT
◼ -I INPUT 1: insert INPUT rule at top
◼ Accept TCP SYNs to from 232.16.4.0/24 to firewall port 22
(ssh)
Syntax of iptables commad
▪ -t <-table->: If you don't specify a table, then the filter table is assumed. As
discussed before, the possible built-in tables include: filter, nat, mangle
▪ -j <target>: Jump to the specified target chain when the packet matches the
current rule.
◼ FILTER
◼ Used to filter the traffic
◼ 3 chain types: INPUT, OUTPUT and FORWARD
◼ NAT
◼ Hide internal network hosts from outside world. Outside world
only sees the gateway’s external IP address, and no other
internal IP addresses
◼ PREROUTING, POSTROUTING, INPUT and OUTPUT
◼ MANGLE
◼ Used to modify the some fields in the packet header
◼ Not widely used
◼ Don’t worry about it
Tables, Chains & Rules
Syntax
iptables -t table -OP chain specifiers
iptables -t table -OP chain line# specifiers
iptables -OP chain specifiers
sudo iptables -I INPUT 1 -i lo -j ACCEPT
Tables, Chains & Rules
◼ First FIT:
◼ Examples:
iptables –A input –s 192.168.100.0/24 –p tcp –j ACCEPT (A: Append)
◼ Permit SSH & deny TELNET
iptables –A INPUT –p tcp --dport 22 –j ACCEPT
iptables –A INPUT –p tcp --dport telnet –j DROP
◼ Delete rule (D: Delete)
iptables –D INPUT 2 ou
iptables –D INPUT -p tcp --dport telnet –j DROP
◼ Action: Targets/Jumps:
◼ ACCEPT, REJECT, DROP, MASQUERADE, RETURN
Chain Operations
◼ Listing a chain
iptables -t table -L chain
iptables -L chain
iptables -L
◼ Flags
-n addr/port numbers rather than names
-v verbose
--line-numbers include line numbers in the listing
Chain Operations
◼ Flushing a chain
Deletes all rules associated with a chain
iptables -t table -F chain
iptables -F chain
iptables -F
◼ Examples
◼ echo-request
◼ echo-reply
◼ destination-unreachable
◼ time-exceeded
Filtering
◼ @ MAC:
◼ iptables -A INPUT -p tcp -m mac --mac-source 00:bb:aa:cc:ed:08 -j DROP
◼ iptables -A INPUT -p tcp -m mac --mac-destination 00:bb:aa:cc:ed:08 -j DROP
◼ Port redirection:
◼ iptables -t nat -A PREROUTING -p tcp --dport 2323 -j REDIRECT --to-port 23
Filtre: Default action
◼ Default policy
flag set
◼ Possible flags
◼ ACK
◼ FIN
◼ RST
◼ PSH
◼ SYN
◼ URG
SYN
• SYN FLOOD:
• Syn request held for 75 sec
SYN
SY
SY C K
N+
SYN+ACK
AC
N
A
K
SY AC
SY + A
ACK
N K
N CK
SYN
SYN+ACK SYN
SYN SYN+ACK
Source SYN+ACK victime ACK
192.168.0.7 182.108.0.7
N victime
SY AC K
N+ 182.108.0.7
SY K
AC
DoS DDoS
K CK
N YN
AC + A
SY S
Rate Limits
◼ rate/unit
◼ rate
◼ Packets per unit time
◼ unit
◼ Second, minute, hour, day
◼ NEW
◼ conntrack
◼ Newest version
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Targets/Actions
◼ Target types
◼ -j ACCEPT
◼ Lets the packet satisfying the specification pass to the next chain in
the packet path
◼ -j DROP
◼ The packet satisfying the specification is dropped with no error
packet sent to the sender
◼ Stealth mode – used for packet blocking on sensitive hosts
Firewall Actions
◼ -j REJECT
◼ The packet satisfying the specification is dropped with an error packet sent
to the sender
◼ -j REJECT default error is port unreachable
◼ -j REJECT --reject-with flag
◼ icmp-net-unreachable
◼ icmp-host-unreachable
◼ icmp-port-unreachable
◼ Icmp-proto-unreachable
◼ icmp-net-prohibited
◼ icmp-host-prohibited
◼ tcp-reset
◼ Sends a tcp packet with the RST bit set
Firewall Actions
◼ -j LOG
◼ Causes the packet satisfying the specification to be logged using the
Syslog facility
◼ --log-prefix “IPT description of entry”
◼ IPT identifies the source of the log entry, i.e. Iptables
◼ Description within quotes is limited to 29 characters
◼ --log-ip-options
◼ --log-level
◼ --log-tcp-options
◼ --log-tcp-sequence
◼ To log a dropped packet a log rule must precede the dropping rule
Log files
◼ Exemple:
◼ iptables -A OUTPUT -j LOG (nano /etc/syslog.conf)
◼ iptables -A INPUT -j LOG
◼ iptables -A FORWARD -j LOG
◼ Log rejected packets inside /var/log/messages
◼ iptables -N LOG_DROP
◼ iptables -A LOG_DROP -p tcp --dport 22 -j LOG --log-prefix ``ssh
access attempt``
◼ iptables -A LOG_DROP -p tcp --dport 22 -j DROP
◼ iptables –I INPUT 1 –j LOG_DROP
Backup your configuration
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 10
tcpflags = syn
command = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
Knockd: port knocking server
Details: https://www.digitalocean.com/community/tutorials/how-to-use-port-knocking-to-hide-your-
ssh-daemon-from-attackers-on-ubuntu
nano /etc/default/knockd
START_KNOCKD=1
KNOCKD_OPTS="-i eth0“
On the Client
#!/bin/bash
for i in 100 200 300 400
do
nmap -Pn --max-retries 0 -p $i 10.102.178.168
done
./knockknock.sh
Port knocking
#!/bin/bash
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -N KNOCKING
iptables -N GATE1
iptables -N GATE2
iptables -N GATE3
iptables -N PASSED
iptables -A GATE1 -p tcp --dport 1111 -m recent --name AUTH1 --set -j DROP
iptables -A GATE1 -j DROP
#!/bin/bash
[$# != 0 ] && echo number of argument && exit 128
your_server=$1
for x in 1111 2222 3333;
do
nmap -Pn --host_timeout 201 --max-retries 0 -p $x your_server && sleep 1;
done && ssh amir@${yourserver}
◼
Uncomplicated firewall: ufw
◼ sudo ufw deny http
◼ sudo ufw deny from 203.0.113.4
◼ sudo ufw deny out 25
◼ sudo ufw allow proto tcp from 202.54.2.5 to 172.24.13.45 port 22
◼ sudo systemctl status ufw.service
◼ sudo ufw status
◼ sudo ufw enable
◼ sudo ufw status => list rules
◼ sudo ufw reload
◼ ufw status numbered
◼ ufw delete 1
◼ sudo ufw delete allow http or ufw delete allow 8080
◼ sudo ufw disable
◼ sudo ufw reset
Uncomplicated firewall: ufw