Thursday, August 2, 2012

Web Server Security: PHP Hardening

PHP is a server-side (web) scripting language to produce dynamic web pages, HTML per se is a static language.

php.ini is PHP's default configuration file usually located at /etc/php.ini on most of the Linux distributions. If you install PHP from source /etc/php.ini file path can be modified as part of compilation
./configure --with-config-file-path=/path/to/php.ini

php.ini has many PHP directives which can be used to secure web applications.

******************Configuration Start************************
;root of the PHP pages
doc_root = "/var/www/html:/etc/scripts/"

;directory under which PHP opens the script
user_dir = /etc/scripts

include_path =

;path to web root
;caution, include all directories which you use 
open_basedir = /var/www/html

save_path =

;disable global variables
register_globals = Off

track_errors = yes
display_errors = Off

;will hide PHP version information
expose_php = Off

;remove few functions based on your requirement
disable_functions = php_uname, getmyuid, getmypid, passthru, leak, listen, diskfreespace, tmpfile, link, ignore_user_abord, shell_exec, dl, set_time_limit, exec, system, highlight_file, source, show_source, fpaththru, virtual, posix_ctermid, posix_getcwd, posix_getegid, posix_geteuid, posix_getgid, posix_getgrgid, posix_getgrnam, posix_getgroups, posix_getlogin, posix_getpgid, posix_getpgrp, posix_getpid, posix, _getppid, posix_getpwnam, posix_getpwuid, posix_getrlimit, posix_getsid, posix_getuid, posix_isatty, posix_kill, posix_mkfifo, posix_setegid, posix_seteuid, posix_setgid, posix_setpgid, posix_setsid, posix_setuid, posix_times, posix_ttyname, posix_uname, proc_open, proc_close, proc_get_status, proc_nice, proc_terminate, phpinfo

disable_classes =

safe_mode = Off

use_trans_sid =

allow_url_fopen = Off

allow_url_include = Off

group_id = 100

magic_quotes_gpc = Off

;disable if files are not uploaded to Web server
file_uploads = On

upload_max_filesize =

;memory_limit is set to a very high value
;recommended value is 8M
memory_limit=128M

;set to a high value, server may lead to DoS
;recommended value is 2M
post_max_size = 8M

upload_tmp_dir =

user_id = 100

force_redirect = 1

cgi.force_redirect = 1

auto_prepend_file =
auto_append_file =

;Disable Remote File Includes
allow_url_fopen = Off
allow_url_include = Off

;session.cookie_httponly = 1
;session.referer_check = your_url.tld
;session.cookie_secure = 1
******************Configuration End************************

HTTP Response Headers for Mitigating Web Hacks is inline with current blog post, might be useful to some of you.

To test php.ini configuration for security issues download PHPSecInfo, security auditing tool.
http://phpsec.org/projects/phpsecinfo/phpsecinfo.zip

Uncompress the archive to web server's root directory (say, /var/www/html) and access the URL as given below
https://testserver.com/phpsecinfo/phpsecinfo-20070406/index.php
NOTE: If php.ini is not used PHPSECINFO will try to read values from default configuration or httpd.conf/ lighttpd.conf

Below is an example snapshot giving notice on probable improper configuration.




Below snapshot gives warning on insecure configuration.




Snapshot showing "Tests not run" and Results Summary page.



To view Web server configuration and PHP configuration, write piece of code with phpinfo() API (application programming interface) and host on webservers root directory.

*********praveend.php************
root@praveend:~# cat praveend.php

<?
phpinfo();
?>
root@praveend:~#
*********praveend.php************

Access praveend.php as shown in below snapshot.



Below links might be useful for securing Web Servers running PHP scripts.
http://php.net/manual/en/index.php
http://www.madirish.net/node/229
http://phpsec.org/projects/guide/

Wednesday, July 25, 2012

Port/Service Scanning using SNMP

Simple Network Management Protocol (SNMP) is used for remote monitoring and managing of hosts, routers, switches or any device connected to network SNMP works on 161/UDP, SNMP Trap on 162/UDP.

By default SNMP comes with two community strings
        public (read only access)
        private (read/write access)

Community strings or User names  with read only access rights can also be used to Scan a machine remotely for open TCP/UDP ports. The community string which I am using is "mysnmp" with read/write permissions.


Below snapshot gives information about process/service names running on the machine.


Evading IDS/IPS
Generally we use NMAP for scanning a remote machine to figure out open TCP or UDP ports. Most of the IDS/IPS might detect the Scans and flag an alert. SNMP scan might evade IDS/IPS because we are sending a legitimate SNMP request to remote devices.

Protection
Remove unnecessary MIBs which are not being used.

Other articles which might be of interest
http://darshanams.blogspot.in/2010/11/wireshark-remote-packet-capture-bit-of.html
http://darshanams.blogspot.in/2012/05/cain-and-abel-password-cracking.html

Enjoy !!!

Wednesday, July 4, 2012

Bittorrent: Detection Mechanisms

Bittorrent is a Peer to Peer(P2P) protocol used to share files and data in internet. In this article we are going to see what are the different mechanisms which can be used to detect and block Bittorrent communication across network.

Below snapshot shows HTTP request which Bittorrent Client uses to communicate with Bittorrent Servers. In the request we can see different parameters like client id, port number.


We can observe that Bittorrent client uses different User Agent request headers like BTWebClient, Bittorrent etc.



UDP is using Source port as 24615, which is being communicated to Bittorrent Server as HTTP Request.


So we can detect or block Bittorrent based on HTTP Request URI or User-Agent header strings or UDP protocol communication.

Below are few Signatures which we can use to detect Bittorrent.

