<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://hoganrichardson.github.io/cs6447-docs/feed.xml" rel="self" type="application/atom+xml" /><link href="https://hoganrichardson.github.io/cs6447-docs/" rel="alternate" type="text/html" /><updated>2020-09-04T01:19:53+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/feed.xml</id><title type="html">cs6447</title><subtitle>System &amp; Software Security Assessment
</subtitle><author><name>Hogan Richardson</name></author><entry><title type="html">Exam Cheat Sheet</title><link href="https://hoganrichardson.github.io/cs6447-docs/revision/2019/08/21/Exam-Cheat-Sheet.html" rel="alternate" type="text/html" title="Exam Cheat Sheet" /><published>2019-08-21T00:00:00+00:00</published><updated>2019-08-21T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/revision/2019/08/21/Exam-Cheat-Sheet</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/revision/2019/08/21/Exam-Cheat-Sheet.html">&lt;h2 id=&quot;exploitation&quot;&gt;Exploitation&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://syscalls.kernelgrok.com/&quot;&gt;Linux Syscall Reference&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;shellcode&quot;&gt;Shellcode&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Launch /bin/sh&lt;/strong&gt; (no null bytes)
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25 bytes&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68732f2f&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; //sh&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6e69622f&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; /bin&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;SYS_execve&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Read/Write to FD&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;56 bytes&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; create 16 byte buffer on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x61616161&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x61616161&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x61616161&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x61616161&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; Read&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x03&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; fd&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; buf&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; count&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; Write&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x04&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&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;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;  

&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;rop&quot;&gt;ROP&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Write Primative&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At some point, you may need to write to somewhere in memory, and then reference this address (e.g. if you need a pointer to a string).&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Find a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rw&lt;/code&gt; memory segment (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;objdump -t binary | grep bss&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Find a gadget of the form &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov [e?x], e?x; ret;&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;This gadget can be used to write data from second register into the address pointer to in the first register&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;heap&quot;&gt;Heap&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Exploit Checklist&lt;/strong&gt;&lt;/p&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Use After Free&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Double Free&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Heap Buffer Overflow&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;Use of Uninitialised Memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Controlling Malloc Behaviour&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc_hook&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free_hook&lt;/code&gt; are function pointers that can be changed at runtime (i.e. if you want to use your own malloc implementation)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/* __malloc_hook */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* __free_hook */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4 id=&quot;heap-data-structures&quot;&gt;Heap Data Structures&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&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;malloc_chunk&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;INTERNAL_SIZE_T&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;mchunk_prev_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* Size of previous chunk (if free).  */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;INTERNAL_SIZE_T&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;mchunk_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;cm&quot;&gt;/* Size in bytes, including overhead. */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc_chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* double links -- used only if free. */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc_chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;cm&quot;&gt;/* Only used for large blocks: pointer to next larger size.  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc_chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd_nextsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;cm&quot;&gt;/* double links -- used only if free. */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc_chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bk_nextsize&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;/div&gt;
&lt;p&gt;&lt;br /&gt;
&lt;em&gt;Visualisation:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Note: the ‘Red’ region is the data chunk. When the chunk is allocated, the data will overwrite this region&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/cs6447-docs/assets/images/chunk-freed-CS.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Source: &lt;a href=&quot;https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/&quot;&gt;Azeria Labs&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;h2 id=&quot;reverse-engineering&quot;&gt;Reverse Engineering&lt;/h2&gt;
&lt;h3 id=&quot;data-and-control-patterns&quot;&gt;Data and Control Patterns&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Arrays &amp;amp; Structs&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The size of offset constants and register usage can provide information about the size of the data structures in the array/struct.&lt;/li&gt;
  &lt;li&gt;Structs are usually accessed via an offset into the struct (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[eax+4h]&lt;/code&gt;). The first item in the struct will just be at the address of the struct&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modular Arithmetic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Calculating the modulo from the magic number:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;m = 2&lt;sup&gt;32&lt;/sup&gt; / magic number&lt;/li&gt;
  &lt;li&gt;If the line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sar edx, arg&lt;/code&gt; exists, multiply m by 2&lt;sup&gt;arg&lt;/sup&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Examples:&lt;/em&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Modulo&lt;/th&gt;
      &lt;th&gt;Assembly&lt;/th&gt;
      &lt;th&gt;2&lt;sup&gt;32&lt;/sup&gt; / Magic Number&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;&lt;img src=&quot;/cs6447-docs/assets/images/modulo/6.png&quot; alt=&quot;&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;5.9999…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;&lt;img src=&quot;/cs6447-docs/assets/images/modulo/7.png&quot; alt=&quot;&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;1.7499…&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;&lt;img src=&quot;/cs6447-docs/assets/images/modulo/9.png&quot; alt=&quot;&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;4.4999…&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;x86-architecture&quot;&gt;x86 Architecture&lt;/h3&gt;
&lt;h4 id=&quot;registers&quot;&gt;Registers&lt;/h4&gt;
&lt;p&gt;&lt;img src=&quot;/cs6447-docs/assets/images/x86-registers.png&quot; alt=&quot;&quot; /&gt;
&lt;small&gt;Source: &lt;a href=&quot;http://flint.cs.yale.edu/cs421/papers/x86-asm/asm.html&quot;&gt;flint.cs.yale.edu/cs421/papers/x86-asm/asm.html&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h4 id=&quot;flags&quot;&gt;Flags&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Subset of relevant flags&lt;/em&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Flag&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Carry&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Parity&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Zero&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Sign&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;instructions&quot;&gt;Instructions&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Frequent, Obscure and confusing instructions&lt;/em&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Instruction&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://c9x.me/x86/html/file_module_x86_id_35.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmp&lt;/code&gt;&lt;/a&gt; a1, a2&lt;/td&gt;
      &lt;td&gt;Result = a2 - a1 (so if equal, zero flag set)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://c9x.me/x86/html/file_module_x86_id_315.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test&lt;/code&gt;&lt;/a&gt; a1, a2&lt;/td&gt;
      &lt;td&gt;a1 AND a2 (so if equal, result is NOT zero - zero flag clear). Commonly used to test whether a value is zero (since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test a, a&lt;/code&gt; sets ZF iff a = 0)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://c9x.me/x86/html/file_module_x86_id_138.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imul&lt;/code&gt;&lt;/a&gt; a1&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;Signed Multiply&lt;/em&gt;. With one operand, multiplies a1 with the value in EAX. Result is stored in EDX:EAX.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lea&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt; dereferencing&lt;/td&gt;
      &lt;td&gt;The square brackets are equivalent to dereferencing a pointer (i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;). So &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov eax, [ebx]&lt;/code&gt; will move the value pointed to by the address ebx into eax). Common arithmetic trick: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lea eax, [ebx*4]&lt;/code&gt; will move the result of the ebx*4 expression into eax.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://c9x.me/x86/html/file_module_x86_id_285.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sar&lt;/code&gt;&lt;/a&gt; a1&lt;/td&gt;
      &lt;td&gt;Part of the ‘Shift’ family. &lt;em&gt;Shift Arithmetic Right&lt;/em&gt;. SAR sets the most significant bit to correspond to the sign bit of the original value.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;hr /&gt;
