Xgu.ru теперь в Контакте  — приходите и подключайтесь.
Пока мы работаем над следующими видео, вы можете подключиться в Контакте. Познакомимся и обсудим новые страницы и ролики.

Vk-big.pngYoutube-big.jpeg

Пример использования ipfw

Материал из Xgu.ru

Перейти к: навигация, поиск

Не указан параметр (1)


[править] NAT

Дано:

Внутренняя подсеть: 192.168.0.0/24

Интерфейс во внутреннюю подсеть: em1

Внешний адрес: 192.168.1.2

Интерфейс во внешнюю сеть: em0

Внутренний WWW сервер: 192.168.0.2

Необходимо:

Добавить в ядро опции:

# Firewall
options		IPFIREWALL		# Firewall
options		IPFIREWALL_FORWARD
options		IPFIREWALL_VERBOSE
options		IPDIVERT
options		DUMMYNET

# Firewall NAT
options		IPFIREWALL_NAT
options		LIBALIAS

Использовать следующими командами

# ipfw nat 1 config ip 192.168.1.2 redirect_port tcp 192.168.0.2:80 80
# ipfw add nat 1 ip from 192.168.0.0/24 to any out via em0
# ipfw add nat 1 ip from any to 192.168.1.2 in via em0

[править] Загрузочный скрипт

/etc/rc.conf

# Firewall
firewall_enable="YES"
firewall_type="SERVER"
firewall_server_iif="em1"
firewall_server_inet="192.168.0.0/24"
firewall_server_oif="em0"
firewall_server_onet="192.168.1.0/24"

# NAT
firewall_nat_enable="YES"
firewall_nat_interface="em0"
firewall_nat_address="192.168.1.2"

/etc/rc.firewall

#!/bin/sh -

#
# Setup system for ipfw(4) firewall service.
#

# Suck in the configuration variables.
if [ -z "${source_rc_confs_defined}" ]; then
	if [ -r /etc/defaults/rc.conf ]; then
		. /etc/defaults/rc.conf
		source_rc_confs
	elif [ -r /etc/rc.conf ]; then
		. /etc/rc.conf
	fi
fi

############
# Define the firewall type in /etc/rc.conf.  Valid values are:
#   open        - will allow anyone in
#   server      - will try to protect a whole network
#   closed      - totally disables IP services except via lo0 interface
#   UNKNOWN     - disables the loading of firewall rules.
#   filename    - will load the rules in the given filename (full path required)
#
# For ``server the entries below should be customized
# appropriately.

setup_loopback () {
	############
	# Only in rare cases do you want to change these rules
	#
	${fwcmd} add 100 pass all from any to any via lo0
	${fwcmd} add 200 deny all from any to 127.0.0.0/8
	${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
}

if [ -n "${1}" ]; then
	firewall_type="${1}"
fi

. /etc/rc.subr
. /etc/network.subr
afexists inet6
ipv6_available=$?

############
# Set quiet mode if requested
#
case ${firewall_quiet} in
[Yy][Ee][Ss])
	fwcmd="/sbin/ipfw -q"
	;;
*)
	fwcmd="/sbin/ipfw"
	;;
esac
 
############
# Flush out the list before we begin.
#
${fwcmd} -f flush

setup_loopback
setup_ipv6_mandatory

############
# Network Address Translation.  All packets are passed to natd(8)
# before they encounter your remaining rules.  The firewall rules
# will then be run again on each packet after translation by natd
# starting at the rule number following the divert rule.
#
# For ``server firewall type the divert rule should be put to a
# different place to not interfere with address-checking rules.
#
case ${firewall_type} in
[Oo][Pp][Ee][Nn])
	case ${firewall_nat_enable} in
	[Yy][Ee][Ss])
		if [ -n "${firewall_nat_interface}" ]; then
			if echo "${firewall_nat_interface}" | \
				grep -q -E '^[0-9]+(\.[0-9]+){0,3}$'; then
				firewall_nat_flags="ip ${firewall_nat_interface} ${firewall_nat_flags}"
			else
				firewall_nat_flags="if ${firewall_nat_interface} ${firewall_nat_flags}"
			fi
			${fwcmd} nat 123 config log ${firewall_nat_flags}
			${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
		fi
		;;
	esac
esac

############
# If you just configured ipfw in the kernel as a tool to solve network
# problems or you just want to disallow some particular kinds of traffic
# then you will want to change the default policy to open.  You can also
# do this as your only action by setting the firewall_type to ``open.
#
# ${fwcmd} add 65000 pass all from any to any


# Prototype setups.
#
case ${firewall_type} in
[Oo][Pp][Ee][Nn])
	${fwcmd} add 65000 pass all from any to any
	;;

