# nmap -sF -p 1-100 66.165.165.2Starting nmap V. 2.54BETA31 ( www.insecure.org/nmap/ )
Interesting ports on ns.example.com (66.165.165.2):
(The 97 ports scanned but not shown below are in state: closed)
Port State Service
22/tcp open ssh
53/tcp open domain
80/tcp open http
iptables -A INPUT -p ICMP -m icmp --icmp-type
\
echo-request
-m limit --limit 1/minute \
-j LOG
--log-prefix "IMCP-packet "
# ping xxx.xxx.xxx.255
# nmap -sP xxx.xxx.xxx.0/24
# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
Or add it to your /etc/sysctl.conf file to make it permanent:
net.ipv4.icmp_echo_ignore_broadcasts = 1
Then run sysctl to update the change
# sysctl -w
# modprobe ip_tables
# lsmod | grep ip_tables
ip_tables
13696 0 (unused)
Kernel Config
Diagram of how packets traverse
the three basic chains in the filter table
(adapted from Iptables HOWTO by
Rusty Russell)
*************** 1. *****************
When a packet enters the firewall system the kernel first looks at the destination address of the packet and makes a routing decision.
This rule allows ping traffic from the internal private network:
iptables -A INPUT -p icmp -s 192.168.1.0/24 -j ACCEPT
These rules deny icmp echo-request traffic and traceroute traffic to the firewall machine from any source address:
iptables -A INPUT -s 0/0 -d $LOCALIP -p icmp \
--icmp-type echo-request -j DROP
iptables -A INPUT -s 0/0 -d $LOCALIP -p udp \
-dport 33435:33525 -j DROP
If forwarding is enabled in the kernel, and the packet is destined for another network card.
A program running on the local system can also send packets
--sport source port --dport destination port -o outgiong interface -i incoming interface
iptables -A OUTPUT -o eth1 -p tcp --sport \
1024:65535 --dport 113 -s $LOCALIP -d $ANYWHERE \
-j ACCEPT
| -P | policy |
| -s | source network |
| -p | protocol |
| ACCEPT | If packet is perfectly matched, the chain is stopped and the packet accepted |
| DROP | Packet is blackholed and not processed and further |
| LOG | Great for debugging, allows logging of short messages to syslogd. |
| REJECT | Sam as DROP, but sends back an error message. Valid in INPUT, FORWARD, and OUTPUT chains. |
| SNAT | Source Network Address Translation. Only valid with POSTROUTING. The argument --to-source can take a range of IPs. Makes your firewall look like several hosts. (used in masquerading with nat) |
| DNAT | Destination Network Address Translation. Only valid with PREROUTING in nat table. The argument --to-destination can take a range of IPs. (used in port forwarding with nat) |
| MASQUERADE | No address required (unlike SNAT), works with DHCP connections. Only valid with POSTROUTING in nat table. Masquerades source address to firewall address whatever it may be. |
| MARK | Only valid in mangle table. For changing information in packet header |
The filter table
The nat table
iptables -t nat -A PREROUTING
-p tcp --dport 80 \
-i eth1 -j DNAT --to 192.168.1.21:80
iptables -t nat -A POSTROUTING
-o eth1 -s 192.168.1.0/24 \
-j snat --to-source 10.2.135.2
iptables -A POSTROUTING -t nat -o $EXT_INT
-j MASQUERADE
The mangle table
(I haven't listed the mangle table in this diagram)
This is one way to go about locking down a private network
Testing HTTP
$ telnet 10.1.7.12 80
Trying 10.2.135.2...
Connected to 10.2.135.2.
Escape character is '^]'.
GET /default.htm HTTP/1.0
<HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
MicrosoftOfficeWebServer: 5.0_Pub
Date: Wed, 07 May 2003 01:20:34
GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Mon, 07 Apr 2003
15:14:59 GMT
ETag: "80db2d7418fdc21:928"
Content-Length: 12513
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
...
</html>
Testing SSH
$ ssh 10.2.135.12
pattyo's passwd:
Authentication successful.
...
Testing Sendmail
$ telnet xx.xx.xx.xx 25
Trying xx.xx.xx.xx...
Connected to mail.example.com.
Escape character is '^]'.
220 ponto.example.com ESMTP
SMTP spoken here; Tue, 6 May 2003 18:30:43 -0700
$ nslookup mail.miracosta.edu
Name: mail.miracosta.edu
Address: 10.1.4.13
$ telnet 10.1.4.13 25
Trying 10.1.4.13...
Connected to 10.1.4.13.
Escape character is '^]'.
220 ma.usa.miracosta.cc.ca.us
Microsoft ESMTP MAIL Service, Version: 5.0.2195.5329 ready at Tue, 6 May 2003 18:27:42 -0700
$ telnet 172.20.2.15 25
Trying 172.20.2.15...
Connected to 172.20.2.15.
Escape character is '^]'.
220 mcc.miracosta.cc.ca.us ESMTP
Sendmail 8.9.1a/8.9.1; Tue, 6 May 2003 18:25:23 -0700 (PDT)
<Cntr>]
telnet> quit
$ nmap 172.20.2.15
Starting nmap V. 2.54BETA31
( www.insecure.org/nmap/ )
Warning: You are not root
-- using TCP pingscan rather than ICMP
Interesting ports on mcc.miracosta.cc.ca.us
(172.20.2.15):
(The 1544 ports scanned but
not shown below are in state: closed)
Port
State Service
21/tcp
open ftp
23/tcp
open telnet
25/tcp
open smtp
53/tcp
open domain
80/tcp
open http
81/tcp
open hosts2-ns
106/tcp open
pop3pw
107/tcp open
rtelnet
110/tcp open
pop-3
3128/tcp open
squid-http
# iptables -L
# iptables -t mangle -L -n
# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target
prot opt source
destination
DNAT
tcp -- 0.0.0.0/0
10.2.135.2 tcp dpt:25 to:192.168.1.20:25
DNAT
tcp -- 0.0.0.0/0
10.2.135.2 tcp dpt:143 to:192.168.1.20:143
DNAT
tcp -- 0.0.0.0/0
10.2.135.2 tcp dpt:22 to:192.168.1.20:22
DNAT
tcp -- 0.0.0.0/0
10.2.135.2 tcp dpt:80 to:192.168.1.20:80
DNAT
tcp -- 0.0.0.0/0
10.2.135.2 tcp dpt:443 to:192.168.1.20:443
Chain POSTROUTING (policy ACCEPT)
target
prot opt source
destination
MASQUERADE all --
0.0.0.0/0
0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
target
prot opt source
destination
# grep SRC iptables* | sed -e 's/.*SRC=//' \
-e 's/ .*//'| sort | uniq -c | \
sort -nr |head -20
# ip
COUNT IPADDR
--------------------
3968 209.129.35.85
659 68.101.245.6
649 64.160.54.43
423 63.227.63.133
415 68.3.97.223
399 64.164.72.41
397 24.165.68.196
378 24.165.11.5
372 68.86.76.83
344 12.233.38.156
342 68.6.108.118
294 63.207.12.171
288 66.235.25.68
284 65.94.105.137
216 24.130.227.252
213 67.68.234.84
213 24.165.41.65
210 68.105.74.31
210 12.219.228.245
204 65.212.170.46
# port
COUNT PORT
--------------------
17253 6112
2740 1433
2340 137
1738 445
1380 1434
566 135
564 139
357 3006
345 21
231 3439
167 6970
131 1921
127 3128
122 17300
115 27374
90 1336
87 3463
86 111
83 1080
82 8080