วันพุธที่ 24 มีนาคม พ.ศ. 2553

ไฟล์วอลล์บน freebsd ตั้งแต่ 6.4 เป็นต้นไป

บทความจาก san แห่งเวบ thaibsd

#kldload ipfw.ko

ค่าดีฟอล์คจะบล๊อคหมดทุกพอร์ต

ตั้งแต่เวอร์ชั่น 6.4 ขึ้นไปสามารถ โหลดที่ kernel ได้เลยไม่ต้องคอมไล์เคอร์แนลก็ได้ ให้ไปใส่ค่า
firewall_enable="YES" ที่ rc.conf

#ee /etc/rc.conf

#### FIREWALL #####
firewall_enable="YES"
#firewall_type="/etc/myrules.conf" -->เขียนกฏไฟล์วอล์เพื่อควบคุม เปิดและปิดพอร์ตเฉพาะที่จะใช้งาน
firewall_type="OPEN"
firewall_quiet="YES"
firewall_logging="YES"

#### NAT #####
natd_enable="YES"
natd_flags="-s -u -m"
natd_interface="interface to nat"

#myrules.conf
ipfw add 100 divert 8668 ip4 from any to any via "nat interface" <--- เปิด NAT
ipfw add 200 allow ip from any to any via lo0
ipfw add 300 deny ip from any to 127.0.0.0/8
ipfw add 400 deny ip from 127.0.0.0/8 to any

ipfw add 500 allow me to any 80 <--WEB
ipfw add 500 allow any to me 22 <--SSH

แก้ไขปัญหา apache ล้มโดยใช้ squid ช่วย

บทความของท่าน yim แห่งเวบไทยบีเอสดี...เกี่ยวกับการทำsquid ช่วย apache ป้องกันเวบล้ม
กรณีทีเว็บมีโหลดมาก ๆ เรามีวิธีการง่าย ๆ ในการลดโหลด apapche ไม่ให้ทำงานหนักจนเกินไปจนเว็บล่ม

ขั้นแรกลง squid ตามปกติ ผมเลือกใช้ squid 2.6.STABLE23

ทำการคอมพาย
cd squid-2.6.STABLE23
--prefix=/usr --exec_prefix=/usr --bindir=/usr/sbin --sbindir=/usr/sbin --libexecdir=/usr/lib/squid --sysconfdir=/etc/squid --localstatedir=/var/spool/squid --datadir=/usr/share/squid --enable-async-io --with-pthreads --enable-storeio=ufs,aufs,diskd,null --enable-removal-policies=lru,heap --enable-snmp --enable-htcp --enable-cache-digests --enable-referer-log --enable-auth=basic,digest,ntlm --enable-carp --enable-follow-x-forwarded-for --with-large-files --enable-useragent-log --enable-ipf-transparent

make
make all
make install

เสร็จแล้ว squid.conf จะอยู่ที่ /etc/squid/squid.conf
แก้เป็น

offline_mode on
http_port 8888 transparent
icp_port 3130

#============================================================$
# LOGFILE PATHNAMES AND CACHE DIRECTORIES
#============================================================$

cache_dir diskd /data/cache 300 128 128 Q1=72 Q2=64
cache_mem 100 MB
cache_replacement_policy heap LFUDA
memory_replacement_policy heap GDSF

logformat TLOG %{%d-%m-%Y %H:%M:%S}tl %6tr %>a %Ss/%03Hs %cache_access_log /var/log/squid/access.log TLOG
cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
pid_filename /tmp/squid.pid
logfile_rotate 2
debug_options ALL,1
buffered_logs off
emulate_httpd_log off

#============================================================$
# FTP section
#============================================================$
ftp_user anonymous
ftp_list_width 32
ftp_passive on
ftp_sanitycheck on




