Thursday 27 February 2014

Nmap truyền kì - Hồi 2 - Port Scanning Techniques Overview

Port Scanning Techniques: nmap có rất nhiều phương pháp để sử dụng. Nhưng chỉ có một phương pháp được sử dụng tại một thời điểm, ngoại trừ UDP scan (-sU) có thể kết hợp với bất kì một trong các loại TCP scan. Các loại scan ở dạng -s<C>, <C> thường là kí tự đầu tiên của loại scan đó (ngoại trừ FTP bounce scan (-b)).
Ta sẽ đi lướt qua từng loại kĩ thuật scanning mà nmap cung cấp, cụ thể như thế nào thì sẽ giới thiệu sau.
-sS : TCP SYN Stealth - Đây là phương pháp phổ biến nhất vì tốc độ của nó khá nhanh. Khả năng ẩn danh tốt hơn connect scan, và làm việc với tất cả chức năng của TCP stacks (không giống như scan với mục đích đặc biệt như FIN scan)
-sT : TCP Connect - Phương pháp này sử dụng system call connect(), thay vì sử dụng raw packet như các phương pháp khác, phương pháp này được sử dụng bởi unprivileges user và đối với các target ipv6 vì SYN scan không hoạt động trong trường hợp này.
-sU : UDP - Không nên bỏ qua các udp port, rất nhiều lỗ hổng bảo mật.
-sF, -sX, -SN : TCP FIN, Xmas, NULL - Các loại scan này với mục đích đặc biệt để vượt qua fw thăm dò hệ thống phía sau chúng. Thật không may chúng dựa trên hành vi của hệ thống thường là không biểu hiện ra.
-sA : TCP ACK - được sử dụng để tìm ra fw ruleset. Nó giúp xác định fw rule ở dạng stateful hay không. Điểm yếu của nó là không phân biệt được open với closed port.
-sW : TCP Window - cũng giống như ACK scan, ngoại trừ nó có khả năng phát hiện port open với closed port.
-sM : TCP Maimon - loại này giống với FIN scan, nhưng có thêm ACK flag. Cho phép nó vượt qua nhiều packet filtering fw, điểm yếu là nó làm làm được việc với ít hệ thống hơn FIN scan.
-SI <zombie host> : TCP Idle - là loại khó phát hiện nhất trong tất cả, và nó có thể exploit trusted IP relationships. Không may nó cũng rất chậm và phức tạp.
-sO : IP Protocol - xác định giao thức IP nào (TCP, ICMP, IGMP ...) được hỗ trợ bởi target. Đây đơn giản không phải là port scan, vì nó thay đổi con số protocol trong IP header thay vì port của TCP hay UDP. Nó có thể sử dụng option -p để chọn protocol number sẽ được scan.
-b : TCP FTP bounce - this deprecated scan type tricks FTP servers into performing port scans by proxy. Most FTP servers are now patched to prevent this, but it is a good way to sneak through restrictive fw when it works.

OK, Vậy bây giờ ta sẽ chọn port nào để thực hiện scan đây? Mặc định, nmap scan 1000 port phổ biến nhất trong file nmap-services tương ứng với loại giao thức nó được yêu cầu. Cách khác, ta có thể chỉ định -F (fast) để chọn 100 port phổ biến nhất đối với từng loại giao thức hay --top-ports để chỉ ra số lượng port để scan. Khi ta muốn chỉ định một port range nào đó, hay một số port nhất định thôi, ta có thể sử dụng option -p.
-p 22 : chỉ định một port duy nhất là port 22.
-p ssh : chỉ định bằng tên, khi sử dụng tên có thể khớp với nhiều port khác.
-p 22,25,80 : chỉ định nhiều port chung với nhau. Chú ý, khi không có giao thức nào được chỉ định, tất cả các port được chỉ định sẽ được sử dụng cho bất kì phương pháp nào được chỉ định trên command-line. Nếu TCP scan sử dụng là SYN scan (-sS), port 22,25 và 80 sẽ được scan tương ứng với các dịch vụ SSH, SMTP và HTTP. Nếu protocol scan (-sO), đó sẽ là 3 IP protocol tương ứng với XNS IDP, Leaf-1 và ISO-IP.