&lt;h2 id=&quot;source-code-auditing&quot;&gt;Source Code Auditing&lt;/h2&gt;
&lt;h3 id=&quot;bug-classification&quot;&gt;Bug Classification&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Bad API Usage
    &lt;ul&gt;
      &lt;li&gt;Incorrect arguments
        &lt;ul&gt;
          &lt;li&gt;E.g. mixing up arguments for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset()&lt;/code&gt; resulting in writing to 0 memory locations!&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Use of Dangerous Functions
        &lt;ul&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gets()&lt;/code&gt; is just bad (buffer overflow)&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcat()&lt;/code&gt; are just bad (buffer overflow)&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strncpy()&lt;/code&gt; does not add a null terminator&lt;/li&gt;
          &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf()&lt;/code&gt; format strings&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Logic Bugs
    &lt;ul&gt;
      &lt;li&gt;Paths in the program that lead to privileged access of some kind that were unintended&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Data Types and Type ~Conversion~ Confusion
    &lt;ul&gt;
      &lt;li&gt;Integer Overflows&lt;/li&gt;
      &lt;li&gt;Implicit Type Conversions resulting in overflow/underflow&lt;/li&gt;
      &lt;li&gt;Pointer Arithmetic&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Race Conditions&lt;/li&gt;
  &lt;li&gt;Heap Vulnerabilities:
    &lt;ul&gt;
      &lt;li&gt;Use After Free&lt;/li&gt;
      &lt;li&gt;Malloc return value not checked (allocate/use null!)&lt;/li&gt;
      &lt;li&gt;Double Frees&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Exploitation Linux Syscall Reference Shellcode Launch /bin/sh (no null bytes) 25 bytes xor eax, eax push eax push 0x68732f2f ; //sh push 0x6e69622f ; /bin mov ebx, esp push eax push SYS_execve pop eax xor ecx, ecx xor edx, edx int 0x80 Read/Write to FD 56 bytes ; create 16 byte buffer on stack push 0x61616161 push 0x61616161 push 0x61616161 push 0x61616161 ; Read mov eax, 0x03 mov ebx, 1000 ; fd mov ecx, esp ; buf mov edx, 20 ; count int 0x80 ; Write mov eax, 0x04 mov ebx, 1 mov edx, 20 int 0x80 ROP Write Primative At some point, you may need to write to somewhere in memory, and then reference this address (e.g. if you need a pointer to a string). Find a rw memory segment (objdump -t binary | grep bss) Find a gadget of the form mov [e?x], e?x; ret; This gadget can be used to write data from second register into the address pointer to in the first register Heap Exploit Checklist Use After Free Double Free Heap Buffer Overflow Use of Uninitialised Memory Controlling Malloc Behaviour malloc_hook and free_hook are function pointers that can be changed at runtime (i.e. if you want to use your own malloc implementation) /* __malloc_hook */ void *function (size_t size, const void *caller) /* __free_hook */ void function (void *ptr, const void *caller) Heap Data Structures struct malloc_chunk { INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk (if free). */ INTERNAL_SIZE_T mchunk_size; /* Size in bytes, including overhead. */ struct malloc_chunk* fd; /* double links -- used only if free. */ struct malloc_chunk* bk; /* Only used for large blocks: pointer to next larger size. */ struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */ struct malloc_chunk* bk_nextsize; }; Visualisation: Note: the ‘Red’ region is the data chunk. When the chunk is allocated, the data will overwrite this region Source: Azeria Labs Reverse Engineering Data and Control Patterns Arrays &amp;amp; Structs The size of offset constants and register usage can provide information about the size of the data structures in the array/struct. Structs are usually accessed via an offset into the struct (e.g. [eax+4h]). The first item in the struct will just be at the address of the struct Modular Arithmetic Calculating the modulo from the magic number: m = 232 / magic number If the line sar edx, arg exists, multiply m by 2arg Examples: Modulo Assembly 232 / Magic Number 6 5.9999… 7 1.7499… 9 4.4999… x86 Architecture Registers Source: flint.cs.yale.edu/cs421/papers/x86-asm/asm.html Flags Subset of relevant flags Flag Purpose CF Carry PF Parity ZF Zero SF Sign Instructions Frequent, Obscure and confusing instructions Instruction Purpose cmp a1, a2 Result = a2 - a1 (so if equal, zero flag set) test a1, a2 a1 AND a2 (so if equal, result is NOT zero - zero flag clear). Commonly used to test whether a value is zero (since test a, a sets ZF iff a = 0) imul a1 Signed Multiply. With one operand, multiplies a1 with the value in EAX. Result is stored in EDX:EAX. lea &amp;amp; mov with [] dereferencing The square brackets are equivalent to dereferencing a pointer (i.e. *). So mov eax, [ebx] will move the value pointed to by the address ebx into eax). Common arithmetic trick: lea eax, [ebx*4] will move the result of the ebx*4 expression into eax. sar a1 Part of the ‘Shift’ family. Shift Arithmetic Right. SAR sets the most significant bit to correspond to the sign bit of the original value. Source Code Auditing Bug Classification Bad API Usage Incorrect arguments E.g. mixing up arguments for memset() resulting in writing to 0 memory locations! Use of Dangerous Functions gets() is just bad (buffer overflow) strcpy() and strcat() are just bad (buffer overflow) strncpy() does not add a null terminator printf() format strings Logic Bugs Paths in the program that lead to privileged access of some kind that were unintended Data Types and Type ~Conversion~ Confusion Integer Overflows Implicit Type Conversions resulting in overflow/underflow Pointer Arithmetic Race Conditions Heap Vulnerabilities: Use After Free Malloc return value not checked (allocate/use null!) Double Frees</summary></entry><entry><title type="html">Tutorial 9 Revision</title><link href="https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/30/Tutorial-9-Revision.html" rel="alternate" type="text/html" title="Tutorial 9 Revision" /><published>2019-07-30T12:00:00+00:00</published><updated>2019-07-30T12:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/30/Tutorial-9-Revision</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/30/Tutorial-9-Revision.html">&lt;h2 id=&quot;reversing-other-languages&quot;&gt;Reversing other languages&lt;/h2&gt;