#===================================================================$
#DEFAULT ACL
#===================================================================$
acl all src 0.0.0.0/0.0.0.0
#acl all src all
acl manager proto cache_object
acl localhost src 127.0.0.0/8
acl to_localhost dst 127.0.0.0/8
acl systemnetworkcare src 192.168.0.0/16 # RFC1918 possible internal network
acl reverse_tomcatapplication dst xxxxxx # แก้ xxx เป็น IPADDRESS SERVER
acl SSL_ports port 443 # https
acl SSL_ports port 563 # snews
acl SSL_ports port 873 # rsync
acl Safe_ports port 80 # http
acl Safe_ports port 81 # http
acl Safe_ports port 31 # http
acl Safe_ports port 32 # http
acl Safe_ports port 35 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl Safe_ports port 631 # cups
acl Safe_ports port 873 # rsync
acl Safe_ports port 901 # SWAT
acl SSL_ports port 8888 # https
acl CONNECT method CONNECT
acl SSL method CONNECT
miss_access allow all
http_access allow reverse_tomcatapplication
http_access allow all
miss_access allow reverse_tomcatapplication
http_reply_access allow all
never_direct allow SSL all
http_access allow all
http_access allow localhost
http_access allow to_localhost
http_access allow Safe_ports
http_access allow CONNECT SSL_ports
http_access allow systemnetworkcare
icp_access allow systemnetworkcare
icp_access allow all

acl QUERY urlpath_regex cgi-bin download files login member
cache deny QUERY
#============================================================$
# Parameter Administratif $
#============================================================$
cache_mgr admin@thaigqsoft.com
cache_effective_user nobody
cache_effective_group nobody
visible_hostname ICache
unique_hostname admin@thaigqsoft.com

#============================================================$
# SNMP
#============================================================$

quick_abort_min 0 KB
quick_abort_max 0 KB
quick_abort_pct 98
negative_ttl 3 minutes
positive_dns_ttl 53 seconds
negative_dns_ttl 29 seconds
forward_timeout 4 minutes
connect_timeout 2 minutes
peer_connect_timeout 1 minutes
pconn_timeout 120 seconds
shutdown_lifetime 10 seconds
read_timeout 15 minutes
client_lifetime 60 minutes
half_closed_clients off

อย่าลืมแก้ acl reverse_tomcatapplication dst xxxxxx เป็นไอพีเว็บ server

เสร็จแล้วใส่ ipfw
ipfw add 137 fwd 202.143.148.36,8888 tcp from any to 202.143.148.36 80 via fxp0
ipfw add 138 fwd 202.143.148.36,8888 tcp from 127.0.0.1 to any 80 via fxp0
ipfw add 139 fwd 202.143.148.36,80 tcp from any to 202.143.148.36 8888 via fxp0

fxp0 คือแลนที่แจกเว็บ
สามบรรทัดนี้จะกันไม่ให้คนอื่นมาแอบใช้ proxy เราโดยตรง
จบแล้วสั่งรัน squid
/usr/sbin/squid

ปล 202.143.148.36 คือไอพี เว็บ server

วันเสาร์ที่ 20 มีนาคม พ.ศ. 2553

ป้องกัน root ssh บน centos

บทความจากอินเตอร์เน็ตจำคนเขียนไม่ได้ขออภัยไม่ได้เอ่ยนามนะครับ..

วิธีการ Set SSH ไม่ให้ root login เข้ามา
วันนี้จะมาสอน security เบื้องต้นของ linux ฮับ

นั่นคือ การไม่ให้ Root Access เครื่องโดยตรง แต่ต้องผ่าน user คนอื่นก่อน

มาดูวิธีกันเลยดีกว่า

[root@valkyrie ~]# useradd
เพื่อสร้าง user ใหม่ครับ ใช้ในการที่เอาไว้ login ssh แทน root

[root@valkyrie ~]# passwd
ทีนี้ก็กำหนด Password ให้มันครับ (แนะนำให้คนละอันกับ root)

[root@valkyrie ~]# vi /etc/ssh/sshd_config
เข้าไปแก้ไข sshd_config ครับ
ตั้ง PermitRootLogin เป็น no
ซึ่งในตอนแรก มันจะปรับเป็น yes แล้ว comment ด้วย # ไว้ เอาออกด้วยนะครับ
แล้วเลื่อนลงมาล่างสุดครับ ให้พิมพ์บรรทัดนี้เพิ่มเข้าไป
AllowUsers
แล้ว save ไฟล์ ปิด ออกมานะครับ

ทีนี้ สั่ง service sshd restart เพื่อสั่งรี service sshd