alert udp any any -> any any ( msg: "Bit Torrent UDP"; content:"41 02"; offset:0; depth:2; content:"38"; offset:13;depth:1; content:"08"; offset:21;depth:1; sid:8888881; rev: 1; )
alert udp any any -> any any ( msg: "Bit Torrent Protocol"; content:"|13|bittorrent|20|protocol";  offset:0; sid:8888882; rev: 1; )
alert udp any any -> any any ( msg: "Bit Torrent UDP Communication"; content:"d1|3a|ad2|3a|id20|3a|"; sid:8888883; rev: 1; )  
 

Below Signature triggers and alerts when the content is seen in HTTP Request URI
alert tcp any any -> any 80 ( msg: "Bit Torrent: HTTP Request"; content:"info_hash=";  pcre:"/announce\?info_hash=/Ui"; sid:8888884; rev: 1; )


Observed following User-Agent strings as part of HTTP Requests
        User-Agent: BitTorrent/7610(27328)
        User-Agent: BTWebClient/7610(27328)
which can be detected using following Signatures
alert tcp any any -> any 80 ( msg: "Bit Torrent: HTTP User Agent 1"; content:"User-Agent: BitTorrent";  sid:8888885; rev: 1; ) 
alert tcp any any -> any 80 ( msg: "Bit Torrent: HTTP User Agent 2"; content:"User-Agent: BTWebClient";  sid:8888886; rev: 1; ) 

Posts related to Snort IDS/IPS which might be of interest to you.
http://darshanams.blogspot.in/2011/05/snort-logging-alerts-to-syslog-server.html
http://darshanams.blogspot.in/2010/06/snort-preprocessors-and-alerts.html
http://darshanams.blogspot.in/2012/05/installing-snort-from-source-as-ips.html

To know about Google Talk Jabber protocol communication
http://darshanams.blogspot.in/2008/11/deciphering-google-talk-jabber.html

For Zebra/Bittorrent protocol communication
http://darshanams.blogspot.in/2009/03/i-was-just-checking-my-mails.html

P.S Signatures given above are neither tested nor fine tuned. Just an 
idea how to detect Bit Torrent communication.

Friday, June 22, 2012

SIP Security1: Scanning VoIP/PBX Servers


To find out a device/machine which is providing VoIP service, scan for ports 5060/5061 on both UDP and TCP. By default VoIP devices run on UDP 5060 port.

We can use tools like SIPVicious svmap.py or SIPSCAN(windows only).
svmap.py is a CLI (Command Line Interface) tool whereas SIPSCAN is a GUI (Graphical User Interface) tool. SIPSCAN is username enumerator rather than a Scanner.


Both the tools support OPTIONS, INVITE and REGISTER methods to find User Agent Server (UAS). But svmap.py can be used to pass any SIP method, infact we can pass invalid argument to detect the VoIP Server.


By default SIPSCAN tries with following extensions/usernames
thisisthecanary, test, echo, admin, dave, 101 to 110 excluding 109, 201 to 210 excluding 209, 401 to 410 excluding 409 and 501 to 510 excluding 509.




While scanning with SIPSCAN leave "Target SIP Domain" as default value to example.com or add domain or IP of your SIP Server. If you leave "Target SIP Domain" blank will not show  any results but with



Happy Week end :-) !!!

Monday, June 18, 2012

svwar.py: Bad user = SIP/2.0 401 - svwar will probably not work!

Running sipwar.py from SIPVicious was throwing below error

#/home/praveen/sipvicious-0.2.7#./svwar.py --force -e100-110 3.3.3.10
WARNING:TakeASip:Bad user = SIP/2.0 401  - svwar will probably not work!
WARNING:TakeASip:We got an unknown response
ERROR:TakeASip:Response: 'SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 127.0.0.1:5060;branch=z9hG4bK-2777087671;received=172.16.1.5;rport=5060\r\nFrom: "100";tag=3130300131353239383436353338\r\nTo: "100";tag=as4442a546\r\nCall-ID: 3403670806\r\nCSeq: 1 REGISTER\r\nServer: Asterisk PBX 1.8.13.0\r\nAllow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH\r\nSupported: replaces, timer\r\nWWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="5858df28"\r\nContent-Length: 0\r\n\r\n'
WARNING:root:found nothing

IP Address 3.3.3.10 is the Asterisk PBX Server.

Googled for the error but couldn'd find solution so thought of going through the SIP Configuration file /etc/asterisk/sip.conf where we can find below description.
    
When an incoming INVITE or REGISTER is to be rejected, for any reason, always reject with     '401 Unauthorized 'instead of letting the requester know whether there was a matching user or peer for their request .i.e. PBX server will always respond with "401 Unauthorized" error to not disclose user details.


Add below line to sip.conf, by default the value is yes.
alwaysauthreject = no

Restart Asterisk PBX for above changes to reflect.

$sudo /etc/init.d/asterisk /stop/start
            or

execute below commands
#asterisk -vvr
asterisk*CLI>sip reload

Now we can successfully run svwar.py which gives below output
#/home/praveen/sipvicious-0.2.7#./svwar.py  -e100-1010 3.3.3.10
| Extension | Authentication |
------------------------------
| 1004      | reqauth        |
| 102       | reqauth        |
| 101       | reqauth        |
| 1003      | reqauth        |
| 1002      | reqauth        |
| 1001      | reqauth        |

Articles which might be of interest 
Packet level view of REGISTER, SUBSCRIBE and NOTIFY methods
http://www.darshanams.blogspot.in/2012/06/x-lite-client-registering-with-pbx.html

Configuration and Registration of Softphone and Analog Phone( with ATA) and Asterisk PBX
http://www.darshanams.blogspot.in/2012/06/softphone-registration-with-asterisk.html

Thank You !!!