<rdf:RDF
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xml:base='http://pabrantes.net/blog/rdf'>
    <s:Snip rdf:about='http://pabrantes.net/blog/rdf#start/2006-05-20/1'
         s:name='start/2006-05-20/1'
         s:cUser='pabrantes'
         s:oUser='pabrantes'
         s:mUser='pabrantes'>
        <s:content>1 Computer Security: What is an exploit? {anchor:Computer Security: What is an exploit?}&#xD;&#xA;I&apos;ve already spoken about exploits in some of my posts, but never took the time to actually explain what it is. I know that most of you that read this blog - not to say all - might already know what an exploit is. But I&apos;m in the mood to write an educative post.&#xD;&#xA;&#xD;&#xA;Exploit is a piece of software or a certain procedure that takes advantage (that&apos;s why it&apos;s called exploit) of a vulnerability in another piece of software in order to make the vulnerable software do something that wasn&apos;t design to do.&#xD;&#xA;&#xD;&#xA;This actions can go from a {link: Deny of Service|url=http://en.wikipedia.org/wiki/Denial-of-service_attack|newWindow=true}, to gaining access to restrict services and/or information locally or even remotely.&#xD;&#xA;&#xD;&#xA;There are different categories of exploits, each category consists in attacking certain kind of services or programs using different techniques. Here&apos;s a list of some of the most known exploits (also featuring some not so common):&#xD;&#xA;&#xD;&#xA;1. __Buffer Overflows__&#xD;&#xA;1. __Cross Site Scripting__ (XSS)&#xD;&#xA;1. Format Strings&#xD;&#xA;1. __SQL Injections__&#xD;&#xA;1. Race Condition&#xD;&#xA;&#xD;&#xA;The ones present in __bold__ are the ones I consider more common, although be aware that I&apos;m doing such statement just using my common sense.&#xD;&#xA;&#xD;&#xA;Let&apos;s get into a brief description in each one of them. &#xD;&#xA;&#xD;&#xA;&#xD;&#xA;\1. __Buffer Overflow__\\&#xD;&#xA;As the name says you have a buffer and you have an overflow! This has probably been the most common exploit until XSS and SQL Injections started gaining a bigger &quot;share&quot;. And for the people that still program in C language, this is definately the top security problem they might have to concern when writing code.&#xD;&#xA;&#xD;&#xA;Anyway the idea of a buffer overflow exploit is to take advantage of non-protected buffers. By non-protected buffer you should understand buffers who store uncontrolled input and may write into the buffer inputs bigger than the buffers size, overlapping with the data of program loaded into memory. When overlapping such memory, the exploit deploys a certain special code that can be executed by the computer, normally called {link: shellcode |url=http://en.wikipedia.org/wiki/Shellcode|newWinndow=true}, or just pure garbage that will make the program crash.&#xD;&#xA;&#xD;&#xA;Below you have a simple example of a C program that contains a vulnerability that allows a buffer overflow exploit to take advantage. &#xD;&#xA;&#xD;&#xA;{code:C}&#xD;&#xA;&#xD;&#xA;#include &lt;stdio.h&gt;&#xD;&#xA;&#xD;&#xA;main(int argc, char **argv) {&#xD;&#xA;    char buf[10];&#xD;&#xA;    if(argc!=2) {&#xD;&#xA;       printf(&quot;Syntax: %s name&quot;, argv[0]);&#xD;&#xA;       exit(-1);&#xD;&#xA;    }  &#xD;&#xA;    strcpy(buf, argv[1]);  /* If argv[1] is bigger than 10 chars it will overflow buf */&#xD;&#xA;    printf(&quot;Hello %s&quot;, buf);&#xD;&#xA;}&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;In this case it was a stack overflow, although there are other kind of overflows like heap overflow, integer overflow, etc.\\&#xD;&#xA;To fix the previous example, you should replace the ~~strcpy~~function by the ~~strnpy~~ function with length 10 (length of buf).&#xD;&#xA;&#xD;&#xA;Preventing buffer overflows starts with validation practices when getting input from untrusted sources, which can be a user, another computer, a database, etc. Although there are other measures it can be taken, like for example, making the stack non-executable to render stack buffer overflows useless. &#xD;&#xA;&#xD;&#xA;References about buffer overflows:&#xD;&#xA;&#xD;&#xA;* {link: Smashing the stack for fun and profit|url=http://www.phrack.org/phrack/60/p60-0x06.txt|newWindow=true}&#xD;&#xA;* {link: How to write buffer overflows|url=http://www.insecure.org/stf/mudge_buffer_overflow_tutorial.html|newWindow=true} &#xD;&#xA;* {link: w00w00 on Heap overflows|url=http://www.w00w00.org/files/articles/heaptut.txt|newWindow=true} &#xD;&#xA;&#xD;&#xA;2. __Cross Site Scripting__ (XSS)&#xD;&#xA;&#xD;&#xA;Probably one of the most common vulnerabilities nowadays, although it&apos;s severity might not be as big as the one of buffer overflows it still can be used to do damage. There are 3 types of XSS attacks, the Type 0, Type 1 and Type 3. I&apos;ll not discuss them here, but you can find an excellent explanation of them at the wikipedia reference I give below.&#xD;&#xA;&#xD;&#xA;If you have been reading my blog you probably already read about all the XSS problems I&apos;ve been reporting about hi5.com. Mainly XSS consists once again in bad validation of data input that allows a malicious user to input code that will be executed in someone else&apos;s computer. &#xD;&#xA;&#xD;&#xA;Sadly this is a very common vulnerability nowadays and even if sometimes it not represents a real issue other times it does.&#xD;&#xA;&#xD;&#xA;Below you can find a vulnerable php script. The idea of the script it to receive from a POST a name and print it:&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;&lt;?&#xD;&#xA;&#xD;&#xA;$name = $_POST[&apos;name&apos;] &#xD;&#xA;echo $name;&#xD;&#xA;&#xD;&#xA;?&gt;&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;If you pass as name a javascript, it will be executed. For further examples in XSS, you can read my post, {link: Smashing hi5 for fun and profit|url=http://www.pabrantes.net/blog/space/start/2006-05-01/1}.&#xD;&#xA;To fix the previous example, you would have escape the possible &apos;&lt;&apos; and &apos;&gt;&apos; from&#xD;&#xA;$name and just after that display it.&#xD;&#xA; &#xD;&#xA;References about XSS exploits:&#xD;&#xA;&#xD;&#xA;* {link: Cross Site Scripting explained in Wikipedia|url=http://en.wikipedia.org/wiki/XSS|newWindow=true}&#xD;&#xA;* {link: XSS cheat sheet: for filter evasion |url=http://ha.ckers.org/xss.html|newWindow=true}&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;3. __Format Strings__&#xD;&#xA;&#xD;&#xA;Format String is a not so common exploit and it&apos;s a particular vulnerability of the C language (which is not type-safe). The idea of the format string exploit is to take advantage of a simple bug, which is instead of having:&#xD;&#xA;&#xD;&#xA;{quote} printf(&quot;%s\n&quot;, userInputString); {quote}&#xD;&#xA;&#xD;&#xA;having:&#xD;&#xA;&#xD;&#xA;{quote} printf(userInputString); {quote}&#xD;&#xA;&#xD;&#xA;~~userInputString~~ is then seen has a format string and will be interpreted as one, so all the formaters present in the string will be interpreted. &#xD;&#xA;&#xD;&#xA;References about Format Strings:&#xD;&#xA;&#xD;&#xA;*{link: Format Strings Attack|url=http://www.lava.net/~newsham/format-string-attacks.pdf|newWindow=true} (pdf)&#xD;&#xA;&#xD;&#xA;4. __SQL Injections__&#xD;&#xA;&#xD;&#xA;SQL Injection is another common vulnerability that has the objective of gaining access to the website backend database or just delete data from it. Once again the great problem is the lack of input validation. When present allows a malicious user to send extra commands to the SQL server. &#xD;&#xA;&#xD;&#xA;Here&apos;s an example of a vulnerable php script that receives the username from a POST.&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;&lt;?&#xD;&#xA;&#xD;&#xA;$username = $_POST[&apos;username&apos;];&#xD;&#xA;&#xD;&#xA;$query = &quot;SELECT * FROM USERNAME WHERE USERNAME=&apos;&quot; . username . &apos;&quot;;&#xD;&#xA;&#xD;&#xA;$result = mysql_query($query);&#xD;&#xA;&#xD;&#xA;// Code to display result here&#xD;&#xA;?&gt;&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;If you would supply for username the following string:&#xD;&#xA;&#xD;&#xA;{quote}&#xD;&#xA;&apos; UNION SELECT * FROM USERNAME;&#xD;&#xA;{quote}&#xD;&#xA;&#xD;&#xA;This would create two queries, one with quering for a blank username &apos;&apos; and another one that would dump all the username table, then would unite both tables and show the result on screen.&#xD;&#xA;To fix this injection problem, once again you would just have to escape the&#xD;&#xA;chars in the $username variable. By the way, you can use ~~mysql_real_escape_string~~ function for this purpose.&#xD;&#xA; &#xD;&#xA;References about SQL Injections:&#xD;&#xA;&#xD;&#xA;* {link: SQL Injections Attacks by example|url=http://www.unixwiz.net/techtips/sql-injection.html|newWindow=true}&#xD;&#xA;* {link: SQL Injection|url=http://www.spidynamics.com/whitepapers/WhitepaperSQLInjection.pdf|newWindow=true} (pdf)&#xD;&#xA;&#xD;&#xA;5. __Race Conditions__&#xD;&#xA;&#xD;&#xA;This is not a so common vulnerability and it&apos;s caused by synchronization problems.  &#xD;&#xA;It can be synchronization within the applications, such as problems that might arise from thread usage without correct sync, or synchronization between an application and the exterior environment, such as another application or even the operating system. &#xD;&#xA;&#xD;&#xA;Here&apos;s the typical example of a race condition between application and Operating System calls:&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;#include &lt;sdtio.h&gt;&#xD;&#xA;#include &lt;unistd.h&gt;&#xD;&#xA;#include &lt;sys/types.h&gt;&#xD;&#xA;#include &lt;sys/stat.h&gt;&#xD;&#xA;#include &lt;fcntl.h&gt;&#xD;&#xA;&#xD;&#xA;main(int argc, char **argv) {&#xD;&#xA;&#xD;&#xA;char buf[1024];&#xD;&#xA;int fd;&#xD;&#xA;if(argc!=2) {&#xD;&#xA;    printf(&quot;Syntax: %s filename&quot;, argv[0]);&#xD;&#xA;    exit(-1); &#xD;&#xA;}&#xD;&#xA;if (access(argv[1], R_OK) != 0) {&#xD;&#xA;   exit(-1);&#xD;&#xA;}&#xD;&#xA;&#xD;&#xA;fd = open(argv[1], O_RDONLY);&#xD;&#xA;while(read(fd,buf,1024)&gt;0) {&#xD;&#xA;  printf(&quot;%s&quot;, buf);&#xD;&#xA;}&#xD;&#xA;}&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;Imagine that the following program is owned by root and it&apos;s SUID. &#xD;&#xA;\\Supposedly you can only read files with this program that you actually have permission to read, since it uses ~~access~~ function. Although that&apos;s not true! You can do the following:&#xD;&#xA;&#xD;&#xA;1. Create a file that you have access&#xD;&#xA;1. Start the program giving that file&#xD;&#xA;1. Replace it by a symlink to the /etc/shadow file just after it did the access call and before it opens the file descriptor.&#xD;&#xA;&#xD;&#xA;It needs to correct timing but you&apos;ll be able to exploit the program and get the content of shadow file. &#xD;&#xA;&#xD;&#xA;References about race conditions:&#xD;&#xA;&#xD;&#xA;* {link: Fixing Races for Fun and Profit: How to abuse atime|url=http://www.cs.berkeley.edu/~nks/papers/races-usenix05.pdf|newWindow=true}&#xD;&#xA;* {link:Secure programmer: Prevent race conditions |url=http://www-128.ibm.com/developerworks/library-combined/l-sprace.html|newWindow=true}&#xD;&#xA;&#xD;&#xA;__Final notes__&#xD;&#xA;&#xD;&#xA;In most of the exploits presented there was a common factor, the lack of input validation was the origin of the vulnerabilities that they took advantage of. So next time you start programming, please remember that!&#xD;&#xA;&#xD;&#xA;I hope you have found this article useful and even if you already knew what I&apos;ve been explaining at least enjoyed the references I pointed. Any other type of exploit you want to discuss, or even something related to the ones presented, please just post. </s:content>
        <s:mTime>2006-05-21 14:46:29.472</s:mTime>
        <s:cTime>2006-05-20 23:39:26.794</s:cTime>
        <s:comments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:snipLinks>
            <rdf:Bag>
                <rdf:li rdf:resource='#snipsnap-notfound'/>
                <rdf:li rdf:resource='http://pabrantes.net/blog/rdf#start/2006-05-01/1'/>
                <rdf:li rdf:resource='http://pabrantes.net/blog/rdf#pabrantes/post-history'/>
                <rdf:li rdf:resource='#pabrantes-pastProjects'/>
                <rdf:li rdf:resource='#pabrantes-CV'/>
                <rdf:li rdf:resource='http://pabrantes.net/blog/rdf#vista/main.html'/>
            </rdf:Bag>
        </s:snipLinks>
        <s:attachments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
    </s:Snip>
</rdf:RDF>