ระหว่างนี้ ห้ามปิด SSH นี้ก่อนนะครับ เพราะเรายังไม่รู้ว่า config ที่เราใช้ มีปัญหาทำให้ login ไม่ได้หรือไม่
ถ้าไม่ทดสอบก่อน อาจจะต้องเดินไปแก้ไฟล์หน้าเครื่องได้นะครับ เพราะจะ ssh ไม่ได้อีกเลย
ดังนั้นให้เปิด terminal ssh เข้าไปยัง server อีกครั้งนะครับ แล้วลอง login ด้วย root ดู
จะพบว่า มัน login ไม่ได้
ให้ปิด terminal นั้นแล้วเปิด ssh ไปอีกหน้าหนึ่ง แล้วทดสอบ login ด้วย user ที่เราสร้างไปเมื่อตอนแรก
ถ้าได้ ก็ ok ครับ ฉลุย ผ่าน

ถ้าเราต้องการใช้สิทธิ Root
คือเนื่องจาก user ที่สร้างใหม่นั้น มันจะไม่มีสิทธิเทียบเท่า Root ดังนั้น วิธีการที่จะทำให้เราเข้าไปใน Root ได้ นั่นคือคำสั่ง
[newusers@valkyrie ~]$ su -
มันจะให้พิมพ์ password root เราก็พิมพ์ root

จบแล้วครับ ^^”

นี่ก็เป็น Security พื้นฐาน ในการทำ ssh นะครับ ลองๆเอาไปใช้ดูละกันครับ

วันศุกร์ที่ 19 มีนาคม พ.ศ. 2553

การแก้ไขระบบพังบน freebsd

บทความจาก mr.T แห่งเวบไทยบีเอสดี..ครับ
วันนี้เครื่อง server มีปัญหา ไม่สามารถใช้งานได้ เนื่องจากว่าไม่สามารถ Mount พาทิชั่นของระบบได้ ไปค้นหาวิธีแก้ ทำไปทำมาทำถูก เลยมาเขียนในกระทู้เพื่อเป็นแนวทางในการแก้ไขครับ
ระบบที่มีปัญหา
พอหลังจากที่รีบู๊ตแล้วจะเข้า single usr mode ซึ่งก็คือ
ระบบจะแจ้งประมาณดังนี้
Enter full pathname of shell or RETURN for /bin/sh:
พอเรากด Enter ระบบกดจะคืน Shell มาให้
#

เนื่องจากว่า / พาทิชั่นไม่สามารถ mount (/etc/fstab) ได้ จึงทำให้ไม่สามารถใช้คำสั่งใดๆ ได้ จากที่ค้นหาข้อมูลในเว็บบอร์ด มีเพียงคำแนะนำสั้นๆ

# mount -u / <---mount: not found
# mount -a <---mount: not found
#fsck -y
ระบบจะตอบกลับมาว่า mount: not found

จากในกรณีของผม
ที่ระบบตอบกลับมาว่าไม่เห็นคำสั่งนี้ เพราะ คำสั่ง mount อยู่ใน Path: /sbin/mount แต่เนื่องจากตอนนี้ระบบเราเสียหายไม่สามารถ mount พาทิชั่นใดๆได้ แม้แต่
Harddisk ที่ลงFreeBSDไว้ก็ถูกล๊อคเป็นแบบ Read Only
ls ดูจะเห็น เพียง / พาทิชั่น กับอีกพาทิชั่นนึง จำไม่ได้แล้ว จริงๆแล้ว / พาทิชั่นนี้ถูกโหลดจาก single user mode ซึ่งเป็นโหมดเอาไว้ Repair ระบบ มีคำสั่งที่จำเป็น
สำหรับใช้ Repair ระบบเท่านั้น ดังนั้น แนวทางแก้ไขคือ เราต้อง mount harddisk แบบแมนนวล

#cd /
#cd rescue <--ที่เก็บรวบรวมคำสั่งเอาไว้ Repair ระบบ
#/rescue/fsck -y /dev/ar0s1 <-- ให้ระบบมันซ่อมแซมตัวเองก่อน
#/rescue/mount /dev/ar0s1 <-- mount HDD ได้แล้ว
#/rescue/reboot

เสร็จแล้วครับ เราซ่อมแซมระบบเรียบร้อยแล้ว หลังจากรีบูต ระบบก็จะใช้งานได้ตามปรกติ