KEMBAR78
Exploitation techniques and fuzzing | PPTX
Exploitation Techniques and
Fuzzing
Index
• Vulnerability
• Classification of Vulnerabilities
• What is fuzzing?
• What data can be fuzzed?
• What does fuzzed data look like?
• When (not) to fuzz?
• Two approaches and a basic methodology
• Advanced techniques
• Unsolved challenges
Vulnerability
• ISO 27005 defines vulnerability as:
• A weakness of an asset or group of assets that can be exploited by one or more
threats.
• where an asset is anything that has value to the organization, its business
operations and their continuity, including information resources that support
the organization's mission.
• IETF RFC 2828 define vulnerability as:
• A flaw or weakness in a system's design, implementation, or operation and
management that could be exploited to violate the system's security policy.
Vulnerability
• A vulnerability is a weakness which can be exploited by a Threat Actor, such as
an attacker, to perform unauthorized actions within a computer system.
• To exploit a vulnerability, an attacker must have at least one applicable tool or
technique that can connect to a system weakness.
• A security risk is often incorrectly classified as a vulnerability.
• The risk is the potential of a significant impact resulting from the exploit of a
vulnerability.
• Then there are vulnerabilities without risk: for example when the
affected asset has no value.
Vulnerability
• A vulnerability with one or more known instances of working and fully
implemented attacks is classified as an exploitable vulnerability—a vulnerability
for which an exploit exists.
• The window of vulnerability is the time from when the security hole was
introduced or manifested in deployed software, to when access was removed, a
security fix was available/deployed, or the attacker was disabled—Example:-
zero-day attack.
Classification of Vulnerability
• Vulnerabilities are classified according to the asset class they are related to:
• Hardware
• susceptibility to humidity
• susceptibility to dust
• susceptibility to soiling
• susceptibility to unprotected storage
• Software
• insufficient testing
• lack of audit trail
• design flaw
Classification of Vulnerability
• Network
• unprotected communication lines
• insecure network architecture
• Personnel
• inadequate recruiting process
• inadequate security awareness
• Physical site
• area subject to flood
• unreliable power source
Privilege Escalation Vulnerability
• Privilege escalation is the act of exploiting a bug, design flaw or configuration
oversight in an operating system or software application to gain elevated access
to resources that are normally protected from an application or user.
• The result is that an application with more privileges than intended by
the application developer or system administrator can
perform unauthorized actions.
• Privilege escalation occurs in two forms:
• Vertical privilege escalation
• Horizontal privilege escalation
Vertical Privilege Escalation
• It is also known as privilege elevation.
• Where a lower privilege user or application accesses functions or content reserved
for higher privilege users or applications (e.g. Internet Banking users can access
site administrative functions or the password for a smartphone can be bypassed.)
• This type of privilege escalation occurs when the user or process is able to obtain
a higher level of access than an administrator or system developer intended,
possibly by performing kernel-level operations.
• Examples
• some cases, a high-privilege application assumes that it would only be provided
with input matching its interface specification, thus doesn't validate this input.
Then, an attacker may be able to exploit this assumption, in order to run
unauthorized code with the application's privileges:
• Examples
• Some Windows services are configured to run under the Local System user
account. A vulnerability such as a buffer overflow may be used to execute
arbitrary code with privilege elevated to Local System. Alternatively, a system
service that is impersonating a lesser user can elevate that user's privileges if
errors are not handled correctly while the user is being impersonated (e.g. if
the user has introduced a malicious error handler)
• Under some legacy versions of the Microsoft Windows operating system, the
All Users screensaver runs under the Local System account – any account that
can replace the current screensaver binary in the file system or Registry can
therefore elevate privileges.
Horizontal privilege escalation
• Where a normal user accesses functions or content reserved for other normal users
(e.g. Internet Banking User A accesses the Internet bank account of User B).
• The result is that the application performs actions with the same but different
security context than intended by the application developer or system
administrator; this is effectively a limited form of privilege escalation
(specifically, the unauthorized assumption of the capability of impersonating other
users).
• Examples:
• This problem often occurs in web applications. Consider the following
example:
• User A has access to their own bank account in an Internet Banking
application.
• User B has access to their own bank account in the same Internet Banking
application.
• The vulnerability occurs when User A is able to access User B's bank account
by performing some sort of malicious activity.
• Potential web application vulnerabilities or situations that may lead to this
condition include:
• Session fixation
• Cross-site Scripting
• Easily guessable passwords
• Theft or hijacking of session cookies
• Keystroke logging
Privilege rings for the x86
Win32k Elevation of Privilege Vulnerability (CVE-2015-0057)
• This vulnerability involving a flaw in a GUI component of Windows 10—
namely the scrollbar element—allows a threat actor to gain complete control
of a Windows machine through privilege escalation.
• win32k.sys in the kernel-mode drivers in Microsoft Windows Server 2003
SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7
SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and
Windows RT Gold and 8.1.
Buffer overflow
• A buffer overflow, or buffer overrun, is an anomaly where a program, while
writing data to a buffer, overruns the buffer's boundary and overwrites
adjacent memory locations.
• Buffers are areas of memory set aside to hold data, often while moving it from one
section of a program to another, or between programs.
• Exploiting the behaviour of a buffer overflow is a well-known security exploit.
• The famed Morris worm in 1988 used this as one of its attack techniques.
What are buffer overflows?
• Suppose a web server contains a function:
void func(char *str) {
char buf[128];
strcpy(buf, str);
do-something(buf);
}
• When the function is invoked the stack looks like:
• What if *str is 136 bytes long? After strcpy:
strret-addrsfpbuf
top
of
stack
str
top
of
stack
*str ret
Many unsafe C lib functions
strcpy (char *dest, const char *src)
strcat (char *dest, const char *src)
gets (char *s)
scanf ( const char *format, … )
• “Safe” versions strncpy(), strncat() are misleading
• strncpy() may leave buffer unterminated.
• strncpy(), strncat() encourage off by 1 bugs.
Buffer Overflow Exploitation
• Stack-based exploitation
• Heap-based exploitation
• Barriers to exploitation
• Practicalities of exploitation
• NOP sled technique
• The jump to address stored in a register technique
Dangling pointer
• Dangling pointers arise during object destruction, when an object that has an
incoming reference is deleted or deallocated, without modifying the value of the
pointer, so that the pointer still points to the memory location of the deallocated
memory.
• The system may reallocate the previously freed memory, and if the program
then dereferences the (now) dangling pointer, unpredictable behaviour may result,
as the memory may now contain completely different data.
• Wild pointers arise when a pointer is used prior to initialization to some known
state, which is possible in some programming languages.
• They show the same erratic behaviour as dangling pointers, though they are less
likely to stay undetected because many compilers will raise a warning at compile
time if declared variables are accessed before being initialized.
Code injection
• Code injection is the exploitation of a computer bug that is caused by processing
invalid data. Injection is used by an attacker to introduce (or "inject") code into a
vulnerable computer program and change the course of execution.
• The result of successful code injection can be disastrous, for example by
allowing computer worms to propagate.
• Code injection vulnerabilities (injection flaws) occur when an application sends
untrusted data to an interpreter.
• Injection flaws are most often found in SQL, LDAP, XPath, or NoSQL queries;
OS commands; XML parsers, SMTP headers, program arguments, etc. Injection
flaws tend to be easier to discover when examining source code than via testing.
• Injection can result in data loss or corruption, lack of accountability, or denial of
access. Injection can sometimes lead to complete host takeover.
• Code injection techniques are popular in system hacking or cracking to gain
information, privilege escalation or unauthorized access to a system.
Examples:
• SQL Injection
• HTML script injection
• Dynamic evaluation vulnerabilities
• Object injection
• Remote file injection
• Format Specifier Injection
• Shell injection
What is fuzzing ?
• Feed target automatically generated malformed data designed to trigger
implementation flaws
• A fuzzer is the programmatic construct to do this
• A fuzzing framework typically includes library code to:
• Generate fuzzed data
• Deliver test cases
• Monitor the target
• Publicly available fuzzing frameworks:
• Spike, Peach Fuzz, Sulley, Schemer
• Requirement of Microsoft’s Secure Development Lifecycle program
• Still a long way to go - many vendors do no fuzzing!
What data can be fuzzed?
• Virtually anything!
• Basic types: bit, byte, word, dword, qword
• Common language specific types: strings, structs, arrays
• High level data representations: text, xml
Where can data be fuzzed?
• Across any security boundary, e.g.:
• An RPC interface on a remote/local machine
• HTTP responses & HTML content served to a browser
• Any file format, e.g. Office document
• Data in a shared section
• Parameters to a system call between user and kernel mode
• HTTP requests sent to a web server
• File system metadata
• ActiveX methods
• Arguments to SUID binaries
What does fuzzed data consist of?
• Fuzzing at the type level:
• Long strings, strings containing special characters, format strings
• Boundary case byte, word, dword, qword values
• Random fuzzing of data buffers
• Fuzzing at the sequence level
• Fuzzing types within sequences
• Nesting sequences a large number of times
• Adding and removing sequences
• Random combinations
• Always record the random seed!!
When to fuzz?
• Fuzzing typically finds implementation flaws, e.g.:
• Memory corruption in native code
• Stack and heap buffer overflows
• Un-validated pointer arithmetic (attacker controlled offset)
• Integer overflows
• Resource exhaustion (disk, CPU, memory)
• Unhandled exceptions in managed code
• Format exceptions (e.g. parsing unexpected types)
• Memory exceptions
• Null reference exceptions
• Injection in web applications
• SQL injection against backend database
• LDAP injection
• HTML injection (Cross-site scripting)
• Code injection
When not to fuzz
• Fuzzing typically does not find logic flaws
• Malformed data likely to lead to crashes, not logic flaws
• e.g. Missing authentication / authorization checks
• Fuzzing does not find design/repurposing flaws
• e.g. A sitelocked ActiveX control with a method named “RunCmd”.
• However transitions in a state machine can be fuzzed...
• Send well-formed requests out of order
• But how to know when you’ve found a bug?
Two approaches
“Dumb” “Smart”
• Fuzzer lacks contextual informational
about data it is manipulating
• May produce totally invalid test cases
• Up and running fast
• Find simple issues in poor quality
code bases
• Fuzzer is context-aware
• Can handle relations between
entities, e.g. block header lengths,
CRCs
• Produces partially well-formed test
cases
• Time consuming to create
• What if protocol is proprietary?
• Can find complex issues
Pseudo-code for dumb fuzzer
for each {byte|word|dword|qword} aligned location in file
for each bad_value in bad_valueset
{
file[location] := bad_value
deliver_test_case()
}
...
o_jpeg = fz3AddObjectToList( NULL, TYPE_BYTE, PTR(0xff), 1 ); // new header
fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xd8), 1 ); // unknown type
(start of file?)
fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xff), 1 ); // new header
fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xe0), 1 ); // extension app0
marker segment
o_jfif_len = fz3AddObjectToList( o_jpeg, TYPE_WORD, BE_W(0x10), 2 ); // length
o_jfif = fz3AddObjectToList( o_jpeg, TYPE_COLLECTION, NULL, 0 );
o_jfif_dat = fz3AddObjectToList( NULL, TYPE_COLLECTION, NULL, 0 );
fz3AddObjectToList( o_jfif_dat, TYPE_STATIC, "JFIF", 5 ); // APP0 marker
fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x0102), 2 ); // version
fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // units
fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x64), 2 ); // x density
fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x64), 2 ); // y density
fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // x thumbnail
fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // y thumbnail
…
fz3AddAdditionalDataToObject( o_jfif, TYPE_COLLECTION, (BYTE *)o_jfif_dat, sizeof(object *) );
…
fz3SetObjectCallback( o_jfif_len, JPEG_set_length, o_jfif );
…
Sample config for smart fuzzer (1)
Sample config for smart fuzzer (2)
Two approaches cont.
• Which approach is better?
• Depends on:
• Time: how long to develop and run fuzzer
• [Security] Code quality of target
• Amount of validation performed by target
• Can patch out CRC check to allow dumb fuzzing
• Complexity of relations between entities in data format
• Don’t rule out either!
• My personal approach: get a dumb fuzzer working first
• Run it while you work on a smart fuzzer
Fuzzing in practice: the basic steps
Monitor Target
Generate next test case
Deliver test case
Target
crashed? Save crash dump
Any more
test cases?
Finish
Start
Monitoring the target
1. Attach a debugger
• Leverage existing functionality
• Scripting, logging, crash dumps etc.
Monitoring the target
2. Write your own debugger
• Actually easy to do
• Lightweight, fast, full control
C++
BOOL WINAPI WaitForDebugEvent(
__out LPDEBUG_EVENT lpDebugEvent,
__in DWORD dwMilliseconds
);
typedef struct _DEBUG_EVENT { /* de */
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union { EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcess;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString; }
u; } DEBUG_EVENT, *LPDEBUG_EVENT;
Monitoring the target
3. Monitor resources:
• File, registry, memory, CPU, logs
Deliver the test case
1. Standalone test harness
• E.g. to launch to client application and have it load fuzzed file format
2. Instrumented client
• Inject function hooking code into target client
• Intercept data and substitute with fuzzed data
• Useful if:
• State machine is complex
• Data is encoded in a non-standard format
• Data is signed or encrypted
Determining exploitability
Determining exploitability
• This process requires experience of debugging security issues, but some steps can
be taken to gain a good idea of how exploitable an issue is…
• Look for any cases where data is written to a controllable address – this is key to
controlling code execution and the majority of such conditions will be exploitable
• Verify whether any registers have been overwritten, if they do not contain part
data sent from the fuzzer, step back in the disassembly to try and find where the
data came from.
• If the register data is controllable, point the register which caused the crash to a
page of memory which is empty, fill that page with data (e.g., ‘aaaaa…’)
Determining exploitability
• Are saved return address/stack variables overwritten?
• Is the crash in a heap management function?
• Are the processor registers derived from data sent by the fuzzer (e.g.
0x61616161)?
• Is the crash triggered by a read operation?
• Can we craft a test case to avoid this?
• Is the crash triggered by a write operation?
• Do we have full or partial control of the faulting address?
• Do we have full or partial control of the written value?
Determining exploitability
• Recent advances – Microsoft !Exploitable
• WinDbg extension to determine exploitability
• Does lightweight taint analysis
• Uses meta-instructions (STACK_PUSH, DATA_MOVE etc.)
• Demoed at CanSec West conference, March 2009
A quick diversion
• These days you can get paid for finding vulnerabilities:
• iDefense, Tipping Point, Pwn2Own
• Write a fuzzer, find bugs.
• Here’s how to get started:
• Download FileFuzz (iDefense)
• Pick a media application and find some samples
• Fuzz! You will find bugs…
A quick diversion
• Code execution in Internet Explorer via 3rd party ActiveX control
A quick diversion
• Vulnerability in widely used enterprise-level software
A quick diversion
• Remotely exploitability vulnerability in critical infrastructure/the core OS
A quick diversion
• Why isn’t everyone in the security community rich…
• The best (= $$$) targets are the most secure
• Few remote vulnerabilities in Vista
• Even less in IIS or Apache
• Clued up vendors are fuzzing:
• Windows Vista file fuzzing effort in numbers:
• 350M iterations total
• 250+ file parsers fuzzed
• 300+ issues fixed
Advanced fuzzing
• Problems with our basic model:
• Fuzzer and target are not in sync
• Fuzzer has no feedback mechanism
• We have no visibility into fuzzer’s effectiveness
• Revised model:
• Send good requests at regular intervals to check health of target
• Fuzzer is able to control target debugger
• Use a code coverage build if possible
Advanced fuzzing
• Fuzzer issues debugger commands
• Can restart target
• Can handle resource exhaustion bugs, e.g. 100% CPU
• Can save callstack, crash dump etc.
• Fuzzing isolated states in a state machine
• Save and restore “state” of process
• See Hoglund, McGraw “Exploiting Software”
• Constrain execution to a single state with breakpoints
• Substitute real data for fuzzed data
• But how to handle handles?
Example
• Microsoft Exchange Remote Code Execution
• Critical Microsoft hotfix, MS06-003 (Heasman & Litchfield)
• Run code on a mail server with a single email!
• Bonus: same bug affected Outlook 2003!
• We found this by fuzzing email…
• Rich text email often has a “winmail.dat” attachment
• This is a binary Transport Neutral Encapsulation Format
• Lets fuzz it!
Example
• TNEF is a Tag Length Value (TLV) format
• Our bug: triggered by tag length of 0xFFFFFFFF for certain properties
• Any ideas what was going on?
• Integer overflow!
• Small memory allocation followed by a large memcpy
• We trash the heap, overwriting heap control structures
• End up with arbitrary DWORD overwrite which we use to get code execution
0xFFFFFFFF + 1 = 0
Example
• Interesting asides: 5 years earlier…
• Bugtraq Security Mailing List - August 2000
• "As a side note it would be an interesting excercise to see if Outlook is
susceptible to a message with a malformed winmail.dat attached. One could
theoretically use winmail.dat to hit on holes in either Outlook itself, or the
Outlook RTF engine"
• February 2009 - MS09-003 “Vulnerabilities in Microsoft Exchange Could Allow
Remote Code Execution”
• TNEF again!
Existing challenges
• Fuzzing likely to trigger same bug repeatedly
• Automatically remove duplicate bugs
• Compare call stacks, register values, memory locations
• What if they are trashed?
• Or slightly different?
• Seems like this problem would benefit from a fuzzy approach
• How to implement code coverage on a binary?
• Without degrading performance
• How to effectively use code coverage to direct fuzzing ?
Existing challenges
• How to measure effectiveness of a fuzzer?
• Number of test cases?
• Number of bugs?
• Severity of bugs?
• % Code coverage?
• How many test cases to run?
• How to balance complexity vs. time
constraints?

