Tuesday 30 June 2015

Broke Authentication and Session Management

Authentication and session management includes all aspects of handling user authentication and managing active sessions. Authentication is a critical aspect of this process, but even solid authentication mechanisms can be undermined by flawed credential management functions, including password change, forgot my password; remember my password, account update, and other related functions. User authentication on the web typically involves the use of a userid and password. If the session tokens are not properly protected, an attacker can hijack an active session and assume the identity of a user.


Web-based applications frequently use sessions to provide a friendly environment to their users. HTTP is a stateless protocol, which means that it provides no integrated way for a web server to maintain states throughout user’s subsequent requests. In order to overcome this problem, web servers – or sometimes web applications – implement various kinds of session management. The basic idea behind web session management is that the server generates a session identifier (ID) at some early point in user interaction, sends this ID to the user’s browser and makes sure that this same ID will be sent back by the browser along with each subsequent request. Session IDs thereby become identification tokens for users, and servers can use them to maintain session data (e.g., variables) and create a session-like experience to the users
There are three widely used methods for maintaining sessions in web environment:
URL arguments, hidden form fields and cookies. While each of them has its benefits and shortcomings, cookies have proven to be the most convenient and also the least insecure of the three
When authentication functions related to the application are NOT implemented correctly which will allow hackers to compromise passwords or session ID's or to exploit other implementation flaws using other users credentials
Following are the attacks an attacker can perform on the application.
· Session Hijacking
· Session Fixation
· Session Timeout
· Session Replay
Prevention of Broke Authentication and Session Management
Password Strength - passwords should have restrictions that require a minimum size and complexity for the password. Complexity typically requires the use of minimum combinations of alphabetic, numeric, and/or non-alphanumeric characters in a user’s password (e.g., at least one of each). Users should be required to change their password periodically. Users should be prevented from reusing previous passwords.
Password Use - Users should be restricted to a defined number of login attempts per unit of time and repeated failed login attempts should be logged.
Password Change Controls - A Users should always be required to provide both their old and new password when changing their password. If forgotten passwords are emailed to users, the system should require the user to re authenticate whenever the user is changing their e-mail address, otherwise an attacker who temporarily has access to their session can simply change their e-mail address and request a ‘forgotten’ password be mailed to them.
Password Storage - All passwords must be stored in either hashed or encrypted form to protect them from exposure, regardless of where they are stored. Hashed form is preferred since it is not reversible. Encryption should be used when the plaintext password is needed, such as when using the password to login to another system. Passwords should never be hard coded in any source code. Decryption keys must be strongly protected to ensure that they cannot be grabbed and used to decrypt the password file.
Protecting Credentials in Transit - The only effective technique is to encrypt the entire login transaction using something like SSL. Simple transformations of the password such as hashing it on the client prior to transmission provide little protection as the hashed version can simply be intercepted and retransmitted even though the actual plaintext password might not be known.
Session ID Protection – Ideally, a user’s entire session should be protected via SSL. If this is done, then the session ID (e.g., session cookie) cannot be grabbed off the network, which is the biggest risk of exposure for a session ID. If SSL is not viable for performance or other reasons then session IDs themselves must be protected in other ways. First, they should never be included in the URL as they can be cached by the browser, sent in the referrer header, or accidentally forwarded to a ‘friend’. Session IDs should be long, complicated, random numbers that cannot be easily guessed. Session IDs can also be changed frequently during a session to reduce how long a session ID is valid. Session IDs must be changed when switching to SSL, authenticating, or other major transitions. Session IDs chosen by a user should never be accepted.
Browser Caching – Authentication and session data should never be submitted as part of a GET, POST should always be used instead. Authentication pages should be marked with all varieties of the no cache tag to prevent someone from using the back button in a user’s browser to backup to the login page and resubmit the previously typed in credentials. Many browsers now support the AUTOCOMPLETE=OFF flag to prevent storing of credentials in autocomplete caches.

Friday 26 June 2015

HTTP - Methods