-p80-85,443,8000-8005 : chỉ đinh port range, các port tương ứng sẽ là 80,81,82,83,84,85,443,8000,8001,8002,8003,8004,8005.
-p-100,60000- : tương ứng với 1-100, 60000-65535.
-p- : tất cả các port (trừ port 0)
-pT:21,23,110,U:53,111,137,161 : phân loại từng loại port cho từng giao thức.
-p http* : ta có thể sử dụng wildcard để tìm tất cả các port bắt đầu bằng http như http(80) http-mgmt(280), https(443), http-proxy(8080).
-p 1-1023,[1024-] : để port trong ngoặc kép để chỉ định các port chỉ được scan khi nó nằm trong file nmap-services. Trong trường hợp này, sẽ scan tất cả các port từ 1-1023, và các port cao được đăng kí trong file nmap-services.

Wednesday 26 February 2014

Nmap truyền kì - Hồi 1 - Host Discovery

List scan là một dạng suy biến của host dicovery, nó đơn giản chỉ liệt kê các host trên network được chỉ định mà không cần gửi bất kì gói tin nào đến target.
List scan là hành động kiểm tra đúng đắn đảm bảo rằng bạn có các địa chỉ IP của đúng target. Nếu hiện ra các domain name mà bạn không nhận ra được thì nên tìm thêm để tránh việc scan nhầm mạng của công ty khác.
Một trong những điểm mạnh của list scan là stealth ( tránh được sự chú ý không mong muốn từ IDS). Trong những trường hợp target chú ý đến tất cả reverse-DNS request, chúng ta có thể sử dụng anonymous recursive DNS server bằng cách sử dụng --dns-servers.
List scan không thể sử dụng chung với các hàm cao cấp hơn như port scanning, OS detecttion, ping scanning.

-sP (Ping scan)
default = -PE -PA
Chỉ thực hiện ping, và in ra các host hồi đáp lại. Không thực hiện thêm các hàm kiểm tra khác như (OS detect, port scanning), ngoại trừ Nmap Scripting Engine (--script) và traceroute probing (--traceroute). Đây là một bước xâm nhập xa hơn list scan, và có thể sử dụng cùng chung mục đích. Đây là việc thực hiện do thám (light reconnisance) network của target nhanh chóng mà không thu hút nhiều sự chú ý.
SysAdmin có thể sử dụng option này một cách hữu ích. Nó có thể được sử dụng để đếm các máy available trên mạng hay monitor server availability. Hành động này được gọi là "ping sweep", đáng tin tưởng hơn là ping địa chỉ broadcast vì nhiều host không trả lời broadcast query.
-sP gửi ICMP echo request và TCP-ACK vào port 80 (default). Vì một số unprivileged Unix user (Window user without WinPcap installed) không thể gửi được raw packet, vì thế SYN được thay thế (-PS). Gói SYN được gửi đi sử dụng TCP connect() system call đến port 80 của target. Khi privileged user thực hiện scan target trên local ethernet network, ARP request (-PR) được sử dụng nếu không có option --send-ip được sử dụng.

.::Host Discovery techniques::.
Nếu chỉ định option -P... option này sẽ thay thế default discovery probé thay vì add thêm vào.

TCP SYN PING (-PS<port-list>)
-PS gửi đi gói tin TCP rỗng được set SYN flag. Port mặc định là 80.
Bình thường khi port closed, gói tin RST được gửi trả về. Khi port open, gói tin SYN/ACK được gửi trả về. Khi đó, nmap sẽ đóng kết nối mới chuẩn bị mở bằng cách gửi gói RST thay vì gói ACK để hoàn thành 3-way-handshake và thiêt lập full connection.
Ở đây nmap không quan tâm port mở hay đóng. Chỉ cần biết gói trả về là RST hay SYN/ACK để xác đinh target up hay down.
Trên Unix, chỉ có user privileged root mới có thể tạo gửi và nhận gói TCP raw. Với unprivileged user, phương pháp thay thế được thực hiện bằng system call connect() được khởi tạo với target port.