[Ss][Ee][Rr][Vv][Ee][Rr])
	############
	# Configuration:
	#  firewall_server_iif:		Inside IPv4 network interface.
	#  firewall_server_inet:	Inside IPv4 network address.
	#  firewall_server_oif:		Outside IPv4 network interface.
	#  firewall_server_onet:	Outside IPv4 network address.
	#  firewall_nat_interface:		NAT interface
	#  firewall_nat_address:		NAT address
	#  firewall_vpn_net:			VPN IPv4 network
	#  firewall_vpn_gateway:	VPN IPv4 gateway
	############

	# set these to your outside interface network
	oif="$firewall_server_oif"
	onet="$firewall_server_onet"

	# set these to your inside interface network
	iif="$firewall_server_iif"
	inet="$firewall_server_inet"

	# set this VPN variable
	vpn_net="${firewall_vpn_net}"
	vpn_gateway="${firewall_vpn_gateway}"

	# Stop spoofing
	${fwcmd} add deny all from ${inet} to any in via ${oif}
	${fwcmd} add deny all from ${onet} to any in via ${iif}

	# BruteBlock
	${fwcmd} add deny ip from table\(1\) to any

	# Block bad gay's
	${fwcmd} add deny ip from table\(2\) to any

	# Stop standart ports
	${fwcmd} add deny tcp from any to me 137,138,139,445,3127,3128,3306 via ${oif}
	${fwcmd} add deny udp from any to me 67,68,137,138,139,145,389,445,631,3128,3306 via ${oif}

	# Stop RFC1918 nets on the outside interface
	${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
	${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
	${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}

	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
	# on the outside interface
	${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
	${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
	${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
	${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
	${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}

	# Squid
	case ${squid_enable} in
	[Yy][Ee][Ss])
		${fwcmd} add fwd 127.0.0.1,3128 tcp from ${inet} to any 80 via ${iif}
	;;
	esac

	case ${firewall_nat_enable} in
	[Yy][Ee][Ss])
		${fwcmd} nat 1 config ip ${firewall_nat_address}
		${fwcmd} add nat 1 ip from ${inet} to any out via ${firewall_nat_interface}
		${fwcmd} add nat 1 ip from any to ${firewall_nat_address} in via ${firewall_nat_interface}
	esac

	# Stop RFC1918 nets on the outside interface
	${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
	${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
	${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}

	# Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
	# DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
	# on the outside interface
	${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
	${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
	${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
	${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
	${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}


	# GRE
	#${fwcmd} add allow tcp from any to me 1723
	#${fwcmd} add allow gre from any to any
	#${fwcmd} add allow ip from any to any via tun0

	# Allow TCP through if setup succeeded
	${fwcmd} add pass tcp from any to any established

	# Allow IP fragments to pass through
	${fwcmd} add pass all from any to any frag

	# Allow setup of incoming email
	${fwcmd} add pass tcp from any to me smtp,pop3,smtps,pop3s setup

	# Allow access to our DNS
	${fwcmd} add pass tcp from any to me domain setup
	${fwcmd} add pass udp from any to me domain
	${fwcmd} add pass udp from me domain to any

	# Allow access to our WWW
	${fwcmd} add pass tcp from any to me http,https setup

	# Allow SSH
	${fwcmd} add pass tcp from any to me ssh setup

	# Reject&Log all setup of incoming connections from the outside
	${fwcmd} add deny log ip4 from any to any in via ${oif} setup proto tcp

	# Allow setup of any other TCP connection
	${fwcmd} add pass tcp from any to any setup

	# Allow DNS queries out in the world
	${fwcmd} add pass udp from me to any domain keep-state

	# Allow NTP queries out in the world
	${fwcmd} add pass udp from me to any ntp keep-state

	# Allow ICMP
	${fwcmd} add pass icmp from any to any

	# Allow UDP
	${fwcmd} add pass udp from any to any

	# BadGuys
	/etc/badguys.sh
	;;

[Cc][Ll][Oo][Ss][Ee][Dd])
	${fwcmd} add 65000 deny ip from any to any
	;;
[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
	;;
*)
	if [ -r "${firewall_type}" ]; then
		${fwcmd} ${firewall_flags} ${firewall_type}
	fi
	;;
esac