&lt;h3 id=&quot;c&quot;&gt;C++&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Functions have really stupid names&lt;/li&gt;
  &lt;li&gt;Still similar to C, with prelogue and epilogue pushing/popping etc.&lt;/li&gt;
  &lt;li&gt;Still have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_pc_thunk&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Objects&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Geeks::Geeks&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;Class called ‘Geeks’, containing a funtion called ‘Geeks’&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;this&lt;/em&gt; operator
    &lt;ul&gt;
      &lt;li&gt;Pointer to a struct that has:
        &lt;ul&gt;
          &lt;li&gt;A collection of variables and function pointers&lt;/li&gt;
          &lt;li&gt;This is the current object, and the struct defines the functions/vars of this object&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Usually some register is selected, and will be used with very strange offsets
        &lt;ul&gt;
          &lt;li&gt;This register is probably &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt;&lt;/li&gt;
          &lt;li&gt;Just assume this is an argument to a function when reversing back into C&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Vectors&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;Arrays on the heap (that can be expanded using realloc)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;go&quot;&gt;Go&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Many of the functions will be error handling etc.&lt;/li&gt;
  &lt;li&gt;Try to ignore all the confusing stuff, and just identify the function calls
    &lt;ul&gt;
      &lt;li&gt;Try to work out the contents by looking for global variables and function calls&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;partial-overwrites&quot;&gt;Partial Overwrites&lt;/h2&gt;