TCP ACK PING (-PA<port-list>)
Giống với SYN Ping, nhưng khác ở chỗ gửi đi gói ACK để đồng ý thiết lập kết nối, nhưng không có kết nối đó tồn tại, do đó target luôn trả lời bằng gói RST, làm lộ diện sự tồn tại của target.
-PA sử dụng default port 80. Nếu unprivileged user thử option này, hoặc IPv6 được chỉ định, system call connect() được thảo luận ở phía trên được sử dụng. Phương pháp thay thế này không hoàn hảo vì connect() thực chất gửi đi gói SYN thay vì gói ACK.
Mục đích của việc cung cấp cả SYN và ACK ping probe làm tăng cơ hội bypass fw. Hầu hết các admin cấu hình router và các fw khác block incomming SYN ngoại trừ các dịch vụ public như website hay mail server.

UDP Ping (-PU<port-list>)
gửi gói UDP rỗng (nếu không sử dụng --data-length để chỉ định độ dài của dữ liệu) đến một port xác định. Nếu không chỉ định port, port mặc định là 31,338. Chỉ định port cao không phổ biến được khuyến khích ở đây vì khi gửi đến port open thường không mong muốn cho loại scan này.
Dựa trên việc gửi vào port close của target, nếu target up, nó sẽ trả về gói tin ICMP port unreachable. Có nhiều loại ICMP error, như host/network unreachable hay TTL exceeded chỉ ra host down hay unreachable. Việc không trả về kết quả có thể xảy ra theo cách này, khi gửi gói tin UDP rỗng vào port open, hầu hết các service sẽ ignore gói tin đó, và tất nhiên sẽ không có gói tin nào sẽ được trả về.
Lợi ích của kĩ thuật này chính là có khả năng bypass fw cũng như filter chỉ quan tâm đến TCP.

ICMP Ping Type (-PE, -PP, -PM)
-PE nmap sẽ gửi gói tin imcp type 8 (echo request) đến target, và hy vọng nhận về icmp type 0 (echo reply) từ host available. Nhưng nhiều host và firewall block loại scan này, dẫn đến kết quả có thể không chính xác.
Trong khi echo request được xem như là dạng chuẩn của ICMP ping query, nhưng nmap không dừng lại ở đây. ICMP standard (RFC 792) cũng chỉ định các loại request khác như: timestamp request (code 13), information request (code 15), address mask request (code 17). Nmap hiện tại chưa hỗ trợ gói tin information request vì nó chưa được hỗ trợ rộng rãi (RFC 1122 nhấn mạnh "a host SHOULD NOT implement these messages").
-PP sẽ gửi gói icmp timestamp request. ICMP code 14 sẽ trả về nếu host available
-PM sẽ gửi gói icmp address mask request. ICMP code 18 sẽ trả về nếu host available

IP Protocol Ping (-PO<protocol-list>)
gửi gói tin IP mà protocol number được set trong IP header. Nêu không có protocol nào được chỉ định, mặc định sẽ gửi gói IP với ICMP (protocol 1), IGMP (protocol 2), IP-in-IP (protocol 4). Chú ý đối với ICMP, IGMP, TCP (protocol 6), UDP (protocol 17), the packets are sent with the proper protocol headers while other protocols are sent with no additional data beyond the IP header(unless the --data-length option is specified)
Phương pháp này tìm kiếm những trả lời sử dùng cùng chung protocol khi probe, hay ICMP unreachable message chỉ ra dấu hiệu protocol không được hỗ trợ. Cả 2 dấu hiệu đó điều chỉ ra là host available.

ARP Scan (-PR)
Một trong những môi trường sử dụng Nmap nhiều nhất là LAN. Khi nmap thử gửi raw IP như là ICMP echo request, OS phải xác định được địa chỉ ARP của target IP do đó

--send-ip chỉ định nmap gửi gói tin ở cấp độ IP (chứ không phải raw ethernet) ngay cả khi ta ở trong mạng local.
















Monday 24 February 2014

Secure webserver

1. Hide Apache Version and OS identity errors
+ ServerSignature Off
+ ServerTokens Prod

2. Disable Directory Listing
<Directory /var/www/html>
    Options -Indexes
</Directory>

3. Disable Unnecessary Modules

