<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpaLinux.com - สปาลีนุกซ์ &#187; Firewall</title>
	<atom:link href="http://spalinux.com/category/firewall/feed" rel="self" type="application/rss+xml" />
	<link>http://spalinux.com</link>
	<description>เพื่อชีวิตที่ผ่อนคลายของคนใช้ลีนุกซ์</description>
	<lastBuildDate>Sat, 12 May 2012 10:38:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>การสตาร์ต Apache Web Server บน CentOS 6</title>
		<link>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6</link>
		<comments>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6#comments</comments>
		<pubDate>Thu, 12 Apr 2012 17:23:20 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[start]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2062</guid>
		<description><![CDATA[จากที่ได้ ติดตั้งเว็บเซิร์ฟเวอร์ ​Apache และ ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache เรียบร้อยแล้ว ขั้นตอนต่อไปก็เป็นรัน Apache Web Server ขึ้นมาเพื่อให้ใช้งานได้ ในบทความนี้จะอธิบายการรันหรือสตาร์ต Apache Web Server บน CentOS 6.2 โดยจะใช้คำสั่ง service เพื่อสะดวกในการจัดการ กล่าวถึงวิธีการแก้ไขคอนฟิกเบื้องต้น และเมื่อทดสอบเรียบร้อยแล้ว ก็จะใช้คำสั่ง chkconfig เพื่อเวลาตอนบู๊ตเครื่องจะได้รันเซอร์วิสเว็บเซิร์ฟเวอร์ขึ้นมาโดยอัตโนมัติ บน CentOS จะตั้งชื่อเซอร์วิสเรียก Apache ว่า httpd เพราะฉะนั้นเวลาใช้คำสั่ง service หรือ chkconfig ต้องใช้ชื่อเป็น httpd รันเซอร์วิส httpd ใช้คำสั่ง service เพื่อสตาร์ตเซอร์วิส httpd [root@cent6 ~]# service httpd start Starting httpd:            [...]]]></description>
			<content:encoded><![CDATA[<p>จากที่ได้ <a title="ติดตั้งเว็บเซิร์ฟเวอร์ ​Apache" href="http://spalinux.com/2012/04/install-apache-web-server">ติดตั้งเว็บเซิร์ฟเวอร์ ​Apache</a> และ <a title="View Details: ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache" href="http://spalinux.com/2012/04/install-php-web-programming-on-apache" rel="bookmark">ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache</a> เรียบร้อยแล้ว</p>
<p>ขั้นตอนต่อไปก็เป็นรัน Apache Web Server ขึ้นมาเพื่อให้ใช้งานได้</p>
<p>ในบทความนี้จะอธิบายการรันหรือสตาร์ต Apache Web Server บน CentOS 6.2 โดยจะใช้คำสั่ง service เพื่อสะดวกในการจัดการ กล่าวถึงวิธีการแก้ไขคอนฟิกเบื้องต้น และเมื่อทดสอบเรียบร้อยแล้ว ก็จะใช้คำสั่ง chkconfig เพื่อเวลาตอนบู๊ตเครื่องจะได้รันเซอร์วิสเว็บเซิร์ฟเวอร์ขึ้นมาโดยอัตโนมัติ</p>
<p><span id="more-2062"></span></p>
<p>บน CentOS จะตั้งชื่อเซอร์วิสเรียก Apache ว่า httpd เพราะฉะนั้นเวลาใช้คำสั่ง service หรือ chkconfig ต้องใช้ชื่อเป็น httpd</p>
<h4>รันเซอร์วิส httpd</h4>
<p>ใช้คำสั่ง service เพื่อสตาร์ตเซอร์วิส httpd</p>
<pre>[root@cent6 ~]# <strong>service httpd start</strong>
Starting httpd:                                            [  OK  ]</pre>
<p>ต้องขึ้นผลลัพธ์ว่า OK แสดงว่าสตาร์ตเซอร์วิสสำเร็จ</p>
<p>หลังจากสตาร์ตเสร็จเรียบร้อย ลองใช้คำสั่ง ps เพื่อดูโปรเซสของ httpd</p>
<pre>[root@cent6 ~]# <strong>ps -ef | egrep "(UID|httpd)"</strong>
UID        PID  PPID  C STIME TTY          TIME CMD
root      1623     1  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1625  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1626  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1627  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1628  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1629  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1630  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1631  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd
apache    1632  1623  0 15:23 ?        00:00:00 /usr/sbin/httpd</pre>
<div></div>
<div>เนื่องจากผลลัพธ์ของคำสั่ง ps จะแสดงโปรเซสทั้งหมดที่อยู่บนเครื่อง เพื่อให้ดูง่าย นิยมใช้คำสั่ง grep เพื่อดูเฉพาะบรรทัดที่มีคำที่ต้องการดู</div>
<p>ในที่นี้ใช้คำสั่ง egrep เพื่อค้นหาบรรทัดที่มีคำว่า UID หรือ httpd เพื่อให้แสดงชื่อฟิลด์ต่างๆ ด้วย</p>
<h4></h4>
<h4>ทดสอบจาก Web Client (Browser)</h4>
<p>หลังจากสตาร์ตเซอร์วิสเรียบร้อยแล้ว ลองใช้ browser จากเครื่อง client อื่นทดสอบดู</p>
<p>หน้าจอของ browser แสดง error ว่าเข้าเว็บไม่ได้</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/w01-cannot-access-web.png"><img class="alignnone size-full wp-image-2065" title="w01-cannot-access-web" src="http://spalinux.com/wp-content/uploads/2012/04/w01-cannot-access-web.png" alt="" width="524" height="218" /></a></p>
<p>&nbsp;</p>
<p>เหตุผลก็เป็นเพราะว่า โดยดีฟอลต์จากการติดตั้ง CentOS คอนฟิกของ firewall จะอนุญาตให้เข้าได้เฉพาะ ssh ได้เท่านั้น</p>
<p>ใช้คำสั่ง system-config-firewall-tui เพื่อเพิ่มคอนฟิก firewall อนุญาตให้เครื่องอื่นสามารถเข้ามาใช้เซอร์วิส WWW(HTTP) ได้</p>
<pre>[root@cent6 ~]# <strong>system-config-firewall-tui</strong></pre>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/w02-allow-www-http-service.png"><img class="alignnone size-full wp-image-2066" title="w02-allow-www-http-service" src="http://spalinux.com/wp-content/uploads/2012/04/w02-allow-www-http-service.png" alt="" width="499" height="337" /></a></p>
<p>&nbsp;</p>
<p>ลองใช้ browser จากเครื่อง client ทดสอบดูอีกครั้ง ก็จะใช้งานได้ ขึ้นหน้า Apache 2 Test Page</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/w03-browser-test-web-ok.png"><img class="alignnone size-full wp-image-2067" title="w03-browser-test-web-ok" src="http://spalinux.com/wp-content/uploads/2012/04/w03-browser-test-web-ok.png" alt="" width="777" height="562" /></a></p>
<p>&nbsp;</p>
<p>หมายเหตุ หน้า Apache 2 Test Page นี้ จะถูกแสดงขึ้นมา ในกรณีที่ไดเร็คทอรีเว็บที่เราเรียก ไม่มีไฟล์ index โดยจะไปเรียกไฟล์​ /var/www/error/noindex.html  มาแสดงแทน</p>
<p>&nbsp;</p>
<h4>ทดสอบเขียนไฟล์เว็บ html</h4>
<p>ดีฟอลต์คอนฟิก apache ที่ติดตั้งจาก rpm ไดเร็คทอรีหลัก (DocumentRoot) ที่เก็บไฟล์เว็บหน้าแรกจะอยู่ใน /var/www/html/</p>
<p>ทดสอบเขียนไฟล์เว็บ ชื่อ test.html ไว้ในไดเร็คทอรี /var/www/html/</p>
<pre>[root@cent6 ~]# <strong>cat /var/www/html/test.html</strong>
 &lt;html&gt;
 &lt;body&gt;
 ทดสอบเขียนเว็บ
 &lt;/body&gt;
 &lt;/html&gt;</pre>
<pre></pre>
<p>ลองเปิดด้วย browser ดู</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/w04-test-web-html.png"><img class="alignnone size-full wp-image-2069" title="w04-test-web-html" src="http://spalinux.com/wp-content/uploads/2012/04/w04-test-web-html.png" alt="" width="309" height="82" /></a></p>
<p>&nbsp;</p>
<h4>ทดลองเขียนไฟล์เว็บโปรแกรม php</h4>
<p>หากได้ <a title="View Details: ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache" href="http://spalinux.com/2012/04/install-php-web-programming-on-apache" rel="bookmark">ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache</a> ด้วย ลองเขียนไฟล์  php เพื่อทดสอบรันบนเว็บเซิร์ฟเวอร์ดู</p>
<pre>[root@cent6 ~]# <strong>cat /var/www/html/test.php</strong></pre>
<pre>&lt;?php</pre>
<pre>print 'เขียนโปรแกรม PHP&lt;br&gt;';</pre>
<pre>for ($i=1; $i&lt;=9; $i++) {
 print "$i";
}</pre>
<pre>?&gt;</pre>
<p>&nbsp;</p>
<p>ลองเปิดด้วย browser ดู</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/w05-test-web-php.png"><img class="alignnone size-full wp-image-2070" title="w05-test-web-php" src="http://spalinux.com/wp-content/uploads/2012/04/w05-test-web-php.png" alt="" width="313" height="101" /></a></p>
<p>&nbsp;</p>
<h4>คอนฟิกให้ สตาร์ต Apache ตอนบู๊ตเครื่อง</h4>
<p>เมื่อทดสอบเรียบร้อยแล้ว หากต้องการให้สตาร์ต apache ตอนบู๊ตเครื่องโดยอัตโนมัติ สามารถทำได้โดยใช้คำสั่ง chkconfig</p>
<pre>[root@cent6 ~]# chkconfig httpd on</pre>
<pre>[root@cent6 ~]# chkconfig --list | grep http
httpd          0:off 1:off 2:on 3:on 4:on 5:on 6:off</pre>
<div></div>
<h4>ปิดเซอร์วิส Apache</h4>
<p>ในกรณีที่ต้องการปิดเซอร์วิส Apache สามารถทำได้โดยใช้คำสั่ง service</p>
<pre>[root@cent6 ~]# <strong>service httpd stop</strong>
Stopping httpd:                                            [  OK  ]</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="ติดตั้งเว็บเซิร์ฟเวอร์ ​Apache" href="http://spalinux.com/2012/04/install-apache-web-server">ติดตั้งเว็บเซิร์ฟเวอร์ ​Apache</a></li>
<li><a title="View Details: ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache" href="http://spalinux.com/2012/04/install-php-web-programming-on-apache" rel="bookmark">ติดตั้งโปรแกรมภาษาเว็บ PHP บน Apache</a></li>
<li><a title="View Details: วิธีการเปิด ปิด เซอร์วิสของ CentOS 6" href="http://spalinux.com/2012/04/how-to-start-stop-service-in-centos-6" rel="bookmark">วิธีการเปิด ปิด เซอร์วิส</a></li>
<li><a title="View Details: การเปิดเซอร์วิสตอนบู๊ตเครื่องของ CentOS 6" href="http://spalinux.com/2012/04/start-service-when-boot-centos-6" rel="bookmark">การเปิดเซอร์วิสตอนบู๊ตเครื่อง</a></li>
<li><a title="View Details: แก้ไขคอนฟิก firewall ด้วย system-config-firewall" href="http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall" rel="bookmark">แก้ไขคอนฟิก firewall ด้วย system-config-firewall</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/howto-start-apache-web-server-on-centos-6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>แก้ไขคอนฟิก firewall ด้วย system-config-firewall</title>
		<link>http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall</link>
		<comments>http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall#comments</comments>
		<pubDate>Sun, 08 Apr 2012 16:46:12 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Linux Installation]]></category>
		<category><![CDATA[Linux Shell]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2032</guid>
		<description><![CDATA[จากบทความ แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables ผู้ที่จะแก้ไขคอนฟิกแบบนี้ได้ ต้องมีความรู้ทางด้านเน็ตเวิร์ก มีความเข้าใจเรื่อง firewall รวมทั้งต้องทราบว่าเซอร์วิสที่รันใช้พอร์ตอะไร ซึ่งอาจทำให้เกิดความยากสำหรับผู้เริ่มต้น เพราะไม่รู้ว่าจะเพิ่ม หรือลบ rule อย่างไร ใส่ออปชั่นอะไรบ้าง สำหรับผู้เริ่มต้นหัดใช้ แนะนำให้ใช้โปรแกรม system-config-firewall เพื่อช่วยแก้ไข rule ของ firewall บนลีนุกซ์ โปรแกรม system-config-firewall มีสองเวอร์ชั่น ทำงานได้เหมือนกัน system-config-firewall รันเป็น GUI บน X Window เช่น GNOME system-config-firewall-tui รันบนหน้าจอคอนโซล จะเป็นแบบเมนูให้เลือก ในที่นี้จะแสดงวิธีการใช้แบบรันบนหน้าจอคอนโซล เพื่อเปรียบเทียบสิ่งที่โปรแกรม system-config-firewall จะแก้ไข ก่อนรันคำสั่ง ใช้คำสั่ง iptables -L เพื่อแสดงคอนฟิก firewall ที่ทำงานอยู่ [root@cent6 ~]# iptables -L -v -n --line-numbers [...]]]></description>
			<content:encoded><![CDATA[<p>จากบทความ <a href="http://spalinux.com/2012/04/configure-linux-firewall-by-iptables">แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables</a> ผู้ที่จะแก้ไขคอนฟิกแบบนี้ได้ ต้องมีความรู้ทางด้านเน็ตเวิร์ก มีความเข้าใจเรื่อง firewall รวมทั้งต้องทราบว่าเซอร์วิสที่รันใช้พอร์ตอะไร</p>
<p>ซึ่งอาจทำให้เกิดความยากสำหรับผู้เริ่มต้น เพราะไม่รู้ว่าจะเพิ่ม หรือลบ rule อย่างไร ใส่ออปชั่นอะไรบ้าง</p>
<p>สำหรับผู้เริ่มต้นหัดใช้ แนะนำให้ใช้โปรแกรม system-config-firewall เพื่อช่วยแก้ไข rule ของ firewall บนลีนุกซ์</p>
<p><span id="more-2032"></span></p>
<p>โปรแกรม system-config-firewall มีสองเวอร์ชั่น ทำงานได้เหมือนกัน</p>
<ul>
<li><strong>system-config-firewall</strong> รันเป็น GUI บน X Window เช่น GNOME</li>
<li><strong>system-config-firewall-tui</strong> รันบนหน้าจอคอนโซล จะเป็นแบบเมนูให้เลือก</li>
</ul>
<p>ในที่นี้จะแสดงวิธีการใช้แบบรันบนหน้าจอคอนโซล</p>
<p>เพื่อเปรียบเทียบสิ่งที่โปรแกรม system-config-firewall จะแก้ไข ก่อนรันคำสั่ง ใช้คำสั่ง iptables -L เพื่อแสดงคอนฟิก firewall ที่ทำงานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     2414  180K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        1    64 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 1379 packets, 505K bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<div></div>
<p>พิมพ์คำสั่ง system-config-firewall-tui</p>
<pre>[root@cent6 ~]# <strong>system-config-firewall-tui</strong></pre>
<p>หมายเหตุ หากรันคำสั่งแล้วขึ้น &#8220;command not found&#8221; ให้ติดตั้งไฟล์ rpm ชื่อ system-config-firewall-tui-1.2.27-5.el6.noarch.rpm</p>
<p>หน้าจอ Firewall Configuration สามารถเลือกที่จะเปิด ปิด firewall ได้</p>
<pre>[*] Enabled เปิดใช้งาน firewall
[ ] Enabled ปิดการใช้ firewall (ไม่แนะนำ)</pre>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/f01-system-config-firewall-tui-Firewall-Configuration.png"><img class="alignnone size-full wp-image-2035" title="f01-system-config-firewall-tui-Firewall Configuration" src="http://spalinux.com/wp-content/uploads/2012/04/f01-system-config-firewall-tui-Firewall-Configuration.png" alt="" width="639" height="411" /></a></p>
<p>&nbsp;</p>
<p>กดปุ่ม [Tab] เพื่อเลื่อนแถบสีไปยัง [Customize] แล้วกดปุ่ม [Enter]</p>
<p>หน้าจอแสดง Services ต่างๆ ที่โปรแกรมเตรียมไว้ให้สามารถเลือกคอนฟิกได้</p>
<p>กดปุ่มลูกศร (cursor) ขึ้นลง เพื่อเลื่อนไปยังเซอร์วิสต่างๆ ได้</p>
<p>โดยดีฟอลต์จากการติดตั้ง ถ้าไม่ได้แก้ไข จะอนุญาตให้เครื่องอื่นเข้ามาใช้ SSH ได้อย่างเดียว</p>
<p>เครื่องหมายดอกจัน ในวงเล็บสี่เหลี่ยม [*] หน้าเซอร์วิส หมายถึงการอนุญาตเซอร์วิสนั้น</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/f02-Trusted-Services.png"><img class="alignnone size-full wp-image-2036" title="f02-Trusted-Services" src="http://spalinux.com/wp-content/uploads/2012/04/f02-Trusted-Services.png" alt="" width="492" height="332" /></a></p>
<p>&nbsp;</p>
<p>กดปุ่มลูกศรลง เพื่อไปยังชื่อเซอร์วิสที่ต้องการอนุญาตให้เครื่องอื่นสามารถเข้ามาใช้บนเครื่องลีนุกซ์นี้ได้</p>
<p>เช่นต้องการให้เครื่องอื่น สามารถใช้เว็บเซิร์ฟเวอร์พอร์ต 80 WWW (HTTP) บนเครื่องเราได้</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/f03-select-WWW.png"><img class="alignnone size-full wp-image-2037" title="f03-select-WWW" src="http://spalinux.com/wp-content/uploads/2012/04/f03-select-WWW.png" alt="" width="313" height="109" /></a></p>
<p>กดปุ่ม [Spacebar] เพื่อเปลี่ยนค่าในวงเล็บสี่เหลี่ยม ต้องเปลี่ยนให้เป็นเครื่องหมายดอกจัน</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/f04-Enable-WWW.png"><img class="alignnone size-full wp-image-2038" title="f04-Enable-WWW" src="http://spalinux.com/wp-content/uploads/2012/04/f04-Enable-WWW.png" alt="" width="312" height="111" /></a></p>
<p>หลังจากเปลี่ยนเป็นเครื่องหมายดอกจันเรียบร้อยแล้ว กดปุ่ม [Tab] เพื่อเลื่อนแถบสีมาที่ [Close] แล้วกดปุ่ม [Enter]</p>
<p>จะกลับมาหน้าจอหลัก กดปุ่ม [Tab] เลื่อนไปยัง [OK] แล้วกดปุ่ม [Enter]<br />
หน้าจอเตือน Warning ว่าจะมีการสร้างคอนฟิกไฟล์ของ firewall ใหม่ เลื่อนแถบสีอยู่ที่ [Yes] แล้วกดปุ่ม [Enter]</p>
<p><a href="http://spalinux.com/wp-content/uploads/2012/04/f05-Warning.png"><img class="alignnone size-full wp-image-2039" title="f05-Warning" src="http://spalinux.com/wp-content/uploads/2012/04/f05-Warning.png" alt="" width="378" height="289" /></a></p>
<p>&nbsp;</p>
<p>ลองใช้คำสั่ง iptables -L เพื่อดูคอนฟิก firewall ที่เปลี่ยนไป จะเห็น num 5 เพิ่มขึ้นมา เพื่ออนุญาต tcp dpt:80 หรือพอร์ตเว็บ WWW (HTTP) นั่นเอง</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       13   916 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        1   100 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
<span style="color: #0000ff;">5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80</span>
6        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 8 packets, 960 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<p>หากต้องการยกเลิก หรือไม่ต้องการให้เครื่องอื่นเข้าใช้งานพอร์ตเว็บบนเครื่องนี้อีกต่อไปแล้ว ก็ใช้คำสั่ง system-config-firewall-tui เข้าไปเอาเครื่องหมายดอกจันหน้าเซอร์วิส WWW (HTTP) ออกไป ทำตามขั้นตอนอื่นๆ เหมือนเดิม</p>
<p>การใช้คำสั่ง system-config-firewall ไม่ว่าจะแบบ GUI หรือคอนโซล เมื่อแก้ไขเสร็จแล้วกดยืนยันเพื่อให้คอนฟิกใหม่มีผล นอกจากจะทำให้คอนฟิกใหม่มีผลทันทีแล้ว คำสั่งนี้จะเข้าไปแก้ไขไฟล์คอนฟิกของ iptables เพื่อให้มีผลต่อการบู๊ตเครื่องครั้งต่อไปด้วย</p>
<p>ไฟล์คอนฟิกของ iptables ของ CentOS คือไฟล์ /etc/sysconfig/iptables</p>
<p>ตัวอย่างของไฟล์หลังจากเพิ่ม การอนุญาตพอร์ต 80</p>
<pre>[root@cent6 ~]# cat /etc/sysconfig/iptables
 # Firewall configuration written by system-config-firewall
 # Manual customization of this file is not recommended.
 *filter
 :INPUT ACCEPT [0:0]
 :FORWARD ACCEPT [0:0]
 :OUTPUT ACCEPT [0:0]
 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 -A INPUT -p icmp -j ACCEPT
 -A INPUT -i lo -j ACCEPT
 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
<span style="color: #0000ff;"> -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT</span>
 -A INPUT -j REJECT --reject-with icmp-host-prohibited
 -A FORWARD -j REJECT --reject-with icmp-host-prohibited
 COMMIT</pre>
<p>ชื่อเซอร์วิสที่มาอ่านไฟล์นี้ก็คือ iptables โดยดีฟอลต์จะถูกรันเมื่อเข้าสู่ runlevel 2,3,4,5 หรือการบู๊ตเครื่องใหม่</p>
<pre>[root@cent6 ~]# <strong>chkconfig --list iptables</strong>
iptables        0:off 1:off 2:on 3:on 4:on 5:on 6:off</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="View Details: แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables" href="http://spalinux.com/2012/04/configure-linux-firewall-by-iptables" rel="bookmark">แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables</a></li>
<li><a title="View Details: การเปิดเซอร์วิสตอนบู๊ตเครื่องของ CentOS 6" href="http://spalinux.com/2012/04/start-service-when-boot-centos-6" rel="bookmark">การเปิดเซอร์วิสตอนบู๊ตเครื่องของ CentOS 6</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>แก้ไขคอนฟิก firewall บนลีนุกซ์ด้วย iptables</title>
		<link>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables</link>
		<comments>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables#comments</comments>
		<pubDate>Sun, 08 Apr 2012 10:28:19 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[CentOS 6]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Linux Installation]]></category>
		<category><![CDATA[Linux Shell]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=2013</guid>
		<description><![CDATA[บนลีนุกซ์มีคุณสมบัติ firewall ติดตั้งมาให้สามารถเลือกอนุญาต หรือปฏิเสธการ รับ/ส่ง packet เข้า/ออกเครื่องได้ จุดมุ่งหมายหลักๆ ของ firewall ก็เพื่อเพิ่มความปลอดภัยให้กับเครื่องเซิร์ฟเวอร์ หลายคนมักจะปิดคุณสมบัตินี้ไป ด้วยเหตุผลบอกว่ายากต่อการทำความเข้าใจ และแก้ไข ในบทความนี้จะอธิบายการใช้คำสั่ง iptables เพื่อแสดงสถานะ firewall ที่เปิดใช้งาน การเพิ่ม rule เพื่ออนุญาตการเชื่อมต่อ การลบ rule ที่ไม่ได้ใช้แล้ว ลองทำตามดู ในหลายเซอร์วิส เพียงแค่แก้ไข rule นิดเดียวเท่านั้น ก็จะใช้งานได้แล้ว ไม่จำเป็นต้องปิดคุณสมบัติ firewall แต่อย่างได้ แสดง (list) rule ที่เปิดใช้งานอยู่ ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่เปิดใช้งานอยู่ ออปชั่นเพิ่มเติม ที่มักจะใช้ร่วมกับออปชั่น &#8220;-L&#8221; ออปชั่น &#8220;-v&#8221; เพื่อแสดงรายละเอียดเพิ่มเติมของ rule เช่นผลรวมสะสมของจำนวน packets (pkts) [...]]]></description>
			<content:encoded><![CDATA[<p>บนลีนุกซ์มีคุณสมบัติ firewall ติดตั้งมาให้สามารถเลือกอนุญาต หรือปฏิเสธการ รับ/ส่ง packet เข้า/ออกเครื่องได้</p>
<p>จุดมุ่งหมายหลักๆ ของ firewall ก็เพื่อเพิ่มความปลอดภัยให้กับเครื่องเซิร์ฟเวอร์</p>
<p>หลายคนมักจะปิดคุณสมบัตินี้ไป ด้วยเหตุผลบอกว่ายากต่อการทำความเข้าใจ และแก้ไข</p>
<p>ในบทความนี้จะอธิบายการใช้คำสั่ง iptables เพื่อแสดงสถานะ firewall ที่เปิดใช้งาน การเพิ่ม rule เพื่ออนุญาตการเชื่อมต่อ การลบ rule ที่ไม่ได้ใช้แล้ว</p>
<p>ลองทำตามดู ในหลายเซอร์วิส เพียงแค่แก้ไข rule นิดเดียวเท่านั้น ก็จะใช้งานได้แล้ว ไม่จำเป็นต้องปิดคุณสมบัติ firewall แต่อย่างได้</p>
<p><span id="more-2013"></span></p>
<h4>แสดง (list) rule ที่เปิดใช้งานอยู่</h4>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่เปิดใช้งานอยู่</p>
<p>ออปชั่นเพิ่มเติม ที่มักจะใช้ร่วมกับออปชั่น &#8220;-L&#8221;</p>
<ul>
<li>ออปชั่น &#8220;<strong>-v</strong>&#8221; เพื่อแสดงรายละเอียดเพิ่มเติมของ rule เช่นผลรวมสะสมของจำนวน packets (pkts) ขนาด (bytes)</li>
<li>ออปชั่น &#8220;<strong>-n</strong>&#8221; เพื่อแสดง IP (source และ destination) และพอร์ตให้เป็นตัวเลข</li>
<li>ออปชั่น &#8220;<strong>&#8211;line-numbers</strong>&#8221; เพื่อแสดงตัวเลขกำกับ rule ในแต่ละบรรทัด เพื่อสะดวกต่อการแก้ไข rule</li>
</ul>
<p>ตัวอย่างการใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงค่าคอนฟิก firewall ที่ใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      630 49714 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 349 packets, 55278 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<pre></pre>
<p>คอนฟิก rule ที่แสดงนี้ เป็นคอนฟิกดีฟอลต์จากติดตั้ง CentOS 6 (หากไม่ได้มีการเลือกแก้ไข firewall ระหว่างการติดตั้ง)</p>
<p>ในที่นี้ขออธิบาย rule คร่าวๆ ในส่วนของ Chain INPUT ที่เปิดใช้งานอยู่ดังนี้</p>
<ul>
<li>num 1 : อนุญาต (ACCEPT) ให้ทุก packet ที่ผ่านขั้นตอนการเชื่อมต่อเรียบร้อยแล้ว (RELATED, ESTABLISHED) เข้าสู่เครื่องได้</li>
<li>num 2 : อนุญาต (ACCEPT) ให้ packet ประเภท (icmp) เช่น ping เข้าสู่เครื่องได้</li>
<li>num 3 : อนุญาต (ACCEPT) ให้ทุก packet ที่ส่งภายในของเครื่องเองได้ (lo หรือ loopback)</li>
<li>num 4 : อนุญาต (ACCEPT) ให้ packet แรก (NEW) ที่พยายามเชื่อมต่อเข้าพอร์ตปลายทาง tcp dpt:22 (secure shell) สามารถเข้าสู่เครื่องได้ หรือสรุปง่ายๆ คืออนุญาตให้สามารถ ssh เข้าสู่เครื่องนี้ได้นั่นเอง</li>
<li>num 5 : packet อื่นๆ นอกจากนี้ จะถูกปฏิเสธ (REJECT) การเข้าถึงเครื่อง และจะมีการตอบ icmp-host-prohibited กลับไปยังเครื่องต้นทางที่พยายามส่ง packet เข้ามา</li>
</ul>
<p>การทำงาน firewall ของลีนุกซ์จะตรวจสอบ (match) ตามลำดับ rule num ที่คอนฟิกไว้ เช่นเมื่อมี packet พยายามส่งเข้า (INPUT) ก็จะตรวจสอบเรียงตาม num 1, 2, 3, 4, 5</p>
<p>&nbsp;</p>
<h4>เพิ่ม (insert) rule ใหม่</h4>
<p>หากเราติดตั้งเซอร์วิสใหม่ คอนฟิกและรันได้เรียบร้อยแล้ว เช่นเว็บเซิร์ฟเวอร์ เปิดใช้งานพอร์ต tcp 80</p>
<p>ตัวอย่างการใช้คำสั่ง netstat -an เพื่อดูพอร์ตที่เปิดอยู่ ในที่นี้จะใช้คำสั่ง grep เพื่อเลือกแสดงเฉพาะบรรทัดที่มีตัวเลข 80</p>
<pre>[root@cent6 ~]# <strong>netstat -an | grep 80</strong>
tcp        0      0 :::80                       :::*                        LISTEN</pre>
<p>เราสามารถทดสอบจากเครื่องที่รันเว็บเซิร์ฟเวอร์เองได้ เพราะ rule ของ firewall โดยดีฟอลต์อนุญาตให้การส่งข้อมูลภายใน (lo) สามารถทำได้ (rule num 3)</p>
<p>ตัวอย่างการใช้คำสั่ง telnet เพื่อทดสอบการเชื่อมต่อเข้าพอร์ต 80 ของเว็บเซิร์ฟเวอร์ 192.168.5.62 (ทดสอบบนเครื่องเดียวกัน กับที่รันเว็บเซิรฟ์เวอร์)</p>
<pre>[root@cent6 ~]# <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.

<strong>HEAD / HTTP/1.0</strong>

HTTP/1.1 403 Forbidden
Date: Sun, 08 Apr 2012 08:53:11 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8

Connection closed by foreign host.</pre>
<p>คำอธิบายการใช้คำสั่ง telnet เข้าพอร์ต</p>
<ul>
<li>หากใช้คำสั่ง telnet แล้วขึ้น &#8220;command not found&#8221; ต้องติดตั้งไฟล์ rpm ของโปรแกรม telnet เพิ่ม ซึ่งอยู่ในแผ่นดีวีดีติดตั้ง CentOS อยู่แล้ว</li>
</ul>
<pre>[root@cent6 Packages]# <strong>rpm -i telnet-0.17-47.el6.x86_64.rpm</strong></pre>
<ul>
<li>เราสามารถใช้คำสั่ง telnet เพื่อทดสอบเข้าพอร์ตเว็บ (tcp 80) ได้ โดยพิมพ์คำสั่ง telnet ตามด้วย IP ของเว็บเซิร์ฟเวอร์ แล้วตามด้วยพอร์ตที่รันเว็บ</li>
<li>ในคำสั่ง telnet เมื่อขึ้น Connected แปลว่าสามารถเชื่อมต่อเข้าพอร์ตที่ระบะไว้ได้</li>
<li>กดปุ่ม [Enter] หนึ่งครั้ง แล้วพิมพ์คำสั่ง HEAD / HTTP/1.0 เพื่อเป็นการส่งคำสั่งขอดูข้อมูลเบื้องต้นของเว็บเซิร์ฟเวอร์ได้</li>
<li>หากต้องการออกจากคำสั่ง telnet ให้กดปุ่ม [Ctrl] และ ] พร้อมกัน จะมีพร้อมพต์ telnet&gt; ขึ้นมาให้พิมพ์คำสั่ง quit เพื่อออจาก telnet</li>
</ul>
<pre>[root@cent6 ~]# <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.
<strong>^]</strong>
telnet&gt; <strong>quit</strong>
Connection closed.</pre>
<p>แต่ถ้าใช้ browser หรือทดสอบด้วยคำสั่ง telnet เข้าพอร์ต 80 จากเครื่องอื่นๆ เปิดเว็บเข้ามา จะเข้าไม่ได้</p>
<pre>$ <strong>telnet 192.168.5.62 80</strong>
 Trying 192.168.5.62...
 telnet: connect to address 192.168.5.62: No route to host</pre>
<p>ต้องเพิ่ม rule ใหม่ เข้าไปเพื่ออนุญาตการเชื่อมต่อเข้ามา โดย rule ที่เพิ่มใหม่ ต้องเพิ่มก่อน rule สุดท้ายที่มีการปฏิเสธการเข้าถึง (REJECT) ทุก packet</p>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดงคอนฟิก firewall ที่ใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2363  182K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 1358 packets, 169K bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<p>ใช้คำสั่ง iptables ออปชั่น &#8220;-I&#8221; เพิ่มด้วยการแทรก (insert) rule ใหม่เข้าไปแทนที่ rule num 5 ระบุว่าอนุญาต (ACCEPT) ให้การเชื่อมต่อใหม่ (NEW) เข้า (INPUT) พอร์ต tcp 80 ได้</p>
<pre>[root@cent6 ~]# <strong>iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT</strong></pre>
<p>ตรวจสอบผลลัพธ์ที่ได้จาก iptables ออปชั่น &#8220;-L&#8221;</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2383  184K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
<span style="color: #0000ff;">5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80</span>
6       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 5 packets, 692 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<pre></pre>
<p>ทดสอบจากเครื่องอื่นด้วยการ telnet เข้าพอร์ต 80 อีกครั้ง จะสามารถเชื่อมต่อได้แล้ว</p>
<pre>$ <strong>telnet 192.168.5.62 80</strong>
Trying 192.168.5.62...
Connected to 192.168.5.62.
Escape character is '^]'.

<strong>HEAD / HTTP/1.0</strong>

HTTP/1.1 403 Forbidden
Date: Sun, 08 Apr 2012 09:06:10 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8

Connection closed by foreign host.</pre>
<pre></pre>
<p>&nbsp;</p>
<h4>ลบ (delete) rule</h4>
<p>หากต้องการยกเลิก rule ที่ใส่ไว้ เช่นไม่ต้องการให้เครื่องอื่นสามารถเชื่อมต่อเข้าพอร์ต 80 ได้อีกต่อไปแล้ว สามารถทำได้โดยใช้คำสั่ง iptables ออปชั่น -D ตามด้วย rule num ที่ต้องการลบ (delete) ใช้คำสั่ง iptables ออปชั่น &#8220;-L&#8221; เพื่อแสดง rule ที่เปิดใช้งานอยู่</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2417  186K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
<span style="color: #0000ff;">5 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80</span>
6       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 27 packets, 5466 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<p>สมมติว่าต้องการลบ rule ที่อนุญาตการเชื่อมต่อเข้า (INPUT) พอร์ต 80 หรือ rule num 5 สามารถทำได้โดย</p>
<pre>[root@cent6 ~]# <strong>iptables -D INPUT 5</strong></pre>
<p>ใช้คำสั่ง iptables -L จะเห็นว่า rule num 5 ถูกลบไป</p>
<pre>[root@cent6 ~]# <strong>iptables -L -v -n --line-numbers</strong>
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     2518  194K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
2        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3       11   660 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        4   252 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
5       10   620 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes)
num   pkts bytes target     prot opt in     out     source               destination</pre>
<p>&nbsp;</p>
<p>ลองทดสอบ telnet เข้าพอร์ต 80 จากเครื่องอื่นดู ก็ไม่สามารถเชื่อมต่อได้แล้ว</p>
<pre>$ telnet 192.168.5.62 80
 Trying 192.168.5.62...
 telnet: connect to address 192.168.5.62: No route to host</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a title="View Details: แก้ไขคอนฟิก firewall ด้วย system-config-firewall" href="http://spalinux.com/2012/04/configure-firewall-using-system-config-firewall" rel="bookmark">แก้ไขคอนฟิก firewall ด้วย system-config-firewall</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/04/configure-linux-firewall-by-iptables/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>คอนฟิก Squid Proxy Server ให้ตรวจสอบ user, password จาก LDAP</title>
		<link>http://spalinux.com/2012/03/configure-squid-proxy-server-to-authenticate-user-with-ldap</link>
		<comments>http://spalinux.com/2012/03/configure-squid-proxy-server-to-authenticate-user-with-ldap#comments</comments>
		<pubDate>Sun, 18 Mar 2012 07:12:32 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Squid]]></category>
		<category><![CDATA[Web Server]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1712</guid>
		<description><![CDATA[จากบทความ คอนฟิก Squid Proxy Server ให้ผู้ใช้ใส่ user, password ก่อนเข้าเว็บ เป็นการตรวจสอบ user จาก Local Password หรือไฟล์ /etc/passwd, /etc/shadow ผ่าน Linux PAM ที่อยู่บนเครื่อง proxy server เอง แต่ถ้าบริษัทหรือองค์กรของเรามีการเก็บ user, password อยู่บนเซิร์ฟเวอร์เครื่องอื่น เช่นเก็บรวมเป็นศูนย์กลางบน LDAP Server เราก็สามารถคอนฟิก Squid Proxy Server ให้ตรวจสอบ user, password จาก LDAP Server ได้ วิธีการคือ แก้ไขคอนฟิกในส่วนของ auth_param basic program เปลี่ยนไปใช้ไฟล์ basic_ldap_auth สมมติว่าค่าคอนฟิกต่างๆ ของ LDAP Server มีดังนี้ ออปชั่น &#8216;-v&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>จากบทความ <a href="http://spalinux.com/2012/03/configure-squid-proxy-server-user-authentication-with-local-password">คอนฟิก Squid Proxy Server ให้ผู้ใช้ใส่ user, password ก่อนเข้าเว็บ</a></p>
<p>เป็นการตรวจสอบ user จาก Local Password หรือไฟล์ /etc/passwd, /etc/shadow ผ่าน Linux PAM ที่อยู่บนเครื่อง proxy server เอง</p>
<p>แต่ถ้าบริษัทหรือองค์กรของเรามีการเก็บ user, password อยู่บนเซิร์ฟเวอร์เครื่องอื่น เช่นเก็บรวมเป็นศูนย์กลางบน LDAP Server เราก็สามารถคอนฟิก Squid Proxy Server ให้ตรวจสอบ user, password จาก LDAP Server ได้</p>
<p><span id="more-1712"></span></p>
<p>วิธีการคือ แก้ไขคอนฟิกในส่วนของ auth_param basic program เปลี่ยนไปใช้ไฟล์ basic_ldap_auth</p>
<p>สมมติว่าค่าคอนฟิกต่างๆ ของ LDAP Server มีดังนี้</p>
<ul>
<li>ออปชั่น &#8216;-v&#8217; ระบุเวอร์ชั่นของ LDAP ที่ใช้ = 3</li>
<li>ออปชั่น &#8216;-b&#8217; ระบุ Base DN สำหรับการค้นหา = &#8220;dc=example,dc=com&#8221;</li>
<li>ออปชั่น &#8216;-f&#8217; ระบุเงื่อนไขการค้นหา uid=% คือให้ค้นหาการ username (บน LDAP Server ก็ต้องเก็บโดยใช้ชื่อฟิลด์ uid ด้วย)</li>
<li>ออปชั่น &#8216;-h&#8217; ระบุ IP Address = 192.168.1.10</li>
</ul>
<p>แต่ถ้า LDAP Server ที่คุณใช้ ต้องมีการระบุชื่อ DN และ password ที่ใช้ bind ด้วย ก็ต้องระบุ 2 ออปชั่นนี้เพิ่มเติม</p>
<ul>
<li>ออปชั่น &#8216;-D&#8217; ระบุชื่อ DN สำหรับการ bind เข้า LDAP</li>
<li>ออปชั่น &#8216;-w&#8217; ระบุ password ที่ใช้ bind เข้า LDAP</li>
</ul>
<p>ตัวอย่างการแก้ไขคอนฟิก</p>
<pre>[root@fc16-64a ~]# <strong>cat /etc/squid/squid.conf</strong>
....
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
auth_param basic children 5
auth_param basic realm Proxy Authentication
auth_param basic credentialsttl 12 hours
<span style="color: #0000ff;">auth_param basic program /usr/lib64/squid/basic_ldap_auth -v 3 -b "dc=example,dc=com" -f uid=%s -h 192.168.1.10</span></pre>
<pre>acl <span style="color: #0000ff;">user_ldap_auth</span> proxy_auth REQUIRED
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet <span style="color: #0000ff;">user_ldap_auth</span>
...</pre>
<p>ใช้คำสั่ง service หรือ systemctl เพื่อ start หรือ reload เซอร์วิส squid</p>
<pre>[root@fc16-64a ~]# <strong>systemctl reload squid.service</strong></pre>
<p>เสร็จแล้วลองทดสอบจาก Browser</p>
<p><strong>หมายเหตุ</strong> หลังจากทดสอบเรียบร้อยใช้งานได้แล้ว เพื่อความปลอดภัยของเซิร์ฟเวอร์ หากคุณเปลี่ยนจาก basic_pam_auth มาเป็น basic_ldap_auth แล้ว ให้ยกเลิก setuid root ของไฟล์ basic_pam_auth ด้วย ใช้คำสั่ง chmod ดังนี้</p>
<pre>[root@fc16-64a ~]# <strong>ls -l /usr/lib64/squid/basic_pam_auth</strong>
-rw<span style="color: #ff0000;">s</span>r-x---. 1 root squid 15416 Sep 20 15:36 /usr/lib64/squid/basic_pam_auth</pre>
<pre>[root@fc16-64a ~]# <strong>chmod u-s /usr/lib64/squid/basic_pam_auth</strong></pre>
<pre>[root@fc16-64a ~]# <strong>ls -l /usr/lib64/squid/basic_pam_auth</strong>
-rw<span style="color: #0000ff;">x</span>r-x---. 1 root squid 15416 Sep 20 15:36 /usr/lib64/squid/basic_pam_auth</pre>
<p>&nbsp;</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://spalinux.com/2012/03/configure-squid-proxy-server-user-authentication-with-local-password">คอนฟิก Squid Proxy Server ให้ผู้ใช้ใส่ user, password ก่อนเข้าเว็บ</a></li>
<li><a href="http://spalinux.com/2010/07/install_openldap_server_on_fedora_13">ติดตั้ง OpenLDAP Server บน Fedora 13</a></li>
<li><a href="http://www.squid-cache.org/Versions/v3/3.2/manuals/basic_ldap_auth.html" target="_blank">squid: basic_ldap_auth</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2012/03/configure-squid-proxy-server-to-authenticate-user-with-ldap/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ล็อกอินผิดมากเกิน แบนซะด้วย fail2ban</title>
		<link>http://spalinux.com/2011/05/ban_failed_login_attempts_using_fail2ban</link>
		<comments>http://spalinux.com/2011/05/ban_failed_login_attempts_using_fail2ban#comments</comments>
		<pubDate>Sat, 07 May 2011 09:01:08 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[fail2ban]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1316</guid>
		<description><![CDATA[หากเซิร์ฟเวอร์ของคุณตั้งอยู่บนอินเตอร์เน็ต เพื่อให้บริการเว็บไซต์หรืออื่นๆ และคุณจำเป็นต้องเปิด SSH เพื่อสามารถ login เข้าไปตรวจสอบสถานะของเครื่องได้ คุณต้องเคยเจอปัญหานี้แน่นอน คือมีการพยายามเจาะระบบด้วยการ ssh เข้ามา ด้วย user, password ต่างๆ ที่คาดว่าจะมีในเครื่อง ตัวอย่างไฟล์ /var/log/secure แสดงถึงการพยายาม ssh แต่ไม่สำเร็จ May  2 16:39:27 server sshd[8309]: Invalid user john from x.x.x.x May  2 16:39:27 server sshd[8310]: input_userauth_request: invalid user john May  2 16:39:27 server sshd[8309]: pam_unix(sshd:auth): check pass; user unknown May  2 16:39:27 server sshd[8309]: pam_unix(sshd:auth): [...]]]></description>
			<content:encoded><![CDATA[<p>หากเซิร์ฟเวอร์ของคุณตั้งอยู่บนอินเตอร์เน็ต เพื่อให้บริการเว็บไซต์หรืออื่นๆ และคุณจำเป็นต้องเปิด SSH เพื่อสามารถ login เข้าไปตรวจสอบสถานะของเครื่องได้</p>
<p>คุณต้องเคยเจอปัญหานี้แน่นอน คือมีการพยายามเจาะระบบด้วยการ ssh เข้ามา ด้วย user, password ต่างๆ ที่คาดว่าจะมีในเครื่อง</p>
<p><span id="more-1316"></span></p>
<p>ตัวอย่างไฟล์ /var/log/secure แสดงถึงการพยายาม ssh แต่ไม่สำเร็จ</p>
<pre>May  2 16:39:27 server sshd[8309]: Invalid user john from x.x.x.x
May  2 16:39:27 server sshd[8310]: input_userauth_request: invalid user john
May  2 16:39:27 server sshd[8309]: pam_unix(sshd:auth): check pass; user unknown
May  2 16:39:27 server sshd[8309]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=x.x.x.x
May  2 16:39:27 server sshd[8309]: pam_succeed_if(sshd:auth): error retrieving information about user john
May  2 16:39:29 server sshd[8309]: Failed password for invalid user john from x.x.x.x port 3291 ssh2
May  2 16:39:29 server sshd[8310]: Received disconnect from x.x.x.x: 11: Bye Bye</pre>
<pre> </pre>
<p>การป้องกัน หรือคำแนะนำอย่างแรก ที่พึงกระทำคือ ตั้ง user, password ให้ยากต่อการคาดเดา แต่เท่านั้นอาจไม่เพียงพอ</p>
<p>บทความนี้ขอเสนอวิธีการป้องกันด้วย fail2ban ซึ่งจะช่วยแบน ip ที่พยายาม login แต่ไม่สำเร็จได้</p>
<p>หลักการทำงานคร่าวๆ คือ โปรแกรม fail2ban จะคอยตรวจสอบไฟล์ที่ตั้งไว้ โดยค้นหาคำหรือประโยคที่เกิดขึ้นในไฟล์ เมื่อมีการ failed attempts เกิดขึ้นถึงจำนวนครั้งที่กำหนด fail2ban จะทำการบางอย่างที่ตั้งไว้ เช่น รันคำสั่ง iptables เพื่อ DROP packets ได้</p>
<h4>ติดตั้งไฟล์ rpm</h4>
<p>ในที่นี้ทดสอบบน Fedora 14 ต้องติดตั้งไฟล์ rpm เพิ่มเติมดังนี้</p>
<pre>[root@fc14-x64 ~]# <strong>rpm -ivh gamin-python-0.1.10-8.fc14.x86_64.rpm
</strong>[root@fc14-x64 ~]# <strong>rpm -ivh shorewall-4.4.11.1-1.fc14.noarch.rpm
</strong>[root@fc14-x64 ~]# <strong>rpm -ivh fail2ban-0.8.4-25.fc14.noarch.rpm</strong></pre>
<p>เพื่อความชัดเจนในการทดสอบ จะรันคำสั่ง service iptables stop เพื่อเคลียร์ rule ของ iptables ทั้งหมดออกก่อน</p>
<pre>[root@fc14-x64 ~]# <strong>service iptables stop
</strong>iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]</pre>
<p>ใช้คำสั่ง iptables เพื่อแสดง rule ที่ใช้งานอยู่</p>
<pre>[root@fc14-x64 ~]# <strong>iptables -L -v -n
</strong>Chain INPUT (policy ACCEPT 46 packets, 3176 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain OUTPUT (policy ACCEPT 40 packets, 5272 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p>ใช้คำสั่ง service เพื่อสตาร์ต fail2ban</p>
<pre>[root@fc14-x64 ~]# <strong>service fail2ban start
</strong>Starting fail2ban:                                         [  OK  ]</pre>
<p>ใช้คำสั่ง ps เพื่อดูโปรเซสของ fail2ban</p>
<pre>[root@fc14-x64 ~]# <strong>ps -ef | grep fail2ban
</strong>root      1151     1  0 23:32 ?        00:00:00 /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock -x</pre>
<p>ดูในไฟล์ /var/log/messages แสดงการเริ่มต้นทำงานของ fail2ban</p>
<pre>[root@fc14-x64 ~]# <strong>tail /var/log/messages</strong></pre>
<pre>May  6 23:35:32 fc14-x64 fail2ban.server : INFO   Changed logging target to SYSLOG for Fail2ban v0.8.4
May  6 23:35:32 fc14-x64 fail2ban.jail   : INFO   Creating new jail 'ssh-iptables'
May  6 23:35:32 fc14-x64 fail2ban.jail   : INFO   Jail 'ssh-iptables' uses Gamin
May  6 23:35:32 fc14-x64 fail2ban.filter : INFO   Added logfile = /var/log/secure
May  6 23:35:32 fc14-x64 fail2ban.filter : INFO   Set maxRetry = 5
May  6 23:35:32 fc14-x64 fail2ban.filter : INFO   Set findtime = 600
May  6 23:35:32 fc14-x64 fail2ban.actions: INFO   Set banTime = 600
May  6 23:35:32 fc14-x64 fail2ban.jail   : INFO   Jail 'ssh-iptables' started</pre>
<p>ดีฟอลต์คอนฟิกไฟล์ rpm ที่ได้มาจาก Fedora 14 จะตรวจสอบเฉพาะการพยายาม SSH โดยดูในไฟล์ /var/log/secure</p>
<p>fail2ban จะสร้าง rule และ chain ใหม่ขึ้นมา ใน iptables</p>
<pre>[root@fc14-x64 ~]# <strong>iptables -L -v -n
</strong>Chain INPUT (policy ACCEPT 92 packets, 7885 bytes)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;"><strong>   53  4052 fail2ban-SSH  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22</strong></span></pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain OUTPUT (policy ACCEPT 84 packets, 8087 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain <strong><span style="color: #0000ff;">fail2ban-SSH</span> </strong>(1 references)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;"><strong>   53  4052 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0</strong></span></pre>
<p> </p>
<h4>ไฟล์คอนฟิกของ fail2ban</h4>
<p>ไฟล์คอนฟิกของ fail2ban อยู่ใน /etc/fail2ban/</p>
<p>ไฟล์ /etc/fail2ban/jail.conf จะเป็นการกำหนดการทำงานของ fail2ban</p>
<p>คอนฟิกบางส่วนจากไฟล์ jail.conf</p>
<pre>[root@fc14-x64 ~]# <strong>cat /etc/fail2ban/jail.conf</strong>
...
[DEFAULT]
...
# "bantime" is the number of seconds that a host is banned.
<span style="color: #0000ff;">bantime  = 600</span></pre>
<pre># A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
<span style="color: #0000ff;">findtime  = 600
</span>...
[ssh-iptables]</pre>
<pre>enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=root, sender=fail2ban@mail.com]
logpath  = /var/log/secure
maxretry = 5</pre>
<p>คำอธิบายคร่าวๆ จากไฟล์ jail.conf คือ ให้ตรวจสอบ (filter) การ ssh โดยดูในไฟล์ (logpath) /var/log/secure ว่า ถ้ามีการ failed login เกิน (maxretry) ครั้ง ภายในเวลา (findtime) 600 วินาที ให้ทำการ (action) รัน iptables ด้วยออปชั่นในวงเล็บ</p>
<p>สามารถดูรายละเอียด filter ในไฟล์ /etc/fail2ban/filter.d/sshd.conf</p>
<pre>[root@fc14-x64 ~]# <strong>cat /etc/fail2ban/filter.d/sshd.conf
</strong>...
[Definition]</pre>
<pre>_daemon = sshd
...
failregex = ^%(__prefix_line)s(?:error: PAM: )?Authentication failure for .* from &lt;HOST&gt;\s*$
            ^%(__prefix_line)s(?:error: PAM: )?User not known to the underlying authentication module for .* from &lt;HOST&gt;\s*$
            ^%(__prefix_line)sFailed (?:password|publickey) for .* from &lt;HOST&gt;(?: port \d*)?(?: ssh\d*)?$
            ^%(__prefix_line)sROOT LOGIN REFUSED.* FROM &lt;HOST&gt;\s*$
            ^%(__prefix_line)s[iI](?:llegal|nvalid) user .* from &lt;HOST&gt;\s*$
            ^%(__prefix_line)sUser \S+ from &lt;HOST&gt; not allowed because not listed in AllowUsers$
            ^%(__prefix_line)sauthentication failure; logname=\S* uid=\S* euid=\S* tty=\S* ruser=\S* rhost=&lt;HOST&gt;(?:\s+user=.*)?\s*$
            ^%(__prefix_line)srefused connect from \S+ \(&lt;HOST&gt;\)\s*$
            ^%(__prefix_line)sAddress &lt;HOST&gt; .* POSSIBLE BREAK-IN ATTEMPT!*\s*$
            ^%(__prefix_line)sUser \S+ from &lt;HOST&gt; not allowed because none of user's groups are listed in AllowGroups$</pre>
<p>คำอธิบายจากไฟล์คอนฟิก filter คือ ให้ตรวจสอบหาคำหรือประโยคที่ระบุไว้ใน failregex (regular expression)</p>
<p>ส่วน action ที่ทำ หลังจาก maxretry ดูได้ในไฟล์  /etc/fail2ban/action.d/iptables.conf</p>
<pre>[root@fc14-x64 ~]# <strong>cat /etc/fail2ban/action.d/iptables.conf
</strong>...
actionban = iptables -I fail2ban-&lt;name&gt; 1 -s &lt;ip&gt; -j DROP
...
actionunban = iptables -D fail2ban-&lt;name&gt; -s &lt;ip&gt; -j DROP</pre>
<p>คำอธิบาย คือการรันคำสั่ง iptables เพื่อสร้าง rule เพื่อจะ ban &lt;ip&gt; ที่พยายามเข้ามา</p>
<p>และเมื่อเกินเวลา (bantime) 600 วินาที แล้ว ก็รันคำสั่ง iptables เพื่อลบ rule ที่สร้างขึ้น อนุญาตให้ &lt;ip&gt; สามารถเข้ามาได้อีกครั้ง</p>
<h4>ทดสอบการ login fail</h4>
<p><span style="color: #ff0000;"><strong>คำเตือน </strong>การทดลองใช้ fail2ban ให้ลองบนเครื่องที่สามารถ console ได้  เพราะการคอนฟิกหรือลอง login fail จะทำให้คุณไม่สามารถ ssh เข้าเครื่องนั้นได้ ช่วงเวลาหนึ่ง</span></p>
<p>สมมุติให้ลอง ssh login fail ด้วย root จาก 192.168.1.12</p>
<p>ดูไฟล์ /var/log/secure ที่เกิดขึ้นบน server</p>
<pre>[root@fc14-x64 ~]#<strong> tail -f /var/log/secure
</strong>May  6 23:43:47 fc14-x64 unix_chkpwd[1340]: password check failed for user (root)
May  6 23:43:47 fc14-x64 sshd[1338]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.12  user=root
May  6 23:43:49 fc14-x64 sshd[1338]: Failed password for root from 192.168.1.12 port 34811 ssh2</pre>
<pre>May  6 23:43:56 fc14-x64 unix_chkpwd[1342]: password check failed for user (root)
May  6 23:43:58 fc14-x64 sshd[1338]: Failed password for root from 192.168.1.12 port 34811 ssh2</pre>
<pre>May  6 23:44:00 fc14-x64 unix_chkpwd[1343]: password check failed for user (root)
May  6 23:44:01 fc14-x64 sshd[1338]: Failed password for root from 192.168.1.12 port 34811 ssh2
May  6 23:44:01 fc14-x64 sshd[1339]: Connection closed by 192.168.1.12
May  6 23:44:01 fc14-x64 sshd[1338]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.12  user=root</pre>
<pre>May  6 23:44:16 fc14-x64 unix_chkpwd[1348]: password check failed for user (root)
May  6 23:44:16 fc14-x64 sshd[1346]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.12  user=root
May  6 23:44:19 fc14-x64 sshd[1346]: Failed password for root from 192.168.1.12 port 34812 ssh2</pre>
<pre>May  6 23:44:22 fc14-x64 unix_chkpwd[1349]: password check failed for user (root)
May  6 23:44:25 fc14-x64 sshd[1346]: Failed password for root from 192.168.1.12 port 34812 ssh2
May  6 23:44:25 fc14-x64 sshd[1347]: Connection closed by 192.168.1.12
May  6 23:44:25 fc14-x64 sshd[1346]: PAM 1 more authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.12  user=root</pre>
<p>เมื่อ login fail เกินค่า maxretry ที่ตั้งไว้ ในที่นี้คือ 5 ครั้ง สังเกตในไฟล์ /var/log/messages  โปรแกรม fail2ban จะทำการ action ขึ้นมา เพื่อ ban 192.168.1.12</p>
<pre>[root@fc14-x64 ~]# <strong>tail /var/log/messages
</strong>May  6 23:44:27 fc14-x64 &lt;28&gt;<span style="color: #0000ff;">fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.1.12</span></pre>
<p>ใช้คำสั่ง iptables เพื่อตรวจสอบ rule ที่เพิ่มขึ้น</p>
<pre>[root@fc14-x64 ~]# <strong>iptables -L -v -n
</strong>Chain INPUT (policy ACCEPT 61 packets, 10699 bytes)
 pkts bytes target     prot opt in     out     source               destination
  453 37587 fail2ban-SSH  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           <span style="color: #0000ff;"><strong>tcp dpt:22</strong></span></pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain OUTPUT (policy ACCEPT 60 packets, 7044 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain fail2ban-SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination
<span style="color: #0000ff;"><strong>    2   120 DROP       all  --  *      *       192.168.1.12            0.0.0.0/0
</strong></span>  451 37467 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0</pre>
<p>สังเกตว่า rule ที่สร้างขึ้น จะ DROP เฉพาะการ ssh (tcp 22) เท่านั้น แปลว่า ip ที่ถูก ban ไป จะไม่สามารถ ssh เข้าเครื่อง server ได้อีก แต่ยังสามารถเปิด web (tcp 80) หรืออื่นๆ ได้ตามปกติ</p>
<p>คอนฟิกในไฟล์ jail.conf เมื่อมีการ ban เกิดขึ้น จะมีการส่ง mail หา root ด้วย</p>
<p>ตัวอย่าง mail ที่ส่งให้ root</p>
<pre>From fail2ban@mail.com  Fri May  6 23:44:28 2011
Return-Path: &lt;fail2ban@mail.com&gt;
Date: Fri, 6 May 2011 23:44:27 +0700
Subject: [Fail2Ban] SSH: banned 192.168.1.12
From: Fail2Ban &lt;fail2ban@mail.com&gt;
To: <a href="mailto:root@fc14-x64.example.com">root@fc14-x64.example.com</a>
Status: R</pre>
<pre>Hi,</pre>
<pre>The IP 192.168.1.12 has just been banned by Fail2Ban after 5 attempts against SSH.
...</pre>
<p>เมื่อเวลาผ่านไป เกินเวลา bantime ในที่นี้ 600 วินาที  โปรแกรม fail2ban ก็จะปลดการ ban ออก ดูได้จากไฟล์ /var/log/messsages</p>
<pre>[root@fc14-x64 ~]# <strong>tail /var/log/messages
</strong>May  6 23:44:27 fc14-x64 &lt;28&gt;fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.1.12
May  6 23:54:28 fc14-x64 &lt;28&gt;<span style="color: #0000ff;">fail2ban.actions: WARNING [ssh-iptables] Unban 192.168.1.12</span></pre>
<p>ใช้คำสั่ง iptables เพื่อตรวจสอบ rule ที่ใช้งานอยู่ หลังจาก unban</p>
<pre>[root@fc14-x64 log]# <strong>iptables -L -v -n
</strong>Chain INPUT (policy ACCEPT 561 packets, 50572 bytes)
 pkts bytes target     prot opt in     out     source               destination
 1753  141K fail2ban-SSH  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain OUTPUT (policy ACCEPT 501 packets, 50410 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<pre>Chain fail2ban-SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination
 1751  141K RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0</pre>
<p> </p>
<p>ลองนำไปใช้ดูครับ เพิ่มความปลอดภัยให้เครื่องได้อีกระดับ</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://www.fail2ban.org/" target="_blank">Fail2ban</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2011/05/ban_failed_login_attempts_using_fail2ban/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>คอนฟิก Solaris IP Filter</title>
		<link>http://spalinux.com/2010/05/configure_solaris_ip_filter</link>
		<comments>http://spalinux.com/2010/05/configure_solaris_ip_filter#comments</comments>
		<pubDate>Sat, 22 May 2010 08:46:27 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Solaris]]></category>
		<category><![CDATA[ip filter]]></category>
		<category><![CDATA[solaris 10]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1104</guid>
		<description><![CDATA[บทความนี้กล่าวถึงวิธีการใช้คำสั่ง ipf เพื่อสร้าง rule ของ Solaris IP Filter แบบง่ายๆ เพื่อใช้กำหนด packet เข้าออกเครื่อง Solaris IP Filter เป็นโปรแกรม Firewall ที่ควบคุม packet เข้า/ออก เครื่องที่ติดตั้ง Solaris ได้ ipf เป็นคำสั่งที่ใช้ในการแก้ไขกฎ (rule) ของ Solaris IP Filter หมายเหตุ ตัวอย่างในบทความนี้ทดสอบบนเครื่องที่ติดตั้ง Solaris 10 10/09 s10x_u8wos_08a X86 ทดสอบกับเครื่องที่สามารถคอนโซลหน้าจอได้เท่านั้น เพราะถ้าคอนฟิก rule ของ Firewall ผิดพลาด อาจทำให้คุณไม่สามารถ remote เช่น Secure Shell ได้ 1. เปิด (Start) เซอร์วิส ipfilter โดยดีฟอลต์จากการติดตั้ง [...]]]></description>
			<content:encoded><![CDATA[<p>บทความนี้กล่าวถึงวิธีการใช้คำสั่ง ipf เพื่อสร้าง rule ของ Solaris IP Filter แบบง่ายๆ เพื่อใช้กำหนด packet เข้าออกเครื่อง</p>
<p><span id="more-1104"></span></p>
<p>Solaris IP Filter เป็นโปรแกรม Firewall ที่ควบคุม packet เข้า/ออก เครื่องที่ติดตั้ง Solaris ได้</p>
<p>ipf เป็นคำสั่งที่ใช้ในการแก้ไขกฎ (rule) ของ Solaris IP Filter</p>
<p>หมายเหตุ</p>
<ul>
<li>ตัวอย่างในบทความนี้ทดสอบบนเครื่องที่ติดตั้ง Solaris 10 10/09 s10x_u8wos_08a X86</li>
<li>ทดสอบกับเครื่องที่สามารถคอนโซลหน้าจอได้เท่านั้น เพราะถ้าคอนฟิก rule ของ Firewall ผิดพลาด อาจทำให้คุณไม่สามารถ remote เช่น Secure Shell ได้</li>
</ul>
<h4>1. เปิด (Start) เซอร์วิส ipfilter</h4>
<p>โดยดีฟอลต์จากการติดตั้ง เซอร์วิส IP Filter จะถูกปิดการใช้งาน</p>
<p>ใช้คำสั่ง svcs ตรวจสอบสถานะ</p>
<pre># <strong>svcs -av | grep ipfilter
</strong>disabled       -             15:25:36      - svc:/network/ipfilter:default</pre>
<p>ใช้คำสั่ง svcadm เพื่อเปิดการเซอร์วิส</p>
<pre># <strong>svcadm enable ipfilter</strong></pre>
<pre># <strong>svcs -av | grep ipfilter
</strong>online         -             15:27:14      - svc:/network/ipfilter:default</pre>
<h4>2. เปิดคุณสมบัติ (Enable) IP Filter</h4>
<p>ขั้นต่อมา ใช้คำสั่ง ipf -E เพื่อเปิดคุณสมบัติ IP Filter</p>
<pre># <strong>ipf -E</strong></pre>
<h4>3. ทดลองการสร้าง rule แบบง่ายๆ</h4>
<p>สร้างไฟล์ /etc/ipf/ipf.conf เพื่อกำหนด rule ที่ต้องการ</p>
<p>ตัวอย่างเช่น ต้องการสร้าง rule เพื่อกำหนดให้เฉพาะเครื่องที่มี IP 192.168.1.2 เท่านั้นที่สามารถ ping (ICMP) เข้ามาที่เครื่องทางพอร์ต e1000g0 ได้</p>
<pre># <strong>cat /etc/ipf/ipf.conf</strong></pre>
<pre>block in on e1000g0 proto icmp from any to any
pass in on e1000g0 proto icmp from 192.168.1.2/32 to any</pre>
<p>หลังจากสร้างไฟล์เรียบร้อย พิมพ์คำสั่ง ipf -f ตามด้วยไฟล์ ipf.conf เพื่อเริ่มการบังคับใช้ rule ที่สร้างขึ้น</p>
<pre># <strong>ipf -f /etc/ipf/ipf.conf</strong></pre>
<p>*** 4. ลบ (Flush) rule ออกไป ***</p>
<p>หากต้องการยกเลิก rule ที่สร้างขึ้น ใช้คำสั่ง ipf -F เพื่อ ลบ (flush) rule ที่มีออกไป</p>
<pre># <strong>ipf -F a</strong></pre>
<h4>5. ปิดคุณสมบัติ (Disable) IP Filter</h4>
<p>ใช้คำสั่ง ipf -D เพื่อปิดคุณสมบัติ IP Filter</p>
<pre># <strong>ipf -D</strong></pre>
<h4>ตัวอย่าง rule</h4>
<p>มีตัวอย่างการสร้างไฟล์ ipf.conf เพื่อกำหนด rule แบบต่างๆ /usr/share/ipfilter/examples/</p>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li>man ipf</li>
<li><a href="http://docs.sun.com/app/docs/doc/816-4554/eupsq?l=en&amp;a=view" target="_blank">System Administration Guide: Solaris IP Filter (Overview)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/05/configure_solaris_ip_filter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>แก้ไขคอนฟิก iptables firewall rule</title>
		<link>http://spalinux.com/2009/07/customize_iptables_firewall_rules</link>
		<comments>http://spalinux.com/2009/07/customize_iptables_firewall_rules#comments</comments>
		<pubDate>Sat, 18 Jul 2009 06:40:45 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[iptables]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=842</guid>
		<description><![CDATA[การติดตั้งลีนุกซ์ส่วนใหญ่รวมทั้ง fedora จะติดตั้ง และเปิดคุณสมบัติ firewall มาด้วย ซึ่งโดยดีฟอลต์แล้ว จะอนุญาตให้ packet ขาเข้า (INPUT) ได้เฉพาะ ping และ SSH เท่านั้น ลีนุกซ์จะใช้โปรแกรมที่ชื่อ iptables ทำหน้าที่เป็น firewall และใช้คำสั่งนี้ในการแสดงและแก้ไขคอนฟิก (rules) ของ firewall ตัวอย่างการใช้คำสั่ง iptables เพื่อแสดง rule [root@fc11-64min ~]# iptables -L -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes)  pkts bytes target     prot opt in     out     source               destination    42  2148 ACCEPT     all  [...]]]></description>
			<content:encoded><![CDATA[<p>การติดตั้งลีนุกซ์ส่วนใหญ่รวมทั้ง fedora จะติดตั้ง และเปิดคุณสมบัติ firewall มาด้วย ซึ่งโดยดีฟอลต์แล้ว จะอนุญาตให้ packet ขาเข้า (INPUT) ได้เฉพาะ ping และ SSH เท่านั้น</p>
<p>ลีนุกซ์จะใช้โปรแกรมที่ชื่อ iptables ทำหน้าที่เป็น firewall และใช้คำสั่งนี้ในการแสดงและแก้ไขคอนฟิก (rules) ของ firewall</p>
<p><span id="more-842"></span></p>
<p>ตัวอย่างการใช้คำสั่ง iptables เพื่อแสดง rule</p>
<pre>[root@fc11-64min ~]# <strong>iptables -L -v
</strong>Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   42  2148 ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
<span style="color: #0000ff;">    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere
</span>    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere
<span style="color: #0000ff;">    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
</span>    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain OUTPUT (policy ACCEPT 63 packets, 7412 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p>ถ้าเราติดตั้งและคอนฟิกโปรแกรมเพิ่มเติมบนเครื่องนี้ เช่น ลงเว็บเซิร์ฟเวอร์ จะทำให้ไม่สามารถเรียกมาจากเครื่องอื่นๆ ได้ นอกจากต้องแก้ไขคอนฟิกของ firewalll ก่อน</p>
<p>บน Fedora จะมี script ชื่อ system-config-firewall-tui ช่วยในการคอนฟิกของ iptables เช่น ถ้าต้องการอนุญาตให้ เครื่องอื่นสามารถเรียกเว็บเซิร์ฟเวอร์ที่รันพอร์ตดีฟอลต์ http (TCP 80) ได้ ต้องแก้ไขดังนี้</p>
<pre>[root@fc11-64min ~]# <strong>system-config-firewall-tui</strong></pre>
<pre>                                      ┌────────────┤ Firewall Configuration ├────────────┐
                                      │                                                  │
                                      │ A firewall protects against unauthorized         │
                                      │ network intrusions. Enabling a firewall blocks   │
                                      │ all incoming connections. Disabling a firewall   │
                                      │ allows all connections and is not recommended.   │
                                      │                                                  │
                                      │              Firewall: [*] Enabled               │
                                      │                                                  │
                                      │    ┌────┐      ┌───────────┐      ┌────────┐     │
                                      │    │ OK │      │ <strong><span style="color: #0000ff;">Customize</span></strong> │      │ Cancel │     │
                                      │    └────┘      └───────────┘      └────────┘     │
                                      │                                                  │
                                      │                                                  │
                                      └──────────────────────────────────────────────────┘</pre>
<p>กด [tab] เพื่อเลื่อนไปที่ &#8220;Customize&#8221;</p>
<pre>                                 ┌───────────────────┤ Trusted Services ├────────────────────┐
                                 │                                                           │
                                 │ Here you can define which services are trusted. Trusted   │
                                 │ services are accessible from all hosts and networks.      │
                                 │                                                           │
                                 │           [ ] RADIUS                        ↑             │
                                 │           [*] SSH                           ▒             │
                                 │           [ ] Samba                         ▒             │
                                 │           [ ] Samba Client                  ▒             │
                                 │           [ ] Secure WWW (HTTPS)            ▮             │
                                 │           <strong><span style="color: #0000ff;">[*] WWW (HTTP)</span></strong>                    ↓             │
                                 │                                                           │
                                 │                                 ┌─────────┐   ┌───────┐   │
                                 │                                 │ Forward │   │ <strong><span style="color: #0000ff;">Close</span></strong> │   │
                                 │                                 └─────────┘   └───────┘   │
                                 │                                                           │
                                 │                                                           │
                                 └───────────────────────────────────────────────────────────┘</pre>
<p>ในหน้า &#8220;Trusted Services&#8221; กดปุ่มลูกศรเพื่อเลื่อนลงมาบรรทัด [x] WWW (HTTP) แล้วกดปุ่ม [space-bar] เพื่อเลือก แล้วกด &#8220;Close&#8221;</p>
<p>หน้าจอจะขึ้นถามให้ บันทึกคอนฟิกใหม่ เลือก [Yes]</p>
<pre>                                         ┌────────────────┤ Warning ├─────────────────┐
                                         │                                            │
                                         │ Clicking the 'Yes' button will override    │
                                         │ any existing firewall configuration. Are   │
                                         │ you sure that you want to do this?         │
                                         │                                            │
                                         │ Please remember to check if the services   │
                                         │ iptables and ip6tables are enabled.        │
                                         │                                            │
                                         │        ┌─────┐              ┌────┐         │
                                         │        │ <strong><span style="color: #0000ff;">Yes</span></strong> │              │ No │         │
                                         │        └─────┘              └────┘         │
                                         │                                            │
                                         │                                            │
                                         └────────────────────────────────────────────┘</pre>
<p>ใช้คำสั่ง iptables อีกครั้ง เพื่อแสดงคอนฟิก firewall ปัจจุบัน หลังการแก้ไข</p>
<pre>[root@fc11-64min ~]# <strong>iptables -L -v
</strong>Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   11   752 ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
<span style="color: #0000ff;"><strong>    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:http
</strong></span>    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited</pre>
<pre>Chain OUTPUT (policy ACCEPT 10 packets, 1072 bytes)
 pkts bytes target     prot opt in     out     source               destination</pre>
<p>หลังจากอนุญาต http แล้ว ตอนนี้เครื่องอื่น ก็สามารถเรียกเว็บเซิร์ฟเวอร์ที่รันบนเครื่องนี้ได้แล้ว</p>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2009/07/customize_iptables_firewall_rules/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

