Author Topic: รวมความรู้เกี่ยวกับ set iptables คืออะไร ใช้งานยังไง by golfreeze  (Read 35017 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
About the script: The script below is used to make an effective and strong firewall for the Gateway
machines. The script is commented to make it self explanatory. This script is completely done using
Iptables.

#!/bin/bash
# iptables, by SysAdmin,
# description: Script for setting IPTABLES rules for Gateway
# processname: iptables

The below options will make us adjust the script parameters in one shot. If there requires a
situation like if we want to swap the eth0 and eth1 for some reason, we can adjust that in the below
option rather than adjusting that for the whole script manually. I am using my network configuration
in here. So modify this according to your requirements.

INTERNALIF="eth1" # Internal Ethernet card identifier
INTERNALNET="192.168.1.0/24" # Internet network address range
INTERNALBCAST="192.168.1.255" # Internal Broadcast address
EXTERNALIF="eth0" # External Ethernet card identifier

# Pathnames
DMESG="/bin/dmesg" # Location of Dmesg
IPTABLES="/sbin/iptables" # Location of Iptables

The section below is used to control the script using “Start” or
“Stop” or “Restart” functions just like controlling services
of a Linux machine. The script has to be called like in example below

# sh /firewall.sh start

if [ "$1" = "" ] # this will make the script to exit if no argument
is present
then
echo "Usage: sh /fire.sh (start|stop|restart)"
exit
else
case "$1" in
stop)
action "Shutting down firewall:" echo
$IPTABLES -F
$IPTABLES -P FORWARD DROP
exit 0
;;
status)
echo "The status command is not supported for iptables"
exit 0
;;
restart|reload)
$0 stop
exec $0 start
;;
start)
echo "Starting Firewall:"
;;
*)
echo "Usage: firewall (start|stop|restart)"
exit 1
esac
fi

To avoid any conflicts that may arise in the implementation of the Firewall, its better to flush
every rules that were actually present in the chains and then to start over.

$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD

The below are some tweaks that can be done to SysCtl to avoid make kinds of attacks. They are pretty
simple yet effective.

Disabling IP Spoofing attacks:

IP spoofing is one of the most common forms of on-line camouflage. In IP spoofing, an attacker gains
unauthorized access to a computer or a network by making it appear that a malicious message has come
from a trusted machine by “spoofing” the IP address of that machine.

echo 2 > /proc/sys/net/ipv4/conf/all/rp_filter

The below change will stop the Gateway from responding to broadcast pings.

echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

Block Source Routing:

Source routing allows the sender of the packet to specify the route that a packet must take in
traveling to the destination. If the selected route is not available for any reason, the packet
would not be delivered. If the recipient replied to the packets, the response would follow the same
route. it can be used to discover the IP addresses of routers within a network. However, it also has
the potential for misuse. A malicious user could use source routing to learn more about a network
that he or she is targeting for attack. Data packets contain information about where they have been
and what machines they have transited. A malicious user might send data into a network in order to
collect information about the network's topology. Its better to block source routing from the
Gateway itself

echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route

Kill timestamps.

echo 0 > /proc/sys/net/ipv4/tcp_timestamps

Trying to prevent SYN flood attacks:

A SYN flood is a form of denial-of-service attack in which an attacker sends a succession of SYN
requests to a target's system. One of most important steps is to enable the operating system's
built-in protection mechanisms like SYN cookies. In Linux, we can set the SYN Cookies in the
following manner.

echo 1 > /proc/sys/net/ipv4/tcp_syncookies

Disable all redirection requests in Gateway machine:

echo 0 >/proc/sys/net/ipv4/conf/all/accept_redirects

The below command enables bad error message protection

echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses

#Allow dynamic ip addresses

echo "1" > /proc/sys/net/ipv4/ip_dynaddr

Its recommend to log packets with impossible addresses. This can be done by giving a positive
parameter to the following sysctl parameter.

echo 1 > /proc/sys/net/ipv4/conf/all/log_martians

Set a local port range:

By default, the local port range is 32768 to 61000 for all new 2.6 kernel based systems. Its
explicitly mentioned in the Firewall script because if we need to increase that further, we can
adjust the port range throug the Firewall script itself.

echo "32768 61000" > /proc/sys/net/ipv4/ip_local_port_range

Denial of Service (DoS) attacks: Denial of Service attack is an attempt by a malicious (or
unwitting) user, process, or system to prevent legitimate users from accessing a resource (usually a
network service) by exploiting a weakness or design limitation in an information system. Examples of
DoS attacks include flooding network connections, filling disk storage, disabling ports, or removing
power. This can be limited by setting timeouts.

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
echo 0 > /proc/sys/net/ipv4/tcp_sack
echo 1280 > /proc/sys/net/ipv4/tcp_max_syn_backlog

Basic Rules of a typical Firewall:

Kill INVALID packets with illegal combination flags.

$IPTABLES -A INPUT -m state --state INVALID -j DROP
$IPTABLES -A FORWARD -m state --state INVALID -j DROP

No restrictions to connections from localhost

$IPTABLES -A INPUT -i lo -j ACCEPT

Reject connections from Outside world to Internal loop back device.

$IPTABLES -A INPUT -d 127.0.0.0/8 -j REJECT

No restrictions for traffic generating from legit internal addresses

$IPTABLES -A INPUT -i $INTERNALIF -s $INTERNALNET -j ACCEPT

Incase we have to use IPv6 addresses in your environment uncomment the below line:

#$IPTABLES -A INPUT -p ipv6 -j ACCEPT

Kill all packets from Outside world claiming to be packets generated from Internal network.

$IPTABLES -A INPUT -i $EXTERNALIF -s $INTERNALNET -j REJECT

Block ICMP requests.

$IPTABLES -A FORWARD -p icmp --icmp-type echo-request -o $INTERNALIF -j REJECT

Prevent Ping flood attacks:

$IPTABLES -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j DROP

Deny pings to local broadcast address:

$IPTABLES -A INPUT -p icmp -d $INTERNALBCAST -j DROP

#Allow all other icmp

$IPTABLES -A INPUT -p icmp -j ACCEPT

No restrictions to established connections:

$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Dont forward SMB related traffic. Samba Services are one of the most aimed targets by hackers.

$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 137 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 138 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p tcp --dport 139 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 137 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 138 -j REJECT
$IPTABLES -A FORWARD -o $EXTERNALIF -p udp --dport 139 -j REJECT
$IPTABLES -A INPUT -i $EXTERNALIF -p udp --dport 137 -j REJECT

1. Disable Samba Share

#$IPTABLES -A INPUT -p tcp --dport 137 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 137 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 138 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 138 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 139 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 139 -j ACCEPT

Allow all other connections to be forwarded

$IPTABLES -A FORWARD -o $EXTERNALIF -i $INTERNALIF -j ACCEPT

Allow replies from established connections :

$IPTABLES -A FORWARD -i $EXTERNALIF -m state --state ESTABLISHED,RELATED -j ACCEPT

#Allow yourself to be a DHCP server for your inside network
#Necessary because the default rule allowing valid addresses ignores broadcast

$IPTABLES -A INPUT -i $INTERNALIF -p tcp --sport 68 --dport 67 -j ACCEPT
$IPTABLES -A INPUT -i $INTERNALIF -p udp --sport 68 --dport 67 -j ACCEPT

1. This section is to allow nameserver packets.

cat /etc/resolv.conf | \
awk '/^nameserver/ {print $2}' | \
xargs -n1 $IPTABLES -A INPUT -p udp --sport 53 -j ACCEPT -s

#From here on, we are dealing with connection attempts. These services are not needed for our
Gateway. But just incase we happen to have them up and running, just uncomment and restart the
firewall script.

# ftp-data
#$IPTABLES -A INPUT -p tcp --dport 20 -j ACCEPT

# ftp
#$IPTABLES -A INPUT -p tcp --dport 21 -j ACCEPT
# ssh

#$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
#telnet

#$IPTABLES -A INPUT -p tcp --dport 23 -j ACCEPT

# smtp One per second limt -burst rate of ten
#$IPTABLES -A INPUT -p tcp --dport 25 --syn -m limit --limit 1/s \
# --limit-burst 10 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 25 --syn -j DROP
#$IPTABLES -A INPUT -p tcp --dport 25 -j ACCEPT

# DNS
#$IPTABLES -A INPUT -p tcp --dport 53 -j ACCEPT
#$IPTABLES -A INPUT -p udp --dport 53 -j ACCEPT

# http
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT

# POP-3
#$IPTABLES -A INPUT -p tcp --dport 110 -j ACCEPT

# identd
#$IPTABLES -A INPUT -p tcp --dport 113 -j ACCEPT

# https
#$IPTABLES -A INPUT -p tcp --dport 443 -j ACCEPT

#VNC Server
#$IPTABLES -A INPUT -p tcp --dport 5801 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 5901 -j ACCEPT
#$IPTABLES -A INPUT -p tcp --dport 6001 -j ACCEPT