&lt;p&gt;Function returning&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; -------------------
|   [small buffer]  | &amp;lt; esp
|                   |
|     saved ebp     | &amp;lt; ebp
|        ret        |
|-------------------|
|  prev func stack  |
|           frame   |
|                   |
|   [big buffer]    |
|                   |
|                   |
|     saved ebp     |
|        ret        |
 -------------------
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leave&lt;/code&gt; instruction:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov esp, ebp; pop ebp;&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;If we overflow the small buffer one byte into saved ebp, then esp will move into our big buffer&lt;/li&gt;
  &lt;li&gt;Now esp is pointing to our big buffer, so when that function ends, we will return to gadgets in big buffer&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Reversing other languages C++ Functions have really stupid names Still similar to C, with prelogue and epilogue pushing/popping etc. Still have get_pc_thunk</summary></entry><entry><title type="html">Lecture 9 Revision</title><link href="https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/29/Lecture-9-Revision.html" rel="alternate" type="text/html" title="Lecture 9 Revision" /><published>2019-07-29T12:00:00+00:00</published><updated>2019-07-29T12:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/29/Lecture-9-Revision</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week09/2019/07/29/Lecture-9-Revision.html">&lt;h2 id=&quot;revision&quot;&gt;REvision&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REP&lt;/code&gt; instruction (repeat). E.g. if in ecx (count), it will count down until it’s 0.
    &lt;ul&gt;
      &lt;li&gt;often used in strcpy, initialising array to all 0’s etc.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBP&lt;/code&gt; offsets
    &lt;ul&gt;
      &lt;li&gt;ESP continually changes, EBP is constant for the entire function&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Structs
    &lt;ul&gt;
      &lt;li&gt;Offsets and the way variables are used (e.g. use of 1-byte registers imply char, dereferencing vs. not dereferencing).&lt;/li&gt;
      &lt;li&gt;Char would be mov byte (not mov dword) etc.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Arrays
    &lt;ul&gt;
      &lt;li&gt;all operations on the array will be the same size (unlike structs)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Tooling:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt; (syscall tracing), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ltrace&lt;/code&gt; (library call tracing)&lt;/p&gt;

&lt;h2 id=&quot;shellcode&quot;&gt;Shellcode&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mprotect&lt;/code&gt; to change memory region permissions
    &lt;ul&gt;
      &lt;li&gt;Jump to PLT (lookup) to call functions&lt;/li&gt;
      &lt;li&gt;Use rop to mprotect, then run your own shellcode&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Avoid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'\0x0a'&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'\x00'&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Partial Return Address Overwrite&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;ASLR only really randomises the middle of the address
    &lt;ul&gt;
      &lt;li&gt;All pages start at 000, and the upper byte is usually the same too.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Since the number is stored in little endian, it’s very easy to just overwrite the lower byte
    &lt;ul&gt;
      &lt;li&gt;And the offsets will be the same for the lower few bytes, so you can work out where functions are and jump to them (relative)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;source-code-auditing&quot;&gt;Source Code Auditing&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Bad API Usage
    &lt;ul&gt;
      &lt;li&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strncpy&lt;/code&gt; if you put exactly n characters, it won’t put the null byte in.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Heap
    &lt;ul&gt;
      &lt;li&gt;Use after free, double free etc.&lt;/li&gt;
      &lt;li&gt;Custom malloc implementations&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Integer overflow/underflow
    &lt;ul&gt;
      &lt;li&gt;look at how the number is later used and what this could do&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Type Conversions&lt;/li&gt;
  &lt;li&gt;Incorrect use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sizeof()&lt;/code&gt; (returns size of pointer)&lt;/li&gt;
  &lt;li&gt;Pointer arithmetic
    &lt;ul&gt;
      &lt;li&gt;Can crop up in for loops&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Forgetting to kill after checking permissions etc.&lt;/li&gt;
  &lt;li&gt;Race conditions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;rop&quot;&gt;ROP&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Chaining functions
    &lt;ul&gt;
      &lt;li&gt;To ensure yo ucan call another function, you need a gadget such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop pop ret&lt;/code&gt; to tidy up the stack after the function call.
        &lt;ul&gt;
          &lt;li&gt;CDECL: caller does housekeeping&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Stack Pivots&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ropper &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; file &lt;span class=&quot;nt&quot;&gt;--stack-pivot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;‘Stack Pivot instructions’: changes ESP somehow
    &lt;ul&gt;
      &lt;li&gt;E.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xchng e?x, esp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add esp, x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret x&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Moving the stack frame&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">REvision</summary></entry><entry><title type="html">Tutorial 8 Heap Practical</title><link href="https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/23/Tutorial-8-Heap-Practical.html" rel="alternate" type="text/html" title="Tutorial 8 Heap Practical" /><published>2019-07-23T12:00:00+00:00</published><updated>2019-07-23T12:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/23/Tutorial-8-Heap-Practical</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/23/Tutorial-8-Heap-Practical.html">&lt;h2 id=&quot;tooling&quot;&gt;Tooling&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;pwndbg vis_heap_chunks is broken&lt;/strong&gt; unless you specify the address and length (for wargame challenges - compilationerror or something)&lt;/p&gt;

