<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.4.3">Jekyll</generator><link href="https://lightski.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://lightski.github.io/" rel="alternate" type="text/html" /><updated>2017-05-09T01:30:46+00:00</updated><id>https://lightski.github.io/</id><title type="html">wannabe hacker</title><subtitle>Thoughts on *Hacking: the Art of Exploitation*</subtitle><entry><title type="html">At Long Last</title><link href="https://lightski.github.io/at-long-last/" rel="alternate" type="text/html" title="At Long Last" /><published>2017-05-08T00:00:00+00:00</published><updated>2017-05-08T00:00:00+00:00</updated><id>https://lightski.github.io/at-long-last</id><content type="html" xml:base="https://lightski.github.io/at-long-last/">&lt;p&gt;Hacking post day eeelevenish, and it’s &lt;em&gt;finally&lt;/em&gt; time! We start the code exploitation chapter and begin hacking.&lt;/p&gt;

&lt;p&gt;Recall that exploitation is making programs do unintended things. Security flaws, often, are extra pieces of functionality used in ways the original programmer did not expect. Small logic errors or seemingly unused code can lead to larger consequences.&lt;/p&gt;

&lt;p&gt;One typical flaw is the &lt;em&gt;off by one&lt;/em&gt; error. This happens when programmer miscounts the number of elements in a loop or conditional check. For example, suppose you want to build a one-hundred meter long fence with posts every 10 meters. How many posts do you need? Intuitively, the answer is ten. But that is incorrect; you actually require eleven. These sorts of errors occur regularly while coding.&lt;/p&gt;

&lt;p&gt;Another common problem is quickly expanding complexity. Programmers sometimes get in a hurry and are unable to fully test functionality. The book gives the example of Microsoft’s IIS handling Unicode characters. IIS is designed to serve both read-only and read-write content. Read-write directories give the user of a website full control of the system, so it’s important to limit that access to certain subdirectories. Unfortunately, IIS later added Unicode support. Unicode expansion happened after the backslash check, so attackers could pass in Unicode characters to escape to different directories. Several worms used this vulnerability to deface websites.&lt;/p&gt;

&lt;p&gt;Next we come to the classic security fault: buffer overflows. Suppose a C programmer allocates 8 bytes of memory, and tries to write 9 bytes to that space. This action is not stopped by the operating system (until more recent security measures), and it often causes the program to crash. But if we’re careful, we can execute arbitrary code this way. Here’s some sample code with a buffer overflow in it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;overflow_example.c&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;one&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;two&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[BEFORE] buffer_one is at %p and contains &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[BEFORE] buffer_two is at %p and contains &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[BEFORE] value is at %p and is %d (0x%08x)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[STRCPY] copying %d bytes into buffer_two&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]));&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[AFTER] buffer_one is at %p and contains &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[AFTER] buffer_two is at %p and contains &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[BEFORE] value is at %p and is %d (0x%08x)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And here it is in action:&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vagrant@vagrant-laptop:~/src &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc overflow_example.c 
vagrant@vagrant-laptop:~/src &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./a.out 12345
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] buffer_one is at 0xbffffc78 and contains &lt;span class=&quot;s1&quot;&gt;'one'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] buffer_two is at 0xbffffc70 and contains &lt;span class=&quot;s1&quot;&gt;'two'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] value is at 0xbffffc84 and is 5 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00000005&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;STRCPY] copying 5 bytes into buffer_two

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] buffer_one is at 0xbffffc78 and contains &lt;span class=&quot;s1&quot;&gt;'one'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] buffer_two is at 0xbffffc70 and contains &lt;span class=&quot;s1&quot;&gt;'12345'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] value is at 0xbffffc84 and is 5 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00000005&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
vagrant@vagrant-laptop:~/src &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./a.out 12345678901234567890                           
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] buffer_one is at 0xbffffc68 and contains &lt;span class=&quot;s1&quot;&gt;'one'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] buffer_two is at 0xbffffc60 and contains &lt;span class=&quot;s1&quot;&gt;'two'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;BEFORE] value is at 0xbffffc74 and is 5 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00000005&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;STRCPY] copying 20 bytes into buffer_two

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] buffer_one is at 0xbffffc68 and contains &lt;span class=&quot;s1&quot;&gt;'901234567890'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] buffer_two is at 0xbffffc60 and contains &lt;span class=&quot;s1&quot;&gt;'12345678901234567890'&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;AFTER] value is at 0xbffffc74 and is 0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00000000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
vagrant@vagrant-laptop:~/src &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The buffers are 8 bytes long, so we can safely store anything up to 8 characters in them. When we pass in a longer input, the extra bytes overwrite other variables. How, exactly is this exploitable? Tune in tomorrow to find out.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking post day eeelevenish, and it’s finally time! We start the code exploitation chapter and begin hacking.</summary></entry><entry><title type="html">What the Struct</title><link href="https://lightski.github.io/what-the-struct/" rel="alternate" type="text/html" title="What the Struct" /><published>2017-05-07T00:00:00+00:00</published><updated>2017-05-07T00:00:00+00:00</updated><id>https://lightski.github.io/what-the-struct</id><content type="html" xml:base="https://lightski.github.io/what-the-struct/">&lt;p&gt;Hacking post day ten. I skipped out on writing yesterday because it was too late at night. This week I am focusing on writing, and sleeping, earlier. But, enough about me! Let’s dive into some more C material. Today is mostly about structs.&lt;/p&gt;