Exploitation techniques and fuzzing

  • 1.
  • 2.
    Index • Vulnerability • Classificationof Vulnerabilities • What is fuzzing? • What data can be fuzzed? • What does fuzzed data look like? • When (not) to fuzz? • Two approaches and a basic methodology • Advanced techniques • Unsolved challenges
  • 3.
    Vulnerability • ISO 27005defines vulnerability as: • A weakness of an asset or group of assets that can be exploited by one or more threats. • where an asset is anything that has value to the organization, its business operations and their continuity, including information resources that support the organization's mission. • IETF RFC 2828 define vulnerability as: • A flaw or weakness in a system's design, implementation, or operation and management that could be exploited to violate the system's security policy.
  • 4.
    Vulnerability • A vulnerabilityis a weakness which can be exploited by a Threat Actor, such as an attacker, to perform unauthorized actions within a computer system. • To exploit a vulnerability, an attacker must have at least one applicable tool or technique that can connect to a system weakness. • A security risk is often incorrectly classified as a vulnerability. • The risk is the potential of a significant impact resulting from the exploit of a vulnerability. • Then there are vulnerabilities without risk: for example when the affected asset has no value.
  • 5.
    Vulnerability • A vulnerabilitywith one or more known instances of working and fully implemented attacks is classified as an exploitable vulnerability—a vulnerability for which an exploit exists. • The window of vulnerability is the time from when the security hole was introduced or manifested in deployed software, to when access was removed, a security fix was available/deployed, or the attacker was disabled—Example:- zero-day attack.
  • 6.
    Classification of Vulnerability •Vulnerabilities are classified according to the asset class they are related to: • Hardware • susceptibility to humidity • susceptibility to dust • susceptibility to soiling • susceptibility to unprotected storage • Software • insufficient testing • lack of audit trail • design flaw
  • 7.
    Classification of Vulnerability •Network • unprotected communication lines • insecure network architecture • Personnel • inadequate recruiting process • inadequate security awareness • Physical site • area subject to flood • unreliable power source
  • 8.
    Privilege Escalation Vulnerability •Privilege escalation is the act of exploiting a bug, design flaw or configuration oversight in an operating system or software application to gain elevated access to resources that are normally protected from an application or user. • The result is that an application with more privileges than intended by the application developer or system administrator can perform unauthorized actions. • Privilege escalation occurs in two forms: • Vertical privilege escalation • Horizontal privilege escalation
  • 9.
    Vertical Privilege Escalation •It is also known as privilege elevation. • Where a lower privilege user or application accesses functions or content reserved for higher privilege users or applications (e.g. Internet Banking users can access site administrative functions or the password for a smartphone can be bypassed.) • This type of privilege escalation occurs when the user or process is able to obtain a higher level of access than an administrator or system developer intended, possibly by performing kernel-level operations. • Examples • some cases, a high-privilege application assumes that it would only be provided with input matching its interface specification, thus doesn't validate this input. Then, an attacker may be able to exploit this assumption, in order to run unauthorized code with the application's privileges:
  • 10.
    • Examples • SomeWindows services are configured to run under the Local System user account. A vulnerability such as a buffer overflow may be used to execute arbitrary code with privilege elevated to Local System. Alternatively, a system service that is impersonating a lesser user can elevate that user's privileges if errors are not handled correctly while the user is being impersonated (e.g. if the user has introduced a malicious error handler) • Under some legacy versions of the Microsoft Windows operating system, the All Users screensaver runs under the Local System account – any account that can replace the current screensaver binary in the file system or Registry can therefore elevate privileges.
  • 11.
    Horizontal privilege escalation •Where a normal user accesses functions or content reserved for other normal users (e.g. Internet Banking User A accesses the Internet bank account of User B). • The result is that the application performs actions with the same but different security context than intended by the application developer or system administrator; this is effectively a limited form of privilege escalation (specifically, the unauthorized assumption of the capability of impersonating other users).
  • 12.
    • Examples: • Thisproblem often occurs in web applications. Consider the following example: • User A has access to their own bank account in an Internet Banking application. • User B has access to their own bank account in the same Internet Banking application. • The vulnerability occurs when User A is able to access User B's bank account by performing some sort of malicious activity. • Potential web application vulnerabilities or situations that may lead to this condition include: • Session fixation • Cross-site Scripting • Easily guessable passwords • Theft or hijacking of session cookies • Keystroke logging
  • 13.
  • 14.
    Win32k Elevation ofPrivilege Vulnerability (CVE-2015-0057) • This vulnerability involving a flaw in a GUI component of Windows 10— namely the scrollbar element—allows a threat actor to gain complete control of a Windows machine through privilege escalation. • win32k.sys in the kernel-mode drivers in Microsoft Windows Server 2003 SP2, Windows Vista SP2, Windows Server 2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and Windows RT Gold and 8.1.
  • 15.
    Buffer overflow • Abuffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory locations. • Buffers are areas of memory set aside to hold data, often while moving it from one section of a program to another, or between programs. • Exploiting the behaviour of a buffer overflow is a well-known security exploit. • The famed Morris worm in 1988 used this as one of its attack techniques.
  • 16.
    What are bufferoverflows? • Suppose a web server contains a function: void func(char *str) { char buf[128]; strcpy(buf, str); do-something(buf); } • When the function is invoked the stack looks like: • What if *str is 136 bytes long? After strcpy: strret-addrsfpbuf top of stack str top of stack *str ret
  • 17.
    Many unsafe Clib functions strcpy (char *dest, const char *src) strcat (char *dest, const char *src) gets (char *s) scanf ( const char *format, … ) • “Safe” versions strncpy(), strncat() are misleading • strncpy() may leave buffer unterminated. • strncpy(), strncat() encourage off by 1 bugs.
  • 18.
    Buffer Overflow Exploitation •Stack-based exploitation • Heap-based exploitation • Barriers to exploitation • Practicalities of exploitation • NOP sled technique • The jump to address stored in a register technique
  • 19.
    Dangling pointer • Danglingpointers arise during object destruction, when an object that has an incoming reference is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. • The system may reallocate the previously freed memory, and if the program then dereferences the (now) dangling pointer, unpredictable behaviour may result, as the memory may now contain completely different data.
  • 20.
    • Wild pointersarise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. • They show the same erratic behaviour as dangling pointers, though they are less likely to stay undetected because many compilers will raise a warning at compile time if declared variables are accessed before being initialized.
  • 21.
    Code injection • Codeinjection is the exploitation of a computer bug that is caused by processing invalid data. Injection is used by an attacker to introduce (or "inject") code into a vulnerable computer program and change the course of execution. • The result of successful code injection can be disastrous, for example by allowing computer worms to propagate. • Code injection vulnerabilities (injection flaws) occur when an application sends untrusted data to an interpreter.
  • 22.
    • Injection flawsare most often found in SQL, LDAP, XPath, or NoSQL queries; OS commands; XML parsers, SMTP headers, program arguments, etc. Injection flaws tend to be easier to discover when examining source code than via testing. • Injection can result in data loss or corruption, lack of accountability, or denial of access. Injection can sometimes lead to complete host takeover. • Code injection techniques are popular in system hacking or cracking to gain information, privilege escalation or unauthorized access to a system.
  • 23.
    Examples: • SQL Injection •HTML script injection • Dynamic evaluation vulnerabilities • Object injection • Remote file injection • Format Specifier Injection • Shell injection
  • 24.
    What is fuzzing? • Feed target automatically generated malformed data designed to trigger implementation flaws • A fuzzer is the programmatic construct to do this • A fuzzing framework typically includes library code to: • Generate fuzzed data • Deliver test cases • Monitor the target
  • 25.
    • Publicly availablefuzzing frameworks: • Spike, Peach Fuzz, Sulley, Schemer • Requirement of Microsoft’s Secure Development Lifecycle program • Still a long way to go - many vendors do no fuzzing!
  • 26.
    What data canbe fuzzed? • Virtually anything! • Basic types: bit, byte, word, dword, qword • Common language specific types: strings, structs, arrays • High level data representations: text, xml
  • 27.
    Where can databe fuzzed? • Across any security boundary, e.g.: • An RPC interface on a remote/local machine • HTTP responses & HTML content served to a browser • Any file format, e.g. Office document • Data in a shared section • Parameters to a system call between user and kernel mode • HTTP requests sent to a web server • File system metadata • ActiveX methods • Arguments to SUID binaries
  • 28.
    What does fuzzeddata consist of? • Fuzzing at the type level: • Long strings, strings containing special characters, format strings • Boundary case byte, word, dword, qword values • Random fuzzing of data buffers • Fuzzing at the sequence level • Fuzzing types within sequences • Nesting sequences a large number of times • Adding and removing sequences • Random combinations • Always record the random seed!!
  • 29.
    When to fuzz? •Fuzzing typically finds implementation flaws, e.g.: • Memory corruption in native code • Stack and heap buffer overflows • Un-validated pointer arithmetic (attacker controlled offset) • Integer overflows • Resource exhaustion (disk, CPU, memory) • Unhandled exceptions in managed code • Format exceptions (e.g. parsing unexpected types) • Memory exceptions • Null reference exceptions • Injection in web applications • SQL injection against backend database • LDAP injection • HTML injection (Cross-site scripting) • Code injection
  • 30.
    When not tofuzz • Fuzzing typically does not find logic flaws • Malformed data likely to lead to crashes, not logic flaws • e.g. Missing authentication / authorization checks • Fuzzing does not find design/repurposing flaws • e.g. A sitelocked ActiveX control with a method named “RunCmd”. • However transitions in a state machine can be fuzzed... • Send well-formed requests out of order • But how to know when you’ve found a bug?
  • 31.
    Two approaches “Dumb” “Smart” •Fuzzer lacks contextual informational about data it is manipulating • May produce totally invalid test cases • Up and running fast • Find simple issues in poor quality code bases • Fuzzer is context-aware • Can handle relations between entities, e.g. block header lengths, CRCs • Produces partially well-formed test cases • Time consuming to create • What if protocol is proprietary? • Can find complex issues
  • 32.
    Pseudo-code for dumbfuzzer for each {byte|word|dword|qword} aligned location in file for each bad_value in bad_valueset { file[location] := bad_value deliver_test_case() }
  • 33.
    ... o_jpeg = fz3AddObjectToList(NULL, TYPE_BYTE, PTR(0xff), 1 ); // new header fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xd8), 1 ); // unknown type (start of file?) fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xff), 1 ); // new header fz3AddObjectToList( o_jpeg, TYPE_BYTE, PTR(0xe0), 1 ); // extension app0 marker segment o_jfif_len = fz3AddObjectToList( o_jpeg, TYPE_WORD, BE_W(0x10), 2 ); // length o_jfif = fz3AddObjectToList( o_jpeg, TYPE_COLLECTION, NULL, 0 ); o_jfif_dat = fz3AddObjectToList( NULL, TYPE_COLLECTION, NULL, 0 ); fz3AddObjectToList( o_jfif_dat, TYPE_STATIC, "JFIF", 5 ); // APP0 marker fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x0102), 2 ); // version fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // units fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x64), 2 ); // x density fz3AddObjectToList( o_jfif_dat, TYPE_WORD, BE_W(0x64), 2 ); // y density fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // x thumbnail fz3AddObjectToList( o_jfif_dat, TYPE_BYTE, PTR(0), 1 ); // y thumbnail … fz3AddAdditionalDataToObject( o_jfif, TYPE_COLLECTION, (BYTE *)o_jfif_dat, sizeof(object *) ); … fz3SetObjectCallback( o_jfif_len, JPEG_set_length, o_jfif ); … Sample config for smart fuzzer (1)
  • 34.
    Sample config forsmart fuzzer (2)
  • 35.
    Two approaches cont. •Which approach is better? • Depends on: • Time: how long to develop and run fuzzer • [Security] Code quality of target • Amount of validation performed by target • Can patch out CRC check to allow dumb fuzzing • Complexity of relations between entities in data format • Don’t rule out either! • My personal approach: get a dumb fuzzer working first • Run it while you work on a smart fuzzer
  • 36.
    Fuzzing in practice:the basic steps Monitor Target Generate next test case Deliver test case Target crashed? Save crash dump Any more test cases? Finish Start
  • 37.
    Monitoring the target 1.Attach a debugger • Leverage existing functionality • Scripting, logging, crash dumps etc.
  • 38.
    Monitoring the target 2.Write your own debugger • Actually easy to do • Lightweight, fast, full control C++ BOOL WINAPI WaitForDebugEvent( __out LPDEBUG_EVENT lpDebugEvent, __in DWORD dwMilliseconds ); typedef struct _DEBUG_EVENT { /* de */ DWORD dwDebugEventCode; DWORD dwProcessId; DWORD dwThreadId; union { EXCEPTION_DEBUG_INFO Exception; CREATE_THREAD_DEBUG_INFO CreateThread; CREATE_PROCESS_DEBUG_INFO CreateProcess; EXIT_THREAD_DEBUG_INFO ExitThread; EXIT_PROCESS_DEBUG_INFO ExitProcess; LOAD_DLL_DEBUG_INFO LoadDll; UNLOAD_DLL_DEBUG_INFO UnloadDll; OUTPUT_DEBUG_STRING_INFO DebugString; } u; } DEBUG_EVENT, *LPDEBUG_EVENT;
  • 39.
    Monitoring the target 3.Monitor resources: • File, registry, memory, CPU, logs
  • 40.
    Deliver the testcase 1. Standalone test harness • E.g. to launch to client application and have it load fuzzed file format 2. Instrumented client • Inject function hooking code into target client • Intercept data and substitute with fuzzed data • Useful if: • State machine is complex • Data is encoded in a non-standard format • Data is signed or encrypted
  • 41.
  • 42.
    Determining exploitability • Thisprocess requires experience of debugging security issues, but some steps can be taken to gain a good idea of how exploitable an issue is… • Look for any cases where data is written to a controllable address – this is key to controlling code execution and the majority of such conditions will be exploitable • Verify whether any registers have been overwritten, if they do not contain part data sent from the fuzzer, step back in the disassembly to try and find where the data came from. • If the register data is controllable, point the register which caused the crash to a page of memory which is empty, fill that page with data (e.g., ‘aaaaa…’)
  • 43.
    Determining exploitability • Aresaved return address/stack variables overwritten? • Is the crash in a heap management function? • Are the processor registers derived from data sent by the fuzzer (e.g. 0x61616161)? • Is the crash triggered by a read operation? • Can we craft a test case to avoid this? • Is the crash triggered by a write operation? • Do we have full or partial control of the faulting address? • Do we have full or partial control of the written value?
  • 44.
    Determining exploitability • Recentadvances – Microsoft !Exploitable • WinDbg extension to determine exploitability • Does lightweight taint analysis • Uses meta-instructions (STACK_PUSH, DATA_MOVE etc.) • Demoed at CanSec West conference, March 2009
  • 45.
    A quick diversion •These days you can get paid for finding vulnerabilities: • iDefense, Tipping Point, Pwn2Own • Write a fuzzer, find bugs. • Here’s how to get started: • Download FileFuzz (iDefense) • Pick a media application and find some samples • Fuzz! You will find bugs…
  • 46.
    A quick diversion •Code execution in Internet Explorer via 3rd party ActiveX control
  • 47.
    A quick diversion •Vulnerability in widely used enterprise-level software
  • 48.
    A quick diversion •Remotely exploitability vulnerability in critical infrastructure/the core OS
  • 49.
    A quick diversion •Why isn’t everyone in the security community rich… • The best (= $$$) targets are the most secure • Few remote vulnerabilities in Vista • Even less in IIS or Apache • Clued up vendors are fuzzing: • Windows Vista file fuzzing effort in numbers: • 350M iterations total • 250+ file parsers fuzzed • 300+ issues fixed
  • 50.
    Advanced fuzzing • Problemswith our basic model: • Fuzzer and target are not in sync • Fuzzer has no feedback mechanism • We have no visibility into fuzzer’s effectiveness • Revised model: • Send good requests at regular intervals to check health of target • Fuzzer is able to control target debugger • Use a code coverage build if possible
  • 51.
    Advanced fuzzing • Fuzzerissues debugger commands • Can restart target • Can handle resource exhaustion bugs, e.g. 100% CPU • Can save callstack, crash dump etc. • Fuzzing isolated states in a state machine • Save and restore “state” of process • See Hoglund, McGraw “Exploiting Software” • Constrain execution to a single state with breakpoints • Substitute real data for fuzzed data • But how to handle handles?
  • 52.
    Example • Microsoft ExchangeRemote Code Execution • Critical Microsoft hotfix, MS06-003 (Heasman & Litchfield) • Run code on a mail server with a single email! • Bonus: same bug affected Outlook 2003! • We found this by fuzzing email… • Rich text email often has a “winmail.dat” attachment • This is a binary Transport Neutral Encapsulation Format • Lets fuzz it!
  • 53.
    Example • TNEF isa Tag Length Value (TLV) format • Our bug: triggered by tag length of 0xFFFFFFFF for certain properties • Any ideas what was going on? • Integer overflow! • Small memory allocation followed by a large memcpy • We trash the heap, overwriting heap control structures • End up with arbitrary DWORD overwrite which we use to get code execution 0xFFFFFFFF + 1 = 0
  • 54.
    Example • Interesting asides:5 years earlier… • Bugtraq Security Mailing List - August 2000 • "As a side note it would be an interesting excercise to see if Outlook is susceptible to a message with a malformed winmail.dat attached. One could theoretically use winmail.dat to hit on holes in either Outlook itself, or the Outlook RTF engine" • February 2009 - MS09-003 “Vulnerabilities in Microsoft Exchange Could Allow Remote Code Execution” • TNEF again!
  • 55.
    Existing challenges • Fuzzinglikely to trigger same bug repeatedly • Automatically remove duplicate bugs • Compare call stacks, register values, memory locations • What if they are trashed? • Or slightly different? • Seems like this problem would benefit from a fuzzy approach • How to implement code coverage on a binary? • Without degrading performance • How to effectively use code coverage to direct fuzzing ?
  • 56.
    Existing challenges • Howto measure effectiveness of a fuzzer? • Number of test cases? • Number of bugs? • Severity of bugs? • % Code coverage? • How many test cases to run? • How to balance complexity vs. time constraints?

