ch01
ch01
ch01
rking/
Secur
ity
o fess i o nal
u i d ed IT Pr
e Self- G
a rts fo r th
t y S m
Securi
ers.
v io u s hack
td e ur
o d a y ’s mos o u s t ock yo
from t helps
y
p p li c ations n e r ’s Guide e nd qu
ickly
e b a e g in d e f
Secur
ew y: A B s, and
a t io n Securit m o n hack
ppli c o m
Web A vent c
t o o lk it, pre
y s.
securit io u s attack
c
t mali apters
agains in c lu des ch
source and
p r a c tical re h o r iz ation,
ser,
This ut e
n t ic a tion, a g w it h brow n e r’s Guid
u t h e a lo n e d AB e g in
on a ment, all sup
port
ecurity:
n m anage r it y — a t io n S
sess io
le sec
u
’ll also plic you’re
a s e , and fi u s t ry. You t io n Web Ap : fi n e d so that
b d c s e
data
tories
from in
erabilit
y dete feature security
terms d
r u e s r v u ln m m o n
by t es fo ll as a Lingo—
Co
practic as we the job on the
get b e s t
lo p m e n t ,
u r it y t h e k now on * o p in io n s based
eve in
cure d ial sec vant
and rele y experience
and se r s e ssent e s , F r a n k savvy,
cov e pla t IMHO — ustr ies ated a ols for
r that ’s tem s of ind chnolog e cre
hav licable to
ch a p t e
. This
b o o k
signe d t o
author s ’ ye a r * ing sec u r it y te
t and Liu pp
ullivan mediately a ting out.”
d a m entals p le s are de T ips for gett ization’s budge g ha cke d. S
w ith im
or just sta r
fu n x a m o te — g a n gettin acked
e t N r o r ity lan on p security p or her tools
sts, an
d
t away. Budge s into you s of secur o r p
ers— o web ap g his
ch e c k li
starte
d r ig h
s, and pro
ce s s e * ns to th e ru le e hack t
now th d approach ctitioner sha Inc.
rpenin
Exceptio ts
ystem k
u get t to
“Ge tials-bas e ra k,
y o d obe S tice — urity p aceboo
lp essen rmation sec ty Manager, F
A c
tual Pra
he t
contex
er a as
search y re . He w es In Ac orld n se o fo u ri
ou can u
in
in real-w
y issu y any e c
ecurit ecurit eehan,
S
Sulliva
n is a s e nior s
eb an
d c lo u d securit
n the
M ic r o soft S
er at
* exp la in e d
ble chec
k li s ts y —Ryan
McG
Bryan s on w ager o manag stomiza
focuse g r a m man lo p ment g tools
,
n — C u
Your Pla
h e r o v e n in
where curity
p d a de y scan pply
hen to a
s e a n b il it
previo
usly a
ment
Lifecy
cle tea
m
design
HP’s v
ulne r a
* the jo b n o w
w, why , a n d w
s on ho
v e lo p e d t o iu. He
De help ch & L
ere he vInspe
ct.
at Sta ineerin
g
io n — T ip at work
HP, wh
c t a n d D e
g p a r t n e r
v e r s e ng
E
s t Into Act and techniques
WebIn
sp e
IS S P , is a m
anagin
P e n e t ration
and R
p
e
a n d was a
n ana
a c
ly
k in g *new sk
ills
t Liu, C tack & y grou hor of
H
Vincen le d the At b a l S ecurit is a coaut d W ir eless,
previo
usly ll’s Glo incent Expose
neywe ncy. V acking
a m s for Ho c u r it y Age io n and H
te e it
Nation
al S hird Ed
at the tions, T
e b Applica 0 USD
d W $40.0
Expose n . mputing
Secon
d Editio @MHCo
TM
Sullivan
Liu
eeks
Jeff W
TM
D esign:
Cover
Secure Beginner’s Guide / Web Application Security, A Beginner’s Guide / Sullivan and Liu /
Blind Folio: 1
Part
I
P ri m e r
Chap
ter 1
e to th e
We l co m f W e b
W o r l d o
W i d e e cu ri t y
i ca t i o n S
A p p l
3
We’ll Cover
●● Misplaced priorities and the need for a new focus
●● Network security versus application security: The parable of the wizard
and the magic fruit trees
●● Thinking like a defender
●● The OWASP Top Ten List
●● Secure features, not just security features
that were so common in the early days of the Web. Vulnerabilities in web applications
have been responsible for some of the most damaging, high-profile breaches in recent
news. Just a small sample of attacks in the first half of 2011 alone includes:
●● The SQL injection attacks on the Sony Music web sites in May 2011 by the LulzSec
organization. While unconfirmed by Sony, it’s also believed that SQL injection
vulnerabilities were responsible for the attacks against the Sony PlayStation Network
and Qriocity that leaked the private data of 77 million users and led Sony to shut
down the services for over a month. The overall cost of this breach to Sony has been
estimated to exceed 171 million dollars (US).
●● A cross-site scripting vulnerability in the Android Market discovered in March 2011
that allowed attackers to remotely install apps onto users’ Android devices without their
knowledge or consent.
●● The attack on information security firm HBGary Federal in February 2011 by the
hacker group Anonymous. Another simple SQL injection vulnerability in the www
.hbgaryfederal.com website, combined with a poorly implemented use of cryptographic
hash functions, enabled Anonymous to extract the company officers’ usernames and
passwords, which then enabled them to read the officers’ confidential internal e-mails.
The CEO of HBGary Federal resigned from the company shortly thereafter, citing a
need to “take care of his family and rebuild his reputation.”
None of these attacks were stopped by the sites’ firewalls! But IT budgets still focus
primarily on firewall defenses. This is puzzling, since network firewalls are completely
useless to prevent almost any web application attack. You can’t use firewalls to close off
ports from which your web applications are being served, because then nobody could
come to your web site. Organizations spend billions of dollars a year on advertising to get
people to come to their sites; they’re certainly not going to close them up with firewalls.
Figure 1-1 shows a diagram of an attacker evading a server’s firewall defenses by simply
entering through the web site port 80.
We as an industry definitely have some misplaced priorities when it comes to
security spending, but the magnitude of the imbalance is simply staggering. In another
recent survey of IT professionals (http://www.barracudanetworks.com/ns/downloads/
White_Papers/Barracuda_Web_App_Firewall_WP_Cenzic_Exec_Summary.pdf), almost
90 percent of companies reported that they spend less money on web application security
than they spend on coffee: less than $1 per day per employee. We’re willing to spend
billions of dollars a year to protect our networks, but when it comes to the targets that are
really getting hit the hardest, we skimp and cut corners. To repeat an often-used analogy,
Firewall
Figure 1-1 A server firewall preventing users (and attackers) from accessing most server
ports but leaving port 80 open for web site traffic
this is like installing burglar alarms and steel bars on all of the windows in your home, but
leaving the front door wide open.
Since the same survey showed that almost 70 percent of organizations rely on network
firewalls for their web application defense—which is essentially the same as having no
defense at all—it’s hard to see this as anything besides an issue of being appropriately
educated on web application security. People know their web applications are important,
but they don’t know how to secure them.
That’s where this book comes in.
his groves, picking as much fruit as they wanted—after all, the trees were magic and grew
new fruit the second the old fruit was picked. Life was good and everyone was happy, until
one day the wizard caught a lovesick young farm boy carving his sweetheart’s initials into
one of the lemonpear trees. He scolded the boy, sent him away, and turned the boy’s ears
into big floppy donkey ears as a punishment (just for a few hours, of course).
The wizard thought that was that and started to get back to his scrolls, but then he
saw another villager trying to dig up a tree so he could take it back to his house and
plant it there. He rushed over to stop this thief but an even more horrific sight caught his
eye first. Two apprentices of the evil wizard who lived across the valley had come with
torches and were trying to burn down the whole orchard to exact revenge for their master’s
embarrassing defeat at wizard chess earlier that month.
Now the wizard had had enough! He threw everyone out, and cast a spell that opened
up a moat of boiling lava to surround the orchard. Now no one could get in to vandalize
his beloved fruit trees, or to steal them, or try to burn them down. The trees were safe—
but the wizard felt unhappy that now he wasn’t able to share his fruit with everyone. And
the villagers did tend to spend a lot of gold pieces buying potions from him while they
were there picking his fruit. To solve this problem, he came up with an ingenious new
solution.
The wizard invited his friend the giant to come live in the orchard. Now whenever
someone wanted a piece of fruit, he would just shout what he wanted to the giant. The
giant would go pick the fruit for them, jump over the lava moat, and then hand them the
fruit. This was a better deal for both the wizard and the villagers. The wizard knew that
all the miscreants would be kept away from the trees, and the villagers didn’t even have to
climb trees any more: the fruit came right to them.
Again, life was good and everyone was happy, until one day one very clever young
man walked up to the edge of the lava where the giant was standing. Instead of asking the
giant to bring him back a basket of persimmons or a fresh raisinmelon, he asked the giant
to go up into the tower and fetch him the wizard’s scrolls. The giant thought this request
was a little strange, but the wizard had just told him to get the people whatever they asked
for. So he went to the tower and brought back the magic scrolls for the young man, who
then ran off with all of the wizard’s precious secrets.
Real-World Parallels
If you were hoping for a happy end to this story, there isn’t one—not yet, at least. First,
let’s take a look at the parallels between this story and the real-world security issues that
organizations like yours face every day.
You (the wizard) have data (fruit) that you’d like to share with some people. But you
know that if you just let everyone have free access to your server farm (orchard), there’ll
be disastrous results. People will deface your servers (vandalize the trees), they’ll install
botnets or other malware to take the servers over for themselves (steal the trees), and
they’ll try to deny service to the servers so no one can use them (burn the trees down).
In response to these threats, you erect a firewall (lava moat) to keep everyone out. This
is good because it keeps the attackers out, but unfortunately it keeps all your legitimate
users out too, as you can see in Figure 1-2. So, you write a web application (a giant) that
can pass through the firewall. The web application needs a lot of privileges on the server
(the way a giant is very powerful and strong) so it can access the system’s database and
the file system. However, while the web application is very powerful, it’s not necessarily
very smart, and this is where web application vulnerabilities come in.
By exploiting logic flaws in the web application, an attacker can essentially “trick” the
web application into performing attacks on his behalf (getting the giant to do his bidding).
He may not be able to just connect into the servers directly to vandalize them or steal
User
Data
User
Attacker
Firewall
User
Figure 1-2 A firewall (lava moat) keeps attackers out, but keeps legitimate users out as well.
from them any more, but if he can get a highly privileged application to do it for him, then
that’s just as good. He may even be able to read the application source code (the wizard’s
scrolls) out of the file system.
The moral of the story is that it’s necessary to use network-level defenses like firewalls
to keep attackers out, but network-level defenses alone are not sufficient. You also need to
weed out all the logic flaws in your web applications so that they can’t be subverted.
Note
A lot of people think that they’re safe from attack because their company is too small to
be noticed by attackers. Hackers only go after the big guys like Google and Microsoft,
right? Think again: According to statistics from the IBM X-Force security research team,
products from the top ten software vendors accounted for only 20 percent of reported
vulnerabilities in 2010 (as seen in Table 1-1), and this number is down from 23 percent
in 2009. Attackers are increasingly targeting the “long tail” of smaller organizations’
web applications, so never think that you’re too small to slip under their radar.
Table 1-1 2010 First-Half Vulnerability Disclosure Rates per Vendor (IBM X-Force 2010
Mid-Year Trend and Risk Report)
However, as with firewalls, knowing how to defend against specific web application
attacks is necessary but not sufficient by itself. Beyond just looking at specific attacks, we
also want to educate you on larger, more general security principles.
This is important because attack methods change all the time. Attackers refine their
methods, finding new ways to break into systems that were previously thought to be
secure. Every year, some security researcher will present a paper at the BlackHat or
DefCon security conference that negates a built-in browser or operating system defense
that developers had come to rely on.
You need to be prepared not just for the attacks that are going to come today, but for the
new attacks that are going to come tomorrow. You can do this not by thinking like an attacker
(which you’re not), but by learning to think like a defender (which you now are). This is
why it’s so important to learn the general security principles behind the specific defenses.
In Actual Practice
The way that a lot of security experts want you to solve this problem is for you to
“think like an attacker.” In their opinion, if you just think the way the attackers do,
you’ll be able to anticipate their moves and block them. What ridiculous advice!
You’re not an attacker—at least, I certainly hope you’re not. If you want any degree of
confidence in your results at all, it’s just not possible for you to snap your fingers and
start thinking like someone with years of experience in a completely different field of
expertise.
To show what an unrealistic expectation this is, when I give presentations to groups
of security professionals, I’ll sometimes challenge them to think like a dentist. I’ll tell
them that my tooth hurts and ask what they plan to do for me. They’ll take an X-ray,
they say. “Fine,” I reply, “what are you going to look for in the image?” They don’t
know. “Have you ever operated an X-ray machine before?” They haven’t. “Are you
sure you’re not going to give me a lethal dose of radiation?” They’re not. This could be
a problem!
When you try to think like an attacker, it’s likely that you’ll not only be lulled
into a false sense of security—thinking you’ve protected yourself when you really
haven’t—but there’s also a good chance that you’ll make matters even worse than they
were before. Maybe we’ll all be better off letting developers be developers, and letting
security researchers be security researchers.
It’s good to know how to appropriately encode HTML output to prevent cross-site scripting
attacks, but it’s even better to know that mixing code and data can lead the application
to interpret user input as command instructions. Knowing this—and developing your
applications with an eye to avoiding this—can help you prevent not just cross-site scripting,
but SQL injection, buffer overflows, request header forgery, and many others. And there’s
a good chance it’ll help you prevent whatever new attacks get dropped at DefCon next
year. The methods may change from year to year, but the underlying principles will always
remain the same.
Tip
Built-in browser defenses can be a great help, but don’t rely on them. It’s very unusual
to be in a situation where you can guarantee that all your users are using the exact
same browser. Certainly this won’t ever be the case if you have any public-facing
web applications. And even if you’re only developing web sites for use inside an
organizational intranet where you can mandate a specific browser, it’s likely that
some users might configure their settings differently, inadvertently disabling the
browser defenses. The bottom line here is that you should treat browser defenses as an
unexpected bonus and not take them for granted. You are the one who needs to take
responsibility for protecting your users. Don’t count on them to do it for you.
#1. Injection
One of an attacker’s primary goals is to find a way to run his own code on your web
server. If he can do this, he may be able to read valuable confidential data stored in your
databases or conscript it into a remote-controllable botnet of “zombie” machines. To
accomplish this through a network-level attack, he might have to find a way to sneak
an executable binary file through your firewall and run it. But with an application-level
attack, he can accomplish his goal through much more subtle means.
A typical web application will pass user input to several other server-side applications
for processing. For example, a search engine application will take the search text entered
by the user, create a database query term from that text, and send that query to the database.
However, unless you take special precautions to prevent it, an attacker may be able to input
code that the application will then execute. In the example of the search engine, the attacker
may enter database commands as his search text. The application then builds a query term
from this text that includes the attacker’s commands, and sends it to the database where it’s
executed. You can see a diagram of this attack in action in Figure 1-4.
This particular attack is called SQL injection and is the most widespread form of injection
attack, but there are many others. We see injection vulnerabilities in XML parsing code
(XPath/XQuery injection), LDAP lookups (LDAP injection), and in an especially dangerous
case where user input is passed directly as a command-line parameter to an operating system
shell (command injection).
Cross-site scripting is dangerous not just because it can have such high-impact effects,
but also because it’s the most pervasive web application vulnerability. You’re potentially
creating cross-site scripting vulnerabilities whenever you accept input from a user and
then display that input back to them—and this happens all the time. Think about blogs that
let users write their own comments and replies to posts. Or collaborative wikis, which let
the users themselves create the site content. Or even something as seemingly innocent as
a search feature: if you display the user’s search term back to them (for example, “Your
search for ‘pandas’ found 2498 results”), you could be opening the door to cross-site
scripting attacks.
from her browser and posts it on a blog, not only is she posting a link to the page she was
looking at but she’s also posting her personal token, and now anyone who follows the link
can impersonate her session.
Note
Throughout this book, you’ll see us use example URLs with a top-level domain of “.cxx”,
like “http://www.myapp.cxx”. We do this because—as of this writing—there is no such
real top-level domain “.cxx”, so there’s no chance that the example site actually exists.
We don’t want to accidentally name a real web site when we’re talking about security
vulnerabilities!
Tip
For an even better way to secure password hashes, you should add a random value (or
“salt”) to the plaintext password before computing its hash value. This approach has
multiple benefits. First, in case the hash value is ever leaked, it makes an attacker’s job
of reverse-engineering the original password text from a pre-computed lookup table (or
“rainbow table”) much more difficult. (In Figure 1-5, you can see a screenshot of a web
site offering rainbow tables for download, which can be used to crack Windows XP
user accounts.)
And second, without salt values, whenever two users have the same password, they’ll
have the same password hash as well. Cracking just one user’s password from a leak
of the hash list could end up revealing account information for potentially hundreds or
thousands of other users as well.
Figure 1-5 A web site offering Windows XP password rainbow tables for download
This design is fine as long as there’s some other kind of authorization mechanism
in place to prevent me from accessing the administration site. If the only thing keeping
me out is the fact that I’m not supposed to know the site is there, that’s not sufficient
protection. If someone on the inside accidentally reveals the secret site, or if I just happen
to guess it, then I’ll be able to just get straight in.
Note
While getting transport layer security right is a critical part of your application’s
security, it should be evident by now that it’s not the only part of your application’s
security. As with firewalls, far too many people tend to put far too much trust in the
little HTTPS lock icon in their browser. Take SQL injection, for example: if your site is
vulnerable to a SQL injection attack, all that you’ll get from using HTTPS is to create a
secure channel that an attacker can use to exploit you.
IMHO
It’s disappointing to me that so many people think of security as just being security
features. If you go to your local bookstore and randomly pick a book from the
computing section, that book will probably have one short chapter on security, and
99 percent of that chapter will cover authentication and authorization methods.
I’ve even seen entire books titled something like “Web Security” that only covered
authentication and authorization.
We’re certainly showing our bias here regarding the value of secure features versus
security features. But don’t take that to mean that security features are unimportant. If
you don’t implement appropriate authentication and authorization checks, or if you use
easily crackable homegrown cryptography, your users’ data will be stolen and they won’t
be happy about it. They won’t care whether it was a cross-site scripting vulnerability or
improper use of SSL that led to their credit card being hijacked. They probably won’t even
understand the difference. All they’ll know is that they were hacked, and you’re the one
responsible. So cover all your bases, both secure features and security features.
Final Thoughts
We’ll meet up with our friend the wizard again at the end of the book to see what he’s learned
to make his magic fruit orchard a safer place. Of course, we know that the wizard is wise
enough not to test out his new spells on anyone’s trees except his own. This goes for you too.
Virtually all of the attack techniques we’ll be describing are illegal for you to test against any
web site, unless you own that site yourself or have explicit permission from the owner.
We’ve Covered
Misplaced priorities and the need for a new focus
●● Seventy percent of attacks come in through a site’s web applications.
●● Spending money on network firewalls isn’t going to help this problem.