4. Run Apache as separate User and Group

5. Use Allow and Deny to Restrict access to Directories
<Directory />
   Options None
   Order deny,allow
   Deny from all
</Directory>

6. Use mod_security and mod_evasive Modules to Secure Apache

7. Disable Apache’s following of Symbolic Links
Options -FollowSymLinks
Note: To enable rewrite rules inside “.htaccess” file “AllowOverride All” should be present in the main configuration globally.

8. Turn off Server Side Includes and CGI Execution
<Directory "/var/www/html/web1">
Options -Includes -ExecCGI
</Directory>
Here are some other values with can be turned On or off with Options directive.
Options All – To enable All options at once. This is the default value, If you don’t want specify any values explicitly in Apache conf file or .htaccess.
Options IncludesNOEXEC – This option allows server side includes without the execute permission to a command or cgi files.
Options MultiViews – Allows content negotiated multiviews with mod_negotiation module.
Options SymLinksIfOwnerMatch – It’s similar to FollowSymLinks. But, this will follow only when the owner is the same between the link and the original directory to which it is linked.


9. Limit Request Size
<Directory "/var/www/myweb1/user_uploads">
   LimitRequestBody 512000
</Directory>

10. Protect DDOS attacks and Hardening
+ TimeOut : This directive allows you to set the amount of time the server will wait for certain events to complete before it fails. Its default value is 300 secs. It’s good to keep this value low on those sites which are subject to DDOS attacks. This value totally depends on kind of request you are getting on your website. Note: It could pose problems with come CGI scripts.
+ MaxClients : This directive allows you to set the limit on connections that will be served simultaneously. Every new connection will be queued up after this limit. It is available with Prefork and Worker both MPM. The default value of it is 256.
+KeepAliveTimeout : Its the amount of time the server will wait for a subsequent request before closing the connection. Default value is 5 secs.
+ LimitRequestFields : It helps us to set a limit on the number of HTTP request’s header fields that will be accepted from the clients. Its default value is 100. It is recommended to lower this value if DDos attacks are occurring as a result of so many http request headers.
+ LimitRequestFieldSize : It helps us to set a size limit on the HTTP Request header.

11. Enable Apache Logging
enable mod_log_config

12. Securing Apache with SSL Certificates


Friday 21 February 2014

RPM practical example

Please remember you must be root user when installing packages in Linux, with the root privileges you can manage rpm commands with their appropriate options.

1. How to Check an RPM Signature Package

Always check the PGP signature of packages before installing them on your Linux systems and make sure its integrity and origin is OK. Use the following command with –checksig (check signature) option to check the signature of a package called pidgin.
[root@tecmint]# rpm --checksig pidgin-2.7.9-5.el6.2.i686.rpm

pidgin-2.7.9-5.el6.2.i686.rpm: rsa sha1 (md5) pgp md5 OK

2. How to Install an RPM Package

For installing an rpm software package, use the following command with -i option. For example, to install an rpm package called pidgin-2.7.9-5.el6.2.i686.rpm.
[root@tecmint]# rpm -ivh pidgin-2.7.9-5.el6.2.i686.rpm

Preparing...                ########################################### [100%]
   1:pidgin                 ########################################### [100%]
RPM command and options
  1. -i : install a package
  2. -v : verbose for a nicer display
  3. -h: print hash marks as the package archive is unpacked.

3. How to check dependencies of RPM Package before Installing

Let’s say you would like to do a dependency check before installing or upgrading a package. For example, use the following command to check the dependencies of BitTorrent-5.2.2-1-Python2.4.noarch.rpm package. It will display the list of dependencies of package.
[root@tecmint]# rpm -qpR BitTorrent-5.2.2-1-Python2.4.noarch.rpm

/usr/bin/python2.4
python >= 2.3
python(abi) = 2.4
python-crypto >= 2.0
python-psyco
python-twisted >= 2.0
python-zopeinterface
rpmlib(CompressedFileNames) = 2.6
RPM command and options
  1. -q : Query a package
  2. -p : List capabilities this package provides.
  3. -R: List capabilities on which this package depends..

4. How to Install a RPM Package Without Dependencies