The set of common methods for HTTP/1.1 is defined below and this set can be expanded based on requirements. These method names are case sensitive and they must be used in uppercase.

GET
The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
HEAD
Same as GET, but transfers the status line and header section only.
POST
A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
PUT
Replaces all current representations of the target resource with the uploaded content.
DELETE
Removes all current representations of the target resource given by a URI.
CONNECT
Establishes a tunnel to the server identified by a given URI.The following example requests a connection with a web server running on the host infosecaffairs.com:
CONNECT www.infosecaffairs.com HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
The connection is established with the server and the following response is sent back to the client:
HTTP/1.1 200 Connection established
Date: Mon, 27 Jul 2015 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
OPTIONS
Describes the communication options for the target resource.The following example requests a list of methods supported by a web server running on infosecaffairs.com:
OPTIONS * HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
The server will send an information based on the current configuration of the server, for example:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2015 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: httpd/unix-directory
TRACE
Performs a message loop-back test along the path to the target resource.The TRACE method is used to echo the contents of an HTTP Request back to the requester which can be used for debugging purpose at the time of development
TRACE / HTTP/1.1
Host: www.infosecaffairs.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
The server will send the following message in response to the above request:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Connection: close
Content-Type: message/http
Content-Length: 39
TRACE / HTTP/1.1
Host: www.infosecaffairs.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

Cookies, Cookies Attributes and Its Importance

Cookies are pieces of information stored on the client side, which are sent to the server with every request made by the client. Cookies are generally used by web sites to track users’ personal preferences so that personalized content can be served to the user. Once the cookie is stored on the
client, the client browser automatically submits this name-value pair every time the user goes to the same site.


Cookies are primarily used for authentication and maintaining sessions. Hence, securing a cookie effectively means securing a user’s identity. Cookies can be secured by properly setting cookie attributes. These attributes are:
Secure
Domain
Path
HTTPOnly
Expires

Secure
One of the simplest and most common ways to steal data, including cookies, is sniffing. Sniffing can be defined as passively reading data that is being transmitted. In order to overcome this problem, we encrypt data before transmission. Encryption of data ensures that any potential attacker who sniffs traffic will not be able to steal clear text data, thus ensuring their safety.
However, many applications encrypt only the login page and other sensitive pages. Other requests such as those for image files are sent to the server using non-encrypted communication. But as cookies are also transmitted along with these requests, an attacker sniffing on a network will be able to steal session information from these cookies. Also, some sites allow access over HTTP as well as HTTPS. In cases like these, it becomes important to make sure the cookie is transmitted only over HTTPS connections and not HTTP. This can be done with the help of the ‘Secure’ attribute of a cookie.
The ‘Secure’ attribute makes sure that the cookie will only be sent with requests made over an encrypted connection and an attacker won’t be able to steal cookies by sniffing. However, we need to be very careful while setting this attribute,we must ensure that the response used to set the ‘Secure’ attribute is sent using an encrypted channel.
Domain and Path
The ‘domain‘ attribute signifies the domain for which the cookie is valid and can be submitted with every request for this domain or its subdomains. If this attribute is not specified, then the hostname of the originating server is used as the default value.
The ‘path‘ attribute signifies the URL or path for which the cookie is valid. The default path attribute is set as ‘/’.‘domain’ and ‘path’ cookie attributes must be properly set in an environment where subdomains and subfolders host different applications
HTTPOnly
When this attribute is set, client-side scripts are not allowed to access the cookie.
Expires
This attribute is used to set persistent cookies. It signifies how long the browser should use the persistent cookie and when the cookie should be deleted.
If this attribute is not specified, then the lifetime of the cookie is the same as that of browser session, i.e. it will be a non-persistent cookie.
we must ensure that the ‘Expires’ attribute is not set for a cookie containing sensitive information
References
http://paladion.net/blogs/

Prevention Techniques: Cross-site request forgery (CSRF)

1. The best defense against CSRF attacks is unpredictable tokens, a piece of data that the server can use to validate the request, and wh...