##Some ports should be denied and logged.

$IPTABLES -A INPUT -p tcp --dport 1433 -m limit -j LOG --log-prefix "Firewalled packet: MSSQL
"
$IPTABLES -A INPUT -p tcp --dport 1433 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6670 -m limit -j LOG --log-prefix "Firewalled packet:
Deepthrt "
$IPTABLES -A INPUT -p tcp --dport 6670 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6711 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6711 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6712 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6712 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6713 -m limit -j LOG --log-prefix "Firewalled packet: Sub7
"
$IPTABLES -A INPUT -p tcp --dport 6713 -j DROP
$IPTABLES -A INPUT -p tcp --dport 12345 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 12345 -j DROP
$IPTABLES -A INPUT -p tcp --dport 12346 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 12346 -j DROP
$IPTABLES -A INPUT -p tcp --dport 20034 -m limit -j LOG --log-prefix "Firewalled packet:
Netbus "
$IPTABLES -A INPUT -p tcp --dport 20034 -j DROP
$IPTABLES -A INPUT -p tcp --dport 31337 -m limit -j LOG --log-prefix "Firewalled packet: BO
"
$IPTABLES -A INPUT -p tcp --dport 31337 -j DROP
$IPTABLES -A INPUT -p tcp --dport 6000 -m limit -j LOG --log-prefix "Firewalled packet: XWin
"
$IPTABLES -A INPUT -p tcp --dport 6000 -j DROP

#Traceroutes depend on finding a rejected port. DROP the ones it uses

$IPTABLES -A INPUT -p udp --dport 33434:33523 -j DROP

#Don't log web or ssl because people surfing for long times lose connection tracking and cause
the system to create a new one, flooding logs.

$IPTABLES -A INPUT -p tcp --dport 80 -j REJECT
$IPTABLES -A INPUT -p tcp --dport 443 -j REJECT

The below rules will be effective it it doesnt match any of the above rules. A log for all
connections will be “noise” and will give us a hard time to find something
relevant from the flooded logs. So the rules below just logs connection requests.

$IPTABLES -A INPUT -p tcp --syn -m limit --limit 5/minute -j LOG --log-prefix "Firewalled
packet:"
$IPTABLES -A FORWARD -p tcp --syn -m limit --limit 5/minute -j LOG --log-prefix "Firewalled
packet:"

#Reject

$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A INPUT -p all -j DROP

$IPTABLES -A FORWARD -p tcp -j REJECT --reject-with tcp-reset
$IPTABLES -A FORWARD -p all -j DROP

#Accept it anyway if it's only output
$IPTABLES -A OUTPUT -j ACCEPT

EXPLICITLY BLOCKING SERVICE PORTS FOR GATEWAY FROM OUTSIDE WORLD

/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 21 # ftp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 22 # ssh
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 23 # telnet
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 25 # smtp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 53 # domain
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 79 # finger
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 80 # httpd
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 110 # pop3
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 111 # sunrpc
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 137 # netbios-ns
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 138 # netbios-dgm
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 139 # netbios
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 143 # imap
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 161 # snmp
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 162 # snmp trap
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 443 # https
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 515 # printer
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 1080 # socks
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 2049 # nfs
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 3128 # squid
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 3306 # mysql
/sbin/iptables -A INPUT -j DROP -i eth0 -p tcp --dport 5432 # postgres

1. EXPLICITLY BLOCKING SERVICE PORTS FOR UDP CONNECTIONS

/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 21 # ftp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 23 # telnet
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 25 # smtp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 53 # domain
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 79 # finger
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 110 # pop3
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 111 # sunrpc
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 137 # netbios-ns
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 138 # netbios-dgm
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 139 # netbios
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 143 # imap
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 161 # snmp
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 162 # snmp trap
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 443 # https
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 515 # printer
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 1080 # socks
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 2049 # nfs
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 3128 # squid
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 3306 # mysql
/sbin/iptables -A INPUT -j DROP -i eth0 -p udp --dport 5432 # postgres

1. this line will prevent people on the internet (or you) from pinging the gateway

/sbin/iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j DROP

exit 0
« Last Edit: พฤศจิกายน 10, 2011, 11:07:00 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #1 on: มกราคม 23, 2011, 12:48:38 AM »
#good document
http://www.gophernet.org/articles/iptables/

#junning secure stack in *nix
http://www.cromwell-intl.com/security/security-stack-hardening.html

เราสามารถ check ว่ามี packet ผ่าน rule ของเราสร้างให้ nat ได้โดย

watch -n 2 "iptables -L -t nat -v -nx"

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #2 on: มกราคม 23, 2011, 12:48:44 AM »
Syntax and Use of IPTables

A comprehensive documentation of IPTables/NetFilter is available online (see references below) and
additional documentation can be found in the IPTables man pages.

IPTables has the following options to manage whole chains:

-N — Create a new chain.

-X — Delete an empty chain.

-P — Change the policy for a built-in chain.

-L — List the rules in a chain.

-F — Flush the rules out of a chain.

-Z — Zero the packet and byte counters.

The following are ways to manipulate rules inside the chain:

-A — Append a new rule.

-I — Insert a new rule.

-R — Replace a rule.

-D — Delete a rule.