If you know that all needed packages are already installed and RPM is just being stupid, you can ignore those dependencies by using the option –nodeps (no dependencies check) before installing the package.
[root@tecmint]# rpm -ivh --nodeps BitTorrent-5.2.2-1-Python2.4.noarch.rpm

Preparing...                ########################################### [100%]
   1:BitTorrent             ########################################### [100%]
The above command forcefully install rpm package by ignoring dependencies errors, but if those dependency files are missing, then the program will not work at all, until you install them.

5. How to check an Installed RPM Package

Using -q option with package name, will show whether an rpm installed or not.
[root@tecmint]# rpm -q BitTorrent

BitTorrent-5.2.2-1.noarch

6. How to List all files of an installed RPM package

To view all the files of an installed rpm packages, use the -ql (query list) with rpm command.
[root@tecmint]# rpm -ql BitTorrent

/usr/bin/bittorrent
/usr/bin/bittorrent-console
/usr/bin/bittorrent-curses
/usr/bin/bittorrent-tracker
/usr/bin/changetracker-console
/usr/bin/launchmany-console
/usr/bin/launchmany-curses
/usr/bin/maketorrent
/usr/bin/maketorrent-console
/usr/bin/torrentinfo-console

7. How to List Recently Installed RPM Packages

Use the following rpm command with -qa (query all) option, will list all the recently installed rpm packages.
[root@tecmint]# rpm -qa --last

BitTorrent-5.2.2-1.noarch                     Tue 04 Dec 2012 05:14:06 PM BDT
pidgin-2.7.9-5.el6.2.i686                     Tue 04 Dec 2012 05:13:51 PM BDT
cyrus-sasl-devel-2.1.23-13.el6_3.1.i686       Tue 04 Dec 2012 04:43:06 PM BDT
cyrus-sasl-2.1.23-13.el6_3.1.i686             Tue 04 Dec 2012 04:43:05 PM BDT
cyrus-sasl-md5-2.1.23-13.el6_3.1.i686         Tue 04 Dec 2012 04:43:04 PM BDT
cyrus-sasl-plain-2.1.23-13.el6_3.1.i686       Tue 04 Dec 2012 04:43:03 PM BDT

8. How to List All Installed RPM Packages

Type the following command to print the all the names of installed packages on your Linux system.
[root@tecmint]# rpm -qa

initscripts-9.03.31-2.el6.centos.i686
polkit-desktop-policy-0.96-2.el6_0.1.noarch
thunderbird-17.0-1.el6.remi.i686

9. How to Upgrade a RPM Package

If we want to upgrade any RPM package “–U” (upgrade) option will be used. One of the major advantages of using this option is that it will not only upgrade the latest version of any package, but it will also maintain the backup of the older package so that in case if the newer upgraded package does not run the previously installed package can be used again.
[root@tecmint]# rpm -Uvh nx-3.5.0-2.el6.centos.i686.rpm
Preparing...                ########################################### [100%]
   1:nx                     ########################################### [100%]

10. How to Remove a RPM Package

To un-install an RPM package, for example we use the package name nx, not the original package name nx-3.5.0-2.el6.centos.i686.rpm. The -e (erase) option is used to remove package.
[root@tecmint]# rpm -evv nx

11. How to Remove an RPM Package Without Dependencies

The –nodeps (Do not check dependencies) option forcefully remove the rpm package from the system. But keep in mind removing particular package may break other working applications.
[root@tecmint]# rpm -ev --nodeps vsftpd

12. How to Query a file that belongs which RPM Package

Let’s say, you have list of files and you would like to find out which package belongs to these files. For example, the following command with -qf (query file) option will show you a file/usr/bin/htpasswd is own by package httpd-tools-2.2.15-15.el6.centos.1.i686.
[root@tecmint]# rpm -qf /usr/bin/htpasswd

httpd-tools-2.2.15-15.el6.centos.1.i686

13. How to Query a Information of Installed RPM Package

Let’s say you have installed an rpm package and want to know the information about the package. The following -qi (query info) option will print the available information of the installed package.
[root@tecmint]# rpm -qi vsftpd

