Secure Web Development
Secure Web Development
Razvan Peteanu
[email protected]
Contents
1
1.1
Why? ..........................................................................................1
Frequently Asked Questions...........................................................2
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.14
2.15
2.16
2.17
2.18
2.19
Legal Notice.
All names, products and services mentioned are the trademarks or registered
trademarks of their respective owners.
Throughout the text, a number of vendors, products or services are listed.
This is not an endorsement of the author for the above, but merely pointers
of real world examples of issues discussed. Omissions are possible and the
author welcomes feedback.
Permission is hereby granted to freely distributed this document as long as it
is not altered.
About the author: Razvan Peteanu works in Toronto and can be contacted
by e-mail at [email protected]
1 Why?
The following document is intended as a guideline for developing secure
web-based applications. It is not about how to configure firewalls, intrusion
detection, DMZ or how to resist DDoS attacks. This is a task best addressed
at system and network level. However, there is little material available today
intended for developers. We have entered the dotcom age in which a web site
is no longer an isolated site, but an extension of the internal business
systems, yet there isnt much about how to create this extension securely.
Traditionally, developers have worked on systems for environments where
malicious intents were not a real threat: internal systems, software for home
use, intranets. There may have been occasional exceptions, sometimes with
embarrassing outcomes, but they could be dealt with at HR level and the
example prevented others from attempting it again. An isolated (read: not
linked with internal systems) web site is not far from the same scenario: the
security was treated mostly at the system level by installing the necessary OS
and web server fixes and applying correct settings and permissions. If a
breach occurred, the system was taken offline, rebuilt better and the site put
up again. Everything at a system administration level.
However, as the Internet becomes more and more commercial (after all, this
is where the .com comes from), a web site becomes more and more an
application. Thus, the team has more and more developers, skilled in web and
traditional development. However, few resources for them focus enough on
security to make them aware about whats out there on the Internet. We
often read that this web site is secure because it uses 128-bit encryption.
Most often, programming books will have a single chapter on security,
compressing SSL, signatures, permissions, cookies and other topics in 20
pages. Little if anything is said about how to think maliciously about your own
code, trying to find out if it has a vulnerability. Little if anything is said about
how to do security-focused code reviews.
We hope this document will fill some of the gap.
It is and will continue to be a work in progress and your feedback is highly
appreciated.
Target Audience
The primary audience are developers and architects as well as infosec
professionals. Project managers may be interested as well to understand
various issues that impact specifications and project schedules.
Some of the areas above are not within the scope of this document. Detection
and monitoring is best addressed at system and network level while legal
issues are best addressed by, well, lawyers.
I thought the firewall would take care of this. Or file permissions. Or
SSL.
Each of the above is useful and necessary to ensure the overall security of the
site, but they address different risks.
Firewalls protect a system from a different class of risks by preventing access
to non-public services and preventing malicious network traffic to reach the
server. SSL provides server (and sometimes client) authentication and
communication privacy, but otherwise its blind to the content of the traffic.
File permissions may prevent abuses of rights when two different user levels
are involved but it will not do so between two users with the same level.
To draw a parallel to the traditional development, coding for security would be
very roughly equivalent to putting error handling. Its got to be in, nothing
around the application can replace it.
Im an experienced web developer and dont think I need this.
This is not about how to do web development. Its specifically about how to
do secure web development. Why is this emphasis relevant? Because creating
an application able to withstand malicious use (and well see later what this
could mean) is not something that (a) is immediately visible; a non-secure
code can do its primary functionality very well (b) has been a concern during
development phases (c) taught in programming books or seen in traditional
development projects when the user community was limited and not
particularly hacker populated.
Cant someone do this after I finish my dev work?
No. Within the context of this document, security needs to be built into the
application from the beginning, its not something thats applied at the end.
Of course, well still have permissions and other administrative operations,
but again, they are not a replacement.
Note: We will try to make this document as vendor-neutral as possible.
However, the authors experience has been mostly with Microsoft technologies
so there will be an inherent slant in this space.
2 Best Practices
2.1 Security as part of the business picture
Surprise-surprise. Until the past year or so, security and business did not
often come together in the same paragraph.
Well, it shouldnt be a surprise because ultimately, security is not about
technology but about managing risk. Security is present in Internet projects
precisely because its needed to mitigate some risks. Any business has some
assets to protect and in the Internet world, its the information assets we are
concerned of. Examples of assets: integrity of the site content, site
availability, data privacy, trust. As you can see, not all assets are physical.
Once the assets are identified, the next step is too identify the risks. If we
look at the example above, we can quickly derive some risks associated with
the enumerated assets: site defacement (the integrity is lost), site is brought
down (remember the DDoS attacks?), customer data gets published on the
web (credit card info is a typical example) or the transaction is made with the
wrong party.
Now, having the risks clearly spelled out, thinking of what security measures
must be put in becomes an easier task, which brings us to the next step:
use cases. How the application will be used is essential to understand the
security implications.
identifying the users, their roles and rights. Again, this goes straight to
designing the authentication and authorization schemes.
In a parallel with the items under the requirements section, the security
architecture will focus on:
possibilities to abuse the use cases (this includes thinking of malicious use
cases)
selecting the platform and technologies that support the users, roles and
access rights. This includes choosing an operating system, the web server,
an application server if applicable, the directory service when a large
number of users is concerned, a user authentication mechanism
(anonymous, cookie, basic, challenge response, digest, certificate-based
etc), the authentication mechanism between the different application tiers
and so on. Certainly, the decisions are not made solely from the security
standpoint but this is the role of the architect: to take in all the application
requirements and find the best possible solution within the constraints.
Using admin accounts is very appealing at the first sight: the developer
doesnt have to bother with access restrictions and can focus on the
functionality. Youve already guessed it, the problem has just been spelled
out: with admin accounts, there is no access restriction. The code can do
anything, anytime. At least, until the release date comes closer or the code is
moving in a pre-production environment where accounts and permissions are
managed properly and then things start to break. Tables that used to be
accessible or writable are no longer because specific access rights have not
been assigned, ACLs are applied and various run time errors occur. In a
distributed application even identifying the root cause can be a bit
challenging. All these will add debugging time at a time when no one wants it.
There is another operational danger posed by using admin accounts: because
access is not confined to a specific application you may inadvertently
overwrite something else. When I was working on a project with SQL Server,
I was personally very close to deleting someone elses tables because I was
using the sa account when operating from the management console. On that
particular server there were several databases for different phases of the
same project. They looked very similar, at least as the tables names went, so
a slip of the mouse to the next database in row followed by a Select All and
Delete almost made me the first lynched individual in the companys history. I
still live because of the confirmation message.
The lesson: use application-specific accounts with rights identified as early as
possible. Yes, it is likely the access rights will have to be refined in time, but
unless you start making use of them, how to find out?
This is not appropriate because, like any other HTTP request, this will get
logged in the logs of the web server as well as in whatever proxies might be
on the way. The above request will get logged in clear text similar to:
2000-03-22 00:26:40 - W3SVC1 GET /process_card.asp cardnumber=1234567890123456 200 0 623 360 570
80 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT) - -
Also, the entire URL may be stored by the browser in its history, potentially
exposing the sensitive information to someone else using the same machine
later.
SSL wouldnt help in this case, because it only protects the request in transit.
Once arrived at the destination, the request will be happily decrypted and
logged in clear text. An apparent rebuttal may come from the standpoint that
the server must be trusted. Yes, it must but this trust implies that private
customer data be dealt with sensibly. There is no reason for the server to
GLOBAL.ASA
the IIS metabase
a public property of a COM component
the registry.
GLOBAL.ASA is generally the best place because declarations put there have
global or application scope and are easy to refer to from ASP. For example,
you can have the database or LDAP connection string defined as an
application-level variable. Then, referring to it would be as easy as something
like Application(DBConnectionString) or Application
(ADsPath) where the two are variable names, you dont have to use the
same. There is still a chance however that a security bug may reveal the
source of global.asa.
The metabase is a directory-like collection where IIS keeps its settings and
can be accessed through ADSI (the IIS Resource Kit has a useful GUI-based
MetaEdit tool, but you could also use Option Packs MDUtil command line
approach). Physically it is stored in %WINNT%\%SYSTEM32%\inetsrv\metabase.bin,
but once loaded the metabase is kept in memory by the web service and
therefore access to it is fast. However, there is little reason to use it instead
of the global.asa for regular tasks.
The last two methods are less used. Using the registry imposes a penalty on
performance. Also, both add some administration overhead (component
registration, .reg files etc), especially at publishing and deployment times.
information (such as a connection string that was once part of the server side
script, then commented out. In time, through inadvertent editing, it can reach
the client script and thus be transmitted to the browser). The comments are
not dangerous per se, but can reveal information.
2.10
Cross-site scripting
This is a more complex issue and, after going through the introductory pages
below, the reader is encouraged to read the materials available at the
following links (more at the end of the section).
CERT Advisory CA-2000-02 Malicious HTML Tags Embedded in Client Web Requests
at http://www.cert.org/advisories/CA-2000-02.html
Not a very straightforward name, but a significant problem which can occur
with sites that allow the user to input some data and later display it. Typical
examples are registration information, bulletin board messages or product
descriptions. In the context of this discussion, the user is the more or less
anonymous user who visits the site and not the site administrator that
changes the content.
Why is this a problem?
Because it breaches trust. When a user visits a site, it has a level of trust in
the content that comes from the server. Usually, this means the user expects
the web site will not perform malicious actions against the client and it will
not attempt to mislead the user to reveal personal information.
With sites that accept user-provided data, later used to build dynamic pages,
an entire can of worms is opened. No longer is the web content authored by
the web creators only, it also comes from what other (potentially anonymous)
users have put in. The risk comes from the existence of a number of ways in
which more than the user-input fields can be manipulated to include more
than simple text, such as scripts or links to other sites. Taking the script
example, the code would be executed on the client machine because it would
undistinguishable from the genuine code written by the site developers.
Everything comes in the HTML stream to the browser.
Quick example: Lets take a site that allows users to input the users name
through a form and that the value entered is later displayed. For brevity, well
use the same form for both inputting the string and displaying it. The source
for the form is
<html>
<%
if request.form ("yourname") <>"" then
Response.Write("Hello " + request.form ("yourname"))
else
%>
<form method="POST">
<input type="text" name= yourname>
10
11
2.11
2.12
Middleware security
Most serious web applications would be complex enough so that
componentizing them is a must. Whether its with COM or EJB, this adds a
layer of complexity to the [security] architecture.
For the security architect, it raises a few specific issues such as how
authentication, authorization and impersonation/delegation of credentials
work in a distributed environment.
This version of the whitepaper will focus on COM security but we hope to add
more material about EJB in the future. In the meantime, keep an eye on the
emerging Java Authentication and Authorization Service
http://java.sun.com/products/jaas/ which adds user-role security to the already
existing mechanisms based on code base and signature. Also, Scott Oaks
Java Security book published by OReilly is an excellent reference into how
Javas security mechanisms actually work. Check the books web site at
http://www.oreilly.com/catalog/javasec/, you can download the code or errata.
12
COM security also is a topic big enough for a book and in fact it is. Its written
by the man to ask about COM security, Keith Brown from Developmentor. Be
sure to check his page http://www.developmentor.com/kbrown/ and
http://www.developmentor.com/securitybriefs/ for details on his brand new book,
Programming Windows Security and also for cool info and utilities to explore
the COM world.
To find out how IIS and MTS/COM+ work together to impersonate a client, read the
following resources:
http://msdn.microsoft.com/msdnmag/issues/0600/websecure/websecure.asp
http://msdn.microsoft.com/msdnmag/issues/0700/websecure2/websecure2.asp
http://www.asptoday.com/articles/20000224.htm
http://www.asptoday.com/articles/20000302.htm and the backgrounder at
http://msdn.microsoft.com/library/techart/msdn_practicom.htm
This last resource has useful tips on the difference between DCOMCNFG and OleView
when it comes to setting component security.
2.13
Declarative vs programmatic
Declarative security takes place when the access control is set from outside
the application, usually through an administrative interface. Programmatic
security is the case in which the logic in the code checks the credentials and
associated rights. In most cases, web applications will be a mixture of these
two methods of enforcing security controls.
When its available, declarative security is quite useful: file permissions, MTS
or database server roles are all examples of this type. They are easy to
administer, require no code changes or an understanding of the code for
regular operational tasks. Of course, knowing how to apply and integrate
them into the whole picture requires a thorough understanding, but once the
pieces are in place, daily tasks (such as user and group management) can be
delegated to a different team.
Declarative security is good to protect resources between different groups of
users (i.e., with different rights). However, when you want a greater
granularity, youll have to use programmatic security. For instance, to
distinguish between two users from the same group, permissions and roles
are not enough. When you do web banking, the web server can allow
anonymous access to some pages and enforce authentication to others, but
once the users authenticate, its the codes task to prevent one user from
accessing anothers account.
Programmatic security can also help when you need better granularity of
controls then what declarative can offer. For instance, with MTS components,
you can enforce security on a per-interface level. If you want to have different
permissions for some methods within the same interface, however, youll
have to resort to calling ObjectContexts IsCallerInRole method. The same
story when you want to know more about the security context in which the
execution takes place and to distinguish between the original and the direct
13
2.14
2.15
Snake oil
The Real World is not necessarily fair and trustworthy and this applies to
security software as well. Once in a while, you will find products with
larger-than-life claims. Revolutionary breakthroughs, the cure to all your
security concerns, the secret software that gives you 100% security forever
without even requiring you to do or understand anything etc. Youve got the
idea. Why they are bad and how to spot such cases is the subject of a
dedicated FAQ: http://www.interhack.net/people/cmcurtin/snake-oil-faq.html (no
longer maintained but still a very good read) or of numerous commentaries in the
Cryptogram newsletter at http://www.counterpane.com/crypto-gram.html
2.16
This isnt the place to discuss how reviews should be done, though so well
leave this item this short.
2.17
2.18
15
By default IIS only logs several fields but the others are useful as well during
development. The easiest way to set extended logging is to do it at the
Master site level, thus all sites youll create in the future will have full logging
enabled by default.
The LDAP log
This is for Site Server Membership Authentication only. Unfortunately, Site
Server does not log failed logons with membership authentication which
would be useful when you bind to the directory from code. Workarounds are
handling the failed logins from code (which should be done anyway) and
using the PerfMon counters to monitor the authentication problems (look for
the Site Server Authentication and LDAP Service counters).
2.19
Other pointers
Some resources you may find useful:
http://java.sun.com/security/seccodeguide.html
security portals:
http://www.securityfocus.com
http://securityportal.com/
http://www.esecurityonline.com/
http://www.ntsecurity.net
16