&lt;h2 id=&quot;notes-for-challenges&quot;&gt;Notes for Challenges&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;The vulnerability will be one of:
    &lt;blockquote&gt;
      &lt;ul&gt;
        &lt;li&gt;Double Free&lt;/li&gt;
        &lt;li&gt;Use after Free&lt;/li&gt;
        &lt;li&gt;Buffer Overflow&lt;/li&gt;
        &lt;li&gt;Use of uninitialised memory&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;Random chunks that weren’t allocated - probably something in libc using malloc&lt;/li&gt;
  &lt;li&gt;Fast bins never update the ‘prev in use’ bit - because it doesn’t do checks to merge. The purpose of this bit is to check whether it should merge&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Tooling</summary></entry><entry><title type="html">Lecture 8 Heap Exploitation</title><link href="https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/22/Lecture-8-Heap-Exploitation.html" rel="alternate" type="text/html" title="Lecture 8 Heap Exploitation" /><published>2019-07-22T12:00:00+00:00</published><updated>2019-07-22T12:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/22/Lecture-8-Heap-Exploitation</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week08/2019/07/22/Lecture-8-Heap-Exploitation.html">&lt;h2 id=&quot;introduction-to-heap&quot;&gt;Introduction to Heap&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Heap is extremely dependent on the implementation, OS, specific implementation details&lt;/li&gt;
  &lt;li&gt;Is a modern exploitation technique&lt;/li&gt;
  &lt;li&gt;Need to understand the structure of the heap and how it works&lt;/li&gt;
  &lt;li&gt;Heap exploitation is basically mixing data and control&lt;/li&gt;
  &lt;li&gt;Harder to identify
    &lt;ul&gt;
      &lt;li&gt;called malloc in certian order, or called free twice etc.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Attacking the implentation of the heap, not programmers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-is-the-heap&quot;&gt;What is the Heap?&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;OS provides memory (through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; API) in multiples of 0x1000&lt;/li&gt;
  &lt;li&gt;Different libraries, OS etc. use different heap management implementations&lt;/li&gt;
  &lt;li&gt;Heap exploitation is &lt;strong&gt;hard&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;You can’t just ‘guess and check’.&lt;/li&gt;
      &lt;li&gt;need to understand the program to solve any heap challenge&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;malloc-implentations&quot;&gt;Malloc Implentations&lt;/h2&gt;
&lt;p&gt;There are several different malloc implementations&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dlmalloc&lt;/code&gt;: general purpose, no threads, very old&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ptmalloc2&lt;/code&gt; (glibc): fast for single-threaded applications, and fast for very small allocations. Also supports multithreading&lt;/li&gt;
  &lt;li&gt;Chrome and Firefox have their own malloc implentations (tcmalloc, jemalloc) for more specialised
    &lt;ul&gt;
      &lt;li&gt;There is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc_hook&lt;/code&gt; function (ptr) that allows you to specify your own memory allocator&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;how-does-malloc-work&quot;&gt;How does Malloc Work?&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Dynamic memory allocator&lt;/li&gt;
  &lt;li&gt;Allocates a large chunk of memory (through a syscall), and then
    &lt;ul&gt;
      &lt;li&gt;returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;struct malloc_chunk&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;‘First Fit’&lt;/em&gt; allocator (usually)
    &lt;ul&gt;
      &lt;li&gt;What’s to return the first chunk that will fulfill the request&lt;/li&gt;
      &lt;li&gt;Each chunk stores metadata on either side of it (see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;struct malloc_chunk&lt;/code&gt; contents)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Maintains a ‘free’ list so it can quickly jump through free blocks&lt;/li&gt;
  &lt;li&gt;Some fields of the struct are only used if the chunk is free&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt; includes the metadata&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fd&lt;/code&gt; is what is returned to the malloc user (and the following addresses are in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bk&lt;/code&gt;). But if the block is free, it is a pointer to the next chunk.&lt;/li&gt;
  &lt;li&gt;On the first allocation, a large chunk is allocated. The first n bytes are the requested bytes from malloc, and the remaining region is the &lt;em&gt;top chunk&lt;/em&gt;. We then break off the top of the &lt;em&gt;top chunk&lt;/em&gt; each time we need more.&lt;/li&gt;
  &lt;li&gt;Global variable in libc called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main_arena&lt;/code&gt; describes the heap&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;how-does-free-work&quot;&gt;How does Free work?&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Free needs to be fast. (Fast Code = low security)&lt;/li&gt;
  &lt;li&gt;The free chunks are divided into different sizes
    &lt;ul&gt;
      &lt;li&gt;and within the size classes, the chunks are sorted into bins (bucket sort)&lt;/li&gt;
      &lt;li&gt;So essentially an O(1) to return a chunk of a certain size&lt;/li&gt;
      &lt;li&gt;Bins are arrays of linked lists of chunks&lt;/li&gt;
      &lt;li&gt;The nodes in the linked lists are &lt;em&gt;old chunks&lt;/em&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Fast Bins&lt;/em&gt; are multiples of 8.
    &lt;ul&gt;
      &lt;li&gt;Array of linked lists of chunks of certian sizes&lt;/li&gt;
      &lt;li&gt;Always take first off the list - so O(1) retrieval&lt;/li&gt;
      &lt;li&gt;Never merge bins&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Small Bins&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;Faster than large bins&lt;/li&gt;
      &lt;li&gt;Each bin maintains a doubly linked list (FIFO)&lt;/li&gt;
      &lt;li&gt;Each bin has chunks of same size.&lt;/li&gt;
      &lt;li&gt;There are more small bins&lt;/li&gt;
      &lt;li&gt;When you free two chunks adjacent to each other: then the chunks are coalesced into one larger chunk&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Unsorted Bin&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;Initially a free chunk is put into an unsorted bin&lt;/li&gt;
      &lt;li&gt;When a new chunk is allocated, it loops through this list and sorts the chunks until the correct size is found.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;tcache&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;Very simliar to fast bins, except they removed all the security checks&lt;/li&gt;
      &lt;li&gt;The only check it does is to ensure that there aren’t already max chunks in the tcache.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/&quot;&gt;Reference: Azeria Labs&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;exploits&quot;&gt;Exploits&lt;/h2&gt;