Name        : vsftpd       Relocations: (not relocatable)
Version     : 2.2.2       Vendor: CentOS
Release     : 11.el6       Build Date: Fri 22 Jun 2012 01:54:24 PM BDT
Install Date: Mon 17 Sep 2012 07:55:28 PM BDT      Build Host: c6b8.bsys.dev.centos.org
Group       : System Environment/Daemons           Source RPM: vsftpd-2.2.2-11.el6.src.rpm
Size        : 351932                               License: GPLv2 with exceptions
Signature   : RSA/SHA1, Mon 25 Jun 2012 04:07:34 AM BDT, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://vsftpd.beasts.org/
Summary     : Very Secure Ftp Daemon
Description :
vsftpd is a Very Secure FTP daemon. It was written completely from
scratch.

14. Get the Information of RPM Package Before Installing

You have download a package from the internet and want to know the information of a package before installing. For example, the following option -qip (query info package) will print the information of a package sqlbuddy.
[root@tecmint]# rpm -qip sqlbuddy-1.3.3-1.noarch.rpm

Name        : sqlbuddy                     Relocations: (not relocatable)
Version     : 1.3.3                        Vendor: (none)
Release     : 1                            Build Date: Wed 02 Nov 2011 11:01:21 PM BDT
Install Date: (not installed)              Build Host: rpm.bar.baz
Group       : Applications/Internet        Source RPM: sqlbuddy-1.3.3-1.src.rpm
Size        : 1155804                      License: MIT
Signature   : (none)
Packager    : Erik M Jacobs
URL         : http://www.sqlbuddy.com/
Summary     : SQL Buddy â Web based MySQL administration
Description :
SQLBuddy is a PHP script that allows for web-based MySQL administration.

15. How to Query documentation of Installed RPM Package

To get the list of available documentation of an installed package, use the following command with option -qdf (query document file) will display the manual pages related to vmstat package.
[root@tecmint]# rpm -qdf /usr/bin/vmstat

/usr/share/doc/procps-3.2.8/BUGS
/usr/share/doc/procps-3.2.8/COPYING
/usr/share/doc/procps-3.2.8/COPYING.LIB
/usr/share/doc/procps-3.2.8/FAQ
/usr/share/doc/procps-3.2.8/NEWS
/usr/share/doc/procps-3.2.8/TODO

16. How to Verify a RPM Package

Verifying a package compares information of installed files of the package against the rpm database. The -Vp (verify package) is used to verify a package.
[root@tecmint downloads]# rpm -Vp sqlbuddy-1.3.3-1.noarch.rpm

S.5....T.  c /etc/httpd/conf.d/sqlbuddy.conf

17. How to Verify all RPM Packages

Type the following command to verify all the installed rpm packages.
[root@tecmint]# rpm -Va

S.5....T.  c /etc/rc.d/rc.local
.......T.  c /etc/dnsmasq.conf
.......T.    /etc/ld.so.conf.d/kernel-2.6.32-279.5.2.el6.i686.conf
S.5....T.  c /etc/yum.conf
S.5....T.  c /etc/yum.repos.d/epel.repo

18. How to Import an RPM GPG key

To verify RHEL/CentOS/Fedora packages, you must import the GPG key. To do so, execute the following command. It will import CentOS 6 GPG key.
[root@tecmint]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

19. How to List all Imported RPM GPG keys

To print all the imported GPG keys in your system, use the following command.
[root@tecmint]# rpm -qa gpg-pubkey*

gpg-pubkey-0608b895-4bd22942
gpg-pubkey-7fac5991-4615767f
gpg-pubkey-0f2672c8-4cd950ee
gpg-pubkey-c105b9de-4e0fd3a3
gpg-pubkey-00f97f56-467e318a
gpg-pubkey-6b8d79e6-3f49313d
gpg-pubkey-849c449f-4cb9df30

20. How To rebuild Corrupted RPM Database

Sometimes rpm database gets corrupted and stops all the functionality of rpm and other applications on the system. So, at the time we need to rebuild the rpm database and restore it with the help of following command.
[root@tecmint]# cd /var/lib
[root@tecmint]# rm __db*
[root@tecmint]# rpm --rebuilddb
[root@tecmint]# rpmdb_verify Packages