Editor's Notes

  • #15 CVE:- Common Vulnerabilities and Exposures (CVE) is a catalog of known security threats. The catalog is sponsored by the United States Department of Homeland Security (DHS), and threats are divided into two categories: vulnerabilities and exposures.
  • #25 Fuzzing is a form of vulnerability analysis and testing. Many slightly anomalous test cases are input into the target application Application is monitored for any sign of error. Example:- Standard HTTP GET request § GET /index.html HTTP/1.1 Anomalous requests § AAAAAA...AAAA /index.html HTTP/1.1 § GET ///////index.html HTTP/1.1 § GET %n%n%n%n%n%n.html HTTP/1.1
  • #26 Spike:- Spike is a program which sends crafted packages to an application in order to make it crash. The packages can be defined as templates. Spike is capable of sending both TCP and UDP packages. Vulnerabilities can be found in applications with the help of Spike. Spike is part of the Kali distribution. Peach Fuzz:- Peach is a Smart Fuzzer that is capable of performing both generation and mutation based fuzzing. Peach requires the creation of PeachPit files that define the structure, type information, and relationships in the data to be fuzzed. It additionally allows for the configuration of a fuzzing run including selecting a data transport , logging interface, etc. Peach has been under active development for seven years and is in its third major version. Peach was created and if actively developed by Michael Eddington of Deja vu Security Sulley:- is an actively developed fuzzing engine and fuzz testing framework consisting of multiple extensible components. Sulley (IMHO) exceeds the capabilities of most previously published fuzzing technologies, commercial and public domain. The goal of the framework is to simplify not only data representation but to simplify data transmission and instrumentation. Sulley is affectionately named after the creature from Monsters Inc., because, well, he is fuzzy. Schemer:- can be enabled as the post-mortem debugger. The previous post-mortem debugger is restored when fuzzing is finished. If Schemer runs for more than a day, when a log entry is written on the new day, the new day is also logged. This will make it easier to keep track of when interesting fuzzing events occur.
  • #27 A dword, which is short for "double word," is a data type definition that is specific to Microsoft Windows. As defined in the file windows.h, a dword is an unsigned, 32-bit unit of data. It can contain an integer value in the range 0 through 4,294,967,295. qword (quadruple word) is a unit of data that is four times the size of a word. On the x86 platform, this unit of data is 64 bits long because the definition of word on an x86
  • #32 Dumb Fuzz: It is an Mutation based fuzzing. A little or no knowledge of the structure of the inputs is assumed. The anomalies are added to existing valid inputs. It may be completely random or follow some Heuristics. It requires little to no set up time. Dumb fuzzing depends on the inputs being modified. It may fail for protocols with checksums, those which dépends on challenge response , etc. Examples: § Taof, GPF, ProxyFuzz, etc. Smart Fuzz: It is an Generation based fuzzing. Test cases are generated from some description of the format: RFC, documentation, etc. The anomalies are added to each possible spot in the inputs. Knowledge of protocol should give better results than random fuzzing It can take significant time to set up. Examples: § SPIKE, Sulley, Mu-4000, Codenomicon