&lt;p&gt;Yo dog, we heard you like variables. So we made a variable you can put more variables into. In C we call it a &lt;strong&gt;struct&lt;/strong&gt;. &lt;strong&gt;structs&lt;/strong&gt; are continuous areas of memory. You declare them like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brewery&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottles_of_beer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cash_on_hand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We get a &lt;strong&gt;struct&lt;/strong&gt; called brewery, which tracks bottles of beer and cash. Next, we must access these values somehow.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// declare struct and vars
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brewery&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mybrew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brewptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;brewptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mybrew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// three ways to access:
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mybrew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottles_of_beer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// direct access
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brewptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cash_on_hand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// pointer access
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_bottles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brewptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// hacky pointer cast
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To access values the first way, we must have the struct in memory. This works sometimes, but can be a hassle. Passing &lt;strong&gt;structs&lt;/strong&gt; around takes a lot of space on the stack. To migigate this, we use the second access method. Having a pointer to the struct is just as good. Note the arrow (-&amp;gt;) notation is for convenience; it’s the same as dereferencing the struct pointer. The first two access methods are proper ways to use structs in C. The third way technically works, but it’s discouraged. How it works, is we cast the pointer as an int pointer and then directly access the memory location. It’s messy because you have to track exactly how the memory is laid out in the struct.&lt;/p&gt;