A simple script with comments written by Oskar Andreasson of BoingWorld.com is shown in Listing 1.
The script shows the implementation of some simple IP forwarding, masquerading (NAT) spoofing checks
of non-routable addresses (RFC191, and the opening of some ports to allow access. If you are
familiar with ipchains, you will probably have little difficulty in following most of the rules.

The command:

iptables -P <chain name> <policy>

sets the default policy for the chain, either ACCEPT or DROP (DENY). Only built-in chains (INPUT,
OUTPUT, and FORWARD) have policies. Packet-mangling activities that modify the packets in transit
(such as NAT and proxying) use two additional predefined chains, PREROUTE (usually for DNAT), and
POSTROUTE (usually for SNAT). The names are descriptive enough and convey the fact that DNAT
destination addresses are typically rewritten before other chain rules are applied, and SNAT source
addresses are rewritten after they have traversed the rule chains.

Rules are added to chains using the following syntax:

iptables -A <chain name> <match condition> -j <jump>

>The condition is a logical match of, for example, the following:

-s <source IP>

-d <destination IP>

-sport <source port>

-dport <destination port>

-p <protocol tcp, udp, or icmp>

-m <match, e.g., MAC address or TCP state>

-owner <user/group/process/session id>

The above match conditions are meant to be extensible, and numerous other extensions exist. To learn
more about a match extension, use the -h option. For example, for the ICMP protocol (-p), type:

#/sbin/iptables -p icmp -h

and a list of the three dozen or so -icmp-type extensions will be displayed.

The jump (or “judgement”) following the -j is one of the following:

ACCEPT — Accept the packet.

DROP — Drop the packet.

REJECT — Drop the packet and respond with “port-unreachable” ICMP packet.

NAT — Rewrite the packets source or destination address.

LOG — Log to the kernel logging daemon klogd.

TOS — Impose a type/level of service.

Example Applications of Some Rules

Here are some example rulesets for the features described in the “Enhancements” section:

1. Stateful Inspection — The rule:

/sbin/iptables -A FORWARD -m state --state \
ESTABLISHED,RELATED -j ACCEPT

forwards packets across the firewall that are part of a pre-existing connection. Besides ESTABLISHED
(packet is part of existing connection) and RELATED (packet is related to existing connection and
passing in same direction), other defined states are NEW (packet is trying to create a new
connection), INVALID (packet doesn’t match any exisitng connection), and RELATED+REPLY (packet is
not part of an existing connection, but is related to one (e.g., ftp-data transfer requested
following an existing ftp-control session).

2. DNAT — To redirect traffic from the 10.0.0.0 network to the Web server 10.0.1.1 to the Web server
10.0.1.100, use the rule:

# /sbin/iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d \
10.0.1.1 -p tcp --dport 80 -j DNAT --to 10.0.1.100

You can implement a load-balancing solution and redirect all incoming traffic to port 8080 on a
group of servers IP 10.0.1.100-10.0.1.102:

# /sbin/iptables -t nat -A POSTROUTING -p -s 10.0.0.0/24 \

-d 10.0.1.1 tcp -dport 80 -j DNAT --to 10.0.1.100-10.0.1.102:8080

3. Enhanced TCP monitoring — To check all six TCP flags and check that the SYN and ACK flags are
set:

#/sbin/ iptables -A INPUT -p tcp --tcp-flags ALL SYN,ACK -j DROP

To deny outgoing connections to insecure telnet, FTP, and rsh services using a single command:

# /sbin/iptables -A input -t DENY -p tcp --destport telnet,ftp,shell

Note that in ipchains, each port would have required its own separate rule.

4. Filtering by MAC address — The rule:

# /sbin/iptables -A FORWARD -m state --state \

NEW -m mac --mac-source 00:C7:8F:72:14 -j ACCEPT

allows only outgoing packets from a known MAC address, given in colon-separated hex notation.

5. Enhanced logging:

# /sbin/iptables -A INPUT -s 192.168.0.1 -m limit -limit \

1/second -j LOG

limits the rate of writes to the logs to one per second. Specific matches can be labeled. For
example, log entries corresponding to connect requests from the Litigation Department’s Cisco 1601
router can be labeled:

# /sbin/iptables -A INPUT -s 192.168.5.254 \

-j LOG --log-prefix ' ## Litigation Dept Cisco 1601 ## '

The logfile entry looks like:

Aug 1 14:58:39 mymachine kernel: ## Litigation Dept Cisco 1601 \

## IN=eth0 OUT= MAC=00:f0:28:2c:69:67:00:00:7a:93:5e:62:08:00 \

SRC=192.168.5.254 DST=192.168.1.254 LEN=40 TOS=0x00 PREC=0x00 \

TTL=247 ID=21864 DF PROTO=TCP SPT=42300 DPT=23 WINDOW=8760 RES=0x0

0 RST URGP=0

6. Rate-Limited matching — To protect from syn-flood denial of service, only accept a maximum of 1
per second:

#/sbin/iptables -A FORWARD -p tcp -syn -m limit -limit 1/s -j ACCEPT

7. Type of Service (TOS) prioritization — To maximize ssh response while maintaining maximum file
data transfer over HTTP connections, the following rule can be applied:

# /sbin/iptables -A PREROUTING -t mangle -p tcp --sport ssh \

-j TOS --set-tos Minimize-Delay

# /sbin/iptables -A PREROUTING -t mangle -p tcp --sport http \

-j TOS --set-tos Maximize-Throughput

Conclusion

This article explains some new firewalling features of IPTables that administrators of
ipchains-based Linux firewalls may find useful. My aim was also to provide a starting point for
administrators who are familiar with ipchains based-firewalling and are considering a move to
IPTables/NetFilter. Much of the hoopla surrounding the Linux 2.4 kernel has revolved around IPTables
support for stateful packet inspection. However, I hope that this article has shown that there is
more to IPTables than merely stateful inspection. IPTables also can provide firewalling scripts that
are cleaner and easier to read, and easier to maintain. It seems that with its many powerful new
features, Open Source Linux firewalling has finally come of age.

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #3 on: มกราคม 23, 2011, 12:49:26 AM »


#step to ทำ nat เป็น firewall
1.สร้าง aliase interface ที่ตัว nat server ก่อน
2.สร้าง rule สำหรับ dnat ขาเข้า
3.สร้าง rule สำหระบ snat ขาออกไป ครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #4 on: มกราคม 23, 2011, 12:49:32 AM »
I. Filter Table
เป็นตารางที่ใช้งานมากที่สุด เป็นจุดที่ใช้ในการตรวจสอบและควบคุมการผ่านเข้าออกของ packet
ถ้าหากจะพิจารณาการไหลเวียนของ packet เฉพาะในส่วนของ filter table โดยไม่สนใจ table อื่นๆ นั้น
ก็พอจะแสดงให้เห็นได้ดังภาพที่ 1 โดยเมื่อ packet เข้ามาในระบบ มันจะเข้าไปยัง routing decision
เพื่อตัดสินใจว่า packet จะถูกส่งไปที่ใด

* ในกรณีที่ packet ถูกส่งผ่านไปยังเครื่องอื่น packet นั้นจะต้องถูกตรวจสอบโดย rule ใน FORWARD
chain
* ถ้า packet นั้น มีเป้าหมายเป็นเครื่องปัจจุบัน (เครื่องที่รัน iptables อยู่นี้ เรียกอีกอย่างว่า
linux box) ตัว packet จะถูกตรวจสอบโดย rule ใน INPUT chain
* และในกรณีที่ packet ถูกสร้างจากเครื่องปัจจุบัน (linux box) ตัว packet จะถูกตรวจสอบจาก rule ใน
OUTPUT chain ก่อนที่จะถูกส่งออกไป


รูปที่ 1 แสดงให้เห็นว่า packet มีเส้นทางการเดินทางอย่างไรเมื่อเข้ามาในระบบ (filter table)

ดังภาพ iptables ประกอบไปด้วย built-in chain จำนวน 3 chain ซึ่งไม่สามารถลบได้คือ INPUT, OUTPUT,
FORWARD เมื่อเครื่องคอมพิวเตอร์เริ่มทำงานในครั้งแรก ทั้งสาม chain จะมี default policy เป็น ACCEPT
ซึ่งหมายความว่าอนุญาตให้ทุกอย่างผ่านเข้าออกได้หมด และสำหรับ FORWARD chain นั้น ถึงแม้จะได้กำหนดให้
policy เป็น ACCEPT แล้ว packet ก็จะยังไม่สามารถถูก forward ไปยังจุดหมายที่ต้องการได้
ตราบใดที่ยังไม่ได้เซ็ตให้ enable IP forwarding ทั้งนี้โดย default แล้ว forward=0 สามารถกำหนดให้
enable IP forwarding (forward=1) ได้ โดย

ใช้คำสั่ง echo "1" > /proc/sys/net/ip_forward เพื่อกำหนดให้ IP forwarding เป็น
enable เพื่อให้ Linux box สามารถ forward ip packet ได้
ในบางครั้งนั้นการใช้คำสั่งดังกล่าวทุกครั้งอาจจะไม่สะดวก สามารถแก้ไขไฟล์ configuration ที่
/etc/sysctl.conf แล้ว set ให้ net.ipv4.ip_forward=1 เพื่อเป็นการแก้ไขแบบถาวร

ในกรณีที่ต้องการให้สนับสนุนการทำงานกับ dynamic IP ด้วย เช่น PPP, SLIP, DHCP
ก็สามารถทำได้โดยใช้คำสั่ง echo "1" > /proc/sys/net/ipv4/ip_dynaddr ได้เช่นเดียวกัน

II. Mangle Table
เป็นตารางที่ใช้สำหรับแก้ไขข้อมูล TOS, TTL, MARK ของ packet ซึ่งโดยปกติแล้วแทบจะไม่ได้ใช้งาน
และไม่ควรทำ packet filtering หรือกรอง packet ที่ตารางนี้ รวมทั้งไม่ควรทำ DNAT, SNAT หรือ
Masquerading ที่ตารางนี้อย่างเด็ดขาดด้วย

III. Nat Table
เป็นตารางที่ใช้สำหรับทำ network address translation เช่น เปลี่ยนค่า source ip address, destination
ip address จุดสำคัญอีกอย่างหนึ่งที่ต้องรู้ก็คือ มีเพียง packet แรกเท่านั้นที่เข้ามาที่ chain นี้
ส่วน packet ถัดไปนั้นจะถูกกระทำเหมือนที่ packet แรกได้รับ ดังนั้นจึงไม่ควรทำ packet filtering ที่
chain เหล่านี้

การใช้งาน Nat table นั้นก็เพียงแต่ใช้ออปชัน -t nat เท่านั้น และ target ที่สามารถใช้งานได้คือ SNAT,
DNAT, Masquerade, Redirect ซึ่งมีรายละเอียดดังนี้

* SNAT
การทำ source NAT จะทำที่ POSTROUTING chain โดย หลักๆ คือทำการเปลี่ยน source address
ก่อนที่จะส่ง packet นั้นออกไป ซึ่งสามารถใช้ออปชัน -o (outgoing interface) ร่วมด้วยได้
นอกจากนี้ยังใช้ -j SNAT และ --to--source หรือ --to เพื่อเปลี่ยนไอพีแอดเดรสหรือ port ไปตามต้องการได้
เช่น

##เปลี่ยน source ip address เป็น 1.2.3.4
# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4

##เปลี่ยน source address เป็น 1.2.3.4, 1.2.3.5 หรือ 1.2.3.6
# iptables -t nat -A POSTROUING -o eth0 -j SNAT --to 1.2.3.4-1.2.3.6

##เปลี่ยน source address เป็น 1.2.3.4 port 1-1023
# iptables -t nat -A POSTROUTING -p tcp -o eth0 -j SNAT --to 1.2.3.4:1-1023

Masquerading
การทำ masquerade นั้นเป็นหนึ่งในรูปแบบของการทำ source NAT แบบพิเศษ ซึ่งใช้สำหรับ
กรณีที่ได้รับไอพีแบบไม่ซ้ำ(dynamically-assigned IP address) เช่น ผู้ที่ใช้โมเด็มล็อกอินเข้าไปยัง isp
(สำหรับผู้ที่ใช้ static IP address ให้ใช้ SNAT ดังตัวอย่างด้านบน) ซึ่งข้อดีของการทำ masquerading
คือไม่จำเป็นต้องระบุค่าของไอพีแอดเดรสที่จะใช้ในการเปลี่ยน เช่น

##ทำ masquerade สำหรับทุก packet ที่วิ่งผ่าน ppp0
# iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

* DNAT
การทำ Destination NAT จะทำภายใต้ PREROUTING chain หลักๆ คือการเปลี่ยนค่า destination address
หรือ port ก่อนที่จะส่ง packet ไปยัง routing decision

โดยปกติการใช้งานจะระบุ -j DNAT และใช้ --to-destination หรือ --to และยังสามารถใช้ -i (incoming
interface) ร่วมด้วยได้ เช่น
##เปลี่ยน destination address เป็น 192.168.1.20
# iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 192.168.1.20

##เปลี่ยน destination address เป็น 192.168.1.20, 192.168.1.21 หรือ 192.168.1.22
# iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 192.168.1.20-192.168.1.22

##เปลี่ยน destination address ของ web traffic เป็น 192.168.1.50 port 8080
# iptables -t nat -A PREROUING -p tcp --dport 80 -ieth0 -j DNAT --to 192.168.1.50:80

Redirection
การทำ redirect นั้นเป็นหนึ่งในรูปแบบของการทำ Destination NAT แบบพิเศษ เช่น
##เปลี่ยน web traffic ธรรมดาให้ผ่านไปยัง squid proxy (transparent)
# ip tables -t nat -A PREROUTING -p tcp -i eth0 --dport 80 -j REDIRECT --to-port 3128
##หมายเหตุ จำเป็นต้องมีการ set ที่ squid เพิ่มเติมด้วย

นอกจากนี้ยังสามารถสร้าง rule ที่ใช้ค่า address ร่วมกันได้ เช่น เปลี่ยน source address ของเครื่อง
192.168.1.1 และ 192.168.1.2 ไปเป็น 1.2.3.4 ร่วมกันได้ เพราะ NAT
ฉลาดเพียงพอที่จะแยกแยะและป้องกันการชนกัน และมีตัวอย่างการใช้งานแบบอื่นอีกเช่น

# iptables -t nat -A POSTROUTING -s 192.168.0/24 -o eth1 -j SNAT --to 1.2.3.0/24
# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j SNAT --to 1.2.3.0-1.2.3.4 --to
1.2.3.6-1.2.3.254

และสำหรับการทำ source NAT ต้องมั่นใจว่า เมื่อ packet เดินทางไปถึงเป้าหมายแล้ว packet นั้นๆ
สามารถเดินทางกลับมาหาเครื่องที่เป็นเจ้าของ packet นั้นได้จริงๆ เช่น หากทำ SNAT จาก private IP เป็น
1.2.3.4 เมื่อ packet ถูกส่งไปถึงปลายทาง และปลายทางต้องการส่งกลับมา packet นั้นจะต้องเดินทางมาถึง
linux box ที่รัน iptables นี้เท่านั้น การสื่อสารจึงจะสมบูรณ์ ซึ่งสามารถทำได้ด้วยวิธีต่างๆ ดังนี้คือ

1. กรณีที่ทำ SNAT โดยใช้ไอพีแอดเดรสของเครื่อง linux box นั้นเอง และเป็นเครื่องที่ทำ routing
เรียบร้อยแล้ว และทุกอย่างสามารถสื่อสารกันได้ ก็ไม่ต้องทำอะไร
2. ถ้าทำ SNAT โดยใช้ไอพีแอดเดรสที่ไม่ใช่ไอพีของ linux box นั้นๆ แต่เป็นไอพีแอดเดรสที่อยู่ใน
network เดียวกันและไม่มีเครื่องใดใช้ เครื่อง linux box นั้นจะต้องสามารถทำ ARP reply ตอบกลับ ARP
request ที่ถูกส่งออกมาเพื่อค้นหา MAC address สำหรับไอพีแอดเดรสที่ทำ SNAT นั้นได้
ซึ่งสามารถแก้ไขได้โดยการสร้าง IP alia ดังคำสั่งด้านล่าง

#ip address add 1.2.3.99 dev eth0
หรืออาจจะเพิ่ม arp record ที่เครื่องที่ทำหน้าที่เป็น gateway ของ linux box

3. หรือถ้าทำ SNAT โดยใช้ไอพีแอดเดรสที่แตกต่างกันออกไปจาก Linux box โดยสิ้นเชิง (เช่น
ไอพีแอดเดรสคนละ network) ก็จำเป็นต้องมีการแก้ไข routing table ที่เครื่องที่ทำหน้าที่เป็น gateway ของ
Linux box ที่รัน iptables อยู่

มีเอกสารอธิบายเกี่ยวกับความปลอดภัยในการใช้ NAT ที่http://thaicert.nectec.or.th/paper/firewall/nat.phpซึ่งหลังจากอ่านแล้วจะช่วยให้มองเห็นภาพได้ชัดเจนว่า ต้องทำ NAT หรือไม่อย่างไร

ตัวอย่างที่ 1 การใช้งาน iptables แบบง่าย
ในการใช้งาน iptables ในระบบที่ใช้งานจริงนั้น โดยส่วนใหญ่จะเขียนเป็น script ขึ้นมา
ซึ่งมีความซับซ้อนพอสมควร ซึ่งก่อนจะไปถึงขั้นนั้นมีวิธีทดลองใช้คำสั่งง่ายๆ
เพื่อทำความเข้าใจหลักการทำงานเบื้องต้นของ iptables

เช่น ต้องการ drop packet ที่เป็น ICMP ทั้งหมด ที่ออกจากเครื่องนี้ ซึ่งในกรณีนี้ไม่จำเป็นต้องระบุ
source ip address เพราะทุก packet ที่ออกจากเครื่องนี้จะต้องผ่านการตรวจสอบจาก OUTPUT chain

เริ่มการทดสอบโดยทดลอง ping ไปยัง www.nectec.or.th ซึ่งจะได้ผลลัพธ์คล้ายด้านล่างนี้

# ping -c 1 www.nectec.or.th
PING www.nectec.or.th (202.44.204.33) from 203.154.207.22 : 56(84) bytes of data.
64 bytes from www.nectec.or.th (202.44.204.33): icmp_seq=0 ttl=61 time=3.710 msec

--- www.nectec.or.th ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/mdev = 3.710/3.710/3.710/0.000 ms

ซึ่ง option -c 1 ระบุให้ส่ง packet ออกไปแค่ packet เดียว จากนั้นให้ใช้คำสั่งดังด้านล่างนี้ เพื่อ
block ICMP packet ที่จะออกจากเครื่องนี้

# iptables -A OUTPUT -p icmp -j DROP

จากนั้นทดลอง ping อีกครั้ง เพื่อตรวจสอบผลลัพธ์ที่ได้

# ping -c 1 www.nectec.or.th
PING www.nectec.or.th (202.44.204.33) from 203.154.207.22 : 56(84) bytes of data.
ping: sendto: Operation not permitted

--- www.nectec.or.th ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

ซึ่งผลลัพธ์แสดงให้เห็นว่า ICMP echo-request packet ดังกล่าวไม่ได้รับอนุญาตให้ผ่าน OUTPUT chain ไปได้
และถ้าหากต้องการลบ rule ดังกล่าวก็สามารถทำได้หลายวิธีคือ

# iptables -F OUTPUT (ซึ่งจะลบทุก rule ใน chain OUTPUT นี้)
# iptables -D OUTPUT 1 (เนื่องจากรู้ว่ามี rule เดียวจึงสามารถสั่งลบ rule ลำดับที่ 1 ได้เลย)
หรือ
# iptables -D OUTPUT -p icmp -j DROP (ลบ rule โดยการระบุออปชันของ rule ที่ต้องการได้โดยตรง)

ตัวอย่างที่ 2 การทำ Masquerading
ตัวอย่างการใช้งาน masquerading ที่เห็นได้ชัดเจนในประเทศไทยก็คือ ร้านอินเทอร์เน็ตคาเฟ่(แบบประหยัด)
ซึ่งนิยมใช้โมเด็ม 1 ตัว connect ไปยัง ISP แล้วใช้ NAT เพื่อให้เครื่องลูกที่มีอยู่หลายๆ เครื่องนั้น
สามารถใช้งานอินเทอร์เน็ตผ่านโมเด็มตัวนั้นได้ โดยเครื่องลูกจะต้องใช้ private ip ซึ่งมีให้เลือกหลาย
class เช่น 10.x.x.x 192.168.x.x เป็นต้น จากนั้นก็ตั้ง gateway ให้ชี้ไปยัง linux box
ที่ทำหน้าที่หมุนโมเด็มและรัน iptables ซึ่งสามารถใช้คำสั่งง่ายๆ
ดังด้านล่างก็ทำให้เครื่องลูกสามารถใช้งานอินเทอร์เน็ตได้แล้ว คือ

# iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
# echo "1" > /proc/sys/net/ip_forward

ตัวอย่างที่ 3 การเขียน rule จากเครือข่ายตัวอย่าง rc.firewall (ตัวอย่างเครือข่ายและ script
นี้คัดลอกมาจากเว็บไซต์ www.boingworld.com)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #5 on: มกราคม 23, 2011, 12:49:42 AM »
พอดีมี โจทย์มาให้ก็คือ ทำเป็น nat server ครับโดยที่

10.x.x.6 forward packet ทุกport ไปที่ 192.x.x.6
10.x.x.7 forward packet ทุกport ไปที่ 192.x.x.7

แล้วตัว load balance ก็ให้ส่ง packet เข้า
10.x.x.52 ส่ง load ไปที่ 192.x.x.52

เราก็ set ได้ดังนี้
เริ่มต้น ต้องสร้าง ip aliase ไว้ก่อนครับ ผมเลือกใช้เป็น debian เพราะเป็น linux ซึ่งมันใช้งานพวก
network ดีมากๆ อยู่แล้ว
#ifconfig eth0:0 10.x.x.6 netmask x.x.x.x up
#ifconfig eth0:0 10.x.x.7 netmask x.x.x.x up

เสร็จแล้วก็ทำ การเขียน rule ของ nat โดยใช้ iptables ครับ
##ขาเข้ามา
#iptables -t nat -A PREROUTING -j DNAT -i eth0 -d 10.x.x.6 --to 192.x.x.6
#iptables -t nat -A PREROUTING -j DNAT -i eth0 -d 10.x.x.7 --to 192.x.x.7

##ขาออกครับ
#iptables -t nat -A POSTROUTING -j SNAT -o eth0 -s 192.x.x.6 --to 10.x.x.6
#iptables -t nat -A POSTROUTING -j SNAT -o eth0 -s 192.x.x.7 --to 10.x.x.7

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #6 on: มกราคม 23, 2011, 12:49:51 AM »
set iptalbes block ssh

iptables -N SSH_CHECK
iptables -A INPUT -p tcp -dport 22 -m state -state NEW -j SSH_CHECK
iptables -A SSH_CHECK -m state -state NEW -m recent -set -name SSH
iptables -A SSH_CHECK -m state -state NEW -m recent -update -seconds 60 -hitcount 4 -name SSH
iptables -A SSH_CHECK -m state -state NEW -m recent -rcheck -seconds 60 -hitcount 4 -name SSH -j
DROP

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #7 on: มกราคม 23, 2011, 12:50:02 AM »
#-----------------------------IPTABLES-ZONE------------------------------
#Flush
iptables -F
iptables -X

#NAT
iptables -t nat -A POSTROUTING -d ! 192.168.1.0/24 -j MASQUERADE
iptables -A FORWARD -s 192.168.1.0/24 -j ACCEPT
iptables -A FORWARD -d 192.168.1.0/24 -j ACCEPT

#DROP
iptables -A FORWARD -j DROP

#Transparent Proxy
iptables -t nat -A PREROUTING -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-port 8080

#Firewall
iptables -A INPUT -s 192.168.1.0/24 -d 0/0 -p tcp --dport 8080 --syn -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -d 0/0 -p tcp --dport 139 --syn -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -d 0/0 -p tcp --dport 21 --syn -j ACCEPT

#------EAK
iptables -A INPUT -p ALL -s! 192.168.1.0/24 --dport 834 -j DROP
iptables -A INPUT -p ALL -s! 192.168.1.0/24 --dport 835 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 111 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 111 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 515 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 515 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2049 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2049 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 6000:6009 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 7100 -j DROP
iptables -A INPUT -p tcp --sport telnet -j DROP
iptables -A INPUT -p udp --sport telnet -j DROP
iptables -A FORWARD -p tcp --sport 137:139 -j DROP
iptables -A FORWARD -p udp --sport 137:139 -j DROP

#Blaster Worm
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 135 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 135 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 69 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 445 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 445 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 4444 -j DROP

#Sobig Worm
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 990:999 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 8998 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 123 -j DROP

#----------- virus
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2018:2021 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2018:2021 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 36794 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 3127:3198 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 137:138 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 593 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 6777 -j DROP#udp
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2535 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 80 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 81 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 37 -j DROP#udp
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 1434 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 6789 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 5554 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 9996 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2745 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 8866 -j DROP
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2556 -j DROP

#---------msn
iptables -A INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 1863 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 44802 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 9943 -j DROP
iptables -A INPUT -p udp -s 0.0.0.0/0 -d 0.0.0.0/0 --dport 2268 -j DROP

#-----Block Ip Spoofing and any outside packets (protect localhost)
iptables -A INPUT -p ALL -i eth1 -j LOG --log-level DEBUG
iptables -A INPUT -j DROP \! -i eth1 -s 192.168.1.0/24
iptables -A FORWARD -j DROP \! -i eth1 -s 192.168.1.0/24
iptables -A INPUT -j DROP -i \! lo -s 127.0.0.0/255.0.0.0
iptables -A FORWARD -j DROP -i \! lo -s 127.0.0.0/255.0.0.0

#------Prevent outside create connection
iptables -A INPUT -m state --state NEW -i eth0 -j DROP
iptables -A FORWARD -m state --state NEW -i eth0 -j DROP

#---------- irc
iptables -A FORWARD -p tcp -s 192.168.1.0/24 -j ACCEPT
iptables -A FORWARD -p tcp --dport 6660:6669 -j DROP
iptables -A FORWARD -p tcp --dport 7000 -j DROP

#iptables -A FORWARD -p tcp -s 192.168.1.0/24 -i eth0 -j DROP
#iptables -A FORWARD -p udp -s 192.168.1.0/24 -i eth0 -j DROP
#iptables -A FORWARD -p tcp -s 192.168.100/24 -i eth0 -j DROP
#iptables -A FORWARD -p udp -s 192.168.100/24 -i eth0 -j DROP
#---- qq
iptables -I INPUT -p udp --dport 4000 -j DROP #qq
iptables -I FORWARD -p udp -s 0/0 -d 0/0 --dport 4000 -j DROP # qq
iptables -I INPUT -s 203.195.110.243 -j DROP
iptables -I FORWARD -d 203.195.110.243 -j DROP


#-------------- backdoor ----------
iptables -A OUTPUT -o eth0 -p tcp --dport 31337 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 31335 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 27444 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 27665 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 20034 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 9704 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 1433 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 2049 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 2432 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 5999 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 6068 -j DROP
iptables -A OUTPUT -o eth0 -p tcp --sport 6900 -j DROP

#Block Ping
iptables -A INPUT -p icmp -s 0.0.0.0/0 -d 0.0.0.0/0 -j DROP

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #8 on: มกราคม 23, 2011, 12:50:19 AM »
Iptables command

Iptables คืออะไร???

Iptables คือเครื่องมือของผู้ดูแลระบบที่ช่วยในการ packet filteringและการทำNAT ครับ
ซึ่งตัวiptablesไ้รับการยอมรับว่ามีประสิทธิภาพที่สูงมาก ถูกใช้งานในการตรวจสอบตารางของ ip packet
filter rule ใน Linux kernel

การใช้งานนั้นสามารถป้อนคำสั่งได้โดยตรงผ่านทาง command line หรือจะเซฟเป็น firewall script ใน dd-wrt
administration panel ก็ได้ แต่ที่เรารู้ๆกันอยู่ว่าคำสั่งของ iptables นี้มีค่อนข้างมาก
วันนี้ผมเลยรวบรวมเอามาฝากกันครับ

เริ่มที่การใช้งานทั่วๆไปครับ

iptables -[AD] chain rule-specification [options]
iptables -[RI] chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LFZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)

command ทั่วๆไป

–append -A chain Append to chain
–delete -D chain Delete matching rule from chain
–delete -D chain rulenum
Delete rule rulenum (1 = first) from chain
–insert -I chain [rulenum]
Insert in chain as rulenum (default 1=first)
–replace -R chain rulenum
Replace rule rulenum (1 = first) in chain
–list -L [chain] List the rules in a chain or all chains
–flush -F [chain] Delete all rules in chain or all chains
–zero -Z [chain] Zero counters in chain or all chains
–new -N chain Create a new user-defined chain
–delete-chain
-X [chain] Delete a user-defined chain
–policy -P chain target
Change policy on chain to target
–rename-chain
-E old-chain new-chain
Change chain name, (moving any references)

Options

–proto -p [!] proto
protocol: by number or name, eg. `tcp’
–source -s [!] address[/mask]
source specification
–destination -d [!] address[/mask]
destination specification
–sport [!] port[:endport]
source port (use `:’ when specifying range)
–dport [!] port[:endport]
destination port
–in-interface -i [!] input name