&lt;p&gt;It’s hard to exploit a program with only one of these vulnerabilities. You usually need a few to perform exploitation.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Use after free
    &lt;ul&gt;
      &lt;li&gt;You can overwrite the forward pointer, and corrupt the free list struture&lt;/li&gt;
      &lt;li&gt;Can also provide leaks&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Double free
    &lt;ul&gt;
      &lt;li&gt;In fastbins, it will check that the free request is not the first item in the free list already (tcache doesn’t). To get double free, need to have something in between the frees so that the check passes&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Leaking with small chunks
    &lt;ul&gt;
      &lt;li&gt;The small bins point back into the main arena (in libc).
        &lt;ul&gt;
          &lt;li&gt;(The first element’s backward pointer points into libc)&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;If you can print the first thing in smallbin list, you have an address leak into libc!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Forging chunks
    &lt;ul&gt;
      &lt;li&gt;Corrupt the free structure to forge a chunk wherever you like in memory&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Heap Spray&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Usually the goal is to get two chunks to overlap and overwrite data in another chunk.&lt;/p&gt;

&lt;p&gt;ROP is hard on with heap (because you can’t control the stack), so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;one_gadget&lt;/code&gt; is the saviour.&lt;/p&gt;

&lt;h2 id=&quot;pwndbg-commands&quot;&gt;pwndbg Commands&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Shows all chunks allocated, and their types&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bins&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Shows type of bins&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vis_heap_chunks&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Visualise the heap chunks&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;need-to-make-sure-using-the-same-libc-library&quot;&gt;Need to make sure using the same libc library&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LD_PRELOAD&lt;/code&gt; allows you to force a program to use a different libc version&lt;/li&gt;
  &lt;li&gt;Need to do this in case the algorithms are different in different versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;suggested-reading&quot;&gt;Suggested Reading&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://phrack.org/issues/57/9.html&quot;&gt;Once Upon a Free &lt;em&gt;(Phrack, 2001)&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;&quot;&gt;Attacking Javascript Engines &lt;em&gt;(Phrack)&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Introduction to Heap Heap is extremely dependent on the implementation, OS, specific implementation details Is a modern exploitation technique Need to understand the structure of the heap and how it works Heap exploitation is basically mixing data and control Harder to identify called malloc in certian order, or called free twice etc. Attacking the implentation of the heap, not programmers</summary></entry><entry><title type="html">Lecture 7 Rop</title><link href="https://hoganrichardson.github.io/cs6447-docs/week07/2019/07/15/Lecture-7-ROP.html" rel="alternate" type="text/html" title="Lecture 7 Rop" /><published>2019-07-15T00:00:00+00:00</published><updated>2019-07-15T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week07/2019/07/15/Lecture-7-ROP</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week07/2019/07/15/Lecture-7-ROP.html">&lt;h2 id=&quot;return-oriented-programming&quot;&gt;Return Oriented Programming&lt;/h2&gt;
&lt;h3 id=&quot;using-buffer-overflow-to-call-2-functions&quot;&gt;Using Buffer Overflow to call 2 functions&lt;/h3&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ----------------
|                |
|                |
|                |
|                | f2 arg2
|   f1 arg 2     | f2 arg 1
|   f1 arg 1     | Expected return for f2
|return addr f1()| &amp;lt;- manipulate to be f2
|address of f1() | &amp;lt;- original return address of overflow function,
|AAAAAAAAAAAAAAAA|                 now overwritten with address of f()
|AAAAAAAAAAAAAAAA|
|AAAAAAAAAAAAAAAA|
|      ...       |
|                |
 ----------------

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

&lt;ul&gt;
  &lt;li&gt;We can’t do it and preserve arguments&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;where-can-we-redirect-execution-when-other-protections-in-place&quot;&gt;Where can we redirect execution (when other protections in place)?&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Static Libraries&lt;/li&gt;
  &lt;li&gt;Lib&lt;/li&gt;
  &lt;li&gt;Pops the last item off the stack and jumps to eip&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;rop-gadgets&quot;&gt;ROP Gadgets&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Find a few instructions followed by ret somewhere in an executable region&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;pop