&lt;p&gt;Next, a quick note on pointers. Pointers hold a memory location, and we typically use them to refer to variables. However, we can also get a pointer to a function’s location, known as a &lt;strong&gt;function pointer&lt;/strong&gt;. Here’s some source from the book showing their use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;funcptr_example.c&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;This is function one&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;);&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;    return 1;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;int func_two() {&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;    printf(&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// funciton pointer!
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;function_ptr = 0x%08x&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;value returned was %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func_two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;function_ptr = 0x%08x&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;value returned was %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So the function pointer gets set to the locations of two different functions and called. This works as you’d expect; both functions get called properly and we see that they live at different memory addresses. I’m not sure what these are used for in C, but it’s here just in case.&lt;/p&gt;

&lt;p&gt;Finally, we look at pseudo-random numbers. Truly random numbers require non-determinism. Computers produce outputs based on inputs, ie they are deterministic, so they cannot provide true randomness. However, some programs require random numbers. AI in games and cryptography are two easy examples which come to mind. In C, we can use the &lt;strong&gt;srand()&lt;/strong&gt; and &lt;strong&gt;rand()&lt;/strong&gt; functions from &amp;lt;stdlib.h&amp;gt;. &lt;strong&gt;srand()&lt;/strong&gt;  takes a seed value, then &lt;strong&gt;rand()&lt;/strong&gt; produces pseudo-random numbers from that seed. It is common to use the seconds elapsed since the epoch (how Linux tracks time) as a seed. Pseudo-random numbers appear random, but if you know the seed you could calculate them.&lt;/p&gt;

&lt;p&gt;This is the end of the C tutorial, whoo!! From here, the book shows one large C program: game_of_chance.c. Maybe I’ll post source later? (it’s on github). Next up is the chapter on exploitation, aka making a program do unintended things. I am quite excited to start, as that will be totally new material. Cannot wait!&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking post day ten. I skipped out on writing yesterday because it was too late at night. This week I am focusing on writing, and sleeping, earlier. But, enough about me! Let’s dive into some more C material. Today is mostly about structs.</summary></entry><entry><title type="html">Cyber Burr Brrr</title><link href="https://lightski.github.io/cyber-burr-brrr/" rel="alternate" type="text/html" title="Cyber Burr Brrr" /><published>2017-05-05T00:00:00+00:00</published><updated>2017-05-05T00:00:00+00:00</updated><id>https://lightski.github.io/cyber-burr-brrr</id><content type="html" xml:base="https://lightski.github.io/cyber-burr-brrr/">&lt;p&gt;Hacking post day the ninth. These days, it’s all about the cyber. We continue with a discussion of Linux user IDs (aka UIDs (aka cyber UIDs)).&lt;/p&gt;

&lt;p&gt;Every user and group in Linux has a unique ID number; they are UIDs and GIDs respectively. View IDs using the &lt;strong&gt;id&lt;/strong&gt; command on Linux shell.&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;id user
&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;id &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;whoami&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;First command will return results of “user” UIDs plus any applicable GIDs. The second command gets the logged in user (via subshell) then asks for IDs related to that. So far, so good.&lt;/p&gt;

&lt;p&gt;Sometimes, multiple users must make changes to a shared file. Linux has a permission to allow this, called &lt;em&gt;set user ID&lt;/em&gt; aka setuid. Set it on myfile.sh like so: sudo chmod u+s ./myfile.sh. When a program has this permission, it runs as the file’s &lt;strong&gt;owner&lt;/strong&gt; instead of current user. The UID we’re running as currently is known as the &lt;em&gt;effective UID&lt;/em&gt; or euid.&lt;/p&gt;

&lt;p&gt;In C, we can view both uid and euid with very simple functions. Here’s a brief demo program:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;real uid: %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;effective uid: %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geteuid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So, when we need to provide access to some shared resource, we can give root access to that resource. Set our program to run as root. Then, track the uid running the program. When writing to resource, record uid of author. To retrieve data from resource, read uid and only return results written by that uid. Clear as mud.&lt;/p&gt;

&lt;p&gt;Thus ends our discussion of UID, GUID, and eUID. Tomorrow we’ll continue travelling the way of C with structs.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking post day the ninth. These days, it’s all about the cyber. We continue with a discussion of Linux user IDs (aka UIDs (aka cyber UIDs)).</summary></entry><entry><title type="html">Rabbit Trail Redux</title><link href="https://lightski.github.io/rabbit-trail-redux/" rel="alternate" type="text/html" title="Rabbit Trail Redux" /><published>2017-05-04T00:00:00+00:00</published><updated>2017-05-04T00:00:00+00:00</updated><id>https://lightski.github.io/rabbit-trail-redux</id><content type="html" xml:base="https://lightski.github.io/rabbit-trail-redux/">&lt;p&gt;Today I am again time-poor, so I have again opted to write a little about my tooling. &lt;em&gt;Hacking: the Art of Exploitation&lt;/em&gt; comes with a live CD for all your hacking needs, so I made it into a virtual machine for easy access. Here I’ll talk about that process and link to a nice guide.&lt;/p&gt;

&lt;p&gt;At first I was going to rip the iso off the included physical CD, which is time-consuming and lame. Then I found that No Starch Press’s &lt;a href=&quot;https://www.nostarch.com/hackingCD.htm&quot;&gt;product page for Hacking: Art of Exploitation&lt;/a&gt; links to a &lt;a href=&quot;https://www.nostarch.com/download/Hacking%20The%20Art%20of%20Exploitation%202nd%20Edition%20Jon%20Erickson%20Official%20LiveCD%20ISO%20No%20Starch%20Press%20[mininova].torrent&quot;&gt;torrent of the iso&lt;/a&gt;. I happily torrented it.&lt;/p&gt;

&lt;p&gt;Next I used the iso file to build a virtual machine. A virtual machine is basically a miniature computer within your computer. In this case, since the &lt;em&gt;Hacking&lt;/em&gt; live CD was based on Ubuntu, I now have a miniature Ubuntu system. It’s especially nice, because the &lt;em&gt;Hacking&lt;/em&gt; toolset is over seven years old. Anti-exploit tools in the Linux kernel have matured since then, making some of the books’ exploits invalid. Additionally, the memory inspection sections are based on 32-bit architecture while I have a 64-bit machine. Using a virtual machine I can see the exact same things as the author. Everything works exactly as expected.&lt;/p&gt;

&lt;p&gt;This setup was nice, but I wanted more. I found myself writing code in the virtual machine, and wanting to post it. This is possible with extra software provided by the virtual machine software. In my case, I tried to install some tools from VirtualBox. However, the provided installation did not work for me! I found out later this was due to the age of the gcc compiler provided with the &lt;em&gt;Hacking&lt;/em&gt; disc. Anyway, in googling for a solution I came across a &lt;a href=&quot;https://gist.github.com/topalovic/e06a82c0f115a261e7b3e932caee3a4f&quot;&gt;very interesting github gist&lt;/a&gt;. The gist described making a Vagrant box out of the &lt;em&gt;Hacking&lt;/em&gt; CD. Vagrant is a semi-recent virtualization technology. It lets you configure virtual machines using command line, and the default access is SSH. Perfect for me! If everything worked out correctly, I would be able to talk to the &lt;em&gt;Hacking&lt;/em&gt; box using SSH. This adds a lot of convenience to my workflow.&lt;/p&gt;

&lt;p&gt;In short, I followed the gist’s guide and set up my box. Along the way, I ran into a couple snags. The total setup took me three hours and some extra googling. So I &lt;a href=&quot;https://gist.github.com/lightski/0096743043f1165e32e257637c312765&quot;&gt;forked the gist&lt;/a&gt;. I will add to it and write again about my modifications.&lt;/p&gt;

&lt;p&gt;Long story short, I ended up with a very nice setup. Now, I open a shell at my vagrant directory and issue these commands.&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    vagrant up
    vagrant ssh
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This boots my virtual machine and connects me via ssh. I have the ~/src folder shared between the virtual machine and my computer. This lets me write code using my local (customized) vim, then switch to the virtual machine to compile and disassemble what I’ve written. Sending code to a post is as easy as using cat to read the file, copying relevant code, and pasting into the post. Very nice!&lt;/p&gt;

&lt;p&gt;Anyway, I’ve very content with my setup now. Next time we’ll continue the discussion of Linux UIDs.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Today I am again time-poor, so I have again opted to write a little about my tooling. Hacking: the Art of Exploitation comes with a live CD for all your hacking needs, so I made it into a virtual machine for easy access. Here I’ll talk about that process and link to a nice guide.</summary></entry><entry><title type="html">Student Becomes Teacher</title><link href="https://lightski.github.io/student-becomes-teacher/" rel="alternate" type="text/html" title="Student Becomes Teacher" /><published>2017-05-03T00:00:00+00:00</published><updated>2017-05-03T00:00:00+00:00</updated><id>https://lightski.github.io/student-becomes-teacher</id><content type="html" xml:base="https://lightski.github.io/student-becomes-teacher/">&lt;p&gt;Hacking book day eight, whoa dude. Today we’re going all-in on &lt;em&gt;advanced&lt;/em&gt; C topics. Enjoy.&lt;/p&gt;

&lt;p&gt;Recall that &lt;a href=&quot;/a-stack-of-turtles/&quot;&gt;yesterday’s discussion&lt;/a&gt; included a section on file system. Today we continue that conversation by discussing &lt;em&gt;access mode flags&lt;/em&gt;. Access mode flags describe how C will access a file. The flags are defined in the C language headers &lt;strong&gt;fcntl.h&lt;/strong&gt; and &lt;strong&gt;sys/stat.h&lt;/strong&gt;. Access mode must use at least one of these flags:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;O_RDONLY&lt;/em&gt;: read-only access&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;O_WRONLY&lt;/em&gt;: write-only access&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;O_RDWR&lt;/em&gt;: both read and write&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, access mode may have optional flags. Here are some of the more useful ones (p84):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;O_APPEND&lt;/em&gt;: write data to file end&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;O_TRUNC&lt;/em&gt;: if file exists, shorten it to length 0&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;O_CREAT&lt;/em&gt;: if file does not exist, make it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To combine these flags, we use bitwise OR operator (the pipe “|”). For example, write only with create option is: O_WRONLY|O_CREAT. Internally, these flags are strings of bits in different positions. So bitwise ORing them adds the bits together and erases no information.&lt;/p&gt;

&lt;p&gt;When creating a file with &lt;em&gt;O_CREAT&lt;/em&gt;, additional flags are required to specify the file’s permissions. They are defined in &lt;strong&gt;sys/stat.h&lt;/strong&gt;, can be combined using bitwise OR, and are as follows (p87):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;S_IRUSR&lt;/em&gt;: File owner can read this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IWUSR&lt;/em&gt;: File owner can write this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IXUSR&lt;/em&gt;: File owner can execute this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IRGRP&lt;/em&gt;: File group can read this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IWGRP&lt;/em&gt;: File group can write this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IXGRP&lt;/em&gt;: File group can execute this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IROTH&lt;/em&gt;: Others can read this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IWOTH&lt;/em&gt;: Others can write this file.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;S_IXOTH&lt;/em&gt;: Others can execute this file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you know Linux file permissions, these are exactly the same. If not, we’re going to go over them right now. On Linux, whomever creates a file is the owner. The group that owner belongs to is assigned to the group of the file. And anyone is other users who are not a member of the group. Each of these have three permissions which may be set in any permutation: &lt;strong&gt;r&lt;/strong&gt;ead, &lt;strong&gt;w&lt;/strong&gt;rite, and &lt;strong&gt;e&lt;/strong&gt;xecute. Here’s a concrete example:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ~/src &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ls -l
total 16
-rwxr-xr-x 1 billy workers 7257 May  2 23:09 a.out
-rw-r--r-- 1 billy workers  994 Apr 30 23:53 scope.c
-rw-r--r-- 1 root  root     169 May  2 23:09 stack_example.c
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, running &lt;strong&gt;ls -l&lt;/strong&gt; displays the file permissions as well as owner and group information. Above, a.out belongs to billy and the workers group. Billy can read, write, and execute a.out; anyone in the workers group can read and execute a.out; and anyone else can read and execute a.out.&lt;/p&gt;

&lt;p&gt;Shorthand for these permissions is (base 10) 4, 2, 1 because each permission has three binary bits. Read permission alone is 100, write alone is 010, and execute alone is 001 in binary. Each of {read, write, execute} can apply to each of {owner, group; others}. That’s three sets of three bits for a total of nine binary bits. Thus, it is common in Linux to express file permissions using a set of three base ten numbers. For example, the permissions on a.out above would be (base 10) 755 which is 111|101|101.&lt;/p&gt;

&lt;p&gt;Finally, the &lt;strong&gt;chmod&lt;/strong&gt; utility lets us change file permissions. Basic usage is chmod 644 myfile. See &lt;strong&gt;man chmod&lt;/strong&gt; for more advanced usage.&lt;/p&gt;

&lt;p&gt;Today we wrapped up our discussion of file access in C. We looked at the different access mode flags, combining them, and the different file permissions under Linux. Permissions seem odd at first, but once you’ve studied them a bit they become very simple. Next up we continue our discussion of advanced topics with User IDs.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day eight, whoa dude. Today we’re going all-in on advanced C topics. Enjoy.</summary></entry><entry><title type="html">A Stack of Turtles</title><link href="https://lightski.github.io/a-stack-of-turtles/" rel="alternate" type="text/html" title="A Stack of Turtles" /><published>2017-05-02T00:00:00+00:00</published><updated>2017-05-02T00:00:00+00:00</updated><id>https://lightski.github.io/a-stack-of-turtles</id><content type="html" xml:base="https://lightski.github.io/a-stack-of-turtles/">&lt;p&gt;Hacking book day seven. And how. Today we continue the looking at memory segments. This is a continuation of &lt;a href=&quot;/make-my-day/&quot;&gt;yesterday’s topic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Recall C uses five different segments in memory: code, data, bss, heap, and stack. Where a variable is stored depends on how it is defined. Initialized static and global variables go to data; uninitialized variables remain in the bss. Heap memory is controlled by programmer; we allocate more space using &lt;strong&gt;malloc()&lt;/strong&gt;. Remaining function variables are stored in stack, where all local context values reside (p75).&lt;/p&gt;

&lt;p&gt;Let’s dive further into the heap, shall we? Using the other four variable segments just depends on how you initialize variables; using heap requires explicit effort (p77). Use &lt;strong&gt;malloc()&lt;/strong&gt; to initialize memory. It returns a pointer to the memory or null if unable. When finished, release memory using &lt;strong&gt;free()&lt;/strong&gt;. Note that &lt;strong&gt;malloc()&lt;/strong&gt; doesn’t know what type of memory it is allocating. Thus &lt;strong&gt;malloc()&lt;/strong&gt; returns a void pointer to the memory, and we the programmer must cast the pointer to the correct type.&lt;/p&gt;

&lt;p&gt;This concludes the essentials of C programming. Next we move on to more advanced C topics using functions.&lt;/p&gt;

&lt;p&gt;Our first &lt;em&gt;advanced C topic&lt;/em&gt; is file access. C language supports low-level file management, using file descriptors, as well as high-level file buffered I/O using file streams. We’re going to look at file descriptors; they are more direct (p81). A &lt;em&gt;file descriptor&lt;/em&gt; is a unique number used to reference open files. It’s like a social security card for open C files. There are four primary functions that operate on file descriptors, and they are as follows:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;function&lt;/th&gt;
      &lt;th&gt;args&lt;/th&gt;
      &lt;th&gt;description&lt;/th&gt;
      &lt;th&gt;return values&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;open()&lt;/td&gt;
      &lt;td&gt;pointer to file, access mode flags&lt;/td&gt;
      &lt;td&gt;opens file for reading/writing&lt;/td&gt;
      &lt;td&gt;int file descriptor; -1 on error&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;close()&lt;/td&gt;
      &lt;td&gt;file descriptor&lt;/td&gt;
      &lt;td&gt;closes file&lt;/td&gt;
      &lt;td&gt;-1 on error&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;read()&lt;/td&gt;
      &lt;td&gt;file descriptor, pointer to data, #bytes to read&lt;/td&gt;
      &lt;td&gt;reads #bytes from file to pointer&lt;/td&gt;
      &lt;td&gt;-1 on error&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;write()&lt;/td&gt;
      &lt;td&gt;file descriptor, pointer to data, #bytes to write&lt;/td&gt;
      &lt;td&gt;writes #bytes from pointer to file&lt;/td&gt;
      &lt;td&gt;-1 on error&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The &lt;em&gt;access mode flags&lt;/em&gt; sent to &lt;strong&gt;open()&lt;/strong&gt; require more time than I’ve got tonight. So our discussion of file access is placed on hold. Today we concluded our discussion of memory segments, and began looking into file access. Next time we’ll finish up file access and try to move through file permissions and user ids. In the next week, I hope to cover actual code exploitation. Can’t wait!&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day seven. And how. Today we continue the looking at memory segments. This is a continuation of yesterday’s topic.</summary></entry><entry><title type="html">Make my Day</title><link href="https://lightski.github.io/make-my-day/" rel="alternate" type="text/html" title="Make my Day" /><published>2017-05-01T00:00:00+00:00</published><updated>2017-05-01T00:00:00+00:00</updated><id>https://lightski.github.io/make-my-day</id><content type="html" xml:base="https://lightski.github.io/make-my-day/">&lt;p&gt;Hacking book day six, what what? Let’s do this. We’re moving on from variable scoping, globals, and statics. Not like &lt;em&gt;way&lt;/em&gt; beyond, just &lt;em&gt;into-the-next-city&lt;/em&gt; beyond.&lt;/p&gt;

&lt;p&gt;Big topic today is memory segmentation. C divides program’s memory into five segments: text, data, bss, heap, and stack (p69). Relevant chart:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;name&lt;/th&gt;
      &lt;th&gt;what it holds&lt;/th&gt;
      &lt;th&gt;permissions&lt;/th&gt;
      &lt;th&gt;growth&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;text (code)&lt;/td&gt;
      &lt;td&gt;assembled machine language instructions&lt;/td&gt;
      &lt;td&gt;read-only&lt;/td&gt;
      &lt;td&gt;fixed size&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;data&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;initialized&lt;/strong&gt; global and static program vars&lt;/td&gt;
      &lt;td&gt;writeable&lt;/td&gt;
      &lt;td&gt;fixed size&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;bss&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;uninitialized&lt;/strong&gt; global and static program vars&lt;/td&gt;
      &lt;td&gt;writeable&lt;/td&gt;
      &lt;td&gt;fixed size&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;heap&lt;/td&gt;
      &lt;td&gt;programmer-allocatable blocks of memory&lt;/td&gt;
      &lt;td&gt;writeable&lt;/td&gt;
      &lt;td&gt;grows &lt;strong&gt;down&lt;/strong&gt; to &lt;em&gt;higher&lt;/em&gt; memory addresses&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;stack&lt;/td&gt;
      &lt;td&gt;temp ‘scractch pad’ for local vars and context during func call&lt;/td&gt;
      &lt;td&gt;writeable&lt;/td&gt;
      &lt;td&gt;grows &lt;strong&gt;up&lt;/strong&gt; to &lt;em&gt;lower&lt;/em&gt; memory addresses&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Here we come to an interesting aside on program execution. Program does not expect to read straight through text/code segment; control structures alter the flow through. As a program executes, EIP is set to first instruction in text. The processor then loops as follows (p69):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Read instruction at $EIP&lt;/li&gt;
  &lt;li&gt;$EIP += byte-length (at $EIP)&lt;/li&gt;
  &lt;li&gt;Execute instruction from #1&lt;/li&gt;
  &lt;li&gt;Loop back to #1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s go into more depth on the stack segment. In general, stacks are FILO (First In Last Out) data structure. Think of it like putting beads on a string, with a knot at one end (p70). Cannot get the first bead without taking the rest off. This behavior is convenient when calling functions. The outermost, or first function, has it’s context buried most deeply in the stack. As function calls return, their context is easily available. It also makes sense that this must be dynamically-sized, because function calls are dynamic. Our program may have zero or many function calls on the stack at any time.&lt;/p&gt;

&lt;p&gt;Function’s context is held in data store called &lt;em&gt;stack frame&lt;/em&gt;. EBP register, aka FP &lt;em&gt;frame pointer&lt;/em&gt; or LBP &lt;em&gt;local base pointer&lt;/em&gt;, references local function variables in current stack frame (p70). Each stack frame holds: function parameters, local variables, the saved frame pointer (SFP), and the return address. SFP restores EBP to its previous value, and return address stores EIP’s next instruction after function call (p70).&lt;/p&gt;

&lt;p&gt;And that’s all for today. We looked into memory segmentation, and defined the five segments. Tune in next time for more discussion on memory segmentation and using the heap.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day six, what what? Let’s do this. We’re moving on from variable scoping, globals, and statics. Not like way beyond, just into-the-next-city beyond.</summary></entry><entry><title type="html">Hairs of my Head</title><link href="https://lightski.github.io/hairs-of-my-head/" rel="alternate" type="text/html" title="Hairs of my Head" /><published>2017-04-30T00:00:00+00:00</published><updated>2017-04-30T00:00:00+00:00</updated><id>https://lightski.github.io/hairs-of-my-head</id><content type="html" xml:base="https://lightski.github.io/hairs-of-my-head/">&lt;p&gt;Hacking book day five. Let’s get right to it.&lt;/p&gt;

&lt;p&gt;Previously, we talked about sending input to a C program using the scanf() function. This works, but there is a more efficient way– command-line arguments. &lt;em&gt;Command-line arguments&lt;/em&gt; is a fancy way of saying &lt;em&gt;provide input as we run the program&lt;/em&gt;. C language provides command line arguments to the main function when we pass in two additional parameters: int arg_count, and char *arg_list[]. The first, arg_count, holds number of arguments passed. The second is a pointer to an array of strings where each element is an argument. The zeroth argument is always the name of the executing binary (program).&lt;/p&gt;

&lt;p&gt;It’s important to note that, in C, programmer is responsible for keeping track of memory bounds. That is, if a program tries to access memory out of it’s segments it crashes. C compiler does not automatically catch this for us. So, if you assume commmand-line arguments are there but they’re not, your program will probably crash (seg fault).&lt;/p&gt;

&lt;p&gt;Our next topic is variable scoping. In C, functions are their own context independent of anything else. So, variable declared within a function is “invisible” to main() context and other functions. This means we can have variables named the same thing, but pointing to different values, as long as they exist in different function contexts.&lt;/p&gt;

&lt;p&gt;Variables can also have global scope if they’re declared outside any function. In such case, the variable is the same throughout function contexts unless it is declared again. This can lead to confusion; best practice is to avoid using global variables.&lt;/p&gt;

&lt;p&gt;Static scope is another way variables can be declared; to do so, we prepend the keyword &lt;strong&gt;static&lt;/strong&gt;. Static variables remain intact across function calls; however, they remain local to a particular context. That is, if you call a function multiple times, the values of it’s static variables remain the same.&lt;/p&gt;

&lt;p&gt;Following example demonstrates the concepts of scope, globals, and static variables.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// j is a global variable
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// k is static variable
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// every call it increments then returns
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// i and j are local vars
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[in func 3] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[in func2] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[in func2] setting j = 1337&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1337&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// writing to global j
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;func3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[back in func2] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[in func1] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[back in func1] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// main's local i
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[in main] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[back in main] i = %d, j = %d, k = %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Compile and run&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gcc scope.c
./a.out
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And we get the following output.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[in main] i = 3, j = 42, k = 1
	[in func1] i = 5, j = 42, k = 2
		[in func2] i = 7, j = 42, k = 3
		[in func2] setting j = 1337
			[in func 3] i = 1, j = 999, k = 4
		[back in func2] i = 7, j = 1337, k = 5
	[back in func1] i = 5, j = 1337, k = 6
[back in main] i = 3, j = 1337, k = 7
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, value of i remains local to contexts. k increments every time we call the function. And, j is global except when we declare it locally.&lt;/p&gt;

&lt;p&gt;That’s a wrap for today. We discussed command line args, variable scope, globals, and static vars. Next topics are memory segmentation, the stac, and the heap.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day five. Let’s get right to it.</summary></entry><entry><title type="html">It Could Always Be Worse</title><link href="https://lightski.github.io/it-always-could-be-worse/" rel="alternate" type="text/html" title="It Could Always Be Worse" /><published>2017-04-29T00:00:00+00:00</published><updated>2017-04-29T00:00:00+00:00</updated><id>https://lightski.github.io/it-always-could-be-worse</id><content type="html" xml:base="https://lightski.github.io/it-always-could-be-worse/">&lt;p&gt;Hacking book day four (or so…just realized my numbering is incredibly inconsistent). Today there’ll be more introductory programming concepts paired with low-level architecture discussion. Let’s get started.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/five-five-five/&quot;&gt;Last discussion&lt;/a&gt; ended with pointers plus the unary operators address-of (&amp;amp;) and dereference (*). Here’s a good mnemonic for remembering their usage:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;When the unary operators are used with pointers, the address-of operator can be thought of as moving &lt;em&gt;backward&lt;/em&gt;, while the dereference operator moves &lt;em&gt;forward&lt;/em&gt; in the direction the pointer is pointing (p47; emphasis added).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next up in our C discussion: format strings. The printf() function can be used to format data for output. By adding parameters, called &lt;em&gt;format parameters&lt;/em&gt;, printf() knows to replace some part of the string with a value. Format parameters in printf() look like: %d. For each format parameter we use, we must pass an extra argument for printf() to insert into the string. For example, to print a decimal we might write:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Value of myvar is: %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myvar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here, %d is the format parameter; myvar is a variable we wish to print formatted as a decimal. Remember- myvar is just some bits in memory. C will print it out as whatever we choose, regardless of the data type we stored it as. The 1s and 0s are inherently meaningless; they get their meaning from the way they are interpreted (sort of like life).&lt;/p&gt;

&lt;p&gt;In format parameters, there is an optional numeric argument. You can specify minimum width, eg %10d. C will pad with empty space, unless your minmum space starts with a 0 eg %09d. Here is a chart of the different parameter options; the data column is the type of format of the parameter printf() expects.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Parameter&lt;/th&gt;
      &lt;th&gt;Output Type&lt;/th&gt;
      &lt;th&gt;Data&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;%d&lt;/td&gt;
      &lt;td&gt;Signed Decimal&lt;/td&gt;
      &lt;td&gt;Value&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;%u&lt;/td&gt;
      &lt;td&gt;Unsigned Decimal&lt;/td&gt;
      &lt;td&gt;Value&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;%x&lt;/td&gt;
      &lt;td&gt;Hexadecimal&lt;/td&gt;
      &lt;td&gt;Value&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;%s&lt;/td&gt;
      &lt;td&gt;String (prints chars until null byte)&lt;/td&gt;
      &lt;td&gt;Char Pointer&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;%n&lt;/td&gt;
      &lt;td&gt;Number of bytes written so far&lt;/td&gt;
      &lt;td&gt;Pointer&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;%p&lt;/td&gt;
      &lt;td&gt;Memory address (equiv to 0x%08x)&lt;/td&gt;
      &lt;td&gt;Pointer&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;These parameters are also used for reading input. Use scanf() to get values from user input. Here’s a sample showing usage of scanf()&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enter an integer: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Running this prompts the user, and then saves whatever you enter into &lt;em&gt;myint&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Writing data into, and out of, a program is foundational. I strongly agree that “Having some form of immediate feedback is fairly vital to the hacker’s learning process” (p51). Even just printing the values of a variable can be enormously helpful. The sooner you get feedback from a system, the sooner you can validate assumptions.&lt;/p&gt;

&lt;p&gt;Next big concept is type-casting. This lets us (the C compiler) interpret values as a certain data type. Typecasting syntax is: (data type) variable, eg “(int) myvar”. Having explicit types is mainly to prevent programming errors; it’s all just bits at the end of the day.&lt;/p&gt;

&lt;p&gt;We use type-casting  to save precision with division as well as using pointers for values. It also comes up when dealing with pointer types. Character data type takes up four bytes, so adding one to a character pointer increments the memory four bytes. This gives unexpected behavior when using pointers of one type to refer to memory locations of another type. So, generally we just don’t do that, but if we must we can cast the pointer to fix the size.&lt;/p&gt;

&lt;p&gt;Using pointers of the correct type is the best practice. However, at times a void pointer is desirable. Void pointers have type void; they must be typecast when dereferencing or performing pointer arithmetic. In short, void pointers just hold a memory address. We have to tell the compiler what type of address as we use it.&lt;/p&gt;

&lt;p&gt;This concludes today’s basic programming discussion. We covered C format strings and type-casting. The big idea was that memory is only bits; it’s up to the compiler to interpret correctly. The programmer guides the compiler to interpretations via data typing.&lt;/p&gt;

&lt;p&gt;Looking forward, tomorrow’s discussion will include more programming bits like command-line arguments and variable scoping. We are halfway through the book’s programming discussion. So far I am enjoying the review; the book gives great examples. The book also does a good job showing the larger context through examining program memory. Still, I’m looking forward to finishing the basics and moving on to exploiting some code.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day four (or so…just realized my numbering is incredibly inconsistent). Today there’ll be more introductory programming concepts paired with low-level architecture discussion. Let’s get started.</summary></entry><entry><title type="html">Five Five Five</title><link href="https://lightski.github.io/five-five-five/" rel="alternate" type="text/html" title="Five Five Five" /><published>2017-04-28T00:00:00+00:00</published><updated>2017-04-28T00:00:00+00:00</updated><id>https://lightski.github.io/five-five-five</id><content type="html" xml:base="https://lightski.github.io/five-five-five/">&lt;p&gt;Hacking book day 3 whoop whoop! 30 minutes to write and publish here we go. More basic programming concepts than you can shake a stick at.&lt;/p&gt;

&lt;p&gt;First up, it’s “Signed, Unsigned, Long, and Short”. These have to do with variable sizing. Signed vars can be + or -; they have a &lt;strong&gt;sign&lt;/strong&gt;. Unsigned, are only positive. All values are stored as binary, so what’s the difference? Signed values require one bit to indicate positive or negative, so they store a smaller range than unsigned. Note that signed negative values are stored as two’s complement. Storing in two’s complement gives us a nice property: negative binary plus it’s oppostive ends up being zero. So we can do subtraction with a simple adder circuit. And finally, long and short just vary the amount of memory, so they store larger or smaller values. The exact amount is architecture-dependent.&lt;/p&gt;

&lt;p&gt;Next we turn our attention to pointers. Recall that&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The EIP register is a pointer that “points” to the current insruction during a program’s execution by containing its memory address (p43).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This idea, that one area of memory points to another, is used in C as well. Moving physical memory takes time, storage space, and computation, so it’s much easier to simply pass around a pointer to memory. In C, pointers are defined using asterisk (*). The pointer type determines what type of data is pointed to. For example,&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// points to an int
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// points to char
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It’s very important to remember that these pointers hold the address (memory location), not a value. To retrieve memory contents of a variable, we dereference it using the asterisk (*) operator. In the example above, pointer is set to the memory location of variable a. To retrieve the value of a (12), we write *pointer. If we want the memory location of any variable, we simply prepend the ampersand (&amp;amp;) operator. Note that both the asterisk and ampersand notations exist in gdb as well as C.&lt;/p&gt;

&lt;p&gt;Tonight’s session was a brief overview of data scoping as well as pointers. Can’t wait to see where we go next.&lt;/p&gt;

&lt;p&gt;More to come.&lt;/p&gt;</content><author><name></name></author><summary type="html">Hacking book day 3 whoop whoop! 30 minutes to write and publish here we go. More basic programming concepts than you can shake a stick at.</summary></entry></feed>