- •Exploiting Software How to Break Code
- •Table of Contents
- •Copyright
- •Praise for Exploiting Software
- •Attack Patterns
- •Foreword
- •Preface
- •What This Book Is About
- •How to Use This Book
- •But Isn't This Too Dangerous?
- •Acknowledgments
- •Greg's Acknowledgments
- •Gary's Acknowledgments
- •Bad Software Is Ubiquitous
- •The Trinity of Trouble
- •The Future of Software
- •What Is Software Security?
- •Conclusion
- •Chapter 2. Attack Patterns
- •A Taxonomy
- •An Open-Systems View
- •Tour of an Exploit
- •Attack Patterns: Blueprints for Disaster
- •An Example Exploit: Microsoft's Broken C++ Compiler
- •Applying Attack Patterns
- •Attack Pattern Boxes
- •Conclusion
- •Into the House of Logic
- •Should Reverse Engineering Be Illegal?
- •Reverse Engineering Tools and Concepts
- •Approaches to Reverse Engineering
- •Methods of the Reverser
- •Writing Interactive Disassembler (IDA) Plugins
- •Decompiling and Disassembling Software
- •Decompilation in Practice: Reversing helpctr.exe
- •Automatic, Bulk Auditing for Vulnerabilities
- •Writing Your Own Cracking Tools
- •Building a Basic Code Coverage Tool
- •Conclusion
- •Chapter 4. Exploiting Server Software
- •The Trusted Input Problem
- •The Privilege Escalation Problem
- •Finding Injection Points
- •Input Path Tracing
- •Exploiting Trust through Configuration
- •Specific Techniques and Attacks for Server Software
- •Conclusion
- •Chapter 5. Exploiting Client Software
- •Client-side Programs as Attack Targets
- •In-band Signals
- •Cross-site Scripting (XSS)
- •Client Scripts and Malicious Code
- •Content-Based Attacks
- •Conclusion
- •Chapter 6. Crafting (Malicious) Input
- •The Defender's Dilemma
- •Intrusion Detection (Not)
- •Partition Analysis
- •Tracing Code
- •Reversing Parser Code
- •Misclassification
- •Audit Poisoning
- •Conclusion
- •Chapter 7. Buffer Overflow
- •Buffer Overflow 101
- •Injection Vectors: Input Rides Again
- •Buffer Overflows and Embedded Systems
- •Database Buffer Overflows
- •Buffer Overflows and Java?!
- •Content-Based Buffer Overflow
- •Audit Truncation and Filters with Buffer Overflow
- •Causing Overflow with Environment Variables
- •The Multiple Operation Problem
- •Finding Potential Buffer Overflows
- •Stack Overflow
- •Arithmetic Errors in Memory Management
- •Format String Vulnerabilities
- •Heap Overflows
- •Buffer Overflows and C++
- •Payloads
- •Payloads on RISC Architectures
- •Multiplatform Payloads
- •Prolog/Epilog Code to Protect Functions
- •Conclusion
- •Chapter 8. Rootkits
- •Subversive Programs
- •A Simple Windows XP Kernel Rootkit
- •Call Hooking
- •Trojan Executable Redirection
- •Hiding Files and Directories
- •Patching Binary Code
- •The Hardware Virus
- •Low-Level Disk Access
- •Adding Network Support to a Driver
- •Interrupts
- •Key Logging
- •Advanced Rootkit Topics
- •Conclusion
- •References
- •Index
Tour of an Exploit
What happens when a software program is attacked? We introduce a simple house analogy to guide you through a software exploit. The "rooms" in our target software correspond to blocks of code in the software that perform some function. The job at hand is to understand
• Table of Contents
enough about the rooms to wander through the house at will.
•Index
EachxploitingblockSoftwarecodeHow(room)to BreakservesCode a unique purpose to the program. Some code blocks read
data from the network. If these blocks are rooms in a house and the attacker is standing
ByGreg Hoglund,Gary McGraw
outside the door on the porch, then networking code can be thought of as the foyer. Such
network code will be the first code to examine and respond to a remote attacker's input. In
Publisher: Addison Wesley
most cases, the network code merely accepts input and packages it into a data stream. This
Pub Date: February 17, 2004
stream is then passed deeper into the house to more complex code segments that parse the
ISBN: 0-201-78695-8
data. So the (network code) foyer is connected by internal doorways to adjacent, more complexPages:rooms512 . In the foyer, not much of interest to our attack can be accomplished, but directly connected to the foyer is a kitchen with many appliances. We like the kitchen, because the kitchen can, for example, open files and query databases. The attacker's goal is to find a path through the foyer into the kitchen.
How does software break? How do attackers make software break on purpose? Why are Thefirewalls,Attacker'sintrusion detectionViewpointsystems, and antivirus software not keeping out the bad guys? What tools can be used to break software? This book provides the answers.
An attack starts with breaking rules and undermining assumptions. One of the key Exploiting Softwareis loaded with examples of real attacks, attack patterns, tools, and
assumptions to test is the "implicit trust" assumption. Attackers will always break any rule techniques used by bad guys to break software. If you want to protect your software from
relating to when, where, and what is "allowed" to be submitted as input. For the same attack, you must first learn how real attacks are really carried out.
reasons that software blueprints are rarely made, software is only rarely subjected to
extensive "stress testing," especially stress testing that involves purposefully presenting This must-have book may shock you—and it will certainly educate you.Getting beyond the
malicious input. The upshot is that users are, for reasons of inherent laziness, trusted by script kiddie treatment found in many hacking books, you will learn about
default. An implicitly trusted user is trusted to supply correctly formed data that play by the
rules and are thus also implicitly "trusted."
Why software exploit will continue to be a serious problem
To make this clearer, we'll restate what's going on. The base assumption we'll work against is
that trustedWhen networkusers willsecuritynot supplymechanisms"malformed"do notorwork"malicious" data! One particular form of this
trust involves client software. If client software is written to send only certain commands,
implicitA tassumptiock pat erns are often made by the architects that a reasonable user will only use the
client software to access the server. The issue that goes un noticed is that attackers usually
Reverse engineering
write software. Clever attackers can write their own client software or hack up an existing
client. An attacker can (and will) craft custom client software capable of delivering malformed
Classic attacks against server software
inputon purpose and at just the right time. This is how the fabric of trust unravels.
Surprising attacks against client software
WhyTechniquesTrustingforUserscraftingIsmaliciousBad input
The technical details of buffer overflows
We now present a trivial example that shows how implicitly trusting a client unravels. Our
example involves the maxsize attribute of a Hypertext Markup Language (HTML) form. Forms
Rootkits
are a common way of querying users on a Web site for data. They are used extensively in
almost every type of Web-based transaction. Unfortunately, most Web forms expect to Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
receive proper input. software.
The developer who constructs a form has the ability to specify the maximum number of characters that a user is allowed to submit. For example, the following code limits the "username" field to ten characters:
<form action="login.cgi" method=GET>
<input maxlength=10 type="input" name="username">Username</input>
•Table of Contents
</form>
•Index
Exploiting Software How to Break Code
ByGreg Hoglund,Gary McGraw
A designer who misunderstands the underlying technology might assume that a remote user
Publisher: Addison Wesley
is limited to submitting only ten characters in the name field. What they might not realize is
Pub Date: February 17, 2004
that the enforcement of field length takes place on the remote user's machine, within the user's Web browser itself! The problem is that the remote user might have a Web browser that doesn't pay attention to the size restriction. Or the remote user might build a malicious browser that has this property (if they are an attacker). Or better yet, the remote user might not use a Web browser at all. A remote user can just submit the form request manually in a specially crafted uniform resource locator (URL):
Howhttp://victim/logindoes software.cgi?username=billthecatbreak? How do attackers make software break on purpose? Why are firewalls, intrusion detection systems, and antivirus software not keeping out the bad guys?
In any case, the remote user should most definitely not be trusted, and neither should the What tools can be used to b eak software? This book provides the answers.
remote user's software! There is absolutely nothing that prevents the remote user from submitExploitingaSoftwareURL suchisasloaded with examples of real attacks, attack patterns, tools, and techniques used by bad guys to break software. If you want to protect your software from
http://victim/login.cgi?username=THIS_IS_WAY_TOO_LONG_FOR_A_USERNAME a ack, you must first learn how real attacks are really carried out.
Assumptions involving trust, like the one presented here, make up secret doorways between This ust-have book may shock you—and it will certainly educate you.Getting beyond the
rooms in the house of logic. A clever user can use the "implicit trust" doorway to sneak right script kiddie treatment found in many hacking books, you will learn ab ut
through the foyer and into the kitchen.
Why software exploit will continue to be a serious problem
Like a Lock Pick
When network security mechanisms do not work
AnattackerAttack mustpatternscarefully craft attack input as data to be presented in a particular order.
Each bit of data in the attack is like a key that opens a code path door. The complete attack is
Reverse engineering
like a set of keys that unlocks the internal code paths of the program, one door at a time.
Note that this set of keys must be used in the precise order that they appear on the key
Classic attacks against server software
chain. And once a key has been used, it must be discarded. In other words, an attack must
include presenting exactly the right data in exactly the right order. In this way, exploiting
Surprising attacks against client software software is like picking locks.
Techniques for crafting malicious input
Software is a matrix of decisions. The decisions translate into branches that connect blocks of
code to one another. Think of these branches as the doorways that connect rooms. Doors will
The technical details of buffer overflows
open if the attacker has placed the right data (the key) in the right order (location on the key
chain). Rootkits
Some of the code locations in the program make branching decisions based on user-supplied Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
data. This is where you can try a key. Although finding these code locations can be very timesoftware.
consuming, in some cases the process can be automated. Figure 2-2 diagrams the code
branches of a common File Transfer Protocol (FTP) server. The graph indicates which
branches are based on user-supplied data.
Figure 2-2. This graph illustrates the branching logic of a common
FTP server. Blocks indicate continuous code and lines indicate jumps
and conditional branches between code blocks. Blocks outlined in
bold indicate that user-supplied data are being processed.
•Table
•Index
Exploiting Software
ByGreg Hoglund,Gary
Publisher: Addison
Pub Date: February
ISBN: 0-201-
Pages: 512
How does software |
Why are |
firewalls, intrusion |
the bad guys? |
What tools can be |
|
Exploiting Software |
tools, and |
techniques used |
software from |
attack, you must |
|
This must-have book may shock you—and it will certainly educate you.Getting beyond the script kiddie treatment found in many hacking books, you will learn about
Graphing of the sort shown in Figure 2-2 is a powerful tool when reverse engineering
Why software exploit will continue to be a serious problem
software. However, sometimes a more sophisticated view is needed. Figure 2-3 shows a more
sophisticated three-dimensional graph that also illuminates program structure. When network security mechanisms do not work
Attack patterns
FigureRev rse2-engineering3. This graph is rendered in three dimensions. Each code
location looks like a small room. We used the OpenGL package to
Classic attacks against server software
illustrate all the code paths leading toward a vulnerable sprintf call
in a target program.
Surprising attacks against client software
Techniques for crafting malicious input
[View full size image]
The technical details of buffer overflows
Rootkits
Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
software.
•
•
Exploiting
ByGreg
Publisher
Pub Date
ISBN
Pages
How does |
are |
firewalls, |
guys? |
What tools |
|
Exploiting |
and |
techniques used by bad guys to break software. If you want to protect your software from
attack, you must first learn how real attacks are really carried out.
Inside particular program rooms, different parts of a user's request are processed. Debugging
toolsThis mustcan help-haveyoubookto determinemay shockwhatyou—sortandofit willprocessingcertainlyiseducatebeing doneyouwhere.Getting. Figurebeyond2-4the
showscript kiddiea disassemblytreatmentoffounda single manycode locationhackingfrombooks,a targetyou willprogramlearn about. Going by our analogy,
this code appears in a single room in the house (one of the many boxes shown in the earlier
figures). The attacker can use information like this to shape an attack, room by room. Why software exploit will continue to be a serious problem
When network security mechanisms do not work
Figure 2-4. Disassembly of one "room" in the target program. The
Attack patterns
code at the top of the listing is a set of program instructions. The
instructions that deal with user-supplied data are called out at the
Reverse e gineering
bottom of the listing. Exploiting software usually involves
understandingClassic attacks a ainst server software
both how data flow in a program (especially user
data) and how data are processed in given code blocks.
Surprising attacks against client software
Techniques for crafting malicious input
[View full size image]
The
Exploiting |
break |
software.
A Simple Example
Consider an exploit in which the attacker executes a shell command on the target system. The particular software bug responsible for causing the vulnerability might be a code snippet like this:
•Table of Contents
•Index
Exploiting Software How to Break Code
ByGreg Hoglund,Gary McGraw
Publisher: Addison Wesley
Pub Date: February 17, 2004
ISBN: 0-201-78695-8
Pages: 512
$username = ARGV; #user-supplied data
system("cat /logs/$username" . ".log");
How does software break? How do attackers make software break on purpose? Why are firewalls, intrusion detection systems, and antivirus software not keeping out the bad guys?
What tools can be used to break software? This bookparameterprovid s the answers.
Note that the call to the system() function takes a that is un checked. Assume, for
this example, that the username parameter is delivered from an HTTP cookie. The HTTP
Exploiting Softwareis loaded with examples of real attacks, attack patterns, tools, and cookie is a small data file that is controlled entirely by the remote user (and is typically
techniques used by bad guys to break software. If you want to protect your software from stored in a Web browser). Software security-savvy developers know that a cookie is
attack, you must first learn how real attacks are really carried out.
something that should never be trusted (unless you can cryptographically protect and verify
it).
This must-have book may shock you—and it will certainly educate you.Getting beyond the
script kiddie treatment found in many hacking books, you will learn about
The vulnerability we exploit in this example arises because untrusted cookie data are being
passed into and used in a shell command. In most systems, shell commands have some level
of system-level access, and if a clever attacker supplies just the right sequence of characters
Why software exploit will continue to be a serious problem
as the "username," the attacker can issue commands that control the system.
When network security mechanisms do not work
Let's examine this in a bit more detail. If the remote user types in the string bracken,
corresponding to a name, then the resulting command sent through the system() call of our Attack patterns
code snippet will be
Reverse engineering
Classic attacks against server software
Surprising attacks against client software
Techniques for crafting malicious input
The technical details of buffer overflows
cat /logs/bracken.log
Rootkits
Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
software.
This shell command displays the contents of the file bracken.log in the directory/logs in the
Web browser. If the remote user supplies a different username, such as nosuchuser, the
resulting command will be
cat /logs/nosuchuser.log
•Table of Contents
•Index
If the file nosuchuser.log does not exist, a minor "error" occurs and is reported. No other
Exploiting Software How to Break Code
data are displayed. From the perspective of an attacker, causing a minor error like this is no
ByGreg Hoglund,Gary McGraw
big deal, but it does give us an idea. Because we control the username variable, we can insert whatever characters we choose as the username we supply. The shell command is fairlyPublisher:complexAddisonandWesleyit understands lots of complex character sequences. We can take
advantage of this fact to have some fun.
Pub Date: February 17, 2004
ISBN: 0-201-78695-8
Let's explore what happens when we supply just the right characters in just the right order.
Pages: 512
Consider the funny-sounding username "../etc/passwd." This results in the following command being run for us:
How does software break? How do attackers make software break on purpose? Why are firewalls, intrusion detection systems, and antivirus software not keeping out the bad guys? What tools can be used to break software? This book provides the answers.
Exploiting Softwareis loaded with examples of real attacks, attack patterns, tools, and techniques used by bad guys to break software. If you want to protect your software from
attack, you must first learn how real attacks are really carried out. cat /logs/../etc/passwd.log
This must-have book may shock you—and it will certainly educate you.Getting beyond the script kiddie treatment found in many hacking books, you will learn about
We are using a classic directory redirection trick to display the file /etc/passwd.log. So as an
Why software exploit will continue to be a serious problem
attacker, we wield complete control of the filename that is being passed to the cat command.
Too bad there isn't a file called /etc/passwd.log on most UNIX systems! When network security mechanisms do not work
Our exploit so far is pretty simple and isn't getting us very far. With a little more cleverness, Attack patterns
we can add another command to the mix. Because we can control the contents of the
command string after cat ..., we can use a trick to add a new command to the mix. Reverse engineering
Consider a devious username, such as "bracken; rm –rf /; cat blah," which results in three Classic attacks against server software
commands being run, one after the other. The second command comes after the first ";" and thethirdSurprisingafter theattackssecondagainst";": client software
Techniques for crafting malicious input
The technical details of buffer overflows
Rootkits
Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
software.
cat /logs/bracken; rm –rf /; cat blah.log
With this simple attack we're using the multiple-command trick to remove all the files recursively from the root directory / (and making the system "just do it" and not ask us any Macintosh-like questions). After we do this, the unfortunate victim will be left with a root
directory and perhaps a lost-and-found directory at most. That's some pretty serious damage that can be inflicted simply as the result of one single username vulnerability on a broken Web site!
It's very important to notice that we chose the value of the username in an intelligent fashion so that the final command string will be formatted correctly and the embedded malicious commands will be properly executed. Because the ";" character is used to separate multiple commands to the system (a UNIX box), we're actually doing three commands here. But this
• |
Table of Contents |
attack isn't all that smart! The final part of the command that runs cat blah.log is unlikely |
|
• |
Index |
to be successful! We deleted all the files!
Exploiting Software How to Break Code
SoByGregall inHoglundall, this,GarysimpleMcGrawattack is about controlling strings of data and leveraging system-level
language syntax.
Publisher: Addison Wesley
Of course our example attack is trivial, but it shows what can result when the target software
Pub Date: February 17, 2004
is capable of running commands on a system that are supplied from an untrusted source.
ISBN: 0-201-78695-8
Stated in terms of the house analogy, there was an overlooked door that allows a malicious
Pages: 512
user to control which commands the program ends up executing.
In this kind of attack we're only exercising preexisting capabilities built right into the target. As we will see, there are far more powerful attacks that completely bypass the capabilities of the target software using injected code (and even viruses). As an example, consider buffer
overflow attacks that are so powerful that they, in some sense, blast new doorways into the How does software break? How do attackers make software break on purpose? Why are
house of logic entirely, breaking down the control flow walls with a giant sledgehammer and firewalls, intrusion detection systems, and antivirus software not keeping out the bad guys?
chain saw. What we're trying to say here is that there exist direct attacks on the very What tools can be used to break software? This book provides the answers.
structure of a program, and sometimes these attacks rely on fairly deep knowledge about
how the house is built to begin with. Sometimes the knowledge required includes machine Exploiting Softwareis loaded examples of real attacks, attack patterns, tools, and
language and microchip architecture. Of course, attacks like this are a bit more complicated techniques used by bad guys o break software. If you want to protect your software from
than the simple one we showed you here.
attack, you must first learn how real attacks are really carried out.
This must-have book may shock you—and it will certainly educate you.Getting beyond the
script kiddie treatment found in many hacking books, you will learn about
Why software exploit will continue to be a serious problem
When network security mechanisms do not work
Attack patterns
Reverse engineering
Classic attacks against server software
Surprising attacks against client software
Techniques for crafting malicious input
The technical details of buffer overflows
Rootkits
Exploiting Softwareis filled with the tools, concepts, and knowledge necessary to break
software.