network interface name (
  • for wildcard)

–jump -j target
target for rule (may load target extension)
–match -m match
extended match (may load extension)
–state state
connection states to match:
INVALID NEW ESTABLISHED RELATED
–tcp-flags [!] mask
match when the TCP flags are as specified:
SYN ACK FIN RST URG PSH ALL NONE
–numeric -n numeric output of addresses and ports
–out-interface -o [!] output name

network interface name (
  • for wildcard)

–table -t table table to manipulate (default: `filter’)
–verbose -v verbose mode
–line-numbers print line numbers when listing
–exact -x expand numbers (display exact values)
–fragment -f match second or further fragments only
–modprobe= try to insert modules using this command
–set-counters PKTS BYTES set the counter during insert/append
–version -V print package version

MAC v1.3.7 options:
–mac-source [!] XX:XX:XX:XX:XX:XX
Match source MAC address

ทีนี้เรามาลองใช้งานจริงๆกันบ้าง อันแรกเลยคือ ทำ Port forwarding ไปที่ IP ที่เรากำหนด จะได้ command
ดังนี้ครับ

iptables -t nat -I PREROUTING -p tcp -d $(nvram get wan_ipaddr) –dport 443 -j DNAT –to
192.168.1.2:443
iptables -I FORWARD -p tcp -d 192.168.1.2 –dport 443 -j ACCEPT

Block IP ที่กำหนดไม่ให้เราเข้าไป access

iptables -I FORWARD -d 123.123.123.123 -j DROP

อันนี้คือห้ามไม่ให้เครื่องของเราทำการติดต่อกับ IP ที่กำหนดหากมีการพยายามแหกกฎก็ให้ทำการเก็บ log
ไว้ด้วย

iptables -I OUTPUT -d 239.255.255.250 -j logdrop

ทำการ block SMTP trafiic ขงทุกเครื่องยกเว้นเฉพาะเครื่องที่เรากำหนดเท่านั้น

/usr/sbin/iptables -I FORWARD 1 -p tcp -d safe.server1.com –dport 25 -j logaccept
/usr/sbin/iptables -I FORWARD 2 -p tcp -d safe.server2.com –dport 25 -j logaccept
/usr/sbin/iptables -I FORWARD 3 -p tcp –dport 25 -j logdrop

ิblock SMTP ฝั่งขาออก

iptables -I FORWARD 1 -p tcp -s 192.168.1.2 –dport 25 -j ACCEPT
iptables -I FORWARD 2 -p tcp -s 192.168.1.1/24 –dport 25 -j REJECT

อนุญาตเฉพาะโดเมนที่กำหนดเท่านั้นที่ให้ติดต่อทาง HTTP ได้

iptables -I FORWARD 1 -p tcp -d dd-wrt.com –dport 80 -j ACCEPT
iptables -I FORWARD 2 -p tcp –dport 80 -j DROP

ฺBlock ทุกๆ traffic ยกเว้น http, https และ ftp

iptables -I FORWARD 1 -p tcp -m multiport –dport 21,80,443 -j ACCEPT
iptables -I FORWARD 2 -m state –state ESTABLISHED,RELATED -j ACCEPT
iptables -I FORWARD 3 -j DROP

จำกัดการเข้าใช้งานโดย MAC Address

insmod ipt_mac
iptables -I INPUT -p tcp –dport 80 -m mac ! –mac-source 00:12:34:56:78:9A -j REJECT –reject-with
tcp-reset

เปลี่ยนค่า Time To Live (TTL)

iptables -t mangle -I POSTROUTING -o vlan1 -j TTL –ttl-set 128

ขอบคุณเว็บhttp://www.easyzonecorp.net

harubiji

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: set iptables by golfreeze
« Reply #9 on: กุมภาพันธ์ 21, 2011, 06:14:07 PM »
ชอบเลยครับ หัวข้อนี้ กำลังหาศึกษาอยู่พอดี  ;)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #10 on: กุมภาพันธ์ 22, 2011, 02:19:07 PM »
ได้ครับพี่พี ลองอ่านดูได้เลยครับผม แต่ที่สำคัญลองไปใช้งานจริงดูนะครับ จะได้เข้าใจยิ่งขึ้น  ;)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set iptables by golfreeze
« Reply #11 on: พฤศจิกายน 10, 2011, 11:06:34 AM »
####การเดินทางของ Packet ผ่าน Server จะแบ่งได้เป็น 3 กรณี คือ

1. ข้อมูลวิ่งมายังเครื่อง Server
กรณีนี้คือ เครื่อง Server เป็นผู้ให้บริการ เช่น ftp, web, mail, dns หรือ proxy เป็นต้น
แล้วเครื่องลูกข่าย หรือเครื่องจากภายนอก วิ่งเข้ามายังเครื่อง Server

การเดินทางของ Packet จะเป็นดังนี้ คือ

จาก Interface ของ Server เช่น eth0, ppp0 หรืออื่น ๆ ที่ข้อมูลวิ่งเข้ามา จะวิ่งเข้ามายัง
Chain PREROUTING  --> Conntrack , mangle , nat ตามลำดับ  จากนั้นก็จะวิ่งไปยัง
Routing Process
Chain INPUT --> Filter , Conntrack , mangle ตามลำดับ จากนั้นก็จะวิ่งไปยัง
Local Process

สรุปง่าย ๆ คือ  Interface -> PREROUTING --> Routing Process --> INPUT --> Local Process

2. ข้อมูลวิ่งออกจากเครื่อง Server
กรณีนี้คือ เมื่อเครื่อง Server ได้รับการติดต่อมาจากภายนอก แล้วทำการตอบกลับ ก็จะมีการส่งข้อมูลออก
เช่น การปิงมายัง Server , การส่งข้อมูลไปยังเครื่องลูกข่าย

การเดินทางของ Packet จะเป็นดังนี้

Local Process
Chain OUTPUT --> Conntrack , mangle , nat , filter ตามลำดับ จากนั้นจะวิ่งไปยัง
Routing Process
Chain POSTROUTING --> mangle , nat , Conntrack ตามลำดับ จากนั้นก็จะวิ่งไปยัง
สู่ Interface ของ Server เช่น ppp0, eth0 เพื่อส่งข้อมูลออกไป

สรุป ง่าย ๆ คือ Local Process --> OUTPUT --> Routing Process --> POSTROUTING --> Interface

3. ข้อมูลวิ่งผ่านเครื่อง Server
ในกรณีที่เครื่องของเราทำหน้าที่เป็น Gateway ก็จะมีข้อมูลวิ่งจาก Interface นึงไปยังอีก Interface นึง
ไม่ว่าจะเป็นจาก WAN --> LAN  หรือ จาก LAN --> WAN ก็จะมีลักษณะเหมือนกัน

ซึ่งจะแทนด้วย Interface A ไปยัง Interface B การเดินทางของ Packet จะเป็นดังนี้

เริ่มต้นจากเข้ามาสู่ Interface A
Chain PREROUTING --> Conntrack , mangle , nat ตามลำดับ จากนั้นจะวิ่งไปยัง
Routing Process
Chain FORWARD --> mangle , filter ตามลำดับ จากนั้นจะวิ่งไปยัง
Chain POSTROUTING --> mangle , nat , Conntrack ตามลำดับ จากนั้นจะวิ่งไปยัง
วิ่งออกทาง Interface B

สรุป ง่าย ๆ คือ Interface A --> PREROUTING --> Routing Process --> FORWARD --> POSTROUTING --> Interface B
« Last Edit: พฤศจิกายน 10, 2011, 11:25:04 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
สำหรับวิธีการการบล็อกพอร์ทนั้น จะมีวิธีที่นิยมอยู่  2  วิธีการคือ

1.เปิดทั้งหมดแล้ว ทำการบล็อกพอร์ทที่จะปิด

ตัวอย่าง

$IPTABLES -P FORWARD ACCEPT                                              #OPEN ALL
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT

$IPTABLES -A FORWARD -p tcp --dport 1863 -j DROP                     #BLOCK MSN
$IPTABLES -A FORWARD -p udp --dport 1863 -j DROP                    #BLOCK MSN
$IPTABLES -A FORWARD -p tcp --dport 4000 -j DROP                     #BLOCK qq
$IPTABLES -A FORWARD -p tcp --dport 5050 -j DROP                     #BLOCK YAHOO CHAT
$IPTABLES -A FORWARD -p tcp --dport 2778  -j DROP                     #BLOCK  CamFrox
 

2. ปิดทั้งหมดแล้วเปิดพอร์ทที่ใช้เท่านั้น    (วิธีการนี้จะมีความปลอดภัยมากกว่าคือ ต้องการเปิดปิดไหนก็เปิดเอาเองได้เลยครับ แต่ถ้าเราเปิดไม่ครบอาจมีปัญหาได้ครับ)

ตัวอย่าง

 

$IPTABLES -P FORWARD DROP                                                                           #CLOSE PORT ALL
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT

$IPTABLES -A FORWARD -p tcp --dport 53 -j ACCEPT   #DNS
$IPTABLES -A FORWARD -p udp --dport 53 -j ACCEPT  #DNS
$IPTABLES -A FORWARD -p tcp --dport 80 -j ACCEPT  #HTTP
$IPTABLES -A FORWARD -p tcp --dport 8080 -j ACCEPT  #HTTP
$IPTABLES -A FORWARD -p tcp --dport 443 -j ACCEPT  #HTTPS
$IPTABLES -A FORWARD -p tcp --dport 8443 -j ACCEPT  #HTTPS
$IPTABLES -A FORWARD -p tcp --dport 20 -j ACCEPT  #FTP
$IPTABLES -A FORWARD -p udp --dport 20 -j ACCEPT  #FTP
$IPTABLES -A FORWARD -p tcp --dport 21 -j ACCEPT  #FTP
$IPTABLES -A FORWARD -p udp --dport 21 -j ACCEPT  #FTP
$IPTABLES -A FORWARD -p tcp --dport 22 -j ACCEPT  #SSH
$IPTABLES -A FORWARD -p tcp --dport 23 -j ACCEPT  #TELNET
$IPTABLES -A FORWARD -p tcp --dport 25 -j ACCEPT  #MAIL
$IPTABLES -A FORWARD -p tcp --dport 110 -j ACCEPT  #MAIL
$IPTABLES -A FORWARD -p tcp --dport 1863 -j ACCEPT #MSN
$IPTABLES -A FORWARD -p udp --dport 1863 -j ACCEPT #MSN
$IPTABLES -A FORWARD -p tcp --dport 4000 -j ACCEPT  #qq
$IPTABLES -A FORWARD -p tcp --dport 554 -j ACCEPT  #RTSP
$IPTABLES -A FORWARD -p udp --dport 554 -j ACCEPT  #RTSP
$IPTABLES -A FORWARD -p tcp --dport 5050 -j ACCEPT  #YAHOO CHAT
$IPTABLES -A FORWARD -p tcp --dport 2778  -j ACCEPT  #CamFrox

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Match

การตั้งเงื่อนไขของการ match นั้นจะต้องอาศัยความเข้าใจในเรื่อง IP, TCP, UDP, และ ICMP มาบ้างพอสมควร
จึงจะสามารถตั้งเงื่อนไขที่เหมาะสมและตรงตามความต้องการได้ ซึ่งมีรายละเอียดดังนี้
การระบุไอพีแอดเดสของต้นทางและปลายทาง

สามารถ ระบุไอพีแอดเดรสต้นทาง (source ip address) ของแพ็กเก็ต โดยใช้ -s หรือ --source หรือ --src
และสำหรับไอพีแอดเดรสปลายทาง (destination ip address) ก็ใช้ -d หรือ --destination หรือ –dst

การทำ Inversion

ในบาง กรณีนั้นหากต้องการระบุเป็นอินเวิร์ส (inverse) เช่น อนุญาตให้ทุกไอพียกเว้นไอพีที่ระบุไว้
ซึ่งการใช้คำสั่งดังกล่าวสามารถทำได้โดยใช้เครื่องหมาย ! นำหน้า argument ที่ต้องการ
(เครื่องหมาย ! หมายถึง NOT) เช่น -p ! TCP ซึ่งจะตรง กับโพรโตคอลทุกๆ ตัวที่ไม่ใช่ทีซีพี
หรือ -s ! localhost ซึ่งหมายถึงแพ็กเก็ต ที่มีไอพีแอดเดรสต้นทาง อื่นๆ ยกเว้น localhost (127.0.0.1)

การระบุโพรโตคอล

สามารถระบุโพรโตคอลที่ต้องการได้ดัง นี้คือ ทีซีพี (TCP), ยูดีพี (UDP), ไอซีเอ็มพี (ICMP)
หรือสามารถใช้ตัวเลขแทนได้ (สำหรับ *NIX อ้างอิงได้จาก /etc/protocols)
และยังสามารถใช้ได้ทั้งตัวอักษรเล็กหรือใหญ่ (ใช้ได้ทั้ง tcp และ TCP) เช่น -p TCP หรือ -p ! tcp

การระบุ interface


-i หรือ --in-interface ตามด้วยชื่ออินเตอร์เฟส ( interface) ใช้เพื่อระบุ incoming interface
 ซึ่งหมายถึงว่า แพ็กเก็ต ที่จะตรงกับกฎ นี้ต้องเข้ามาจากอินเตอร์เฟส ที่กำหนด เช่น -i eth0 หมายความว่า ทุกแพ็กเก็ต
ที่เข้ามาทาง eth0 จะ ตรงกับกฎ นี้ ทั้งนี้ชื่ออินเตอร์เฟส ที่สามารถใช้ได้นั้น สามารถตรวจสอบได้โดยใช้คำสั่ง ifconfig
และ -o หรือ --out-interface ตามด้วยชื่อของอินเตอร์เฟส ใช้เพื่อระบุ outgoing interface ซึ่งหมายถึงว่า แพ็กเก็ต
ที่จะ ตรงกับกฎ นี้ กำลังจะเดินทางผ่านอินเตอร์เฟส ที่ระบุไว้ เช่น -o eth1 หรือ -o ! eth1

fragment packet

ในการส่งข้อมูลใน ip network นั้นเป็นเรื่องปกติที่จะเกิดการแฟรกเมนต์ (fragment)
ของแพ็กเก็ตเนื่องจากขนาดของแพ็กเก็ต มีขนาดใหญ่เกินไปที่จะส่งไปในครั้งเดียว
จำเป็นต้องมีการแบ่งแพ็กเก็ต ออกเป็นหลายๆ ชิ้นทยอยส่งไป ซึ่งเรียกกันว่าการทำแฟรกเมนต(fragment)
โดยเครื่องปลายทางจะทำหน้าที่ประกอบ แฟรกเมนต์แพ็กเก็ต(fragment packet)รวมกันเป็นแพ็กเก็ต ที่สมบูรณ์ดังเดิม
ข้อมูลที่เป็นแฟรกเมนต์แพ็กเก็ต นั้นจะมีเฮดเดอร์ (header)ที่สมบูรณ์แค่แพ็กเก็ต แรกเท่านั้น ส่วนแพ็กเก็ต
ที่ตามมาจะมีแค่เฮดเดอร์ บางส่วนคือ ไอพีแอดเดรสเท่านั้น ไม่มีข้อมูลของโพรโตคอลแนบมาด้วย
ดังนั้นการตรวจสอบข้อมูลเฮดเดอร์ ของ ทีซีพี ยูดีพี ไอซีเอ็มพี จึงไม่สามารถทำได้ในแพ็กเก็ต ที่สองเป็นต้นมา

หากใช้แนต (NAT)แพ็กเก็ตประเภทแฟรกเมนต์แพ็กเก็ต จะถูกประกอบเข้าด้วยกันจนสมบูรณ์ก่อนที่ แพ็กเก็ต จะเข้าไปถึง
 แพ็กเก็ตฟิลเตอร์ริ่ง (packet filtering) ดังนั้นจึงไม่มีความจำเป็นที่จะต้องกังวลเกี่ยวกับ แฟรก-เมนต์แพ็กเก็ต
ในกรณีที่ไม่ได้ใช้แนต (NAT) แพ็กเก็ตประเภทแฟรกเมนต์แพ็กเก็ตก็จะไม่ถูกประกอบเข้าด้วยกัน ไอพีเทเบิล
จะมีกระบวนการในการทำงานกับ แฟรกเมนต์แพ็กเก็ตดังนี้ หลังจากที่แฟรกเมนต์แพ็กเก็ตแรกผ่านเข้ามาแล้วไอพีเทเบิล
สามารถตรวจสอบได้ว่าจะอนุญาตให้ผ่านหรือไม่ ในขณะที่แฟรกเมนต์แพ็กเก็ต ที่สองและหลังจากนั้นที่ตามมานั้น
จะไม่สามารถนำไปตรวจสอบกับกฎ ใดๆ เลย เช่น -p TCP --sport www หรือแม้แต่ -p TCP --sport ! www
แต่ยังสามารถเขียนกฎ ให้ตรวจสอบทั้งแฟรกเมนต์แพ็กเก็ต ตัวที่สองและหลังจากนั้นที่ตามมาได้ด้วยการใช้ -f หรือ --fragment
ทั้งนี้อาจจะเขียนในทางตรงข้ามคือไม่ต้องตรวจสอบแฟรกเมนต์แพ็กเก็ต ที่สองและหลังจากนั้นโดยใช้ ! -f ก็ได้

โดยปกติแล้วมักจะปล่อยให้แฟรกเมนต์แพ็กเก็ต ผ่านไป เนื่องจากถ้าสามารถทิ้งแฟรกเมนต์แพ็กเก็ตแรกได้แล้ว
แพ็กเก็ตทั้งหมดก็จะไม่สามารถถูกประกอบที่เครื่องปลายทางได้ แต่ทั้งนี้แฟรกเมนต์แพ็กเก็ต
ที่ถูกปล่อยไปดังกล่าวอาจจะทำให้เครื่องที่ได้รับนั้นเกิดการชะงัก หรือเกิดความเสียหายได้
หรืออาจจะเกิดการโจมตีแบบดีไนอัลออฟเซอร์วิส (Denial of Service) โดยใช้แฟรกเมนต์แพ็กเก็ตได้

การระบุ target
user-defined chain


เนื่อง จากไอพีเทเบิล อนุญาตให้ผู้ใช้สามารถสร้างเชน (chain) ขึ้นมาได้ใหม่นอกเหนือจากเชนที่มีมาอยู่แล้ว
(built-in chain) ทั้งสามตัว (INPUT, OUTPUT, FORWARD) ทั้งนี้จะต้องใช้ตัวอักษรตัวเล็กทั้งหมดสำหรับเชน
ที่ผู้ใช้สร้างขึ้นเอง เมื่อแพ็กเก็ตตรงกับกฎ ที่เป็นเชนที่ผู้ใช้กำหนดขึ้น (user-defined chain)
แพ็กเก็ตนั้นจะถูกนำไปตรวจสอบใหม่โดยเชนที่ผู้ใช้กำหนด นั้นๆ และถ้าในเชนนั้นๆ ไม่มีการตัดสินใจใดๆ
แพ็กเก็ตนั้นก็สามารถย้อนกลับมายังกฎ ถัดไปในเชนที่เริ่มต้นเดินทางได้

new target

เป็น target ที่สร้างเพิ่มเติมขึ้นมาคือ

LOG เป็น โมดูลที่มีความสามารถในการเก็บข้อมูลลงล็อก (มี syslog facility เป็น kernel)
สำหรับแพ็กเก็ตที่ตรงกับกฎที่ระบุปลายทาง เป็น LOG มีออปชันให้เลือกใช้งานดังนี้คือ--log-level
เป็นการระบุ priority level ของ log ซึ่งกำหนดได้ตั้งแต่ debug, info ,notice, warning, crit, alert, emerg--log-prefix
ตามด้วยชุดของตัวอักษรยาวไม่เกิน 29 ตัว ซึ่งชุดของตัวอักษรดังกล่าวจะปรากฏอยู่บนล็อกไฟล์
ความสำคัญของ 3 ตารางหลัก ในคำสั่งไอพีเทเบิล


1.Filter Table
เป็น ตารางที่ใช้งานมากที่สุด เป็นจุดที่ใช้ในการตรวจสอบและควบคุมการผ่านเข้าออกของแพ็กเก็ต
ถ้าหากจะพิจารณาการไหลเวียนของแพ็กเก็ต เฉพาะในส่วนของฟิลเตอร์เทเบิล (filter table) โดยไม่สนใจเทเบิล อื่นๆ นั้น
 ก็พอจะแสดงให้เห็นได้ดังรูปที่ 2.20 โดยเมื่อแพ็กเก็ต เข้ามาในระบบ มันจะเข้าไปยัง routing decision เพื่อตัดสินใจว่า แพ็กเก็ต จะถูกส่งไปที่ใด

2.Mangle Table เป็นตารางที่ใช้สำหรับแก้ไข ข้อมูล TOS, TTL, MARK ของแพ็กเก็ต ซึ่งโดยปกติแล้วแทบจะไม่ได้ใช้งาน
และไม่ควรทำ packet filtering หรือกรองแพ็กเก็ต ที่ตารางนี้ รวมทั้งไม่ควรทำ DNAT, SNAT หรือ Masquerading ที่ตารางนี้อย่างเด็ดขาดด้วย

3.Nat Table เป็น ตารางที่ใช้สำหรับทำการแปลงแอดเดรส (network address translation) เช่น
เปลี่ยนค่าไอพีแอดเดรสต้นทางและปลายทาง (source ip address, destination ip address) จุดสำคัญอีกอย่างหนึ่งที่ต้องรู้ก็คือ
 มีเพียงแพ็กเก็ต แรกเท่านั้นที่เข้ามาที่เชน นี้ ส่วนแพ็กเก็ต ถัดไปนั้นจะถูกกระทำเหมือนที่แพ็กเก็ต แรกได้รับ ดังนั้นจึงไม่ควรทำ packet filtering ที่เชน เหล่านี้

การใช้งาน Nat table นั้นก็เพียงแต่ใช้ออปชัน -t nat เท่านั้น และ target
ที่สามารถใช้งานได้คือ SNAT, DNAT, Masquerade, Redirect ซึ่งมีรายละเอียดดังนี้

SNAT

การ ทำ source NAT จะทำที่ POSTROUTING chain โดย หลักๆ คือทำการเปลี่ยนแอดเดรสต้นทาง (source address)
ก่อนที่จะส่งแพ็กเก็ตนั้นออกไป ซึ่งสามารถใช้ออปชัน -o (outgoing interface) ร่วมด้วยได้
นอกจากนี้ยังใช้ -j SNAT และ --to--source หรือ --to เพื่อเปลี่ยนไอพีแอดเดรสหรือ port ไปตามต้องการได้ เช่น

เปลี่ยน source ip address เป็น 1.2.3.4

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4

เปลี่ยน source address เป็น 1.2.3.4, 1.2.3.5 หรือ 1.2.3.6

iptables -t nat -A POSTROUING -o eth0 -j SNAT --to 1.2.3.4-1.2.3.6

เปลี่ยน source address เป็น 1.2.3.4 port 1-1023

iptables -t nat -A POSTROUTING -p tcp -o eth0 -j SNAT --to 1.2.3.4:1-1023

Masquerading

การ ทำ masquerade นั้นเป็นหนึ่งในรูปแบบของการทำ source NAT แบบพิเศษ ซึ่งใช้สำหรับ
กรณีที่ได้รับไอพีแบบไม่ซ้ำ(dynamically-assigned IP address) เช่น ผู้ที่ใช้โมเด็มล็อกอินเข้าไปยัง isp
(สำหรับผู้ที่ใช้ static IP address ให้ใช้ SNAT ดังตัวอย่างด้านบน) ซึ่งข้อดีของการทำ masquerading
คือไม่จำเป็นต้องระบุค่าของไอพีแอดเดรสที่จะใช้ในการเปลี่ยน เช่น

ทำ masquerade สำหรับทุก packet ที่วิ่งผ่าน ppp0

iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

DNAT

การ ทำ Destination NAT จะทำภายใต้ PREROUTING chain หลักๆ คือการเปลี่ยนค่า destination address
หรือ port ก่อนที่จะส่งpacket ไปยัง routing decision โดยปกติการใช้งานจะระบุ -j DNAT
และใช้ --to-destination หรือ --to และยังสามารถใช้ -i(incoming interface) ร่วมด้วยได้ เช่น

เปลี่ยน destination address เป็น 192.168.1.20

iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 192.168.1.20

เปลี่ยน destination address เป็น 192.168.1.20, 192.168.1.21 หรือ 192.168.1.22

iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 192.168.1.20-192.168.1.22

เปลี่ยน destination address ของ web traffic เป็น 192.168.1.50 port 8080

iptables -t nat -A PREROUING -p tcp --dport 80 -ieth0 -j DNAT --to 192.168.1.50:8080


Redirection


การทำ redirect นั้นเป็นหนึ่งในรูปแบบของการทำ Destination NAT แบบพิเศษ เช่น

เปลี่ยน web traffic ธรรมดาให้ผ่านไปยัง squid proxy (transparent)

ip tables -t nat -A PREROUTING -p tcp -i eth0 --dport 80 -j REDIRECT --to-port 3128

ตัวอย่าง การทำ Masquerading

ตัวอย่าง การใช้งาน masquerading ที่เห็นได้ชัดเจนในประเทศไทยก็คือ ร้านอินเทอร์เน็ตคาเฟ่(แบบประหยัด)
ซึ่งนิยมใช้โมเด็ม 1 ตัว ติดต่อ(connect)ไปยัง ไอเอสพี (Internet Service Provider) แล้วใช้ NAT เพื่อให้เครื่องลูกที่มีอยู่หลายๆ
เครื่องนั้น สามารถใช้งานอินเทอร์เน็ตผ่านโมเด็มตัวนั้นได้ โดยเครื่องลูกจะต้องใช้ private ip ซึ่งมีให้เลือกหลายคลาส (class)
เช่น 10.x.x.x 192.168.x.x เป็นต้น จากนั้นก็ตั้งเกตเวย์ให้ชี้ไปยัง linux box ที่ทำหน้าที่หมุนโมเด็มและรันไอพีเทเบิล
ซึ่งสามารถใช้คำสั่งง่ายๆ ดังด้านล่างก็ทำให้เครื่องลูกสามารถใช้งานอินเทอร์เน็ตได้แล้ว คือ

iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
echo "1" > /proc/sys/net/ip_forward



ขอขอบพระคุณข้อมูลจาก http://www.linuxthai.org และ http://golfreeze.packetlove.com/smileboard
« Last Edit: พฤศจิกายน 10, 2011, 11:31:23 AM by golfreeze »