Attack I: SQL Injection at Telegraph Results in 700,000 Emails Stolen
Attack I: SQL Injection at Telegraph Results in 700,000 Emails Stolen
There is no glamour in building secure ASP.NET web applications. The vast majority of
developers I’ve met would much rather focus on building new & flashy features that can
impress their managers and end-users. Even though security can usually take a backseat
during the initial stages of development, it usually comes back to bite you when it’s least
expected. This article covers some of the security aspects to be aware of when developing a
new web application and what to do throughout the development process to protect
applications and databases against common attacks.
Back to Reality
Building web applications with ASP.NET has been getting easier as the technology and the
development environment, Visual Studio, become more sophisticated. Many of the
complexities are taken care of by the framework and are out of view from developers. This
allows us to focus more on the business value and features of our web applications and less
on technical aspects that very few really understand or appreciate. As a result, many
developers believe that security is also taken care of for them and they need not to worry
about it.
Unfortunately the reality is quite different. Unless developers make security one of their
priorities early on in the development cycle, project managers might end up with a great
application but one that cannot go live without compromising the safety of their end-users
and their data.
Collapse
public abstract class BaseWebPage : System.Web.UI.Page
{
...
}
Note: This class should verify general information submitted by users, such as URL
parameters, form data to some extent, cookies and other info that is not tied to any
particular user.
Diagram 2: User base class to be inherited by all pages used by logged-in users
Collapse
public abstract class UserWebPage : BaseWebPage
{
...
}
Note: This class should ensure that the user is currently logged-in and has access to the
page functionality and to the date they want to view.
Diagram 3: Administrator base class to be inherited by all pages used by
administrators
Collapse
public abstract class AdminWebPage : UserWebPage
{
...
}
Note: This class should ensure that in the current user is indeed an administrator.
Linking the Page to Our Web Framework
Utilizing the ASP.NET web framework above requires very minor adjustments to a web page.
Rather than using the default ASP.NET page base class (System.Web.UI.Page) use the
appropriate base class defined earlier.
For instance, if we defined a new ASPX page that is supposed to serve logged-in users we
would define it as follows:
Diagram 4: Sample ASPX code-behind page serving a particular user
Collapse
public partial class ShowUserSettingsPage : UserWebPage
{
...
}
Collapse
public abstract class BaseWebPage : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
AuthorizeUser();
ValidateSubmittedData();
}
...
}
Note: Class calls a virtual method AuthorizeUser that inheriting classes can implement to
force specific user access rights, and once the user is authorized, the class verifies all data
submitted to the web page.
Diagram 6: Page authorization on UserWebPage for ASP.NET web pages
designated for logged-in users
Collapse
public abstract class UserWebPage : BaseWebPage
{
protected override void AuthorizeUser()
{
if(!User.IsInRole("User"))
{
//user cannot see the page - redirect to appropriate page
}
}
}
Note: Class overrides the AuthorizeUser to make sure current user is logged-in.
Application Configuration
Different applications require different configurations but the focus of this section is on those
that fall under the responsibility of the web developer.
Encrypting Sensitive Configuration Information
Any sensitive configuration information on the web.config and beyond should be properly
encrypted to avoid exposing sensitive details like connection strings to potential attackers.
You can use the web.config protectedData section to indicate which sections of the
web.config file should be encrypted.
The following shows how to protect the connectionStrings section:
Diagram 7: Protecting the connectionStrings section within the web.config
Collapse
<protectedData>
<protecteddatasections>
<add name="connectionStrings" provider="RSAProtectedConfigurationProvider">
</add>
</protecteddatasections>
Note: This is a more suitable way to protect data across different web servers. Unlike
DPAPI, the information is not tied down to any particular machine.
You can now use aspnet_regiis to encrypt the appropriate section:
aspnet_regiis -pef connectionStrings "c:\...\MyWebApplication"
Protect Internal Exceptions
Prevent detailed exceptions from being displayed on the client’s browser entirely or display
this only when the client is browsing on the web server itself.
Final Note
Even though it can be tempting to push off dealing with security (or avoiding it altogether),
its importance should never be underestimated. An attack or even a request for a security
audit by a customer can cost you time, money and potentially your company’s reputation.