pop
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;Some instuctions can be found by changing the offset on bytes (so it will treat the bytes differently).
    &lt;ul&gt;
      &lt;li&gt;In GDB x/12i 0xdeadbeef will display memory interpreted as assembly&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;We can then jump to this to perform desired actions (e.g. to push the appropriate arguments onto the stack to call two functions at once)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;finding-gadgets&quot;&gt;Finding Gadgets&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Pwntools
    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ELF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'./mybinary'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;gadget&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROPgadget&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROPPER&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;N.B. Don’t use automated tools blindly to generate payloads! Use them to find instructions&lt;/p&gt;

&lt;h3 id=&quot;ret-to-libc&quot;&gt;RET to Libc&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;In some programs (e.g. small programs), it’s hard to find the instructions you need.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libc&lt;/code&gt; must be somewhere on the machine, so all libraries are there (we just don’t know where they are)
    &lt;ul&gt;
      &lt;li&gt;we can use gadgets within libc in exactly the same way as using gadgets within the program&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Where&lt;/em&gt; is libc? (in memory - what’s the offset etc.)
    &lt;ul&gt;
      &lt;li&gt;GOT table: leak libc address.&lt;/li&gt;
      &lt;li&gt;How do you leak??
        &lt;pre&gt;&lt;code class=&quot;language-C&quot;&gt;puts(*puts);
