13 Error Handling and Logging
13 Error Handling and Logging
13
ERROR
HANDLING AND
LOGGING
PRACTICAL WEB DEFENSE COURSE
eLearnSecurity 2013 ©
All rights reserved. No part of this document may be reproduced in any form or by any electronic or
mechanical means, including information storage and retrieval systems, without written permission
from the publisher, except in the case of a reviewer, who may quote brief passages embodied in
critical articles or in a review.
eLearnSecurity s.r.l.
36,38 Via Matteucci
Pisa, ITALY 56124
P W D / P R A C T I C A L W E B D E F E N S E
1. TABLE OF CONTENTS
INTRODUCTION 1
1. ERROR HANDLING AND LOGGING 2
1.1 INTRODUCTION 2
1.2 WHAT THE PROBLEM IS 2
1.3 HOW CAN I SEE IF I AM VULNERABLE TO THIS? 4
1.3.1 Business logic flaws due to improper error handling 4
1.3.2 Platform error messages 4
1.3.3 Application error messages 4
1.3.4 Logging 5
1.4 HOW CAN I FIX THIS? 7
1.4.1 Fail Securely 7
1.4.2 Platform error messages 7
1.4.3 Application error messages 8
1.4.4 Logging 9
1.5 FURTHER READING 10
P W D / P R A C T I C A L W E B D E F E N S E
INTRODUCTION
I
INTRODUCTION
This module focuses on specific defense tactics to handle error conditions and logging.
Sooner or later all web applications face unexpected conditions, typically due to a
combination of input that was not anticipated but potentially also related to environment
factors such as server load. Regardless of the reason, web applications need to handle the
following very carefully:
• Handle errors gracefully
• Log errors so that they can be reviewed later
For example, most web attacks involve a combination of input that the application did not
anticipate, if there are no adequate logs in place (i.e. The POST body is not logged by
default on most web servers), it might not be possible to determine the root cause of the
problem, whether it is a security issue or not.
In addition to this, common misconfiguration errors such as displaying verbose error
messages on the screen may be of assistance to a prospective attacker.
1
1. ERROR HANDLING AND LOGGING
1.1 Introduction
All applications, no matter what size they are, will eventually crash or hit an error
condition, either in production or during User Acceptance Testing (UAT) as well as
development.
Error handling defines how these unexpected error conditions will be dealt with by the
application and platform in use.
Logging defines how traces of errors or other events may be saved into persistent storage
(i.e. generally files or a database) in order to be reviewed later.
Tools such as sqlmap1 will sometimes be significantly faster to exploit SQL injection
vulnerabilities when error messages are present:
• Netvolution referer header SQL injection vulnerability
[11:02:00] [INFO] testing 'AND boolean-based blind — WHERE or HAVING clause'
parsed error message(s) showed that the back-end DBMS could be Microsoft SQL Server.
Do you want to skip test payloads specific for other DBMSes? [Y/n] Y
[11:02:16] [INFO] testing 'Microsoft SQL Server/Sybase AND error-based
— WHERE or HAVING clause'
http://census-labs.com/news/2011/10/03/netvolution-referer-SQLi/
A good write-up about how error messages can be helpful to exploit SQL injection faster
can be found here:
• Speeding up Blind SQL Injections using Conditional Errors in MySQL
http://ha.xxor.se/2011/06/speeding-up-blind-sql-injections-using.html
1 http://sqlmap.org/
For example:
Error message Security implication
“The username you provided does not exist” User enumeration
“This record can only be viewed by the User enumeration, information leakage
following users: …..” that might be leveraged to attack such
users in order to escalate privileges
“You have a syntax error in your SQL query Attacker assistance to exploit SQL
near …” injection more easily
“Invalid <unsanitized input here>” Classic XSS vulnerability when displaying
user input in an error message without
output encoding first
“Saving file in /home/www/payroll/…” Reveals the underlying operating system
being a Unix derivative and location of
files
“The SQL query is: SELECT column1, Full SQL query, tables and columns
column2 …” revealed
1.3.4 Logging
Logging needs will vary from application to application and from organization to
organization. But generally speaking, if logging is insufficient to track a security incident,
then there will likely be a problem.
It is important to note here that a security incident may be as simple as a direct object
reference2: Having a user change the record ID to a different number, in order to try to
view what they should not be allowed to view. Testing of logging issues should try to
answer questions such as:
• Are security occurrences recorded? Even when they happen at the application
layer?
• Is information stored in the logs sufficient for security, compliance and root-cause
identification?
• Can logs be tampered? If the web server is hacked and the attacker is root can the
logs be trusted?
• Are logs archived for long enough?
The answer to this question must take into account average breach
detection rates:
According to the Verizon Data Breach Investigation Report, 66 percent of breaches took months
or even years to discover. And a recent Ponemon Institute report sponsored by Solera Networks, a
Blue Coat company, found that, on average, it is taking companies three months to discover a
malicious breach and then more than four months to resolve it
2 https://www.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References
http://www.darkreading.com/attacks-breaches/why-are-we-so-slow-to-
detect-data-breach/240156987
Verizon's latest annual "Data Breach Investigations Report" was not able to find a single
company it studied that was able to discover a breach within minutes or hours. Meanwhile, a
study from security firm Trustwave found the average time between breach and detection was 210
days.
http://www.scmagazine.com/it-decision-makers-are-more-optimistic-
about-breach-detection-than-they-should-be/article/299103/
display_startup_errors = Off
display_errors = Off
html_errors = Off
error_log = /path/to/error_log
• Research ways in which configuration may be bypassed and review them in the
application
For example, in PHP it may be possible to override php.ini settings via the ini_set4:
ini_set('display_errors', 'Off');
ini_set('html_errors', 'Off');
ini_set('error_log', 'path/to/error_log');
3 http://www.php.net/manual/en/errorfunc.configuration.php
4 http://www.php.net/ini_set
• Make it easy to swap environments and use separate files for development and
production
It can save a lot of time to have errors displayed on the screen during development,
ensuring that the mechanism to swap files from development to production is easy is a
guarantee, by itself, that procedures will be followed. If developers are forbidden error
messages on the screen they will find ways around this restriction to meet their (usually)
tight deadlines. Occasional debug messages will make it through to production more than
once if this principle is not followed.
1.4.4 Logging
The following guidelines should help to address this problem:
• Ensure all security occurrences are recorded
When in doubt: record it and err on the side of “record more”.
• Ensure the application has adequate exception handlers and checks in place for
logging
The goal here is to ensure that as close to 100% of the security incidents are
correctly logged by the application. This will depend on the platform but the
business logic may also require modifications to ensure even invalid direct object
references are recorded.
Whenever possible, try to centralize the code that handles logging at the
application layer, consider the following ideas: Would changing the code to throw
“security exceptions” simplify logging? Can (all or part of) access control be
incorporated in a central component such as base class that is inherited, known
foreign keys or database permissions?
• Is information stored in the logs sufficient for security, compliance and root-cause
identification?
Time synchronization between logs is very important for correlation purposes
later. In addition to this, the logs should have sufficient information to identify
both the root cause and facilitate attribution5. Things like logging the IP address,
time, username (if possible), etc. of an attack is essential.
• Preserving log integrity
Log integrity is very important. Whenever budget allows it, it is a very good idea to
write the logs to write-once media and/or another server. The reason for this is
that, if the web server is compromised and the adversary escalates to get root
privileges, then the logs simply cannot be trusted if they are on the same server.
This may be crucial in a case where prosecution is being sought: If all the proof is
in the logs and log integrity cannot be guaranteed, then the proof will likely be
invalidated by most courts.
• Implement an adequate log retention policy
Log retention policies of at least 1 year are highly recommended based on
measured breach to breach detection duration (i.e. 210 days on average6).
Compliance and industry regulations are another factor to consider appropriate log
retention policies.
5 http://www.hacksurfer.com/amplifications/270-experts-weigh-in-8211-the-attribution-problem-8230-or-non-
problem
6
http://www2.trustwave.com/rs/trustwave/images/Trustwave_GSR_ExecutiveSummary_4page_Final_Digita
l.pdf