&lt;/code&gt;&lt;/pre&gt;
      &lt;/li&gt;
      &lt;li&gt;Execute puts with puts got offset and it will print the libc address of puts&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;What&lt;/em&gt; version of libc?
    &lt;ul&gt;
      &lt;li&gt;Leak the address: and then calculate offset to desired libc function (or gadget)&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://libcdb.com&quot;&gt;Libc DB&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Extensions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;ROP chains can become very large, and you may not be able to fit them all in your buffer overflow&lt;/li&gt;
  &lt;li&gt;Can move stack somewhere else that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-wx&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Use partial/complete overwrite of esp to move stack&lt;/li&gt;
  &lt;li&gt;Use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; sled and jump back to your own buffer (make sure you return to a 4-byte aligned address)&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;one gadget&lt;/em&gt; is a series of instructions that result in a shell (usually requires some registers to be set up correctly beforehand)
    &lt;ul&gt;
      &lt;li&gt;useful if you have limited space or only space to execute one gadget&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;one_gadget&lt;/code&gt; program will find them in libc&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Return Oriented Programming Using Buffer Overflow to call 2 functions ``` —————- | | | | | | | | f2 arg2 | f1 arg 2 | f2 arg 1 | f1 arg 1 | Expected return for f2 |return addr f1()| &amp;lt;- manipulate to be f2 |address of f1() | &amp;lt;- original return address of overflow function, |AAAAAAAAAAAAAAAA| now overwritten with address of f() |AAAAAAAAAAAAAAAA| |AAAAAAAAAAAAAAAA| | … | | | —————-</summary></entry><entry><title type="html">Midsem Exam Recap</title><link href="https://hoganrichardson.github.io/cs6447-docs/week06/2019/07/09/MidSem-Exam-recap.html" rel="alternate" type="text/html" title="Midsem Exam Recap" /><published>2019-07-09T00:00:00+00:00</published><updated>2019-07-09T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week06/2019/07/09/MidSem-Exam-recap</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week06/2019/07/09/MidSem-Exam-recap.html">&lt;h2 id=&quot;mid-sem-summary&quot;&gt;Mid-Sem Summary&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Finding Function Locations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;For non-PIE binaries, can get address of functions using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;objdump&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;objdump &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; bunary | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;func
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or alternatively, with pwntools:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symbols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;func&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Avoid Null-bytes in Shellcode&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov eax, SYS_execve&lt;/code&gt;, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov al, SYS_execve&lt;/code&gt; (there iwll be no null padding)&lt;/li&gt;
  &lt;li&gt;pwntools &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shellcraft&lt;/code&gt; tries to avoid nulls and newlines (however can be long)&lt;/li&gt;
  &lt;li&gt;Sometimes payload needs to be only ASCII&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;My Shellcode&lt;/em&gt; (created post-exam)&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;    xor eax, eax
    push eax        
    push 0x68732f2f  
    push 0x6e69622f  
    mov ebx, esp 
    push eax 

    push SYS_execve
    pop eax
    xor ecx, ecx
    xor edx, edx

    int 0x80
&lt;/code&gt;&lt;/pre&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Mid-Sem Summary</summary></entry><entry><title type="html">Lecture 5 Rootkits</title><link href="https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Rootkits.html" rel="alternate" type="text/html" title="Lecture 5 Rootkits" /><published>2019-07-01T00:00:00+00:00</published><updated>2019-07-01T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Rootkits</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Rootkits.html">&lt;h2 id=&quot;what-is-a-rootkit&quot;&gt;What is a Rootkit?&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Achieve root access on a system&lt;/li&gt;
  &lt;li&gt;May want to persist&lt;/li&gt;
  &lt;li&gt;Aim to hide presence to the system user&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;userland-kits&quot;&gt;Userland Kits&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Changing widely used programs (e.g. ls)&lt;/li&gt;
  &lt;li&gt;often to hide other aspects&lt;/li&gt;
  &lt;li&gt;infect some process and run from them&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;kernel-kits&quot;&gt;Kernel Kits&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Hooking&lt;/strong&gt; Redirect code execution to your own code&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;type-1&quot;&gt;Type 1&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Syscall Table&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Array of function pointers (syscall id is an index into this table)&lt;/li&gt;
  &lt;li&gt;Can easily replace syscalls to perform extra checks and run your own code on calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interrupt Descriptor Table&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Used for handling different types of interrupts&lt;/li&gt;
  &lt;li&gt;Div0 handler, pagefault handler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changing these are simple, and easy to detect&lt;/p&gt;

&lt;h3 id=&quot;type-2&quot;&gt;Type 2&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Manipulate internal kernel data structures&lt;/li&gt;
  &lt;li&gt;Hook &lt;em&gt;special&lt;/em&gt; files (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/proc&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev&lt;/code&gt;) - these call functions when read (and they are dynamic)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;detection&quot;&gt;Detection&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Userland: signature everything&lt;/li&gt;
  &lt;li&gt;Kernel: check addresses are in sane locations
    &lt;ul&gt;
      &lt;li&gt;Check data structures vs what a process tells you about something (e.g. files in dir)&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tasklist&lt;/code&gt;: in linux, every new process will have one, so the rootkit will have one of these
        &lt;ul&gt;
          &lt;li&gt;Look for things that the rootkit author can’t destroy&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">What is a Rootkit?</summary></entry><entry><title type="html">Lecture 5 Source Code Auditing</title><link href="https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Source-Code-Auditing.html" rel="alternate" type="text/html" title="Lecture 5 Source Code Auditing" /><published>2019-07-01T00:00:00+00:00</published><updated>2019-07-01T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Source-Code-Auditing</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week05/2019/07/01/Lecture-5-Source-Code-Auditing.html">&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Much easier to find bugs when reading C code (not assembly)&lt;/li&gt;
  &lt;li&gt;Need to understand the purpose of the program in order to find the bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;classifications-of-bugs&quot;&gt;Classifications of Bugs&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Bad API usage&lt;/li&gt;
  &lt;li&gt;Logic Bugs&lt;/li&gt;
  &lt;li&gt;Integer overflows
    &lt;ul&gt;
      &lt;li&gt;Most real-world buffer overflows are caused by overflow in buff size calculations&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Type &lt;del&gt;Conversion&lt;/del&gt; Confusion (can lead to overflow)&lt;/li&gt;
  &lt;li&gt;Operators&lt;/li&gt;
  &lt;li&gt;Pointer Aithmetic&lt;/li&gt;
  &lt;li&gt;Race conditions&lt;/li&gt;
  &lt;li&gt;Heap Overflows:
    &lt;ul&gt;
      &lt;li&gt;Use After Free&lt;/li&gt;
      &lt;li&gt;Failed malloc not checked (can allocate null, so that it gets dereferenced and used!)&lt;/li&gt;
      &lt;li&gt;Double Frees&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many of these are down to programmer error&lt;/p&gt;

&lt;h3 id=&quot;dangerous-functions&quot;&gt;Dangerous Functions&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gets()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf()&lt;/code&gt; when misused&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strncpy()&lt;/code&gt; - doesn’t add null terminator&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memset()&lt;/code&gt; - people mix up the arguments!&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Motivation</summary></entry><entry><title type="html">Lecture 4 Format Strings</title><link href="https://hoganrichardson.github.io/cs6447-docs/week04/2019/06/24/Lecture-4-Format-Strings.html" rel="alternate" type="text/html" title="Lecture 4 Format Strings" /><published>2019-06-24T00:00:00+00:00</published><updated>2019-06-24T00:00:00+00:00</updated><id>https://hoganrichardson.github.io/cs6447-docs/week04/2019/06/24/Lecture-4-Format-Strings</id><content type="html" xml:base="https://hoganrichardson.github.io/cs6447-docs/week04/2019/06/24/Lecture-4-Format-Strings.html">&lt;h2 id=&quot;format-strings&quot;&gt;Format Strings&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;The format string specifies how subsequent arguments are converted to output&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;~ Man Page&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%n&lt;/code&gt;: Writes number of characters written so far to the argument (i.e. not to the output stream)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;To avoid null byte problem, put the data at the end, and ensure all format arguments are at the beginning (so that they get executed before the printing terminates)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-is-libc&quot;&gt;Where is libc?&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;PLT&lt;/strong&gt;: Process Linkage Table&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Call external functions whose addresses are loaded in dynamically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GOT&lt;/strong&gt;: Global Offset Table&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;‘Refernce Table’ for where various libc functions can be found&lt;/li&gt;
  &lt;li&gt;Initially, entirely populated with pointers back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PLT&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Jump to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ld.so&lt;/code&gt; (Dynamic linker/loader), which then finds and loads the required function&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;We can manipulate the GOT to point to our own functions instead of library functions
    &lt;ul&gt;
      &lt;li&gt;GOT should not be writable, but it is (because things aren’t linked at launch, they are dynamically linked at runtime).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Hogan Richardson</name></author><summary type="html">Format Strings</summary></entry></feed>